@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,1841 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PoolCacheV1 = void 0;
|
|
4
|
+
const ton_1 = require("@ton/ton");
|
|
5
|
+
const pool_1 = require("./contracts/core/pool");
|
|
6
|
+
const type_1 = require("./contracts/oracle/redstone-onchain-oracle/type");
|
|
7
|
+
const account_1 = require("./contracts/core/account");
|
|
8
|
+
const jetton_minter_1 = require("./contracts/jetton/jetton-minter");
|
|
9
|
+
const jetton_wallet_1 = require("./contracts/jetton/jetton-wallet");
|
|
10
|
+
const tracker_1 = require("./utils/tracker");
|
|
11
|
+
const query_id_generactor_1 = require("./utils/tracker/query-id-generactor");
|
|
12
|
+
const parser_1 = require("./utils/parser");
|
|
13
|
+
const jetton_wallet_2 = require("./contracts/wton/jetton-wallet");
|
|
14
|
+
const redstone_onchain_oracle_1 = require("./contracts/oracle/redstone-onchain-oracle");
|
|
15
|
+
const redstoneHelper_1 = require("./utils/oracle/redstone/redstoneHelper");
|
|
16
|
+
const service_1 = require("./common/service");
|
|
17
|
+
const jetton_minter_2 = require("./contracts/wton/jetton-minter");
|
|
18
|
+
const readonlyCachedRedstone_1 = require("./utils/oracle/redstone/readonlyCachedRedstone");
|
|
19
|
+
const toAddress = (value) => {
|
|
20
|
+
if (value instanceof ton_1.Address) {
|
|
21
|
+
return value;
|
|
22
|
+
}
|
|
23
|
+
return ton_1.Address.parse(value);
|
|
24
|
+
};
|
|
25
|
+
const toString = (value) => {
|
|
26
|
+
if (value instanceof ton_1.Address) {
|
|
27
|
+
return value.toString();
|
|
28
|
+
}
|
|
29
|
+
return value;
|
|
30
|
+
};
|
|
31
|
+
class PoolCacheV1 extends service_1.ServiceBaseV1 {
|
|
32
|
+
// user -> asset -> wallet
|
|
33
|
+
walletAddresses = {};
|
|
34
|
+
// user -> pool -> account
|
|
35
|
+
accountAddresses = {};
|
|
36
|
+
async getJettonWalletAddress(owner, jetton) {
|
|
37
|
+
this.walletAddresses[owner] ||= {};
|
|
38
|
+
if (!this.walletAddresses[owner][jetton]) {
|
|
39
|
+
const jettonMinter = this.open(jetton_minter_1.JettonMinter.createFromAddress(toAddress(jetton)));
|
|
40
|
+
this.walletAddresses[owner][jetton] = await jettonMinter.getWalletAddress(toAddress(owner));
|
|
41
|
+
}
|
|
42
|
+
return this.walletAddresses[owner][jetton];
|
|
43
|
+
}
|
|
44
|
+
async getAccountAddress(owner, pool) {
|
|
45
|
+
this.accountAddresses[owner] ||= {};
|
|
46
|
+
if (!this.accountAddresses[owner][pool]) {
|
|
47
|
+
const poolContract = this.open(pool_1.Pool.createFromAddress(toAddress(pool)));
|
|
48
|
+
this.accountAddresses[owner][pool] = await poolContract.getAccountAddress(toAddress(owner));
|
|
49
|
+
}
|
|
50
|
+
return this.accountAddresses[owner][pool];
|
|
51
|
+
}
|
|
52
|
+
async findAndCallback(target, step, createdLt, callbackFn) {
|
|
53
|
+
const matchedTx = await (0, tracker_1.findTx)(this.client, toAddress(target), createdLt.toString(), step);
|
|
54
|
+
step.matchedTx = matchedTx;
|
|
55
|
+
if (matchedTx.inMessage?.body) {
|
|
56
|
+
const parsed = (0, parser_1.parseActionNotification)(matchedTx.inMessage.body);
|
|
57
|
+
if (parsed) {
|
|
58
|
+
step.actionNotificationResult = parsed;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
if (callbackFn)
|
|
62
|
+
callbackFn(step);
|
|
63
|
+
return matchedTx;
|
|
64
|
+
}
|
|
65
|
+
async supply(sender, params) {
|
|
66
|
+
const isWTON = toString(params.asset) == this.contracts.WTON;
|
|
67
|
+
const jettonForwardTonAmount = isWTON ? (0, ton_1.toNano)(0.02) : 0n;
|
|
68
|
+
const jettonForwardPayload = isWTON ? jetton_minter_2.WTON_UNWRAP_CELL : null;
|
|
69
|
+
const pool = this.open(pool_1.Pool.createFromAddress(toAddress(params.pool)));
|
|
70
|
+
const poolData = await pool.getPoolData();
|
|
71
|
+
// async calculateGasFee(actionOp: number, baseFee: bigint, forwardFee: bigint, forwardTonAmount: bigint, useInternalOracle: boolean, useOracle: boolean, gas: PoolGasFee) {
|
|
72
|
+
const gas = pool_1.Pool.calculateGasFee(pool_1.Pool.Op.Supply, pool_1.Pool.Gas.baseFee, pool_1.Pool.Gas.forwardFee, jettonForwardTonAmount, !poolData.oracle, false, poolData.minimumGasFee);
|
|
73
|
+
if (toString(params.asset) == this.contracts.WTON) {
|
|
74
|
+
const poolWTONWalletAddress = await this.getJettonWalletAddress(toString(params.pool), toString(params.asset));
|
|
75
|
+
const poolWTONWallet = this.open(jetton_wallet_2.WTONWallet.createFromAddress(poolWTONWalletAddress));
|
|
76
|
+
await poolWTONWallet.sendExternalTransfer(sender, jetton_wallet_2.WTONWallet.Gas.ExternalTransfer, {
|
|
77
|
+
recipient: toAddress(params.pool),
|
|
78
|
+
response: toAddress(params.response),
|
|
79
|
+
amount: params.amount,
|
|
80
|
+
forwardTonAmount: gas,
|
|
81
|
+
forwardPayload: pool_1.Pool.createSupplyPayload({
|
|
82
|
+
asset: toAddress(params.asset),
|
|
83
|
+
recipient: toAddress(params.recipient),
|
|
84
|
+
response: toAddress(params.response),
|
|
85
|
+
jettonForwardTonAmount,
|
|
86
|
+
jettonForwardPayload,
|
|
87
|
+
}),
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
else {
|
|
91
|
+
const senderJettonWalletAddress = await this.getJettonWalletAddress(toString(params.sender), toString(params.asset));
|
|
92
|
+
const senderJettonWallet = this.open(jetton_wallet_1.JettonWallet.createFromAddress(senderJettonWalletAddress));
|
|
93
|
+
await senderJettonWallet.sendTransfer(sender, gas + jetton_wallet_1.JettonWallet.Gas.Transfer, {
|
|
94
|
+
recipient: toAddress(params.pool),
|
|
95
|
+
response: toAddress(params.response),
|
|
96
|
+
amount: params.amount,
|
|
97
|
+
forwardTonAmount: gas,
|
|
98
|
+
forwardPayload: pool_1.Pool.createSupplyPayload({
|
|
99
|
+
asset: toAddress(params.asset),
|
|
100
|
+
recipient: toAddress(params.recipient),
|
|
101
|
+
response: toAddress(params.response),
|
|
102
|
+
jettonForwardTonAmount,
|
|
103
|
+
jettonForwardPayload,
|
|
104
|
+
}),
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
async supplyAndWaitTx(sender, params, callbackFn) {
|
|
109
|
+
const senderJettonWalletAddress = await this.getJettonWalletAddress(toString(params.sender), toString(params.asset));
|
|
110
|
+
const senderJettonWallet = this.open(jetton_wallet_1.JettonWallet.createFromAddress(senderJettonWalletAddress));
|
|
111
|
+
const pool = this.open(pool_1.Pool.createFromAddress(toAddress(params.pool)));
|
|
112
|
+
const poolJettonWalletAddress = await this.getJettonWalletAddress(toString(params.pool), toString(params.asset));
|
|
113
|
+
const recipientAccountAddress = await this.getAccountAddress(toString(params.recipient), toString(params.pool));
|
|
114
|
+
const isWTON = toString(params.asset) == this.contracts.WTON;
|
|
115
|
+
const jettonForwardTonAmount = isWTON ? (0, ton_1.toNano)(0.02) : 0n;
|
|
116
|
+
const jettonForwardPayload = isWTON ? jetton_minter_2.WTON_UNWRAP_CELL : null;
|
|
117
|
+
const poolData = await pool.getPoolData();
|
|
118
|
+
const gas = pool_1.Pool.calculateGasFee(pool_1.Pool.Op.Supply, pool_1.Pool.Gas.baseFee, pool_1.Pool.Gas.forwardFee, jettonForwardTonAmount, !poolData.oracle, false, poolData.minimumGasFee);
|
|
119
|
+
const opts = {
|
|
120
|
+
recipient: toAddress(params.pool),
|
|
121
|
+
response: toAddress(params.response),
|
|
122
|
+
amount: params.amount,
|
|
123
|
+
forwardTonAmount: gas,
|
|
124
|
+
forwardPayload: pool_1.Pool.createSupplyPayload({
|
|
125
|
+
asset: toAddress(params.asset),
|
|
126
|
+
recipient: toAddress(params.recipient),
|
|
127
|
+
response: toAddress(params.response),
|
|
128
|
+
jettonForwardTonAmount,
|
|
129
|
+
jettonForwardPayload,
|
|
130
|
+
}),
|
|
131
|
+
};
|
|
132
|
+
const body = senderJettonWallet.createTransferBody(opts);
|
|
133
|
+
const queryId = (0, query_id_generactor_1.generateQueryId)(toAddress(params.sender), body);
|
|
134
|
+
const steps = [];
|
|
135
|
+
if (toString(params.asset) == this.contracts.WTON) {
|
|
136
|
+
const poolWTONWalletAddress = await this.getJettonWalletAddress(toString(params.pool), toString(params.asset));
|
|
137
|
+
const poolWTONWallet = this.open(jetton_wallet_2.WTONWallet.createFromAddress(poolWTONWalletAddress));
|
|
138
|
+
await poolWTONWallet.sendExternalTransfer(sender, jetton_wallet_2.WTONWallet.Gas.ExternalTransfer, {
|
|
139
|
+
queryId: queryId,
|
|
140
|
+
...opts,
|
|
141
|
+
});
|
|
142
|
+
steps.push(...[
|
|
143
|
+
{
|
|
144
|
+
queryId,
|
|
145
|
+
description: "Sending the tx",
|
|
146
|
+
from: toAddress(params.sender),
|
|
147
|
+
to: poolWTONWalletAddress,
|
|
148
|
+
opcode: jetton_wallet_2.WTONWallet.Op.ExternalTransfer,
|
|
149
|
+
},
|
|
150
|
+
{
|
|
151
|
+
queryId,
|
|
152
|
+
description: "Supplying jetton to the pool",
|
|
153
|
+
from: toAddress(params.sender),
|
|
154
|
+
to: poolWTONWalletAddress,
|
|
155
|
+
opcode: jetton_wallet_2.WTONWallet.Op.ExternalTransfer,
|
|
156
|
+
},
|
|
157
|
+
]);
|
|
158
|
+
}
|
|
159
|
+
else {
|
|
160
|
+
await senderJettonWallet.sendTransfer(sender, gas + jetton_wallet_1.JettonWallet.Gas.Transfer, {
|
|
161
|
+
queryId: queryId,
|
|
162
|
+
...opts,
|
|
163
|
+
});
|
|
164
|
+
steps.push({
|
|
165
|
+
queryId,
|
|
166
|
+
description: "Sending the tx",
|
|
167
|
+
from: toAddress(params.sender),
|
|
168
|
+
to: senderJettonWalletAddress,
|
|
169
|
+
opcode: jetton_wallet_1.JettonWallet.Op.Transfer,
|
|
170
|
+
});
|
|
171
|
+
steps.push({
|
|
172
|
+
queryId,
|
|
173
|
+
description: "Supplying jetton to the pool",
|
|
174
|
+
from: senderJettonWalletAddress,
|
|
175
|
+
to: poolJettonWalletAddress,
|
|
176
|
+
opcode: jetton_wallet_1.JettonWallet.Op.InternalTransfer,
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
steps.push(...[
|
|
180
|
+
{
|
|
181
|
+
queryId,
|
|
182
|
+
description: "Pool notification",
|
|
183
|
+
from: poolJettonWalletAddress,
|
|
184
|
+
to: pool.address,
|
|
185
|
+
opcode: jetton_wallet_1.JettonWallet.Op.TransferNotification,
|
|
186
|
+
},
|
|
187
|
+
{
|
|
188
|
+
queryId,
|
|
189
|
+
description: "Action notification",
|
|
190
|
+
from: pool.address,
|
|
191
|
+
to: toAddress(params.sender),
|
|
192
|
+
opcode: pool_1.Pool.Op.ActionNotification,
|
|
193
|
+
},
|
|
194
|
+
{
|
|
195
|
+
queryId,
|
|
196
|
+
description: "Updating the account",
|
|
197
|
+
from: pool.address,
|
|
198
|
+
to: recipientAccountAddress,
|
|
199
|
+
opcode: pool_1.Pool.Op.UpdateAccountStatus,
|
|
200
|
+
},
|
|
201
|
+
]);
|
|
202
|
+
(async () => {
|
|
203
|
+
const matchedTx0 = await this.findAndCallback(toAddress(params.sender), steps[0], "0", callbackFn);
|
|
204
|
+
const matchedTx1 = await this.findAndCallback(poolJettonWalletAddress, steps[1], matchedTx0.lt.toString(), callbackFn);
|
|
205
|
+
const matchedTx2 = await this.findAndCallback(pool.address, steps[2], matchedTx1.lt.toString(), callbackFn);
|
|
206
|
+
const matchedTx3 = await this.findAndCallback(toAddress(params.sender), steps[3], matchedTx2.lt.toString(), callbackFn);
|
|
207
|
+
const matchedTx4 = await this.findAndCallback(recipientAccountAddress, steps[4], matchedTx3.lt.toString(), callbackFn);
|
|
208
|
+
})();
|
|
209
|
+
return {
|
|
210
|
+
action: {
|
|
211
|
+
type: "pool-supply",
|
|
212
|
+
params,
|
|
213
|
+
queryId,
|
|
214
|
+
},
|
|
215
|
+
steps,
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
async findTx(action, callbackFn) {
|
|
219
|
+
if (action.type == "pool-supply") {
|
|
220
|
+
return this.findSupplyTx(BigInt(action.queryId), action.params, callbackFn);
|
|
221
|
+
}
|
|
222
|
+
else if (action.type == "pool-withdraw") {
|
|
223
|
+
return this.findWithdrawTx(BigInt(action.queryId), action.params, callbackFn);
|
|
224
|
+
}
|
|
225
|
+
else if (action.type == "pool-borrow") {
|
|
226
|
+
return this.findBorrowTx(BigInt(action.queryId), action.params, callbackFn);
|
|
227
|
+
}
|
|
228
|
+
else if (action.type == "pool-repay") {
|
|
229
|
+
return this.findRepayTx(BigInt(action.queryId), action.params, callbackFn);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
async getPricesV2(poolAddress) {
|
|
233
|
+
const pool = this.client.open(pool_1.Pool.createFromAddress(toAddress(poolAddress)));
|
|
234
|
+
const poolData = await pool.getPoolData();
|
|
235
|
+
const _assets = Object.keys(poolData.assets);
|
|
236
|
+
const oracleConfig = poolData.oracleConfig;
|
|
237
|
+
const noVaultAssets = [...Object.values(oracleConfig.redstoneAssetInfo).map(item => item.address.toString()), ...Object.keys(oracleConfig.assetOnchainDataInfo).map(item => item)];
|
|
238
|
+
const assets = [];
|
|
239
|
+
const vaultAssets = [];
|
|
240
|
+
for (const asset of _assets) {
|
|
241
|
+
if (noVaultAssets.includes(asset.toString())) {
|
|
242
|
+
assets.push(asset);
|
|
243
|
+
}
|
|
244
|
+
else {
|
|
245
|
+
vaultAssets.push(asset);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
const { redstonePrices, onchainData: od } = await this.getRedstonePrices(assets, poolData);
|
|
249
|
+
const onchainData2 = await this.updateOnchainData(oracleConfig.assetOnchainDataInfo);
|
|
250
|
+
const onchainData = { ...od, ...onchainData2 };
|
|
251
|
+
const prices = this.calculatePrices(redstonePrices, onchainData);
|
|
252
|
+
return {
|
|
253
|
+
...prices,
|
|
254
|
+
...vaultAssets.reduce((acc, asset) => {
|
|
255
|
+
acc[asset] = { type: 10, price: BigInt(1e8) };
|
|
256
|
+
return acc;
|
|
257
|
+
}, {})
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
async findSupplyTx(queryId, params, callbackFn) {
|
|
261
|
+
const senderJettonWalletAddress = await this.getJettonWalletAddress(toString(params.sender), toString(params.asset));
|
|
262
|
+
const pool = this.open(pool_1.Pool.createFromAddress(toAddress(params.pool)));
|
|
263
|
+
const poolJettonWalletAddress = await this.getJettonWalletAddress(toString(params.pool), toString(params.asset));
|
|
264
|
+
const recipientAccountAddress = await this.getAccountAddress(toString(params.recipient), toString(params.pool));
|
|
265
|
+
const steps = [];
|
|
266
|
+
if (toString(params.asset) == this.contracts.WTON) {
|
|
267
|
+
const poolWTONWalletAddress = await this.getJettonWalletAddress(toString(params.pool), toString(params.asset));
|
|
268
|
+
const poolWTONWallet = this.open(jetton_wallet_2.WTONWallet.createFromAddress(poolWTONWalletAddress));
|
|
269
|
+
steps.push(...[
|
|
270
|
+
{
|
|
271
|
+
queryId,
|
|
272
|
+
description: "Sending the tx",
|
|
273
|
+
from: toAddress(params.sender),
|
|
274
|
+
to: poolWTONWalletAddress,
|
|
275
|
+
opcode: jetton_wallet_2.WTONWallet.Op.ExternalTransfer,
|
|
276
|
+
},
|
|
277
|
+
{
|
|
278
|
+
queryId,
|
|
279
|
+
description: "Supplying jetton to the pool",
|
|
280
|
+
from: toAddress(params.sender),
|
|
281
|
+
to: poolWTONWalletAddress,
|
|
282
|
+
opcode: jetton_wallet_2.WTONWallet.Op.ExternalTransfer,
|
|
283
|
+
},
|
|
284
|
+
]);
|
|
285
|
+
}
|
|
286
|
+
else {
|
|
287
|
+
steps.push({
|
|
288
|
+
queryId,
|
|
289
|
+
description: "Sending the tx",
|
|
290
|
+
from: toAddress(params.sender),
|
|
291
|
+
to: senderJettonWalletAddress,
|
|
292
|
+
opcode: jetton_wallet_1.JettonWallet.Op.Transfer,
|
|
293
|
+
});
|
|
294
|
+
steps.push({
|
|
295
|
+
queryId,
|
|
296
|
+
description: "Supplying jetton to the pool",
|
|
297
|
+
from: senderJettonWalletAddress,
|
|
298
|
+
to: poolJettonWalletAddress,
|
|
299
|
+
opcode: jetton_wallet_1.JettonWallet.Op.InternalTransfer,
|
|
300
|
+
});
|
|
301
|
+
}
|
|
302
|
+
steps.push(...[
|
|
303
|
+
{
|
|
304
|
+
queryId,
|
|
305
|
+
description: "Pool notification",
|
|
306
|
+
from: poolJettonWalletAddress,
|
|
307
|
+
to: pool.address,
|
|
308
|
+
opcode: jetton_wallet_1.JettonWallet.Op.TransferNotification,
|
|
309
|
+
},
|
|
310
|
+
{
|
|
311
|
+
queryId,
|
|
312
|
+
description: "Action notification",
|
|
313
|
+
from: pool.address,
|
|
314
|
+
to: toAddress(params.sender),
|
|
315
|
+
opcode: pool_1.Pool.Op.ActionNotification,
|
|
316
|
+
},
|
|
317
|
+
{
|
|
318
|
+
queryId,
|
|
319
|
+
description: "Updating the account",
|
|
320
|
+
from: pool.address,
|
|
321
|
+
to: recipientAccountAddress,
|
|
322
|
+
opcode: pool_1.Pool.Op.UpdateAccountStatus,
|
|
323
|
+
},
|
|
324
|
+
]);
|
|
325
|
+
(async () => {
|
|
326
|
+
const matchedTx0 = await this.findAndCallback(toAddress(params.sender), steps[0], "0", callbackFn);
|
|
327
|
+
const matchedTx1 = await this.findAndCallback(poolJettonWalletAddress, steps[1], matchedTx0.lt.toString(), callbackFn);
|
|
328
|
+
const matchedTx2 = await this.findAndCallback(pool.address, steps[2], matchedTx1.lt.toString(), callbackFn);
|
|
329
|
+
const matchedTx3 = await this.findAndCallback(toAddress(params.sender), steps[3], matchedTx2.lt.toString(), callbackFn);
|
|
330
|
+
const matchedTx4 = await this.findAndCallback(recipientAccountAddress, steps[4], matchedTx3.lt.toString(), callbackFn);
|
|
331
|
+
})();
|
|
332
|
+
return {
|
|
333
|
+
action: {
|
|
334
|
+
type: "pool-supply",
|
|
335
|
+
params,
|
|
336
|
+
queryId,
|
|
337
|
+
},
|
|
338
|
+
steps,
|
|
339
|
+
};
|
|
340
|
+
}
|
|
341
|
+
async withdraw(sender, params) {
|
|
342
|
+
const senderAccountAddress = await this.getAccountAddress(toString(params.sender), toString(params.pool));
|
|
343
|
+
const senderAccount = this.open(account_1.Account.createFromAddress(senderAccountAddress));
|
|
344
|
+
const accountData = await senderAccount.getAccountData();
|
|
345
|
+
const pool = this.open(pool_1.Pool.createFromAddress(toAddress(params.pool)));
|
|
346
|
+
const poolData = (await pool.getPoolData());
|
|
347
|
+
let oracleParams = null;
|
|
348
|
+
if (account_1.Account.haveBorrow(accountData)) {
|
|
349
|
+
const assets = [...new Set([...account_1.Account.haveAssets(accountData)])];
|
|
350
|
+
const result = redstone_onchain_oracle_1.RedstoneOnchainOracle.getOracleRequestParams(assets, poolData.oracleConfig);
|
|
351
|
+
const helper = (0, redstoneHelper_1.createRedstoneHelper)("prod");
|
|
352
|
+
const payload = await helper.createPayload(result.redstoneAssetNames);
|
|
353
|
+
const requestAssetCell = redstone_onchain_oracle_1.RedstoneOnchainOracle.createAddressToTupleCell(result.requestAssets);
|
|
354
|
+
const dataFeedIdsTupleBuilder = new ton_1.TupleBuilder();
|
|
355
|
+
result.feedIds.forEach((item) => dataFeedIdsTupleBuilder.writeNumber(item));
|
|
356
|
+
const feedIdCell = (0, ton_1.serializeTuple)(dataFeedIdsTupleBuilder.build());
|
|
357
|
+
oracleParams = (0, ton_1.beginCell)()
|
|
358
|
+
.storeRef(feedIdCell)
|
|
359
|
+
.storeRef(payload)
|
|
360
|
+
.storeRef(requestAssetCell)
|
|
361
|
+
.endCell();
|
|
362
|
+
}
|
|
363
|
+
const isWTON = toString(params.asset) == this.contracts.WTON;
|
|
364
|
+
const jettonForwardTonAmount = isWTON ? (0, ton_1.toNano)(0.02) : 0n;
|
|
365
|
+
const jettonForwardPayload = isWTON ? jetton_minter_2.WTON_UNWRAP_CELL : null;
|
|
366
|
+
const gas = pool_1.Pool.calculateGasFee(pool_1.Pool.Op.Withdraw, pool_1.Pool.Gas.baseFee, pool_1.Pool.Gas.forwardFee, jettonForwardTonAmount, !poolData.oracle, !!oracleParams, poolData.minimumGasFee);
|
|
367
|
+
await pool.sendWithdraw(sender, gas, {
|
|
368
|
+
asset: toAddress(params.asset),
|
|
369
|
+
isShare: params.isShare,
|
|
370
|
+
amount: params.amount,
|
|
371
|
+
recipient: toAddress(params.recipient),
|
|
372
|
+
response: toAddress(params.response),
|
|
373
|
+
jettonForwardTonAmount,
|
|
374
|
+
jettonForwardPayload,
|
|
375
|
+
oracleParams: oracleParams,
|
|
376
|
+
});
|
|
377
|
+
}
|
|
378
|
+
async withdrawAndWaitTx(sender, params, callbackFn) {
|
|
379
|
+
const senderAccountAddress = await this.getAccountAddress(toString(params.sender), toString(params.pool));
|
|
380
|
+
const senderAccount = this.open(account_1.Account.createFromAddress(senderAccountAddress));
|
|
381
|
+
const accountData = await senderAccount.getAccountData();
|
|
382
|
+
const pool = this.open(pool_1.Pool.createFromAddress(toAddress(params.pool)));
|
|
383
|
+
const poolData = (await pool.getPoolData());
|
|
384
|
+
const poolJettonWalletAddress = await this.getJettonWalletAddress(toString(params.pool), toString(params.asset));
|
|
385
|
+
const recipeintJettonWalletAddress = await this.getJettonWalletAddress(toString(params.recipient), toString(params.asset));
|
|
386
|
+
let oracleParams = null;
|
|
387
|
+
if (account_1.Account.haveBorrow(accountData)) {
|
|
388
|
+
const assets = [...new Set([...account_1.Account.haveAssets(accountData)])];
|
|
389
|
+
const result = redstone_onchain_oracle_1.RedstoneOnchainOracle.getOracleRequestParams(assets, poolData.oracleConfig);
|
|
390
|
+
const helper = (0, redstoneHelper_1.createRedstoneHelper)("prod");
|
|
391
|
+
const payload = await helper.createPayload(result.redstoneAssetNames);
|
|
392
|
+
const requestAssetCell = redstone_onchain_oracle_1.RedstoneOnchainOracle.createAddressToTupleCell(result.requestAssets);
|
|
393
|
+
const dataFeedIdsTupleBuilder = new ton_1.TupleBuilder();
|
|
394
|
+
result.feedIds.forEach((item) => dataFeedIdsTupleBuilder.writeNumber(item));
|
|
395
|
+
const feedIdCell = (0, ton_1.serializeTuple)(dataFeedIdsTupleBuilder.build());
|
|
396
|
+
oracleParams = (0, ton_1.beginCell)()
|
|
397
|
+
.storeRef(feedIdCell)
|
|
398
|
+
.storeRef(payload)
|
|
399
|
+
.storeRef(requestAssetCell)
|
|
400
|
+
.endCell();
|
|
401
|
+
}
|
|
402
|
+
const isWTON = toString(params.asset) == this.contracts.WTON;
|
|
403
|
+
const jettonForwardTonAmount = isWTON ? (0, ton_1.toNano)(0.02) : 0n;
|
|
404
|
+
const jettonForwardPayload = isWTON ? jetton_minter_2.WTON_UNWRAP_CELL : null;
|
|
405
|
+
const opts = {
|
|
406
|
+
asset: toAddress(params.asset),
|
|
407
|
+
isShare: params.isShare,
|
|
408
|
+
amount: params.amount,
|
|
409
|
+
recipient: toAddress(params.recipient),
|
|
410
|
+
response: toAddress(params.response),
|
|
411
|
+
jettonForwardTonAmount,
|
|
412
|
+
jettonForwardPayload,
|
|
413
|
+
oracleParams: oracleParams,
|
|
414
|
+
};
|
|
415
|
+
const body = pool_1.Pool.createWithdrawMsgBody(opts);
|
|
416
|
+
const queryId = (0, query_id_generactor_1.generateQueryId)(toAddress(params.sender), body);
|
|
417
|
+
const gas = pool_1.Pool.calculateGasFee(pool_1.Pool.Op.Withdraw, pool_1.Pool.Gas.baseFee, pool_1.Pool.Gas.forwardFee, jettonForwardTonAmount, !poolData.oracle, !!oracleParams, poolData.minimumGasFee);
|
|
418
|
+
await pool.sendWithdraw(sender, gas, {
|
|
419
|
+
...opts,
|
|
420
|
+
queryId,
|
|
421
|
+
});
|
|
422
|
+
const steps = [
|
|
423
|
+
{
|
|
424
|
+
queryId,
|
|
425
|
+
description: "Sending the tx",
|
|
426
|
+
from: toAddress(params.sender),
|
|
427
|
+
to: toAddress(params.pool),
|
|
428
|
+
opcode: pool_1.Pool.Op.Withdraw,
|
|
429
|
+
},
|
|
430
|
+
{
|
|
431
|
+
queryId,
|
|
432
|
+
description: "Calculating oracle data",
|
|
433
|
+
from: toAddress(poolData.oracle ? poolData.oracle : params.pool),
|
|
434
|
+
to: toAddress(params.pool),
|
|
435
|
+
},
|
|
436
|
+
{
|
|
437
|
+
queryId,
|
|
438
|
+
description: "Updating the account",
|
|
439
|
+
from: toAddress(params.pool),
|
|
440
|
+
to: senderAccountAddress,
|
|
441
|
+
opcode: pool_1.Pool.Op.ExecuteAction,
|
|
442
|
+
},
|
|
443
|
+
{
|
|
444
|
+
queryId,
|
|
445
|
+
description: "Action notification",
|
|
446
|
+
from: toAddress(params.pool),
|
|
447
|
+
to: toAddress(params.sender),
|
|
448
|
+
opcode: pool_1.Pool.Op.ActionNotification,
|
|
449
|
+
},
|
|
450
|
+
{
|
|
451
|
+
queryId,
|
|
452
|
+
description: "Withdrawing jetton from the pool",
|
|
453
|
+
from: poolJettonWalletAddress,
|
|
454
|
+
to: recipeintJettonWalletAddress,
|
|
455
|
+
opcode: jetton_wallet_1.JettonWallet.Op.InternalTransfer,
|
|
456
|
+
},
|
|
457
|
+
];
|
|
458
|
+
(async () => {
|
|
459
|
+
const matchedTx0 = await this.findAndCallback(toAddress(params.sender), steps[0], "0", callbackFn);
|
|
460
|
+
if (poolData.oracle && oracleParams) {
|
|
461
|
+
const matchedTx1 = await this.findAndCallback(toAddress(params.pool), steps[1], matchedTx0.lt.toString(), callbackFn);
|
|
462
|
+
}
|
|
463
|
+
const matchedTx2 = await this.findAndCallback(toAddress(senderAccountAddress), steps[2], matchedTx0.lt.toString(), callbackFn);
|
|
464
|
+
const matchedTx3 = await this.findAndCallback(toAddress(params.sender), steps[3], matchedTx2.lt.toString(), callbackFn);
|
|
465
|
+
const matchedTx4 = await this.findAndCallback(toAddress(recipeintJettonWalletAddress), steps[4], matchedTx3.lt.toString(), callbackFn);
|
|
466
|
+
})();
|
|
467
|
+
return {
|
|
468
|
+
action: {
|
|
469
|
+
type: "pool-withdraw",
|
|
470
|
+
params,
|
|
471
|
+
queryId,
|
|
472
|
+
},
|
|
473
|
+
steps,
|
|
474
|
+
};
|
|
475
|
+
}
|
|
476
|
+
async findWithdrawTx(queryId, params, callbackFn) {
|
|
477
|
+
const senderAccountAddress = await this.getAccountAddress(toString(params.sender), toString(params.pool));
|
|
478
|
+
const senderAccount = this.open(account_1.Account.createFromAddress(senderAccountAddress));
|
|
479
|
+
const accountData = await senderAccount.getAccountData();
|
|
480
|
+
const pool = this.open(pool_1.Pool.createFromAddress(toAddress(params.pool)));
|
|
481
|
+
const poolData = (await pool.getPoolData());
|
|
482
|
+
const poolJettonWalletAddress = await this.getJettonWalletAddress(toString(params.pool), toString(params.asset));
|
|
483
|
+
const recipeintJettonWalletAddress = await this.getJettonWalletAddress(toString(params.recipient), toString(params.asset));
|
|
484
|
+
let oracleParams = null;
|
|
485
|
+
if (account_1.Account.haveBorrow(accountData)) {
|
|
486
|
+
const assets = [...new Set([...account_1.Account.haveAssets(accountData)])];
|
|
487
|
+
const result = redstone_onchain_oracle_1.RedstoneOnchainOracle.getOracleRequestParams(assets, poolData.oracleConfig);
|
|
488
|
+
const helper = (0, redstoneHelper_1.createRedstoneHelper)("prod");
|
|
489
|
+
const payload = await helper.createPayload(result.redstoneAssetNames);
|
|
490
|
+
const requestAssetCell = redstone_onchain_oracle_1.RedstoneOnchainOracle.createAddressToTupleCell(result.requestAssets);
|
|
491
|
+
const dataFeedIdsTupleBuilder = new ton_1.TupleBuilder();
|
|
492
|
+
result.feedIds.forEach((item) => dataFeedIdsTupleBuilder.writeNumber(item));
|
|
493
|
+
const feedIdCell = (0, ton_1.serializeTuple)(dataFeedIdsTupleBuilder.build());
|
|
494
|
+
oracleParams = (0, ton_1.beginCell)()
|
|
495
|
+
.storeRef(feedIdCell)
|
|
496
|
+
.storeRef(payload)
|
|
497
|
+
.storeRef(requestAssetCell)
|
|
498
|
+
.endCell();
|
|
499
|
+
}
|
|
500
|
+
const steps = [
|
|
501
|
+
{
|
|
502
|
+
queryId,
|
|
503
|
+
description: "Sending the tx",
|
|
504
|
+
from: toAddress(params.sender),
|
|
505
|
+
to: toAddress(params.pool),
|
|
506
|
+
opcode: pool_1.Pool.Op.Withdraw,
|
|
507
|
+
},
|
|
508
|
+
{
|
|
509
|
+
queryId,
|
|
510
|
+
description: "Calculating oracle data",
|
|
511
|
+
from: toAddress(poolData.oracle ? poolData.oracle : params.pool),
|
|
512
|
+
to: toAddress(params.pool),
|
|
513
|
+
},
|
|
514
|
+
{
|
|
515
|
+
queryId,
|
|
516
|
+
description: "Updating the account",
|
|
517
|
+
from: toAddress(params.pool),
|
|
518
|
+
to: senderAccountAddress,
|
|
519
|
+
opcode: pool_1.Pool.Op.ExecuteAction,
|
|
520
|
+
},
|
|
521
|
+
{
|
|
522
|
+
queryId,
|
|
523
|
+
description: "Action notification",
|
|
524
|
+
from: toAddress(params.pool),
|
|
525
|
+
to: toAddress(params.sender),
|
|
526
|
+
opcode: pool_1.Pool.Op.ActionNotification,
|
|
527
|
+
},
|
|
528
|
+
{
|
|
529
|
+
queryId,
|
|
530
|
+
description: "Withdrawing jetton from the pool",
|
|
531
|
+
from: poolJettonWalletAddress,
|
|
532
|
+
to: recipeintJettonWalletAddress,
|
|
533
|
+
opcode: jetton_wallet_1.JettonWallet.Op.InternalTransfer,
|
|
534
|
+
},
|
|
535
|
+
];
|
|
536
|
+
(async () => {
|
|
537
|
+
const matchedTx0 = await this.findAndCallback(toAddress(params.sender), steps[0], "0", callbackFn);
|
|
538
|
+
if (poolData.oracle && oracleParams) {
|
|
539
|
+
const matchedTx1 = await this.findAndCallback(toAddress(params.pool), steps[1], matchedTx0.lt.toString(), callbackFn);
|
|
540
|
+
}
|
|
541
|
+
const matchedTx2 = await this.findAndCallback(toAddress(senderAccountAddress), steps[2], matchedTx0.lt.toString(), callbackFn);
|
|
542
|
+
const matchedTx3 = await this.findAndCallback(toAddress(params.sender), steps[3], matchedTx2.lt.toString(), callbackFn);
|
|
543
|
+
const matchedTx4 = await this.findAndCallback(toAddress(recipeintJettonWalletAddress), steps[4], matchedTx3.lt.toString(), callbackFn);
|
|
544
|
+
})();
|
|
545
|
+
return {
|
|
546
|
+
action: {
|
|
547
|
+
type: "pool-withdraw",
|
|
548
|
+
params,
|
|
549
|
+
queryId,
|
|
550
|
+
},
|
|
551
|
+
steps,
|
|
552
|
+
};
|
|
553
|
+
}
|
|
554
|
+
async borrow(sender, params) {
|
|
555
|
+
const senderAccountAddress = await this.getAccountAddress(toString(params.sender), toString(params.pool));
|
|
556
|
+
const senderAccount = this.open(account_1.Account.createFromAddress(senderAccountAddress));
|
|
557
|
+
const accountData = await senderAccount.getAccountData();
|
|
558
|
+
const pool = this.open(pool_1.Pool.createFromAddress(toAddress(params.pool)));
|
|
559
|
+
const poolData = (await pool.getPoolData());
|
|
560
|
+
const assets = [
|
|
561
|
+
...new Set([...account_1.Account.haveAssets(accountData), toString(params.asset)]),
|
|
562
|
+
];
|
|
563
|
+
const result = redstone_onchain_oracle_1.RedstoneOnchainOracle.getOracleRequestParams(assets, poolData.oracleConfig);
|
|
564
|
+
const helper = (0, redstoneHelper_1.createRedstoneHelper)("prod");
|
|
565
|
+
const payload = await helper.createPayload(result.redstoneAssetNames);
|
|
566
|
+
const requestAssetCell = redstone_onchain_oracle_1.RedstoneOnchainOracle.createAddressToTupleCell(result.requestAssets);
|
|
567
|
+
const dataFeedIdsTupleBuilder = new ton_1.TupleBuilder();
|
|
568
|
+
result.feedIds.forEach((item) => dataFeedIdsTupleBuilder.writeNumber(item));
|
|
569
|
+
const feedIdCell = (0, ton_1.serializeTuple)(dataFeedIdsTupleBuilder.build());
|
|
570
|
+
const oracleParams = (0, ton_1.beginCell)()
|
|
571
|
+
.storeRef(feedIdCell)
|
|
572
|
+
.storeRef(payload)
|
|
573
|
+
.storeRef(requestAssetCell)
|
|
574
|
+
.endCell();
|
|
575
|
+
const isWTON = toString(params.asset) == this.contracts.WTON;
|
|
576
|
+
const jettonForwardTonAmount = isWTON ? (0, ton_1.toNano)(0.02) : 0n;
|
|
577
|
+
const jettonForwardPayload = isWTON ? jetton_minter_2.WTON_UNWRAP_CELL : null;
|
|
578
|
+
const gas = pool_1.Pool.calculateGasFee(pool_1.Pool.Op.Borrow, pool_1.Pool.Gas.baseFee, pool_1.Pool.Gas.forwardFee, jettonForwardTonAmount, !poolData.oracle, !!oracleParams, poolData.minimumGasFee);
|
|
579
|
+
await pool.sendBorrow(sender, gas, {
|
|
580
|
+
asset: toAddress(params.asset),
|
|
581
|
+
isShare: params.isShare,
|
|
582
|
+
amount: params.amount,
|
|
583
|
+
recipient: toAddress(params.recipient),
|
|
584
|
+
response: toAddress(params.response),
|
|
585
|
+
jettonForwardTonAmount,
|
|
586
|
+
jettonForwardPayload,
|
|
587
|
+
oracleParams: oracleParams,
|
|
588
|
+
});
|
|
589
|
+
}
|
|
590
|
+
async borrowAndWaitTx(sender, params, callbackFn) {
|
|
591
|
+
const senderAccountAddress = await this.getAccountAddress(toString(params.sender), toString(params.pool));
|
|
592
|
+
const senderAccount = this.open(account_1.Account.createFromAddress(senderAccountAddress));
|
|
593
|
+
const accountData = await senderAccount.getAccountData();
|
|
594
|
+
const pool = this.open(pool_1.Pool.createFromAddress(toAddress(params.pool)));
|
|
595
|
+
const poolData = (await pool.getPoolData());
|
|
596
|
+
const poolJettonWalletAddress = await this.getJettonWalletAddress(toString(params.pool), toString(params.asset));
|
|
597
|
+
const recipeintJettonWalletAddress = await this.getJettonWalletAddress(toString(params.recipient), toString(params.asset));
|
|
598
|
+
const assets = [
|
|
599
|
+
...new Set([...account_1.Account.haveAssets(accountData), toString(params.asset)]),
|
|
600
|
+
];
|
|
601
|
+
const result = redstone_onchain_oracle_1.RedstoneOnchainOracle.getOracleRequestParams(assets, poolData.oracleConfig);
|
|
602
|
+
const helper = (0, redstoneHelper_1.createRedstoneHelper)("prod");
|
|
603
|
+
const payload = await helper.createPayload(result.redstoneAssetNames);
|
|
604
|
+
const requestAssetCell = redstone_onchain_oracle_1.RedstoneOnchainOracle.createAddressToTupleCell(result.requestAssets);
|
|
605
|
+
const dataFeedIdsTupleBuilder = new ton_1.TupleBuilder();
|
|
606
|
+
result.feedIds.forEach((item) => dataFeedIdsTupleBuilder.writeNumber(item));
|
|
607
|
+
const feedIdCell = (0, ton_1.serializeTuple)(dataFeedIdsTupleBuilder.build());
|
|
608
|
+
const oracleParams = (0, ton_1.beginCell)()
|
|
609
|
+
.storeRef(feedIdCell)
|
|
610
|
+
.storeRef(payload)
|
|
611
|
+
.storeRef(requestAssetCell)
|
|
612
|
+
.endCell();
|
|
613
|
+
const isWTON = toString(params.asset) == this.contracts.WTON;
|
|
614
|
+
const jettonForwardTonAmount = isWTON ? (0, ton_1.toNano)(0.02) : 0n;
|
|
615
|
+
const jettonForwardPayload = isWTON ? jetton_minter_2.WTON_UNWRAP_CELL : null;
|
|
616
|
+
const opts = {
|
|
617
|
+
asset: toAddress(params.asset),
|
|
618
|
+
isShare: params.isShare,
|
|
619
|
+
amount: params.amount,
|
|
620
|
+
recipient: toAddress(params.recipient),
|
|
621
|
+
response: toAddress(params.response),
|
|
622
|
+
jettonForwardTonAmount,
|
|
623
|
+
jettonForwardPayload,
|
|
624
|
+
oracleParams: oracleParams,
|
|
625
|
+
};
|
|
626
|
+
const body = pool_1.Pool.createWithdrawMsgBody(opts);
|
|
627
|
+
const queryId = (0, query_id_generactor_1.generateQueryId)(toAddress(params.sender), body);
|
|
628
|
+
const gas = pool_1.Pool.calculateGasFee(pool_1.Pool.Op.Borrow, pool_1.Pool.Gas.baseFee, pool_1.Pool.Gas.forwardFee, jettonForwardTonAmount, !poolData.oracle, !!oracleParams, poolData.minimumGasFee);
|
|
629
|
+
await pool.sendBorrow(sender, gas, {
|
|
630
|
+
...opts,
|
|
631
|
+
queryId,
|
|
632
|
+
});
|
|
633
|
+
const steps = [
|
|
634
|
+
{
|
|
635
|
+
queryId,
|
|
636
|
+
description: "Sending the tx",
|
|
637
|
+
from: toAddress(params.sender),
|
|
638
|
+
to: toAddress(params.pool),
|
|
639
|
+
opcode: pool_1.Pool.Op.Borrow,
|
|
640
|
+
},
|
|
641
|
+
{
|
|
642
|
+
queryId,
|
|
643
|
+
description: "Calculating oracle data",
|
|
644
|
+
from: toAddress(poolData.oracle ? poolData.oracle : params.pool),
|
|
645
|
+
to: toAddress(params.pool),
|
|
646
|
+
},
|
|
647
|
+
{
|
|
648
|
+
queryId,
|
|
649
|
+
description: "Action notification",
|
|
650
|
+
from: toAddress(params.pool),
|
|
651
|
+
to: toAddress(params.sender),
|
|
652
|
+
opcode: pool_1.Pool.Op.ActionNotification,
|
|
653
|
+
},
|
|
654
|
+
{
|
|
655
|
+
queryId,
|
|
656
|
+
description: "Updating the account",
|
|
657
|
+
from: toAddress(params.pool),
|
|
658
|
+
to: senderAccountAddress,
|
|
659
|
+
opcode: pool_1.Pool.Op.ProcessBorrowActionResult,
|
|
660
|
+
},
|
|
661
|
+
{
|
|
662
|
+
queryId,
|
|
663
|
+
description: "Withdrawing jetton from the pool",
|
|
664
|
+
from: poolJettonWalletAddress,
|
|
665
|
+
to: recipeintJettonWalletAddress,
|
|
666
|
+
opcode: jetton_wallet_1.JettonWallet.Op.InternalTransfer,
|
|
667
|
+
},
|
|
668
|
+
];
|
|
669
|
+
(async () => {
|
|
670
|
+
const matchedTx0 = await this.findAndCallback(toAddress(params.sender), steps[0], "0", callbackFn);
|
|
671
|
+
if (poolData.oracle && oracleParams) {
|
|
672
|
+
const matchedTx1 = await this.findAndCallback(toAddress(params.pool), steps[1], matchedTx0.lt.toString(), callbackFn);
|
|
673
|
+
}
|
|
674
|
+
const matchedTx2 = await this.findAndCallback(toAddress(params.sender), steps[2], matchedTx0.lt.toString(), callbackFn);
|
|
675
|
+
const matchedTx4 = await this.findAndCallback(toAddress(senderAccountAddress), steps[3], matchedTx0.lt.toString(), callbackFn);
|
|
676
|
+
const matchedTx3 = await this.findAndCallback(toAddress(recipeintJettonWalletAddress), steps[4], matchedTx0.lt.toString(), callbackFn);
|
|
677
|
+
})();
|
|
678
|
+
return {
|
|
679
|
+
action: {
|
|
680
|
+
type: "pool-borrow",
|
|
681
|
+
params,
|
|
682
|
+
queryId,
|
|
683
|
+
},
|
|
684
|
+
steps,
|
|
685
|
+
};
|
|
686
|
+
}
|
|
687
|
+
async findBorrowTx(queryId, params, callbackFn) {
|
|
688
|
+
const senderAccountAddress = await this.getAccountAddress(toString(params.sender), toString(params.pool));
|
|
689
|
+
const senderAccount = this.open(account_1.Account.createFromAddress(senderAccountAddress));
|
|
690
|
+
const accountData = await senderAccount.getAccountData();
|
|
691
|
+
const pool = this.open(pool_1.Pool.createFromAddress(toAddress(params.pool)));
|
|
692
|
+
const poolData = (await pool.getPoolData());
|
|
693
|
+
const poolJettonWalletAddress = await this.getJettonWalletAddress(toString(params.pool), toString(params.asset));
|
|
694
|
+
const recipeintJettonWalletAddress = await this.getJettonWalletAddress(toString(params.recipient), toString(params.asset));
|
|
695
|
+
const assets = [
|
|
696
|
+
...new Set([...account_1.Account.haveAssets(accountData), toString(params.asset)]),
|
|
697
|
+
];
|
|
698
|
+
const result = redstone_onchain_oracle_1.RedstoneOnchainOracle.getOracleRequestParams(assets, poolData.oracleConfig);
|
|
699
|
+
const helper = (0, redstoneHelper_1.createRedstoneHelper)("prod");
|
|
700
|
+
const payload = await helper.createPayload(result.redstoneAssetNames);
|
|
701
|
+
const requestAssetCell = redstone_onchain_oracle_1.RedstoneOnchainOracle.createAddressToTupleCell(result.requestAssets);
|
|
702
|
+
const dataFeedIdsTupleBuilder = new ton_1.TupleBuilder();
|
|
703
|
+
result.feedIds.forEach((item) => dataFeedIdsTupleBuilder.writeNumber(item));
|
|
704
|
+
const feedIdCell = (0, ton_1.serializeTuple)(dataFeedIdsTupleBuilder.build());
|
|
705
|
+
const oracleParams = (0, ton_1.beginCell)()
|
|
706
|
+
.storeRef(feedIdCell)
|
|
707
|
+
.storeRef(payload)
|
|
708
|
+
.storeRef(requestAssetCell)
|
|
709
|
+
.endCell();
|
|
710
|
+
const steps = [
|
|
711
|
+
{
|
|
712
|
+
queryId,
|
|
713
|
+
description: "Sending the tx",
|
|
714
|
+
from: toAddress(params.sender),
|
|
715
|
+
to: toAddress(params.pool),
|
|
716
|
+
opcode: pool_1.Pool.Op.Borrow,
|
|
717
|
+
},
|
|
718
|
+
{
|
|
719
|
+
queryId,
|
|
720
|
+
description: "Calculating oracle data",
|
|
721
|
+
from: toAddress(poolData.oracle ? poolData.oracle : params.pool),
|
|
722
|
+
to: toAddress(params.pool),
|
|
723
|
+
},
|
|
724
|
+
{
|
|
725
|
+
queryId,
|
|
726
|
+
description: "Action notification",
|
|
727
|
+
from: toAddress(params.pool),
|
|
728
|
+
to: toAddress(params.sender),
|
|
729
|
+
opcode: pool_1.Pool.Op.ActionNotification,
|
|
730
|
+
},
|
|
731
|
+
{
|
|
732
|
+
queryId,
|
|
733
|
+
description: "Updating the account",
|
|
734
|
+
from: toAddress(params.pool),
|
|
735
|
+
to: senderAccountAddress,
|
|
736
|
+
opcode: pool_1.Pool.Op.ProcessBorrowActionResult,
|
|
737
|
+
},
|
|
738
|
+
{
|
|
739
|
+
queryId,
|
|
740
|
+
description: "Withdrawing jetton from the pool",
|
|
741
|
+
from: poolJettonWalletAddress,
|
|
742
|
+
to: recipeintJettonWalletAddress,
|
|
743
|
+
opcode: jetton_wallet_1.JettonWallet.Op.InternalTransfer,
|
|
744
|
+
},
|
|
745
|
+
];
|
|
746
|
+
(async () => {
|
|
747
|
+
const matchedTx0 = await this.findAndCallback(toAddress(params.sender), steps[0], "0", callbackFn);
|
|
748
|
+
if (poolData.oracle && oracleParams) {
|
|
749
|
+
const matchedTx1 = await this.findAndCallback(toAddress(params.pool), steps[1], matchedTx0.lt.toString(), callbackFn);
|
|
750
|
+
}
|
|
751
|
+
const matchedTx2 = await this.findAndCallback(toAddress(params.sender), steps[2], matchedTx0.lt.toString(), callbackFn);
|
|
752
|
+
const matchedTx4 = await this.findAndCallback(toAddress(senderAccountAddress), steps[3], matchedTx0.lt.toString(), callbackFn);
|
|
753
|
+
const matchedTx3 = await this.findAndCallback(toAddress(recipeintJettonWalletAddress), steps[4], matchedTx0.lt.toString(), callbackFn);
|
|
754
|
+
})();
|
|
755
|
+
return {
|
|
756
|
+
action: {
|
|
757
|
+
type: "pool-borrow",
|
|
758
|
+
params,
|
|
759
|
+
queryId,
|
|
760
|
+
},
|
|
761
|
+
steps,
|
|
762
|
+
};
|
|
763
|
+
}
|
|
764
|
+
async repay(sender, params) {
|
|
765
|
+
const isWTON = toString(params.asset) == this.contracts.WTON;
|
|
766
|
+
const jettonForwardTonAmount = isWTON ? (0, ton_1.toNano)(0.02) : 0n;
|
|
767
|
+
const jettonForwardPayload = isWTON ? jetton_minter_2.WTON_UNWRAP_CELL : null;
|
|
768
|
+
const pool = this.open(pool_1.Pool.createFromAddress(toAddress(params.pool)));
|
|
769
|
+
const poolData = await pool.getPoolData();
|
|
770
|
+
const gas = pool_1.Pool.calculateGasFee(pool_1.Pool.Op.Repay, pool_1.Pool.Gas.baseFee, pool_1.Pool.Gas.forwardFee, jettonForwardTonAmount, !poolData.oracle, false, poolData.minimumGasFee);
|
|
771
|
+
if (toString(params.asset) == this.contracts.WTON) {
|
|
772
|
+
const poolWTONWalletAddress = await this.getJettonWalletAddress(toString(params.pool), toString(params.asset));
|
|
773
|
+
const poolWTONWallet = this.open(jetton_wallet_2.WTONWallet.createFromAddress(poolWTONWalletAddress));
|
|
774
|
+
await poolWTONWallet.sendExternalTransfer(sender, jetton_wallet_2.WTONWallet.Gas.ExternalTransfer, {
|
|
775
|
+
recipient: toAddress(params.pool),
|
|
776
|
+
response: toAddress(params.response),
|
|
777
|
+
amount: params.amount,
|
|
778
|
+
forwardTonAmount: gas,
|
|
779
|
+
forwardPayload: pool_1.Pool.createRepayPayload({
|
|
780
|
+
asset: toAddress(params.asset),
|
|
781
|
+
recipient: toAddress(params.recipient),
|
|
782
|
+
response: toAddress(params.response),
|
|
783
|
+
jettonForwardTonAmount,
|
|
784
|
+
jettonForwardPayload,
|
|
785
|
+
}),
|
|
786
|
+
});
|
|
787
|
+
}
|
|
788
|
+
else {
|
|
789
|
+
const senderJettonWalletAddress = await this.getJettonWalletAddress(toString(params.sender), toString(params.asset));
|
|
790
|
+
const senderJettonWallet = this.open(jetton_wallet_1.JettonWallet.createFromAddress(senderJettonWalletAddress));
|
|
791
|
+
await senderJettonWallet.sendTransfer(sender, gas + jetton_wallet_1.JettonWallet.Gas.Transfer, {
|
|
792
|
+
recipient: toAddress(params.pool),
|
|
793
|
+
response: toAddress(params.response),
|
|
794
|
+
amount: params.amount,
|
|
795
|
+
forwardTonAmount: gas,
|
|
796
|
+
forwardPayload: pool_1.Pool.createRepayPayload({
|
|
797
|
+
asset: toAddress(params.asset),
|
|
798
|
+
recipient: toAddress(params.recipient),
|
|
799
|
+
response: toAddress(params.response),
|
|
800
|
+
jettonForwardTonAmount,
|
|
801
|
+
jettonForwardPayload,
|
|
802
|
+
}),
|
|
803
|
+
});
|
|
804
|
+
}
|
|
805
|
+
}
|
|
806
|
+
async repayAndWaitTx(sender, params, callbackFn) {
|
|
807
|
+
const isWTON = toString(params.asset) == this.contracts.WTON;
|
|
808
|
+
const jettonForwardTonAmount = isWTON ? (0, ton_1.toNano)(0.02) : 0n;
|
|
809
|
+
const jettonForwardPayload = isWTON ? jetton_minter_2.WTON_UNWRAP_CELL : null;
|
|
810
|
+
const senderJettonWalletAddress = await this.getJettonWalletAddress(toString(params.sender), toString(params.asset));
|
|
811
|
+
const senderJettonWallet = this.open(jetton_wallet_1.JettonWallet.createFromAddress(senderJettonWalletAddress));
|
|
812
|
+
const pool = this.open(pool_1.Pool.createFromAddress(toAddress(params.pool)));
|
|
813
|
+
const poolJettonWalletAddress = await this.getJettonWalletAddress(toString(params.pool), toString(params.asset));
|
|
814
|
+
const poolData = await pool.getPoolData();
|
|
815
|
+
const recipientAccountAddress = await this.getAccountAddress(toString(params.recipient), toString(params.pool));
|
|
816
|
+
const gas = pool_1.Pool.calculateGasFee(pool_1.Pool.Op.Repay, pool_1.Pool.Gas.baseFee, pool_1.Pool.Gas.forwardFee, jettonForwardTonAmount, !poolData.oracle, false, poolData.minimumGasFee);
|
|
817
|
+
const opts = {
|
|
818
|
+
recipient: toAddress(params.pool),
|
|
819
|
+
response: toAddress(params.response),
|
|
820
|
+
amount: params.amount,
|
|
821
|
+
forwardTonAmount: gas,
|
|
822
|
+
forwardPayload: pool_1.Pool.createRepayPayload({
|
|
823
|
+
asset: toAddress(params.asset),
|
|
824
|
+
recipient: toAddress(params.recipient),
|
|
825
|
+
response: toAddress(params.response),
|
|
826
|
+
jettonForwardTonAmount,
|
|
827
|
+
jettonForwardPayload,
|
|
828
|
+
}),
|
|
829
|
+
};
|
|
830
|
+
const body = senderJettonWallet.createTransferBody(opts);
|
|
831
|
+
const queryId = (0, query_id_generactor_1.generateQueryId)(toAddress(params.sender), body);
|
|
832
|
+
const steps = [];
|
|
833
|
+
if (toString(params.asset) == this.contracts.WTON) {
|
|
834
|
+
const poolWTONWalletAddress = await this.getJettonWalletAddress(toString(params.pool), toString(params.asset));
|
|
835
|
+
const poolWTONWallet = this.open(jetton_wallet_2.WTONWallet.createFromAddress(poolWTONWalletAddress));
|
|
836
|
+
await poolWTONWallet.sendExternalTransfer(sender, jetton_wallet_2.WTONWallet.Gas.ExternalTransfer, {
|
|
837
|
+
queryId: queryId,
|
|
838
|
+
...opts,
|
|
839
|
+
});
|
|
840
|
+
steps.push(...[
|
|
841
|
+
{
|
|
842
|
+
queryId,
|
|
843
|
+
description: "Sending the tx",
|
|
844
|
+
from: toAddress(params.sender),
|
|
845
|
+
to: poolWTONWalletAddress,
|
|
846
|
+
opcode: jetton_wallet_2.WTONWallet.Op.ExternalTransfer,
|
|
847
|
+
},
|
|
848
|
+
{
|
|
849
|
+
queryId,
|
|
850
|
+
description: "Repaying jetton to the pool",
|
|
851
|
+
from: toAddress(params.sender),
|
|
852
|
+
to: poolWTONWalletAddress,
|
|
853
|
+
opcode: jetton_wallet_2.WTONWallet.Op.ExternalTransfer,
|
|
854
|
+
},
|
|
855
|
+
]);
|
|
856
|
+
}
|
|
857
|
+
else {
|
|
858
|
+
await senderJettonWallet.sendTransfer(sender, gas + jetton_wallet_1.JettonWallet.Gas.Transfer, {
|
|
859
|
+
...opts,
|
|
860
|
+
queryId,
|
|
861
|
+
});
|
|
862
|
+
steps.push(...[
|
|
863
|
+
{
|
|
864
|
+
queryId,
|
|
865
|
+
description: "Sending the tx",
|
|
866
|
+
from: toAddress(params.sender),
|
|
867
|
+
to: senderJettonWalletAddress,
|
|
868
|
+
opcode: jetton_wallet_1.JettonWallet.Op.Transfer,
|
|
869
|
+
},
|
|
870
|
+
{
|
|
871
|
+
queryId,
|
|
872
|
+
description: "Repaying jetton to the pool",
|
|
873
|
+
from: senderJettonWalletAddress,
|
|
874
|
+
to: poolJettonWalletAddress,
|
|
875
|
+
opcode: jetton_wallet_1.JettonWallet.Op.InternalTransfer,
|
|
876
|
+
},
|
|
877
|
+
]);
|
|
878
|
+
}
|
|
879
|
+
steps.push(...[
|
|
880
|
+
{
|
|
881
|
+
queryId,
|
|
882
|
+
description: "Pool notification",
|
|
883
|
+
from: poolJettonWalletAddress,
|
|
884
|
+
to: pool.address,
|
|
885
|
+
opcode: jetton_wallet_1.JettonWallet.Op.TransferNotification,
|
|
886
|
+
},
|
|
887
|
+
{
|
|
888
|
+
queryId,
|
|
889
|
+
description: "Updating the account",
|
|
890
|
+
from: pool.address,
|
|
891
|
+
to: recipientAccountAddress,
|
|
892
|
+
opcode: pool_1.Pool.Op.ExecuteAction,
|
|
893
|
+
},
|
|
894
|
+
{
|
|
895
|
+
queryId,
|
|
896
|
+
description: "Action notification",
|
|
897
|
+
from: pool.address,
|
|
898
|
+
to: toAddress(params.sender),
|
|
899
|
+
opcode: pool_1.Pool.Op.ActionNotification,
|
|
900
|
+
},
|
|
901
|
+
]);
|
|
902
|
+
(async () => {
|
|
903
|
+
const matchedTx0 = await this.findAndCallback(toAddress(params.sender), steps[0], "0", callbackFn);
|
|
904
|
+
const matchedTx1 = await this.findAndCallback(poolJettonWalletAddress, steps[1], matchedTx0.lt.toString(), callbackFn);
|
|
905
|
+
const matchedTx2 = await this.findAndCallback(pool.address, steps[2], matchedTx1.lt.toString(), callbackFn);
|
|
906
|
+
const matchedTx3 = await this.findAndCallback(recipientAccountAddress, steps[3], matchedTx2.lt.toString(), callbackFn);
|
|
907
|
+
const matchedTx4 = await this.findAndCallback(toAddress(params.sender), steps[4], matchedTx3.lt.toString(), callbackFn);
|
|
908
|
+
})();
|
|
909
|
+
return {
|
|
910
|
+
action: {
|
|
911
|
+
type: "pool-repay",
|
|
912
|
+
params,
|
|
913
|
+
queryId,
|
|
914
|
+
},
|
|
915
|
+
steps,
|
|
916
|
+
};
|
|
917
|
+
}
|
|
918
|
+
async findRepayTx(queryId, params, callbackFn) {
|
|
919
|
+
const senderJettonWalletAddress = await this.getJettonWalletAddress(toString(params.sender), toString(params.asset));
|
|
920
|
+
const pool = this.open(pool_1.Pool.createFromAddress(toAddress(params.pool)));
|
|
921
|
+
const poolJettonWalletAddress = await this.getJettonWalletAddress(toString(params.pool), toString(params.asset));
|
|
922
|
+
const recipientAccountAddress = await this.getAccountAddress(toString(params.recipient), toString(params.pool));
|
|
923
|
+
const steps = [];
|
|
924
|
+
if (toString(params.asset) == this.contracts.WTON) {
|
|
925
|
+
const poolWTONWalletAddress = await this.getJettonWalletAddress(toString(params.pool), toString(params.asset));
|
|
926
|
+
steps.push(...[
|
|
927
|
+
{
|
|
928
|
+
queryId,
|
|
929
|
+
description: "Sending the tx",
|
|
930
|
+
from: toAddress(params.sender),
|
|
931
|
+
to: poolWTONWalletAddress,
|
|
932
|
+
opcode: jetton_wallet_2.WTONWallet.Op.ExternalTransfer,
|
|
933
|
+
},
|
|
934
|
+
{
|
|
935
|
+
queryId,
|
|
936
|
+
description: "Repaying jetton to the pool",
|
|
937
|
+
from: toAddress(params.sender),
|
|
938
|
+
to: poolWTONWalletAddress,
|
|
939
|
+
opcode: jetton_wallet_2.WTONWallet.Op.ExternalTransfer,
|
|
940
|
+
},
|
|
941
|
+
]);
|
|
942
|
+
}
|
|
943
|
+
else {
|
|
944
|
+
steps.push(...[
|
|
945
|
+
{
|
|
946
|
+
queryId,
|
|
947
|
+
description: "Sending the tx",
|
|
948
|
+
from: toAddress(params.sender),
|
|
949
|
+
to: senderJettonWalletAddress,
|
|
950
|
+
opcode: jetton_wallet_1.JettonWallet.Op.Transfer,
|
|
951
|
+
},
|
|
952
|
+
{
|
|
953
|
+
queryId,
|
|
954
|
+
description: "Repaying jetton to the pool",
|
|
955
|
+
from: senderJettonWalletAddress,
|
|
956
|
+
to: poolJettonWalletAddress,
|
|
957
|
+
opcode: jetton_wallet_1.JettonWallet.Op.InternalTransfer,
|
|
958
|
+
},
|
|
959
|
+
]);
|
|
960
|
+
}
|
|
961
|
+
steps.push(...[
|
|
962
|
+
{
|
|
963
|
+
queryId,
|
|
964
|
+
description: "Pool notification",
|
|
965
|
+
from: poolJettonWalletAddress,
|
|
966
|
+
to: pool.address,
|
|
967
|
+
opcode: jetton_wallet_1.JettonWallet.Op.TransferNotification,
|
|
968
|
+
},
|
|
969
|
+
{
|
|
970
|
+
queryId,
|
|
971
|
+
description: "Updating the account",
|
|
972
|
+
from: pool.address,
|
|
973
|
+
to: recipientAccountAddress,
|
|
974
|
+
opcode: pool_1.Pool.Op.ExecuteAction,
|
|
975
|
+
},
|
|
976
|
+
{
|
|
977
|
+
queryId,
|
|
978
|
+
description: "Action notification",
|
|
979
|
+
from: pool.address,
|
|
980
|
+
to: toAddress(params.sender),
|
|
981
|
+
opcode: pool_1.Pool.Op.ActionNotification,
|
|
982
|
+
},
|
|
983
|
+
]);
|
|
984
|
+
(async () => {
|
|
985
|
+
const matchedTx0 = await this.findAndCallback(toAddress(params.sender), steps[0], "0", callbackFn);
|
|
986
|
+
const matchedTx1 = await this.findAndCallback(poolJettonWalletAddress, steps[1], matchedTx0.lt.toString(), callbackFn);
|
|
987
|
+
const matchedTx2 = await this.findAndCallback(pool.address, steps[2], matchedTx1.lt.toString(), callbackFn);
|
|
988
|
+
const matchedTx3 = await this.findAndCallback(recipientAccountAddress, steps[3], matchedTx2.lt.toString(), callbackFn);
|
|
989
|
+
const matchedTx4 = await this.findAndCallback(toAddress(params.sender), steps[4], matchedTx3.lt.toString(), callbackFn);
|
|
990
|
+
})();
|
|
991
|
+
return {
|
|
992
|
+
action: {
|
|
993
|
+
type: "pool-repay",
|
|
994
|
+
params,
|
|
995
|
+
queryId,
|
|
996
|
+
},
|
|
997
|
+
steps,
|
|
998
|
+
};
|
|
999
|
+
}
|
|
1000
|
+
async liquidateSimulate(params) {
|
|
1001
|
+
const isWTON = toString(params.repayAsset) == this.contracts.WTON;
|
|
1002
|
+
const jettonForwardTonAmount = isWTON ? (0, ton_1.toNano)(0.02) : 0n;
|
|
1003
|
+
const jettonForwardPayload = isWTON ? jetton_minter_2.WTON_UNWRAP_CELL : null;
|
|
1004
|
+
const borrowerAccountAddress = await this.getAccountAddress(toString(params.borrower), toString(params.pool));
|
|
1005
|
+
const borrowerAccount = this.open(account_1.Account.createFromAddress(toAddress(borrowerAccountAddress)));
|
|
1006
|
+
const accountData = await borrowerAccount.getAccountData();
|
|
1007
|
+
const assets = [...new Set([...account_1.Account.haveAssets(accountData)])];
|
|
1008
|
+
const pool = this.open(pool_1.Pool.createFromAddress(toAddress(params.pool)));
|
|
1009
|
+
const poolData = (await pool.getPoolData());
|
|
1010
|
+
const result = redstone_onchain_oracle_1.RedstoneOnchainOracle.getOracleRequestParams(assets, poolData.oracleConfig);
|
|
1011
|
+
const helper = (0, redstoneHelper_1.createRedstoneHelper)("prod");
|
|
1012
|
+
const payload = await helper.createPayload(result.redstoneAssetNames);
|
|
1013
|
+
const requestAssetCell = redstone_onchain_oracle_1.RedstoneOnchainOracle.createAddressToTupleCell(result.requestAssets);
|
|
1014
|
+
const dataFeedIdsTupleBuilder = new ton_1.TupleBuilder();
|
|
1015
|
+
result.feedIds.forEach((item) => dataFeedIdsTupleBuilder.writeNumber(item));
|
|
1016
|
+
const feedIdCell = (0, ton_1.serializeTuple)(dataFeedIdsTupleBuilder.build());
|
|
1017
|
+
const oracleParams = (0, ton_1.beginCell)()
|
|
1018
|
+
.storeRef(feedIdCell)
|
|
1019
|
+
.storeRef(payload)
|
|
1020
|
+
.storeRef(requestAssetCell)
|
|
1021
|
+
.endCell();
|
|
1022
|
+
const gas = pool_1.Pool.calculateGasFee(pool_1.Pool.Op.Liquidate, pool_1.Pool.Gas.baseFee, pool_1.Pool.Gas.forwardFee, jettonForwardTonAmount, !poolData.oracle, false, poolData.minimumGasFee);
|
|
1023
|
+
if (toString(params.repayAsset) == this.contracts.WTON) {
|
|
1024
|
+
const poolWTONWalletAddress = await this.getJettonWalletAddress(toString(params.pool), toString(params.repayAsset));
|
|
1025
|
+
const poolWTONWallet = this.open(jetton_wallet_2.WTONWallet.createFromAddress(poolWTONWalletAddress));
|
|
1026
|
+
// await poolWTONWallet.sendExternalTransfer(sender, WTONWallet.Gas.ExternalTransfer, {
|
|
1027
|
+
// recipient: toAddress(params.pool),
|
|
1028
|
+
// response: toAddress(params.response),
|
|
1029
|
+
// amount: params.amount,
|
|
1030
|
+
// forwardTonAmount: gas,
|
|
1031
|
+
// forwardPayload: Pool.createLiquidatePayload({
|
|
1032
|
+
// repayAsset: toAddress(params.repayAsset),
|
|
1033
|
+
// seizeAsset: toAddress(params.seizeAsset),
|
|
1034
|
+
// borrower: toAddress(params.borrower),
|
|
1035
|
+
// response: toAddress(params.response),
|
|
1036
|
+
// jettonForwardTonAmount,
|
|
1037
|
+
// jettonForwardPayload,
|
|
1038
|
+
// oracleParams,
|
|
1039
|
+
// }),
|
|
1040
|
+
// });
|
|
1041
|
+
}
|
|
1042
|
+
else {
|
|
1043
|
+
const senderJettonWalletAddress = await this.getJettonWalletAddress(toString(params.sender), toString(params.repayAsset));
|
|
1044
|
+
const senderJettonWallet = this.open(jetton_wallet_1.JettonWallet.createFromAddress(senderJettonWalletAddress));
|
|
1045
|
+
// await senderJettonWallet.sendTransfer(sender, gas + JettonWallet.Gas.Transfer, {
|
|
1046
|
+
// recipient: toAddress(params.pool),
|
|
1047
|
+
// response: toAddress(params.response),
|
|
1048
|
+
// amount: params.amount,
|
|
1049
|
+
// forwardTonAmount: gas,
|
|
1050
|
+
// forwardPayload: Pool.createLiquidatePayload({
|
|
1051
|
+
// repayAsset: toAddress(params.repayAsset),
|
|
1052
|
+
// seizeAsset: toAddress(params.seizeAsset),
|
|
1053
|
+
// borrower: toAddress(params.borrower),
|
|
1054
|
+
// response: toAddress(params.response),
|
|
1055
|
+
// jettonForwardTonAmount,
|
|
1056
|
+
// jettonForwardPayload,
|
|
1057
|
+
// oracleParams,
|
|
1058
|
+
// }),
|
|
1059
|
+
// });
|
|
1060
|
+
}
|
|
1061
|
+
}
|
|
1062
|
+
async liquidateAndWaitTxSimulate(queryId, params, callbackFn) {
|
|
1063
|
+
const isWTON = toString(params.repayAsset) == this.contracts.WTON;
|
|
1064
|
+
const jettonForwardTonAmount = isWTON ? (0, ton_1.toNano)(0.02) : 0n;
|
|
1065
|
+
const jettonForwardPayload = isWTON ? jetton_minter_2.WTON_UNWRAP_CELL : null;
|
|
1066
|
+
const senderJettonWalletAddress = await this.getJettonWalletAddress(toString(params.sender), toString(params.repayAsset));
|
|
1067
|
+
const senderJettonWallet = this.open(jetton_wallet_1.JettonWallet.createFromAddress(senderJettonWalletAddress));
|
|
1068
|
+
const pool = this.open(pool_1.Pool.createFromAddress(toAddress(params.pool)));
|
|
1069
|
+
const poolJettonWalletAddress = await this.getJettonWalletAddress(toString(params.pool), toString(params.repayAsset));
|
|
1070
|
+
const senderAccountAddress = await this.getAccountAddress(toString(params.sender), toString(params.pool));
|
|
1071
|
+
const borrowerAccountAddress = await this.getAccountAddress(toString(params.borrower), toString(params.pool));
|
|
1072
|
+
const borrowerAccount = this.open(account_1.Account.createFromAddress(toAddress(borrowerAccountAddress)));
|
|
1073
|
+
const accountData = await borrowerAccount.getAccountData();
|
|
1074
|
+
const assets = [...new Set([...account_1.Account.haveAssets(accountData)])];
|
|
1075
|
+
const poolData = (await pool.getPoolData());
|
|
1076
|
+
const result = redstone_onchain_oracle_1.RedstoneOnchainOracle.getOracleRequestParams(assets, poolData.oracleConfig);
|
|
1077
|
+
const helper = (0, redstoneHelper_1.createRedstoneHelper)("prod");
|
|
1078
|
+
const payload = await helper.createPayload(result.redstoneAssetNames);
|
|
1079
|
+
const requestAssetCell = redstone_onchain_oracle_1.RedstoneOnchainOracle.createAddressToTupleCell(result.requestAssets);
|
|
1080
|
+
const dataFeedIdsTupleBuilder = new ton_1.TupleBuilder();
|
|
1081
|
+
result.feedIds.forEach((item) => dataFeedIdsTupleBuilder.writeNumber(item));
|
|
1082
|
+
const feedIdCell = (0, ton_1.serializeTuple)(dataFeedIdsTupleBuilder.build());
|
|
1083
|
+
const oracleParams = (0, ton_1.beginCell)()
|
|
1084
|
+
.storeRef(feedIdCell)
|
|
1085
|
+
.storeRef(payload)
|
|
1086
|
+
.storeRef(requestAssetCell)
|
|
1087
|
+
.endCell();
|
|
1088
|
+
const gas = pool_1.Pool.calculateGasFee(pool_1.Pool.Op.Liquidate, pool_1.Pool.Gas.baseFee, pool_1.Pool.Gas.forwardFee, jettonForwardTonAmount, !poolData.oracle, false, poolData.minimumGasFee);
|
|
1089
|
+
const opts = {
|
|
1090
|
+
recipient: toAddress(params.pool),
|
|
1091
|
+
response: toAddress(params.response),
|
|
1092
|
+
amount: params.amount,
|
|
1093
|
+
forwardTonAmount: gas,
|
|
1094
|
+
forwardPayload: pool_1.Pool.createLiquidatePayload({
|
|
1095
|
+
repayAsset: toAddress(params.repayAsset),
|
|
1096
|
+
seizeAsset: toAddress(params.seizeAsset),
|
|
1097
|
+
borrower: toAddress(params.borrower),
|
|
1098
|
+
response: toAddress(params.response),
|
|
1099
|
+
jettonForwardTonAmount,
|
|
1100
|
+
jettonForwardPayload,
|
|
1101
|
+
oracleParams,
|
|
1102
|
+
}),
|
|
1103
|
+
};
|
|
1104
|
+
const body = senderJettonWallet.createTransferBody(opts);
|
|
1105
|
+
const steps = [];
|
|
1106
|
+
if (toString(params.repayAsset) == this.contracts.WTON) {
|
|
1107
|
+
const poolWTONWalletAddress = await this.getJettonWalletAddress(toString(params.pool), toString(params.repayAsset));
|
|
1108
|
+
const poolWTONWallet = this.open(jetton_wallet_2.WTONWallet.createFromAddress(poolWTONWalletAddress));
|
|
1109
|
+
// await poolWTONWallet.sendExternalTransfer(sender, WTONWallet.Gas.ExternalTransfer, {
|
|
1110
|
+
// queryId: queryId,
|
|
1111
|
+
// ...opts,
|
|
1112
|
+
// });
|
|
1113
|
+
steps.push(...[
|
|
1114
|
+
{
|
|
1115
|
+
queryId,
|
|
1116
|
+
description: "tx send",
|
|
1117
|
+
from: toAddress(params.sender),
|
|
1118
|
+
to: poolWTONWalletAddress,
|
|
1119
|
+
opcode: jetton_wallet_2.WTONWallet.Op.ExternalTransfer,
|
|
1120
|
+
},
|
|
1121
|
+
{
|
|
1122
|
+
queryId,
|
|
1123
|
+
description: "send wton transfer to pool",
|
|
1124
|
+
from: toAddress(params.sender),
|
|
1125
|
+
to: poolWTONWalletAddress,
|
|
1126
|
+
opcode: jetton_wallet_2.WTONWallet.Op.ExternalTransfer,
|
|
1127
|
+
},
|
|
1128
|
+
]);
|
|
1129
|
+
}
|
|
1130
|
+
else {
|
|
1131
|
+
// await senderJettonWallet.sendTransfer(sender, gas + JettonWallet.Gas.Transfer, {
|
|
1132
|
+
// ...opts,
|
|
1133
|
+
// queryId,
|
|
1134
|
+
// });
|
|
1135
|
+
steps.push(...[
|
|
1136
|
+
{
|
|
1137
|
+
queryId,
|
|
1138
|
+
description: "tx send",
|
|
1139
|
+
from: toAddress(params.sender),
|
|
1140
|
+
to: senderJettonWalletAddress,
|
|
1141
|
+
opcode: jetton_wallet_1.JettonWallet.Op.Transfer,
|
|
1142
|
+
},
|
|
1143
|
+
{
|
|
1144
|
+
queryId,
|
|
1145
|
+
description: "send jetton transfer to pool",
|
|
1146
|
+
from: senderJettonWalletAddress,
|
|
1147
|
+
to: poolJettonWalletAddress,
|
|
1148
|
+
opcode: jetton_wallet_1.JettonWallet.Op.InternalTransfer,
|
|
1149
|
+
},
|
|
1150
|
+
]);
|
|
1151
|
+
}
|
|
1152
|
+
steps.push(...[
|
|
1153
|
+
{
|
|
1154
|
+
queryId,
|
|
1155
|
+
description: "pool notification",
|
|
1156
|
+
from: poolJettonWalletAddress,
|
|
1157
|
+
to: pool.address,
|
|
1158
|
+
opcode: jetton_wallet_1.JettonWallet.Op.TransferNotification,
|
|
1159
|
+
},
|
|
1160
|
+
{
|
|
1161
|
+
queryId,
|
|
1162
|
+
description: "unlock & update account",
|
|
1163
|
+
from: pool.address,
|
|
1164
|
+
to: borrowerAccountAddress,
|
|
1165
|
+
opcode: pool_1.Pool.Op.ExecuteAction,
|
|
1166
|
+
},
|
|
1167
|
+
{
|
|
1168
|
+
queryId,
|
|
1169
|
+
description: "send seize collateral token",
|
|
1170
|
+
from: pool.address,
|
|
1171
|
+
to: senderAccountAddress,
|
|
1172
|
+
},
|
|
1173
|
+
{
|
|
1174
|
+
queryId,
|
|
1175
|
+
description: "action notification",
|
|
1176
|
+
from: pool.address,
|
|
1177
|
+
to: toAddress(params.sender),
|
|
1178
|
+
},
|
|
1179
|
+
]);
|
|
1180
|
+
(async () => {
|
|
1181
|
+
const matchedTx0 = await this.findAndCallback(toAddress(params.sender), steps[0], "0", callbackFn);
|
|
1182
|
+
const matchedTx1 = await this.findAndCallback(poolJettonWalletAddress, steps[1], matchedTx0.lt.toString(), callbackFn);
|
|
1183
|
+
const matchedTx2 = await this.findAndCallback(pool.address, steps[2], matchedTx1.lt.toString(), callbackFn);
|
|
1184
|
+
const matchedTx3 = await this.findAndCallback(borrowerAccountAddress, steps[3], matchedTx2.lt.toString(), callbackFn);
|
|
1185
|
+
const matchedTx4 = await this.findAndCallback(senderAccountAddress, steps[4], matchedTx3.lt.toString(), callbackFn);
|
|
1186
|
+
const matchedTx5 = await this.findAndCallback(toAddress(params.sender), steps[5], matchedTx4.lt.toString(), callbackFn);
|
|
1187
|
+
})();
|
|
1188
|
+
return steps;
|
|
1189
|
+
}
|
|
1190
|
+
async liquidate(sender, params) {
|
|
1191
|
+
const isWTON = toString(params.repayAsset) == this.contracts.WTON;
|
|
1192
|
+
const jettonForwardTonAmount = isWTON ? (0, ton_1.toNano)(0.02) : 0n;
|
|
1193
|
+
const jettonForwardPayload = isWTON ? jetton_minter_2.WTON_UNWRAP_CELL : null;
|
|
1194
|
+
const borrowerAccountAddress = await this.getAccountAddress(toString(params.borrower), toString(params.pool));
|
|
1195
|
+
const borrowerAccount = this.open(account_1.Account.createFromAddress(toAddress(borrowerAccountAddress)));
|
|
1196
|
+
const accountData = await borrowerAccount.getAccountData();
|
|
1197
|
+
const assets = [...new Set([...account_1.Account.haveAssets(accountData)])];
|
|
1198
|
+
const pool = this.open(pool_1.Pool.createFromAddress(toAddress(params.pool)));
|
|
1199
|
+
const poolData = (await pool.getPoolData());
|
|
1200
|
+
const result = redstone_onchain_oracle_1.RedstoneOnchainOracle.getOracleRequestParams(assets, poolData.oracleConfig);
|
|
1201
|
+
const helper = (0, redstoneHelper_1.createRedstoneHelper)("prod");
|
|
1202
|
+
const payload = await helper.createPayload(result.redstoneAssetNames);
|
|
1203
|
+
const requestAssetCell = redstone_onchain_oracle_1.RedstoneOnchainOracle.createAddressToTupleCell(result.requestAssets);
|
|
1204
|
+
const dataFeedIdsTupleBuilder = new ton_1.TupleBuilder();
|
|
1205
|
+
result.feedIds.forEach((item) => dataFeedIdsTupleBuilder.writeNumber(item));
|
|
1206
|
+
const feedIdCell = (0, ton_1.serializeTuple)(dataFeedIdsTupleBuilder.build());
|
|
1207
|
+
const oracleParams = (0, ton_1.beginCell)()
|
|
1208
|
+
.storeRef(feedIdCell)
|
|
1209
|
+
.storeRef(payload)
|
|
1210
|
+
.storeRef(requestAssetCell)
|
|
1211
|
+
.endCell();
|
|
1212
|
+
const gas = pool_1.Pool.calculateGasFee(pool_1.Pool.Op.Liquidate, pool_1.Pool.Gas.baseFee, pool_1.Pool.Gas.forwardFee, jettonForwardTonAmount, !poolData.oracle, false, poolData.minimumGasFee);
|
|
1213
|
+
if (toString(params.repayAsset) == this.contracts.WTON) {
|
|
1214
|
+
const poolWTONWalletAddress = await this.getJettonWalletAddress(toString(params.pool), toString(params.repayAsset));
|
|
1215
|
+
const poolWTONWallet = this.open(jetton_wallet_2.WTONWallet.createFromAddress(poolWTONWalletAddress));
|
|
1216
|
+
await poolWTONWallet.sendExternalTransfer(sender, jetton_wallet_2.WTONWallet.Gas.ExternalTransfer, {
|
|
1217
|
+
recipient: toAddress(params.pool),
|
|
1218
|
+
response: toAddress(params.response),
|
|
1219
|
+
amount: params.amount,
|
|
1220
|
+
forwardTonAmount: gas,
|
|
1221
|
+
forwardPayload: pool_1.Pool.createLiquidatePayload({
|
|
1222
|
+
repayAsset: toAddress(params.repayAsset),
|
|
1223
|
+
seizeAsset: toAddress(params.seizeAsset),
|
|
1224
|
+
borrower: toAddress(params.borrower),
|
|
1225
|
+
response: toAddress(params.response),
|
|
1226
|
+
jettonForwardTonAmount,
|
|
1227
|
+
jettonForwardPayload,
|
|
1228
|
+
oracleParams,
|
|
1229
|
+
}),
|
|
1230
|
+
});
|
|
1231
|
+
}
|
|
1232
|
+
else {
|
|
1233
|
+
const senderJettonWalletAddress = await this.getJettonWalletAddress(toString(params.sender), toString(params.repayAsset));
|
|
1234
|
+
const senderJettonWallet = this.open(jetton_wallet_1.JettonWallet.createFromAddress(senderJettonWalletAddress));
|
|
1235
|
+
await senderJettonWallet.sendTransfer(sender, gas + jetton_wallet_1.JettonWallet.Gas.Transfer, {
|
|
1236
|
+
recipient: toAddress(params.pool),
|
|
1237
|
+
response: toAddress(params.response),
|
|
1238
|
+
amount: params.amount,
|
|
1239
|
+
forwardTonAmount: gas,
|
|
1240
|
+
forwardPayload: pool_1.Pool.createLiquidatePayload({
|
|
1241
|
+
repayAsset: toAddress(params.repayAsset),
|
|
1242
|
+
seizeAsset: toAddress(params.seizeAsset),
|
|
1243
|
+
borrower: toAddress(params.borrower),
|
|
1244
|
+
response: toAddress(params.response),
|
|
1245
|
+
jettonForwardTonAmount,
|
|
1246
|
+
jettonForwardPayload,
|
|
1247
|
+
oracleParams,
|
|
1248
|
+
}),
|
|
1249
|
+
});
|
|
1250
|
+
}
|
|
1251
|
+
}
|
|
1252
|
+
async liquidateAndWaitTx(sender, params, callbackFn) {
|
|
1253
|
+
const isWTON = toString(params.repayAsset) == this.contracts.WTON;
|
|
1254
|
+
const jettonForwardTonAmount = isWTON ? (0, ton_1.toNano)(0.02) : 0n;
|
|
1255
|
+
const jettonForwardPayload = isWTON ? jetton_minter_2.WTON_UNWRAP_CELL : null;
|
|
1256
|
+
const senderJettonWalletAddress = await this.getJettonWalletAddress(toString(params.sender), toString(params.repayAsset));
|
|
1257
|
+
const senderJettonWallet = this.open(jetton_wallet_1.JettonWallet.createFromAddress(senderJettonWalletAddress));
|
|
1258
|
+
const pool = this.open(pool_1.Pool.createFromAddress(toAddress(params.pool)));
|
|
1259
|
+
const poolJettonWalletAddress = await this.getJettonWalletAddress(toString(params.pool), toString(params.repayAsset));
|
|
1260
|
+
const senderAccountAddress = await this.getAccountAddress(toString(params.sender), toString(params.pool));
|
|
1261
|
+
const borrowerAccountAddress = await this.getAccountAddress(toString(params.borrower), toString(params.pool));
|
|
1262
|
+
const borrowerAccount = this.open(account_1.Account.createFromAddress(toAddress(borrowerAccountAddress)));
|
|
1263
|
+
const accountData = await borrowerAccount.getAccountData();
|
|
1264
|
+
const assets = [...new Set([...account_1.Account.haveAssets(accountData)])];
|
|
1265
|
+
const poolData = (await pool.getPoolData());
|
|
1266
|
+
const result = redstone_onchain_oracle_1.RedstoneOnchainOracle.getOracleRequestParams(assets, poolData.oracleConfig);
|
|
1267
|
+
const helper = (0, redstoneHelper_1.createRedstoneHelper)("prod");
|
|
1268
|
+
const payload = await helper.createPayload(result.redstoneAssetNames);
|
|
1269
|
+
const requestAssetCell = redstone_onchain_oracle_1.RedstoneOnchainOracle.createAddressToTupleCell(result.requestAssets);
|
|
1270
|
+
const dataFeedIdsTupleBuilder = new ton_1.TupleBuilder();
|
|
1271
|
+
result.feedIds.forEach((item) => dataFeedIdsTupleBuilder.writeNumber(item));
|
|
1272
|
+
const feedIdCell = (0, ton_1.serializeTuple)(dataFeedIdsTupleBuilder.build());
|
|
1273
|
+
const oracleParams = (0, ton_1.beginCell)()
|
|
1274
|
+
.storeRef(feedIdCell)
|
|
1275
|
+
.storeRef(payload)
|
|
1276
|
+
.storeRef(requestAssetCell)
|
|
1277
|
+
.endCell();
|
|
1278
|
+
const gas = pool_1.Pool.calculateGasFee(pool_1.Pool.Op.Liquidate, pool_1.Pool.Gas.baseFee, pool_1.Pool.Gas.forwardFee, jettonForwardTonAmount, !poolData.oracle, false, poolData.minimumGasFee);
|
|
1279
|
+
const opts = {
|
|
1280
|
+
recipient: toAddress(params.pool),
|
|
1281
|
+
response: toAddress(params.response),
|
|
1282
|
+
amount: params.amount,
|
|
1283
|
+
forwardTonAmount: gas,
|
|
1284
|
+
forwardPayload: pool_1.Pool.createLiquidatePayload({
|
|
1285
|
+
repayAsset: toAddress(params.repayAsset),
|
|
1286
|
+
seizeAsset: toAddress(params.seizeAsset),
|
|
1287
|
+
borrower: toAddress(params.borrower),
|
|
1288
|
+
response: toAddress(params.response),
|
|
1289
|
+
jettonForwardTonAmount,
|
|
1290
|
+
jettonForwardPayload,
|
|
1291
|
+
oracleParams,
|
|
1292
|
+
}),
|
|
1293
|
+
};
|
|
1294
|
+
const body = senderJettonWallet.createTransferBody(opts);
|
|
1295
|
+
const queryId = (0, query_id_generactor_1.generateQueryId)(toAddress(params.sender), body);
|
|
1296
|
+
const steps = [];
|
|
1297
|
+
if (toString(params.repayAsset) == this.contracts.WTON) {
|
|
1298
|
+
const poolWTONWalletAddress = await this.getJettonWalletAddress(toString(params.pool), toString(params.repayAsset));
|
|
1299
|
+
const poolWTONWallet = this.open(jetton_wallet_2.WTONWallet.createFromAddress(poolWTONWalletAddress));
|
|
1300
|
+
await poolWTONWallet.sendExternalTransfer(sender, jetton_wallet_2.WTONWallet.Gas.ExternalTransfer, {
|
|
1301
|
+
queryId: queryId,
|
|
1302
|
+
...opts,
|
|
1303
|
+
});
|
|
1304
|
+
steps.push(...[
|
|
1305
|
+
{
|
|
1306
|
+
queryId,
|
|
1307
|
+
description: "tx send",
|
|
1308
|
+
from: toAddress(params.sender),
|
|
1309
|
+
to: poolWTONWalletAddress,
|
|
1310
|
+
opcode: jetton_wallet_2.WTONWallet.Op.ExternalTransfer,
|
|
1311
|
+
},
|
|
1312
|
+
{
|
|
1313
|
+
queryId,
|
|
1314
|
+
description: "send wton transfer to pool",
|
|
1315
|
+
from: toAddress(params.sender),
|
|
1316
|
+
to: poolWTONWalletAddress,
|
|
1317
|
+
opcode: jetton_wallet_2.WTONWallet.Op.ExternalTransfer,
|
|
1318
|
+
},
|
|
1319
|
+
]);
|
|
1320
|
+
}
|
|
1321
|
+
else {
|
|
1322
|
+
await senderJettonWallet.sendTransfer(sender, gas + jetton_wallet_1.JettonWallet.Gas.Transfer, {
|
|
1323
|
+
...opts,
|
|
1324
|
+
queryId,
|
|
1325
|
+
});
|
|
1326
|
+
steps.push(...[
|
|
1327
|
+
{
|
|
1328
|
+
queryId,
|
|
1329
|
+
description: "tx send",
|
|
1330
|
+
from: toAddress(params.sender),
|
|
1331
|
+
to: senderJettonWalletAddress,
|
|
1332
|
+
opcode: jetton_wallet_1.JettonWallet.Op.Transfer,
|
|
1333
|
+
},
|
|
1334
|
+
{
|
|
1335
|
+
queryId,
|
|
1336
|
+
description: "send jetton transfer to pool",
|
|
1337
|
+
from: senderJettonWalletAddress,
|
|
1338
|
+
to: poolJettonWalletAddress,
|
|
1339
|
+
opcode: jetton_wallet_1.JettonWallet.Op.InternalTransfer,
|
|
1340
|
+
},
|
|
1341
|
+
]);
|
|
1342
|
+
}
|
|
1343
|
+
steps.push(...[
|
|
1344
|
+
{
|
|
1345
|
+
queryId,
|
|
1346
|
+
description: "pool notification",
|
|
1347
|
+
from: poolJettonWalletAddress,
|
|
1348
|
+
to: pool.address,
|
|
1349
|
+
opcode: jetton_wallet_1.JettonWallet.Op.TransferNotification,
|
|
1350
|
+
},
|
|
1351
|
+
{
|
|
1352
|
+
queryId,
|
|
1353
|
+
description: "unlock & update account",
|
|
1354
|
+
from: pool.address,
|
|
1355
|
+
to: borrowerAccountAddress,
|
|
1356
|
+
opcode: pool_1.Pool.Op.ExecuteAction,
|
|
1357
|
+
},
|
|
1358
|
+
{
|
|
1359
|
+
queryId,
|
|
1360
|
+
description: "send seize collateral token",
|
|
1361
|
+
from: pool.address,
|
|
1362
|
+
to: senderAccountAddress,
|
|
1363
|
+
},
|
|
1364
|
+
{
|
|
1365
|
+
queryId,
|
|
1366
|
+
description: "action notification",
|
|
1367
|
+
from: pool.address,
|
|
1368
|
+
to: toAddress(params.sender),
|
|
1369
|
+
},
|
|
1370
|
+
]);
|
|
1371
|
+
(async () => {
|
|
1372
|
+
const matchedTx0 = await this.findAndCallback(toAddress(params.sender), steps[0], "0", callbackFn);
|
|
1373
|
+
const matchedTx1 = await this.findAndCallback(poolJettonWalletAddress, steps[1], matchedTx0.lt.toString(), callbackFn);
|
|
1374
|
+
const matchedTx2 = await this.findAndCallback(pool.address, steps[2], matchedTx1.lt.toString(), callbackFn);
|
|
1375
|
+
const matchedTx3 = await this.findAndCallback(borrowerAccountAddress, steps[3], matchedTx2.lt.toString(), callbackFn);
|
|
1376
|
+
const matchedTx4 = await this.findAndCallback(senderAccountAddress, steps[4], matchedTx3.lt.toString(), callbackFn);
|
|
1377
|
+
const matchedTx5 = await this.findAndCallback(toAddress(params.sender), steps[5], matchedTx4.lt.toString(), callbackFn);
|
|
1378
|
+
})();
|
|
1379
|
+
return {
|
|
1380
|
+
action: {
|
|
1381
|
+
type: "pool-liquidate",
|
|
1382
|
+
params,
|
|
1383
|
+
queryId,
|
|
1384
|
+
},
|
|
1385
|
+
steps,
|
|
1386
|
+
};
|
|
1387
|
+
}
|
|
1388
|
+
async updateOnchainData(onchainDataInfo) {
|
|
1389
|
+
for (let [asset, data] of Object.entries(onchainDataInfo)) {
|
|
1390
|
+
if (data.sourceType === redstone_onchain_oracle_1.RedstoneOnchainOracle.SourceType.tonstakers &&
|
|
1391
|
+
"underlyingAddress" in data) {
|
|
1392
|
+
const result = await this.client
|
|
1393
|
+
.provider(ton_1.Address.parse(data.sourceAddress.toString()))
|
|
1394
|
+
.get("get_pool_state", []);
|
|
1395
|
+
result.stack.readBigNumber();
|
|
1396
|
+
data.underlyingReserve = result.stack.readBigNumber();
|
|
1397
|
+
data.totalSupply = result.stack.readBigNumber();
|
|
1398
|
+
}
|
|
1399
|
+
else if (data.sourceType === redstone_onchain_oracle_1.RedstoneOnchainOracle.SourceType.dedust &&
|
|
1400
|
+
data.assetType === type_1.AssetType.MultiAsset) {
|
|
1401
|
+
if (this.network === "mainnet") {
|
|
1402
|
+
const results = await Promise.all([
|
|
1403
|
+
this.client
|
|
1404
|
+
.provider(ton_1.Address.parse(data.sourceAddress.toString()))
|
|
1405
|
+
.get("get_reserves", []),
|
|
1406
|
+
this.client
|
|
1407
|
+
.provider(ton_1.Address.parse(data.sourceAddress.toString()))
|
|
1408
|
+
.get("get_jetton_data", []),
|
|
1409
|
+
]);
|
|
1410
|
+
data.underlying0Reserve = results[0].stack.readBigNumber();
|
|
1411
|
+
data.underlying1Reserve = results[0].stack.readBigNumber();
|
|
1412
|
+
data.totalSupply = results[1].stack.readBigNumber();
|
|
1413
|
+
}
|
|
1414
|
+
else {
|
|
1415
|
+
const result = await this.client
|
|
1416
|
+
.provider(ton_1.Address.parse(data.sourceAddress.toString()))
|
|
1417
|
+
.get("get_pool_state", []);
|
|
1418
|
+
result.stack.readBigNumber();
|
|
1419
|
+
data.underlying0Reserve = result.stack.readBigNumber();
|
|
1420
|
+
data.underlying1Reserve = result.stack.readBigNumber();
|
|
1421
|
+
data.totalSupply = result.stack.readBigNumber();
|
|
1422
|
+
}
|
|
1423
|
+
}
|
|
1424
|
+
else if (data.sourceType === redstone_onchain_oracle_1.RedstoneOnchainOracle.SourceType.stonfi &&
|
|
1425
|
+
data.assetType === type_1.AssetType.MultiAsset) {
|
|
1426
|
+
if (this.network === "mainnet") {
|
|
1427
|
+
const result = await this.client
|
|
1428
|
+
.provider(ton_1.Address.parse(data.sourceAddress.toString()))
|
|
1429
|
+
.get("get_pool_data", []);
|
|
1430
|
+
result.stack.readNumber();
|
|
1431
|
+
result.stack.readAddress();
|
|
1432
|
+
data.totalSupply = result.stack.readBigNumber();
|
|
1433
|
+
data.underlying0Reserve = result.stack.readBigNumber();
|
|
1434
|
+
data.underlying1Reserve = result.stack.readBigNumber();
|
|
1435
|
+
}
|
|
1436
|
+
else {
|
|
1437
|
+
const result = await this.client
|
|
1438
|
+
.provider(ton_1.Address.parse(data.sourceAddress.toString()))
|
|
1439
|
+
.get("get_pool_state", []);
|
|
1440
|
+
result.stack.readBigNumber();
|
|
1441
|
+
data.underlying0Reserve = result.stack.readBigNumber();
|
|
1442
|
+
data.underlying1Reserve = result.stack.readBigNumber();
|
|
1443
|
+
data.totalSupply = result.stack.readBigNumber();
|
|
1444
|
+
}
|
|
1445
|
+
}
|
|
1446
|
+
else if (data.sourceType === redstone_onchain_oracle_1.RedstoneOnchainOracle.SourceType.storm &&
|
|
1447
|
+
data.assetType === type_1.AssetType.SingleAsset) {
|
|
1448
|
+
if (this.network === "mainnet") {
|
|
1449
|
+
const vault = this.open(StormVault.createFromAddress(data.sourceAddress));
|
|
1450
|
+
const { lpRate } = await vault.getVaultRate();
|
|
1451
|
+
data.underlyingReserve = lpRate;
|
|
1452
|
+
if (!data.totalSupply) {
|
|
1453
|
+
data.totalSupply = BigInt(1e9);
|
|
1454
|
+
}
|
|
1455
|
+
}
|
|
1456
|
+
else {
|
|
1457
|
+
}
|
|
1458
|
+
}
|
|
1459
|
+
else if (data.sourceType === redstone_onchain_oracle_1.RedstoneOnchainOracle.SourceType.tradoor &&
|
|
1460
|
+
data.assetType === type_1.AssetType.SingleAsset) {
|
|
1461
|
+
if (this.network === "mainnet") {
|
|
1462
|
+
const results = await this.client
|
|
1463
|
+
.provider(ton_1.Address.parse(data.sourceAddress.toString()))
|
|
1464
|
+
.get("tlpPrice", []);
|
|
1465
|
+
const lpRate = results.stack.readBigNumber();
|
|
1466
|
+
data.underlyingReserve = lpRate;
|
|
1467
|
+
if (!data.totalSupply) {
|
|
1468
|
+
data.totalSupply = BigInt(18e9);
|
|
1469
|
+
}
|
|
1470
|
+
}
|
|
1471
|
+
}
|
|
1472
|
+
}
|
|
1473
|
+
return onchainDataInfo;
|
|
1474
|
+
}
|
|
1475
|
+
async getRedstonePrices(assets, poolData) {
|
|
1476
|
+
const result = redstone_onchain_oracle_1.RedstoneOnchainOracle.getOracleRequestParams(assets, poolData.oracleConfig);
|
|
1477
|
+
const redstonePrices = await readonlyCachedRedstone_1.ReadonlyCachedRedstone.getPrices(result.redstoneAssetNames);
|
|
1478
|
+
const prices = {};
|
|
1479
|
+
const onchainData = {};
|
|
1480
|
+
redstonePrices.forEach((item) => {
|
|
1481
|
+
try {
|
|
1482
|
+
const rainfo = poolData.oracleConfig.redstoneAssetInfo[item.feedId.toString()];
|
|
1483
|
+
if (rainfo.assetType === 0) {
|
|
1484
|
+
prices[rainfo.address.toString()] = {
|
|
1485
|
+
type: type_1.AssetType.Native,
|
|
1486
|
+
timestamp: item.timestamp,
|
|
1487
|
+
price: (item.price * BigInt(1e8)) / rainfo.precision,
|
|
1488
|
+
};
|
|
1489
|
+
}
|
|
1490
|
+
else if (rainfo.assetType === 1) {
|
|
1491
|
+
onchainData[rainfo.address.toString()] = {
|
|
1492
|
+
assetType: type_1.AssetType.SingleAsset,
|
|
1493
|
+
underlyingAddress: rainfo.underlyingAddress,
|
|
1494
|
+
underlyingReserve: item.price,
|
|
1495
|
+
totalSupply: rainfo.precision,
|
|
1496
|
+
};
|
|
1497
|
+
}
|
|
1498
|
+
}
|
|
1499
|
+
catch (e) { }
|
|
1500
|
+
});
|
|
1501
|
+
return { redstonePrices: prices, onchainData };
|
|
1502
|
+
}
|
|
1503
|
+
calculatePrices(_prices, oracleData) {
|
|
1504
|
+
const prices = { ..._prices };
|
|
1505
|
+
let oracles = Object.entries(oracleData);
|
|
1506
|
+
while (oracles.length !== 0) {
|
|
1507
|
+
const restOracles = [];
|
|
1508
|
+
for (let [asset, onchainDataInfo] of oracles) {
|
|
1509
|
+
if (!onchainDataInfo)
|
|
1510
|
+
throw "not found onchain data info1";
|
|
1511
|
+
if (onchainDataInfo.assetType === type_1.AssetType.SingleAsset) {
|
|
1512
|
+
if (prices[onchainDataInfo.underlyingAddress.toString()]) {
|
|
1513
|
+
const underlyingPriceInfo = prices[onchainDataInfo.underlyingAddress.toString()];
|
|
1514
|
+
prices[asset.toString()] = {
|
|
1515
|
+
type: type_1.AssetType.SingleAsset,
|
|
1516
|
+
price: (underlyingPriceInfo.price *
|
|
1517
|
+
onchainDataInfo.underlyingReserve) /
|
|
1518
|
+
onchainDataInfo.totalSupply,
|
|
1519
|
+
};
|
|
1520
|
+
}
|
|
1521
|
+
else {
|
|
1522
|
+
restOracles.push([asset, onchainDataInfo]);
|
|
1523
|
+
}
|
|
1524
|
+
}
|
|
1525
|
+
else if (onchainDataInfo.assetType === type_1.AssetType.MultiAsset) {
|
|
1526
|
+
if (prices[onchainDataInfo.underlying0Address.toString()] &&
|
|
1527
|
+
prices[onchainDataInfo.underlying1Address.toString()]) {
|
|
1528
|
+
const underlying0PriceInfo = prices[onchainDataInfo.underlying0Address.toString()];
|
|
1529
|
+
const underlying1PriceInfo = prices[onchainDataInfo.underlying1Address.toString()];
|
|
1530
|
+
const price0 = (underlying0PriceInfo.price *
|
|
1531
|
+
onchainDataInfo.underlying0Reserve) /
|
|
1532
|
+
onchainDataInfo.totalSupply;
|
|
1533
|
+
const price1 = (underlying1PriceInfo.price *
|
|
1534
|
+
onchainDataInfo.underlying1Reserve) /
|
|
1535
|
+
onchainDataInfo.totalSupply;
|
|
1536
|
+
prices[asset.toString()] = {
|
|
1537
|
+
type: type_1.AssetType.MultiAsset,
|
|
1538
|
+
price: price0 + price1,
|
|
1539
|
+
};
|
|
1540
|
+
}
|
|
1541
|
+
else {
|
|
1542
|
+
restOracles.push([asset, onchainDataInfo]);
|
|
1543
|
+
}
|
|
1544
|
+
}
|
|
1545
|
+
else if (onchainDataInfo.assetType === type_1.AssetType.Native) {
|
|
1546
|
+
}
|
|
1547
|
+
else {
|
|
1548
|
+
throw `invalid asset type ${onchainDataInfo.assetType}`;
|
|
1549
|
+
}
|
|
1550
|
+
}
|
|
1551
|
+
oracles = restOracles;
|
|
1552
|
+
}
|
|
1553
|
+
return prices;
|
|
1554
|
+
}
|
|
1555
|
+
decomposeAccountStatus(accountStatus, prices, onchainData) {
|
|
1556
|
+
const accumStatus = {};
|
|
1557
|
+
let userAssets = Object.values(accountStatus);
|
|
1558
|
+
while (userAssets.length !== 0) {
|
|
1559
|
+
const restUserAssets = [];
|
|
1560
|
+
for (let i = 0; i < userAssets.length; i++) {
|
|
1561
|
+
const state = userAssets[i];
|
|
1562
|
+
if (state.supply > 0n || state.borrow > 0n) {
|
|
1563
|
+
if (!accumStatus[userAssets[i].address.toString()])
|
|
1564
|
+
accumStatus[userAssets[i].address.toString()] = {
|
|
1565
|
+
supply: 0n,
|
|
1566
|
+
borrow: 0n,
|
|
1567
|
+
};
|
|
1568
|
+
if (state.supply > 0n) {
|
|
1569
|
+
accumStatus[userAssets[i].address.toString()].supply +=
|
|
1570
|
+
state.supply;
|
|
1571
|
+
}
|
|
1572
|
+
if (state.borrow > 0n) {
|
|
1573
|
+
accumStatus[userAssets[i].address.toString()].borrow +=
|
|
1574
|
+
state.borrow;
|
|
1575
|
+
}
|
|
1576
|
+
const onchainDataInfo = onchainData[state.address.toString()];
|
|
1577
|
+
if (!onchainDataInfo && !prices[state.address.toString()]) {
|
|
1578
|
+
throw "not found onchain data info2";
|
|
1579
|
+
}
|
|
1580
|
+
if (onchainDataInfo) {
|
|
1581
|
+
if (onchainDataInfo.assetType === type_1.AssetType.SingleAsset &&
|
|
1582
|
+
onchainDataInfo.underlyingReserve &&
|
|
1583
|
+
onchainDataInfo.totalSupply) {
|
|
1584
|
+
const underlyingSupply = (state.supply * onchainDataInfo.underlyingReserve) /
|
|
1585
|
+
onchainDataInfo.totalSupply;
|
|
1586
|
+
const underlyingBorrow = (state.borrow * onchainDataInfo.underlyingReserve) /
|
|
1587
|
+
onchainDataInfo.totalSupply;
|
|
1588
|
+
restUserAssets.push({
|
|
1589
|
+
address: onchainDataInfo.underlyingAddress,
|
|
1590
|
+
supply: underlyingSupply,
|
|
1591
|
+
borrow: underlyingBorrow,
|
|
1592
|
+
});
|
|
1593
|
+
}
|
|
1594
|
+
else if (onchainDataInfo.assetType === type_1.AssetType.MultiAsset &&
|
|
1595
|
+
onchainDataInfo.underlying0Reserve &&
|
|
1596
|
+
onchainDataInfo.underlying1Reserve &&
|
|
1597
|
+
onchainDataInfo.totalSupply) {
|
|
1598
|
+
const underlying0Supply = (state.supply * onchainDataInfo.underlying0Reserve) /
|
|
1599
|
+
onchainDataInfo.totalSupply;
|
|
1600
|
+
const underlying0Borrow = (state.borrow * onchainDataInfo.underlying0Reserve) /
|
|
1601
|
+
onchainDataInfo.totalSupply;
|
|
1602
|
+
restUserAssets.push({
|
|
1603
|
+
address: onchainDataInfo.underlying0Address,
|
|
1604
|
+
supply: underlying0Supply,
|
|
1605
|
+
borrow: underlying0Borrow,
|
|
1606
|
+
});
|
|
1607
|
+
const underlying1Supply = (state.supply * onchainDataInfo.underlying1Reserve) /
|
|
1608
|
+
onchainDataInfo.totalSupply;
|
|
1609
|
+
const underlying1Borrow = (state.borrow * onchainDataInfo.underlying1Reserve) /
|
|
1610
|
+
onchainDataInfo.totalSupply;
|
|
1611
|
+
restUserAssets.push({
|
|
1612
|
+
address: onchainDataInfo.underlying1Address,
|
|
1613
|
+
supply: underlying1Supply,
|
|
1614
|
+
borrow: underlying1Borrow,
|
|
1615
|
+
});
|
|
1616
|
+
}
|
|
1617
|
+
}
|
|
1618
|
+
}
|
|
1619
|
+
}
|
|
1620
|
+
userAssets = restUserAssets;
|
|
1621
|
+
}
|
|
1622
|
+
return accumStatus;
|
|
1623
|
+
}
|
|
1624
|
+
calculateRisk(decomposedAccountStatus, prices, poolData) {
|
|
1625
|
+
let collateral = 0n;
|
|
1626
|
+
let debt = 0n;
|
|
1627
|
+
let risk = 0n;
|
|
1628
|
+
Object.entries(decomposedAccountStatus).forEach(([asset, status]) => {
|
|
1629
|
+
const price = prices[asset];
|
|
1630
|
+
const suppliedValue = (BigInt(status.supply) * price.price) / BigInt(1e8);
|
|
1631
|
+
const borrowedValue = (BigInt(status.borrow) * price.price) / BigInt(1e8);
|
|
1632
|
+
const isCollateral = poolData.assets[asset].isCollateral;
|
|
1633
|
+
if (price.type === type_1.AssetType.Native) {
|
|
1634
|
+
if (isCollateral) {
|
|
1635
|
+
collateral += suppliedValue;
|
|
1636
|
+
}
|
|
1637
|
+
debt += borrowedValue;
|
|
1638
|
+
}
|
|
1639
|
+
if (isCollateral) {
|
|
1640
|
+
const delta = suppliedValue > borrowedValue
|
|
1641
|
+
? suppliedValue - borrowedValue
|
|
1642
|
+
: borrowedValue - suppliedValue;
|
|
1643
|
+
risk += (delta * poolData.assets[asset].riskFactor) / BigInt(1e4);
|
|
1644
|
+
}
|
|
1645
|
+
else {
|
|
1646
|
+
risk +=
|
|
1647
|
+
(borrowedValue * poolData.assets[asset].riskFactor) / BigInt(1e4);
|
|
1648
|
+
}
|
|
1649
|
+
});
|
|
1650
|
+
if (collateral === 0n && debt === 0n) {
|
|
1651
|
+
return {
|
|
1652
|
+
result: true,
|
|
1653
|
+
healthy: true,
|
|
1654
|
+
unhealthy: false,
|
|
1655
|
+
collateral,
|
|
1656
|
+
debt,
|
|
1657
|
+
risk,
|
|
1658
|
+
riskRatio: 0n,
|
|
1659
|
+
leverageRatio: 0n,
|
|
1660
|
+
};
|
|
1661
|
+
}
|
|
1662
|
+
const netValue = collateral - debt;
|
|
1663
|
+
if (netValue <= 0n) {
|
|
1664
|
+
return {
|
|
1665
|
+
result: false,
|
|
1666
|
+
healthy: false,
|
|
1667
|
+
unhealthy: true,
|
|
1668
|
+
collateral,
|
|
1669
|
+
debt,
|
|
1670
|
+
risk,
|
|
1671
|
+
riskRatio: 2n ** 128n - 1n,
|
|
1672
|
+
leverageRatio: 2n ** 128n - 1n,
|
|
1673
|
+
};
|
|
1674
|
+
}
|
|
1675
|
+
const leverageRatio = (collateral * BigInt(1e4)) / netValue;
|
|
1676
|
+
const riskRatio = (risk * BigInt(1e4)) / netValue;
|
|
1677
|
+
const unhealthy = poolData.liquidateLeverageRatio === 0n
|
|
1678
|
+
? riskRatio > poolData.liquidateVarRatio
|
|
1679
|
+
: leverageRatio > poolData.liquidateLeverageRatio ||
|
|
1680
|
+
riskRatio > poolData.liquidateVarRatio;
|
|
1681
|
+
return {
|
|
1682
|
+
result: !unhealthy,
|
|
1683
|
+
healthy: !unhealthy,
|
|
1684
|
+
unhealthy: unhealthy,
|
|
1685
|
+
collateral,
|
|
1686
|
+
debt,
|
|
1687
|
+
risk,
|
|
1688
|
+
riskRatio,
|
|
1689
|
+
leverageRatio,
|
|
1690
|
+
};
|
|
1691
|
+
}
|
|
1692
|
+
async getOracleRequestParams(assets, oracleConfig) {
|
|
1693
|
+
return redstone_onchain_oracle_1.RedstoneOnchainOracle.getOracleRequestParams(assets, oracleConfig);
|
|
1694
|
+
}
|
|
1695
|
+
async isHealthy(poolAddress, ownerAddress) {
|
|
1696
|
+
const pool = this.open(pool_1.Pool.createFromAddress(toAddress(poolAddress)));
|
|
1697
|
+
const borrowerAccountAddress = await this.getAccountAddress(toString(ownerAddress), toString(poolAddress));
|
|
1698
|
+
const borrowerAccount = this.open(account_1.Account.createFromAddress(toAddress(borrowerAccountAddress)));
|
|
1699
|
+
// const poolData = await pool.getPoolData() as any;
|
|
1700
|
+
const poolData = pool_1.Pool.simulateAccrueInterest(await pool.getPoolData());
|
|
1701
|
+
let accountData = null;
|
|
1702
|
+
try {
|
|
1703
|
+
accountData = account_1.Account.convertShareToAmount(await borrowerAccount.getAccountData(), poolData);
|
|
1704
|
+
// accountData.status["EQCxE6mUtQJKFnGfaROTKOt1lZbDiiX1kCixRv7Nw2Id_sDs"].borrow += 4637173n;
|
|
1705
|
+
}
|
|
1706
|
+
catch (e) {
|
|
1707
|
+
return {
|
|
1708
|
+
result: true,
|
|
1709
|
+
healthy: true,
|
|
1710
|
+
unhealthy: false,
|
|
1711
|
+
collateral: 0n,
|
|
1712
|
+
debt: 0n,
|
|
1713
|
+
risk: 0n,
|
|
1714
|
+
riskRatio: 0n,
|
|
1715
|
+
leverageRatio: 0n,
|
|
1716
|
+
};
|
|
1717
|
+
}
|
|
1718
|
+
const assets = account_1.Account.haveAssets(accountData);
|
|
1719
|
+
if (assets.length === 0) {
|
|
1720
|
+
return {
|
|
1721
|
+
result: true,
|
|
1722
|
+
healthy: true,
|
|
1723
|
+
unhealthy: false,
|
|
1724
|
+
collateral: 0n,
|
|
1725
|
+
debt: 0n,
|
|
1726
|
+
risk: 0n,
|
|
1727
|
+
riskRatio: 0n,
|
|
1728
|
+
leverageRatio: 0n,
|
|
1729
|
+
};
|
|
1730
|
+
}
|
|
1731
|
+
const result = redstone_onchain_oracle_1.RedstoneOnchainOracle.getOracleRequestParams(assets, poolData.oracleConfig);
|
|
1732
|
+
const { redstonePrices, onchainData: od } = await this.getRedstonePrices(assets, poolData);
|
|
1733
|
+
const onchainData2 = await this.updateOnchainData(poolData.oracleConfig.assetOnchainDataInfo);
|
|
1734
|
+
const onchainData = { ...od, ...onchainData2 };
|
|
1735
|
+
Object.keys(onchainData).forEach((key) => {
|
|
1736
|
+
if (!result.requestAssets.map((item) => item.toString()).includes(key) &&
|
|
1737
|
+
!result.redstoneAssets.map((item) => item.toString()).includes(key)) {
|
|
1738
|
+
delete onchainData[key];
|
|
1739
|
+
}
|
|
1740
|
+
});
|
|
1741
|
+
const prices = this.calculatePrices(redstonePrices, onchainData);
|
|
1742
|
+
const decomposedAccountStatus = this.decomposeAccountStatus(accountData.status, prices, onchainData);
|
|
1743
|
+
return this.calculateRisk(decomposedAccountStatus, prices, poolData);
|
|
1744
|
+
}
|
|
1745
|
+
async getPrices(poolAddress) {
|
|
1746
|
+
const pool = this.open(pool_1.Pool.createFromAddress(toAddress(poolAddress)));
|
|
1747
|
+
const poolData = (await pool.getPoolData());
|
|
1748
|
+
const assets = Object.keys(poolData.assets);
|
|
1749
|
+
const { redstonePrices, onchainData: od } = await this.getRedstonePrices(assets, poolData);
|
|
1750
|
+
const onchainData2 = await this.updateOnchainData(poolData.oracleConfig.assetOnchainDataInfo);
|
|
1751
|
+
const onchainData = { ...od, ...onchainData2 };
|
|
1752
|
+
const prices = this.calculatePrices(redstonePrices, onchainData);
|
|
1753
|
+
return prices;
|
|
1754
|
+
}
|
|
1755
|
+
async getPoolData(poolAddress) {
|
|
1756
|
+
if (!(poolAddress instanceof ton_1.Address)) {
|
|
1757
|
+
poolAddress = ton_1.Address.parse(poolAddress);
|
|
1758
|
+
}
|
|
1759
|
+
const pool = this.open(pool_1.Pool.createFromAddress(poolAddress));
|
|
1760
|
+
return await pool.getPoolData();
|
|
1761
|
+
}
|
|
1762
|
+
async getAccountData(poolAddress, userAddress) {
|
|
1763
|
+
if (!(poolAddress instanceof ton_1.Address)) {
|
|
1764
|
+
poolAddress = ton_1.Address.parse(poolAddress);
|
|
1765
|
+
}
|
|
1766
|
+
if (!(userAddress instanceof ton_1.Address)) {
|
|
1767
|
+
userAddress = ton_1.Address.parse(userAddress);
|
|
1768
|
+
}
|
|
1769
|
+
const pool = this.open(pool_1.Pool.createFromAddress(poolAddress));
|
|
1770
|
+
if (!this.accountAddresses[userAddress.toString()]?.[poolAddress.toString()]) {
|
|
1771
|
+
if (!this.accountAddresses[userAddress.toString()]) {
|
|
1772
|
+
this.accountAddresses[userAddress.toString()] = {};
|
|
1773
|
+
}
|
|
1774
|
+
this.accountAddresses[userAddress.toString()][poolAddress.toString()] =
|
|
1775
|
+
await pool.getAccountAddress(userAddress);
|
|
1776
|
+
}
|
|
1777
|
+
const account = this.open(account_1.Account.createFromAddress(this.accountAddresses[userAddress.toString()][poolAddress.toString()]));
|
|
1778
|
+
let accountData;
|
|
1779
|
+
const poolData = await pool.getPoolData();
|
|
1780
|
+
const assets = poolData.assets;
|
|
1781
|
+
const emptyStatus = Object.keys(assets).reduce((acc, asset) => {
|
|
1782
|
+
acc[asset] = {
|
|
1783
|
+
address: ton_1.Address.parse(asset),
|
|
1784
|
+
supply: 0n,
|
|
1785
|
+
borrow: 0n,
|
|
1786
|
+
};
|
|
1787
|
+
return acc;
|
|
1788
|
+
}, {});
|
|
1789
|
+
try {
|
|
1790
|
+
/** When Account is deployed */
|
|
1791
|
+
const accountDataFromOnchain = await account.getAccountData();
|
|
1792
|
+
const parsedAccountData = {
|
|
1793
|
+
...accountDataFromOnchain,
|
|
1794
|
+
status: { ...emptyStatus, ...accountDataFromOnchain.status },
|
|
1795
|
+
};
|
|
1796
|
+
accountData = parsedAccountData;
|
|
1797
|
+
}
|
|
1798
|
+
catch {
|
|
1799
|
+
/** When Account is not deployed(User never interacted with the pool), "Not Active" thrown */
|
|
1800
|
+
accountData = {
|
|
1801
|
+
address: userAddress,
|
|
1802
|
+
pool: poolAddress,
|
|
1803
|
+
owner: poolData.owner,
|
|
1804
|
+
status: emptyStatus,
|
|
1805
|
+
};
|
|
1806
|
+
}
|
|
1807
|
+
return accountData;
|
|
1808
|
+
}
|
|
1809
|
+
}
|
|
1810
|
+
exports.PoolCacheV1 = PoolCacheV1;
|
|
1811
|
+
class StormVault {
|
|
1812
|
+
address;
|
|
1813
|
+
init;
|
|
1814
|
+
constructor(address, init) {
|
|
1815
|
+
this.address = address;
|
|
1816
|
+
this.init = init;
|
|
1817
|
+
}
|
|
1818
|
+
static createFromAddress(address) {
|
|
1819
|
+
return new StormVault(address);
|
|
1820
|
+
}
|
|
1821
|
+
async getVaultRate(provider) {
|
|
1822
|
+
const { stack } = await provider.get("get_vault_data", []);
|
|
1823
|
+
const jettonWalletAddress = stack.readAddress();
|
|
1824
|
+
const lpRate = stack.readBigNumber();
|
|
1825
|
+
const lpTotalSupply = stack.readBigNumber();
|
|
1826
|
+
const freeBalance = stack.readBigNumber();
|
|
1827
|
+
const lockedBalance = stack.readBigNumber();
|
|
1828
|
+
const withdrawLockedBalance = stack.readBigNumber();
|
|
1829
|
+
const stakersBalance = stack.readBigNumber();
|
|
1830
|
+
const executorsBalance = stack.readBigNumber();
|
|
1831
|
+
return {
|
|
1832
|
+
jettonWalletAddress,
|
|
1833
|
+
lpRate,
|
|
1834
|
+
lpTotalSupply,
|
|
1835
|
+
freeBalance,
|
|
1836
|
+
lockedBalance,
|
|
1837
|
+
withdrawLockedBalance,
|
|
1838
|
+
stakersBalance,
|
|
1839
|
+
};
|
|
1840
|
+
}
|
|
1841
|
+
}
|