Code Monkey home page Code Monkey logo

Comments (6)

mattsse avatar mattsse commented on July 30, 2024

Unable to answer this unfortunately, could you please phrase this in English?

from core.

mingtianquan avatar mingtianquan commented on July 30, 2024

How to use allocation to implement a function written in the ethers library? Given abi, for example, the string transfer (address, uint256) passes the parameter string

from core.

DaniPopes avatar DaniPopes commented on July 30, 2024

You can parse a signature string to alloy_json_abi::Function with Function::parse and then use alloy_dyn_abi::JsonAbiExt to encode data https://docs.rs/alloy-dyn-abi/latest/alloy_dyn_abi/trait.JsonAbiExt.html

For parsing values you can use alloy_dyn_abi::DynSolType::coerce_str

from core.

DaniPopes avatar DaniPopes commented on July 30, 2024

Here's an example with your data

fn parse_and_encode(function: &str, values: &str) -> Vec<u8> {
    let function = Function::parse(function).unwrap();
    let values = values
        .split(',')
        .zip(&function.inputs)
        .map(|(value, param)| param.resolve().unwrap().coerce_str(value).unwrap())
        .collect::<Vec<_>>();
    function.abi_encode_input(&values).unwrap()
}

#[test]
fn test_parse_and_encode() {
    let function = "transfer(address,uint256)";
    let values = "0x9Cf5D791047dC7323c7c362cd60a94f8f14ec067,1000000000000000000";
    let output = parse_and_encode(function, values);
    assert_eq!(hex::encode_prefixed(output), "0xa9059cbb0000000000000000000000009cf5d791047dc7323c7c362cd60a94f8f14ec0670000000000000000000000000000000000000000000000000de0b6b3a7640000");
}

from core.

mingtianquan avatar mingtianquan commented on July 30, 2024

If there is an array in the parameters, for example
let function = "swapExactETHForTokens(uint256,address[],address,uint256)";
let values = "12000000000000000000,0x5b7109dd5F3cD041546e6c49A08f8d5D738A32cf|0x0A8Cd01d2ffe74c90c9f2155fb8f36CeB17fFa0C,0x61d8f439cC8dD92E81fe57F70Ef1A042D5CC5692,1663409025";
Running this way will result in an error

from core.

DaniPopes avatar DaniPopes commented on July 30, 2024

You have to write your own parser if coerce_str doesn't work for you, since I don't know what the format of "values" is. All the functions you need otherwise are in the example above

from core.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.