2

This may be javascript question, but I am totally out of ideas.

I have a function in solidity code returning a variable:

  function getIdentifier() public view returns (string) {
    return identifier;
  }

I want to simply call this function with web3.js and store result as a variable. But all I can do is console.log the result (I know, I know =/).

Following code console.logs the variable:

contractInstance.methods.getIdentifier().call().then(console.log);

Works fine, but I simply want to store the return of getIdentifier() in a variable x.

None of the following naive code work:

x = contractInstance.methods.getIdentifier();
x = contractInstance.methods.getIdentifier().call();

I am sorry for a dumb question, but I have tried tons of things and it still does not work.

EDIT: Thanks smarx your code works, but I still would like to keep the variable for later use. Strangely something like this does not work:

var y = "";
contractInstance.methods.getIdentifier().call()
.then(function (x) {
  y = x;
});
console.log(y) // still empty!!! :(

5 Answers 5

3

This is just a JavaScript question. :-)

contractInstance.methods.getIdentifier()
.then(function (x) {
  // Use x in here.
});

// This code runs BEFORE the callback, and x won't be defined out here.
2
  • Thank you for the reply. It works if I use ....getIdentifier().call().then( ..., but this does not quite solve my problem as I would like to use the variable later on in the code. Could you check my updated answer.
    – Winten
    Commented Jan 27, 2018 at 13:48
  • The comment in my code already anticipated that confusion. :-) Your console.log happens before you've assigned anything to y. Put the code that uses x inside the then function.
    – user19510
    Commented Jan 27, 2018 at 17:39
1

Let's say the abi of my contract is 'abi' and the variable I am using to interact with my contract is 'agreem'. The address of deployed contract is contract_address.

let agreem =  new web3.eth.Contract(abi, contract_address);

Now, I will say my function's name is retrieve and it returns two values. My function takes two parameters as well. To call my function-

const bal=await agreem.methods.retrieve(uname,ps).call({from:accounts[0]});

The variable bal will store both the return values returned by my function.To access them you can use bal[0] and bal[1] .

Hope it helps.

1
0

You need your ABI and your address saved as a string

You create the contract instance like this:

var contract = web3.eth.contract(JSON.parse(abi)).at(address);

You can save the contract call like this:

contract.contractInstance.call();

Do you use truffle to develop? Truffle calls are different from web3js calls.

EDIT: This solution works with plain web3.js not truffle

0

Here is an example:

contract("MyContractTest", () => {
    let myContract;

    before(async () => {
        myContract = await artifacts.require("MyContract.sol").new(construction arguments);
    });

    describe(`test:`, async () => {
        it(`getIdentifier:`, async () => {
            let identifier = await myContract.getIdentifier();
            console.log(identifier);
        });
    });
});

If the function is not pure/view/constant, then it's a little more complicated (you need to add an event in the Solidity contract, and extract it from the returned value in the Javascript test).

0
var x = await contractInstance.methods.getIdentifier().call();

Though this will work only in async function. In synchronous code yuu will need to use callback:

contractInstance.methods.getIdentifier().call().then (x => {/* use x here */});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.