@zebec-network/zebec-vault-sdk 1.0.8 → 2.0.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.
package/dist/pda.d.ts CHANGED
@@ -1,4 +1,4 @@
1
1
  import { Address } from "@coral-xyz/anchor";
2
2
  import { PublicKey } from "@solana/web3.js";
3
3
  export declare function deriveVaultSigner(vault: Address, programId: Address): [PublicKey, number];
4
- export declare function deriveWhitelist(programId: Address): [PublicKey, number];
4
+ export declare function deriveUserVault(user: Address, programId: Address): [PublicKey, number];
package/dist/pda.js CHANGED
@@ -1,18 +1,18 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.deriveVaultSigner = deriveVaultSigner;
4
- exports.deriveWhitelist = deriveWhitelist;
4
+ exports.deriveUserVault = deriveUserVault;
5
5
  const anchor_1 = require("@coral-xyz/anchor");
6
6
  const web3_js_1 = require("@solana/web3.js");
7
7
  const SEEDS = {
8
+ USER_VAULT: [122, 101, 98, 101, 99, 95, 117, 115, 101, 114, 95, 118, 97, 117, 108, 116],
8
9
  VAULT_SIGNER: [122, 101, 98, 101, 99, 95, 118, 97, 117, 108, 116, 95, 115, 105, 103, 110, 101, 114],
9
- WHITELIST: [122, 101, 98, 101, 99, 95, 118, 97, 117, 108, 116, 95, 119, 104, 105, 116, 101, 108, 105, 115, 116],
10
10
  };
11
11
  function deriveVaultSigner(vault, programId) {
12
12
  const addressAndBump = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from(SEEDS.VAULT_SIGNER), (0, anchor_1.translateAddress)(vault).toBuffer()], (0, anchor_1.translateAddress)(programId));
13
13
  return addressAndBump;
14
14
  }
15
- function deriveWhitelist(programId) {
16
- const addressAndBump = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from(SEEDS.WHITELIST)], (0, anchor_1.translateAddress)(programId));
15
+ function deriveUserVault(user, programId) {
16
+ const addressAndBump = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from(SEEDS.USER_VAULT), (0, anchor_1.translateAddress)(user).toBuffer()], (0, anchor_1.translateAddress)(programId));
17
17
  return addressAndBump;
18
18
  }
package/dist/service.d.ts CHANGED
@@ -3,53 +3,46 @@ import { AccountMeta, Keypair, PublicKey, Signer, TransactionInstruction } from
3
3
  import { TransactionPayload } from "@zebec-network/solana-common";
4
4
  import { ZebecVaultIdlV1 } from "./artifacts";
5
5
  import { RpcNetwork } from "./constants";
6
- import { Numeric, ProposalAction, ProposalInfo, VaultInfo, WhitelistInfo } from "./types";
6
+ import { Numeric, ProposalAction, ProposalInfo, VaultInfo } from "./types";
7
7
  export declare class ZebecVaultService {
8
8
  readonly provider: Provider;
9
9
  readonly program: Program<ZebecVaultIdlV1>;
10
10
  readonly network: RpcNetwork;
11
11
  constructor(provider: Provider, program: Program<ZebecVaultIdlV1>, network: RpcNetwork);
12
12
  static create(provider: Provider, network: RpcNetwork, program?: Program<ZebecVaultIdlV1>): Promise<ZebecVaultService>;
13
- getCreateVaultInstruction(payer: PublicKey, vault: PublicKey, owner: PublicKey, signerBump: number): Promise<TransactionInstruction>;
14
- getDepositSolInstruction(depositor: PublicKey, vault: PublicKey, amount: BN): Promise<TransactionInstruction>;
15
- getWithdrawSolInstruction(withdrawer: PublicKey, vault: PublicKey, amount: BN): Promise<TransactionInstruction>;
16
- getDepositTokenInstruction(depositor: PublicKey, vault: PublicKey, tokenMint: PublicKey, amount: BN, decimals: number): Promise<TransactionInstruction>;
17
- getWithdrawTokenInstruction(withdrawer: PublicKey, vault: PublicKey, tokenMint: PublicKey, amount: BN, decimals: number): Promise<TransactionInstruction>;
18
- getCreateProposalInstruction(proposer: PublicKey, vault: PublicKey, proposal: PublicKey, name: string, actions: ProposalAction[], proposalAccountSize: number): Promise<TransactionInstruction>;
13
+ getCreateVaultInstruction(payer: PublicKey, owner: PublicKey, signerBump: number): Promise<TransactionInstruction>;
14
+ getDepositSolInstruction(depositor: PublicKey, amount: BN): Promise<TransactionInstruction>;
15
+ getWithdrawSolInstruction(withdrawer: PublicKey, amount: BN): Promise<TransactionInstruction>;
16
+ getDepositTokenInstruction(depositor: PublicKey, tokenMint: PublicKey, amount: BN, decimals: number): Promise<TransactionInstruction>;
17
+ getWithdrawTokenInstruction(withdrawer: PublicKey, tokenMint: PublicKey, amount: BN, decimals: number): Promise<TransactionInstruction>;
18
+ getCreateProposalInstruction(proposer: PublicKey, proposal: PublicKey, name: string, actions: ProposalAction[], proposalAccountSize: number): Promise<TransactionInstruction>;
19
19
  getAppendActionInstruction(proposer: PublicKey, proposal: PublicKey, actions: ProposalAction[], newProposalAccountSize: number): Promise<TransactionInstruction>;
20
20
  getDeleteProposalInstruction(proposal: PublicKey): Promise<TransactionInstruction>;
21
21
  getExecuteProposalInstruction(caller: PublicKey, proposal: PublicKey, remainingAccounts: AccountMeta[]): Promise<TransactionInstruction>;
22
- getExecuteProposalDirectInstruction(vault: PublicKey, proposer: PublicKey, actions: ProposalAction[], remainingAccounts: AccountMeta[]): Promise<TransactionInstruction>;
23
- getWhitelistTokenInstruction(whitelister: PublicKey, tokenAddresses: PublicKey[]): Promise<TransactionInstruction>;
22
+ getExecuteProposalDirectInstruction(proposer: PublicKey, actions: ProposalAction[], remainingAccounts: AccountMeta[]): Promise<TransactionInstruction>;
24
23
  createVault(params: {
25
24
  payer?: Address;
26
- vaultKeypair?: Keypair;
27
25
  }): Promise<TransactionPayload>;
28
26
  depositSol(params: {
29
27
  depositor?: Address;
30
- vault: Address;
31
28
  amount: Numeric;
32
29
  }): Promise<TransactionPayload>;
33
30
  withdrawSol(params: {
34
31
  withdrawer?: Address;
35
- vault: Address;
36
32
  amount: Numeric;
37
33
  }): Promise<TransactionPayload>;
38
34
  depositToken(params: {
39
35
  depositor?: Address;
40
- vault: Address;
41
36
  tokenMint: Address;
42
37
  amount: Numeric;
43
38
  }): Promise<TransactionPayload>;
44
39
  withdrawToken(params: {
45
40
  withdrawer?: Address;
46
- vault: Address;
47
41
  tokenMint: Address;
48
42
  amount: Numeric;
49
43
  }): Promise<TransactionPayload>;
50
44
  createProposal(params: {
51
45
  proposer?: Address;
52
- vault: Address;
53
46
  proposalKeypair?: Keypair;
54
47
  name: string;
55
48
  actions: TransactionInstruction[];
@@ -70,21 +63,15 @@ export declare class ZebecVaultService {
70
63
  addressLookupTables?: Address[];
71
64
  }): Promise<TransactionPayload>;
72
65
  executeProposalDirect(params: {
73
- vault: Address;
74
66
  proposer?: Address;
75
67
  actions: TransactionInstruction[];
76
68
  partialSigners?: Signer[];
77
69
  addressLookupTables?: Address[];
78
70
  }): Promise<TransactionPayload>;
79
- whitelistTokens(params: {
80
- whitelister?: Address;
81
- tokenAddresses: Address[];
82
- }): Promise<TransactionPayload>;
83
71
  private _createTransactionPayload;
84
- getVaultsInfoOfUser(user?: Address): Promise<VaultInfo[]>;
72
+ getVaultInfoOfUser(user?: Address): Promise<VaultInfo | null>;
85
73
  getAllVaultsInfo(): Promise<VaultInfo[]>;
86
74
  getProposalsInfoOfVault(vault: Address): Promise<ProposalInfo[]>;
87
- getWhitelistInfo(): Promise<WhitelistInfo | null>;
88
75
  get programId(): PublicKey;
89
76
  get connection(): import("@solana/web3.js").Connection;
90
77
  }
package/dist/service.js CHANGED
@@ -24,7 +24,7 @@ class ZebecVaultService {
24
24
  program = program ?? new anchor_1.Program(artifacts_1.ZEBEC_VAULT_IDL_V1, provider);
25
25
  return new ZebecVaultService(provider, program, network);
26
26
  }
27
- async getCreateVaultInstruction(payer, vault, owner, signerBump) {
27
+ async getCreateVaultInstruction(payer, owner, signerBump) {
28
28
  return this.program.methods
29
29
  .createVault({
30
30
  owner,
@@ -32,59 +32,54 @@ class ZebecVaultService {
32
32
  })
33
33
  .accounts({
34
34
  payer,
35
- vault,
36
35
  })
37
36
  .instruction();
38
37
  }
39
- async getDepositSolInstruction(depositor, vault, amount) {
38
+ async getDepositSolInstruction(depositor, amount) {
40
39
  return this.program.methods
41
40
  .depositSol({
42
41
  amount,
43
42
  })
44
43
  .accounts({
45
- vault,
46
44
  depositor,
47
45
  })
48
46
  .instruction();
49
47
  }
50
- async getWithdrawSolInstruction(withdrawer, vault, amount) {
48
+ async getWithdrawSolInstruction(withdrawer, amount) {
51
49
  return this.program.methods
52
50
  .withdrawSol({
53
51
  amount,
54
52
  })
55
53
  .accounts({
56
- vault,
57
54
  withdrawer,
58
55
  })
59
56
  .instruction();
60
57
  }
61
- async getDepositTokenInstruction(depositor, vault, tokenMint, amount, decimals) {
58
+ async getDepositTokenInstruction(depositor, tokenMint, amount, decimals) {
62
59
  return this.program.methods
63
60
  .depositToken({
64
61
  amount,
65
62
  decimals,
66
63
  })
67
64
  .accounts({
68
- vault,
69
65
  depositor,
70
66
  tokenMint,
71
67
  })
72
68
  .instruction();
73
69
  }
74
- async getWithdrawTokenInstruction(withdrawer, vault, tokenMint, amount, decimals) {
70
+ async getWithdrawTokenInstruction(withdrawer, tokenMint, amount, decimals) {
75
71
  return this.program.methods
76
72
  .withdrawToken({
77
73
  amount,
78
74
  decimals,
79
75
  })
80
76
  .accounts({
81
- vault,
82
77
  withdrawer,
83
78
  tokenMint,
84
79
  })
85
80
  .instruction();
86
81
  }
87
- async getCreateProposalInstruction(proposer, vault, proposal, name, actions, proposalAccountSize) {
82
+ async getCreateProposalInstruction(proposer, proposal, name, actions, proposalAccountSize) {
88
83
  return this.program.methods
89
84
  .createProposal({
90
85
  actions,
@@ -92,7 +87,6 @@ class ZebecVaultService {
92
87
  proposalAccountSize,
93
88
  })
94
89
  .accounts({
95
- vault,
96
90
  proposal,
97
91
  proposer,
98
92
  })
@@ -128,46 +122,34 @@ class ZebecVaultService {
128
122
  .remainingAccounts(remainingAccounts)
129
123
  .instruction();
130
124
  }
131
- async getExecuteProposalDirectInstruction(vault, proposer, actions, remainingAccounts) {
125
+ async getExecuteProposalDirectInstruction(proposer, actions, remainingAccounts) {
132
126
  return this.program.methods
133
127
  .executeProposalDirect({
134
128
  actions,
135
129
  })
136
130
  .accounts({
137
- vault,
138
131
  proposer,
139
132
  })
140
133
  .remainingAccounts(remainingAccounts)
141
134
  .instruction();
142
135
  }
143
- async getWhitelistTokenInstruction(whitelister, tokenAddresses) {
144
- return this.program.methods
145
- .whitelistToken({
146
- tokenAddresses,
147
- })
148
- .accounts({
149
- whitelister,
150
- })
151
- .instruction();
152
- }
153
136
  async createVault(params) {
154
137
  const payer = params.payer ? (0, anchor_1.translateAddress)(params.payer) : this.provider.publicKey;
155
138
  if (!payer) {
156
139
  throw new Error("Either provide a payer or use AnchorProvider for provider in the service");
157
140
  }
158
- const vaultKeypair = params.vaultKeypair ?? web3_js_1.Keypair.generate();
159
- const [, signerBump] = (0, pda_1.deriveVaultSigner)(vaultKeypair.publicKey, this.programId);
160
- const ix = await this.getCreateVaultInstruction(payer, vaultKeypair.publicKey, payer, signerBump);
161
- return this._createTransactionPayload(payer, [ix], [vaultKeypair]);
141
+ const [vault] = (0, pda_1.deriveUserVault)(payer, this.programId);
142
+ const [, signerBump] = (0, pda_1.deriveVaultSigner)(vault, this.programId);
143
+ const ix = await this.getCreateVaultInstruction(payer, payer, signerBump);
144
+ return this._createTransactionPayload(payer, [ix]);
162
145
  }
163
146
  async depositSol(params) {
164
147
  const depositor = params.depositor ? (0, anchor_1.translateAddress)(params.depositor) : this.provider.publicKey;
165
148
  if (!depositor) {
166
149
  throw new Error("Either provide a depositor or use AnchorProvider for provider in the service");
167
150
  }
168
- const vault = (0, anchor_1.translateAddress)(params.vault);
169
151
  const amount = new anchor_1.BN((0, solana_common_1.parseSol)(params.amount).toString());
170
- const ix = await this.getDepositSolInstruction(depositor, vault, amount);
152
+ const ix = await this.getDepositSolInstruction(depositor, amount);
171
153
  return this._createTransactionPayload(depositor, [ix]);
172
154
  }
173
155
  async withdrawSol(params) {
@@ -175,9 +157,8 @@ class ZebecVaultService {
175
157
  if (!withdrawer) {
176
158
  throw new Error("Either provide a withdrawer or use AnchorProvider for provider in the service");
177
159
  }
178
- const vault = (0, anchor_1.translateAddress)(params.vault);
179
160
  const amount = new anchor_1.BN((0, solana_common_1.parseSol)(params.amount).toString());
180
- const ix = await this.getWithdrawSolInstruction(withdrawer, vault, amount);
161
+ const ix = await this.getWithdrawSolInstruction(withdrawer, amount);
181
162
  return this._createTransactionPayload(withdrawer, [ix]);
182
163
  }
183
164
  async depositToken(params) {
@@ -185,11 +166,10 @@ class ZebecVaultService {
185
166
  if (!depositor) {
186
167
  throw new Error("Either provide a depositor or use AnchorProvider for provider in the service");
187
168
  }
188
- const vault = (0, anchor_1.translateAddress)(params.vault);
189
169
  const tokenMint = (0, anchor_1.translateAddress)(params.tokenMint);
190
170
  const decimals = await (0, solana_common_1.getMintDecimals)(this.provider.connection, tokenMint);
191
171
  const amount = new anchor_1.BN((0, solana_common_1.parseToken)(params.amount, decimals).toString());
192
- const ix = await this.getDepositTokenInstruction(depositor, vault, tokenMint, amount, decimals);
172
+ const ix = await this.getDepositTokenInstruction(depositor, tokenMint, amount, decimals);
193
173
  return this._createTransactionPayload(depositor, [ix]);
194
174
  }
195
175
  async withdrawToken(params) {
@@ -197,11 +177,10 @@ class ZebecVaultService {
197
177
  if (!withdrawer) {
198
178
  throw new Error("Either provide a withdrawer or use AnchorProvider for provider in the service");
199
179
  }
200
- const vault = (0, anchor_1.translateAddress)(params.vault);
201
180
  const tokenMint = (0, anchor_1.translateAddress)(params.tokenMint);
202
181
  const decimals = await (0, solana_common_1.getMintDecimals)(this.provider.connection, tokenMint);
203
182
  const amount = new anchor_1.BN((0, solana_common_1.parseToken)(params.amount, decimals).toString());
204
- const ix = await this.getWithdrawTokenInstruction(withdrawer, vault, tokenMint, amount, decimals);
183
+ const ix = await this.getWithdrawTokenInstruction(withdrawer, tokenMint, amount, decimals);
205
184
  return this._createTransactionPayload(withdrawer, [ix]);
206
185
  }
207
186
  async createProposal(params) {
@@ -209,7 +188,6 @@ class ZebecVaultService {
209
188
  if (!proposer) {
210
189
  throw new Error("Either provide a proposer or use AnchorProvider for provider in the service");
211
190
  }
212
- const vault = (0, anchor_1.translateAddress)(params.vault);
213
191
  const proposalKeypair = params.proposalKeypair ?? web3_js_1.Keypair.generate();
214
192
  const actions = params.actions.map((ix) => ({
215
193
  accountSpecs: ix.keys,
@@ -220,7 +198,7 @@ class ZebecVaultService {
220
198
  if (proposalAccountSize > 10_000) {
221
199
  throw new Error("Proposal size exceeds maximum allowed size of 10,000 bytes");
222
200
  }
223
- const ix = await this.getCreateProposalInstruction(proposer, vault, proposalKeypair.publicKey, params.name, actions, proposalAccountSize);
201
+ const ix = await this.getCreateProposalInstruction(proposer, proposalKeypair.publicKey, params.name, actions, proposalAccountSize);
224
202
  return this._createTransactionPayload(proposer, [ix], [proposalKeypair]);
225
203
  }
226
204
  async appendActions(params) {
@@ -301,7 +279,7 @@ class ZebecVaultService {
301
279
  if (!proposer) {
302
280
  throw new Error("Either provide a caller or use AnchorProvider for provider in the service");
303
281
  }
304
- const vault = (0, anchor_1.translateAddress)(params.vault);
282
+ const [vault] = (0, pda_1.deriveUserVault)(proposer, this.programId);
305
283
  const [vaultSigner] = (0, pda_1.deriveVaultSigner)(vault, this.programId);
306
284
  const actions = params.actions.map((ix) => ({
307
285
  accountSpecs: ix.keys,
@@ -321,7 +299,7 @@ class ZebecVaultService {
321
299
  });
322
300
  return acc;
323
301
  }, []);
324
- const ix = await this.getExecuteProposalDirectInstruction(vault, proposer, actions, remainingAccounts);
302
+ const ix = await this.getExecuteProposalDirectInstruction(proposer, actions, remainingAccounts);
325
303
  const addressLookupTableAccounts = [];
326
304
  if (params.addressLookupTables) {
327
305
  const promises = params.addressLookupTables.map(async (lookupTable) => {
@@ -335,15 +313,6 @@ class ZebecVaultService {
335
313
  }
336
314
  return this._createTransactionPayload(proposer, [ix], params.partialSigners, addressLookupTableAccounts);
337
315
  }
338
- async whitelistTokens(params) {
339
- const whitelister = params.whitelister ? (0, anchor_1.translateAddress)(params.whitelister) : this.provider.publicKey;
340
- if (!whitelister) {
341
- throw new Error("Either provide a whitelister or use AnchorProvider for provider in the service");
342
- }
343
- const tokens = params.tokenAddresses.map((address) => (0, anchor_1.translateAddress)(address));
344
- const ix = await this.getWhitelistTokenInstruction(whitelister, tokens);
345
- return this._createTransactionPayload(whitelister, [ix]);
346
- }
347
316
  async _createTransactionPayload(payerKey, instructions, signers, addressLookupTableAccounts) {
348
317
  const errorMap = new Map();
349
318
  this.program.idl.errors.forEach((error) => errorMap.set(error.code, error.msg));
@@ -356,40 +325,22 @@ class ZebecVaultService {
356
325
  }
357
326
  return new solana_common_1.TransactionPayload(this.provider.connection, errorMap, instructions, payerKey, signers, addressLookupTableAccounts, signTransaction);
358
327
  }
359
- async getVaultsInfoOfUser(user) {
328
+ async getVaultInfoOfUser(user) {
360
329
  user = user ? (0, anchor_1.translateAddress)(user) : this.provider.publicKey;
361
330
  if (!user) {
362
331
  throw new Error("Either provide a user or use AnchorProvider for provider in the service");
363
332
  }
364
- const accountInfos = await this.connection.getProgramAccounts(this.programId, {
365
- commitment: this.connection.commitment,
366
- filters: [
367
- {
368
- memcmp: {
369
- offset: 0, // offset for discriminator in Vault
370
- bytes: anchor_1.utils.bytes.bs58.encode(this.program.idl.accounts[1].discriminator),
371
- encoding: "base58",
372
- },
373
- },
374
- {
375
- memcmp: {
376
- offset: 8, // offset for owner field in Vault
377
- bytes: user.toBase58(),
378
- encoding: "base58",
379
- },
380
- },
381
- ],
382
- });
383
- const vaults = accountInfos.map((accountInfo) => {
384
- const vaultAccount = this.program.coder.accounts.decode(this.program.idl.accounts[1].name, accountInfo.account.data);
385
- return {
386
- vault: accountInfo.pubkey,
387
- owner: vaultAccount.owner,
388
- createdDate: vaultAccount.createdDate.toNumber(),
389
- signerBump: vaultAccount.signerBump,
390
- };
391
- });
392
- return vaults;
333
+ const [vault] = (0, pda_1.deriveUserVault)(user, this.programId);
334
+ const vaultAccount = await this.program.account.vault.fetchNullable(vault, this.connection.commitment);
335
+ if (!vaultAccount) {
336
+ return null;
337
+ }
338
+ return {
339
+ createdDate: vaultAccount.createdDate.toNumber(),
340
+ owner: vaultAccount.owner,
341
+ signerBump: vaultAccount.signerBump,
342
+ vault,
343
+ };
393
344
  }
394
345
  async getAllVaultsInfo() {
395
346
  const accountInfos = await this.connection.getProgramAccounts(this.programId, {
@@ -451,17 +402,6 @@ class ZebecVaultService {
451
402
  });
452
403
  return proposals;
453
404
  }
454
- async getWhitelistInfo() {
455
- const [whitelist] = (0, pda_1.deriveWhitelist)(this.programId);
456
- const whitelistAccount = await this.program.account.whitelist.fetchNullable(whitelist, this.connection.commitment);
457
- if (!whitelistAccount) {
458
- return null;
459
- }
460
- return {
461
- ...whitelistAccount,
462
- whitelist,
463
- };
464
- }
465
405
  get programId() {
466
406
  return this.program.programId;
467
407
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zebec-network/zebec-vault-sdk",
3
- "version": "1.0.8",
3
+ "version": "2.0.0",
4
4
  "description": "An SDK for zebec vault solana program",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -21,11 +21,14 @@
21
21
  "build": "npm run clean && tsc",
22
22
  "clean": "rimraf ./dist",
23
23
  "format": "prettier --write .",
24
- "test": "ts-mocha -p ./tsconfig.json -t 1000000000"
24
+ "test:single": "ts-mocha -p ./tsconfig.json -t 1000000000",
25
+ "test": "ts-mocha -p ./tsconfig.json -t 1000000000 test/**/*.test.ts"
25
26
  },
26
27
  "devDependencies": {
27
28
  "@types/mocha": "^10.0.10",
28
29
  "@types/node": "^24.0.1",
30
+ "@zebec-network/core-utils": "^1.0.4",
31
+ "@zebec-network/zebec-card-v2-sdk": "^1.1.0",
29
32
  "@zebec-network/zebec-stake-sdk": "^1.0.10",
30
33
  "dotenv": "^16.5.0",
31
34
  "mocha": "^11.6.0",