@zebec-network/zebec-vault-sdk 1.1.0 → 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/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,13 +122,12 @@ 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)
@@ -145,19 +138,18 @@ class ZebecVaultService {
145
138
  if (!payer) {
146
139
  throw new Error("Either provide a payer or use AnchorProvider for provider in the service");
147
140
  }
148
- const vaultKeypair = params.vaultKeypair ?? web3_js_1.Keypair.generate();
149
- const [, signerBump] = (0, pda_1.deriveVaultSigner)(vaultKeypair.publicKey, this.programId);
150
- const ix = await this.getCreateVaultInstruction(payer, vaultKeypair.publicKey, payer, signerBump);
151
- 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]);
152
145
  }
153
146
  async depositSol(params) {
154
147
  const depositor = params.depositor ? (0, anchor_1.translateAddress)(params.depositor) : this.provider.publicKey;
155
148
  if (!depositor) {
156
149
  throw new Error("Either provide a depositor or use AnchorProvider for provider in the service");
157
150
  }
158
- const vault = (0, anchor_1.translateAddress)(params.vault);
159
151
  const amount = new anchor_1.BN((0, solana_common_1.parseSol)(params.amount).toString());
160
- const ix = await this.getDepositSolInstruction(depositor, vault, amount);
152
+ const ix = await this.getDepositSolInstruction(depositor, amount);
161
153
  return this._createTransactionPayload(depositor, [ix]);
162
154
  }
163
155
  async withdrawSol(params) {
@@ -165,9 +157,8 @@ class ZebecVaultService {
165
157
  if (!withdrawer) {
166
158
  throw new Error("Either provide a withdrawer or use AnchorProvider for provider in the service");
167
159
  }
168
- const vault = (0, anchor_1.translateAddress)(params.vault);
169
160
  const amount = new anchor_1.BN((0, solana_common_1.parseSol)(params.amount).toString());
170
- const ix = await this.getWithdrawSolInstruction(withdrawer, vault, amount);
161
+ const ix = await this.getWithdrawSolInstruction(withdrawer, amount);
171
162
  return this._createTransactionPayload(withdrawer, [ix]);
172
163
  }
173
164
  async depositToken(params) {
@@ -175,11 +166,10 @@ class ZebecVaultService {
175
166
  if (!depositor) {
176
167
  throw new Error("Either provide a depositor or use AnchorProvider for provider in the service");
177
168
  }
178
- const vault = (0, anchor_1.translateAddress)(params.vault);
179
169
  const tokenMint = (0, anchor_1.translateAddress)(params.tokenMint);
180
170
  const decimals = await (0, solana_common_1.getMintDecimals)(this.provider.connection, tokenMint);
181
171
  const amount = new anchor_1.BN((0, solana_common_1.parseToken)(params.amount, decimals).toString());
182
- const ix = await this.getDepositTokenInstruction(depositor, vault, tokenMint, amount, decimals);
172
+ const ix = await this.getDepositTokenInstruction(depositor, tokenMint, amount, decimals);
183
173
  return this._createTransactionPayload(depositor, [ix]);
184
174
  }
185
175
  async withdrawToken(params) {
@@ -187,11 +177,10 @@ class ZebecVaultService {
187
177
  if (!withdrawer) {
188
178
  throw new Error("Either provide a withdrawer or use AnchorProvider for provider in the service");
189
179
  }
190
- const vault = (0, anchor_1.translateAddress)(params.vault);
191
180
  const tokenMint = (0, anchor_1.translateAddress)(params.tokenMint);
192
181
  const decimals = await (0, solana_common_1.getMintDecimals)(this.provider.connection, tokenMint);
193
182
  const amount = new anchor_1.BN((0, solana_common_1.parseToken)(params.amount, decimals).toString());
194
- const ix = await this.getWithdrawTokenInstruction(withdrawer, vault, tokenMint, amount, decimals);
183
+ const ix = await this.getWithdrawTokenInstruction(withdrawer, tokenMint, amount, decimals);
195
184
  return this._createTransactionPayload(withdrawer, [ix]);
196
185
  }
197
186
  async createProposal(params) {
@@ -199,7 +188,6 @@ class ZebecVaultService {
199
188
  if (!proposer) {
200
189
  throw new Error("Either provide a proposer or use AnchorProvider for provider in the service");
201
190
  }
202
- const vault = (0, anchor_1.translateAddress)(params.vault);
203
191
  const proposalKeypair = params.proposalKeypair ?? web3_js_1.Keypair.generate();
204
192
  const actions = params.actions.map((ix) => ({
205
193
  accountSpecs: ix.keys,
@@ -210,7 +198,7 @@ class ZebecVaultService {
210
198
  if (proposalAccountSize > 10_000) {
211
199
  throw new Error("Proposal size exceeds maximum allowed size of 10,000 bytes");
212
200
  }
213
- 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);
214
202
  return this._createTransactionPayload(proposer, [ix], [proposalKeypair]);
215
203
  }
216
204
  async appendActions(params) {
@@ -291,7 +279,7 @@ class ZebecVaultService {
291
279
  if (!proposer) {
292
280
  throw new Error("Either provide a caller or use AnchorProvider for provider in the service");
293
281
  }
294
- const vault = (0, anchor_1.translateAddress)(params.vault);
282
+ const [vault] = (0, pda_1.deriveUserVault)(proposer, this.programId);
295
283
  const [vaultSigner] = (0, pda_1.deriveVaultSigner)(vault, this.programId);
296
284
  const actions = params.actions.map((ix) => ({
297
285
  accountSpecs: ix.keys,
@@ -311,7 +299,7 @@ class ZebecVaultService {
311
299
  });
312
300
  return acc;
313
301
  }, []);
314
- const ix = await this.getExecuteProposalDirectInstruction(vault, proposer, actions, remainingAccounts);
302
+ const ix = await this.getExecuteProposalDirectInstruction(proposer, actions, remainingAccounts);
315
303
  const addressLookupTableAccounts = [];
316
304
  if (params.addressLookupTables) {
317
305
  const promises = params.addressLookupTables.map(async (lookupTable) => {
@@ -337,40 +325,22 @@ class ZebecVaultService {
337
325
  }
338
326
  return new solana_common_1.TransactionPayload(this.provider.connection, errorMap, instructions, payerKey, signers, addressLookupTableAccounts, signTransaction);
339
327
  }
340
- async getVaultsInfoOfUser(user) {
328
+ async getVaultInfoOfUser(user) {
341
329
  user = user ? (0, anchor_1.translateAddress)(user) : this.provider.publicKey;
342
330
  if (!user) {
343
331
  throw new Error("Either provide a user or use AnchorProvider for provider in the service");
344
332
  }
345
- const accountInfos = await this.connection.getProgramAccounts(this.programId, {
346
- commitment: this.connection.commitment,
347
- filters: [
348
- {
349
- memcmp: {
350
- offset: 0, // offset for discriminator in Vault
351
- bytes: anchor_1.utils.bytes.bs58.encode(this.program.idl.accounts[1].discriminator),
352
- encoding: "base58",
353
- },
354
- },
355
- {
356
- memcmp: {
357
- offset: 8, // offset for owner field in Vault
358
- bytes: user.toBase58(),
359
- encoding: "base58",
360
- },
361
- },
362
- ],
363
- });
364
- const vaults = accountInfos.map((accountInfo) => {
365
- const vaultAccount = this.program.coder.accounts.decode(this.program.idl.accounts[1].name, accountInfo.account.data);
366
- return {
367
- vault: accountInfo.pubkey,
368
- owner: vaultAccount.owner,
369
- createdDate: vaultAccount.createdDate.toNumber(),
370
- signerBump: vaultAccount.signerBump,
371
- };
372
- });
373
- 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
+ };
374
344
  }
375
345
  async getAllVaultsInfo() {
376
346
  const accountInfos = await this.connection.getProgramAccounts(this.programId, {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zebec-network/zebec-vault-sdk",
3
- "version": "1.1.0",
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",
@@ -27,6 +27,8 @@
27
27
  "devDependencies": {
28
28
  "@types/mocha": "^10.0.10",
29
29
  "@types/node": "^24.0.1",
30
+ "@zebec-network/core-utils": "^1.0.4",
31
+ "@zebec-network/zebec-card-v2-sdk": "^1.1.0",
30
32
  "@zebec-network/zebec-stake-sdk": "^1.0.10",
31
33
  "dotenv": "^16.5.0",
32
34
  "mocha": "^11.6.0",