/
syncNative.ts
112 lines (100 loc) · 3.19 KB
/
syncNative.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import { struct, u8 } from '@solana/buffer-layout';
import type { AccountMeta, PublicKey } from '@solana/web3.js';
import { TransactionInstruction } from '@solana/web3.js';
import { TOKEN_PROGRAM_ID } from '../constants.js';
import {
TokenInvalidInstructionDataError,
TokenInvalidInstructionKeysError,
TokenInvalidInstructionProgramError,
TokenInvalidInstructionTypeError,
} from '../errors.js';
import { TokenInstruction } from './types.js';
/** TODO: docs */
export interface SyncNativeInstructionData {
instruction: TokenInstruction.SyncNative;
}
/** TODO: docs */
export const syncNativeInstructionData = struct<SyncNativeInstructionData>([u8('instruction')]);
/**
* Construct a SyncNative instruction
*
* @param account Native account to sync lamports from
* @param programId SPL Token program account
*
* @return Instruction to add to a transaction
*/
export function createSyncNativeInstruction(account: PublicKey, programId = TOKEN_PROGRAM_ID): TransactionInstruction {
const keys = [{ pubkey: account, isSigner: false, isWritable: true }];
const data = Buffer.alloc(syncNativeInstructionData.span);
syncNativeInstructionData.encode({ instruction: TokenInstruction.SyncNative }, data);
return new TransactionInstruction({ keys, programId, data });
}
/** A decoded, valid SyncNative instruction */
export interface DecodedSyncNativeInstruction {
programId: PublicKey;
keys: {
account: AccountMeta;
};
data: {
instruction: TokenInstruction.SyncNative;
};
}
/**
* Decode a SyncNative instruction and validate it
*
* @param instruction Transaction instruction to decode
* @param programId SPL Token program account
*
* @return Decoded, valid instruction
*/
export function decodeSyncNativeInstruction(
instruction: TransactionInstruction,
programId = TOKEN_PROGRAM_ID
): DecodedSyncNativeInstruction {
if (!instruction.programId.equals(programId)) throw new TokenInvalidInstructionProgramError();
if (instruction.data.length !== syncNativeInstructionData.span) throw new TokenInvalidInstructionDataError();
const {
keys: { account },
data,
} = decodeSyncNativeInstructionUnchecked(instruction);
if (data.instruction !== TokenInstruction.SyncNative) throw new TokenInvalidInstructionTypeError();
if (!account) throw new TokenInvalidInstructionKeysError();
// TODO: key checks?
return {
programId,
keys: {
account,
},
data,
};
}
/** A decoded, non-validated SyncNative instruction */
export interface DecodedSyncNativeInstructionUnchecked {
programId: PublicKey;
keys: {
account: AccountMeta | undefined;
};
data: {
instruction: number;
};
}
/**
* Decode a SyncNative instruction without validating it
*
* @param instruction Transaction instruction to decode
*
* @return Decoded, non-validated instruction
*/
export function decodeSyncNativeInstructionUnchecked({
programId,
keys: [account],
data,
}: TransactionInstruction): DecodedSyncNativeInstructionUnchecked {
return {
programId,
keys: {
account,
},
data: syncNativeInstructionData.decode(data),
};
}