decodeFunctionResult: get uint256 output from a transaction receipt using ethers v5 #2395
-
Describe the bug Reproduction steps simple contract1.sol 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;
}
} //endcon script js: // create the factory and deploy the contract1
const fact_0 = new ethers.ContractFactory(Contract1['abi'], Contract1['bytecode'], signers[0]);
const in_str_1_1 = 'hola';
const in_val_1_2 = ethers.BigNumber.from(1);
const con_0 = await fact_0.deploy(in_str_1_1, in_val_1_2);
await con_0.deployTransaction.wait(); // wait to be mined, in order to be safe to interact with
console.log(`successfully deployed: ${JSON.stringify(con_0)}`);
// interaction {get,set}
const ins_1 = con_0; // instance. get directly the one deployed
const str_1 = await ins_1.get_str(); // get
await ins_1.set_str('superhola'); // set
const str_2 = await ins_1.get_str();
console.log(`get_function: success. valor: ${str_1}`);
console.log(`set_function: success. valor: ${str_2}`);
// try read tx_receipt and get uint256 output
const in_val_2_1 = ethers.BigNumber.from(2);
const tx_2 = await ins_1.fun_sum256(in_val_2_1);
const tx_2_post = await tx_2.wait();
// const obj_iface = new ethers.utils.Interface(Contract1.abi);
// const resp_1 = await obj_iface.decodeFunctionResult("fun_sum256", tx_2.data);
const resp_2 = await ins_1.interface.decodeFunctionResult(ins_1.interface.functions["fun_sum256(uint256)"], tx_2.data); //also tried from Contract1 error:
Environment: Search Terms |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 3 replies
-
The |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
The encodeFunctIonData and decodeFunctionData work on the data you pass along in as data to a transaction, either using sendTransaction or call. The encodeFunctionResult and decodeFunctionResult work on the data return sed from a call. Make sense? (Moving this to discussions) |
Beta Was this translation helpful? Give feedback.
-
I am sorry to say bad news, but what I read from What I read from IMHO it's definitely a bug, I am really sorry for what that means.. In the meanwhile I will move back to v.4. Otherwise could you provide a simple reproducible script that works? Thank you again anyway. |
Beta Was this translation helpful? Give feedback.
-
Oh I see.. Then if I understood properly, it is for call functions that are only for view-functions, so for read-only functions (no tx, no change of the state, no gas cost). Then.. how Remix-ide is able to retrieve the uint256 value output result from a non-view function (only public)? For me it's a great mistery beyond my small knowledge :( That was the small example and script I made for reproducing the issue. If I have something better I'll let you know. I see now with more detail that in ethers v5 docs |
Beta Was this translation helpful? Give feedback.
The
decodeFuncrionResult
method is for results. You likely want thedecodeFunctionData
method, which operates on transaction data.