代码之家  ›  专栏  ›  技术社区  ›  slider

打字和web3批处理请求

  •  0
  • slider  · 技术社区  · 5 年前

    我正试图从web3向我的合约发送一批交易请求。使用Typechain,我的合约的方法类型为 NonPayableTransactionObject<void> ,它似乎不支持为那个讨厌的东西打字 .request 所需方法 batch.add() 例如。。。

    let batch = new web3.eth.BatchRequest();
    for (let id of tokenIDs) {
        batch.add(myContract.methods.myMethod(id).send.request({ from: defaultAccount })   
    }
    batch.execute();
    

    这产生 Property 'request' does not exist on type '(tx?: NonPayableTx | undefined) => PromiEvent<TransactionReceipt>'.ts(2339) .

    是否有另一种使用Typescript/Typechain通过Batch Request向合约发送交易的方法?

    0 回复  |  直到 2 年前
        1
  •  0
  •   tenbits    2 年前

    TypeChain没有类型声明 .request 方法既不 .send 也不在 .call 方法。但是作为 TypeChain 只是类型定义、方法 .request 在运行时仍然可用。为了防止TS编译器抛出错误,您可以简单地强制转换 .send 方法to any :

    batch.add((myContract.methods.myMethod(id).send as any).request({ from: defaultAccount });
    

    在TypeScript中使用静态类型合约类的另一种选择是 0xweb 图书馆。

    您使用以下命令生成类 0xweb install 0x.... --chain eth --name MyContract 然后批量调用read方法:

    import { MyContract } from '@dequanto/eth/MyContract'
    
    const myContract = new MyContract();
    const requests = tokenIds.map(id => {
        return myContract
            .$config({ send: 'manual', from:'0x...'})
            .myMethod(id);
    });
    const results = await myContract.$executeBatch(requests);
    
    推荐文章