Daniel Portugal
Daniel Portugal

Reputation: 161

Polkadot: Weird Transaction Hash using JS API with Westend chain

I’m using the Polkadot JS API to send transactions on Westend. For that, the snippet is something like this:

const hash = await api.tx.balances
      .transfer(to, amount)
      .signAndSend(from, { tip, nonce: -1 })
      .catch(e => {
        throw e;
      });

I’m getting a hash just fine and balances are being updated as expected. But the hash I get is nowhere to be found on Subscan. When I fetch the account’s history of transactions, I see a different hash there, not the one I got from executing the above function. That hash I get from querying Subscan directly (at https://westend.subscan.io/api/scan/transfers), is the correct one. It can be found on Subscan both via the API and using Subscan’s UI.

Example: I've just sent a new transaction on Westend. I got the hash 0xdc2605ef0f21c77aa09f4e2df762a729bb2ecb5bb5602fe7a0858be2515c085c;

If I search for that hash on Subscan, it’s not there.

Now, I get the account's history of transactions and find out that my last transaction is this one:

{
  "from": "5Ec2BnQhmoM7anBrXi2wvgPKBUrTDS1ELmcR3j68cgXZxCuo",
  "to": "5HeRXojbvrsgk3x37haUJgxZhFZy8hvsBo9zDGsEsU8XhRRh",
  "extrinsic_index": "6089109-2",
  "success": true,
  "hash": "0xf026f07bf8736e7b9a664d08529ef88466f5d52a7237d202560cad680865c5a5",
  "block_num": 6089109,
  "block_timestamp": 1623872502,
  "module": "balances",
  "amount": "0.000000010014",
  "fee": "15700001585",
  "nonce": 20,
  "asset_symbol": "",
  "from_account_display": {
    "address": "5Ec2BnQhmoM7anBrXi2wvgPKBUrTDS1ELmcR3j68cgXZxCuo",
    "display": "",
    "judgements": null,
    "account_index": "",
    "identity": false,
    "parent": null
  },
  "to_account_display": {
    "address": "5HeRXojbvrsgk3x37haUJgxZhFZy8hvsBo9zDGsEsU8XhRRh",
    "display": "",
    "judgements": null,
    "account_index": "",
    "identity": false,
    "parent": null
  }
}

The hash we see there (0xf026f07bf8736e7b9a664d08529ef88466f5d52a7237d202560cad680865c5a5) is the right one. And the previous hash (0xdc2605ef0f21c77aa09f4e2df762a729bb2ecb5bb5602fe7a0858be2515c085c) is not even referenced, I have no idea what it is, nor why the function signAndSend returned it. I’m trying to fetch how many confirmations a given tx has, but I can’t, since the hash I’m getting when I make the transaction is (apparently) useless - and I'm storing on my database the hash that I get when I make the transaction.

Can anyone please shed some light here?

Thanks!

Upvotes: 0

Views: 572

Answers (1)

Daniel Portugal
Daniel Portugal

Reputation: 161

Found it!

The variable hash is not in hexadecimal format - well, it actually is, but not in the way the system wants it. I don't understand why, but the fact is that you have to do a hash.toHex() in order to get the right value. So, the solution is something like this:

const hash = await api.tx.balances
      .transfer(to, amount)
      .signAndSend(from, { tip, nonce: -1 })
      .catch(e => {
        throw e;
      });

return hash.toHex();

It was in the docs, I didn't pay attention to it: https://polkadot.js.org/docs/api/examples/promise/make-transfer

Upvotes: 0

Related Questions