@dripfi/drip-sdk 1.0.10 → 1.0.11
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/.eslintrc.json +38 -0
- package/.vscode/settings.json +17 -0
- package/README.md +150 -28
- package/dist/DripConfig.js +4 -2
- package/dist/DripSdk.d.ts +5 -5
- package/dist/DripSdk.js +81 -70
- package/package.json +7 -7
- package/.prettierignore +0 -2
- package/.prettierrc +0 -11
- package/dist/test.d.ts +0 -2
- package/dist/test.js +0 -24
package/.eslintrc.json
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
{
|
2
|
+
"env": {
|
3
|
+
"browser": true,
|
4
|
+
"es2021": true
|
5
|
+
},
|
6
|
+
"extends": [
|
7
|
+
"eslint:recommended",
|
8
|
+
"plugin:@typescript-eslint/recommended"
|
9
|
+
],
|
10
|
+
"parser": "@typescript-eslint/parser",
|
11
|
+
"parserOptions": {
|
12
|
+
"ecmaVersion": "latest",
|
13
|
+
"sourceType": "module"
|
14
|
+
},
|
15
|
+
"plugins": [
|
16
|
+
"@typescript-eslint"
|
17
|
+
],
|
18
|
+
"rules": {
|
19
|
+
"quotes": [
|
20
|
+
"error",
|
21
|
+
"single"
|
22
|
+
],
|
23
|
+
"semi": "off",
|
24
|
+
"@typescript-eslint/semi": "error",
|
25
|
+
"@typescript-eslint/no-explicit-any": "off",
|
26
|
+
"curly": "error"
|
27
|
+
},
|
28
|
+
"overrides": [
|
29
|
+
{
|
30
|
+
"files": [
|
31
|
+
"tests/**/*"
|
32
|
+
],
|
33
|
+
"env": {
|
34
|
+
"jest": true
|
35
|
+
}
|
36
|
+
}
|
37
|
+
]
|
38
|
+
}
|
@@ -0,0 +1,17 @@
|
|
1
|
+
{
|
2
|
+
"editor.defaultFormatter": "dbaeumer.vscode-eslint",
|
3
|
+
"editor.codeActionsOnSave": {
|
4
|
+
"source.fixAll.eslint": "explicit"
|
5
|
+
},
|
6
|
+
"editor.formatOnSave": true,
|
7
|
+
"[javascript]": {
|
8
|
+
"editor.defaultFormatter": "dbaeumer.vscode-eslint"
|
9
|
+
},
|
10
|
+
"[typescript]": {
|
11
|
+
"editor.defaultFormatter": "dbaeumer.vscode-eslint"
|
12
|
+
},
|
13
|
+
"eslint.workingDirectories": [
|
14
|
+
{"directory": "./src", "changeProcessCWD": true},
|
15
|
+
]
|
16
|
+
}
|
17
|
+
|
package/README.md
CHANGED
@@ -4,7 +4,14 @@
|
|
4
4
|
|
5
5
|
The Drip SDK is a TypeScript library designed to interact with the Drip protocol. It provides methods to retrieve information about Drip Vaults, manage user authentication, and fetch user balances.
|
6
6
|
|
7
|
-
##
|
7
|
+
## Table of Contents
|
8
|
+
- [Installation](#installation)
|
9
|
+
- [Usage](#usage)
|
10
|
+
- [Methods](#methods)
|
11
|
+
- [Examples](#examples)
|
12
|
+
- [Types](#types)
|
13
|
+
|
14
|
+
# Installation
|
8
15
|
|
9
16
|
To use the Drip SDK in your project, you can install it via npm or yarn:
|
10
17
|
|
@@ -12,7 +19,7 @@ To use the Drip SDK in your project, you can install it via npm or yarn:
|
|
12
19
|
npm i @dripfi/drip-sdk
|
13
20
|
```
|
14
21
|
|
15
|
-
|
22
|
+
# Usage
|
16
23
|
|
17
24
|
```typescript
|
18
25
|
import DripSdk from '@drip/sdk'
|
@@ -32,44 +39,146 @@ const signer: ethers.Signer = /* your Signer instance */;
|
|
32
39
|
const dripSdk = new DripSdk(dripConfig, signer);
|
33
40
|
```
|
34
41
|
|
35
|
-
|
42
|
+
# Methods
|
43
|
+
|
44
|
+
| Name | Requires authentication | Description |
|
45
|
+
| ------ | ------ | ----------- |
|
46
|
+
| `getAllVaults(): Promise<Vault[]>`| No | Fetches details of all Drip Vaults. |
|
47
|
+
| `getVaultDetails(vaultAddress: string): Promise<Vault>` | NO | Fetches details of a specific Drip Vault identified by its address. |
|
48
|
+
| `authenticate(): Promise<boolean>` | NO | Initiates the user authentication process and returns a boolean indicating success. |
|
49
|
+
| `isUserAuthenticated(): Promise<AuthenticationStatus>` | NO | Checks if the user is authenticated and returns authentication status along with relevant information. |
|
50
|
+
| `updateSigner(newSigner: Signer): Promise<void>`| NO | Updates the signer for the SDK instance. |
|
51
|
+
| `getUserBalance(vault: Vault): Promise<UserBalance>` | YES | Fetches the user's balance for a specific Drip Vault. |
|
52
|
+
| `deposit(tokenAddress: string, vaultAddress: string, amount: string): Promise<string>` | NO | The deposit function allows you to deposit tokens into a specific vault. Returns txHash. |
|
53
|
+
| `swapAndDeposit(fromTokenAddress: string, toTokenAddress: string, fromTokenAmount: string, vaultAddress: string, ethAmount?: string): Promise<string>` | YES | The swapAndDeposit function allows you to deposit a different token or ether and it will take care of swapping to the correct token before making the deposit. Returns txHash. |
|
54
|
+
| `withdraw(vault: Vault, amountToWithdraw?: string): Promise<string>` | YES | Withdraws tokens from a vault. After withdrawing, you must wait for the withdrawal to be processed by the 'DoHardWork' function, and then you can claim those tokens using claimWithdraws(). Returns txHash. |
|
55
|
+
| `claimWithdraws(vaultAddress: string): Promise<string>` | YES | After the withdrawal has been processed by the 'DoHardWork' function, the 'claimWithdraws' function transfers the withdrawn tokens to their personal account. Returns txHash. |
|
56
|
+
| `fastWithdraw(vault: Vault, amountToWithdraw?: string): Promise<string>` | YES | For users who prefer not to wait for withdrawals to be processed, there is a Fast Withdrawal method. While this approach is more gas-intensive, as users bear the cost of executing the withdrawal process themselves, it allows for instant access to the withdrawn tokens. When utilizing Fast Withdrawal, users immediately receive the tokens, eliminating the need to initiate the 'claimWithdrawal' process separately. Returns txHash. |
|
57
|
+
|
58
|
+
|
59
|
+
> [!IMPORTANT]
|
60
|
+
> **You must authenticate once for every wallet you want to use**
|
36
61
|
|
37
|
-
|
38
|
-
Fetches details of all Drip Vaults.
|
62
|
+
---
|
39
63
|
|
40
|
-
|
41
|
-
Fetches details of a specific Drip Vault identified by its address.
|
64
|
+
# Examples
|
42
65
|
|
43
|
-
|
44
|
-
|
66
|
+
### `deposit(tokenAddress: string, vaultAddress: string, amount: string): Promise<string>`
|
67
|
+
|
68
|
+
I want to deposit 100 USDC in a USDC vault:
|
69
|
+
```typescript
|
70
|
+
USDC_TOKEN_ADDRESS = "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48" --> USDC contract address in tenderly environment
|
71
|
+
```
|
72
|
+
you can get vault token address from the vault.depositToken.tokenAddress
|
45
73
|
|
46
|
-
|
47
|
-
|
74
|
+
```typescript
|
75
|
+
VAULT_ADDRESS = "0x...c167" --> Check vaults returned by getAllVaults() and get vault address like: vault.vaultAddress
|
48
76
|
|
49
|
-
|
50
|
-
|
77
|
+
const txHash = await deposit(USDC_TOKEN_ADDRESS, VAULT_ADDRESS, '100')
|
78
|
+
```
|
51
79
|
|
52
|
-
|
53
|
-
|
80
|
+
> [!NOTE]
|
81
|
+
> User will be prompted with 2 tx:
|
82
|
+
> 1st to approve token usage
|
83
|
+
> 2nd to deposit the funds in the vault
|
54
84
|
|
55
|
-
|
56
|
-
|
85
|
+
> [!NOTE]
|
86
|
+
> Allowance is calculated by the deposit method, based on the current allowance and the required
|
57
87
|
|
58
|
-
|
59
|
-
|
88
|
+
> [!IMPORTANT]
|
89
|
+
> Amount should be formatted, not parsed. In the example we want to deposit 100 USDC so we just input that value instead of adding 6 decimals (100000000)
|
60
90
|
|
61
|
-
|
62
|
-
Withdraws tokens from a vault. After withdrawing, you must wait for the withdrawal to be processed by the 'DoHardWork' function, and then you can claim those tokens using claimWithdraws()
|
91
|
+
---
|
63
92
|
|
64
|
-
|
65
|
-
After the withdrawal has been processed by the 'DoHardWork' function, the 'claimWithdraws' function transfers the withdrawn tokens to their personal account.
|
93
|
+
### `swapAndDeposit(fromTokenAddress: string, toTokenAddress: string, fromTokenAmount: string, vaultAddress: string, ethAmount?: string): Promise<string>`
|
66
94
|
|
67
|
-
|
68
|
-
For users who prefer not to wait for withdrawals to be processed, there is a Fast Withdrawal method. While this approach is more gas-intensive, as users bear the cost of executing the withdrawal process themselves, it allows for instant access to the withdrawn tokens. When utilizing Fast Withdrawal, users immediately receive the tokens, eliminating the need to initiate the 'claimWithdrawal' process separately.
|
95
|
+
I want to deposit 1.5ETH in a WETH vault:
|
69
96
|
|
70
|
-
|
97
|
+
```typescript
|
98
|
+
WETH_TOKEN_ADDRESS = "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48" --> WETH contract address in tenderly environment
|
71
99
|
|
100
|
+
const txHash = await swapAndDeposit(WETH_TOKEN_ADDRESS, WETH_TOKEN_ADDRESS, '0', vaultAddress, '1.5')
|
72
101
|
```
|
102
|
+
you can get vault token address from the vault.depositToken.tokenAddress
|
103
|
+
|
104
|
+
> [!IMPORTANT]
|
105
|
+
> ethAmount and fromTokenAmount should be formatted, not parsed. In the example we want to deposit 1.5 WETH so we just input that value instead of adding 18 decimals (1500000000000000000)
|
106
|
+
|
107
|
+
---
|
108
|
+
|
109
|
+
### `withdraw(vault: Vault, amountToWithdraw?: string): Promise<string>`
|
110
|
+
|
111
|
+
```typescript
|
112
|
+
const userBalanceObject = await getUserBalance()
|
113
|
+
const balance = userBalanceObject.userbalance
|
114
|
+
```
|
115
|
+
> [!NOTE]
|
116
|
+
> If i check balance, that is the max amount I can withdraw
|
117
|
+
|
118
|
+
I want to withdraw 100 USDC from the 1st vault fetched. ---> Let's assume the first vault is a USDC vault for simplicity
|
119
|
+
Fetch vaults returned by getAllVault() and pass the vault you want to withdraw from, and the amount to withdraw (100 in this example)
|
120
|
+
|
121
|
+
```typescript
|
122
|
+
const vaults = await getAllVaults()
|
123
|
+
const vaultToWithdrawFrom = vaults[0]
|
124
|
+
|
125
|
+
const txHash = await withdraw(vaultToWithdrawFrom, '100')
|
126
|
+
```
|
127
|
+
|
128
|
+
if you want to withdraw all funds in the vault, don't specify the amountToWithdraw:
|
129
|
+
|
130
|
+
```typescript
|
131
|
+
const txHash = await withdraw(vaultToWithdrawFrom)
|
132
|
+
```
|
133
|
+
> [!IMPORTANT]
|
134
|
+
> you will have to claim your withdraws after DHW is processed to transfer the withdrawn funds to your wallet
|
135
|
+
|
136
|
+
---
|
137
|
+
|
138
|
+
### `claimWithdraws(vaultAddress: string): Promise<string>`
|
139
|
+
|
140
|
+
```typescript
|
141
|
+
const userBalanceObject = await getUserBalance()
|
142
|
+
const hasWithdrawsToClaim = userBalanceObject.hasWithdrawsToClaim
|
143
|
+
```
|
144
|
+
if hasWithdrawsToClaim is true you have available funds to transfer to your address
|
145
|
+
|
146
|
+
```typescript
|
147
|
+
const txHash = await claimWithdraws()
|
148
|
+
```
|
149
|
+
It will transfer all available funds withdrawn with withdraw() function, to your address
|
150
|
+
|
151
|
+
---
|
152
|
+
|
153
|
+
### `fastWithdraw(vault: Vault, amountToWithdraw?: string): Promise<string>`
|
154
|
+
|
155
|
+
```typescript
|
156
|
+
const userBalanceObject = await getUserBalance()
|
157
|
+
const balance = userBalanceObject.userbalance
|
158
|
+
```
|
159
|
+
|
160
|
+
> [!NOTE]
|
161
|
+
> If i check balance, that is the max amount I can withdraw
|
162
|
+
|
163
|
+
I want to withdraw 3.55 WETH from the 1st vault fetched. ---> Let's assume the first vault is a WETH vault for simplicity
|
164
|
+
Fetch vaults returned by getAllVault() and pass the vault you want to withdraw from, and the amount to withdraw (3.55 in this example)
|
165
|
+
|
166
|
+
```typescript
|
167
|
+
const vaults = await getAllVaults()
|
168
|
+
const vaultToWithdrawFrom = vaults[0]
|
169
|
+
|
170
|
+
const txHash = await fastWithdraw(vaultToWithdrawFrom, '3.55')
|
171
|
+
```
|
172
|
+
|
173
|
+
if you want to withdraw all funds in the vault, don't specify the amountToWithdraw, like:
|
174
|
+
|
175
|
+
```typescript
|
176
|
+
const txHash = await fastWithdraw(vaultToWithdrawFrom)
|
177
|
+
```
|
178
|
+
|
179
|
+
# Types
|
180
|
+
|
181
|
+
```typescript
|
73
182
|
type Vault = {
|
74
183
|
vaultName: string
|
75
184
|
vaultAddress: string
|
@@ -117,7 +226,7 @@ type NFTBoost = {
|
|
117
226
|
multiplier: number
|
118
227
|
nftAddress: string
|
119
228
|
network: string
|
120
|
-
initialBlock: number
|
229
|
+
initialBlock: number
|
121
230
|
imagePath: string
|
122
231
|
}
|
123
232
|
|
@@ -130,7 +239,11 @@ interface StretchGoal {
|
|
130
239
|
}
|
131
240
|
|
132
241
|
type RewardsInformation = {
|
133
|
-
[tokenAddress: string]: {
|
242
|
+
[tokenAddress: string]: {
|
243
|
+
blockNumber: string;
|
244
|
+
rewardrate: string;
|
245
|
+
timestamp: string;
|
246
|
+
endTime: string }
|
134
247
|
}
|
135
248
|
|
136
249
|
type Strategy = {
|
@@ -167,4 +280,13 @@ type SwapInfo = {
|
|
167
280
|
token: string
|
168
281
|
swapCallData: any
|
169
282
|
}
|
283
|
+
|
284
|
+
type UserBalance = {
|
285
|
+
hasWithdrawsToClaim: boolean
|
286
|
+
userBalance: string
|
287
|
+
pendingUserBalance: string
|
288
|
+
pendingWithdrawalBalance: string
|
289
|
+
withdrawableBalance: string
|
290
|
+
};
|
291
|
+
|
170
292
|
```
|
package/dist/DripConfig.js
CHANGED
@@ -18,8 +18,9 @@ class DripConfig {
|
|
18
18
|
}
|
19
19
|
getSwapAndDepositContractAddress(signer) {
|
20
20
|
return __awaiter(this, void 0, void 0, function* () {
|
21
|
-
if (!signer)
|
21
|
+
if (!signer) {
|
22
22
|
throw Error('No signer provided');
|
23
|
+
}
|
23
24
|
if (!this.swapAndDepositContractAddress) {
|
24
25
|
const contract = yield this.internalConfig.getChainAddresses(signer);
|
25
26
|
this.swapAndDepositContractAddress = contract.IDepositSwap;
|
@@ -29,8 +30,9 @@ class DripConfig {
|
|
29
30
|
}
|
30
31
|
getSmartVaultManagerAddress(signer) {
|
31
32
|
return __awaiter(this, void 0, void 0, function* () {
|
32
|
-
if (!signer)
|
33
|
+
if (!signer) {
|
33
34
|
throw Error('No signer provided');
|
35
|
+
}
|
34
36
|
if (!this.smartVaultManagerAddress) {
|
35
37
|
const contract = yield this.internalConfig.getChainAddresses(signer);
|
36
38
|
this.smartVaultManagerAddress = contract.ISmartVaultManager;
|
package/dist/DripSdk.d.ts
CHANGED
@@ -15,11 +15,11 @@ export default class DripSdk {
|
|
15
15
|
isUserAuthenticated(): Promise<AuthenticationStatus>;
|
16
16
|
updateSigner(newSigner: Signer): void;
|
17
17
|
getUserBalance(vault: Vault): Promise<UserBalance>;
|
18
|
-
fastWithdraw(vault: Vault, amountToWithdraw?: string): Promise<
|
19
|
-
deposit(tokenAddress: string, vaultAddress: string, amount: string): Promise<
|
20
|
-
swapAndDeposit(fromTokenAddress: string, toTokenAddress: string, fromTokenAmount: string, vaultAddress: string, ethAmount?: string): Promise<
|
21
|
-
withdraw(vault: Vault, amountToWithdraw?: string): Promise<
|
22
|
-
claimWithdraws(vaultAddress: string): Promise<
|
18
|
+
fastWithdraw(vault: Vault, amountToWithdraw?: string): Promise<string>;
|
19
|
+
deposit(tokenAddress: string, vaultAddress: string, amount: string): Promise<string>;
|
20
|
+
swapAndDeposit(fromTokenAddress: string, toTokenAddress: string, fromTokenAmount: string, vaultAddress: string, ethAmount?: string): Promise<string>;
|
21
|
+
withdraw(vault: Vault, amountToWithdraw?: string): Promise<string>;
|
22
|
+
claimWithdraws(vaultAddress: string): Promise<string>;
|
23
23
|
private generateOldRedeemBagStruct;
|
24
24
|
private generateNewRedeemBagStruct;
|
25
25
|
private getAllSvts;
|
package/dist/DripSdk.js
CHANGED
@@ -41,8 +41,9 @@ class DripSdk {
|
|
41
41
|
authenticate() {
|
42
42
|
return __awaiter(this, void 0, void 0, function* () {
|
43
43
|
try {
|
44
|
-
if (!this.signer)
|
44
|
+
if (!this.signer) {
|
45
45
|
throw Error('No signer provided');
|
46
|
+
}
|
46
47
|
const address = yield this.signer.getAddress();
|
47
48
|
const cookieName = `auth_${address.toLowerCase()}`;
|
48
49
|
const token = yield web3_token_1.default.sign((msg) => __awaiter(this, void 0, void 0, function* () { return yield this.signer.signMessage(msg); }), {
|
@@ -90,8 +91,9 @@ class DripSdk {
|
|
90
91
|
getUserBalance(vault) {
|
91
92
|
return __awaiter(this, void 0, void 0, function* () {
|
92
93
|
const authData = yield this.isUserAuthenticated();
|
93
|
-
if (!authData.isAuthenticated)
|
94
|
+
if (!authData.isAuthenticated) {
|
94
95
|
throw Error(`User not authenticated: ${authData.message}`);
|
96
|
+
}
|
95
97
|
const userAddress = authData.address;
|
96
98
|
const token = authData.token;
|
97
99
|
const dnfts = yield this.dripApi.fetchAllUserDNFTForVault(vault.vaultAddress, userAddress, token);
|
@@ -116,7 +118,7 @@ class DripSdk {
|
|
116
118
|
const [estimatedPendingWithdrawalBalance, estimatedWithdrawableBalance, estimatedWithdrawals] = yield this.calculateAllWithdrawalBalances(wnfts, vault.vaultAddress, decimals, token);
|
117
119
|
const fastWithdrawBalance = yield this.calculateFastWithdrawBalancesOld(authData.token, vault.vaultAddress, userAddress, decimals);
|
118
120
|
const hasWithdrawsToClaim = this.checkIfUserHasWithdrawsToClaim(wnfts);
|
119
|
-
|
121
|
+
const balance = allDeposits
|
120
122
|
.sub(estimatedWithdrawals)
|
121
123
|
.sub(estimatedWithdrawableBalance)
|
122
124
|
.sub(estimatedPendingWithdrawalBalance)
|
@@ -135,46 +137,45 @@ class DripSdk {
|
|
135
137
|
return __awaiter(this, void 0, void 0, function* () {
|
136
138
|
var _a, _b;
|
137
139
|
const authData = yield this.isUserAuthenticated();
|
138
|
-
if (!authData.isAuthenticated)
|
140
|
+
if (!authData.isAuthenticated) {
|
139
141
|
throw Error(`User not authenticated: ${authData.message}`);
|
140
|
-
|
142
|
+
}
|
143
|
+
if (!this.signer) {
|
141
144
|
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
145
|
}
|
155
|
-
|
156
|
-
|
146
|
+
const signerAddress = yield this.signer.getAddress();
|
147
|
+
if (!signerAddress) {
|
148
|
+
throw Error('Error fetching address');
|
149
|
+
}
|
150
|
+
const redeemBagStruct = this.shouldUseNewWithdrawLogic(signerAddress, vault)
|
151
|
+
? yield this.generateNewRedeemBagStruct(authData.token, vault, signerAddress, amountToWithdraw)
|
152
|
+
: yield this.generateOldRedeemBagStruct(authData.token, vault, signerAddress, amountToWithdraw);
|
153
|
+
const currentBlockNumber = yield ((_a = this.signer.provider) === null || _a === void 0 ? void 0 : _a.getBlockNumber());
|
154
|
+
if (!currentBlockNumber) {
|
155
|
+
throw Error('Error fetching block number');
|
157
156
|
}
|
157
|
+
const redeemTx = yield ((_b = this.spoolSdk) === null || _b === void 0 ? void 0 : _b.redeemFast(redeemBagStruct, signerAddress.toLowerCase(), currentBlockNumber));
|
158
|
+
const txReceipt = yield redeemTx.wait();
|
159
|
+
return txReceipt.transactionHash;
|
158
160
|
});
|
159
161
|
}
|
160
162
|
deposit(tokenAddress, vaultAddress, amount) {
|
161
163
|
return __awaiter(this, void 0, void 0, function* () {
|
162
|
-
|
163
|
-
if (!authData.isAuthenticated)
|
164
|
-
throw Error(`User not authenticated: ${authData.message}`);
|
165
|
-
if (!this.signer)
|
164
|
+
if (!this.signer) {
|
166
165
|
throw Error('No signer provided');
|
166
|
+
}
|
167
167
|
const currentTokenAllowance = yield this.getTokenAllowanceForDeposit(tokenAddress);
|
168
168
|
const decimals = yield this.getERC20Precission(tokenAddress);
|
169
|
-
|
169
|
+
const amountToWithdrawFixedDecimals = parseFloat(amount).toFixed(decimals);
|
170
170
|
const amountToDeposit = ethers_1.ethers.utils.parseUnits(amountToWithdrawFixedDecimals, decimals);
|
171
171
|
if (amountToDeposit.gt(currentTokenAllowance)) {
|
172
172
|
const requiredTokenAllowance = amountToDeposit.sub(currentTokenAllowance);
|
173
173
|
yield this.approveTokenForDeposit(tokenAddress, requiredTokenAllowance);
|
174
174
|
}
|
175
175
|
const signerAddress = yield this.signer.getAddress();
|
176
|
-
if (!signerAddress)
|
176
|
+
if (!signerAddress) {
|
177
177
|
throw Error('Error fetching address');
|
178
|
+
}
|
178
179
|
const depositBagStruct = {
|
179
180
|
smartVault: vaultAddress,
|
180
181
|
assets: [amountToDeposit],
|
@@ -183,17 +184,19 @@ class DripSdk {
|
|
183
184
|
doFlush: false,
|
184
185
|
};
|
185
186
|
const depositTx = yield this.spoolSdk.deposit(depositBagStruct);
|
186
|
-
yield depositTx.wait();
|
187
|
+
const txReceipt = yield depositTx.wait();
|
188
|
+
return txReceipt.transactionHash;
|
187
189
|
});
|
188
190
|
}
|
189
191
|
swapAndDeposit(fromTokenAddress, toTokenAddress, fromTokenAmount, vaultAddress, ethAmount) {
|
190
192
|
return __awaiter(this, void 0, void 0, function* () {
|
191
|
-
var _a, _b;
|
192
193
|
const authData = yield this.isUserAuthenticated();
|
193
|
-
if (!authData.isAuthenticated)
|
194
|
+
if (!authData.isAuthenticated) {
|
194
195
|
throw Error(`User not authenticated: ${authData.message}`);
|
195
|
-
|
196
|
+
}
|
197
|
+
if (!this.signer) {
|
196
198
|
throw Error('No signer provided');
|
199
|
+
}
|
197
200
|
const decimals = yield this.getERC20Precission(fromTokenAddress);
|
198
201
|
const amountToWithdrawFixedDecimals = parseFloat(fromTokenAmount).toFixed(decimals);
|
199
202
|
const fromToken = ethers_1.ethers.utils.parseUnits(amountToWithdrawFixedDecimals, decimals);
|
@@ -214,49 +217,47 @@ class DripSdk {
|
|
214
217
|
referral: ethers_1.ethers.constants.AddressZero,
|
215
218
|
doFlush: false,
|
216
219
|
};
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
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());
|
220
|
+
const swapAndDepositRequest = ethAmount
|
221
|
+
? yield this.spoolSdk.swapAndDeposit(swapDepositBagStruct, { value: ethers_1.ethers.utils.parseEther(ethAmount) })
|
222
|
+
: yield this.spoolSdk.swapAndDeposit(swapDepositBagStruct);
|
223
|
+
const txReceipt = yield swapAndDepositRequest.wait();
|
224
|
+
return txReceipt === null || txReceipt === void 0 ? void 0 : txReceipt.transactionHash;
|
226
225
|
});
|
227
226
|
}
|
228
227
|
withdraw(vault, amountToWithdraw) {
|
229
228
|
return __awaiter(this, void 0, void 0, function* () {
|
230
229
|
const authData = yield this.isUserAuthenticated();
|
231
|
-
if (!authData.isAuthenticated)
|
230
|
+
if (!authData.isAuthenticated) {
|
232
231
|
throw Error(`User not authenticated: ${authData.message}`);
|
233
|
-
|
232
|
+
}
|
233
|
+
if (!this.signer) {
|
234
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
235
|
}
|
245
|
-
|
246
|
-
|
236
|
+
const signerAddress = yield this.signer.getAddress();
|
237
|
+
if (!signerAddress) {
|
238
|
+
throw Error('Error fetching address');
|
247
239
|
}
|
240
|
+
const redeemBagStruct = this.shouldUseNewWithdrawLogic(signerAddress, vault)
|
241
|
+
? yield this.generateNewRedeemBagStruct(authData.token, vault, signerAddress, amountToWithdraw)
|
242
|
+
: yield this.generateOldRedeemBagStruct(authData.token, vault, signerAddress, amountToWithdraw);
|
243
|
+
const redeemTx = yield this.spoolSdk.redeem(redeemBagStruct, signerAddress.toLowerCase(), false);
|
244
|
+
const redeemTxReceipt = yield redeemTx.wait();
|
245
|
+
return redeemTxReceipt.transactionHash;
|
248
246
|
});
|
249
247
|
}
|
250
248
|
claimWithdraws(vaultAddress) {
|
251
249
|
return __awaiter(this, void 0, void 0, function* () {
|
252
250
|
const authData = yield this.isUserAuthenticated();
|
253
|
-
if (!authData.isAuthenticated)
|
251
|
+
if (!authData.isAuthenticated) {
|
254
252
|
throw Error(`User not authenticated: ${authData.message}`);
|
255
|
-
|
253
|
+
}
|
254
|
+
if (!this.signer) {
|
256
255
|
throw Error('No signer provided');
|
256
|
+
}
|
257
257
|
const signerAddress = yield this.signer.getAddress();
|
258
|
-
if (!signerAddress)
|
258
|
+
if (!signerAddress) {
|
259
259
|
throw Error('Error fetching address');
|
260
|
+
}
|
260
261
|
const wnfts = yield this.dripApi.fetchEnrichedUserWNFTForVault(vaultAddress, signerAddress, authData.token);
|
261
262
|
//! Shares come as Strings instead of BigNumber from our Backend
|
262
263
|
const nftIds = wnfts
|
@@ -266,7 +267,8 @@ class DripSdk {
|
|
266
267
|
.filter((item) => !item.isBurned && ethers_1.BigNumber.from(item.shares).gt(ethers_1.BigNumber.from('0')) && item.isDHWFinished)
|
267
268
|
.map((item) => item.shares.toString());
|
268
269
|
const claimWithdrawTx = yield this.spoolSdk.claimWithdrawal(vaultAddress, nftIds, nftAmounts, signerAddress.toLowerCase());
|
269
|
-
yield claimWithdrawTx.wait();
|
270
|
+
const txReceipt = yield claimWithdrawTx.wait();
|
271
|
+
return txReceipt.transactionHash;
|
270
272
|
});
|
271
273
|
}
|
272
274
|
generateOldRedeemBagStruct(token, vault, signerAddress, amountToWithdraw) {
|
@@ -283,7 +285,7 @@ class DripSdk {
|
|
283
285
|
}
|
284
286
|
else {
|
285
287
|
//! Not a MAX Withdraw (calculate the shares to withdraw)
|
286
|
-
|
288
|
+
const amountToWithdrawFixedDecimals = parseFloat(amountToWithdraw).toFixed(decimals);
|
287
289
|
sharesToWithdraw = ethers_1.ethers.utils
|
288
290
|
.parseUnits(amountToWithdrawFixedDecimals, decimals)
|
289
291
|
.mul(totalShares)
|
@@ -315,22 +317,23 @@ class DripSdk {
|
|
315
317
|
let dnfts = yield this.dripApi.fetchEnrichedUserDNFTForVault(vault.vaultAddress.toLowerCase(), signerAddress, token);
|
316
318
|
dnfts = yield this.getAllSvts(vault.vaultAddress.toLowerCase(), signerAddress, dnfts, token);
|
317
319
|
let shares = ethers_1.BigNumber.from(0);
|
318
|
-
|
319
|
-
|
320
|
+
const nftIds = [];
|
321
|
+
const nftAmounts = [];
|
320
322
|
let index = 0;
|
321
323
|
while (dnfts[index] && (totalAmountToWithdraw.gt(0) || withdrawAll)) {
|
322
324
|
const dnft = dnfts[index];
|
323
325
|
const userBalance = this.calculateBalanceForDNFT(dnft, decimals);
|
324
326
|
if (userBalance.gt(0)) {
|
325
|
-
|
327
|
+
const amountToWithdraw = totalAmountToWithdraw;
|
326
328
|
const userSvts = dnft.svts;
|
327
329
|
let svtsToWithdraw = ethers_1.BigNumber.from(0);
|
328
330
|
let nftAmount = ethers_1.BigNumber.from(0);
|
329
331
|
if (amountToWithdraw.gte(userBalance) || withdrawAll) {
|
330
332
|
nftAmount = dnft.shares;
|
331
333
|
svtsToWithdraw = userSvts;
|
332
|
-
if (!withdrawAll)
|
334
|
+
if (!withdrawAll) {
|
333
335
|
totalAmountToWithdraw = totalAmountToWithdraw.sub(userBalance);
|
336
|
+
}
|
334
337
|
}
|
335
338
|
else {
|
336
339
|
nftAmount = amountToWithdraw.mul(dnft.shares).div(userBalance);
|
@@ -362,11 +365,13 @@ class DripSdk {
|
|
362
365
|
}
|
363
366
|
getERC20Precission(tokenAddress) {
|
364
367
|
return __awaiter(this, void 0, void 0, function* () {
|
365
|
-
if (!this.signer)
|
368
|
+
if (!this.signer) {
|
366
369
|
throw Error('No signer provided');
|
370
|
+
}
|
367
371
|
const signerAddress = yield this.signer.getAddress();
|
368
|
-
if (!signerAddress)
|
372
|
+
if (!signerAddress) {
|
369
373
|
throw Error('Error fetching address');
|
374
|
+
}
|
370
375
|
const erc20Instance = spool_v2_sdk_1.ERC20__factory.connect(tokenAddress, this.signer);
|
371
376
|
const decimals = yield erc20Instance.decimals();
|
372
377
|
return decimals;
|
@@ -452,9 +457,9 @@ class DripSdk {
|
|
452
457
|
}
|
453
458
|
calculateAllWithdrawalBalances(wnfts, vaultAddress, decimals, token) {
|
454
459
|
return __awaiter(this, void 0, void 0, function* () {
|
455
|
-
if (!this.signer)
|
460
|
+
if (!this.signer) {
|
456
461
|
throw Error('No signer provided');
|
457
|
-
|
462
|
+
}
|
458
463
|
let estimatedPendingWithdrawalBalance = ethers_1.BigNumber.from(0);
|
459
464
|
let estimatedWithdrawableBalance = ethers_1.BigNumber.from(0);
|
460
465
|
let withdrawals = 0;
|
@@ -484,11 +489,13 @@ class DripSdk {
|
|
484
489
|
}
|
485
490
|
getTokenAllowanceForSwapAndDepositContractAddress(tokenAddress) {
|
486
491
|
return __awaiter(this, void 0, void 0, function* () {
|
487
|
-
if (!this.signer)
|
492
|
+
if (!this.signer) {
|
488
493
|
throw Error('No signer provided');
|
494
|
+
}
|
489
495
|
const signerAddress = yield this.signer.getAddress();
|
490
|
-
if (!signerAddress)
|
496
|
+
if (!signerAddress) {
|
491
497
|
throw Error('Error fetching address');
|
498
|
+
}
|
492
499
|
const erc20Instance = spool_v2_sdk_1.ERC20__factory.connect(tokenAddress, this.signer);
|
493
500
|
const swapAndDepositAddress = yield this.dripConfig.getSwapAndDepositContractAddress(this.signer);
|
494
501
|
const allowance = yield erc20Instance.allowance(signerAddress, swapAndDepositAddress);
|
@@ -497,8 +504,9 @@ class DripSdk {
|
|
497
504
|
}
|
498
505
|
approveTokenForSwapAndDepositContract(tokenAddress, amount) {
|
499
506
|
return __awaiter(this, void 0, void 0, function* () {
|
500
|
-
if (!this.signer)
|
507
|
+
if (!this.signer) {
|
501
508
|
throw Error('No signer provided');
|
509
|
+
}
|
502
510
|
const swapAndDepositContractAddress = yield this.dripConfig.getSwapAndDepositContractAddress(this.signer);
|
503
511
|
const erc20Instance = spool_v2_sdk_1.ERC20__factory.connect(tokenAddress, this.signer);
|
504
512
|
const approveTx = yield erc20Instance.approve(swapAndDepositContractAddress, amount);
|
@@ -507,11 +515,13 @@ class DripSdk {
|
|
507
515
|
}
|
508
516
|
getTokenAllowanceForDeposit(tokenAddress) {
|
509
517
|
return __awaiter(this, void 0, void 0, function* () {
|
510
|
-
if (!this.signer)
|
518
|
+
if (!this.signer) {
|
511
519
|
throw Error('No signer provided');
|
520
|
+
}
|
512
521
|
const signerAddress = yield this.signer.getAddress();
|
513
|
-
if (!signerAddress)
|
522
|
+
if (!signerAddress) {
|
514
523
|
throw Error('Error fetching address');
|
524
|
+
}
|
515
525
|
const erc20Instance = spool_v2_sdk_1.ERC20__factory.connect(tokenAddress, this.signer);
|
516
526
|
const smartVaultManagerAddress = yield this.dripConfig.getSmartVaultManagerAddress(this.signer);
|
517
527
|
const allowance = yield erc20Instance.allowance(signerAddress, smartVaultManagerAddress);
|
@@ -520,8 +530,9 @@ class DripSdk {
|
|
520
530
|
}
|
521
531
|
approveTokenForDeposit(tokenAddress, amount) {
|
522
532
|
return __awaiter(this, void 0, void 0, function* () {
|
523
|
-
if (!this.signer)
|
533
|
+
if (!this.signer) {
|
524
534
|
throw Error('No signer provided');
|
535
|
+
}
|
525
536
|
const smartVaultManagerAddress = yield this.dripConfig.getSmartVaultManagerAddress(this.signer);
|
526
537
|
const erc20Instance = spool_v2_sdk_1.ERC20__factory.connect(tokenAddress, this.signer);
|
527
538
|
const approveTx = yield erc20Instance.approve(smartVaultManagerAddress, amount);
|
package/package.json
CHANGED
@@ -1,27 +1,27 @@
|
|
1
1
|
{
|
2
2
|
"name": "@dripfi/drip-sdk",
|
3
|
-
"version": "1.0.
|
3
|
+
"version": "1.0.11",
|
4
4
|
"description": "Drip SDK",
|
5
5
|
"main": "dist/index.js",
|
6
6
|
"types": "dist/index.d.ts",
|
7
7
|
"scripts": {
|
8
8
|
"prepublish": "npm run build",
|
9
9
|
"build": "tsc",
|
10
|
-
"format": "prettier --write .",
|
11
10
|
"test": "echo \"Error: no test specified\" && exit 1"
|
12
11
|
},
|
13
12
|
"dependencies": {
|
14
13
|
"@spool.fi/spool-v2-sdk": "1.0.14",
|
15
14
|
"ethers": "^5.7.2",
|
16
|
-
"
|
17
|
-
"
|
15
|
+
"js-cookie": "^3.0.5",
|
16
|
+
"web3-token": "^1.0.6"
|
18
17
|
},
|
19
18
|
"author": "",
|
20
19
|
"license": "ISC",
|
21
20
|
"devDependencies": {
|
22
|
-
"typescript": "^5.4.5",
|
23
21
|
"@types/js-cookie": "^3.0.6",
|
24
|
-
"
|
25
|
-
"
|
22
|
+
"@typescript-eslint/eslint-plugin": "^7.12.0",
|
23
|
+
"@typescript-eslint/parser": "^7.12.0",
|
24
|
+
"eslint": "^8.57.0",
|
25
|
+
"typescript": "^5.4.5"
|
26
26
|
}
|
27
27
|
}
|
package/.prettierignore
DELETED
package/.prettierrc
DELETED
package/dist/test.d.ts
DELETED
package/dist/test.js
DELETED
@@ -1,24 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.signer = void 0;
|
4
|
-
const ethers_1 = require("ethers");
|
5
|
-
const _1 = require(".");
|
6
|
-
const dripProdConfig = new _1.DripConfig('https://subgraph.satsuma-prod.com/49eb322da234/solidant/spool-v2/api', 'https://pricefeed.v2.spool.fi/', 'https://rewards.v2.spool.fi/', 'https://fastwithdraw.v2.spool.fi/', {
|
7
|
-
1: {
|
8
|
-
ISmartVaultManager: '0x23Daf34e2b9Af02A74dC19cB52Af727B19403874',
|
9
|
-
IDepositSwap: '0xd8534197Bd587F8226d12E0C864ef2CaE6f82f5C',
|
10
|
-
ISmartVaultFactory: '0x8049Fc710D4a1Deea6a6bCeF772C166CEd7A82F5',
|
11
|
-
IDepositManager: '0x823Ba38992825FF37E72B6c3D669a09173B8F7bf',
|
12
|
-
IRewardManager: '0xd8d2C1C3C7982272e3e12dEC5aF681433fdcf003',
|
13
|
-
IStrategyRegistry: '0x554c6bCB54656390aca0a0af38CA954dbE653F15',
|
14
|
-
ISpoolLens: '0x8aa6174333F75421903b2B5c70DdF8DA5D84f74F',
|
15
|
-
},
|
16
|
-
}, 'http://localhost:7070');
|
17
|
-
const provider = new ethers_1.ethers.providers.StaticJsonRpcProvider('https://mainnet.infura.io/v3/fa1bf2cea12147559c9634e80be76d61', 1);
|
18
|
-
exports.signer = new ethers_1.ethers.Wallet('6ffc226f7b7769e27124317372c9dbb579a324e67e97bf07131bf2f59ec0f4fe', provider);
|
19
|
-
const dripSdk = new _1.DripSdk(dripProdConfig, exports.signer);
|
20
|
-
// dripSdk.isUserAuthenticated().then((result) => {
|
21
|
-
// console.log(result)
|
22
|
-
// }).catch((error) => {
|
23
|
-
// console.log(error)
|
24
|
-
// })
|