@affluent-org/sdk 0.0.2 → 0.0.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.
- package/dist/build/Account.compiled.json +1 -0
- package/dist/build/Receipt.compiled.json +1 -0
- package/dist/build/WTONWallet.compiled.json +1 -0
- package/dist/common/cache.d.ts +16 -0
- package/dist/common/cache.js +96 -0
- package/dist/common/service.d.ts +20 -0
- package/dist/common/service.js +69 -0
- package/dist/common/type.d.ts +14 -0
- package/dist/common/type.js +2 -0
- package/dist/common/unknown-contract.d.ts +14 -0
- package/dist/common/unknown-contract.js +18 -0
- package/dist/common/versions.d.ts +14 -0
- package/dist/common/versions.js +22 -0
- package/dist/constants/pool.d.ts +1 -0
- package/dist/constants/pool.js +2 -0
- package/dist/contracts/_mock/simple-oracle.d.ts +34 -0
- package/dist/contracts/_mock/simple-oracle.js +73 -0
- package/dist/contracts/vault/share-vault/type.d.ts +30 -0
- package/dist/contracts/vault/share-vault/type.js +2 -0
- package/dist/contracts/vault/strategy-vault/utils.d.ts +6 -0
- package/dist/contracts/vault/strategy-vault/utils.js +32 -0
- package/dist/factorial.d.ts +14 -0
- package/dist/factorial.js +20 -0
- package/dist/farm.d.ts +92 -0
- package/dist/farm.js +209 -0
- package/dist/monitor.d.ts +57 -0
- package/dist/monitor.js +527 -0
- package/dist/monitorCacheV1.d.ts +52 -0
- package/dist/monitorCacheV1.js +504 -0
- package/dist/oracle/oracle-v2.d.ts +39 -0
- package/dist/oracle/oracle-v2.js +151 -0
- package/dist/oracle/oracle.d.ts +107 -0
- package/dist/oracle/oracle.js +392 -0
- package/dist/periphery.d.ts +259 -0
- package/dist/periphery.js +1087 -0
- package/dist/pool.d.ts +216 -0
- package/dist/pool.js +2298 -0
- package/dist/poolCacheV1.d.ts +139 -0
- package/dist/poolCacheV1.js +1841 -0
- package/dist/rfq-auction.d.ts +75 -0
- package/dist/rfq-auction.js +220 -0
- package/dist/rfq-batch.d.ts +112 -0
- package/dist/rfq-batch.js +284 -0
- package/dist/services/share-vault/computation.d.ts +14 -17
- package/dist/services/share-vault/computation.js +39 -0
- package/dist/services/share-vault/index.js +6 -3
- package/dist/services/share-vault/query.d.ts +38 -8
- package/dist/services/share-vault/query.js +20 -27
- package/dist/services/share-vault/type.d.ts +19 -0
- package/dist/services/share-vault/type.js +2 -0
- package/dist/services/share-vault/user/index.js +3 -1
- package/dist/services/strategy-vault/computation.d.ts +1 -0
- package/dist/services/strategy-vault/computation.js +15 -0
- package/dist/services/strategy-vault/query.d.ts +147 -0
- package/dist/services/strategy-vault/query.js +67 -0
- package/dist/services/strategy-vault/type.d.ts +2 -0
- package/dist/services/strategy-vault/type.js +2 -0
- package/dist/share-vault.d.ts +91 -0
- package/dist/share-vault.js +747 -0
- package/dist/stonfi.d.ts +18 -0
- package/dist/stonfi.js +76 -0
- package/dist/strategy_vault/base.d.ts +399 -0
- package/dist/strategy_vault/base.js +1199 -0
- package/dist/strategy_vault/index.d.ts +3 -0
- package/dist/strategy_vault/index.js +7 -0
- package/dist/strategy_vault/steps.d.ts +49 -0
- package/dist/strategy_vault/steps.js +170 -0
- package/dist/types/action.d.ts +55 -0
- package/dist/types/action.js +2 -0
- package/dist/types/messages.d.ts +7 -0
- package/dist/types/messages.js +2 -0
- package/dist/types/params.d.ts +19 -0
- package/dist/types/params.js +2 -0
- package/dist/types/pool.d.ts +83 -0
- package/dist/types/pool.js +2 -0
- package/dist/types/transaction.d.ts +40 -0
- package/dist/types/transaction.js +2 -0
- package/dist/utils/_parse_temp/JumpIRM.d.ts +37 -0
- package/dist/utils/_parse_temp/JumpIRM.js +71 -0
- package/dist/utils/_parse_temp/Pool.d.ts +559 -0
- package/dist/utils/_parse_temp/Pool.js +1023 -0
- package/dist/utils/_parse_temp/ShareVault.d.ts +264 -0
- package/dist/utils/_parse_temp/ShareVault.js +479 -0
- package/dist/utils/_parse_temp/StrategyVault.d.ts +729 -0
- package/dist/utils/_parse_temp/StrategyVault.js +1865 -0
- package/dist/utils/_parse_temp/parseMsgBody.d.ts +13 -0
- package/dist/utils/_parse_temp/parseMsgBody.js +313 -0
- package/dist/utils/assert.d.ts +1 -0
- package/dist/utils/assert.js +9 -0
- package/dist/utils/client-for-parameter.d.ts +12 -0
- package/dist/utils/client-for-parameter.js +97 -0
- package/dist/utils/oracle/index.d.ts +4 -0
- package/dist/utils/oracle/index.js +19 -0
- package/dist/utils/oracle/redstone/helper.d.ts +22 -0
- package/dist/utils/oracle/redstone/helper.js +186 -0
- package/dist/utils/tracer.d.ts +13 -0
- package/dist/utils/tracer.js +137 -0
- package/dist/utils/tracker/index.d.ts +5 -0
- package/dist/utils/tracker/index.js +118 -0
- package/dist/utils/tracker/query-id-generactor.d.ts +2 -0
- package/dist/utils/tracker/query-id-generactor.js +12 -0
- package/dist/utils/tracker/type.d.ts +34 -0
- package/dist/utils/tracker/type.js +2 -0
- package/package.json +1 -1
|
@@ -0,0 +1,747 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ShareVaultV1 = void 0;
|
|
4
|
+
const ton_1 = require("@ton/ton");
|
|
5
|
+
const pool_1 = require("./contracts/core/pool");
|
|
6
|
+
const jetton_minter_1 = require("./contracts/jetton/jetton-minter");
|
|
7
|
+
const jetton_wallet_1 = require("./contracts/jetton/jetton-wallet");
|
|
8
|
+
const tracker_1 = require("./utils/tracker");
|
|
9
|
+
const share_vault_1 = require("./contracts/vault/share-vault");
|
|
10
|
+
const query_id_generactor_1 = require("./utils/tracker/query-id-generactor");
|
|
11
|
+
const parser_1 = require("./utils/parser");
|
|
12
|
+
const jetton_wallet_2 = require("./contracts/wton/jetton-wallet");
|
|
13
|
+
const jetton_minter_2 = require("./contracts/wton/jetton-minter");
|
|
14
|
+
const service_1 = require("./common/service");
|
|
15
|
+
const TIMEOUT_SEC = 2 * 60 * 1000;
|
|
16
|
+
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
17
|
+
const toAddress = (value) => {
|
|
18
|
+
if (value instanceof ton_1.Address) {
|
|
19
|
+
return value;
|
|
20
|
+
}
|
|
21
|
+
return ton_1.Address.parse(value);
|
|
22
|
+
};
|
|
23
|
+
const toString = (value) => {
|
|
24
|
+
if (value instanceof ton_1.Address) {
|
|
25
|
+
return value.toString();
|
|
26
|
+
}
|
|
27
|
+
return value;
|
|
28
|
+
};
|
|
29
|
+
class ShareVaultV1 extends service_1.ServiceBaseV1 {
|
|
30
|
+
// user -> asset -> wallet
|
|
31
|
+
walletAddresses = {};
|
|
32
|
+
async getJettonWalletAddress(owner, jetton) {
|
|
33
|
+
if (!this.walletAddresses[owner]) {
|
|
34
|
+
this.walletAddresses[owner] = {};
|
|
35
|
+
}
|
|
36
|
+
if (!this.walletAddresses[owner][jetton]) {
|
|
37
|
+
const jettonMinter = this.client.open(jetton_minter_1.JettonMinter.createFromAddress(toAddress(jetton)));
|
|
38
|
+
this.walletAddresses[owner][jetton] = await jettonMinter.getWalletAddress(toAddress(owner));
|
|
39
|
+
}
|
|
40
|
+
return this.walletAddresses[owner][jetton];
|
|
41
|
+
}
|
|
42
|
+
async findTx(action, callbackFn) {
|
|
43
|
+
if (action.type == "vault-supply") {
|
|
44
|
+
return this.findSupplyTx(BigInt(action.queryId), action.params, callbackFn);
|
|
45
|
+
}
|
|
46
|
+
else if (action.type == "vault-withdraw") {
|
|
47
|
+
return this.findWithdrawTx(BigInt(action.queryId), action.params, callbackFn);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
async supply(sender, params) {
|
|
51
|
+
const shareVault = this.client.open(share_vault_1.ShareVault.createFromAddress(toAddress(params.vault)));
|
|
52
|
+
const vaultData = await shareVault.getVaultData();
|
|
53
|
+
const gas = share_vault_1.ShareVault.calculateGasFee(share_vault_1.ShareVault.Op.Supply, share_vault_1.ShareVault.Gas.baseFee, Object.keys(vaultData.whitelistedPools).length, vaultData.minimumGasFee);
|
|
54
|
+
if (toString(vaultData.asset) == this.contracts.WTON) {
|
|
55
|
+
const vaultWTONWalletAddress = await this.getJettonWalletAddress(toString(params.vault), toString(vaultData.asset));
|
|
56
|
+
const vaultWTONWallet = this.client.open(jetton_wallet_2.WTONWallet.createFromAddress(vaultWTONWalletAddress));
|
|
57
|
+
await vaultWTONWallet.sendExternalTransfer(sender, jetton_wallet_2.WTONWallet.Gas.ExternalTransfer, {
|
|
58
|
+
recipient: toAddress(params.vault),
|
|
59
|
+
response: toAddress(params.response),
|
|
60
|
+
amount: params.amount,
|
|
61
|
+
forwardTonAmount: gas,
|
|
62
|
+
forwardPayload: (0, ton_1.beginCell)().storeUint(0, 32).endCell(),
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
const senderJettonWalletAddress = await this.getJettonWalletAddress(toString(params.sender), toString(vaultData.asset));
|
|
67
|
+
const senderJettonWallet = this.client.open(jetton_wallet_1.JettonWallet.createFromAddress(senderJettonWalletAddress));
|
|
68
|
+
await senderJettonWallet.sendTransfer(sender, gas + jetton_wallet_1.JettonWallet.Gas.Transfer, {
|
|
69
|
+
recipient: toAddress(params.vault),
|
|
70
|
+
response: toAddress(params.response),
|
|
71
|
+
amount: params.amount,
|
|
72
|
+
forwardTonAmount: gas,
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
async supplyAndWaitTx(sender, params, callbackFn) {
|
|
77
|
+
const shareVault = this.client.open(share_vault_1.ShareVault.createFromAddress(toAddress(params.vault)));
|
|
78
|
+
const vaultData = await shareVault.getVaultData();
|
|
79
|
+
const gas = share_vault_1.ShareVault.calculateGasFee(share_vault_1.ShareVault.Op.Supply, share_vault_1.ShareVault.Gas.baseFee, Object.keys(vaultData.whitelistedPools).length, vaultData.minimumGasFee);
|
|
80
|
+
const senderJettonWalletAddress = await this.getJettonWalletAddress(toString(params.sender), toString(vaultData.asset));
|
|
81
|
+
const senderJettonWallet = this.client.open(jetton_wallet_1.JettonWallet.createFromAddress(senderJettonWalletAddress));
|
|
82
|
+
const senderVaultWalletAddress = await this.getJettonWalletAddress(toString(params.sender), toString(params.vault));
|
|
83
|
+
const opt = {
|
|
84
|
+
recipient: toAddress(params.vault),
|
|
85
|
+
response: toAddress(params.response),
|
|
86
|
+
amount: params.amount,
|
|
87
|
+
forwardTonAmount: gas,
|
|
88
|
+
};
|
|
89
|
+
const body = senderJettonWallet.createTransferBody(opt);
|
|
90
|
+
const queryId = (0, query_id_generactor_1.generateQueryId)(toAddress(params.sender), body);
|
|
91
|
+
const steps = [];
|
|
92
|
+
if (toString(vaultData.asset) == this.contracts.WTON) {
|
|
93
|
+
const vaultWTONWalletAddress = await this.getJettonWalletAddress(toString(params.vault), toString(vaultData.asset));
|
|
94
|
+
const vaultWTONWallet = this.client.open(jetton_wallet_2.WTONWallet.createFromAddress(vaultWTONWalletAddress));
|
|
95
|
+
await vaultWTONWallet.sendExternalTransfer(sender, jetton_wallet_2.WTONWallet.Gas.ExternalTransfer, {
|
|
96
|
+
...opt,
|
|
97
|
+
queryId: queryId,
|
|
98
|
+
});
|
|
99
|
+
steps.push(...[
|
|
100
|
+
{
|
|
101
|
+
queryId,
|
|
102
|
+
description: "Starting the tx",
|
|
103
|
+
from: toAddress(params.sender),
|
|
104
|
+
to: vaultWTONWalletAddress,
|
|
105
|
+
opcode: jetton_wallet_2.WTONWallet.Op.ExternalTransfer,
|
|
106
|
+
},
|
|
107
|
+
]);
|
|
108
|
+
}
|
|
109
|
+
else {
|
|
110
|
+
await senderJettonWallet.sendTransfer(sender, gas + jetton_wallet_1.JettonWallet.Gas.Transfer, {
|
|
111
|
+
...opt,
|
|
112
|
+
queryId: queryId,
|
|
113
|
+
});
|
|
114
|
+
steps.push(...[
|
|
115
|
+
{
|
|
116
|
+
description: "Starting the tx",
|
|
117
|
+
from: toAddress(params.sender),
|
|
118
|
+
to: senderJettonWalletAddress,
|
|
119
|
+
opcode: jetton_wallet_1.JettonWallet.Op.Transfer,
|
|
120
|
+
queryId: queryId,
|
|
121
|
+
},
|
|
122
|
+
]);
|
|
123
|
+
}
|
|
124
|
+
steps.push(...[
|
|
125
|
+
{
|
|
126
|
+
description: "Sending the token to the vault",
|
|
127
|
+
to: toAddress(params.vault),
|
|
128
|
+
queryId: queryId,
|
|
129
|
+
opcode: jetton_wallet_1.JettonWallet.Op.TransferNotification,
|
|
130
|
+
},
|
|
131
|
+
{
|
|
132
|
+
description: "Aggregating pools",
|
|
133
|
+
to: toAddress(params.vault),
|
|
134
|
+
queryId: queryId,
|
|
135
|
+
opcode: share_vault_1.ShareVault.Op.TakeAggregatedPool,
|
|
136
|
+
},
|
|
137
|
+
{
|
|
138
|
+
description: "Sending asset to the pool",
|
|
139
|
+
queryId: queryId,
|
|
140
|
+
opcode: jetton_wallet_1.JettonWallet.Op.TransferNotification,
|
|
141
|
+
},
|
|
142
|
+
{
|
|
143
|
+
description: "Action notification",
|
|
144
|
+
to: toAddress(params.vault),
|
|
145
|
+
queryId: queryId,
|
|
146
|
+
opcode: pool_1.Pool.Op.ActionNotification,
|
|
147
|
+
},
|
|
148
|
+
{
|
|
149
|
+
description: "Minting the vault share token",
|
|
150
|
+
from: toAddress(params.vault),
|
|
151
|
+
to: toAddress(senderVaultWalletAddress),
|
|
152
|
+
queryId: queryId,
|
|
153
|
+
opcode: jetton_wallet_1.JettonWallet.Op.InternalTransfer,
|
|
154
|
+
},
|
|
155
|
+
]);
|
|
156
|
+
(async () => {
|
|
157
|
+
const matchedTx1 = await (0, tracker_1.findTx)(this.client, toAddress(params.sender), "0", steps[0]);
|
|
158
|
+
steps[0].matchedTx = matchedTx1;
|
|
159
|
+
if (callbackFn)
|
|
160
|
+
callbackFn(steps[0]);
|
|
161
|
+
const matchedTx2 = await (0, tracker_1.findTx)(this.client, toAddress(params.vault), matchedTx1.lt.toString(), steps[1]);
|
|
162
|
+
steps[1].matchedTx = matchedTx2;
|
|
163
|
+
if (callbackFn)
|
|
164
|
+
callbackFn(steps[1]);
|
|
165
|
+
let poolAddress;
|
|
166
|
+
if (matchedTx2.outMessages[0]?.parsedBody?.type ===
|
|
167
|
+
"ShareVault.Op.ProvideAggregatedPool") {
|
|
168
|
+
const matchedTx3 = await (0, tracker_1.findTx)(this.client, toAddress(params.vault), matchedTx2.lt.toString(), steps[2]);
|
|
169
|
+
steps[2].matchedTx = matchedTx3;
|
|
170
|
+
if (callbackFn)
|
|
171
|
+
callbackFn(steps[2]);
|
|
172
|
+
if (matchedTx3.outMessages[0]?.parsedBody?.type ===
|
|
173
|
+
"JettonWallet.Op.Transfer") {
|
|
174
|
+
poolAddress = matchedTx3.outMessages[0]?.parsedBody.recipient;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
else if (matchedTx2.outMessages[0]?.parsedBody?.type ===
|
|
178
|
+
"JettonWallet.Op.Transfer") {
|
|
179
|
+
poolAddress = matchedTx2.outMessages[0]?.parsedBody.recipient;
|
|
180
|
+
}
|
|
181
|
+
if (poolAddress) {
|
|
182
|
+
const matchedTx4 = await (0, tracker_1.findTx)(this.client, toAddress(poolAddress), matchedTx2.lt.toString(), steps[3]);
|
|
183
|
+
steps[3].matchedTx = matchedTx4;
|
|
184
|
+
if (callbackFn)
|
|
185
|
+
callbackFn(steps[3]);
|
|
186
|
+
const matchedTx5 = await (0, tracker_1.findTx)(this.client, toAddress(params.vault), matchedTx4.lt.toString(), steps[4]);
|
|
187
|
+
steps[4].matchedTx = matchedTx5;
|
|
188
|
+
if (matchedTx5.inMessage?.body) {
|
|
189
|
+
steps[4].actionNotificationResult = (0, parser_1.parseActionNotification)(matchedTx5.inMessage?.body);
|
|
190
|
+
}
|
|
191
|
+
if (callbackFn)
|
|
192
|
+
callbackFn(steps[4]);
|
|
193
|
+
}
|
|
194
|
+
const matchedTx6 = await (0, tracker_1.findTx)(this.client, toAddress(senderVaultWalletAddress), matchedTx2.lt.toString(), steps[5]);
|
|
195
|
+
steps[5].matchedTx = matchedTx6;
|
|
196
|
+
if (callbackFn)
|
|
197
|
+
callbackFn(steps[5]);
|
|
198
|
+
})();
|
|
199
|
+
return {
|
|
200
|
+
action: {
|
|
201
|
+
type: "vault-supply",
|
|
202
|
+
params,
|
|
203
|
+
queryId,
|
|
204
|
+
},
|
|
205
|
+
steps,
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
async findSupplyTx(queryId, params, callbackFn) {
|
|
209
|
+
const shareVault = this.client.open(share_vault_1.ShareVault.createFromAddress(toAddress(params.vault)));
|
|
210
|
+
const vaultData = await shareVault.getVaultData();
|
|
211
|
+
const senderJettonWalletAddress = await this.getJettonWalletAddress(toString(params.sender), toString(vaultData.asset));
|
|
212
|
+
const senderVaultWalletAddress = await this.getJettonWalletAddress(toString(params.sender), toString(params.vault));
|
|
213
|
+
const steps = [];
|
|
214
|
+
if (toString(vaultData.asset) == this.contracts.WTON) {
|
|
215
|
+
const vaultWTONWalletAddress = await this.getJettonWalletAddress(toString(params.vault), toString(vaultData.asset));
|
|
216
|
+
const vaultWTONWallet = this.client.open(jetton_wallet_2.WTONWallet.createFromAddress(vaultWTONWalletAddress));
|
|
217
|
+
steps.push(...[
|
|
218
|
+
{
|
|
219
|
+
queryId,
|
|
220
|
+
description: "Starting the tx",
|
|
221
|
+
from: toAddress(params.sender),
|
|
222
|
+
to: vaultWTONWalletAddress,
|
|
223
|
+
opcode: jetton_wallet_2.WTONWallet.Op.ExternalTransfer,
|
|
224
|
+
},
|
|
225
|
+
]);
|
|
226
|
+
}
|
|
227
|
+
else {
|
|
228
|
+
steps.push(...[
|
|
229
|
+
{
|
|
230
|
+
description: "Starting the tx",
|
|
231
|
+
from: toAddress(params.sender),
|
|
232
|
+
to: senderJettonWalletAddress,
|
|
233
|
+
opcode: jetton_wallet_1.JettonWallet.Op.Transfer,
|
|
234
|
+
queryId: queryId,
|
|
235
|
+
},
|
|
236
|
+
]);
|
|
237
|
+
}
|
|
238
|
+
steps.push(...[
|
|
239
|
+
{
|
|
240
|
+
description: "Sending the token to the vault",
|
|
241
|
+
to: toAddress(params.vault),
|
|
242
|
+
queryId: queryId,
|
|
243
|
+
opcode: jetton_wallet_1.JettonWallet.Op.TransferNotification,
|
|
244
|
+
},
|
|
245
|
+
{
|
|
246
|
+
description: "Aggregating pools",
|
|
247
|
+
to: toAddress(params.vault),
|
|
248
|
+
queryId: queryId,
|
|
249
|
+
opcode: share_vault_1.ShareVault.Op.TakeAggregatedPool,
|
|
250
|
+
},
|
|
251
|
+
{
|
|
252
|
+
description: "Sending asset to the pool",
|
|
253
|
+
queryId: queryId,
|
|
254
|
+
opcode: jetton_wallet_1.JettonWallet.Op.TransferNotification,
|
|
255
|
+
},
|
|
256
|
+
{
|
|
257
|
+
description: "Action notification",
|
|
258
|
+
to: toAddress(params.vault),
|
|
259
|
+
queryId: queryId,
|
|
260
|
+
opcode: pool_1.Pool.Op.ActionNotification,
|
|
261
|
+
},
|
|
262
|
+
{
|
|
263
|
+
description: "Minting the vault share token",
|
|
264
|
+
from: toAddress(params.vault),
|
|
265
|
+
to: toAddress(senderVaultWalletAddress),
|
|
266
|
+
queryId: queryId,
|
|
267
|
+
opcode: jetton_wallet_1.JettonWallet.Op.InternalTransfer,
|
|
268
|
+
},
|
|
269
|
+
]);
|
|
270
|
+
(async () => {
|
|
271
|
+
const matchedTx1 = await (0, tracker_1.findTx)(this.client, toAddress(params.sender), "0", steps[0]);
|
|
272
|
+
steps[0].matchedTx = matchedTx1;
|
|
273
|
+
if (callbackFn)
|
|
274
|
+
callbackFn(steps[0]);
|
|
275
|
+
const matchedTx2 = await (0, tracker_1.findTx)(this.client, toAddress(params.vault), matchedTx1.lt.toString(), steps[1]);
|
|
276
|
+
steps[1].matchedTx = matchedTx2;
|
|
277
|
+
if (callbackFn)
|
|
278
|
+
callbackFn(steps[1]);
|
|
279
|
+
let poolAddress;
|
|
280
|
+
if (matchedTx2.outMessages[0]?.parsedBody?.type ===
|
|
281
|
+
"ShareVault.Op.ProvideAggregatedPool") {
|
|
282
|
+
const matchedTx3 = await (0, tracker_1.findTx)(this.client, toAddress(params.vault), matchedTx2.lt.toString(), steps[2]);
|
|
283
|
+
steps[2].matchedTx = matchedTx3;
|
|
284
|
+
if (callbackFn)
|
|
285
|
+
callbackFn(steps[2]);
|
|
286
|
+
if (matchedTx3.outMessages[0]?.parsedBody?.type ===
|
|
287
|
+
"JettonWallet.Op.Transfer") {
|
|
288
|
+
poolAddress = matchedTx3.outMessages[0]?.parsedBody.recipient;
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
else if (matchedTx2.outMessages[0]?.parsedBody?.type ===
|
|
292
|
+
"JettonWallet.Op.Transfer") {
|
|
293
|
+
poolAddress = matchedTx2.outMessages[0]?.parsedBody.recipient;
|
|
294
|
+
}
|
|
295
|
+
if (poolAddress) {
|
|
296
|
+
const matchedTx4 = await (0, tracker_1.findTx)(this.client, toAddress(poolAddress), matchedTx2.lt.toString(), steps[3]);
|
|
297
|
+
steps[3].matchedTx = matchedTx4;
|
|
298
|
+
if (callbackFn)
|
|
299
|
+
callbackFn(steps[3]);
|
|
300
|
+
const matchedTx5 = await (0, tracker_1.findTx)(this.client, toAddress(params.vault), matchedTx4.lt.toString(), steps[4]);
|
|
301
|
+
steps[4].matchedTx = matchedTx5;
|
|
302
|
+
if (matchedTx5.inMessage?.body) {
|
|
303
|
+
steps[4].actionNotificationResult = (0, parser_1.parseActionNotification)(matchedTx5.inMessage?.body);
|
|
304
|
+
}
|
|
305
|
+
if (callbackFn)
|
|
306
|
+
callbackFn(steps[4]);
|
|
307
|
+
}
|
|
308
|
+
const matchedTx6 = await (0, tracker_1.findTx)(this.client, toAddress(senderVaultWalletAddress), matchedTx2.lt.toString(), steps[5]);
|
|
309
|
+
steps[5].matchedTx = matchedTx6;
|
|
310
|
+
if (callbackFn)
|
|
311
|
+
callbackFn(steps[5]);
|
|
312
|
+
})();
|
|
313
|
+
return {
|
|
314
|
+
action: {
|
|
315
|
+
type: "vault-supply",
|
|
316
|
+
params,
|
|
317
|
+
queryId,
|
|
318
|
+
},
|
|
319
|
+
steps,
|
|
320
|
+
};
|
|
321
|
+
}
|
|
322
|
+
async withdraw(sender, params) {
|
|
323
|
+
const senderVaultWalletAddress = await this.getJettonWalletAddress(toString(params.sender), toString(params.vault));
|
|
324
|
+
const senderVaultWallet = this.client.open(jetton_wallet_1.JettonWallet.createFromAddress(senderVaultWalletAddress));
|
|
325
|
+
const shareVault = this.client.open(share_vault_1.ShareVault.createFromAddress(toAddress(params.vault)));
|
|
326
|
+
const vaultData = await shareVault.getVaultData();
|
|
327
|
+
const { count } = await this.estimateWithdrawLiquidityFulfillmentPoolCount(vaultData, params.amount);
|
|
328
|
+
const gas = share_vault_1.ShareVault.calculateGasFee(share_vault_1.ShareVault.Op.Withdraw, share_vault_1.ShareVault.Gas.baseFee, count, vaultData.minimumGasFee) + (0, ton_1.toNano)(0.1); // TODO: re-mapping withdraw gas fee
|
|
329
|
+
await senderVaultWallet.sendBurn(sender, gas, {
|
|
330
|
+
response: toAddress(params.response),
|
|
331
|
+
amount: params.amount,
|
|
332
|
+
});
|
|
333
|
+
}
|
|
334
|
+
async withdrawAndWaitTx(sender, params, callbackFn) {
|
|
335
|
+
const shareVault = this.client.open(share_vault_1.ShareVault.createFromAddress(toAddress(params.vault)));
|
|
336
|
+
const vaultData = await shareVault.getVaultData();
|
|
337
|
+
const asset = vaultData.asset;
|
|
338
|
+
const vaultJettonWalletAddress = await this.getJettonWalletAddress(toString(params.vault), toString(vaultData.asset));
|
|
339
|
+
const senderJettonWalletAddress = await this.getJettonWalletAddress(toString(params.sender), toString(vaultData.asset));
|
|
340
|
+
const senderJettonWallet = this.client.open(jetton_wallet_1.JettonWallet.createFromAddress(senderJettonWalletAddress));
|
|
341
|
+
const senderVaultWalletAddress = await this.getJettonWalletAddress(toString(params.sender), toString(params.vault));
|
|
342
|
+
const senderVaultWallet = this.client.open(jetton_wallet_1.JettonWallet.createFromAddress(senderVaultWalletAddress));
|
|
343
|
+
const { count } = await this.estimateWithdrawLiquidityFulfillmentPoolCount(vaultData, params.amount);
|
|
344
|
+
const gas = share_vault_1.ShareVault.calculateGasFee(share_vault_1.ShareVault.Op.Withdraw, share_vault_1.ShareVault.Gas.baseFee, count, vaultData.minimumGasFee) + (0, ton_1.toNano)(0.1); // TODO: re-mapping withdraw gas fee
|
|
345
|
+
const opt = { response: toAddress(params.response), amount: params.amount };
|
|
346
|
+
// const queryId = 16131232817234889649n;
|
|
347
|
+
const queryId = (0, query_id_generactor_1.generateQueryId)(toAddress(params.sender), jetton_wallet_1.JettonWallet.getBurnMsgBody(opt));
|
|
348
|
+
await senderVaultWallet.sendBurn(sender, gas, {
|
|
349
|
+
...opt,
|
|
350
|
+
queryId: queryId,
|
|
351
|
+
});
|
|
352
|
+
const steps = [
|
|
353
|
+
{
|
|
354
|
+
description: "Staring the tx",
|
|
355
|
+
from: toAddress(params.sender),
|
|
356
|
+
to: senderVaultWalletAddress,
|
|
357
|
+
opcode: jetton_wallet_1.JettonWallet.Op.Burn,
|
|
358
|
+
queryId: queryId,
|
|
359
|
+
},
|
|
360
|
+
{
|
|
361
|
+
description: "Burning the vault share token",
|
|
362
|
+
to: toAddress(params.vault),
|
|
363
|
+
queryId: queryId,
|
|
364
|
+
opcode: jetton_wallet_1.JettonWallet.Op.BurnNotification,
|
|
365
|
+
},
|
|
366
|
+
{
|
|
367
|
+
description: "Aggregating pools",
|
|
368
|
+
to: toAddress(params.vault),
|
|
369
|
+
queryId: queryId,
|
|
370
|
+
opcode: share_vault_1.ShareVault.Op.TakeAggregatedPool,
|
|
371
|
+
},
|
|
372
|
+
{
|
|
373
|
+
description: "Withdrawing from the pool",
|
|
374
|
+
from: toAddress(params.vault),
|
|
375
|
+
queryId: queryId,
|
|
376
|
+
opcode: pool_1.Pool.Op.Withdraw,
|
|
377
|
+
},
|
|
378
|
+
{
|
|
379
|
+
description: "Action notification",
|
|
380
|
+
to: toAddress(params.vault),
|
|
381
|
+
queryId: queryId,
|
|
382
|
+
opcode: pool_1.Pool.Op.ActionNotification,
|
|
383
|
+
},
|
|
384
|
+
{
|
|
385
|
+
description: "Receiving the token from the vault",
|
|
386
|
+
from: toAddress(vaultJettonWalletAddress),
|
|
387
|
+
to: toAddress(senderJettonWalletAddress),
|
|
388
|
+
queryId: queryId,
|
|
389
|
+
opcode: jetton_wallet_1.JettonWallet.Op.InternalTransfer,
|
|
390
|
+
},
|
|
391
|
+
];
|
|
392
|
+
(async () => {
|
|
393
|
+
const matchedTx0 = await (0, tracker_1.findTx)(this.client, senderVaultWalletAddress, "0", steps[0]);
|
|
394
|
+
steps[0].matchedTx = matchedTx0;
|
|
395
|
+
if (callbackFn)
|
|
396
|
+
callbackFn(steps[0]);
|
|
397
|
+
const matchedTx1 = await (0, tracker_1.findTx)(this.client, toAddress(params.vault), matchedTx0.lt.toString(), steps[1]);
|
|
398
|
+
steps[1].matchedTx = matchedTx1;
|
|
399
|
+
if (callbackFn)
|
|
400
|
+
callbackFn(steps[1]);
|
|
401
|
+
const transferMessage = matchedTx1.outMessages.find((item) => item?.parsedBody?.type === "JettonWallet.Op.Transfer");
|
|
402
|
+
const poolAggregateMessage = matchedTx1.outMessages.find((item) => item?.parsedBody?.type === "ShareVault.Op.ProvideAggregatedPool");
|
|
403
|
+
if (transferMessage && !poolAggregateMessage) {
|
|
404
|
+
const matchedTx5 = await (0, tracker_1.findTx)(this.client, toAddress(senderJettonWalletAddress), matchedTx1.lt.toString(), steps[5]);
|
|
405
|
+
steps[5].matchedTx = matchedTx5;
|
|
406
|
+
if (callbackFn)
|
|
407
|
+
callbackFn(steps[5]);
|
|
408
|
+
return;
|
|
409
|
+
}
|
|
410
|
+
if (poolAggregateMessage) {
|
|
411
|
+
const matchedTx2 = await (0, tracker_1.findTx)(this.client, toAddress(params.vault), matchedTx1.lt.toString(), steps[2]);
|
|
412
|
+
steps[2].matchedTx = matchedTx2;
|
|
413
|
+
if (callbackFn)
|
|
414
|
+
callbackFn(steps[2]);
|
|
415
|
+
const transferMessage = matchedTx2.outMessages.find((item) => item?.parsedBody?.type === "JettonWallet.Op.Transfer");
|
|
416
|
+
const withdrawMessage = matchedTx2.outMessages.find((item) => item?.parsedBody?.type === "Pool.Op.TransferOut");
|
|
417
|
+
if (transferMessage && !withdrawMessage) {
|
|
418
|
+
const matchedTx5 = await (0, tracker_1.findTx)(this.client, toAddress(params.vault), matchedTx2.lt.toString(), steps[5]);
|
|
419
|
+
steps[5].matchedTx = matchedTx5;
|
|
420
|
+
if (callbackFn)
|
|
421
|
+
callbackFn(steps[5]);
|
|
422
|
+
return;
|
|
423
|
+
}
|
|
424
|
+
const poolAddresses = matchedTx2.outMessages
|
|
425
|
+
.filter((item) => item?.parsedBody?.type === "Pool.Op.TransferOut")
|
|
426
|
+
.map((item) => item?.dst)
|
|
427
|
+
.filter((item) => !!item);
|
|
428
|
+
const matchedTxs3 = [];
|
|
429
|
+
for (let poolAddress of poolAddresses) {
|
|
430
|
+
const matchedTx = await (0, tracker_1.findTx)(this.client, toAddress(params.vault), matchedTx2.lt.toString(), { ...steps[3], to: poolAddress });
|
|
431
|
+
matchedTxs3.push(matchedTx);
|
|
432
|
+
}
|
|
433
|
+
steps[3].matchedTx = matchedTxs3;
|
|
434
|
+
if (callbackFn)
|
|
435
|
+
callbackFn(steps[3]);
|
|
436
|
+
const matchedTxs4 = [];
|
|
437
|
+
for (let poolAddress of poolAddresses) {
|
|
438
|
+
const matchedTx = await (0, tracker_1.findTx)(this.client, toAddress(params.vault), matchedTx2.lt.toString(), { ...steps[4], from: poolAddress });
|
|
439
|
+
matchedTxs4.push(matchedTx);
|
|
440
|
+
}
|
|
441
|
+
steps[4].matchedTx = matchedTxs4;
|
|
442
|
+
if (callbackFn)
|
|
443
|
+
callbackFn(steps[4]);
|
|
444
|
+
const matchedTxs5 = [];
|
|
445
|
+
for (let poolAddress of poolAddresses) {
|
|
446
|
+
const poolJettonWalletAddress = await this.getJettonWalletAddress(toString(poolAddress), toString(vaultData.asset));
|
|
447
|
+
const matchedTx = await (0, tracker_1.findTx)(this.client, poolJettonWalletAddress, matchedTx2.lt.toString(), { ...steps[5], from: poolJettonWalletAddress });
|
|
448
|
+
matchedTxs5.push(matchedTx);
|
|
449
|
+
}
|
|
450
|
+
steps[5].matchedTx = matchedTxs5;
|
|
451
|
+
if (callbackFn)
|
|
452
|
+
callbackFn(steps[5]);
|
|
453
|
+
}
|
|
454
|
+
})();
|
|
455
|
+
return {
|
|
456
|
+
action: {
|
|
457
|
+
type: "vault-withdraw",
|
|
458
|
+
params,
|
|
459
|
+
queryId,
|
|
460
|
+
},
|
|
461
|
+
steps,
|
|
462
|
+
};
|
|
463
|
+
}
|
|
464
|
+
async findWithdrawTx(queryId, params, callbackFn) {
|
|
465
|
+
const shareVault = this.client.open(share_vault_1.ShareVault.createFromAddress(toAddress(params.vault)));
|
|
466
|
+
const vaultData = await shareVault.getVaultData();
|
|
467
|
+
const vaultJettonWalletAddress = await this.getJettonWalletAddress(toString(params.vault), toString(vaultData.asset));
|
|
468
|
+
const senderJettonWalletAddress = await this.getJettonWalletAddress(toString(params.sender), toString(vaultData.asset));
|
|
469
|
+
const senderVaultWalletAddress = await this.getJettonWalletAddress(toString(params.sender), toString(params.vault));
|
|
470
|
+
// const queryId = 16131232817234889649n;
|
|
471
|
+
const steps = [
|
|
472
|
+
{
|
|
473
|
+
description: "Staring the tx",
|
|
474
|
+
from: toAddress(params.sender),
|
|
475
|
+
to: senderVaultWalletAddress,
|
|
476
|
+
opcode: jetton_wallet_1.JettonWallet.Op.Burn,
|
|
477
|
+
queryId: queryId,
|
|
478
|
+
},
|
|
479
|
+
{
|
|
480
|
+
description: "Burning the vault share token",
|
|
481
|
+
to: toAddress(params.vault),
|
|
482
|
+
queryId: queryId,
|
|
483
|
+
opcode: jetton_wallet_1.JettonWallet.Op.BurnNotification,
|
|
484
|
+
},
|
|
485
|
+
{
|
|
486
|
+
description: "Aggregating pools",
|
|
487
|
+
to: toAddress(params.vault),
|
|
488
|
+
queryId: queryId,
|
|
489
|
+
opcode: share_vault_1.ShareVault.Op.TakeAggregatedPool,
|
|
490
|
+
},
|
|
491
|
+
{
|
|
492
|
+
description: "Withdrawing from the pool",
|
|
493
|
+
from: toAddress(params.vault),
|
|
494
|
+
queryId: queryId,
|
|
495
|
+
opcode: pool_1.Pool.Op.Withdraw,
|
|
496
|
+
},
|
|
497
|
+
{
|
|
498
|
+
description: "Action notification",
|
|
499
|
+
to: toAddress(params.vault),
|
|
500
|
+
queryId: queryId,
|
|
501
|
+
opcode: pool_1.Pool.Op.ActionNotification,
|
|
502
|
+
},
|
|
503
|
+
{
|
|
504
|
+
description: "Receiving the token from the vault",
|
|
505
|
+
from: toAddress(vaultJettonWalletAddress),
|
|
506
|
+
to: toAddress(senderJettonWalletAddress),
|
|
507
|
+
queryId: queryId,
|
|
508
|
+
opcode: jetton_wallet_1.JettonWallet.Op.InternalTransfer,
|
|
509
|
+
},
|
|
510
|
+
];
|
|
511
|
+
(async () => {
|
|
512
|
+
const matchedTx0 = await (0, tracker_1.findTx)(this.client, senderVaultWalletAddress, "0", steps[0]);
|
|
513
|
+
steps[0].matchedTx = matchedTx0;
|
|
514
|
+
if (callbackFn)
|
|
515
|
+
callbackFn(steps[0]);
|
|
516
|
+
const matchedTx1 = await (0, tracker_1.findTx)(this.client, toAddress(params.vault), matchedTx0.lt.toString(), steps[1]);
|
|
517
|
+
steps[1].matchedTx = matchedTx1;
|
|
518
|
+
if (callbackFn)
|
|
519
|
+
callbackFn(steps[1]);
|
|
520
|
+
const transferMessage = matchedTx1.outMessages.find((item) => item?.parsedBody?.type === "JettonWallet.Op.Transfer");
|
|
521
|
+
const poolAggregateMessage = matchedTx1.outMessages.find((item) => item?.parsedBody?.type === "ShareVault.Op.ProvideAggregatedPool");
|
|
522
|
+
if (transferMessage && !poolAggregateMessage) {
|
|
523
|
+
const matchedTx5 = await (0, tracker_1.findTx)(this.client, toAddress(senderJettonWalletAddress), matchedTx1.lt.toString(), steps[5]);
|
|
524
|
+
steps[5].matchedTx = matchedTx5;
|
|
525
|
+
if (callbackFn)
|
|
526
|
+
callbackFn(steps[5]);
|
|
527
|
+
return;
|
|
528
|
+
}
|
|
529
|
+
if (poolAggregateMessage) {
|
|
530
|
+
const matchedTx2 = await (0, tracker_1.findTx)(this.client, toAddress(params.vault), matchedTx1.lt.toString(), steps[2]);
|
|
531
|
+
steps[2].matchedTx = matchedTx2;
|
|
532
|
+
if (callbackFn)
|
|
533
|
+
callbackFn(steps[2]);
|
|
534
|
+
const transferMessage = matchedTx2.outMessages.find((item) => item?.parsedBody?.type === "JettonWallet.Op.Transfer");
|
|
535
|
+
const withdrawMessage = matchedTx2.outMessages.find((item) => item?.parsedBody?.type === "Pool.Op.TransferOut");
|
|
536
|
+
if (transferMessage && !withdrawMessage) {
|
|
537
|
+
const matchedTx5 = await (0, tracker_1.findTx)(this.client, toAddress(params.vault), matchedTx2.lt.toString(), steps[5]);
|
|
538
|
+
steps[5].matchedTx = matchedTx5;
|
|
539
|
+
if (callbackFn)
|
|
540
|
+
callbackFn(steps[5]);
|
|
541
|
+
return;
|
|
542
|
+
}
|
|
543
|
+
const poolAddresses = matchedTx2.outMessages
|
|
544
|
+
.filter((item) => item?.parsedBody?.type === "Pool.Op.TransferOut")
|
|
545
|
+
.map((item) => item?.dst)
|
|
546
|
+
.filter((item) => !!item);
|
|
547
|
+
const matchedTxs3 = [];
|
|
548
|
+
for (let poolAddress of poolAddresses) {
|
|
549
|
+
const matchedTx = await (0, tracker_1.findTx)(this.client, toAddress(params.vault), matchedTx2.lt.toString(), { ...steps[3], to: poolAddress });
|
|
550
|
+
matchedTxs3.push(matchedTx);
|
|
551
|
+
}
|
|
552
|
+
steps[3].matchedTx = matchedTxs3;
|
|
553
|
+
if (callbackFn)
|
|
554
|
+
callbackFn(steps[3]);
|
|
555
|
+
const matchedTxs4 = [];
|
|
556
|
+
for (let poolAddress of poolAddresses) {
|
|
557
|
+
const matchedTx = await (0, tracker_1.findTx)(this.client, toAddress(params.vault), matchedTx2.lt.toString(), { ...steps[4], from: poolAddress });
|
|
558
|
+
matchedTxs4.push(matchedTx);
|
|
559
|
+
}
|
|
560
|
+
steps[4].matchedTx = matchedTxs4;
|
|
561
|
+
if (callbackFn)
|
|
562
|
+
callbackFn(steps[4]);
|
|
563
|
+
const matchedTxs5 = [];
|
|
564
|
+
for (let poolAddress of poolAddresses) {
|
|
565
|
+
const poolJettonWalletAddress = await this.getJettonWalletAddress(toString(poolAddress), toString(vaultData.asset));
|
|
566
|
+
const matchedTx = await (0, tracker_1.findTx)(this.client, poolJettonWalletAddress, matchedTx2.lt.toString(), { ...steps[5], from: poolJettonWalletAddress });
|
|
567
|
+
matchedTxs5.push(matchedTx);
|
|
568
|
+
}
|
|
569
|
+
steps[5].matchedTx = matchedTxs5;
|
|
570
|
+
if (callbackFn)
|
|
571
|
+
callbackFn(steps[5]);
|
|
572
|
+
}
|
|
573
|
+
})();
|
|
574
|
+
return {
|
|
575
|
+
action: {
|
|
576
|
+
type: "vault-withdraw",
|
|
577
|
+
params,
|
|
578
|
+
queryId,
|
|
579
|
+
},
|
|
580
|
+
steps,
|
|
581
|
+
};
|
|
582
|
+
}
|
|
583
|
+
async supplyToPool(sender, params) {
|
|
584
|
+
const shareVault = this.client.open(share_vault_1.ShareVault.createFromAddress(toAddress(params.vault)));
|
|
585
|
+
const vaultData = await shareVault.getVaultData();
|
|
586
|
+
const gas = share_vault_1.ShareVault.calculateGasFee(share_vault_1.ShareVault.Op.supplyToPool, share_vault_1.ShareVault.Gas.baseFee, Object.keys(vaultData.whitelistedPools).length, vaultData.minimumGasFee);
|
|
587
|
+
await shareVault.sendSupplyToPool(sender, gas, {
|
|
588
|
+
pool: toAddress(params.pool),
|
|
589
|
+
amount: params.amount,
|
|
590
|
+
});
|
|
591
|
+
}
|
|
592
|
+
async withdrawFromPool(sender, params) {
|
|
593
|
+
const shareVault = this.client.open(share_vault_1.ShareVault.createFromAddress(toAddress(params.vault)));
|
|
594
|
+
console.log("shareVault", shareVault);
|
|
595
|
+
const vaultData = await shareVault.getVaultData();
|
|
596
|
+
console.log("vaultData", vaultData);
|
|
597
|
+
const gas = share_vault_1.ShareVault.calculateGasFee(share_vault_1.ShareVault.Op.withdrawFromPool, share_vault_1.ShareVault.Gas.baseFee, Object.keys(vaultData.whitelistedPools).length, vaultData.minimumGasFee);
|
|
598
|
+
console.log("gas", gas);
|
|
599
|
+
await shareVault.sendWithdrawFromPool(sender, gas, {
|
|
600
|
+
pool: toAddress(params.pool),
|
|
601
|
+
isShare: params.isShare,
|
|
602
|
+
amount: params.amount,
|
|
603
|
+
});
|
|
604
|
+
}
|
|
605
|
+
async withdrawFromPoolSimulate(sender, params) {
|
|
606
|
+
const shareVault = this.client.open(share_vault_1.ShareVault.createFromAddress(toAddress(params.vault)));
|
|
607
|
+
const vaultData = await shareVault.getVaultData();
|
|
608
|
+
const gas = share_vault_1.ShareVault.calculateGasFee(share_vault_1.ShareVault.Op.withdrawFromPool, share_vault_1.ShareVault.Gas.baseFee, Object.keys(vaultData.whitelistedPools).length, vaultData.minimumGasFee);
|
|
609
|
+
console.log("vaultData", vaultData);
|
|
610
|
+
console.log("gas", gas);
|
|
611
|
+
// await shareVault.sendWithdrawFromPool(sender, gas, {
|
|
612
|
+
// pool: toAddress(params.pool),
|
|
613
|
+
// isShare: params.isShare,
|
|
614
|
+
// amount: params.amount,
|
|
615
|
+
// });
|
|
616
|
+
}
|
|
617
|
+
async setTargetWeight(sender, params) {
|
|
618
|
+
const shareVault = this.client.open(share_vault_1.ShareVault.createFromAddress(toAddress(params.vault)));
|
|
619
|
+
await shareVault.sendSetTargetWeight(sender, (0, ton_1.toNano)(0.006), {
|
|
620
|
+
pool: toAddress(params.pool),
|
|
621
|
+
targetWeight: params.targetWeight,
|
|
622
|
+
});
|
|
623
|
+
}
|
|
624
|
+
async getVaultData(vaultAddress) {
|
|
625
|
+
const vault = this.client.open(share_vault_1.ShareVault.createFromAddress(toAddress(vaultAddress)));
|
|
626
|
+
return await vault.getVaultData();
|
|
627
|
+
}
|
|
628
|
+
async getPoolCashAmounts(vaultData) {
|
|
629
|
+
const poolCashAmounts = {};
|
|
630
|
+
const poolAddresses = Object.keys(vaultData.whitelistedPools);
|
|
631
|
+
await Promise.all(poolAddresses.map(async (poolAddress) => {
|
|
632
|
+
const pool = this.getByContract(pool_1.Pool, poolAddress);
|
|
633
|
+
const poolCash = await pool.getPoolData();
|
|
634
|
+
poolCashAmounts[poolAddress] =
|
|
635
|
+
poolCash.assets[vaultData.asset.toString()].cash;
|
|
636
|
+
}));
|
|
637
|
+
return poolCashAmounts;
|
|
638
|
+
}
|
|
639
|
+
async getPoolInfoForShareVault(vaultData) {
|
|
640
|
+
const poolInfoForShareVault = {};
|
|
641
|
+
await Promise.all(Object.entries(vaultData.whitelistedPools).map(async ([poolAddress, whitelistedPool]) => {
|
|
642
|
+
const pool = this.getByContract(pool_1.Pool, poolAddress);
|
|
643
|
+
const poolData = await pool.getPoolData();
|
|
644
|
+
const poolAssetInfo = poolData.assets[vaultData.asset.toString()];
|
|
645
|
+
poolInfoForShareVault[poolAddress] = {
|
|
646
|
+
cash: poolData.assets[vaultData.asset.toString()].cash,
|
|
647
|
+
totalSupplyAmount: poolAssetInfo.totalSupplyAmount,
|
|
648
|
+
totalSupplyShare: poolAssetInfo.totalSupplyShare,
|
|
649
|
+
vaultSupplyAmount: (poolAssetInfo.totalSupplyAmount * whitelistedPool.supply) /
|
|
650
|
+
poolAssetInfo.totalSupplyShare,
|
|
651
|
+
};
|
|
652
|
+
}));
|
|
653
|
+
return poolInfoForShareVault;
|
|
654
|
+
}
|
|
655
|
+
async estimateWithdrawLiquidityFulfillmentPoolCount(vaultData, requst_vault_shares) {
|
|
656
|
+
const poolInfoForShareVault = await this.getPoolInfoForShareVault(vaultData);
|
|
657
|
+
const totalAssetAmount = vaultData.cash +
|
|
658
|
+
Object.values(poolInfoForShareVault).reduce((acc, pool) => acc + pool.vaultSupplyAmount, 0n);
|
|
659
|
+
const sortedPools = this.getPoolTupleSortedByTargetWeight(vaultData.whitelistedPools, totalAssetAmount, vaultData.totalTargetWeight, poolInfoForShareVault);
|
|
660
|
+
let restAmount = (requst_vault_shares * totalAssetAmount) / vaultData.totalSupply;
|
|
661
|
+
let count = 0;
|
|
662
|
+
for (const pool of sortedPools) {
|
|
663
|
+
const withdrawFulfillAmount = pool.availableAmount < restAmount ? pool.availableAmount : restAmount;
|
|
664
|
+
if (withdrawFulfillAmount !== 0n) {
|
|
665
|
+
restAmount -= withdrawFulfillAmount;
|
|
666
|
+
count++;
|
|
667
|
+
if (restAmount === 0n) {
|
|
668
|
+
break;
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
count += 1; // TODO: refine gas estimation - temporary add buffer, needs accurate calculation later
|
|
673
|
+
return { count, sortedPools };
|
|
674
|
+
}
|
|
675
|
+
getPoolTupleSortedByTargetWeight(whitelistedPools, totalAssetAmount, totalTargetWeight, poolInfoForShareVault) {
|
|
676
|
+
const DECIMALS_OF_WEIGHT = 10000n;
|
|
677
|
+
const poolTuples = [];
|
|
678
|
+
for (const [poolAddress, pool] of Object.entries(whitelistedPools)) {
|
|
679
|
+
if (pool.supply > 0n) {
|
|
680
|
+
const poolCash = poolInfoForShareVault[poolAddress].cash || 0n;
|
|
681
|
+
const supplyAmount = (poolInfoForShareVault[poolAddress].totalSupplyAmount * pool.supply) /
|
|
682
|
+
poolInfoForShareVault[poolAddress].totalSupplyShare;
|
|
683
|
+
const availableAmount = poolCash > supplyAmount ? supplyAmount : poolCash;
|
|
684
|
+
if (pool.targetWeight === 0n) {
|
|
685
|
+
// target weight가 0인 경우
|
|
686
|
+
if (poolCash > 0n && pool.supply > 0n) {
|
|
687
|
+
poolTuples.push({
|
|
688
|
+
targetWeightRatio: -1, // 특별한 값으로 마킹
|
|
689
|
+
poolAddress,
|
|
690
|
+
availableAmount,
|
|
691
|
+
pool,
|
|
692
|
+
});
|
|
693
|
+
}
|
|
694
|
+
}
|
|
695
|
+
else {
|
|
696
|
+
// target weight가 있는 경우
|
|
697
|
+
const suppliedWeight = (DECIMALS_OF_WEIGHT * pool.supply) / totalAssetAmount;
|
|
698
|
+
const targetWeight = (DECIMALS_OF_WEIGHT * pool.targetWeight) / totalTargetWeight;
|
|
699
|
+
const targetWeightRatio = Number((suppliedWeight * DECIMALS_OF_WEIGHT) / targetWeight) /
|
|
700
|
+
Number(DECIMALS_OF_WEIGHT);
|
|
701
|
+
poolTuples.push({
|
|
702
|
+
targetWeightRatio,
|
|
703
|
+
poolAddress,
|
|
704
|
+
availableAmount,
|
|
705
|
+
pool,
|
|
706
|
+
});
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
}
|
|
710
|
+
// Sort: items with targetWeightRatio -1 come first, others in descending order
|
|
711
|
+
return poolTuples.sort((a, b) => {
|
|
712
|
+
if (a.targetWeightRatio === -1 && b.targetWeightRatio === -1)
|
|
713
|
+
return 0;
|
|
714
|
+
if (a.targetWeightRatio === -1)
|
|
715
|
+
return -1;
|
|
716
|
+
if (b.targetWeightRatio === -1)
|
|
717
|
+
return 1;
|
|
718
|
+
return b.targetWeightRatio - a.targetWeightRatio;
|
|
719
|
+
});
|
|
720
|
+
}
|
|
721
|
+
WTON = {
|
|
722
|
+
unwrap: async (sender, params) => {
|
|
723
|
+
const walletAddress = await this.getJettonWalletAddress(toString(params.sender), this.contracts.WTON);
|
|
724
|
+
const wtonWallet = this.client.open(jetton_wallet_2.WTONWallet.createFromAddress(walletAddress));
|
|
725
|
+
await wtonWallet.sendBurn(sender, (0, ton_1.toNano)(0.05), {
|
|
726
|
+
amount: params.amount,
|
|
727
|
+
response: toAddress(params.recipient),
|
|
728
|
+
});
|
|
729
|
+
},
|
|
730
|
+
wrap: async (sender, params) => {
|
|
731
|
+
const walletAddress = await this.getJettonWalletAddress(toString(params.recipient), this.contracts.WTON);
|
|
732
|
+
const wtonWallet = this.client.open(jetton_wallet_2.WTONWallet.createFromAddress(walletAddress));
|
|
733
|
+
const wtonWalletState = await wtonWallet.getState();
|
|
734
|
+
wtonWalletState.state.type === "active";
|
|
735
|
+
if (wtonWalletState.state.type === "active") {
|
|
736
|
+
await wtonWallet.sendMint(sender, (0, ton_1.toNano)(0.05) + params.amount);
|
|
737
|
+
}
|
|
738
|
+
else {
|
|
739
|
+
const wton = this.client.open(jetton_minter_2.WTONMinter.createFromAddress(ton_1.Address.parse(this.contracts.WTON)));
|
|
740
|
+
await wton.sendMint(sender, (0, ton_1.toNano)(0.05) + params.amount, {
|
|
741
|
+
recipient: toAddress(params.recipient),
|
|
742
|
+
});
|
|
743
|
+
}
|
|
744
|
+
},
|
|
745
|
+
};
|
|
746
|
+
}
|
|
747
|
+
exports.ShareVaultV1 = ShareVaultV1;
|