Solidity

安全帽測試返回事務而不是返回值

  • January 25, 2022

我剛從松露到安全帽。就像標題說的那樣,我有一個合約的返回值(地址)並取回交易。知道我能做些什麼嗎?

it("Check all tested", async function () {
testingFunction("createPair")

let justSo = await token.connect(actorA).approve(liqminter.address, startAmount)

const shareTokenAddress = await liqminter.connect(actorA).createPair(arg0, arg1, arg2, arg3, arg4, arg5)
console.log("Created Token: ", shareTokenAddress)

});

輸出:

Created Token:  {
hash: '0xd1bd72405efb01c80b7dfe75507d93cb878ebba08bd61501dae586e938e74a88',
type: 2,
accessList: [],
blockHash: '0x044321bf6f437da19ef27f5e46e482bf58bafd823c4b8b010ff70d72dbc12498',
blockNumber: 8,
transactionIndex: 0,
confirmations: 1,
from: '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266',
gasPrice: BigNumber { _hex: '0x525b8ada', _isBigNumber: true },
maxPriorityFeePerGas: BigNumber { _hex: '0x3b9aca00', _isBigNumber: true },
maxFeePerGas: BigNumber { _hex: '0x691c4bb4', _isBigNumber: true },
gasLimit: BigNumber { _hex: '0x01bae3d8', _isBigNumber: true },
to: '0x8ce361602B935680E8DeC218b820ff5056BeB7af',
value: BigNumber { _hex: '0x00', _isBigNumber: true },
nonce: 3,
data:   '0xb34471f2000000000000000000000000e7f1725e7734ce288f8367e1bb143e90bb3f05120000000000000000000000000    00000000000000000000000000000000000000000000000000000000000000090f79bf6eb2c4f870365e785982e1f101e93b    9060000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000    00000000000000000000000000027100000000000000000000000000000000000000000000000056bc75e2d63100000',
r: '0x2320064cb991b41feba18c5307d11a38e1d66344e75dc9d0d6844127910e3b49',
s: '0x4253b8a878827affc6f5d08d6122ac21c28ef1f3a9227ae6025ac6a0afd1cde0',
v: 0,
creates: null,
chainId: 31337,
wait: [Function (anonymous)]
}

我遇到過同樣的問題。沒有看到你的契約發布很難說,但對我來說,我試圖簡單地從公共函式中讀取一個值。

你需要讓乙太坊知道你正在返回一個只讀值,它不需要交易。為此,請將您的函式標記為視圖

function ping() public view returns (string memory) {
   return "pong";
}

要獲得返回值,您應該添加callStatic它。

>   const shareTokenAddress = await liqminter.connect(actorA).callStatic.createPair([...])

callStatic是只讀操作,不會消耗任何乙太幣。它模擬事務中會發生的情況,但在完成後丟棄所有狀態更改。

引用自:https://ethereum.stackexchange.com/questions/111916