我正试图从web3向我的合同发送一批事务请求。使用Typechain,我的合同的方法是NonPayableTransactionObject<void>类型,它似乎不支持输入batch.add()所需的讨厌的.request方法。例如..。
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)。
是否有另一种方式通过批处理请求使用类型记录/Typechain将事务发送到合同?
发布于 2022-10-25 12:09:55
TypeChain在.send和.call方法中都没有.request方法的类型声明。但是,由于TypeChain只是类型定义,所以.request方法在运行时仍然可用。为了防止TS编译器抛出错误,可以简单地将.send方法转换为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);https://stackoverflow.com/questions/63590294
复制相似问题