Coder Social home page Coder Social logo

rust-ethereum-abi's People

Contributors

dependabot-preview[bot] avatar dependabot[bot] avatar feliperosa avatar mercuricchloride avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

rust-ethereum-abi's Issues

web3::types::H256 with decode_log_from_slice function

Hello!

I was happy to find this crate, because I thought I can decode the response from the emitted events, but I encountered some problems.
First of all the from_reader function is removed in the 4th version of the library but the tutorial still contains it. It would be good to have an easy to read in ABI function. For example I created this for myself:

pub fn read_abi_from_file(p: &str) -> Result<Abi, anyhow::Error> {
    // Open the file in read-only mode with buffer.
    let path = Path::new(&p);
    let file = File::open(path)?;
    let reader = BufReader::new(file);

    // Read the JSON contains the ABI
    let abi = serde_json::from_reader(reader)?;

    // Return the `User`.
    Ok(abi)
}

But the bigger problem just came later. When I tried to use decode_log_from_slice function. I'm using web3 to listen for the event stream and it works great, but its getting a Vec and this library neads &[H256]. Okay I checked the compiler and told me I need primitive_types::H256. Still wrote me a weird error message:

= note: expected reference `&[primitive_types::H256]` (struct `primitive_types::H256`)
              found reference `&[primitive_types::H256]` (struct `primitive_types::H256`)

Okay I checked the library's toml file and I found out it used ethereum_types, so I just copied and put into my toml. Did the weird type conversation like:

let topic0 = log.clone().unwrap().topics[0].to_fixed_bytes();
            let topic1 = log.clone().unwrap().topics[1].to_fixed_bytes();
            let topic2 = log.clone().unwrap().topics[2].to_fixed_bytes();
            let topics: &[ethereum_types::H256] = &[
                ethereum_types::H256::from_slice(&topic0),
                ethereum_types::H256::from_slice(&topic1),
                ethereum_types::H256::from_slice(&topic2),
            ];

And I got the error again:

= note: expected reference `&[primitive_types::H256]`
              found reference `&[ethereum_types::H256]` 

I would suggest to have a new function what can handle web3 types for the topics Vec and also the Bytes one
https://docs.rs/web3/0.18.0/web3/types/struct.Log.html

If that would be implemented decoding the events would be so easy. Thank you if you considering it.

`ethereum_abi::abi::Abi::decode_input_from_slice` panic and crash program

decode_input_from_slice sometimes crah

My code is the following

let (function, decoded_params) =
		match DEPOSIT_CONTRACT_ABI.decode_input_from_slice(&transaction.input.0) {
			Ok(d) => d,
			Err(_) => return Ok(()),
		};

Here is the backtrace:

thread 'main' panicked at 'range end index 4 out of range for slice of length 0', library/core/src/slice/index.rs:73:5
stack backtrace:
   0: rust_begin_unwind
             at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/std/src/panicking.rs:584:5
   1: core::panicking::panic_fmt
             at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/core/src/panicking.rs:143:14
   2: core::slice::index::slice_end_index_len_fail_rt
             at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/core/src/slice/index.rs:73:5
   3: core::ops::function::FnOnce::call_once
             at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/core/src/ops/function.rs:227:5
   4: core::intrinsics::const_eval_select
             at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/core/src/intrinsics.rs:2361:5
   5: core::slice::index::slice_end_index_len_fail
             at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/core/src/slice/index.rs:67:9
   6: ethereum_abi::abi::Abi::decode_input_from_slice
   7: <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll
   8: <futures_util::future::try_maybe_done::TryMaybeDone<Fut> as core::future::future::Future>::poll
   9: <futures_util::future::try_join_all::TryJoinAll<F> as core::future::future::Future>::poll
  10: <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll
  11: <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll
  12: <tokio::future::poll_fn::PollFn<F> as core::future::future::Future>::poll
  13: eth_parser::main::{{closure}}
  14: std::thread::local::LocalKey<T>::with
  15: tokio::park::thread::CachedParkThread::block_on
  16: tokio::runtime::thread_pool::ThreadPool::block_on
  17: tokio::runtime::Runtime::block_on
  18: eth_parser::main
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

Full trace:

thread 'main' panicked at 'range end index 4 out of range for slice of length 0', library/core/src/slice/index.rs:73:5
stack backtrace:
   0:     0x55e80b54d27d - std::backtrace_rs::backtrace::libunwind::trace::h22893a5306c091b4
                               at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/std/src/../../backtrace/src/backtrace/libunwind.rs:93:5
   1:     0x55e80b54d27d - std::backtrace_rs::backtrace::trace_unsynchronized::h29c3bc6f9e91819d
                               at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:     0x55e80b54d27d - std::sys_common::backtrace::_print_fmt::he497d8a0ec903793
                               at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/std/src/sys_common/backtrace.rs:66:5
   3:     0x55e80b54d27d - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::h9c2a9d2774d81873
                               at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/std/src/sys_common/backtrace.rs:45:22
   4:     0x55e80b56f29c - core::fmt::write::hba4337c43d992f49
                               at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/core/src/fmt/mod.rs:1194:17
   5:     0x55e80b547d11 - std::io::Write::write_fmt::heb73de6e02cfabed
                               at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/std/src/io/mod.rs:1655:15
   6:     0x55e80b54eda5 - std::sys_common::backtrace::_print::h63c8b24acdd8e8ce
                               at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/std/src/sys_common/backtrace.rs:48:5
   7:     0x55e80b54eda5 - std::sys_common::backtrace::print::h426700d6240cdcc2
                               at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/std/src/sys_common/backtrace.rs:35:9
   8:     0x55e80b54eda5 - std::panicking::default_hook::{{closure}}::hc9a76eed0b18f82b
                               at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/std/src/panicking.rs:295:22
   9:     0x55e80b54ea59 - std::panicking::default_hook::h2e88d02087fae196
                               at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/std/src/panicking.rs:314:9
  10:     0x55e80b54f2f2 - std::panicking::rust_panic_with_hook::habfdcc2e90f9fd4c
                               at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/std/src/panicking.rs:698:17
  11:     0x55e80b54f1d7 - std::panicking::begin_panic_handler::{{closure}}::he054b2a83a51d2cd
                               at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/std/src/panicking.rs:588:13
  12:     0x55e80b54d734 - std::sys_common::backtrace::__rust_end_short_backtrace::ha48b94ab49b30915
                               at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/std/src/sys_common/backtrace.rs:138:18
  13:     0x55e80b54ef09 - rust_begin_unwind
                               at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/std/src/panicking.rs:584:5
  14:     0x55e80afb3fc3 - core::panicking::panic_fmt::h366d3a309ae17c94
                               at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/core/src/panicking.rs:143:14
  15:     0x55e80b571076 - core::slice::index::slice_end_index_len_fail_rt::hab01086a1430d984
                               at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/core/src/slice/index.rs:73:5
  16:     0x55e80b568c77 - core::ops::function::FnOnce::call_once::h02950ad71da619df
                               at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/core/src/ops/function.rs:227:5
  17:     0x55e80b56d146 - core::intrinsics::const_eval_select::h73e7b6f162eb47b3
                               at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/core/src/intrinsics.rs:2361:5
  18:     0x55e80afb40d6 - core::slice::index::slice_end_index_len_fail::h2f0744292281ab75
                               at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/core/src/slice/index.rs:67:9
  19:     0x55e80b07bfaf - ethereum_abi::abi::Abi::decode_input_from_slice::h69ff843441a587e0
  20:     0x55e80b061c9c - <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll::hfc6e5399539d72eb
  21:     0x55e80b04be95 - <futures_util::future::try_maybe_done::TryMaybeDone<Fut> as core::future::future::Future>::poll::hc7850360150769d9
  22:     0x55e80b02db7e - <futures_util::future::try_join_all::TryJoinAll<F> as core::future::future::Future>::poll::h35f63be4ea6954d6
  23:     0x55e80b06099c - <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll::h535b49f40b99dc62
  24:     0x55e80b05ca78 - <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll::h12b9dacf14f1e112
  25:     0x55e80b03aafa - <tokio::future::poll_fn::PollFn<F> as core::future::future::Future>::poll::h6805b7a2e8c3cde9
  26:     0x55e80b067a67 - eth_parser::main::{{closure}}::hed50cd11203d3a44
  27:     0x55e80b04ca3f - std::thread::local::LocalKey<T>::with::h5f1aba2ed9a3c839
  28:     0x55e80b04aefd - tokio::park::thread::CachedParkThread::block_on::h632428f18e9174a5
  29:     0x55e80b04b1fb - tokio::runtime::thread_pool::ThreadPool::block_on::hd9590cb649db3db9
  30:     0x55e80b0096fe - tokio::runtime::Runtime::block_on::hd8e5af3b368c359c
  31:     0x55e80b055c24 - eth_parser::main::h28fc9691d052d5cd
  32:     0x55e80b047c66 - std::sys_common::backtrace::__rust_begin_short_backtrace::h7b393c2764e1e58f
  33:     0x55e80b056bb8 - std::rt::lang_start::{{closure}}::h8105a3db185a32c1
  34:     0x55e80b542bee - core::ops::function::impls::<impl core::ops::function::FnOnce<A> for &F>::call_once::had4f69b3aefb47a8
                               at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/core/src/ops/function.rs:259:13
  35:     0x55e80b542bee - std::panicking::try::do_call::hf2ad5355fcafe775
                               at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/std/src/panicking.rs:492:40
  36:     0x55e80b542bee - std::panicking::try::h0a63ac363423e61e
                               at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/std/src/panicking.rs:456:19
  37:     0x55e80b542bee - std::panic::catch_unwind::h18088edcecb8693a
                               at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/std/src/panic.rs:137:14
  38:     0x55e80b542bee - std::rt::lang_start_internal::{{closure}}::ha7dad166dc711761
                               at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/std/src/rt.rs:128:48
  39:     0x55e80b542bee - std::panicking::try::do_call::hda0c61bf3a57d6e6
                               at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/std/src/panicking.rs:492:40
  40:     0x55e80b542bee - std::panicking::try::hbc940e68560040a9
                               at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/std/src/panicking.rs:456:19
  41:     0x55e80b542bee - std::panic::catch_unwind::haed0df2aeb3fa368
                               at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/std/src/panic.rs:137:14
  42:     0x55e80b542bee - std::rt::lang_start_internal::h9c06694362b5b80c
                               at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/std/src/rt.rs:128:20
  43:     0x55e80b055d12 - main
  44:     0x7f671f28ed90 - <unknown>
  45:     0x7f671f28ee40 - __libc_start_main
  46:     0x55e80afb4265 - _start
  47:                0x0 - <unknown>

Make DecodedParams serializable

I would like to be able to serde_json serialize/deserialize DecodedParams struct.

This would allow me to store user transaction inputs in my database.
I understand this also implicates serializing the Value variants which could potentially be a tough task.

How does U256 represent "signed int value"

/// ABI decoded value.
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum Value {
    /// Unsigned int value (uint<M>).
    Uint(U256, usize),
    /// Signed int value (int<M>).
    Int(U256, usize),
    ...
}

Why "signed int value" is defined as Int(U256, usize)?

Overflow when decoding multicall transaction

struct TestCase {
            data: String,
            types: Vec<Type>,
            results: Vec<Value>
        }

        let test_case = TestCase {
            data: "0000000000000000000000000000000000000000000000000000000062262ba1000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000000e404e45aaf000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc200000000000000000000000000000000000000000000000000000000000001f4000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000084b6a5c40000000000000000000000000000000000000000000000000bd373e0061c7e7f94000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004449404b7c00000000000000000000000000000000000000000000000bd373e0061c7e7f9400000000000000000000000016ee789b50d3d49b8f71b5314c367e3fef24d74600000000000000000000000000000000000000000000000000000000".to_string(),
            types: vec![Type::Uint(256), Type::Array(Box::new(Type::Bytes))],
            results: vec![
                Value::Uint(EthersU256::from_dec_str("1646668705").unwrap(), 256),
                Value::Array(vec![
                    Value::Bytes(hex::decode("04e45aaf000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc200000000000000000000000000000000000000000000000000000000000001f4000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000084b6a5c40000000000000000000000000000000000000000000000000bd373e0061c7e7f940000000000000000000000000000000000000000000000000000000000000000").unwrap()),
                    Value::Bytes(hex::decode("49404b7c00000000000000000000000000000000000000000000000bd373e0061c7e7f9400000000000000000000000016ee789b50d3d49b8f71b5314c367e3fef24d746").unwrap()),
                ], Type::Bytes),
            ],
        };

        let values = Value::decode_from_slice(Vec::from_hex(test_case.data).unwrap().as_slice(), test_case.types.as_slice()).unwrap();
        for (idx, value) in values.iter().enumerate() {
            assert_eq!(value.clone(), test_case.results[idx]);
        }

Test case above

For a multicall transaction, there is an overflow when casting to usize. I believe this is because of the check if the type is dynamic in the fixedarray match arm, but taking it away will add problems to other cases. When in the branch, it already knows the size of the array, and has already accounted for offset. I am going to try to fix this myself but might need some help :)

Thanks

Make Abi serializable

I would like to serde serialize/deserialize my own struct that holds the ethereum_abi::Abi struct.

I would like to serialize the whole thing to write the Abi as a string to my database and then deserialize it into a matching struct when read from the database.

Currently this limitation forces me to do Abi::from_str every time I want to decode an input.

error[E0277]: the trait bound `Abi: firestore_data::_::_serde::Serialize` is not satisfied
    --> src\firestore_data.rs:33:5
     |
33   |     pub abi_struct: Abi,
     |     ^^^ the trait `firestore_data::_::_serde::Serialize` is not implemented for `Abi`
     |
note: required by a bound in `firestore_data::_::_serde::ser::SerializeStruct::serialize_field`
    --> C:\Users\Stormblessed\.cargo\registry\src\github.com-1ecc6299db9ec823\serde-1.0.136\src\ser\mod.rs:1899:12
     |
1899 |         T: Serialize;
     |            ^^^^^^^^^ required by this bound in `firestore_data::_::_serde::ser::SerializeStruct::serialize_field`

At the moment it seems the struct is only deserializable.
Forgive my ignorance if there is any known limitation to this at the moment.

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.