网站建设是干什么的,wordpress在线文档下载,天津市建设银行网站,线下推广有哪些渠道前言#xff1a; 作为开发#xff0c;你一定经历过调用合约的时候发现 gas fee 超出限制#xff0c;但是不知道报了什么错。这个时候一般都是触发了require错误合约校验。对于用户来说他不理解为什么一笔交易会花费如此大的gas#xff0c;那我们作为开发如何尽量避免这种情… 前言 作为开发你一定经历过调用合约的时候发现 gas fee 超出限制但是不知道报了什么错。这个时候一般都是触发了require错误合约校验。对于用户来说他不理解为什么一笔交易会花费如此大的gas那我们作为开发如何尽量避免这种情况的产生呢 一、static call ethersJS V5 文档的解释比 V6更清晰。 我们直接来看官方文档解释 Rather than executing the state-change of a transaction, it is possible to ask a node to pretend that a call is not state-changing and return the result. This does not actually change any state, but is free. This in some cases can be used to determine if a transaction will fail or succeed. 我们可以看到这是一种不执行交易的状态更改而是要求节点假装调用没有状态更改并返回结果的调用。这实际上并不改变任何状态而且是免费的。在某些情况下这可用于确定交易是失败还是成功。简单举个例子
// call method in v5 docs
// contract.callStatic.METHOD_NAME( ...args [ , overrides ] ) ⇒ Promise any const preTransaction async () { try {const preCall await contract.callStatic.transferFrom(from, to) } catch(e) {console.log(err , e)} } ehtersV5 文档地址 ethers V6 文档地址
二、 wagmi库 如果你使用的是wagmi库它已经替我们封装好了调用方法。举个栗子
import { prepareWriteContract } from wagmi/coreconst simulate await prepareWriteContract({address: contractAddress,abi: contractAbi,functionName: transfer,args: [],
})
console.debug(market order simulate , simulate) 总结 通过上面的方法可以大大避免用户触发 transcatin expected to fail 的报错。可以算作一个优化前端交互体验的加分点。