2

I have a contract which I want to call using the polkadot{.js} extension. To call the method successfully, you have to sign it. When I am now signing a method, I have trouble reading out the value the method should give me, when executed properly.

If I would not have to sign the method, I would read the output the follwing way:

const { output } = await contract.query.method(
    account.address,
    {
        gasLimit,
        storageDepositLimit,
    },
    variables
)

console.log(output.toHuman())

And the output would look smth like that:

Ok('result here')

Now I have to sign the method first, and the code looks the following way:

const { output } = await contract.tx
    .method({ storageDepositLimit, gasLimit }, variables)
    .signAndSend(account.address, {signer: injector.signer},  result => {
    if (result.status.isInBlock) {
        console.log('in a block');
    } else if (result.status.isFinalized) {
      console.log('finalized');
      console.log(result.toHuman())
    }
})

The 'result.toHuman()' part does not give me the output of the method. And when I try to call 'output.toHuman()' it tells me that output is undefined.

How can I access the output of the method, when signing the execution prior?

2

1 Answer 1

4

Like P.Ossun said, the solution is to dry-run the contract first and check the answer, then to run the contract again, this time making actual changes on the blockchain.

Here you can find the documantation how to do the dry run.

The solution to my question would look the following way:

const { gasRequired, storageDeposit, result, output } = await contract.query.method(
    account.address,
    {
        gasLimit,
        storageDepositLimit,
    },
    variables
)

console.log(output.toHuman())

if(result.isOk){
    await contract.tx
        .method({ storageDepositLimit, gasLimit }, variables)
        .signAndSend(account.address, {signer: injector.signer},  result => {
        if (result.status.isInBlock) {
            console.log('in a block');
        } else if (result.status.isFinalized) {
            console.log('finalized');
        }
    })
}

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.