The challenge is to extract an output uint256 from a receipt tx of a public smart contract using ethers in a js script that interacts with a smart contract.
Consider the following very simple contract1.sol. We are gonna play with the variable _val_1, and the function fun_sum256.
pragma solidity ^0.8.0;
// SPDX-License-Identifier: MIT
contract Contract1 {
string public _str_1;
uint256 public _val_1;
constructor(string memory str_in1, uint256 in_val_1 ) {
_str_1 = str_in1;
_val_1 = in_val_1;
} //endconstructor
function get_str() external view returns (string memory) {
return _str_1;
} //endfun get_str
function set_str(string memory str_in1) external returns (string memory) {
_str_1 = str_in1;
return _str_1;
} //endfun set_str
function fun_sum256(uint256 in_val_2) public returns (uint256) {
_val_1 += in_val_2;
return _val_1;
} //endfun sum256
} //endcon
As observed in ethers, the procedure for decoding the tx_receipt.data requires the usage of an interface,
let value = contract.interface.decodeFunctionResult(fragment, result);
where fragment is the "fun_sum256" corresponding function fragment, and result, is the data of the tx-receipt (properly mined and waited). The function fragment can also be directly called in the following way, <yourDeployedContract>.interface.functions["fun_sum256(uint256)"].
It simply does not work, at least for me. I can extract the other string argument _str_1, but not the uint types. But on the contrary, on remix-ide, it does. If I init the _val_1 with value 1 in constructor, and then I invoke the fun_uint256 public function with an input of 2, it successfully works on remix, performing 1+2=3, to see the decoded output "0: uint256: 3". And indeed, from my js script that interacts with the contract I can foresee the value in the txdata, the last cypher:
0x78081f400000000000000000000000000000000000000000000000000000000000000003
But ethers v5 simply seems incapable of extracting that "3".
Can anyone provide a very simple example with a simple.sol and the corresponding simple.js to interact with, that works with ethers library in js?