lfx-reward-back-sdk 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,63 @@
1
+ import * as web3 from '@solana/web3.js';
2
+ /**
3
+ * Admin/Backoffice SDK for LFX Reward program.
4
+ *
5
+ * Responsibilities:
6
+ * - Provide admin instructions for configuring global state.
7
+ * - Provide helpers for deposit, withdrawal, and withdrawal wallet voting.
8
+ * - Provide read helpers for global info and withdrawal vote state.
9
+ */
10
+ export declare class LfxRewardBackClient {
11
+ private connection;
12
+ private wallet;
13
+ private programId;
14
+ private idl;
15
+ constructor(connection: web3.Connection, wallet: web3.Keypair, _opts?: web3.ConfirmOptions);
16
+ getDiscriminator(instructionName: string): Buffer;
17
+ getGlobalInfoPDA(): [web3.PublicKey, number];
18
+ getVaultTokenAccountPDA(): [web3.PublicKey, number];
19
+ getWithdrawalVotePDA(): [web3.PublicKey, number];
20
+ /**
21
+ * Admin: init_global_info
22
+ *
23
+ * NOTE:
24
+ * - This instruction initializes global config and vault PDA.
25
+ * - It should be called only once.
26
+ */
27
+ initGlobalInfoIx(adminWallet: web3.PublicKey, apiWallet: web3.PublicKey, internalMultisigWallets: web3.PublicKey[], withdrawalWallet: web3.PublicKey, rewardTokenMint: web3.PublicKey): web3.TransactionInstruction;
28
+ /**
29
+ * Admin: deposit_tokens
30
+ *
31
+ * High-level helper that derives admin ATA and vault PDA automatically.
32
+ */
33
+ depositTokensIx(amount: bigint | number, mint: web3.PublicKey): Promise<web3.TransactionInstruction>;
34
+ /**
35
+ * Admin: update_global_info
36
+ *
37
+ * Update admin_wallet / api_wallet / internal_multisig_wallets / is_emergency.
38
+ * withdrawal_wallet cannot be changed here (must use on-chain voting).
39
+ */
40
+ updateGlobalInfoIx(adminWallet: web3.PublicKey | null, isEmergency: boolean | null, apiWallet: web3.PublicKey | null, internalMultisigWallets: web3.PublicKey[] | null): web3.TransactionInstruction;
41
+ /**
42
+ * Admin: withdraw_tokens
43
+ *
44
+ * Withdraw from vault to the configured withdrawal_wallet's ATA.
45
+ * Caller must pass the recipient ATA (for withdrawal_wallet).
46
+ */
47
+ withdrawTokensIx(recipientTokenAccount: web3.PublicKey, tokenMint: web3.PublicKey, amount: bigint | number): web3.TransactionInstruction;
48
+ /**
49
+ * Admin: start_withdrawal_vote
50
+ */
51
+ startWithdrawalVoteIx(proposedWithdrawalWallet: web3.PublicKey): web3.TransactionInstruction;
52
+ /**
53
+ * Admin/Multisig member: vote_withdrawal_wallet
54
+ *
55
+ * Voter must be one of internal_multisig_wallets in GlobalInfo.
56
+ */
57
+ voteWithdrawalWalletIx(voter: web3.PublicKey): web3.TransactionInstruction;
58
+ getGlobalInfo(commitment?: web3.Commitment): Promise<any | null>;
59
+ getWithdrawalVote(commitment?: web3.Commitment): Promise<any | null>;
60
+ getConnection(): web3.Connection;
61
+ getWallet(): web3.Keypair;
62
+ getProgramId(): web3.PublicKey;
63
+ }
package/dist/client.js ADDED
@@ -0,0 +1,308 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ var __importDefault = (this && this.__importDefault) || function (mod) {
36
+ return (mod && mod.__esModule) ? mod : { "default": mod };
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.LfxRewardBackClient = void 0;
40
+ const web3 = __importStar(require("@solana/web3.js"));
41
+ const lfx_reward_sc_json_1 = __importDefault(require("./idl/lfx_reward_sc.json"));
42
+ const anchor_1 = require("@coral-xyz/anchor");
43
+ const spl_token_1 = require("@solana/spl-token");
44
+ /**
45
+ * Admin/Backoffice SDK for LFX Reward program.
46
+ *
47
+ * Responsibilities:
48
+ * - Provide admin instructions for configuring global state.
49
+ * - Provide helpers for deposit, withdrawal, and withdrawal wallet voting.
50
+ * - Provide read helpers for global info and withdrawal vote state.
51
+ */
52
+ class LfxRewardBackClient {
53
+ constructor(connection, wallet, _opts) {
54
+ this.connection = connection;
55
+ this.wallet = wallet;
56
+ this.idl = lfx_reward_sc_json_1.default;
57
+ if (this.idl.address) {
58
+ this.programId = new web3.PublicKey(this.idl.address);
59
+ }
60
+ else {
61
+ throw new Error('Program ID not found in IDL');
62
+ }
63
+ }
64
+ // Helper to extract discriminator from IDL
65
+ getDiscriminator(instructionName) {
66
+ const ix = this.idl.instructions?.find((i) => i.name === instructionName);
67
+ if (ix?.discriminator) {
68
+ return Buffer.from(ix.discriminator);
69
+ }
70
+ throw new Error(`Instruction ${instructionName} not found in IDL`);
71
+ }
72
+ ////////////////////////////////////////////////////////////////////////
73
+ // PDA helpers
74
+ ////////////////////////////////////////////////////////////////////////
75
+ getGlobalInfoPDA() {
76
+ return web3.PublicKey.findProgramAddressSync([Buffer.from('global')], this.programId);
77
+ }
78
+ getVaultTokenAccountPDA() {
79
+ return web3.PublicKey.findProgramAddressSync([Buffer.from('vault')], this.programId);
80
+ }
81
+ getWithdrawalVotePDA() {
82
+ return web3.PublicKey.findProgramAddressSync([Buffer.from('withdrawal_vote')], this.programId);
83
+ }
84
+ ////////////////////////////////////////////////////////////////////////
85
+ // Admin instruction builders
86
+ ////////////////////////////////////////////////////////////////////////
87
+ /**
88
+ * Admin: init_global_info
89
+ *
90
+ * NOTE:
91
+ * - This instruction initializes global config and vault PDA.
92
+ * - It should be called only once.
93
+ */
94
+ initGlobalInfoIx(adminWallet, apiWallet, internalMultisigWallets, withdrawalWallet, rewardTokenMint) {
95
+ if (internalMultisigWallets.length !== 5) {
96
+ throw new Error('internalMultisigWallets must contain exactly 5 wallets');
97
+ }
98
+ const [globalInfoPda] = this.getGlobalInfoPDA();
99
+ const [vaultTokenAccountPda] = this.getVaultTokenAccountPDA();
100
+ const discriminator = this.getDiscriminator('init_global_info');
101
+ // Anchor args layout now contains only:
102
+ // admin_wallet, api_wallet, internal_multisig_wallets[5], withdrawal_wallet
103
+ const data = Buffer.concat([
104
+ discriminator,
105
+ adminWallet.toBuffer(),
106
+ apiWallet.toBuffer(),
107
+ Buffer.concat(internalMultisigWallets.map((w) => w.toBuffer())),
108
+ withdrawalWallet.toBuffer(),
109
+ ]);
110
+ return new web3.TransactionInstruction({
111
+ programId: this.programId,
112
+ keys: [
113
+ { pubkey: this.wallet.publicKey, isSigner: true, isWritable: true }, // init_wallet signer & payer
114
+ { pubkey: globalInfoPda, isSigner: false, isWritable: true }, // global_info
115
+ { pubkey: vaultTokenAccountPda, isSigner: false, isWritable: true }, // vault_token_account
116
+ { pubkey: rewardTokenMint, isSigner: false, isWritable: true }, // mint
117
+ { pubkey: spl_token_1.TOKEN_2022_PROGRAM_ID, isSigner: false, isWritable: false }, // token_program
118
+ { pubkey: web3.SystemProgram.programId, isSigner: false, isWritable: false }, // system_program
119
+ ],
120
+ data,
121
+ });
122
+ }
123
+ /**
124
+ * Admin: deposit_tokens
125
+ *
126
+ * High-level helper that derives admin ATA and vault PDA automatically.
127
+ */
128
+ async depositTokensIx(amount, mint) {
129
+ const [globalInfoPda] = this.getGlobalInfoPDA();
130
+ const [vaultTokenAccountPda] = this.getVaultTokenAccountPDA();
131
+ const adminTokenAccount = await (0, spl_token_1.getAssociatedTokenAddress)(mint, this.wallet.publicKey, false, spl_token_1.TOKEN_2022_PROGRAM_ID);
132
+ const discriminator = this.getDiscriminator('deposit_tokens');
133
+ const amountBig = typeof amount === 'bigint' ? amount : BigInt(amount);
134
+ const amountBuffer = Buffer.allocUnsafe(8);
135
+ amountBuffer.writeBigUInt64LE(amountBig, 0);
136
+ const data = Buffer.concat([discriminator, amountBuffer]);
137
+ return new web3.TransactionInstruction({
138
+ programId: this.programId,
139
+ keys: [
140
+ { pubkey: this.wallet.publicKey, isSigner: true, isWritable: false }, // admin
141
+ { pubkey: adminTokenAccount, isSigner: false, isWritable: true }, // admin_token_account
142
+ { pubkey: vaultTokenAccountPda, isSigner: false, isWritable: true }, // vault_token_account
143
+ { pubkey: mint, isSigner: false, isWritable: true }, // mint
144
+ { pubkey: globalInfoPda, isSigner: false, isWritable: true }, // global_info
145
+ { pubkey: spl_token_1.TOKEN_2022_PROGRAM_ID, isSigner: false, isWritable: false }, // token_program
146
+ { pubkey: web3.SystemProgram.programId, isSigner: false, isWritable: false }, // system_program
147
+ ],
148
+ data,
149
+ });
150
+ }
151
+ /**
152
+ * Admin: update_global_info
153
+ *
154
+ * Update admin_wallet / api_wallet / internal_multisig_wallets / is_emergency.
155
+ * withdrawal_wallet cannot be changed here (must use on-chain voting).
156
+ */
157
+ updateGlobalInfoIx(adminWallet, isEmergency, apiWallet, internalMultisigWallets) {
158
+ if (internalMultisigWallets !== null && internalMultisigWallets.length !== 5) {
159
+ throw new Error('internalMultisigWallets must contain exactly 5 wallets');
160
+ }
161
+ const [globalInfoPda] = this.getGlobalInfoPDA();
162
+ const discriminator = this.getDiscriminator('update_global_info');
163
+ let data = discriminator;
164
+ // admin_wallet: Option<Pubkey>
165
+ if (adminWallet !== null) {
166
+ data = Buffer.concat([data, Buffer.from([1]), adminWallet.toBuffer()]);
167
+ }
168
+ else {
169
+ data = Buffer.concat([data, Buffer.from([0])]);
170
+ }
171
+ // is_emergency: Option<bool>
172
+ if (isEmergency !== null) {
173
+ data = Buffer.concat([data, Buffer.from([1]), Buffer.from([isEmergency ? 1 : 0])]);
174
+ }
175
+ else {
176
+ data = Buffer.concat([data, Buffer.from([0])]);
177
+ }
178
+ // api_wallet: Option<Pubkey>
179
+ if (apiWallet !== null) {
180
+ data = Buffer.concat([data, Buffer.from([1]), apiWallet.toBuffer()]);
181
+ }
182
+ else {
183
+ data = Buffer.concat([data, Buffer.from([0])]);
184
+ }
185
+ // internal_multisig_wallets: Option<[Pubkey;5]>
186
+ if (internalMultisigWallets !== null) {
187
+ const buf = Buffer.concat(internalMultisigWallets.map((w) => w.toBuffer()));
188
+ data = Buffer.concat([data, Buffer.from([1]), buf]);
189
+ }
190
+ else {
191
+ data = Buffer.concat([data, Buffer.from([0])]);
192
+ }
193
+ return new web3.TransactionInstruction({
194
+ programId: this.programId,
195
+ keys: [
196
+ { pubkey: this.wallet.publicKey, isSigner: true, isWritable: false }, // admin
197
+ { pubkey: globalInfoPda, isSigner: false, isWritable: true }, // global_info
198
+ ],
199
+ data,
200
+ });
201
+ }
202
+ /**
203
+ * Admin: withdraw_tokens
204
+ *
205
+ * Withdraw from vault to the configured withdrawal_wallet's ATA.
206
+ * Caller must pass the recipient ATA (for withdrawal_wallet).
207
+ */
208
+ withdrawTokensIx(recipientTokenAccount, tokenMint, amount) {
209
+ const [globalInfoPda] = this.getGlobalInfoPDA();
210
+ const [vaultTokenAccountPda] = this.getVaultTokenAccountPDA();
211
+ const discriminator = this.getDiscriminator('withdraw_tokens');
212
+ const amountBig = typeof amount === 'bigint' ? amount : BigInt(amount);
213
+ const amountBuffer = Buffer.allocUnsafe(8);
214
+ amountBuffer.writeBigUInt64LE(amountBig, 0);
215
+ const data = Buffer.concat([discriminator, amountBuffer]);
216
+ return new web3.TransactionInstruction({
217
+ programId: this.programId,
218
+ keys: [
219
+ { pubkey: this.wallet.publicKey, isSigner: true, isWritable: false }, // admin
220
+ { pubkey: recipientTokenAccount, isSigner: false, isWritable: true }, // recipient_token_account
221
+ { pubkey: vaultTokenAccountPda, isSigner: false, isWritable: true }, // vault_token_account
222
+ { pubkey: tokenMint, isSigner: false, isWritable: true }, // mint
223
+ { pubkey: globalInfoPda, isSigner: false, isWritable: true }, // global_info
224
+ { pubkey: spl_token_1.TOKEN_2022_PROGRAM_ID, isSigner: false, isWritable: false }, // token_program
225
+ { pubkey: web3.SystemProgram.programId, isSigner: false, isWritable: false }, // system_program
226
+ ],
227
+ data,
228
+ });
229
+ }
230
+ /**
231
+ * Admin: start_withdrawal_vote
232
+ */
233
+ startWithdrawalVoteIx(proposedWithdrawalWallet) {
234
+ const [globalInfoPda] = this.getGlobalInfoPDA();
235
+ const [withdrawalVotePda] = this.getWithdrawalVotePDA();
236
+ const discriminator = this.getDiscriminator('start_withdrawal_vote');
237
+ const data = Buffer.concat([
238
+ discriminator,
239
+ proposedWithdrawalWallet.toBuffer(),
240
+ ]);
241
+ return new web3.TransactionInstruction({
242
+ programId: this.programId,
243
+ keys: [
244
+ { pubkey: this.wallet.publicKey, isSigner: true, isWritable: true }, // admin
245
+ { pubkey: globalInfoPda, isSigner: false, isWritable: true }, // global_info
246
+ { pubkey: withdrawalVotePda, isSigner: false, isWritable: true }, // withdrawal_vote
247
+ { pubkey: web3.SystemProgram.programId, isSigner: false, isWritable: false }, // system_program
248
+ ],
249
+ data,
250
+ });
251
+ }
252
+ /**
253
+ * Admin/Multisig member: vote_withdrawal_wallet
254
+ *
255
+ * Voter must be one of internal_multisig_wallets in GlobalInfo.
256
+ */
257
+ voteWithdrawalWalletIx(voter) {
258
+ const [globalInfoPda] = this.getGlobalInfoPDA();
259
+ const [withdrawalVotePda] = this.getWithdrawalVotePDA();
260
+ const discriminator = this.getDiscriminator('vote_withdrawal_wallet');
261
+ const data = discriminator;
262
+ return new web3.TransactionInstruction({
263
+ programId: this.programId,
264
+ keys: [
265
+ { pubkey: voter, isSigner: true, isWritable: false }, // voter
266
+ { pubkey: globalInfoPda, isSigner: false, isWritable: true }, // global_info
267
+ { pubkey: withdrawalVotePda, isSigner: false, isWritable: true }, // withdrawal_vote
268
+ ],
269
+ data,
270
+ });
271
+ }
272
+ ////////////////////////////////////////////////////////////////////////
273
+ // Read helpers
274
+ ////////////////////////////////////////////////////////////////////////
275
+ async getGlobalInfo(commitment) {
276
+ const [globalInfoPda] = this.getGlobalInfoPDA();
277
+ const accountInfo = await this.connection.getAccountInfo(globalInfoPda, commitment || 'processed');
278
+ if (!accountInfo) {
279
+ return null;
280
+ }
281
+ const coder = new anchor_1.BorshAccountsCoder(this.idl);
282
+ const globalInfo = coder.decode('GlobalInfo', accountInfo.data);
283
+ return globalInfo;
284
+ }
285
+ async getWithdrawalVote(commitment) {
286
+ const [votePda] = this.getWithdrawalVotePDA();
287
+ const accountInfo = await this.connection.getAccountInfo(votePda, commitment || 'processed');
288
+ if (!accountInfo) {
289
+ return null;
290
+ }
291
+ const coder = new anchor_1.BorshAccountsCoder(this.idl);
292
+ const vote = coder.decode('WithdrawalVote', accountInfo.data);
293
+ return vote;
294
+ }
295
+ ////////////////////////////////////////////////////////////////////////
296
+ // Getters
297
+ ////////////////////////////////////////////////////////////////////////
298
+ getConnection() {
299
+ return this.connection;
300
+ }
301
+ getWallet() {
302
+ return this.wallet;
303
+ }
304
+ getProgramId() {
305
+ return this.programId;
306
+ }
307
+ }
308
+ exports.LfxRewardBackClient = LfxRewardBackClient;