Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

212
Views
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.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!