5

Why are account inputs for similar transactions with similar instructions placed at a different position? For instance, in the below example, the program account is either at position 4 or 5. The transactions are built by the same frontend logic using Solana web3.js.

What determines this position? The only delta in the transaction is two addresses.

For analyzing the accounts I now need to derive the account input from an inner instruction, but would prefer to do that directly from the transaction.

TX1: TX_1

TX2 accounts #4 and #5 are swapped: TX_2

2 Answers 2

5

web3.js sorts the accounts based on what Solana expects, i.e. signers before non-signers, writables before read-onlys.

In your case, both accounts #4 and #5 are read-only non-signer accounts so it then uses their public key for sorting. In both of your screenshots, the two accounts are sorted alphabetically.

This is the relevant part of the web3.js code where this sorting occurs.

2
  • 1
    Tiny nitpick: based on the source code it is deterministic how the "equal" accounts are sorted: it's by public key. return x.pubkey.toBase58().localeCompare(y.pubkey.toBase58());. You can see in the question examples exactly what's going on from there: AAtZ... comes before DCDX..., but Test... comes after DCDX...
    – Callum M
    Commented Jul 22, 2022 at 11:37
  • 2
    That's not a nitpick, that's a huge fkup on my part! The whole unstable sorting algorithm doesn't even come into it. I edited my answer. Thanks for pointing it out. Off to optometrist.
    – sohrab
    Commented Jul 22, 2022 at 11:49
2

The runtime only cares about the ordering of pubkeys with respect to their class as defined in MessageHeader. That is, one of: writable signers, read-only signers, writable non-signers and read-only non-signers. Within these classes, the ordering is irrelevant, so left open for SDKs to implement as they please

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.