前言: 作为开发,你一定经历过调用合约的时候发现 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