Skip to content

Commit

Permalink
refactor to preserve account ordering
Browse files Browse the repository at this point in the history
  • Loading branch information
barnjamin committed Oct 27, 2023
1 parent 3909aeb commit a5d8912
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions watcher/src/watchers/SolanaWatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,14 +170,29 @@ export class SolanaWatcher extends Watcher {

// Important to return the addresses in the order they're specified in the
// address table lookup object. Note writable comes first, then readable.
// TODO: check if this is broken by multiple addressTableLookups in a single message
return atl.writableIndexes
.concat(atl.readonlyIndexes)
.map((i) => lookupTableAccount.state.addresses[i]);
return [
atl.accountKey,
atl.writableIndexes.map((i) => lookupTableAccount.state.addresses[i]),
atl.readonlyIndexes.map((i) => lookupTableAccount.state.addresses[i]),
] as [PublicKey, PublicKey[], PublicKey[]];
});

// Lookup all addresses in parallel
const lookups = await Promise.all(lookupPromises);
accountKeys = accountKeys.concat(...lookups);

// Ensure the order is maintained for lookups
// Static, Writable, Readable
// ref: https://github.com/gagliardetto/solana-go/blob/main/message.go#L414-L464
const writable: PublicKey[] = [];
const readable: PublicKey[] = [];
for (const atl of message.addressTableLookups) {
const table = lookups.find((l) => l[0].equals(atl.accountKey));
if (!table) throw new Error('Could not find address table lookup');
writable.push(...table[1]);
readable.push(...table[2]);
}

accountKeys.push(...writable.concat(readable));
}

const programIdIndex = accountKeys.findIndex((i) => i.toBase58() === this.programId);
Expand Down

0 comments on commit a5d8912

Please sign in to comment.