@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,1199 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.StrategyVaultBase = void 0;
|
|
4
|
+
exports.createQueryId = createQueryId;
|
|
5
|
+
const core_1 = require("@ton/core");
|
|
6
|
+
const service_1 = require("../common/service");
|
|
7
|
+
const utils_1 = require("../contracts/common/utils");
|
|
8
|
+
const jetton_minter_1 = require("../contracts/jetton/jetton-minter");
|
|
9
|
+
const redstone_onchain_oracle_1 = require("../contracts/oracle/redstone-onchain-oracle");
|
|
10
|
+
const strategy_vault_1 = require("../contracts/vault/strategy-vault");
|
|
11
|
+
const jetton_wallet_1 = require("../contracts/jetton/jetton-wallet");
|
|
12
|
+
const pool_1 = require("../contracts/core/pool");
|
|
13
|
+
const rfq_auction_1 = require("../contracts/rfq/rfq_auction");
|
|
14
|
+
const type_1 = require("../contracts/oracle/redstone-onchain-oracle/type");
|
|
15
|
+
const __1 = require("..");
|
|
16
|
+
const jetton_minter_2 = require("../contracts/wton/jetton-minter");
|
|
17
|
+
const jetton_wallet_2 = require("../contracts/wton/jetton-wallet");
|
|
18
|
+
const sha256_1 = require("@noble/hashes/sha256");
|
|
19
|
+
const monitorCacheV1_1 = require("../monitorCacheV1");
|
|
20
|
+
const redstoneHelper_1 = require("../utils/oracle/redstone/redstoneHelper");
|
|
21
|
+
const readonlyCachedRedstone_1 = require("../utils/oracle/redstone/readonlyCachedRedstone");
|
|
22
|
+
const oracle_1 = require("../oracle/oracle");
|
|
23
|
+
const unknown_contract_1 = require("../common/unknown-contract");
|
|
24
|
+
function jsonReplacer(_, value) {
|
|
25
|
+
if (typeof value === "bigint") {
|
|
26
|
+
return { __type: "bigint", value: value.toString() };
|
|
27
|
+
}
|
|
28
|
+
if (value instanceof core_1.Address) {
|
|
29
|
+
return { __type: "address", value: value.toString() };
|
|
30
|
+
}
|
|
31
|
+
if (value instanceof core_1.Cell) {
|
|
32
|
+
return { __type: "cell", value: value.hash().toString("hex") };
|
|
33
|
+
}
|
|
34
|
+
return value;
|
|
35
|
+
}
|
|
36
|
+
function createQueryId(data, timestamp = Date.now()) {
|
|
37
|
+
const dataWithTime = {
|
|
38
|
+
...data,
|
|
39
|
+
__timestamp: timestamp, // 키 이름은 prefix로 충돌 방지
|
|
40
|
+
};
|
|
41
|
+
const json = JSON.stringify(dataWithTime, jsonReplacer);
|
|
42
|
+
const hash = (0, sha256_1.sha256)(Buffer.from(json));
|
|
43
|
+
return BigInt("0x" + Buffer.from(hash).subarray(0, 8).toString("hex"));
|
|
44
|
+
}
|
|
45
|
+
function assertValidSlippageRange(value, name) {
|
|
46
|
+
if (value < 0) {
|
|
47
|
+
throw new Error(`Slippage ${name} must be greater than 0.`);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
function toSlippageConfig(input) {
|
|
51
|
+
if (typeof input === "number") {
|
|
52
|
+
assertValidSlippageRange(input, "slippage");
|
|
53
|
+
return {
|
|
54
|
+
min: (0, core_1.toNano)(1 - input),
|
|
55
|
+
max: (0, core_1.toNano)(1 + input),
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
const { min, max } = input;
|
|
60
|
+
assertValidSlippageRange(min, "slippage min");
|
|
61
|
+
assertValidSlippageRange(max, "slippage max");
|
|
62
|
+
if (min >= max) {
|
|
63
|
+
throw new Error("Slippage min must be less than max.");
|
|
64
|
+
}
|
|
65
|
+
return {
|
|
66
|
+
min: (0, core_1.toNano)(min),
|
|
67
|
+
max: (0, core_1.toNano)(max),
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
class StrategyVaultBase extends service_1.ServiceBaseV1 {
|
|
72
|
+
async getVaultData(strategyVaultAddress) {
|
|
73
|
+
const vault = this.getVault(strategyVaultAddress);
|
|
74
|
+
return await vault.getVaultData();
|
|
75
|
+
}
|
|
76
|
+
getVault(strategyVaultAddress) {
|
|
77
|
+
return this.getByContract(strategy_vault_1.StrategyVault, (0, utils_1.toAddress)(strategyVaultAddress));
|
|
78
|
+
}
|
|
79
|
+
async getVaultWallet(strategyVaultAddress, accountAddress) {
|
|
80
|
+
const vault = this.getVault(strategyVaultAddress);
|
|
81
|
+
return this.getByContract(jetton_wallet_1.JettonWallet, await vault.getWalletAddress((0, utils_1.toAddress)(accountAddress)));
|
|
82
|
+
}
|
|
83
|
+
async sendSetOwner(sender, params) {
|
|
84
|
+
const strategyVault = this.getVault(params.strategyVaultAddress);
|
|
85
|
+
await strategyVault.sendSetOwnerAddress(sender, (0, core_1.toNano)(0.03), {
|
|
86
|
+
newOwner: (0, utils_1.toAddress)(params.owner),
|
|
87
|
+
queryID: params.queryId,
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
async sendSetGuardianAddress(sender, params, value) {
|
|
91
|
+
const strategyVault = this.getVault(params.strategyVaultAddress);
|
|
92
|
+
await strategyVault.sendSetGuardianAddress(sender, value ?? (0, core_1.toNano)(0.1), {
|
|
93
|
+
guardianAddress: (0, utils_1.toAddress)(params.guardianAddress),
|
|
94
|
+
queryID: params.queryId,
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
async sendSetManager(sender, params) {
|
|
98
|
+
const strategyVault = this.getVault(params.strategyVaultAddress);
|
|
99
|
+
await strategyVault.sendSetManagerAddress(sender, (0, core_1.toNano)(0.03), {
|
|
100
|
+
managerAddress: (0, utils_1.toAddress)(params.manager),
|
|
101
|
+
queryID: params.queryId,
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
async sendSetPrivateVault(sender, params, value) {
|
|
105
|
+
const strategyVault = this.getVault(params.strategyVaultAddress);
|
|
106
|
+
await strategyVault.sendSetPrivateVault(sender, value ?? (0, core_1.toNano)(0.02), {
|
|
107
|
+
isPrivateVault: params.isPrivateVault,
|
|
108
|
+
queryID: params.queryId,
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
async sendSetWhitelistedMinters(sender, params, value) {
|
|
112
|
+
const strategyVault = this.getByContract(strategy_vault_1.StrategyVault, (0, utils_1.toAddress)(params.strategyVaultAddress));
|
|
113
|
+
const mintersDict = core_1.Dictionary.empty(core_1.Dictionary.Keys.Address(), strategy_vault_1.StrategyVault.Dictionary.Values.Empty());
|
|
114
|
+
for (const minter of params.whitelistedMinters) {
|
|
115
|
+
mintersDict.set((0, utils_1.toAddress)(minter), null);
|
|
116
|
+
}
|
|
117
|
+
await strategyVault.sendSetWhitelistedMinters(sender, value ?? (0, core_1.toNano)(0.03), {
|
|
118
|
+
whitelistedMinters: (0, core_1.beginCell)().storeDictDirect(mintersDict).endCell(),
|
|
119
|
+
queryID: params.queryId,
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
async sendSetMaxLeverageRatio(sender, params, value) {
|
|
123
|
+
const strategyVault = this.getByContract(strategy_vault_1.StrategyVault, (0, utils_1.toAddress)(params.strategyVaultAddress));
|
|
124
|
+
await strategyVault.sendSetMaxLeverageRatio(sender, value ?? (0, core_1.toNano)(0.03), {
|
|
125
|
+
maxLeverageRatio: params.maxLeverageRatio,
|
|
126
|
+
queryID: params.queryId,
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
async sendExecuteOwnerAction(sender, params, value) {
|
|
130
|
+
const strategyVault = this.getVault(params.strategyVaultAddress);
|
|
131
|
+
await strategyVault.sendExecuteOwnerAction(sender, value ?? (0, core_1.toNano)(0.1), {
|
|
132
|
+
updateIndex: params.updateIndex,
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
async sendRevertOwnerAction(sender, params, value) {
|
|
136
|
+
const strategyVault = this.getVault(params.strategyVaultAddress);
|
|
137
|
+
await strategyVault.sendRevertOwnerAction(sender, value ?? (0, core_1.toNano)(0.1), {
|
|
138
|
+
updateIndex: params.updateIndex,
|
|
139
|
+
queryID: params.queryId,
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
async sendSetTimelock(sender, params, value) {
|
|
143
|
+
const strategyVault = this.getVault(params.strategyVaultAddress);
|
|
144
|
+
await strategyVault.sendSetTimelock(sender, value ?? (0, core_1.toNano)(0.1), {
|
|
145
|
+
timelock: params.timelock,
|
|
146
|
+
queryID: params.queryId,
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
async sendSetManagementFeeRecipientAddress(sender, params, value) {
|
|
150
|
+
const strategyVault = this.getVault(params.strategyVaultAddress);
|
|
151
|
+
await strategyVault.sendSetManagementFeeRecipientAddress(sender, value ?? (0, core_1.toNano)(0.1), {
|
|
152
|
+
managementFeeRecipientAddress: (0, utils_1.toAddress)(params.managementFeeRecipientAddress),
|
|
153
|
+
queryID: params.queryId,
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
async sendSetProtocolFeeRatePerYear(sender, params, value) {
|
|
157
|
+
const strategyVault = this.getVault(params.strategyVaultAddress);
|
|
158
|
+
await strategyVault.sendSetProtocolFeeRatePerYear(sender, value ?? (0, core_1.toNano)(0.1), {
|
|
159
|
+
protocolFeeRate: params.protocolFeeRatePerYear,
|
|
160
|
+
queryID: params.queryId,
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
async sendSetProtocolFeeManagerAddress(sender, params, value) {
|
|
164
|
+
const strategyVault = this.getVault(params.strategyVaultAddress);
|
|
165
|
+
await strategyVault.sendSetProtocolFeeManagerAddress(sender, value ?? (0, core_1.toNano)(0.1), {
|
|
166
|
+
protocolFeeManagerAddress: (0, utils_1.toAddress)(params.protocolFeeManagerAddress),
|
|
167
|
+
queryID: params.queryId,
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
async sendSetFactorialPoolConfig(sender, params) {
|
|
171
|
+
const strategyVault = this.getByContract(strategy_vault_1.StrategyVault, (0, utils_1.toAddress)(params.strategyVaultAddress));
|
|
172
|
+
const factorialPoolConfig = (0, core_1.beginCell)()
|
|
173
|
+
.storeDictDirect(strategy_vault_1.StrategyVault.createFactorialPoolUpdateConfig(params.configs.map((config) => [
|
|
174
|
+
(0, utils_1.toAddress)(config.poolAddress),
|
|
175
|
+
(0, utils_1.toAddress)(config.assetAddress),
|
|
176
|
+
config.isSupplyable,
|
|
177
|
+
config.isBorrowable,
|
|
178
|
+
])))
|
|
179
|
+
.endCell();
|
|
180
|
+
await strategyVault.sendSetFactorialPoolConfig(sender, (0, core_1.toNano)(0.03), {
|
|
181
|
+
updateConfig: factorialPoolConfig,
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
async sendSetAssetConfig(sender, params, value) {
|
|
185
|
+
const strategyVault = this.getByContract(strategy_vault_1.StrategyVault, (0, utils_1.toAddress)(params.strategyVaultAddress));
|
|
186
|
+
await strategyVault.sendSetAssetConfig(sender, value ?? (0, core_1.toNano)(0.02), {
|
|
187
|
+
assetAddress: (0, utils_1.toAddress)(params.assetAddress),
|
|
188
|
+
isWhitelisted: params.isWhitelisted,
|
|
189
|
+
isDepositable: params.isDepositable,
|
|
190
|
+
isWithdrawable: params.isWithdrawable,
|
|
191
|
+
exposureCap: params.exposureCap,
|
|
192
|
+
queryID: params.queryId,
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
async sendSetRFQConfig(sender, params, value) {
|
|
196
|
+
const strategyVault = this.getByContract(strategy_vault_1.StrategyVault, (0, utils_1.toAddress)(params.strategyVaultAddress));
|
|
197
|
+
await strategyVault.sendSetRFQConfig(sender, value ?? (0, core_1.toNano)(0.02), {
|
|
198
|
+
rfqConfig: (0, core_1.beginCell)()
|
|
199
|
+
.storeUint(params.priceDeviationTolerance, 14)
|
|
200
|
+
.storeAddress((0, utils_1.toAddress)(params.rfqEmitterAddress))
|
|
201
|
+
.endCell(),
|
|
202
|
+
queryID: params.queryId,
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
async getOracleParamsV2(vaultAddress, vaultData, extraAssets) {
|
|
206
|
+
const vault = this.getByContract(strategy_vault_1.StrategyVault, (0, utils_1.toAddress)(vaultAddress));
|
|
207
|
+
vaultData = vaultData ?? (await vault.getVaultData());
|
|
208
|
+
const oracleConfig = vaultData.oracleConfig;
|
|
209
|
+
let { assets, vaultAssets } = await this.separateAssets(Object.keys(vaultData.assets), oracleConfig);
|
|
210
|
+
vaultAssets = [
|
|
211
|
+
...new Set([
|
|
212
|
+
...vaultAssets,
|
|
213
|
+
...(extraAssets ? extraAssets.vaultAssets : []),
|
|
214
|
+
]),
|
|
215
|
+
];
|
|
216
|
+
const vaultPositions = await this.getParsedVaults([
|
|
217
|
+
...vaultAssets,
|
|
218
|
+
(0, utils_1.toAddress)(vaultAddress),
|
|
219
|
+
]);
|
|
220
|
+
const exposures = await this.getExposures(vaultPositions);
|
|
221
|
+
const exposurePools = exposures.pools;
|
|
222
|
+
assets = [
|
|
223
|
+
...new Set([
|
|
224
|
+
...assets,
|
|
225
|
+
...exposures.assets,
|
|
226
|
+
...(extraAssets ? extraAssets.assets : []),
|
|
227
|
+
]),
|
|
228
|
+
];
|
|
229
|
+
let requestFactorialPoolsCell = exposurePools.length > 0
|
|
230
|
+
? redstone_onchain_oracle_1.RedstoneOnchainOracle.createAddressToTupleCell(exposurePools.map((pool) => core_1.Address.parse(pool)))
|
|
231
|
+
: null;
|
|
232
|
+
let requestFactorialVaultsCell = vaultAssets.length > 0
|
|
233
|
+
? redstone_onchain_oracle_1.RedstoneOnchainOracle.createAddressToTupleCell(vaultAssets.map((vault) => core_1.Address.parse(vault)))
|
|
234
|
+
: null;
|
|
235
|
+
let requestFactorialCell = (0, core_1.beginCell)()
|
|
236
|
+
.storeMaybeRef(requestFactorialPoolsCell)
|
|
237
|
+
.storeMaybeRef(requestFactorialVaultsCell)
|
|
238
|
+
.endCell();
|
|
239
|
+
const result = redstone_onchain_oracle_1.RedstoneOnchainOracle.getOracleRequestParams(assets,
|
|
240
|
+
// @ts-ignore
|
|
241
|
+
vaultData.oracleConfig);
|
|
242
|
+
const helper = (0, redstoneHelper_1.createRedstoneHelper)("prod");
|
|
243
|
+
const payload = await helper.createPayload(result.redstoneAssetNames);
|
|
244
|
+
const requestAssetCell = redstone_onchain_oracle_1.RedstoneOnchainOracle.createAddressToTupleCell(result.requestAssets);
|
|
245
|
+
const dataFeedIdsTupleBuilder = new core_1.TupleBuilder();
|
|
246
|
+
result.feedIds.forEach((item) => dataFeedIdsTupleBuilder.writeNumber(item));
|
|
247
|
+
const feedIdCell = (0, core_1.serializeTuple)(dataFeedIdsTupleBuilder.build());
|
|
248
|
+
const oracleParams = (0, core_1.beginCell)()
|
|
249
|
+
.storeRef(feedIdCell)
|
|
250
|
+
.storeRef(payload)
|
|
251
|
+
.storeRef(requestAssetCell)
|
|
252
|
+
.storeRef(requestFactorialCell)
|
|
253
|
+
.endCell();
|
|
254
|
+
return oracleParams;
|
|
255
|
+
}
|
|
256
|
+
async getOracleParams(vaultAddress, additionalAssets = [], allFactorialVaults = [], vaultData) {
|
|
257
|
+
const vault = this.getByContract(strategy_vault_1.StrategyVault, (0, utils_1.toAddress)(vaultAddress));
|
|
258
|
+
vaultData = vaultData ?? (await vault.getVaultData());
|
|
259
|
+
const assetsInfo = await this.splitPureAndVaultAsset([
|
|
260
|
+
...new Set(additionalAssets
|
|
261
|
+
.map((item) => (0, utils_1.toAddress)(item).toString())
|
|
262
|
+
.concat(Object.keys(vaultData.assets))),
|
|
263
|
+
]);
|
|
264
|
+
const pools = [];
|
|
265
|
+
for (const poolAddress of Object.keys(vaultData.factorialPools)) {
|
|
266
|
+
const assets = vaultData.factorialPools[poolAddress];
|
|
267
|
+
for (const assetAddress of Object.keys(assets)) {
|
|
268
|
+
if (assets[assetAddress].supply > 0 ||
|
|
269
|
+
assets[assetAddress].borrow > 0) {
|
|
270
|
+
pools.push(poolAddress);
|
|
271
|
+
break;
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
const assets = [];
|
|
276
|
+
const vaults = assetsInfo.vaultAssetAddresses;
|
|
277
|
+
for (const assetAddress of assetsInfo.pureAssetAddresses) {
|
|
278
|
+
if (allFactorialVaults.includes(assetAddress)) {
|
|
279
|
+
vaults.push(assetAddress);
|
|
280
|
+
}
|
|
281
|
+
else {
|
|
282
|
+
assets.push(assetAddress);
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
const result = redstone_onchain_oracle_1.RedstoneOnchainOracle.getOracleRequestParams(assets,
|
|
286
|
+
// @ts-ignore
|
|
287
|
+
vaultData.oracleConfig);
|
|
288
|
+
const helper = (0, redstoneHelper_1.createRedstoneHelper)("prod");
|
|
289
|
+
const payload = await helper.createPayload(result.redstoneAssetNames);
|
|
290
|
+
const requestAssetCell = redstone_onchain_oracle_1.RedstoneOnchainOracle.createAddressToTupleCell(result.requestAssets);
|
|
291
|
+
const dataFeedIdsTupleBuilder = new core_1.TupleBuilder();
|
|
292
|
+
result.feedIds.forEach((item) => dataFeedIdsTupleBuilder.writeNumber(item));
|
|
293
|
+
const feedIdCell = (0, core_1.serializeTuple)(dataFeedIdsTupleBuilder.build());
|
|
294
|
+
let requestFactorialPoolsCell = pools.length > 0
|
|
295
|
+
? redstone_onchain_oracle_1.RedstoneOnchainOracle.createAddressToTupleCell(pools.map((item) => core_1.Address.parse(item)))
|
|
296
|
+
: null;
|
|
297
|
+
let requestFactorialVaultsCell = vaults.length > 0
|
|
298
|
+
? redstone_onchain_oracle_1.RedstoneOnchainOracle.createAddressToTupleCell(vaults.map((item) => core_1.Address.parse(item)))
|
|
299
|
+
: null;
|
|
300
|
+
let requestFactorialCell = (0, core_1.beginCell)()
|
|
301
|
+
.storeMaybeRef(requestFactorialPoolsCell)
|
|
302
|
+
.storeMaybeRef(requestFactorialVaultsCell)
|
|
303
|
+
.endCell();
|
|
304
|
+
const oracleParams = (0, core_1.beginCell)()
|
|
305
|
+
.storeRef(feedIdCell)
|
|
306
|
+
.storeRef(payload)
|
|
307
|
+
.storeRef(requestAssetCell)
|
|
308
|
+
.storeRef(requestFactorialCell)
|
|
309
|
+
.endCell();
|
|
310
|
+
return oracleParams;
|
|
311
|
+
}
|
|
312
|
+
async sendDepositAsset(sender, params, value) {
|
|
313
|
+
const vault = this.getVault(params.strategyVaultAddress);
|
|
314
|
+
const vaultData = await vault.getVaultData();
|
|
315
|
+
const oracle = new oracle_1.OracleLibs(this.client, this.network);
|
|
316
|
+
// need all assets in vault
|
|
317
|
+
const oraclePayload = await oracle.getOracleParamsForStrategyVault(vaultData, [params.assetAddress.toString(), ...Object.keys(vaultData.assets)]);
|
|
318
|
+
const isWTON = params.assetAddress.toString() === this.contracts.WTON ? true : false;
|
|
319
|
+
const forwardPayload = strategy_vault_1.StrategyVault.createDepositPayload(oraclePayload, isWTON ? (0, core_1.toNano)(0.02) : 0n, isWTON ? jetton_minter_2.WTON_UNWRAP_CELL : undefined);
|
|
320
|
+
const assetAddress = (0, utils_1.toAddress)(params.assetAddress);
|
|
321
|
+
const fee = vaultData.gasConfig[strategy_vault_1.StrategyVault.GasKey.Deposit] +
|
|
322
|
+
vaultData.gasConfig[strategy_vault_1.StrategyVault.GasKey.DepositLogic] +
|
|
323
|
+
(isWTON ? (0, core_1.toNano)(0.02) : 0n);
|
|
324
|
+
const opts = {
|
|
325
|
+
recipient: vault.address,
|
|
326
|
+
amount: params.amount,
|
|
327
|
+
response: sender.address,
|
|
328
|
+
forwardTonAmount: fee,
|
|
329
|
+
forwardPayload,
|
|
330
|
+
queryId: params.queryId ?? createQueryId({ ...params, forwardPayload }),
|
|
331
|
+
};
|
|
332
|
+
if (assetAddress.toString() === (0, utils_1.toAddress)(this.contracts.WTON).toString()) {
|
|
333
|
+
// send to vault wton wallet
|
|
334
|
+
const wtonMaster = this.getByContract(jetton_minter_2.WTONMinter, assetAddress);
|
|
335
|
+
const wtonWallet = this.getByContract(jetton_wallet_2.WTONWallet, (0, utils_1.toAddress)(await wtonMaster.getWalletAddress(vault.address)));
|
|
336
|
+
await wtonWallet.sendExternalTransfer(sender, value ?? jetton_wallet_2.WTONWallet.Gas.ExternalTransfer, opts);
|
|
337
|
+
}
|
|
338
|
+
else {
|
|
339
|
+
// send to sender jetton wallet
|
|
340
|
+
const jettonMaster = this.getByContract(jetton_minter_1.JettonMinter, assetAddress);
|
|
341
|
+
const jettonWalletAddress = await jettonMaster.getWalletAddress(sender.address);
|
|
342
|
+
const jettonWallet = this.getByContract(jetton_wallet_1.JettonWallet, jettonWalletAddress);
|
|
343
|
+
await jettonWallet.sendTransfer(sender, value ?? fee + (0, core_1.toNano)(0.1), opts);
|
|
344
|
+
}
|
|
345
|
+
return opts;
|
|
346
|
+
}
|
|
347
|
+
async sendBurnVaultAsset(sender, params, value) {
|
|
348
|
+
const vault = this.getVault(params.strategyVaultAddress);
|
|
349
|
+
const vaultData = await vault.getVaultData();
|
|
350
|
+
const vaultAssetWalllet = await this.getVaultWallet(params.strategyVaultAddress, sender.address);
|
|
351
|
+
const oraclePayload = await this.getOracleParamsV2(params.strategyVaultAddress);
|
|
352
|
+
const withdrawAssetAddress = (0, utils_1.toAddress)(params.withdrawAssetAddress);
|
|
353
|
+
const isWTON = withdrawAssetAddress.toString() === this.contracts.WTON ? true : false;
|
|
354
|
+
const forwardPayload = strategy_vault_1.StrategyVault.createWithdrawPayload(withdrawAssetAddress, oraclePayload, isWTON ? (0, core_1.toNano)(0.02) : 0n, isWTON ? jetton_minter_2.WTON_UNWRAP_CELL : undefined);
|
|
355
|
+
const fee = vaultData.gasConfig[strategy_vault_1.StrategyVault.GasKey.Withdraw] +
|
|
356
|
+
vaultData.gasConfig[strategy_vault_1.StrategyVault.GasKey.WithdrawLogic] +
|
|
357
|
+
(isWTON ? (0, core_1.toNano)(0.02) : 0n) +
|
|
358
|
+
(0, core_1.toNano)(0.1);
|
|
359
|
+
const opts = {
|
|
360
|
+
amount: params.vaultShareAmountForBurn,
|
|
361
|
+
response: sender.address,
|
|
362
|
+
forwardPayload: forwardPayload,
|
|
363
|
+
queryId: createQueryId({ ...params, forwardPayload }),
|
|
364
|
+
};
|
|
365
|
+
await vaultAssetWalllet.sendBurn(sender, value ?? fee, opts);
|
|
366
|
+
return opts;
|
|
367
|
+
}
|
|
368
|
+
async createInternalDepositAsset(sender, params, value) {
|
|
369
|
+
const vault = this.getVault(params.strategyVaultAddress);
|
|
370
|
+
const vaultData = await vault.getVaultData();
|
|
371
|
+
const oracle = new oracle_1.OracleLibs(this.client, this.network);
|
|
372
|
+
// need all assets in vault
|
|
373
|
+
const oraclePayload = await oracle.getOracleParamsForStrategyVault(vaultData, [params.assetAddress.toString(), ...Object.keys(vaultData.assets)]);
|
|
374
|
+
const isWTON = params.assetAddress.toString() === this.contracts.WTON ? true : false;
|
|
375
|
+
const forwardPayload = strategy_vault_1.StrategyVault.createDepositPayload(oraclePayload, isWTON ? (0, core_1.toNano)(0.02) : 0n, isWTON ? jetton_minter_2.WTON_UNWRAP_CELL : undefined);
|
|
376
|
+
const assetAddress = (0, utils_1.toAddress)(params.assetAddress);
|
|
377
|
+
const fee = vaultData.gasConfig[strategy_vault_1.StrategyVault.GasKey.Deposit] +
|
|
378
|
+
vaultData.gasConfig[strategy_vault_1.StrategyVault.GasKey.DepositLogic] +
|
|
379
|
+
(isWTON ? (0, core_1.toNano)(0.02) : 0n);
|
|
380
|
+
const opts = {
|
|
381
|
+
recipient: vault.address,
|
|
382
|
+
amount: params.amount,
|
|
383
|
+
response: sender.address,
|
|
384
|
+
forwardTonAmount: fee,
|
|
385
|
+
forwardPayload,
|
|
386
|
+
queryId: params.queryId ?? createQueryId({ ...params, forwardPayload }),
|
|
387
|
+
};
|
|
388
|
+
if (assetAddress.toString() === (0, utils_1.toAddress)(this.contracts.WTON).toString()) {
|
|
389
|
+
// send to vault wton wallet
|
|
390
|
+
const wtonMaster = this.getByContract(jetton_minter_2.WTONMinter, assetAddress);
|
|
391
|
+
const wtonWallet = this.getByContract(jetton_wallet_2.WTONWallet, (0, utils_1.toAddress)(await wtonMaster.getWalletAddress(vault.address)));
|
|
392
|
+
return wtonWallet.createInternalExternalTransfer(value ?? jetton_wallet_2.WTONWallet.Gas.ExternalTransfer, opts);
|
|
393
|
+
}
|
|
394
|
+
else {
|
|
395
|
+
// send to sender jetton wallet
|
|
396
|
+
const jettonMaster = this.getByContract(jetton_minter_1.JettonMinter, assetAddress);
|
|
397
|
+
const jettonWalletAddress = await jettonMaster.getWalletAddress(sender.address);
|
|
398
|
+
const jettonWallet = this.getByContract(jetton_wallet_1.JettonWallet, jettonWalletAddress);
|
|
399
|
+
return jettonWallet.createInternalTransfer(value ?? fee + (0, core_1.toNano)(0.1), opts);
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
async createInternalBurnVaultAsset(sender, params, value) {
|
|
403
|
+
const vault = this.getVault(params.strategyVaultAddress);
|
|
404
|
+
const vaultData = await vault.getVaultData();
|
|
405
|
+
const vaultAssetWalllet = await this.getVaultWallet(params.strategyVaultAddress, sender.address);
|
|
406
|
+
const oraclePayload = await this.getOracleParamsV2(params.strategyVaultAddress);
|
|
407
|
+
const withdrawAssetAddress = (0, utils_1.toAddress)(params.withdrawAssetAddress);
|
|
408
|
+
const isWTON = withdrawAssetAddress.toString() === this.contracts.WTON ? true : false;
|
|
409
|
+
const forwardPayload = strategy_vault_1.StrategyVault.createWithdrawPayload(withdrawAssetAddress, oraclePayload, isWTON ? (0, core_1.toNano)(0.02) : 0n, isWTON ? jetton_minter_2.WTON_UNWRAP_CELL : undefined);
|
|
410
|
+
const fee = vaultData.gasConfig[strategy_vault_1.StrategyVault.GasKey.Withdraw] +
|
|
411
|
+
vaultData.gasConfig[strategy_vault_1.StrategyVault.GasKey.WithdrawLogic] +
|
|
412
|
+
(isWTON ? (0, core_1.toNano)(0.02) : 0n) +
|
|
413
|
+
(0, core_1.toNano)(0.1);
|
|
414
|
+
const opts = {
|
|
415
|
+
amount: params.vaultShareAmountForBurn,
|
|
416
|
+
response: sender.address,
|
|
417
|
+
forwardPayload: forwardPayload,
|
|
418
|
+
queryId: createQueryId({ ...params, forwardPayload }),
|
|
419
|
+
};
|
|
420
|
+
return vaultAssetWalllet.createInternalBurn(value ?? fee, opts);
|
|
421
|
+
}
|
|
422
|
+
async getRFQAuctionAddress(strategyVaultAddress, index) {
|
|
423
|
+
const strategyVault = this.getByContract(strategy_vault_1.StrategyVault, strategyVaultAddress);
|
|
424
|
+
return await strategyVault.getRfqAddress(index);
|
|
425
|
+
}
|
|
426
|
+
async getRFQAuction(strategyVaultAddress, index) {
|
|
427
|
+
const strategyVault = this.getByContract(strategy_vault_1.StrategyVault, strategyVaultAddress);
|
|
428
|
+
const rfqAuction = this.getByContract(rfq_auction_1.RFQAuction, await strategyVault.getRfqAddress(index));
|
|
429
|
+
return rfqAuction;
|
|
430
|
+
}
|
|
431
|
+
async sendCreateAmountRFQ(sender, params, value) {
|
|
432
|
+
const strategyVault = this.getVault(params.strategyVaultAddress);
|
|
433
|
+
const vaultData = await strategyVault.getVaultData();
|
|
434
|
+
const rfqIndex = vaultData.rfqIndex;
|
|
435
|
+
const rfqAuctionAddress = await strategyVault.getRfqAddress(rfqIndex);
|
|
436
|
+
const sellAssetAddress = (0, utils_1.toAddress)(params.sellAssetAddress);
|
|
437
|
+
const buyAssetAddress = (0, utils_1.toAddress)(params.buyAssetAddress);
|
|
438
|
+
const sellAssetRFQWallet = await this.getByContract(jetton_minter_1.JettonMinter, sellAssetAddress).getWalletAddress(rfqAuctionAddress);
|
|
439
|
+
const buyAssetRFQWallet = await this.getByContract(jetton_minter_1.JettonMinter, buyAssetAddress).getWalletAddress(rfqAuctionAddress);
|
|
440
|
+
const oracleLibs = new oracle_1.OracleLibs(this.client, this.network);
|
|
441
|
+
const { oracleParams, bucket } = await oracleLibs.getOracleParamsForStrategyVaultV2(vaultData, [
|
|
442
|
+
...Object.keys(vaultData.assets),
|
|
443
|
+
]);
|
|
444
|
+
const oracleCalculateGas = BigInt(bucket.assets.size + bucket.pools.size + bucket.vaults.size) *
|
|
445
|
+
(0, core_1.toNano)(0.05);
|
|
446
|
+
await strategyVault.sendCreateRFQ(sender, value ??
|
|
447
|
+
vaultData.gasConfig[strategy_vault_1.StrategyVault.GasKey.ExecuteStrategy] +
|
|
448
|
+
vaultData.gasConfig[strategy_vault_1.StrategyVault.GasKey.CreateRFQ] +
|
|
449
|
+
oracleCalculateGas +
|
|
450
|
+
(0, core_1.toNano)(0.1), {
|
|
451
|
+
rfqIndex: rfqIndex,
|
|
452
|
+
sellAsset: sellAssetAddress,
|
|
453
|
+
buyAsset: buyAssetAddress,
|
|
454
|
+
sellAssetAmount: params.sellAssetAmount,
|
|
455
|
+
minBuyAssetValue: params.minBuyAssetAmount,
|
|
456
|
+
maxBuyAssetValue: params.maxBuyAssetAmount,
|
|
457
|
+
sellAssetRFQWallet,
|
|
458
|
+
buyAssetRFQWallet,
|
|
459
|
+
oracleParams,
|
|
460
|
+
period: params.period,
|
|
461
|
+
allowSellerCancel: params.allowSellerCancel,
|
|
462
|
+
allowBidderCancel: params.allowBidderCancel,
|
|
463
|
+
isAmountBid: true,
|
|
464
|
+
queryID: params.queryId,
|
|
465
|
+
});
|
|
466
|
+
}
|
|
467
|
+
async sendUnwrapWTON(sender, params, value) {
|
|
468
|
+
if (!(0, utils_1.toAddress)(params.wtonAddress).equals((0, utils_1.toAddress)(this.contracts.WTON))) {
|
|
469
|
+
throw new Error("WTON address is not valid");
|
|
470
|
+
}
|
|
471
|
+
const senderAddress = (0, utils_1.toAddress)(params.senderAddress);
|
|
472
|
+
const wtonAddress = (0, utils_1.toAddress)(params.wtonAddress);
|
|
473
|
+
}
|
|
474
|
+
async sendCreateSlippageRFQ(sender, params, value) {
|
|
475
|
+
const strategyVault = this.getVault(params.strategyVaultAddress);
|
|
476
|
+
const vaultData = await strategyVault.getVaultData();
|
|
477
|
+
const rfqIndex = vaultData.rfqIndex;
|
|
478
|
+
const rfqAuctionAddress = await strategyVault.getRfqAddress(rfqIndex);
|
|
479
|
+
const sellAssetAddress = (0, utils_1.toAddress)(params.sellAssetAddress);
|
|
480
|
+
const buyAssetAddress = (0, utils_1.toAddress)(params.buyAssetAddress);
|
|
481
|
+
const sellAssetRFQWallet = await this.getByContract(jetton_minter_1.JettonMinter, sellAssetAddress).getWalletAddress(rfqAuctionAddress);
|
|
482
|
+
const buyAssetRFQWallet = await this.getByContract(jetton_minter_1.JettonMinter, buyAssetAddress).getWalletAddress(rfqAuctionAddress);
|
|
483
|
+
const oracleLibs = new oracle_1.OracleLibs(this.client, this.network);
|
|
484
|
+
const { oracleParams, bucket } = await oracleLibs.getOracleParamsForStrategyVaultV2(vaultData, [
|
|
485
|
+
...Object.keys(vaultData.assets),
|
|
486
|
+
]);
|
|
487
|
+
const oracleCalculateGas = BigInt(bucket.assets.size + bucket.pools.size + bucket.vaults.size) *
|
|
488
|
+
(0, core_1.toNano)(0.05);
|
|
489
|
+
const slippageBoundConfig = toSlippageConfig(params.slippageBound);
|
|
490
|
+
await strategyVault.sendCreateRFQ(sender, value ??
|
|
491
|
+
vaultData.gasConfig[strategy_vault_1.StrategyVault.GasKey.ExecuteStrategy] +
|
|
492
|
+
vaultData.gasConfig[strategy_vault_1.StrategyVault.GasKey.CreateRFQ] +
|
|
493
|
+
oracleCalculateGas +
|
|
494
|
+
(0, core_1.toNano)(0.1), {
|
|
495
|
+
rfqIndex: rfqIndex,
|
|
496
|
+
sellAsset: sellAssetAddress,
|
|
497
|
+
buyAsset: buyAssetAddress,
|
|
498
|
+
sellAssetAmount: params.sellAssetAmount,
|
|
499
|
+
minBuyAssetValue: slippageBoundConfig.min,
|
|
500
|
+
maxBuyAssetValue: slippageBoundConfig.max,
|
|
501
|
+
sellAssetRFQWallet,
|
|
502
|
+
buyAssetRFQWallet,
|
|
503
|
+
oracleParams,
|
|
504
|
+
period: params.period,
|
|
505
|
+
allowSellerCancel: params.allowSellerCancel,
|
|
506
|
+
allowBidderCancel: params.allowBidderCancel,
|
|
507
|
+
isAmountBid: false,
|
|
508
|
+
queryID: params.queryId,
|
|
509
|
+
});
|
|
510
|
+
}
|
|
511
|
+
async sendCancelRFQ(sender, params, value) {
|
|
512
|
+
const strategyVault = this.getByContract(strategy_vault_1.StrategyVault, params.strategyVaultAddress);
|
|
513
|
+
await strategyVault.sendCancelRFQ(sender, value ?? (0, core_1.toNano)(0.05), {
|
|
514
|
+
rfqIndex: params.rfqIndex,
|
|
515
|
+
});
|
|
516
|
+
}
|
|
517
|
+
async sendSupplyToFactorial(sender, params, value) {
|
|
518
|
+
const strategyVault = this.getByContract(strategy_vault_1.StrategyVault, params.strategyVaultAddress);
|
|
519
|
+
const vaultData = await strategyVault.getVaultData();
|
|
520
|
+
await strategyVault.sendSupplyToFactorial(sender, value ??
|
|
521
|
+
vaultData.gasConfig[strategy_vault_1.StrategyVault.GasKey.FactorialTransferIn] +
|
|
522
|
+
vaultData.gasConfig[strategy_vault_1.StrategyVault.GasKey.ExecuteStrategy] +
|
|
523
|
+
(0, core_1.toNano)(0.1), {
|
|
524
|
+
pool: (0, utils_1.toAddress)(params.poolAddress),
|
|
525
|
+
asset: (0, utils_1.toAddress)(params.assetAddress),
|
|
526
|
+
amount: params.assetAmount,
|
|
527
|
+
response: sender.address,
|
|
528
|
+
});
|
|
529
|
+
}
|
|
530
|
+
async sendBorrowFromFactorial(sender, params, value) {
|
|
531
|
+
const strategyVault = this.getByContract(strategy_vault_1.StrategyVault, params.strategyVaultAddress);
|
|
532
|
+
const targetPool = this.getByContract(pool_1.Pool, params.poolAddress);
|
|
533
|
+
const oracleLibs = new oracle_1.OracleLibs(this.client, this.network);
|
|
534
|
+
const vaultData = await strategyVault.getVaultData();
|
|
535
|
+
const oraclePayload = await oracleLibs.getOracleParamsForStrategyVault(vaultData, [params.assetAddress.toString(), ...Object.keys(vaultData.assets)]);
|
|
536
|
+
const poolData = await targetPool.getPoolData();
|
|
537
|
+
const poolOraclePayload = await oracleLibs.getOracleParamsForPool(poolData, [params.assetAddress.toString()]);
|
|
538
|
+
const oracleCalculateGas = BigInt(Object.keys(vaultData.assets).length +
|
|
539
|
+
Object.keys(vaultData.factorialPools).length) * (0, core_1.toNano)(0.05);
|
|
540
|
+
await strategyVault.sendBorrowFromFactorial(sender, value ??
|
|
541
|
+
vaultData.gasConfig[strategy_vault_1.StrategyVault.GasKey.FactorialTransferOut] +
|
|
542
|
+
vaultData.gasConfig[strategy_vault_1.StrategyVault.GasKey.ExecuteStrategy] +
|
|
543
|
+
oracleCalculateGas +
|
|
544
|
+
(0, core_1.toNano)(0.1), {
|
|
545
|
+
oracleParams: oraclePayload,
|
|
546
|
+
poolOracleParams: poolOraclePayload,
|
|
547
|
+
pool: (0, utils_1.toAddress)(params.poolAddress),
|
|
548
|
+
asset: (0, utils_1.toAddress)(params.assetAddress),
|
|
549
|
+
amount: params.assetAmount,
|
|
550
|
+
isShare: params.isShare,
|
|
551
|
+
response: sender.address,
|
|
552
|
+
});
|
|
553
|
+
}
|
|
554
|
+
async sendRepayToFactorial(sender, params, value) {
|
|
555
|
+
const strategyVault = this.getByContract(strategy_vault_1.StrategyVault, params.strategyVaultAddress);
|
|
556
|
+
const targetPool = this.getByContract(pool_1.Pool, params.poolAddress);
|
|
557
|
+
const vaultData = await strategyVault.getVaultData();
|
|
558
|
+
const poolData = await targetPool.getPoolData();
|
|
559
|
+
const oracleLibs = new oracle_1.OracleLibs(this.client, this.network);
|
|
560
|
+
const poolOraclePayload = await oracleLibs.getOracleParamsForPool(poolData, [params.assetAddress.toString()]);
|
|
561
|
+
await strategyVault.sendRepayToFactorial(sender, value ??
|
|
562
|
+
vaultData.gasConfig[strategy_vault_1.StrategyVault.GasKey.FactorialTransferIn] +
|
|
563
|
+
vaultData.gasConfig[strategy_vault_1.StrategyVault.GasKey.ExecuteStrategy] +
|
|
564
|
+
(0, core_1.toNano)(0.1), {
|
|
565
|
+
pool: (0, utils_1.toAddress)(params.poolAddress),
|
|
566
|
+
asset: (0, utils_1.toAddress)(params.assetAddress),
|
|
567
|
+
amount: params.assetAmount,
|
|
568
|
+
response: sender.address,
|
|
569
|
+
});
|
|
570
|
+
}
|
|
571
|
+
async sendWithdrawFromFactorial(sender, params, value) {
|
|
572
|
+
const strategyVault = this.getByContract(strategy_vault_1.StrategyVault, params.strategyVaultAddress);
|
|
573
|
+
const pool = this.getByContract(pool_1.Pool, params.poolAddress);
|
|
574
|
+
const oracleLibs = new oracle_1.OracleLibs(this.client, this.network);
|
|
575
|
+
const vaultData = await strategyVault.getVaultData();
|
|
576
|
+
const poolData = await pool.getPoolData();
|
|
577
|
+
const poolOraclePayload = await oracleLibs.getOracleParamsForPool(poolData, [params.assetAddress.toString()]);
|
|
578
|
+
await strategyVault.sendWithdrawFromFactorial(sender, value ??
|
|
579
|
+
vaultData.gasConfig[strategy_vault_1.StrategyVault.GasKey.FactorialTransferOut] +
|
|
580
|
+
vaultData.gasConfig[strategy_vault_1.StrategyVault.GasKey.ExecuteStrategy] +
|
|
581
|
+
(0, core_1.toNano)(0.1), {
|
|
582
|
+
oracleParams: poolOraclePayload,
|
|
583
|
+
pool: (0, utils_1.toAddress)(params.poolAddress),
|
|
584
|
+
asset: (0, utils_1.toAddress)(params.assetAddress),
|
|
585
|
+
amount: params.assetAmount,
|
|
586
|
+
isShare: params.isShare,
|
|
587
|
+
response: sender.address,
|
|
588
|
+
});
|
|
589
|
+
}
|
|
590
|
+
async sendLiquidateToFactorial(sender, params, value) {
|
|
591
|
+
const strategyVault = this.getByContract(strategy_vault_1.StrategyVault, params.strategyVaultAddress);
|
|
592
|
+
const vaultData = await strategyVault.getVaultData();
|
|
593
|
+
const oraclePayload = await this.getOracleParamsV2(params.strategyVaultAddress);
|
|
594
|
+
const oracleCalculateGas = BigInt(Object.keys(vaultData.assets).length +
|
|
595
|
+
Object.keys(vaultData.factorialPools).length) * (0, core_1.toNano)(0.05);
|
|
596
|
+
await strategyVault.sendLiquidateToFactorial(sender, value ??
|
|
597
|
+
vaultData.gasConfig[strategy_vault_1.StrategyVault.GasKey.FactorialLiquidate] +
|
|
598
|
+
vaultData.gasConfig[strategy_vault_1.StrategyVault.GasKey.ExecuteStrategy] +
|
|
599
|
+
oracleCalculateGas +
|
|
600
|
+
(0, core_1.toNano)(0.1), {
|
|
601
|
+
oracleParams: oraclePayload,
|
|
602
|
+
pool: (0, utils_1.toAddress)(params.poolAddress),
|
|
603
|
+
borrower: (0, utils_1.toAddress)(params.borrowerAddress),
|
|
604
|
+
repayAsset: (0, utils_1.toAddress)(params.repayAssetAddress),
|
|
605
|
+
seizeAsset: (0, utils_1.toAddress)(params.seizeAssetAddress),
|
|
606
|
+
repayAmount: params.repayAmount,
|
|
607
|
+
response: sender.address,
|
|
608
|
+
});
|
|
609
|
+
}
|
|
610
|
+
async splitPureAndVaultAsset(addresses) {
|
|
611
|
+
const pureAssetAddresses = [];
|
|
612
|
+
const vaultAssetAddresses = [];
|
|
613
|
+
await Promise.all(addresses.map(utils_1.toAddress).map(async (assetAddress) => {
|
|
614
|
+
const address = assetAddress.toString();
|
|
615
|
+
if (await this.isVaultAsset(address)) {
|
|
616
|
+
vaultAssetAddresses.push(address);
|
|
617
|
+
}
|
|
618
|
+
else {
|
|
619
|
+
pureAssetAddresses.push(address);
|
|
620
|
+
}
|
|
621
|
+
}));
|
|
622
|
+
return {
|
|
623
|
+
vaultAssetAddresses,
|
|
624
|
+
pureAssetAddresses,
|
|
625
|
+
};
|
|
626
|
+
}
|
|
627
|
+
async isVaultAsset(address) {
|
|
628
|
+
try {
|
|
629
|
+
await this.client.runMethod((0, utils_1.toAddress)(address), "get_jetton_data"); // TODO: Network Error?
|
|
630
|
+
return false;
|
|
631
|
+
}
|
|
632
|
+
catch {
|
|
633
|
+
return true;
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
async getAssetPrices(strategyVaultAddress, vaultData) {
|
|
637
|
+
const strategyVault = this.getByContract(strategy_vault_1.StrategyVault, strategyVaultAddress);
|
|
638
|
+
vaultData = vaultData ?? (await strategyVault.getVaultData());
|
|
639
|
+
const monitor = new monitorCacheV1_1.MonitorCacheV1(this.client, this.network);
|
|
640
|
+
const allUnderlyingAssetAddresses = [
|
|
641
|
+
...new Set(Object.keys(vaultData.assets)),
|
|
642
|
+
];
|
|
643
|
+
const { pureAssetAddresses, vaultAssetAddresses } = await this.splitPureAndVaultAsset(allUnderlyingAssetAddresses);
|
|
644
|
+
const assetPrices = await monitor.getPricesNew(pureAssetAddresses, vaultData.oracleConfig);
|
|
645
|
+
await Promise.all(vaultAssetAddresses.map(async (vaultAssetAddress) => {
|
|
646
|
+
assetPrices[vaultAssetAddress] = {
|
|
647
|
+
type: type_1.AssetType.VaultAsset,
|
|
648
|
+
timestamp: Date.now(),
|
|
649
|
+
price: await this.getPrice(vaultAssetAddress),
|
|
650
|
+
};
|
|
651
|
+
}));
|
|
652
|
+
return assetPrices;
|
|
653
|
+
}
|
|
654
|
+
async getPrice(strategyVaultAddress, vaultData) {
|
|
655
|
+
const strategyVault = this.getByContract(strategy_vault_1.StrategyVault, strategyVaultAddress);
|
|
656
|
+
vaultData = vaultData ?? (await strategyVault.getVaultData());
|
|
657
|
+
const { totalSupply } = vaultData;
|
|
658
|
+
const vaultTokenPrice = totalSupply === 0n
|
|
659
|
+
? 0n
|
|
660
|
+
: (await this.getTotalValue(strategyVaultAddress, vaultData)) /
|
|
661
|
+
totalSupply;
|
|
662
|
+
return vaultTokenPrice;
|
|
663
|
+
}
|
|
664
|
+
async getPriceV2(strategyVaultAddress, vaultData) {
|
|
665
|
+
const strategyVault = this.getByContract(strategy_vault_1.StrategyVault, strategyVaultAddress);
|
|
666
|
+
vaultData = vaultData ?? (await strategyVault.getVaultData());
|
|
667
|
+
const vault = this.open(strategy_vault_1.StrategyVault.createFromAddress((0, utils_1.toAddress)(strategyVaultAddress)));
|
|
668
|
+
const _assets = Object.keys(vaultData.assets);
|
|
669
|
+
const oracleConfig = vaultData.oracleConfig;
|
|
670
|
+
let { assets, vaultAssets } = await this.separateAssets(_assets, oracleConfig);
|
|
671
|
+
const vaultPositions = await this.getParsedVaults([
|
|
672
|
+
...vaultAssets,
|
|
673
|
+
(0, utils_1.toAddress)(strategyVaultAddress),
|
|
674
|
+
]);
|
|
675
|
+
const exposures = await this.getExposures(vaultPositions);
|
|
676
|
+
const exposurePools = exposures.pools;
|
|
677
|
+
assets = [...new Set([...assets, ...exposures.assets])];
|
|
678
|
+
const { redstonePrices, onchainData: od } = await this.getRedstonePrices(assets, oracleConfig);
|
|
679
|
+
const onchainData2 = await this.updateOnchainData(oracleConfig.assetOnchainDataInfo);
|
|
680
|
+
const onchainData = { ...od, ...onchainData2 };
|
|
681
|
+
const prices = this.calculatePrices(redstonePrices, onchainData);
|
|
682
|
+
const exposurePoolsData = {};
|
|
683
|
+
for (let exposurePool of exposurePools) {
|
|
684
|
+
const pool = this.open(pool_1.Pool.createFromAddress((0, utils_1.toAddress)(exposurePool)));
|
|
685
|
+
const poolData = await pool.getPoolData();
|
|
686
|
+
exposurePoolsData[exposurePool] = poolData;
|
|
687
|
+
}
|
|
688
|
+
const vaultPrices = await this.calculateVaultPrices(prices, exposurePoolsData, vaultPositions);
|
|
689
|
+
return vaultPrices[(0, utils_1.toAddress)(strategyVaultAddress).toString()] ?? 0n;
|
|
690
|
+
}
|
|
691
|
+
async getAssetPricesV2(vaultAddress) {
|
|
692
|
+
const vault = this.open(strategy_vault_1.StrategyVault.createFromAddress((0, utils_1.toAddress)(vaultAddress)));
|
|
693
|
+
const vaultData = await vault.getVaultData();
|
|
694
|
+
const _assets = Object.keys(vaultData.assets);
|
|
695
|
+
const oracleConfig = vaultData.oracleConfig;
|
|
696
|
+
let { assets, vaultAssets } = await this.separateAssets(_assets, oracleConfig);
|
|
697
|
+
const vaultPositions = await this.getParsedVaults([...vaultAssets]);
|
|
698
|
+
const exposures = await this.getExposures(vaultPositions);
|
|
699
|
+
const exposurePools = exposures.pools;
|
|
700
|
+
assets = [...new Set([...assets, ...exposures.assets])];
|
|
701
|
+
const { redstonePrices, onchainData: od } = await this.getRedstonePrices(assets, oracleConfig);
|
|
702
|
+
const onchainData2 = await this.updateOnchainData(oracleConfig.assetOnchainDataInfo);
|
|
703
|
+
const onchainData = { ...od, ...onchainData2 };
|
|
704
|
+
const prices = this.calculatePrices(redstonePrices, onchainData);
|
|
705
|
+
const exposurePoolsData = {};
|
|
706
|
+
for (let exposurePool of exposurePools) {
|
|
707
|
+
const pool = this.open(pool_1.Pool.createFromAddress((0, utils_1.toAddress)(exposurePool)));
|
|
708
|
+
const poolData = await pool.getPoolData();
|
|
709
|
+
exposurePoolsData[exposurePool] = poolData;
|
|
710
|
+
}
|
|
711
|
+
const vaultPrices = await this.calculateVaultPrices(prices, exposurePoolsData, vaultPositions);
|
|
712
|
+
return {
|
|
713
|
+
...prices,
|
|
714
|
+
...vaultPrices,
|
|
715
|
+
};
|
|
716
|
+
}
|
|
717
|
+
async calculateVaultPrices(price, poolInfos, vaults) {
|
|
718
|
+
const vaultPrices = {};
|
|
719
|
+
while (Object.keys(vaults).length > 0) {
|
|
720
|
+
const vaultAddresses = Object.keys(vaults);
|
|
721
|
+
for (let vaultAddress of vaultAddresses) {
|
|
722
|
+
try {
|
|
723
|
+
const amountVaultPosition = shareToAmount(vaults[vaultAddress], poolInfos);
|
|
724
|
+
let totalValue = 0n;
|
|
725
|
+
for (const assetAddress of Object.keys(amountVaultPosition.assetAmounts)) {
|
|
726
|
+
const assetPrice = price[assetAddress]?.price || vaultPrices[assetAddress].price;
|
|
727
|
+
if (!assetPrice)
|
|
728
|
+
throw "error";
|
|
729
|
+
totalValue +=
|
|
730
|
+
amountVaultPosition.assetAmounts[assetAddress] * assetPrice;
|
|
731
|
+
}
|
|
732
|
+
if (totalValue === 0n || amountVaultPosition.totalSupply === 0n) {
|
|
733
|
+
vaultPrices[vaultAddress] = {
|
|
734
|
+
type: 10,
|
|
735
|
+
price: 0n,
|
|
736
|
+
additionalData: {
|
|
737
|
+
decomposed: {},
|
|
738
|
+
totalSupply: 0n,
|
|
739
|
+
},
|
|
740
|
+
};
|
|
741
|
+
}
|
|
742
|
+
else {
|
|
743
|
+
vaultPrices[vaultAddress] = {
|
|
744
|
+
type: 10,
|
|
745
|
+
price: totalValue / amountVaultPosition.totalSupply,
|
|
746
|
+
additionalData: amountVaultPosition.additionalData,
|
|
747
|
+
};
|
|
748
|
+
}
|
|
749
|
+
delete vaults[vaultAddress];
|
|
750
|
+
}
|
|
751
|
+
catch (e) { }
|
|
752
|
+
}
|
|
753
|
+
}
|
|
754
|
+
return vaultPrices;
|
|
755
|
+
}
|
|
756
|
+
async getParsedVaults(vaultAddresses) {
|
|
757
|
+
const decomposedVault = {};
|
|
758
|
+
for (let vaultAddress of vaultAddresses) {
|
|
759
|
+
const state = await this.getByContract(unknown_contract_1.UnknownContract, vaultAddress).getState();
|
|
760
|
+
if (state.state.type === "active" && state.state.data) {
|
|
761
|
+
const data = core_1.Cell.fromBoc(state.state.data)[0];
|
|
762
|
+
try {
|
|
763
|
+
const vaultData = strategy_vault_1.StrategyVault.parseVaultData(data);
|
|
764
|
+
const vaultDataa = {
|
|
765
|
+
totalSupply: vaultData.totalSupply,
|
|
766
|
+
assets: Object.keys(vaultData.assets).reduce((acc, asset) => {
|
|
767
|
+
acc[asset] = vaultData.assets[asset].cash;
|
|
768
|
+
return acc;
|
|
769
|
+
}, {}),
|
|
770
|
+
poolPositions: Object.keys(vaultData.factorialPools).reduce((acc, pool) => {
|
|
771
|
+
acc[pool] = Object.keys(vaultData.factorialPools[pool]).reduce((acc, asset) => {
|
|
772
|
+
acc[asset] = {
|
|
773
|
+
supply: vaultData.factorialPools[pool][asset].supply,
|
|
774
|
+
borrow: vaultData.factorialPools[pool][asset].borrow,
|
|
775
|
+
};
|
|
776
|
+
return acc;
|
|
777
|
+
}, {});
|
|
778
|
+
return acc;
|
|
779
|
+
}, {}),
|
|
780
|
+
};
|
|
781
|
+
decomposedVault[vaultAddress.toString()] = vaultDataa;
|
|
782
|
+
}
|
|
783
|
+
catch (e) {
|
|
784
|
+
const vaultData = __1.ShareVault.parseVaultData(data);
|
|
785
|
+
const vaultDataa = {
|
|
786
|
+
totalSupply: vaultData.totalSupply,
|
|
787
|
+
assets: {
|
|
788
|
+
[vaultData.asset.toString()]: vaultData.cash,
|
|
789
|
+
},
|
|
790
|
+
poolPositions: Object.values(vaultData.whitelistedPools).reduce((acc, pool) => {
|
|
791
|
+
acc[pool.address.toString()] = {
|
|
792
|
+
[vaultData.asset.toString()]: {
|
|
793
|
+
supply: pool.supply,
|
|
794
|
+
borrow: 0n,
|
|
795
|
+
},
|
|
796
|
+
};
|
|
797
|
+
return acc;
|
|
798
|
+
}, {}),
|
|
799
|
+
};
|
|
800
|
+
decomposedVault[vaultAddress.toString()] = vaultDataa;
|
|
801
|
+
}
|
|
802
|
+
}
|
|
803
|
+
}
|
|
804
|
+
return decomposedVault;
|
|
805
|
+
}
|
|
806
|
+
async getExposures(vaults) {
|
|
807
|
+
const assets = new Set();
|
|
808
|
+
const pools = new Set();
|
|
809
|
+
for (let vault of Object.values(vaults)) {
|
|
810
|
+
const _assets = vault.assets;
|
|
811
|
+
for (let asset of Object.keys(_assets)) {
|
|
812
|
+
const position = vault.assets[asset];
|
|
813
|
+
if (position > 0n) {
|
|
814
|
+
assets.add(asset);
|
|
815
|
+
}
|
|
816
|
+
}
|
|
817
|
+
for (let pool of Object.keys(vault.poolPositions)) {
|
|
818
|
+
const _assets = vault.poolPositions[pool];
|
|
819
|
+
for (let asset of Object.keys(_assets)) {
|
|
820
|
+
const position = vault.poolPositions[pool][asset];
|
|
821
|
+
if (position.supply > 0n || position.borrow > 0n) {
|
|
822
|
+
pools.add(pool);
|
|
823
|
+
assets.add(asset);
|
|
824
|
+
}
|
|
825
|
+
}
|
|
826
|
+
}
|
|
827
|
+
}
|
|
828
|
+
const result = { pools: [...pools], assets: [...assets] };
|
|
829
|
+
return result;
|
|
830
|
+
}
|
|
831
|
+
calculatePrices(_prices, oracleData) {
|
|
832
|
+
const prices = { ..._prices };
|
|
833
|
+
let oracles = Object.entries(oracleData);
|
|
834
|
+
while (oracles.length !== 0) {
|
|
835
|
+
const restOracles = [];
|
|
836
|
+
for (let [asset, onchainDataInfo] of oracles) {
|
|
837
|
+
if (!onchainDataInfo)
|
|
838
|
+
throw "not found onchain data info1";
|
|
839
|
+
if (onchainDataInfo.assetType === type_1.AssetType.SingleAsset) {
|
|
840
|
+
if (prices[onchainDataInfo.underlyingAddress.toString()]) {
|
|
841
|
+
const underlyingPriceInfo = prices[onchainDataInfo.underlyingAddress.toString()];
|
|
842
|
+
prices[asset.toString()] = {
|
|
843
|
+
type: type_1.AssetType.SingleAsset,
|
|
844
|
+
price: (underlyingPriceInfo.price *
|
|
845
|
+
onchainDataInfo.underlyingReserve) /
|
|
846
|
+
onchainDataInfo.totalSupply,
|
|
847
|
+
};
|
|
848
|
+
}
|
|
849
|
+
else {
|
|
850
|
+
restOracles.push([asset, onchainDataInfo]);
|
|
851
|
+
}
|
|
852
|
+
}
|
|
853
|
+
else if (onchainDataInfo.assetType === type_1.AssetType.MultiAsset) {
|
|
854
|
+
if (prices[onchainDataInfo.underlying0Address.toString()] &&
|
|
855
|
+
prices[onchainDataInfo.underlying1Address.toString()]) {
|
|
856
|
+
const underlying0PriceInfo = prices[onchainDataInfo.underlying0Address.toString()];
|
|
857
|
+
const underlying1PriceInfo = prices[onchainDataInfo.underlying1Address.toString()];
|
|
858
|
+
const price0 = (underlying0PriceInfo.price *
|
|
859
|
+
onchainDataInfo.underlying0Reserve) /
|
|
860
|
+
onchainDataInfo.totalSupply;
|
|
861
|
+
const price1 = (underlying1PriceInfo.price *
|
|
862
|
+
onchainDataInfo.underlying1Reserve) /
|
|
863
|
+
onchainDataInfo.totalSupply;
|
|
864
|
+
prices[asset.toString()] = {
|
|
865
|
+
type: type_1.AssetType.MultiAsset,
|
|
866
|
+
price: price0 + price1,
|
|
867
|
+
};
|
|
868
|
+
}
|
|
869
|
+
else {
|
|
870
|
+
restOracles.push([asset, onchainDataInfo]);
|
|
871
|
+
}
|
|
872
|
+
}
|
|
873
|
+
else if (onchainDataInfo.assetType === type_1.AssetType.Native) {
|
|
874
|
+
}
|
|
875
|
+
else {
|
|
876
|
+
throw `invalid asset type ${onchainDataInfo.assetType}`;
|
|
877
|
+
}
|
|
878
|
+
}
|
|
879
|
+
oracles = restOracles;
|
|
880
|
+
}
|
|
881
|
+
return prices;
|
|
882
|
+
}
|
|
883
|
+
async updateOnchainData(onchainDataInfo) {
|
|
884
|
+
for (let [asset, data] of Object.entries(onchainDataInfo)) {
|
|
885
|
+
if (data.sourceType === redstone_onchain_oracle_1.RedstoneOnchainOracle.SourceType.tonstakers &&
|
|
886
|
+
"underlyingAddress" in data) {
|
|
887
|
+
const result = await this.client
|
|
888
|
+
.provider(core_1.Address.parse(data.sourceAddress.toString()))
|
|
889
|
+
.get("get_pool_state", []);
|
|
890
|
+
result.stack.readBigNumber();
|
|
891
|
+
data.underlyingReserve = result.stack.readBigNumber();
|
|
892
|
+
data.totalSupply = result.stack.readBigNumber();
|
|
893
|
+
}
|
|
894
|
+
else if (data.sourceType === redstone_onchain_oracle_1.RedstoneOnchainOracle.SourceType.dedust &&
|
|
895
|
+
data.assetType === type_1.AssetType.MultiAsset) {
|
|
896
|
+
if (this.network === "mainnet") {
|
|
897
|
+
const results = await Promise.all([
|
|
898
|
+
this.client
|
|
899
|
+
.provider(core_1.Address.parse(data.sourceAddress.toString()))
|
|
900
|
+
.get("get_reserves", []),
|
|
901
|
+
this.client
|
|
902
|
+
.provider(core_1.Address.parse(data.sourceAddress.toString()))
|
|
903
|
+
.get("get_jetton_data", []),
|
|
904
|
+
]);
|
|
905
|
+
data.underlying0Reserve = results[0].stack.readBigNumber();
|
|
906
|
+
data.underlying1Reserve = results[0].stack.readBigNumber();
|
|
907
|
+
data.totalSupply = results[1].stack.readBigNumber();
|
|
908
|
+
}
|
|
909
|
+
else {
|
|
910
|
+
const result = await this.client
|
|
911
|
+
.provider(core_1.Address.parse(data.sourceAddress.toString()))
|
|
912
|
+
.get("get_pool_state", []);
|
|
913
|
+
result.stack.readBigNumber();
|
|
914
|
+
data.underlying0Reserve = result.stack.readBigNumber();
|
|
915
|
+
data.underlying1Reserve = result.stack.readBigNumber();
|
|
916
|
+
data.totalSupply = result.stack.readBigNumber();
|
|
917
|
+
}
|
|
918
|
+
}
|
|
919
|
+
else if (data.sourceType === redstone_onchain_oracle_1.RedstoneOnchainOracle.SourceType.stonfi &&
|
|
920
|
+
data.assetType === type_1.AssetType.MultiAsset) {
|
|
921
|
+
if (this.network === "mainnet") {
|
|
922
|
+
const result = await this.client
|
|
923
|
+
.provider(core_1.Address.parse(data.sourceAddress.toString()))
|
|
924
|
+
.get("get_pool_data", []);
|
|
925
|
+
result.stack.readNumber();
|
|
926
|
+
result.stack.readAddress();
|
|
927
|
+
data.totalSupply = result.stack.readBigNumber();
|
|
928
|
+
data.underlying0Reserve = result.stack.readBigNumber();
|
|
929
|
+
data.underlying1Reserve = result.stack.readBigNumber();
|
|
930
|
+
}
|
|
931
|
+
else {
|
|
932
|
+
const result = await this.client
|
|
933
|
+
.provider(core_1.Address.parse(data.sourceAddress.toString()))
|
|
934
|
+
.get("get_pool_state", []);
|
|
935
|
+
result.stack.readBigNumber();
|
|
936
|
+
data.underlying0Reserve = result.stack.readBigNumber();
|
|
937
|
+
data.underlying1Reserve = result.stack.readBigNumber();
|
|
938
|
+
data.totalSupply = result.stack.readBigNumber();
|
|
939
|
+
}
|
|
940
|
+
}
|
|
941
|
+
else if (data.sourceType === redstone_onchain_oracle_1.RedstoneOnchainOracle.SourceType.storm &&
|
|
942
|
+
data.assetType === type_1.AssetType.SingleAsset) {
|
|
943
|
+
if (this.network === "mainnet") {
|
|
944
|
+
const results = await this.client
|
|
945
|
+
.provider(core_1.Address.parse(data.sourceAddress.toString()))
|
|
946
|
+
.get("get_vault_data", []);
|
|
947
|
+
const jettonWalletAddress = results.stack.readAddress();
|
|
948
|
+
const lpRate = results.stack.readBigNumber();
|
|
949
|
+
const lpTotalSupply = results.stack.readBigNumber();
|
|
950
|
+
const freeBalance = results.stack.readBigNumber();
|
|
951
|
+
const lockedBalance = results.stack.readBigNumber();
|
|
952
|
+
const withdrawLockedBalance = results.stack.readBigNumber();
|
|
953
|
+
const stakersBalance = results.stack.readBigNumber();
|
|
954
|
+
const executorsBalance = results.stack.readBigNumber();
|
|
955
|
+
data.underlyingReserve = lpRate;
|
|
956
|
+
if (!data.totalSupply) {
|
|
957
|
+
data.totalSupply = BigInt(1e9);
|
|
958
|
+
}
|
|
959
|
+
}
|
|
960
|
+
else {
|
|
961
|
+
}
|
|
962
|
+
}
|
|
963
|
+
else if (data.sourceType === redstone_onchain_oracle_1.RedstoneOnchainOracle.SourceType.tradoor &&
|
|
964
|
+
data.assetType === type_1.AssetType.SingleAsset) {
|
|
965
|
+
if (this.network === "mainnet") {
|
|
966
|
+
const results = await this.client
|
|
967
|
+
.provider(core_1.Address.parse(data.sourceAddress.toString()))
|
|
968
|
+
.get("tlpPrice", []);
|
|
969
|
+
const lpRate = results.stack.readBigNumber();
|
|
970
|
+
data.underlyingReserve = lpRate;
|
|
971
|
+
if (!data.totalSupply) {
|
|
972
|
+
data.totalSupply = BigInt(18e9);
|
|
973
|
+
}
|
|
974
|
+
}
|
|
975
|
+
}
|
|
976
|
+
}
|
|
977
|
+
return onchainDataInfo;
|
|
978
|
+
}
|
|
979
|
+
async getRedstonePrices(assets, oracleConfig) {
|
|
980
|
+
const result = redstone_onchain_oracle_1.RedstoneOnchainOracle.getOracleRequestParams(assets, oracleConfig);
|
|
981
|
+
const redstonePrices = await readonlyCachedRedstone_1.ReadonlyCachedRedstone.getPrices(result.redstoneAssetNames);
|
|
982
|
+
const prices = {};
|
|
983
|
+
const onchainData = {};
|
|
984
|
+
redstonePrices.forEach((item) => {
|
|
985
|
+
try {
|
|
986
|
+
const rainfo = oracleConfig.redstoneAssetInfo[item.feedId.toString()];
|
|
987
|
+
if (rainfo.assetType === 0) {
|
|
988
|
+
prices[rainfo.address.toString()] = {
|
|
989
|
+
type: type_1.AssetType.Native,
|
|
990
|
+
timestamp: item.timestamp,
|
|
991
|
+
price: (item.price * BigInt(1e8)) / rainfo.precision,
|
|
992
|
+
};
|
|
993
|
+
}
|
|
994
|
+
else if (rainfo.assetType === 1) {
|
|
995
|
+
onchainData[rainfo.address.toString()] = {
|
|
996
|
+
assetType: type_1.AssetType.SingleAsset,
|
|
997
|
+
underlyingAddress: rainfo.underlyingAddress,
|
|
998
|
+
underlyingReserve: item.price,
|
|
999
|
+
totalSupply: rainfo.precision,
|
|
1000
|
+
};
|
|
1001
|
+
}
|
|
1002
|
+
}
|
|
1003
|
+
catch (e) { }
|
|
1004
|
+
});
|
|
1005
|
+
return { redstonePrices: prices, onchainData };
|
|
1006
|
+
}
|
|
1007
|
+
async separateAssets(_assets, oracleConfig) {
|
|
1008
|
+
const noVaultAssets = [
|
|
1009
|
+
...Object.values(oracleConfig.redstoneAssetInfo).map((item) => item.address.toString()),
|
|
1010
|
+
...Object.keys(oracleConfig.assetOnchainDataInfo).map((item) => item),
|
|
1011
|
+
];
|
|
1012
|
+
const assets = [];
|
|
1013
|
+
const vaultAssets = [];
|
|
1014
|
+
for (const asset of _assets) {
|
|
1015
|
+
if (noVaultAssets.includes(asset.toString())) {
|
|
1016
|
+
assets.push(asset);
|
|
1017
|
+
}
|
|
1018
|
+
else {
|
|
1019
|
+
vaultAssets.push(asset);
|
|
1020
|
+
}
|
|
1021
|
+
}
|
|
1022
|
+
return {
|
|
1023
|
+
assets,
|
|
1024
|
+
vaultAssets,
|
|
1025
|
+
};
|
|
1026
|
+
}
|
|
1027
|
+
async sendCollectProtocolFee(sender, params, value) {
|
|
1028
|
+
const strategyVault = this.getVault(params.strategyVaultAddress);
|
|
1029
|
+
await strategyVault.sendClaimProtocolFee(sender, value ?? (0, core_1.toNano)(0.1), {
|
|
1030
|
+
receiverAddress: (0, utils_1.toAddress)(params.assetAddress),
|
|
1031
|
+
queryID: params.queryId,
|
|
1032
|
+
});
|
|
1033
|
+
}
|
|
1034
|
+
async sendClaimManagementFee(sender, params, value) {
|
|
1035
|
+
const strategyVault = this.getVault(params.strategyVaultAddress);
|
|
1036
|
+
await strategyVault.sendClaimManagementFee(sender, value ?? (0, core_1.toNano)(0.1), {
|
|
1037
|
+
receiverAddress: (0, utils_1.toAddress)(params.receiverAddress),
|
|
1038
|
+
queryID: params.queryId,
|
|
1039
|
+
});
|
|
1040
|
+
}
|
|
1041
|
+
async sendSetManagementFeeRatePerYear(sender, params, value) {
|
|
1042
|
+
const strategyVault = this.getVault(params.strategyVaultAddress);
|
|
1043
|
+
await strategyVault.sendSetManagementFeeRatePerYear(sender, value ?? (0, core_1.toNano)(0.1), {
|
|
1044
|
+
managementFeeRate: params.managementFeeRatePerYear,
|
|
1045
|
+
queryID: params.queryId,
|
|
1046
|
+
});
|
|
1047
|
+
}
|
|
1048
|
+
async sendSetDepositCloseTimestamp(sender, params, value) {
|
|
1049
|
+
const strategyVault = this.getVault(params.strategyVaultAddress);
|
|
1050
|
+
await strategyVault.sendSetDepositCloseTimestamp(sender, value ?? (0, core_1.toNano)(0.1), {
|
|
1051
|
+
depositCloseTimestamp: params.depositCloseTimestamp,
|
|
1052
|
+
queryID: params.queryId,
|
|
1053
|
+
});
|
|
1054
|
+
}
|
|
1055
|
+
async sendSetWithdrawOpenTimestamp(sender, params, value) {
|
|
1056
|
+
const strategyVault = this.getVault(params.strategyVaultAddress);
|
|
1057
|
+
await strategyVault.sendSetWithdrawOpenTimestamp(sender, value ?? (0, core_1.toNano)(0.1), {
|
|
1058
|
+
withdrawOpenTimestamp: params.withdrawOpenTimestamp,
|
|
1059
|
+
queryID: params.queryId,
|
|
1060
|
+
});
|
|
1061
|
+
}
|
|
1062
|
+
async sendSetGasConfig(sender, params, value) {
|
|
1063
|
+
const strategyVault = this.getVault(params.strategyVaultAddress);
|
|
1064
|
+
await strategyVault.sendSetGasConfig(sender, value ?? (0, core_1.toNano)(0.1), {
|
|
1065
|
+
gasConfig: params.gasConfig,
|
|
1066
|
+
queryID: params.queryId,
|
|
1067
|
+
});
|
|
1068
|
+
}
|
|
1069
|
+
async sendSetOracleConfig(sender, params, value) {
|
|
1070
|
+
const strategyVault = this.getVault(params.strategyVaultAddress);
|
|
1071
|
+
await strategyVault.sendSetOracleConfig(sender, value ?? (0, core_1.toNano)(0.1), {
|
|
1072
|
+
oracleConfig: params.oracleConfig,
|
|
1073
|
+
queryID: params.queryId,
|
|
1074
|
+
});
|
|
1075
|
+
}
|
|
1076
|
+
async sendSetWalletCode(sender, params, value) {
|
|
1077
|
+
const strategyVault = this.getVault(params.strategyVaultAddress);
|
|
1078
|
+
await strategyVault.sendSetWalletCode(sender, value ?? (0, core_1.toNano)(0.1), {
|
|
1079
|
+
walletCode: params.walletCode,
|
|
1080
|
+
queryID: params.queryId,
|
|
1081
|
+
});
|
|
1082
|
+
}
|
|
1083
|
+
async sendSetDataAggregatorCode(sender, params, value) {
|
|
1084
|
+
const strategyVault = this.getVault(params.strategyVaultAddress);
|
|
1085
|
+
await strategyVault.sendSetDataAggregatorCode(sender, value ?? (0, core_1.toNano)(0.1), {
|
|
1086
|
+
dataAggregatorCode: params.dataAggregatorCode,
|
|
1087
|
+
queryID: params.queryId,
|
|
1088
|
+
});
|
|
1089
|
+
}
|
|
1090
|
+
async sendSetRFQCode(sender, params, value) {
|
|
1091
|
+
const strategyVault = this.getVault(params.strategyVaultAddress);
|
|
1092
|
+
await strategyVault.sendSetRFQCode(sender, value ?? (0, core_1.toNano)(0.1), {
|
|
1093
|
+
rfqCode: params.rfqCode,
|
|
1094
|
+
queryID: params.queryId,
|
|
1095
|
+
});
|
|
1096
|
+
}
|
|
1097
|
+
async sendSetContent(sender, params, value) {
|
|
1098
|
+
const strategyVault = this.getVault(params.strategyVaultAddress);
|
|
1099
|
+
await strategyVault.sendSetContent(sender, value ?? (0, core_1.toNano)(0.1), {
|
|
1100
|
+
content: params.content,
|
|
1101
|
+
queryID: params.queryId,
|
|
1102
|
+
});
|
|
1103
|
+
}
|
|
1104
|
+
async sendUpgradeCode(sender, params, value) {
|
|
1105
|
+
const strategyVault = this.getVault(params.strategyVaultAddress);
|
|
1106
|
+
await strategyVault.sendUpgradeCode(sender, value ?? (0, core_1.toNano)(0.1), {
|
|
1107
|
+
newCode: params.newCode,
|
|
1108
|
+
queryID: params.queryId,
|
|
1109
|
+
});
|
|
1110
|
+
}
|
|
1111
|
+
async getTotalValue(strategyVaultAddress, vaultData) {
|
|
1112
|
+
const strategyVault = this.getByContract(strategy_vault_1.StrategyVault, strategyVaultAddress);
|
|
1113
|
+
vaultData = vaultData ?? (await strategyVault.getVaultData());
|
|
1114
|
+
const monitor = new monitorCacheV1_1.MonitorCacheV1(this.client, this.network);
|
|
1115
|
+
const allRelatedUnderlyingAssetAddresses = [
|
|
1116
|
+
...new Set(Object.values(vaultData.factorialPools).flatMap((pool) => Object.keys(pool))),
|
|
1117
|
+
];
|
|
1118
|
+
let assetInfo = await this.splitPureAndVaultAsset(allRelatedUnderlyingAssetAddresses);
|
|
1119
|
+
const assetPrices = await monitor.getPricesNew(assetInfo.pureAssetAddresses, vaultData.oracleConfig);
|
|
1120
|
+
assetInfo = await this.splitPureAndVaultAsset(Object.keys(vaultData.assets));
|
|
1121
|
+
let valueSum = 0n;
|
|
1122
|
+
for (const assetAddress of assetInfo.pureAssetAddresses) {
|
|
1123
|
+
valueSum +=
|
|
1124
|
+
vaultData.assets[assetAddress].cash * assetPrices[assetAddress].price;
|
|
1125
|
+
}
|
|
1126
|
+
for (const [poolAddress, vaultPoolData] of Object.entries(vaultData.factorialPools)) {
|
|
1127
|
+
const pool = this.getByContract(pool_1.Pool, poolAddress);
|
|
1128
|
+
const poolData = pool_1.Pool.simulateAccrueInterest(await pool.getPoolData());
|
|
1129
|
+
for (const [assetAddress, poolShares] of Object.entries(vaultPoolData)) {
|
|
1130
|
+
const supplyAmount = poolShares.supply > 0n
|
|
1131
|
+
? (poolShares.supply *
|
|
1132
|
+
poolData.assets[assetAddress].totalSupplyAmount) /
|
|
1133
|
+
poolData.assets[assetAddress].totalSupplyShare
|
|
1134
|
+
: 0n;
|
|
1135
|
+
const borrowAmount = poolShares.borrow > 0n
|
|
1136
|
+
? (poolShares.borrow *
|
|
1137
|
+
poolData.assets[assetAddress].totalBorrowAmount) /
|
|
1138
|
+
poolData.assets[assetAddress].totalBorrowShare
|
|
1139
|
+
: 0n;
|
|
1140
|
+
valueSum +=
|
|
1141
|
+
(supplyAmount - borrowAmount) * assetPrices[assetAddress].price;
|
|
1142
|
+
}
|
|
1143
|
+
}
|
|
1144
|
+
return valueSum;
|
|
1145
|
+
}
|
|
1146
|
+
}
|
|
1147
|
+
exports.StrategyVaultBase = StrategyVaultBase;
|
|
1148
|
+
const shareToAmount = (vault, poolInfos) => {
|
|
1149
|
+
const amountVaultPosition = {};
|
|
1150
|
+
const assetAmount = {};
|
|
1151
|
+
const decomposed = {};
|
|
1152
|
+
for (let asset of Object.keys(vault.assets)) {
|
|
1153
|
+
assetAmount[asset] = vault.assets[asset];
|
|
1154
|
+
decomposed[asset] = {
|
|
1155
|
+
supply: vault.assets[asset],
|
|
1156
|
+
borrow: 0n,
|
|
1157
|
+
};
|
|
1158
|
+
}
|
|
1159
|
+
for (let pool of Object.keys(vault.poolPositions)) {
|
|
1160
|
+
const poolInfo = poolInfos[pool];
|
|
1161
|
+
const assets = vault.poolPositions[pool];
|
|
1162
|
+
for (let asset of Object.keys(assets)) {
|
|
1163
|
+
const position = assets[asset];
|
|
1164
|
+
let supply = 0n;
|
|
1165
|
+
let borrow = 0n;
|
|
1166
|
+
if (position.supply !== 0n &&
|
|
1167
|
+
poolInfo.assets[asset].totalSupplyShare !== 0n) {
|
|
1168
|
+
supply =
|
|
1169
|
+
(position.supply * poolInfo.assets[asset].totalSupplyAmount) /
|
|
1170
|
+
poolInfo.assets[asset].totalSupplyShare;
|
|
1171
|
+
}
|
|
1172
|
+
if (position.borrow !== 0n &&
|
|
1173
|
+
poolInfo.assets[asset].totalBorrowShare !== 0n) {
|
|
1174
|
+
borrow =
|
|
1175
|
+
(position.borrow * poolInfo.assets[asset].totalBorrowAmount) /
|
|
1176
|
+
poolInfo.assets[asset].totalBorrowShare;
|
|
1177
|
+
}
|
|
1178
|
+
amountVaultPosition[pool] ??= {};
|
|
1179
|
+
amountVaultPosition[pool][asset] ??= {
|
|
1180
|
+
supply,
|
|
1181
|
+
borrow,
|
|
1182
|
+
};
|
|
1183
|
+
assetAmount[asset] ??= 0n;
|
|
1184
|
+
assetAmount[asset] += supply - borrow;
|
|
1185
|
+
decomposed[asset].supply += supply;
|
|
1186
|
+
decomposed[asset].borrow += borrow;
|
|
1187
|
+
}
|
|
1188
|
+
}
|
|
1189
|
+
return {
|
|
1190
|
+
assets: vault.assets,
|
|
1191
|
+
poolPositions: amountVaultPosition,
|
|
1192
|
+
assetAmounts: assetAmount,
|
|
1193
|
+
totalSupply: vault.totalSupply,
|
|
1194
|
+
additionalData: {
|
|
1195
|
+
decomposed: decomposed,
|
|
1196
|
+
totalSupply: vault.totalSupply,
|
|
1197
|
+
},
|
|
1198
|
+
};
|
|
1199
|
+
};
|