How to propertly decode result after calling a contract?

Hi! I’m calling a smart contract function deployed on a state channel, and after that I would like to decode the result. I’m calling using:

const result = await channel.callContract({
            amount: 0,
            callData: await player.contractEncodeCallDataAPI(contract_source, 'get_board', []),
            contract: contract_address,
            abiVersion: 1
            }, async (tx) => await player.signTransaction(tx))
    );

Logging result:

{ accepted: true,
signedTx: 'tx_+QGLCwH4hLhAbqicl6nzeigTHzWeYNv5Ebl146Lc/HgVbt+eXabKKqgEgGLtwfC2Tnf/znfcFz89+iTR7p/Lkg2PTuY7Emj6BrhAwRiM2m3Gztu6ktWd0YMPvI1gReHlAc4MZaQWJGazHtTdnZylHaCuq3e/qYB6P0vV6L9T/ILI0KD4dxtqsibfBLkBAPj+OQGhBqdhF5uIsdE7n3VGlsz7Lev+koQ4US1BkvQJx+DBtpgEA/i2uLT4soICPgGhAem79gTmEbVGCjs5mel3G29gQX1zznxVGeEvfhJ6EiXKoQUeyLfVVo4K47ktqNO0kyaW7WxPArwqOAThFrl7YMI9xgEAgw9CQAG4YAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgkqcNIuOScYTV89X2RYZD/to8tBgh0GwKgTN4yc/0vusAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMCgd6uReEf4UxS7tRVHwmzbXmtv9pWofdheEE50iXTQg579Fqsp' }

How can I decode the result? (I’m expecting an int)

I tried with:

let tx_decoded = Crypto.deserialize(Crypto.decode("int",result), {prettyTags: true});

and also with Crypto.decodeTx and other methods.

1 Like

Question is forwarded to @pegah.chain

1 Like

First thing you need to get contract call result using getContractCall method. To call this method you need three arguments:

  1. address of the caller of contract
  2. address of the contract
  3. nonce when the contract was called

Then you can use contractDecodeDataAPI method to decode the result.

In your case the code will look something like this:

// call contract
const result = await channel.callContract({
  amount: 0,
  callData: await player.contractEncodeCallDataAPI(contract_source, 'get_board', []),
  contract: contract_address,
  abiVersion: 1
}, async (tx) => await player.signTransaction(tx)));

// get nonce
const nonce = Number(unpackTx(result.signedTx).tx.encodedTx.tx.round);

// get contract call
const call = await channel.getContractCall({
  caller: await player.address(),
  contract: contract_address,
  round: nonce
});

// decode return value
const value = await player.contractDecodeDataAPI('int', call.returnValue);
console.log(value);
2 Likes