Skip to content

Commit 50325da

Browse files
committed
watcher: FT review rework
1 parent ab7c3df commit 50325da

File tree

2 files changed

+12
-52
lines changed

2 files changed

+12
-52
lines changed

watcher/src/watchers/FTSolanaWatcher.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -236,13 +236,13 @@ export class FTSolanaWatcher extends SolanaWatcher {
236236
);
237237
}
238238

239-
const message = res.transaction.message;
240-
const instructions = message.compiledInstructions;
239+
const allKeys = await getAllKeys(this.getConnection(), res);
241240

242241
// filter out instructions that are not for the program we are interested in
243-
const programIdIndex = res.transaction.message.staticAccountKeys.findIndex((i) =>
244-
i.equals(programId)
245-
);
242+
const programIdIndex = allKeys.findIndex((i) => i.equals(programId));
243+
244+
const message = res.transaction.message;
245+
const instructions = message.compiledInstructions;
246246

247247
const programInstructions = instructions
248248
.map((ix, seq) => {
@@ -252,7 +252,7 @@ export class FTSolanaWatcher extends SolanaWatcher {
252252

253253
for (const ix of programInstructions) {
254254
try {
255-
await this.parseInstruction(res, ix.ix, ix.seq);
255+
await this.parseInstruction(res, ix.ix, ix.seq, allKeys);
256256
} catch (error) {
257257
this.logger.error('parseInstruction error:', error);
258258
}
@@ -264,9 +264,9 @@ export class FTSolanaWatcher extends SolanaWatcher {
264264
async parseInstruction(
265265
res: VersionedTransactionResponse,
266266
instruction: MessageCompiledInstruction,
267-
seq: number
267+
seq: number,
268+
allKeys: PublicKey[]
268269
): Promise<void> {
269-
const allKeys = await getAllKeys(this.getConnection(), res);
270270
const decodedData = this.matchingEngineBorshCoder.instruction.decode(
271271
Buffer.from(instruction.data),
272272
'base58'

watcher/src/watchers/SolanaWatcher.ts

+4-44
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
PublicKey,
66
SolanaJSONRPCError,
77
VersionedBlockResponse,
8+
VersionedMessage,
89
} from '@solana/web3.js';
910
import { decode } from 'bs58';
1011
import { z } from 'zod';
@@ -13,13 +14,13 @@ import { VaasByBlock } from '../databases/types';
1314
import { makeBlockKey, makeVaaKey } from '../databases/utils';
1415
import {
1516
Mode,
16-
isLegacyMessage,
1717
normalizeCompileInstruction,
1818
universalAddress_stripped,
1919
} from '@wormhole-foundation/wormhole-monitor-common';
2020
import { Watcher } from './Watcher';
2121
import { Network, contracts } from '@wormhole-foundation/sdk-base';
2222
import { deserializePostMessage } from '@wormhole-foundation/sdk-solana-core';
23+
import { getAllKeys } from '../utils/solana';
2324

2425
const COMMITMENT: Commitment = 'finalized';
2526
const GET_SIGNATURES_LIMIT = 1000;
@@ -159,50 +160,9 @@ export class SolanaWatcher extends Watcher {
159160
);
160161
}
161162

162-
const message = res.transaction.message;
163-
let accountKeys = isLegacyMessage(message)
164-
? message.accountKeys
165-
: message.staticAccountKeys;
166-
167-
// If the message contains an address table lookup, we need to resolve the addresses
168-
// before looking for the programIdIndex
169-
if (message.addressTableLookups.length > 0) {
170-
const lookupPromises = message.addressTableLookups.map(async (atl) => {
171-
const lookupTableAccount = await this.getConnection()
172-
.getAddressLookupTable(atl.accountKey)
173-
.then((res) => res.value);
174-
175-
if (!lookupTableAccount)
176-
throw new Error('lookupTableAccount is null, cant resolve addresses');
177-
178-
// Important to return the addresses in the order they're specified in the
179-
// address table lookup object. Note writable comes first, then readable.
180-
return [
181-
atl.accountKey,
182-
atl.writableIndexes.map((i) => lookupTableAccount.state.addresses[i]),
183-
atl.readonlyIndexes.map((i) => lookupTableAccount.state.addresses[i]),
184-
] as [PublicKey, PublicKey[], PublicKey[]];
185-
});
186-
187-
// Lookup all addresses in parallel
188-
const lookups = await Promise.all(lookupPromises);
189-
190-
// Ensure the order is maintained for lookups
191-
// Static, Writable, Readable
192-
// ref: https://github.com/gagliardetto/solana-go/blob/main/message.go#L414-L464
193-
const writable: PublicKey[] = [];
194-
const readable: PublicKey[] = [];
195-
for (const atl of message.addressTableLookups) {
196-
const table = lookups.find((l) => l[0].equals(atl.accountKey));
197-
if (!table) throw new Error('Could not find address table lookup');
198-
writable.push(...table[1]);
199-
readable.push(...table[2]);
200-
}
201-
202-
accountKeys.push(...writable.concat(readable));
203-
}
204-
163+
const accountKeys = await getAllKeys(this.getConnection(), res);
205164
const programIdIndex = accountKeys.findIndex((i) => i.toBase58() === this.programId);
165+
const message: VersionedMessage = res.transaction.message;
206166
const instructions = message.compiledInstructions;
207167
const innerInstructions =
208168
res.meta?.innerInstructions?.flatMap((i) =>

0 commit comments

Comments
 (0)