@dripfi/drip-sdk 1.4.28-silo-sdk-2 → 1.4.28-silo-sdk-3
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.
|
@@ -1,11 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
3
|
exports.SiloVaultOperations = void 0;
|
|
7
4
|
const ethers_1 = require("ethers");
|
|
8
|
-
const ERC20TokenContract_1 = __importDefault(require("../contracts/ERC20TokenContract"));
|
|
9
5
|
/**
|
|
10
6
|
* Silo SDK vault operations implementation
|
|
11
7
|
*/
|
|
@@ -24,11 +20,9 @@ class SiloVaultOperations {
|
|
|
24
20
|
// For Silo, we use the standard ERC20 allowance
|
|
25
21
|
const allowance = await this.perqSdk.tokenUtils.getERC20TokenAllowance(vaultAddress, sourceTokenAddress);
|
|
26
22
|
console.log('🚀 ~ allowance:', allowance);
|
|
27
|
-
// Get
|
|
28
|
-
const
|
|
29
|
-
|
|
30
|
-
const requiredAmount = ethers_1.ethers.utils.parseUnits(amount, decimals);
|
|
31
|
-
console.log('🚀 ~ requiredAmount:', requiredAmount);
|
|
23
|
+
// Get required amount with correct decimals
|
|
24
|
+
const requiredAmount = await this.perqSdk.tokenUtils.parseAmountWithDecimals(amount, sourceTokenAddress);
|
|
25
|
+
console.log('🚀 ~ requiredAmount:', requiredAmount.toString());
|
|
32
26
|
return allowance.gte(requiredAmount);
|
|
33
27
|
}
|
|
34
28
|
catch (error) {
|
|
@@ -42,16 +36,20 @@ class SiloVaultOperations {
|
|
|
42
36
|
if (!this.perqSdk.siloPackage) {
|
|
43
37
|
throw new Error('Silo package not initialized. Please provide a signer.');
|
|
44
38
|
}
|
|
39
|
+
const amountToApprove = await this.perqSdk.tokenUtils.parseAmountWithDecimals(amount, sourceTokenAddress);
|
|
40
|
+
console.log('🚀 ~ amountToApprove:', amountToApprove.toString());
|
|
45
41
|
// For Silo, we use the standard ERC20 approval
|
|
46
|
-
return await this.perqSdk.tokenUtils.approveAllowance(sourceTokenAddress,
|
|
42
|
+
return await this.perqSdk.tokenUtils.approveAllowance(sourceTokenAddress, amountToApprove.toString(), vaultAddress);
|
|
47
43
|
}
|
|
48
44
|
async deposit(params) {
|
|
49
|
-
const { amount, vaultAddress } = params;
|
|
45
|
+
const { amount, vaultAddress, sourceTokenAddress } = params;
|
|
50
46
|
console.log(`🚀 ~ silo deposit called with these params:`, { amount, vaultAddress });
|
|
51
47
|
if (!this.perqSdk.siloPackage) {
|
|
52
48
|
throw new Error('Silo package not initialized. Please provide a signer.');
|
|
53
49
|
}
|
|
54
|
-
const
|
|
50
|
+
const parsedAmount = await this.perqSdk.tokenUtils.parseAmountWithDecimals(amount, sourceTokenAddress);
|
|
51
|
+
console.log('🚀 ~ parsedAmount:', parsedAmount.toString());
|
|
52
|
+
const tx = await this.perqSdk.siloPackage.deposit(parsedAmount.toString(), vaultAddress);
|
|
55
53
|
return this.extractTransactionHash(tx);
|
|
56
54
|
}
|
|
57
55
|
async withdraw(params) {
|
|
@@ -60,17 +58,18 @@ class SiloVaultOperations {
|
|
|
60
58
|
if (!this.perqSdk.siloPackage) {
|
|
61
59
|
throw new Error('Silo package not initialized. Please provide a signer.');
|
|
62
60
|
}
|
|
63
|
-
|
|
61
|
+
const parsedAmount = await this.perqSdk.tokenUtils.parseAmountWithDecimals(amount, sourceTokenAddress);
|
|
62
|
+
console.log('🚀 ~ parsedAmount:', parsedAmount.toString());
|
|
63
|
+
return await this.handleSiloWithdraw(sourceTokenAddress, vaultAddress, parsedAmount.toString());
|
|
64
64
|
}
|
|
65
65
|
async getBalance(vaultAddress, tokenAddress) {
|
|
66
66
|
if (!this.perqSdk.signer) {
|
|
67
67
|
throw new Error('Signer not initialized');
|
|
68
68
|
}
|
|
69
|
-
const tokenContract = new ERC20TokenContract_1.default(tokenAddress, this.perqSdk.signer);
|
|
70
|
-
const decimals = await tokenContract.getPrecission();
|
|
71
69
|
const balance = await this.perqSdk.siloPackage.getBalanceInAssets(vaultAddress);
|
|
72
|
-
const formattedBalance =
|
|
73
|
-
|
|
70
|
+
const formattedBalance = await this.perqSdk.tokenUtils.parseAmountWithDecimals(balance.toString(), tokenAddress);
|
|
71
|
+
console.log('🚀 ~ formattedBalance:', formattedBalance.toString());
|
|
72
|
+
return formattedBalance.toString();
|
|
74
73
|
}
|
|
75
74
|
/**
|
|
76
75
|
* Handles Silo withdraw with automatic full/partial withdraw detection
|
|
@@ -80,13 +79,14 @@ class SiloVaultOperations {
|
|
|
80
79
|
let userBalance = ethers_1.BigNumber.from(0);
|
|
81
80
|
try {
|
|
82
81
|
userBalance = await this.perqSdk.siloPackage.getBalanceInAssets(vaultAddress);
|
|
82
|
+
console.log('🚀 ~ userBalance:', userBalance.toString());
|
|
83
83
|
}
|
|
84
84
|
catch (error) {
|
|
85
85
|
console.error('Error getting user balance for silo withdraw: ', error);
|
|
86
86
|
}
|
|
87
|
-
|
|
88
|
-
const
|
|
89
|
-
|
|
87
|
+
// Parse withdraw amount with correct decimals
|
|
88
|
+
const parsedWithdrawAmount = await this.perqSdk.tokenUtils.parseAmountWithDecimals(amount, tokenAddress);
|
|
89
|
+
console.log('🚀 ~ parsedWithdrawAmount:', parsedWithdrawAmount.toString());
|
|
90
90
|
try {
|
|
91
91
|
if (parsedWithdrawAmount.gte(userBalance)) {
|
|
92
92
|
return await this.handleSiloFullWithdraw(vaultAddress);
|
|
@@ -3,6 +3,13 @@ import PerqSdk from '../PerqSdk';
|
|
|
3
3
|
export default class TokenUtilsPackage {
|
|
4
4
|
private perqSdk;
|
|
5
5
|
constructor(perqSdk: PerqSdk);
|
|
6
|
+
/**
|
|
7
|
+
* Helper method to parse amount with correct token decimals
|
|
8
|
+
* @param amount - The amount as a string
|
|
9
|
+
* @param tokenAddress - The token contract address
|
|
10
|
+
* @returns Promise<BigNumber> - The parsed amount with correct decimals
|
|
11
|
+
*/
|
|
12
|
+
parseAmountWithDecimals(amount: string, tokenAddress: string): Promise<BigNumber>;
|
|
6
13
|
getTokenPrice(tokenName: string): Promise<number>;
|
|
7
14
|
getAllowance(tokenAddress: string, spender: string): Promise<string>;
|
|
8
15
|
approveAllowance(tokenAddress: string, amount: string, spenderAddress: string): Promise<string>;
|
|
@@ -11,6 +11,20 @@ class TokenUtilsPackage {
|
|
|
11
11
|
constructor(perqSdk) {
|
|
12
12
|
this.perqSdk = perqSdk;
|
|
13
13
|
}
|
|
14
|
+
/**
|
|
15
|
+
* Helper method to parse amount with correct token decimals
|
|
16
|
+
* @param amount - The amount as a string
|
|
17
|
+
* @param tokenAddress - The token contract address
|
|
18
|
+
* @returns Promise<BigNumber> - The parsed amount with correct decimals
|
|
19
|
+
*/
|
|
20
|
+
async parseAmountWithDecimals(amount, tokenAddress) {
|
|
21
|
+
if (!this.perqSdk.signer) {
|
|
22
|
+
throw new Error('No signer provided');
|
|
23
|
+
}
|
|
24
|
+
const tokenContract = new ERC20TokenContract_1.default(tokenAddress, this.perqSdk.signer);
|
|
25
|
+
const decimals = await tokenContract.getPrecission();
|
|
26
|
+
return ethers_1.ethers.utils.parseUnits(amount, decimals);
|
|
27
|
+
}
|
|
14
28
|
async getTokenPrice(tokenName) {
|
|
15
29
|
return this.perqSdk.perqApi.fetchTokenPrice(tokenName);
|
|
16
30
|
}
|
|
@@ -27,8 +41,7 @@ class TokenUtilsPackage {
|
|
|
27
41
|
throw Error('No signer provided');
|
|
28
42
|
}
|
|
29
43
|
const tokenContract = new ERC20TokenContract_1.default(tokenAddress, this.perqSdk.signer);
|
|
30
|
-
const
|
|
31
|
-
const parsedAmount = ethers_1.ethers.utils.parseUnits(parseFloat(amount).toFixed(decimals), decimals);
|
|
44
|
+
const parsedAmount = await this.parseAmountWithDecimals(amount, tokenAddress);
|
|
32
45
|
return await tokenContract.approveToken(spenderAddress, parsedAmount.toString());
|
|
33
46
|
}
|
|
34
47
|
async transferErc20Token(tokenAddress, amount, receiver) {
|
|
@@ -36,8 +49,8 @@ class TokenUtilsPackage {
|
|
|
36
49
|
throw Error('No signer provided');
|
|
37
50
|
}
|
|
38
51
|
const tokenContract = new ERC20TokenContract_1.default(tokenAddress, this.perqSdk.signer);
|
|
39
|
-
const
|
|
40
|
-
const txHash = await tokenContract.transfer(receiver,
|
|
52
|
+
const parsedAmount = await this.parseAmountWithDecimals(amount, tokenAddress);
|
|
53
|
+
const txHash = await tokenContract.transfer(receiver, parsedAmount.toString());
|
|
41
54
|
return txHash;
|
|
42
55
|
}
|
|
43
56
|
async wrapEther(amount, tokenAddress) {
|
|
@@ -1,11 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
3
|
exports.YelayVaultOperations = void 0;
|
|
7
4
|
const ethers_1 = require("ethers");
|
|
8
|
-
const ERC20TokenContract_1 = __importDefault(require("../contracts/ERC20TokenContract"));
|
|
9
5
|
class YelayVaultOperations {
|
|
10
6
|
perqSdk;
|
|
11
7
|
constructor(perqSdk) {
|
|
@@ -44,10 +40,8 @@ class YelayVaultOperations {
|
|
|
44
40
|
default:
|
|
45
41
|
throw new Error(`Unsupported deposit type: ${depositType}`);
|
|
46
42
|
}
|
|
47
|
-
// Get
|
|
48
|
-
const
|
|
49
|
-
const decimals = await tokenContract.getPrecission();
|
|
50
|
-
const requiredAmount = ethers_1.ethers.utils.parseUnits(amount, decimals);
|
|
43
|
+
// Get required amount with correct decimals
|
|
44
|
+
const requiredAmount = await this.perqSdk.tokenUtils.parseAmountWithDecimals(amount, sourceTokenAddress);
|
|
51
45
|
return allowance.gte(requiredAmount);
|
|
52
46
|
}
|
|
53
47
|
catch (error) {
|
|
@@ -67,13 +61,15 @@ class YelayVaultOperations {
|
|
|
67
61
|
throw new Error('OnChainProjectId is required for Yelay vaults');
|
|
68
62
|
}
|
|
69
63
|
const depositType = this.determineDepositType(params);
|
|
64
|
+
const parsedAmount = await this.perqSdk.tokenUtils.parseAmountWithDecimals(amount, sourceTokenAddress);
|
|
65
|
+
console.log('🚀 ~ parsedAmount:', parsedAmount.toString());
|
|
70
66
|
switch (depositType) {
|
|
71
67
|
case 'direct':
|
|
72
|
-
return await this.perqSdk.tokenUtils.approveAllowance(sourceTokenAddress,
|
|
68
|
+
return await this.perqSdk.tokenUtils.approveAllowance(sourceTokenAddress, parsedAmount.toString(), vaultAddress);
|
|
73
69
|
case 'wrap':
|
|
74
70
|
throw new Error("Can't approve a native token");
|
|
75
71
|
case 'swap':
|
|
76
|
-
return await this.perqSdk.lite.approveSwapAndDeposit(sourceTokenAddress,
|
|
72
|
+
return await this.perqSdk.lite.approveSwapAndDeposit(sourceTokenAddress, parsedAmount.toString());
|
|
77
73
|
default:
|
|
78
74
|
throw new Error(`Unsupported deposit type: ${depositType}`);
|
|
79
75
|
}
|
|
@@ -90,6 +86,8 @@ class YelayVaultOperations {
|
|
|
90
86
|
if (!onChainProjectId || onChainProjectId === -1) {
|
|
91
87
|
throw new Error('OnChainProjectId is required for Yelay vaults');
|
|
92
88
|
}
|
|
89
|
+
const parsedAmount = await this.perqSdk.tokenUtils.parseAmountWithDecimals(amount, sourceTokenAddress);
|
|
90
|
+
console.log('🚀 ~ parsedAmount:', parsedAmount.toString());
|
|
93
91
|
const depositType = this.determineDepositType(params);
|
|
94
92
|
switch (depositType) {
|
|
95
93
|
case 'direct':
|
|
@@ -97,35 +95,36 @@ class YelayVaultOperations {
|
|
|
97
95
|
sourceTokenAddress,
|
|
98
96
|
vaultAddress,
|
|
99
97
|
onChainProjectId,
|
|
100
|
-
|
|
98
|
+
parsedAmount: parsedAmount.toString(),
|
|
101
99
|
});
|
|
102
|
-
return await this.perqSdk.lite.deposit(sourceTokenAddress, vaultAddress, onChainProjectId,
|
|
100
|
+
return await this.perqSdk.lite.deposit(sourceTokenAddress, vaultAddress, onChainProjectId, parsedAmount.toString());
|
|
103
101
|
case 'wrap':
|
|
104
102
|
console.log('🚀 ~ this.perqSdk.lite.wrapAndDepositEth called', {
|
|
105
103
|
vaultAddress,
|
|
106
104
|
onChainProjectId,
|
|
107
|
-
|
|
105
|
+
parsedAmount: parsedAmount.toString(),
|
|
108
106
|
});
|
|
109
107
|
return await this.perqSdk.lite.wrapAndDepositEth(vaultAddress, onChainProjectId, amount);
|
|
110
108
|
case 'swap':
|
|
111
109
|
console.log('🚀 ~ this.perqSdk.lite.swapAndDeposit called', {
|
|
112
110
|
sourceTokenAddress,
|
|
113
111
|
vaultTokenAddress,
|
|
114
|
-
|
|
112
|
+
parsedAmount: parsedAmount.toString(),
|
|
115
113
|
vaultAddress,
|
|
116
114
|
onChainProjectId,
|
|
117
115
|
});
|
|
118
|
-
return await this.perqSdk.lite.swapAndDeposit(sourceTokenAddress, vaultTokenAddress,
|
|
116
|
+
return await this.perqSdk.lite.swapAndDeposit(sourceTokenAddress, vaultTokenAddress, parsedAmount.toString(), vaultAddress, onChainProjectId);
|
|
119
117
|
default:
|
|
120
118
|
throw new Error(`Unsupported deposit type: ${depositType}`);
|
|
121
119
|
}
|
|
122
120
|
}
|
|
123
121
|
async withdraw(params) {
|
|
124
122
|
const { sourceTokenAddress, vaultAddress, amount, onChainProjectId } = params;
|
|
123
|
+
const parsedAmount = await this.perqSdk.tokenUtils.parseAmountWithDecimals(amount, sourceTokenAddress);
|
|
125
124
|
console.log(`🚀 ~ yelay withdraw called with these params: `, {
|
|
126
125
|
sourceTokenAddress,
|
|
127
126
|
vaultAddress,
|
|
128
|
-
|
|
127
|
+
parsedAmount: parsedAmount.toString(),
|
|
129
128
|
onChainProjectId,
|
|
130
129
|
});
|
|
131
130
|
if (!onChainProjectId || onChainProjectId === -1) {
|
|
@@ -135,9 +134,9 @@ class YelayVaultOperations {
|
|
|
135
134
|
sourceTokenAddress,
|
|
136
135
|
vaultAddress,
|
|
137
136
|
onChainProjectId,
|
|
138
|
-
|
|
137
|
+
parsedAmount: parsedAmount.toString(),
|
|
139
138
|
});
|
|
140
|
-
return await this.perqSdk.lite.withdraw(sourceTokenAddress, vaultAddress, onChainProjectId,
|
|
139
|
+
return await this.perqSdk.lite.withdraw(sourceTokenAddress, vaultAddress, onChainProjectId, parsedAmount.toString());
|
|
141
140
|
}
|
|
142
141
|
async getBalance(vaultAddress, tokenAddress, onChainProjectId) {
|
|
143
142
|
return await this.perqSdk.pools.getUserPoolBalance(vaultAddress, onChainProjectId);
|