gakonst / ethers-rs

Complete Ethereum & Celo library and wallet implementation in Rust. https://docs.rs/ethers
Apache License 2.0
2.5k stars 796 forks source link

how to use ethers::contract::encode_function_data #2537

Open SixoneKui opened 1 year ago

SixoneKui commented 1 year ago

Please post your question as a discussion in Telegram: https://t.me/ethers_rs Hi, I would like to encode like how I did with ethers.js: let codedData = iface.encodeFunctionData("function name", [ parameters ]); I am confused with ethers::contract::encode_function_data. There is only one sentence in the doc Could you provide some sample codes.

many thanks

dospore commented 1 year ago

You might be looking for contract.encode You can do something like this.

let arg1 = H160::from_str("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee").unwrap();
let encoded = some_contract.encode("someFunc", [
    Token::Address(arg1)
]);

You can see reference to other args. There might be better ways to do this as well, i'm a new rustacean.

If you dont have a contract instance you should be able to get the Function and call encode_input

1500256797 commented 1 year ago

how to use encode_function_data, i also meet this error .

here is my code

    let binding = contract.exact_input_single(params.clone());
    let function = binding.function;
    let u256_fee = U256::from(params.fee);
    let u256_sqrt_price_limit_x96 = U256::from(params.sqrt_price_limit_x96);
    let tokenize_arg = (
        Token::Address(params.token_in),
        Token::Address(params.token_out),
        Token::Uint(u256_fee),
        Token::Address(params.recipient),
        Token::Uint(params.amount_in),
        Token::Uint(params.amount_out_minimum),
        Token::Uint(u256_sqrt_price_limit_x96),
    );
    let bytes = encode_function_data(&function, tokenize_arg).unwrap();
    println!("bytes is {:?}", bytes);

this is my Function

Function { name: "exactInputSingle", inputs: [Param { name: "params", kind: Tuple([Address, Address, Uint(24), Address, Uint(256), Uint(256), Uint(160)]), internal_type: Some("struct IV3SwapRouter.ExactInputSingleParams") }], outputs: [Param { name: "amountOut", kind: Uint(256), internal_type: Some("uint256") }], constant: None, state_mutability: Payable }

this is error

panicked at 'called `Result::unwrap()` on an `Err` value: DecodingError(InvalidData)', src/util/pancakev3.rs:158:63
stack backtrace:
   0: rust_begin_unwind
             at /rustc/d5c2e9c342b358556da91d61ed4133f6f50fc0c3/library/std/src/panicking.rs:593:5
   1: core::panicking::panic_fmt
             at /rustc/d5c2e9c342b358556da91d61ed4133f6f50fc0c3/library/core/src/panicking.rs:67:14
   2: core::result::unwrap_failed
             at /rustc/d5c2e9c342b358556da91d61ed4133f6f50fc0c3/library/core/src/result.rs:1651:5
   3: core::result::Result<T,E>::unwrap
             at /rustc/d5c2e9c342b358556da91d61ed4133f6f50fc0c3/library/core/src/result.rs:1076:23
   4: rust_hello::util::pancakev3::test_exact_swap::{{closure}}
             at ./src/util/pancakev3.rs:158:17
   5: <core::pin::Pin<P> as core::future::future::Future>::poll
             at /rustc/d5c2e9c342b358556da91d61ed4133f6f50fc0c3/library/core/src/future/future.rs:125:9
   6: <core::pin::Pin<P> as core::future::future::Future>::poll
             at /rustc/d5c2e9c342b358556da91d61ed4133f6f50fc0c3/library/core/src/future/future.rs:125:9
   7: tokio::runtime::scheduler::current_thread::CoreGuard::block_on::{{closure}}::{{closure}}::{{closure}}
             at /Users/ouhuang/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.32.0/src/runtime/scheduler/current_thread/mod.rs:665:57
   8: tokio::runtime::coop::with_budget
             at /Users/ouhuang/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.32.0/src/runtime/coop.rs:107:5
   9: tokio::runtime::coop::budget
             at /Users/ouhuang/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.32.0/src/runtime/coop.rs:73:5
  10: tokio::runtime::scheduler::current_thread::CoreGuard::block_on::{{closure}}::{{closure}}
             at /Users/ouhuang/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.32.0/src/runtime/scheduler/current_thread/mod.rs:665:25
  11: tokio::runtime::scheduler::current_thread::Context::enter
             at /Users/ouhuang/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.32.0/src/runtime/scheduler/current_thread/mod.rs:410:19
  12: tokio::runtime::scheduler::current_thread::CoreGuard::block_on::{{closure}}
             at /Users/ouhuang/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.32.0/src/runtime/scheduler/current_thread/mod.rs:664:36
  13: tokio::runtime::scheduler::current_thread::CoreGuard::enter::{{closure}}
             at /Users/ouhuang/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.32.0/src/runtime/scheduler/current_thread/mod.rs:743:68
  14: tokio::runtime::context::scoped::Scoped<T>::set
             at /Users/ouhuang/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.32.0/src/runtime/context/scoped.rs:40:9
  15: tokio::runtime::context::set_scheduler::{{closure}}
             at /Users/ouhuang/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.32.0/src/runtime/context.rs:176:26
  16: std::thread::local::LocalKey<T>::try_with
             at /rustc/d5c2e9c342b358556da91d61ed4133f6f50fc0c3/library/std/src/thread/local.rs:270:16
  17: std::thread::local::LocalKey<T>::with
             at /rustc/d5c2e9c342b358556da91d61ed4133f6f50fc0c3/library/std/src/thread/local.rs:246:9
  18: tokio::runtime::context::set_scheduler
             at /Users/ouhuang/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.32.0/src/runtime/context.rs:176:9
  19: tokio::runtime::scheduler::current_thread::CoreGuard::enter
             at /Users/ouhuang/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.32.0/src/runtime/scheduler/current_thread/mod.rs:743:27
  20: tokio::runtime::scheduler::current_thread::CoreGuard::block_on
             at /Users/ouhuang/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.32.0/src/runtime/scheduler/current_thread/mod.rs:652:19
  21: tokio::runtime::scheduler::current_thread::CurrentThread::block_on::{{closure}}
             at /Users/ouhuang/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.32.0/src/runtime/scheduler/current_thread/mod.rs:175:28
  22: tokio::runtime::context::runtime::enter_runtime
             at /Users/ouhuang/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.32.0/src/runtime/context/runtime.rs:65:16
  23: tokio::runtime::scheduler::current_thread::CurrentThread::block_on
             at /Users/ouhuang/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.32.0/src/runtime/scheduler/current_thread/mod.rs:167:9
  24: tokio::runtime::runtime::Runtime::block_on
             at /Users/ouhuang/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.32.0/src/runtime/runtime.rs:347:47
  25: rust_hello::util::pancakev3::test_exact_swap
             at ./src/util/pancakev3.rs:161:5
  26: rust_hello::util::pancakev3::test_exact_swap::{{closure}}
             at ./src/util/pancakev3.rs:110:35
  27: core::ops::function::FnOnce::call_once
             at /rustc/d5c2e9c342b358556da91d61ed4133f6f50fc0c3/library/core/src/ops/function.rs:250:5
  28: core::ops::function::FnOnce::call_once
             at /rustc/d5c2e9c342b358556da91d61ed4133f6f50fc0c3/library/core/src/ops/function.rs:250:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.