从状态改变方法返回一个值
由于状态改变(非常量)函数可能需要大量时间来挖掘,响应是一个交易,不能直接返回值。

使用事件,我们可以模拟非常量函数的返回值。

contract Example {
    event Return(uint256);

    uint256 _accum = 0;

    function increment() returns (uint256 sum) {
        _accum++;
        Returns(_accum);
    }
}
const assert = require('assert')

const {
    Contract,
    Wallet,
    getDefaultProvider
} = require('ethers')

const provider = getDefaultProvider('ropsten')

const wallet = new Wallet(privateKey, provider)

const abi = [
    "event Return(uint256)",
    "function increment() returns (uint256 sum)"
]

const contractAddress = "0x..."

const contract = new Contract(contractAddress, abi)

async function increment() {

    // Call the contract, getting back the transaction
    let tx = await contract.increment()

    // Wait for the transaction to have 2 confirmations.
    // See the note below on "Economic Value" for considerations
    // regarding the number of suggested confirmations
    let receipt = await tx.wait(2)

    // The receipt will have an "events" Array, which will have
    // the emitted event from the Contract. The "Return(uint256)"
    // call is the last event.
    let sumEvent = receipt.events.pop()

    // Not necessary; these are just for the purpose of this
    // example
    assert.equal(sumEvent.event, 'Return')
    assert.equal(sumEvent.eventSignature, 'Return(uint256)')

    // The sum is the first (and in this case only) parameter
    // in the "Return(uint256 sum)" event
    let sum = sumEvent.args[0]

    return sum
}

increment.then((value) => {
    console.log(value);
});

https://docs.ethers.org/v4/cookbook-contracts.html