@dripfi/drip-sdk 1.0.9 → 1.0.10

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/DripApi.d.ts CHANGED
@@ -11,6 +11,7 @@ export default class DripApi {
11
11
  fetchUserSVTBalance(vaultAddress: string, walletAddress: string, token: string): Promise<BigNumber>;
12
12
  fetchUserBalance(vaultAddress: string, walletAddress: string, token: string): Promise<Record<string, string>>;
13
13
  fetchEnrichedUserDNFTForVault(vaultAddress: string, walletAddress: string, token: string): Promise<any[]>;
14
+ fetchEnrichedUserWNFTForVault(vaultAddress: string, walletAddress: string, token: string): Promise<any[]>;
14
15
  fetchUserSVTFromNfts(vaultAddress: string, userAddress: string, dnfts: number[], token: string): Promise<BigNumber[]>;
15
16
  fetchAllUserWNFTForVault(vaultAddress: string, walletAddress: string, token: string): Promise<any[]>;
16
17
  fetchAllUserDNFTForVault(vaultAddress: string, walletAddress: string, token: string): Promise<any[]>;
package/dist/DripApi.js CHANGED
@@ -78,6 +78,17 @@ class DripApi {
78
78
  return data;
79
79
  });
80
80
  }
81
+ fetchEnrichedUserWNFTForVault(vaultAddress, walletAddress, token) {
82
+ return __awaiter(this, void 0, void 0, function* () {
83
+ const headers = new Headers();
84
+ headers.append('Authorization', token);
85
+ const res = yield fetch(`/api-be/api/spool/user/wNft/${walletAddress}/${vaultAddress}`, {
86
+ headers,
87
+ });
88
+ const data = yield res.json();
89
+ return data;
90
+ });
91
+ }
81
92
  fetchUserSVTFromNfts(vaultAddress, userAddress, dnfts, token) {
82
93
  return __awaiter(this, void 0, void 0, function* () {
83
94
  const headers = new Headers();
package/dist/DripSdk.d.ts CHANGED
@@ -32,7 +32,6 @@ export default class DripSdk {
32
32
  private calculateBalanceForDNFT;
33
33
  private checkIfUserHasWithdrawsToClaim;
34
34
  private shouldUseNewWithdrawLogic;
35
- private generateToken;
36
35
  private calculateAllWithdrawalBalances;
37
36
  private getTokenAllowanceForSwapAndDepositContractAddress;
38
37
  private approveTokenForSwapAndDepositContract;
package/dist/DripSdk.js CHANGED
@@ -98,7 +98,7 @@ class DripSdk {
98
98
  const wnfts = yield this.dripApi.fetchAllUserWNFTForVault(vault.vaultAddress, userAddress, token);
99
99
  const decimals = yield this.getERC20Precission(vault.depositToken.tokenAddress);
100
100
  if (this.shouldUseNewWithdrawLogic(userAddress, vault)) {
101
- const [estimatedPendingWithdrawalBalance, estimatedWithdrawableBalance] = yield this.calculateAllWithdrawalBalances(wnfts, vault.vaultAddress, decimals);
101
+ const [estimatedPendingWithdrawalBalance, estimatedWithdrawableBalance] = yield this.calculateAllWithdrawalBalances(wnfts, vault.vaultAddress, decimals, token);
102
102
  const hasWithdrawsToClaim = this.checkIfUserHasWithdrawsToClaim(wnfts);
103
103
  const pendingDeposits = this.calculatePendingDeposits(dnfts, decimals);
104
104
  const userBalance = this.calculateAllDeposits(dnfts, decimals).sub(pendingDeposits);
@@ -113,8 +113,8 @@ class DripSdk {
113
113
  else {
114
114
  const allDeposits = this.calculateDepositsOld(dnfts, decimals);
115
115
  const pendingDeposits = this.calculatePendingDepositsOld(dnfts, decimals);
116
- const [estimatedPendingWithdrawalBalance, estimatedWithdrawableBalance, estimatedWithdrawals] = yield this.calculateAllWithdrawalBalances(wnfts, vault.vaultAddress, decimals);
117
- const fastWithdrawBalance = yield this.calculateFastWithdrawBalancesOld(vault.vaultAddress, userAddress, decimals);
116
+ const [estimatedPendingWithdrawalBalance, estimatedWithdrawableBalance, estimatedWithdrawals] = yield this.calculateAllWithdrawalBalances(wnfts, vault.vaultAddress, decimals, token);
117
+ const fastWithdrawBalance = yield this.calculateFastWithdrawBalancesOld(authData.token, vault.vaultAddress, userAddress, decimals);
118
118
  const hasWithdrawsToClaim = this.checkIfUserHasWithdrawsToClaim(wnfts);
119
119
  let balance = allDeposits
120
120
  .sub(estimatedWithdrawals)
@@ -133,105 +133,144 @@ class DripSdk {
133
133
  }
134
134
  fastWithdraw(vault, amountToWithdraw) {
135
135
  return __awaiter(this, void 0, void 0, function* () {
136
- // if (!this.signer) throw Error('No signer provided')
137
- // try {
138
- // const signerAddress = await this.signer.getAddress()
139
- // if (!signerAddress) throw Error('Error fetching address')
140
- // const redeemBagStruct: RedeemBagStruct = this.shouldUseNewWithdrawLogic(signerAddress, vault)
141
- // ? await this.generateNewRedeemBagStruct(vault, signerAddress, amountToWithdraw)
142
- // : await this.generateOldRedeemBagStruct(vault, signerAddress, amountToWithdraw)
143
- // const currentBlockNumber = await this.signer.provider?.getBlockNumber()
144
- // if (!currentBlockNumber) throw Error('Error fetching block number')
145
- // const redeemTx = await this.spoolSdk?.redeemFast(
146
- // redeemBagStruct,
147
- // signerAddress.toLowerCase(),
148
- // currentBlockNumber,
149
- // )
150
- // const redeemTxReceipt = await redeemTx!.wait()
151
- // } catch (error) {
152
- // console.log(error)
153
- // }
136
+ var _a, _b;
137
+ const authData = yield this.isUserAuthenticated();
138
+ if (!authData.isAuthenticated)
139
+ throw Error(`User not authenticated: ${authData.message}`);
140
+ if (!this.signer)
141
+ throw Error('No signer provided');
142
+ try {
143
+ const signerAddress = yield this.signer.getAddress();
144
+ if (!signerAddress)
145
+ throw Error('Error fetching address');
146
+ const redeemBagStruct = this.shouldUseNewWithdrawLogic(signerAddress, vault)
147
+ ? yield this.generateNewRedeemBagStruct(authData.token, vault, signerAddress, amountToWithdraw)
148
+ : yield this.generateOldRedeemBagStruct(authData.token, vault, signerAddress, amountToWithdraw);
149
+ const currentBlockNumber = yield ((_a = this.signer.provider) === null || _a === void 0 ? void 0 : _a.getBlockNumber());
150
+ if (!currentBlockNumber)
151
+ throw Error('Error fetching block number');
152
+ const redeemTx = yield ((_b = this.spoolSdk) === null || _b === void 0 ? void 0 : _b.redeemFast(redeemBagStruct, signerAddress.toLowerCase(), currentBlockNumber));
153
+ const redeemTxReceipt = yield redeemTx.wait();
154
+ }
155
+ catch (error) {
156
+ console.log(error);
157
+ }
154
158
  });
155
159
  }
156
160
  deposit(tokenAddress, vaultAddress, amount) {
157
161
  return __awaiter(this, void 0, void 0, function* () {
158
- // if (!this.signer) throw Error('No signer provided')
159
- // const currentTokenAllowance = await this.getTokenAllowanceForDeposit(tokenAddress)
160
- // const decimals = await this.getERC20Precission(tokenAddress)
161
- // let amountToWithdrawFixedDecimals = parseFloat(amount).toFixed(decimals)
162
- // const amountToDeposit = ethers.utils.parseUnits(amountToWithdrawFixedDecimals, decimals)
163
- // if (amountToDeposit.gt(currentTokenAllowance)) {
164
- // const requiredTokenAllowance = amountToDeposit.sub(currentTokenAllowance)
165
- // await this.approveTokenForDeposit(tokenAddress, requiredTokenAllowance)
166
- // }
167
- // const signerAddress = await this.signer.getAddress()
168
- // if (!signerAddress) throw Error('Error fetching address')
169
- // const depositBagStruct: DepositBagStruct = {
170
- // smartVault: vaultAddress,
171
- // assets: [amountToDeposit],
172
- // receiver: signerAddress,
173
- // referral: ethers.constants.AddressZero,
174
- // doFlush: false,
175
- // }
176
- // const depositTx = await this.spoolSdk!.deposit(depositBagStruct)
177
- // await depositTx.wait()
162
+ const authData = yield this.isUserAuthenticated();
163
+ if (!authData.isAuthenticated)
164
+ throw Error(`User not authenticated: ${authData.message}`);
165
+ if (!this.signer)
166
+ throw Error('No signer provided');
167
+ const currentTokenAllowance = yield this.getTokenAllowanceForDeposit(tokenAddress);
168
+ const decimals = yield this.getERC20Precission(tokenAddress);
169
+ let amountToWithdrawFixedDecimals = parseFloat(amount).toFixed(decimals);
170
+ const amountToDeposit = ethers_1.ethers.utils.parseUnits(amountToWithdrawFixedDecimals, decimals);
171
+ if (amountToDeposit.gt(currentTokenAllowance)) {
172
+ const requiredTokenAllowance = amountToDeposit.sub(currentTokenAllowance);
173
+ yield this.approveTokenForDeposit(tokenAddress, requiredTokenAllowance);
174
+ }
175
+ const signerAddress = yield this.signer.getAddress();
176
+ if (!signerAddress)
177
+ throw Error('Error fetching address');
178
+ const depositBagStruct = {
179
+ smartVault: vaultAddress,
180
+ assets: [amountToDeposit],
181
+ receiver: signerAddress,
182
+ referral: ethers_1.ethers.constants.AddressZero,
183
+ doFlush: false,
184
+ };
185
+ const depositTx = yield this.spoolSdk.deposit(depositBagStruct);
186
+ yield depositTx.wait();
178
187
  });
179
188
  }
180
189
  swapAndDeposit(fromTokenAddress, toTokenAddress, fromTokenAmount, vaultAddress, ethAmount) {
181
190
  return __awaiter(this, void 0, void 0, function* () {
182
- // if (!this.signer) throw Error('No signer provided')
183
- // const decimals = await this.getERC20Precission(fromTokenAddress)
184
- // const amountToWithdrawFixedDecimals = parseFloat(fromTokenAmount).toFixed(decimals)
185
- // const fromToken = ethers.utils.parseUnits(amountToWithdrawFixedDecimals, decimals)
186
- // const signerAddress = await this.signer.getAddress()
187
- // if (fromToken.gt(BigNumber.from(0))) {
188
- // const currentTokenAllowance = await this.getTokenAllowanceForSwapAndDepositContractAddress(fromTokenAddress)
189
- // if (fromToken.gt(currentTokenAllowance)) {
190
- // await this.approveTokenForSwapAndDepositContract(fromTokenAddress, fromToken.sub(currentTokenAllowance))
191
- // }
192
- // }
193
- // const swapInfo = await this.dripApi.getSwapInfo(fromTokenAddress, toTokenAddress, fromToken, signerAddress)
194
- // const swapDepositBagStruct = {
195
- // inTokens: [fromTokenAddress],
196
- // inAmounts: [fromToken],
197
- // smartVault: vaultAddress,
198
- // swapInfo,
199
- // receiver: signerAddress,
200
- // referral: ethers.constants.AddressZero,
201
- // doFlush: false,
202
- // }
203
- // let swapAndDepositRequest: ethers.ContractTransaction | undefined
204
- // if (ethAmount) {
205
- // const eth = ethers.utils.parseEther(ethAmount)
206
- // swapAndDepositRequest = await this.spoolSdk?.swapAndDeposit(swapDepositBagStruct, { value: eth })
207
- // } else {
208
- // swapAndDepositRequest = await this.spoolSdk?.swapAndDeposit(swapDepositBagStruct)
209
- // }
210
- // await swapAndDepositRequest?.wait()
191
+ var _a, _b;
192
+ const authData = yield this.isUserAuthenticated();
193
+ if (!authData.isAuthenticated)
194
+ throw Error(`User not authenticated: ${authData.message}`);
195
+ if (!this.signer)
196
+ throw Error('No signer provided');
197
+ const decimals = yield this.getERC20Precission(fromTokenAddress);
198
+ const amountToWithdrawFixedDecimals = parseFloat(fromTokenAmount).toFixed(decimals);
199
+ const fromToken = ethers_1.ethers.utils.parseUnits(amountToWithdrawFixedDecimals, decimals);
200
+ const signerAddress = yield this.signer.getAddress();
201
+ if (fromToken.gt(ethers_1.BigNumber.from(0))) {
202
+ const currentTokenAllowance = yield this.getTokenAllowanceForSwapAndDepositContractAddress(fromTokenAddress);
203
+ if (fromToken.gt(currentTokenAllowance)) {
204
+ yield this.approveTokenForSwapAndDepositContract(fromTokenAddress, fromToken.sub(currentTokenAllowance));
205
+ }
206
+ }
207
+ const swapInfo = yield this.dripApi.getSwapInfo(fromTokenAddress, toTokenAddress, fromToken, signerAddress);
208
+ const swapDepositBagStruct = {
209
+ inTokens: [fromTokenAddress],
210
+ inAmounts: [fromToken],
211
+ smartVault: vaultAddress,
212
+ swapInfo,
213
+ receiver: signerAddress,
214
+ referral: ethers_1.ethers.constants.AddressZero,
215
+ doFlush: false,
216
+ };
217
+ let swapAndDepositRequest;
218
+ if (ethAmount) {
219
+ const eth = ethers_1.ethers.utils.parseEther(ethAmount);
220
+ swapAndDepositRequest = yield ((_a = this.spoolSdk) === null || _a === void 0 ? void 0 : _a.swapAndDeposit(swapDepositBagStruct, { value: eth }));
221
+ }
222
+ else {
223
+ swapAndDepositRequest = yield ((_b = this.spoolSdk) === null || _b === void 0 ? void 0 : _b.swapAndDeposit(swapDepositBagStruct));
224
+ }
225
+ yield (swapAndDepositRequest === null || swapAndDepositRequest === void 0 ? void 0 : swapAndDepositRequest.wait());
211
226
  });
212
227
  }
213
228
  withdraw(vault, amountToWithdraw) {
214
229
  return __awaiter(this, void 0, void 0, function* () {
215
- // if (!this.signer) throw Error('No signer provided')
216
- // try {
217
- // const signerAddress = await this.signer.getAddress()
218
- // if (!signerAddress) throw Error('Error fetching address')
219
- // const redeemBagStruct: RedeemBagStruct = this.shouldUseNewWithdrawLogic(signerAddress, vault)
220
- // ? await this.generateNewRedeemBagStruct(vault, signerAddress, amountToWithdraw)
221
- // : await this.generateOldRedeemBagStruct(vault, signerAddress, amountToWithdraw)
222
- // const redeemTx = await this.spoolSdk!.redeem(redeemBagStruct, signerAddress.toLowerCase(), false)
223
- // const redeemTxReceipt = await redeemTx.wait()
224
- // } catch (error) {
225
- // console.log(error)
226
- // }
230
+ const authData = yield this.isUserAuthenticated();
231
+ if (!authData.isAuthenticated)
232
+ throw Error(`User not authenticated: ${authData.message}`);
233
+ if (!this.signer)
234
+ throw Error('No signer provided');
235
+ try {
236
+ const signerAddress = yield this.signer.getAddress();
237
+ if (!signerAddress)
238
+ throw Error('Error fetching address');
239
+ const redeemBagStruct = this.shouldUseNewWithdrawLogic(signerAddress, vault)
240
+ ? yield this.generateNewRedeemBagStruct(authData.token, vault, signerAddress, amountToWithdraw)
241
+ : yield this.generateOldRedeemBagStruct(authData.token, vault, signerAddress, amountToWithdraw);
242
+ const redeemTx = yield this.spoolSdk.redeem(redeemBagStruct, signerAddress.toLowerCase(), false);
243
+ const redeemTxReceipt = yield redeemTx.wait();
244
+ }
245
+ catch (error) {
246
+ console.log(error);
247
+ }
227
248
  });
228
249
  }
229
250
  claimWithdraws(vaultAddress) {
230
- return __awaiter(this, void 0, void 0, function* () { });
251
+ return __awaiter(this, void 0, void 0, function* () {
252
+ const authData = yield this.isUserAuthenticated();
253
+ if (!authData.isAuthenticated)
254
+ throw Error(`User not authenticated: ${authData.message}`);
255
+ if (!this.signer)
256
+ throw Error('No signer provided');
257
+ const signerAddress = yield this.signer.getAddress();
258
+ if (!signerAddress)
259
+ throw Error('Error fetching address');
260
+ const wnfts = yield this.dripApi.fetchEnrichedUserWNFTForVault(vaultAddress, signerAddress, authData.token);
261
+ //! Shares come as Strings instead of BigNumber from our Backend
262
+ const nftIds = wnfts
263
+ .filter((item) => !item.isBurned && ethers_1.BigNumber.from(item.shares).gt(ethers_1.BigNumber.from('0')) && item.isDHWFinished)
264
+ .map((item) => item.nftId.toString());
265
+ const nftAmounts = wnfts
266
+ .filter((item) => !item.isBurned && ethers_1.BigNumber.from(item.shares).gt(ethers_1.BigNumber.from('0')) && item.isDHWFinished)
267
+ .map((item) => item.shares.toString());
268
+ const claimWithdrawTx = yield this.spoolSdk.claimWithdrawal(vaultAddress, nftIds, nftAmounts, signerAddress.toLowerCase());
269
+ yield claimWithdrawTx.wait();
270
+ });
231
271
  }
232
- generateOldRedeemBagStruct(vault, signerAddress, amountToWithdraw) {
272
+ generateOldRedeemBagStruct(token, vault, signerAddress, amountToWithdraw) {
233
273
  return __awaiter(this, void 0, void 0, function* () {
234
- const token = yield this.generateToken();
235
274
  const dnfts = yield this.dripApi.fetchEnrichedUserDNFTForVault(vault.vaultAddress, signerAddress, token);
236
275
  const userBalance = yield this.dripApi.fetchUserBalance(vault.vaultAddress, signerAddress, token);
237
276
  const totalTokenBalance = ethers_1.BigNumber.from(userBalance[vault.depositToken.tokenAddress]);
@@ -267,15 +306,14 @@ class DripSdk {
267
306
  };
268
307
  });
269
308
  }
270
- generateNewRedeemBagStruct(vault, signerAddress, amountToWithdraw) {
309
+ generateNewRedeemBagStruct(token, vault, signerAddress, amountToWithdraw) {
271
310
  return __awaiter(this, void 0, void 0, function* () {
272
- const token = yield this.generateToken();
273
311
  const decimals = yield this.getERC20Precission(vault.depositToken.tokenAddress.toLowerCase());
274
312
  const withdrawAll = !amountToWithdraw;
275
313
  const initialAmountToWithdraw = ethers_1.ethers.utils.parseUnits(amountToWithdraw || '0', decimals);
276
314
  let totalAmountToWithdraw = initialAmountToWithdraw;
277
315
  let dnfts = yield this.dripApi.fetchEnrichedUserDNFTForVault(vault.vaultAddress.toLowerCase(), signerAddress, token);
278
- dnfts = yield this.getAllSvts(vault.vaultAddress.toLowerCase(), signerAddress, dnfts);
316
+ dnfts = yield this.getAllSvts(vault.vaultAddress.toLowerCase(), signerAddress, dnfts, token);
279
317
  let shares = ethers_1.BigNumber.from(0);
280
318
  let nftIds = [];
281
319
  let nftAmounts = [];
@@ -313,9 +351,8 @@ class DripSdk {
313
351
  };
314
352
  });
315
353
  }
316
- getAllSvts(vaultAddress, userAddress, dnfts) {
354
+ getAllSvts(vaultAddress, userAddress, dnfts, token) {
317
355
  return __awaiter(this, void 0, void 0, function* () {
318
- const token = yield this.generateToken();
319
356
  const svts = yield this.dripApi.fetchUserSVTFromNfts(vaultAddress, userAddress, dnfts.map((element) => parseInt(element.nftId)), token);
320
357
  const result = dnfts.map((element, index) => {
321
358
  return { nftId: element.nftId, assets: element.assets, shares: element.shares, svts: svts[index] };
@@ -335,9 +372,8 @@ class DripSdk {
335
372
  return decimals;
336
373
  });
337
374
  }
338
- calculateFastWithdrawBalancesOld(vaultAddress, userAddress, decimals) {
375
+ calculateFastWithdrawBalancesOld(token, vaultAddress, userAddress, decimals) {
339
376
  return __awaiter(this, void 0, void 0, function* () {
340
- const token = yield this.generateToken();
341
377
  const fastWithdrawNFTs = yield this.dripApi.fetchFastWithdrawNFTs(vaultAddress, userAddress, token);
342
378
  let fastWithdrawBalance = 0;
343
379
  for (const wnft of fastWithdrawNFTs) {
@@ -414,15 +450,7 @@ class DripSdk {
414
450
  }
415
451
  return vault.newWithdraw;
416
452
  }
417
- generateToken() {
418
- return __awaiter(this, void 0, void 0, function* () {
419
- if (!this.signer)
420
- throw Error('No signer provided');
421
- const token = yield web3_token_1.default.sign((msg) => __awaiter(this, void 0, void 0, function* () { return yield this.signer.signMessage(msg); }), '1d');
422
- return token;
423
- });
424
- }
425
- calculateAllWithdrawalBalances(wnfts, vaultAddress, decimals) {
453
+ calculateAllWithdrawalBalances(wnfts, vaultAddress, decimals, token) {
426
454
  return __awaiter(this, void 0, void 0, function* () {
427
455
  if (!this.signer)
428
456
  throw Error('No signer provided');
@@ -437,7 +465,6 @@ class DripSdk {
437
465
  continue;
438
466
  }
439
467
  const currentBlockNumber = wnft.blockNumber - 1;
440
- const token = yield this.generateToken();
441
468
  const assetsPerSvtAtBlock = yield this.dripApi.fetchAssetPerSvtAtBlock(vaultAddress, currentBlockNumber, token);
442
469
  const estimatedValueOfNFT = ethers_1.BigNumber.from(parseInt(ethers_1.ethers.utils.formatUnits(ethers_1.BigNumber.from(wnft.svtWithdrawn).mul(assetsPerSvtAtBlock), 36)).toString());
443
470
  if (wnft.isDHWFinished) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dripfi/drip-sdk",
3
- "version": "1.0.9",
3
+ "version": "1.0.10",
4
4
  "description": "Drip SDK",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",