-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathsolana.ts
62 lines (58 loc) · 2.07 KB
/
solana.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import {
CompiledInstruction,
Message,
MessageCompiledInstruction,
MessageV0,
} from '@solana/web3.js';
import { decode } from 'bs58';
import { Connection } from '@solana/web3.js';
import { RPCS_BY_CHAIN } from '@certusone/wormhole-sdk/lib/cjs/relayer';
import { CONTRACTS } from '@certusone/wormhole-sdk';
export const isLegacyMessage = (message: Message | MessageV0): message is Message => {
return message.version === 'legacy';
};
export const normalizeCompileInstruction = (
instruction: CompiledInstruction | MessageCompiledInstruction
): MessageCompiledInstruction => {
if ('accounts' in instruction) {
return {
accountKeyIndexes: instruction.accounts,
data: decode(instruction.data),
programIdIndex: instruction.programIdIndex,
};
} else {
return instruction;
}
};
export async function convertSolanaTxToAccts(txHash: string): Promise<string[]> {
const POST_MESSAGE_IX_ID = 0x01;
let accounts: string[] = [];
const connection = new Connection(RPCS_BY_CHAIN.MAINNET.solana!, 'finalized');
const txs = await connection.getTransactions([txHash], {
maxSupportedTransactionVersion: 0,
});
for (const tx of txs) {
if (!tx) {
continue;
}
const message = tx.transaction.message;
const accountKeys = isLegacyMessage(message) ? message.accountKeys : message.staticAccountKeys;
const programIdIndex = accountKeys.findIndex(
(i) => i.toBase58() === CONTRACTS.MAINNET.solana.core
);
const instructions = message.compiledInstructions;
const innerInstructions =
tx.meta?.innerInstructions?.flatMap((i) => i.instructions.map(normalizeCompileInstruction)) ||
[];
const whInstructions = innerInstructions
.concat(instructions)
.filter((i) => i.programIdIndex === programIdIndex);
for (const instruction of whInstructions) {
// skip if not postMessage instruction
const instructionId = instruction.data;
if (instructionId[0] !== POST_MESSAGE_IX_ID) continue;
accounts.push(accountKeys[instruction.accountKeyIndexes[1]].toBase58());
}
}
return accounts;
}