0

Appreciate any help I can get. I am trying to make an assertion on returned values from a smart contract, and I cannot figure out how to get it to work for a Truffle test. I cannot show all my code in raw format because it is sensitive in nature, but here is what you need from the test:

it("should accept a list of obj's and return a string & addr", async () => {
    let cInstance = await Contract.deployed("tester2");
    let cAddress = cInstance.address;
    let returnResult = await cInstance.submission.call("tester2","0xdade","type2","0xwizards",[1,"2","4",4]);
    assert.equal(returnResult.valueOf(), "0xdade", {contractaddress});

Here is the function I'm calling on contract:

function submission(string memory Id, string memory vId, string memory entryType, string memory hashSig, uint256[] memory list) public returns(string memory, address) {

    return(submittedObjStructs[vId].vId, address(this))
}

The contract function returns a string and the contract address, which comes back in an object format.

When I try to use assert.equal(result.valueOf(), string value, address value) it doesn't work and gives me an error that the values don't match for various reasons. And if I try array notation like assert.equal(result[0].valueOf(), string value) I get a vm error and the transaction reverts. Any way I can compare the value of the object that comes back from the function call: {0: '0xdade', 1: '0xcontractaddressexample'}

1 Answer 1

1

When none of the returned values is an integer (such as in your case), you can use this:

const [x, y] = await cInstance.submission.call(...);
const expected = `0xdade, ${contractaddress}`;
const actual = `${x}, ${y}`;
assert.equal(actual, expected);

In other cases (that you may possibly have), for any integer value, add .toFixed().

For example, suppose that the first returned-value is uint256, then use this:

const actual = `${x.toFixed()}, ${y}`;
8
  • thanks for this! When I run this I get "TypeError: (intermediate value) is not iterable" with a string 0xdade and the contract address
    – FS_woods
    Feb 26, 2019 at 15:24
  • @colyerfs: On which line are you getting this error? Feb 26, 2019 at 16:37
  • one the line reading: const [x, y] = await cInstance.submission.call(...);
    – FS_woods
    Feb 26, 2019 at 17:27
  • @colyerfs: Well, why is this function not declared view (i.e., not constant)??? An off-chain call to a non-constant function returns an object which contains transaction-related info, not the value or values in the function's return-statement. If this function modifies the state of the blockchain (i.e., this contract or other contracts), then you have no choice. But it doesn't seem to be the case in your code, so you can and should declare this function view, and then you'll be able to obtain the returned-value from the off-chain. In addition, calling it will not cost any gas. Feb 26, 2019 at 18:12
  • I only posted a subset of the code in the function. It does modify state. It adds the value of the arguments into a struct array. I get a response back in object format {'0': "<value>", '1': '<address>'
    – FS_woods
    Feb 26, 2019 at 18:17

Your Answer

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

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