-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathtransfer-interface.ts
More file actions
239 lines (224 loc) · 7.67 KB
/
transfer-interface.ts
File metadata and controls
239 lines (224 loc) · 7.67 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import {
PublicKey,
Signer,
TransactionInstruction,
SystemProgram,
} from '@solana/web3.js';
import { LIGHT_TOKEN_PROGRAM_ID } from '@lightprotocol/stateless.js';
import {
TOKEN_2022_PROGRAM_ID,
TOKEN_PROGRAM_ID,
createTransferInstruction as createSplTransferInstruction,
createTransferCheckedInstruction as createSplTransferCheckedInstruction,
} from '@solana/spl-token';
/**
* Light token transfer instruction discriminator
*/
const LIGHT_TOKEN_TRANSFER_DISCRIMINATOR = 3;
/**
* Light token transfer_checked instruction discriminator (SPL-compatible)
*/
const LIGHT_TOKEN_TRANSFER_CHECKED_DISCRIMINATOR = 12;
/**
* Create a light-token transfer instruction.
*
* For light-token accounts with compressible extension, the program needs
* system_program and fee_payer to handle rent top-ups.
*
* @param source Source light-token account
* @param destination Destination light-token account
* @param owner Owner of the source account (signer, also pays for compressible extension top-ups)
* @param amount Amount to transfer
* @param feePayer Optional fee payer for top-ups (defaults to owner)
* @returns Transaction instruction for light-token transfer
*/
export function createLightTokenTransferInstruction(
source: PublicKey,
destination: PublicKey,
owner: PublicKey,
amount: number | bigint,
feePayer?: PublicKey,
): TransactionInstruction {
// Instruction data format:
// byte 0: discriminator (3)
// bytes 1-8: amount (u64 LE)
const data = Buffer.alloc(9);
data.writeUInt8(LIGHT_TOKEN_TRANSFER_DISCRIMINATOR, 0);
data.writeBigUInt64LE(BigInt(amount), 1);
const effectiveFeePayer = feePayer ?? owner;
// Account order per program:
// 0: source (writable)
// 1: destination (writable)
// 2: authority/owner (signer, writable for top-ups)
// 3: system_program (for top-ups via CPI)
// 4: fee_payer (signer, writable - pays for top-ups)
const keys = [
{ pubkey: source, isSigner: false, isWritable: true },
{ pubkey: destination, isSigner: false, isWritable: true },
{ pubkey: owner, isSigner: true, isWritable: true },
{ pubkey: SystemProgram.programId, isSigner: false, isWritable: false },
{
pubkey: effectiveFeePayer,
isSigner: !effectiveFeePayer.equals(owner), // Only mark as signer if different from owner (owner already signed)
isWritable: true,
},
];
return new TransactionInstruction({
programId: LIGHT_TOKEN_PROGRAM_ID,
keys,
data,
});
}
/**
* Create a light-token transfer_checked instruction.
*
* Account order matches SPL Token's transferChecked:
* [source, mint, destination, authority]
*
* On-chain, the program validates that `decimals` matches the mint's decimals
* field, preventing decimal-related transfer errors.
*
* @param source Source light-token account
* @param mint Mint account (used for decimals validation)
* @param destination Destination light-token account
* @param owner Owner of the source account (signer, also pays for compressible extension top-ups)
* @param amount Amount to transfer
* @param decimals Expected decimals of the mint
* @returns Transaction instruction for light-token transfer_checked
*/
export function createLightTokenTransferCheckedInstruction(
source: PublicKey,
mint: PublicKey,
destination: PublicKey,
owner: PublicKey,
amount: number | bigint,
decimals: number,
): TransactionInstruction {
// Instruction data format:
// byte 0: discriminator (12)
// bytes 1-8: amount (u64 LE)
// byte 9: decimals (u8)
const data = Buffer.alloc(10);
data.writeUInt8(LIGHT_TOKEN_TRANSFER_CHECKED_DISCRIMINATOR, 0);
data.writeBigUInt64LE(BigInt(amount), 1);
data.writeUInt8(decimals, 9);
const keys = [
{ pubkey: source, isSigner: false, isWritable: true },
{ pubkey: mint, isSigner: false, isWritable: false },
{ pubkey: destination, isSigner: false, isWritable: true },
{ pubkey: owner, isSigner: true, isWritable: true },
];
return new TransactionInstruction({
programId: LIGHT_TOKEN_PROGRAM_ID,
keys,
data,
});
}
/**
* Construct a transfer instruction for SPL/T22/light-token. Defaults to
* light-token program.
*
* @param source Source token account
* @param destination Destination token account
* @param owner Owner of the source account (signer)
* @param amount Amount to transfer
* @param multiSigners Multi-signers (SPL/T22 only)
* @param programId Token program ID (default: LIGHT_TOKEN_PROGRAM_ID)
* @returns instruction for transfer
*/
export function createTransferInterfaceInstruction(
source: PublicKey,
destination: PublicKey,
owner: PublicKey,
amount: number | bigint,
multiSigners: (Signer | PublicKey)[] = [],
programId: PublicKey = LIGHT_TOKEN_PROGRAM_ID,
): TransactionInstruction {
if (programId.equals(LIGHT_TOKEN_PROGRAM_ID)) {
if (multiSigners.length > 0) {
throw new Error(
'Light token transfer does not support multi-signers. Use a single owner.',
);
}
return createLightTokenTransferInstruction(
source,
destination,
owner,
amount,
);
}
if (
programId.equals(TOKEN_PROGRAM_ID) ||
programId.equals(TOKEN_2022_PROGRAM_ID)
) {
return createSplTransferInstruction(
source,
destination,
owner,
amount,
multiSigners.map(pk =>
pk instanceof PublicKey ? pk : pk.publicKey,
),
programId,
);
}
throw new Error(`Unsupported program ID: ${programId.toBase58()}`);
}
/**
* Construct a transfer_checked instruction for SPL/T22/light-token. Defaults to
* light-token program. On-chain, validates that `decimals` matches the mint.
*
* @param source Source token account
* @param mint Mint account
* @param destination Destination token account
* @param owner Owner of the source account (signer)
* @param amount Amount to transfer
* @param decimals Expected decimals of the mint
* @param multiSigners Multi-signers (SPL/T22 only)
* @param programId Token program ID (default: LIGHT_TOKEN_PROGRAM_ID)
* @returns instruction for transfer_checked
*/
export function createTransferInterfaceCheckedInstruction(
source: PublicKey,
mint: PublicKey,
destination: PublicKey,
owner: PublicKey,
amount: number | bigint,
decimals: number,
multiSigners: (Signer | PublicKey)[] = [],
programId: PublicKey = LIGHT_TOKEN_PROGRAM_ID,
): TransactionInstruction {
if (programId.equals(LIGHT_TOKEN_PROGRAM_ID)) {
if (multiSigners.length > 0) {
throw new Error(
'Light token transfer does not support multi-signers. Use a single owner.',
);
}
return createLightTokenTransferCheckedInstruction(
source,
mint,
destination,
owner,
amount,
decimals,
);
}
if (
programId.equals(TOKEN_PROGRAM_ID) ||
programId.equals(TOKEN_2022_PROGRAM_ID)
) {
return createSplTransferCheckedInstruction(
source,
mint,
destination,
owner,
amount,
decimals,
multiSigners.map(pk =>
pk instanceof PublicKey ? pk : pk.publicKey,
),
programId,
);
}
throw new Error(`Unsupported program ID: ${programId.toBase58()}`);
}