简体   繁体   中英

Return value of smart contract function

In a truffle project, I have a smart contract function as below:

function add_something(string memory _firstName, string memory _lastName) external returns (address) {
    // Do some updates etc 
    return someAddress;
}

After the smart contract is deployed using truffle migrate command, I want to run a JS script to call some functions on the deployed smart contract. ( truffle exec add.js )

const MyContract = artifacts.require("MyContract");
module.exports = async function(callback) {

    let accounts = await web3.eth.getAccounts();

    let instance = await MyContract.deployed();

    let result = await instance.add_something("Stack", "Overflow", {from: accounts[0]});
    console.log(result);

Expected : I want to have the return value (address) in the result.

Actual : result contains the following Transaction receipt object:

{"tx":"0x2e5b2fb1a9dd023be04e12f73fcea3cab84214258be71ac2f76b86c58e485433","receipt":{"transactionHash":"0x2e5b2fb1a9dd023be04e12f73fcea3cab84214258be71ac2f76b86c58e485433","transactionIndex":0,"blockHash":"0xbdbef8e9b38cd66748eca51809869185deb61d00bb0f93916f76ee5594ca945d","blockNumber":45,"from":"0x75c1df3c54b6eb371169737fb62f2c5b7c178479","to":"0x786676da2567c51c83b56888e48030fa5fea0e03","gasUsed":2406988,"cumulativeGasUsed":2406988,"contractAddress":null,"logs":[],"status":true,"logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","rawLogs":[]},"logs":[]}

I have tried using.methods, call() and send(), but get the following errors:

let result = await instance.add_something("Stack", "Overflow", {from: accounts[0]}).call();
TypeError: instance.add_something(...).call is not a function

Please suggest a suitable solution to read the return value of a non-view smart contract function.

If I understood your question correctly you should consider using events. Under normal circumstances solidity functions do not return a value to begin with but events can change that

contract Test {
   event someEvent(string _someString);
   function doSomething()public external {      
      emit someEvent("This is some event");
   }
}

You can also pass a variable like this

function doSomething(string _somethingGoesHere)public external {      
      emit someEvent(_somethingGoesHere);
 }

You can then go onto the JavaScript code and read the result or rather the output like so

var abi = /* abi as generated using compiler */;
var Test = web3.eth.contract(abi);
/* Deploy your contract and save the address*/
var testContract = Test.at("0x1234...ab67" /* address */);

var event = testContract.doSomething(function(error, result) {
   if (!error)console.log(result);
});

This was you do not need to use any return value or a view function, instead you just read whatever is returned or rather emitted from the Solidity event

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM