Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

213
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda