【问题标题】:How do I pass in a number to web3.eth().block()?如何将数字传递给 web3.eth().block()?
【发布时间】:2021-09-04 07:16:12
【问题描述】:

我正在使用rust-web3 crate 连接到以太坊节点并通过块号(或块高度)获取有关块的信息。基于this example,这是我能够实现它的方式:

use web3;
use web3::types::{BlockId, BlockNumber, U64};

#[tokio::main]
async fn main() -> web3::Result<()> {
    let transport = web3::transports::Http::new(WEB3_URL)?;
    let web3 = web3::Web3::new(transport);

    web3.eth().block(BlockId::Number(BlockNumber::Number(U64([42]))));
    Ok(())
}

有没有更简单的方法将 42 传递给web3.eth().block()

现在我正在使用两个枚举和U64 结构来创建编译器接受的变量。我是 rust 新手,我想我错过了可以简化它的语言的一个重要概念。

【问题讨论】:

    标签: rust ethereum web3


    【解决方案1】:

    有一个impl From&lt;U64&gt; for BlockId (source),所以你可以缩短通话时间:

    web3.eth().block(U64([42]).into());
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-30
      • 2020-01-23
      • 2022-01-15
      • 2022-07-10
      • 2019-09-01
      • 2022-11-19
      • 2021-09-16
      • 2018-09-16
      相关资源
      最近更新 更多