-
Notifications
You must be signed in to change notification settings - Fork 652
/
cpi.ts
92 lines (88 loc) · 2.33 KB
/
cpi.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import { PublicKey, PublicKeyInitData } from "@solana/web3.js";
import {
deriveEmitterSequenceKey,
deriveFeeCollectorKey,
deriveWormholeEmitterKey,
deriveWormholeBridgeDataKey,
getEmitterKeys,
} from "./accounts";
import { getPostMessageAccounts } from "./instructions";
export interface WormholeDerivedAccounts {
/**
* seeds = ["Bridge"], seeds::program = wormholeProgram
*/
wormholeBridge: PublicKey;
/**
* seeds = ["emitter"], seeds::program = cpiProgramId
*/
wormholeEmitter: PublicKey;
/**
* seeds = ["Sequence", wormholeEmitter], seeds::program = wormholeProgram
*/
wormholeSequence: PublicKey;
/**
* seeds = ["fee_collector"], seeds::program = wormholeProgram
*/
wormholeFeeCollector: PublicKey;
}
/**
* Generate Wormhole PDAs.
*
* @param cpiProgramId
* @param wormholeProgramId
* @returns
*/
export function getWormholeDerivedAccounts(
cpiProgramId: PublicKeyInitData,
wormholeProgramId: PublicKeyInitData
): WormholeDerivedAccounts {
const { emitter: wormholeEmitter, sequence: wormholeSequence } =
getEmitterKeys(cpiProgramId, wormholeProgramId);
return {
wormholeBridge: deriveWormholeBridgeDataKey(wormholeProgramId),
wormholeEmitter,
wormholeSequence,
wormholeFeeCollector: deriveFeeCollectorKey(wormholeProgramId),
};
}
export interface PostMessageCpiAccounts extends WormholeDerivedAccounts {
payer: PublicKey;
wormholeMessage: PublicKey;
clock: PublicKey;
rent: PublicKey;
systemProgram: PublicKey;
}
/**
* Generate accounts needed to perform `post_message` instruction
* as cross-program invocation.
*
* @param cpiProgramId
* @param wormholeProgramId
* @param payer
* @param message
* @returns
*/
export function getPostMessageCpiAccounts(
cpiProgramId: PublicKeyInitData,
wormholeProgramId: PublicKeyInitData,
payer: PublicKeyInitData,
message: PublicKeyInitData
): PostMessageCpiAccounts {
const accounts = getPostMessageAccounts(
wormholeProgramId,
payer,
cpiProgramId,
message
);
return {
payer: accounts.payer,
wormholeBridge: accounts.bridge,
wormholeMessage: accounts.message,
wormholeEmitter: accounts.emitter,
wormholeSequence: accounts.sequence,
wormholeFeeCollector: accounts.feeCollector,
clock: accounts.clock,
rent: accounts.rent,
systemProgram: accounts.systemProgram,
};
}