@affluent-org/sdk 0.0.4 → 0.0.6
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/affluent.d.ts +3 -3
- package/dist/affluent.js +3 -3
- package/dist/common/computation.d.ts +6 -0
- package/dist/common/computation.js +54 -0
- package/dist/common/helper.d.ts +5 -0
- package/dist/common/helper.js +29 -0
- package/dist/common/service.d.ts +10 -2
- package/dist/common/service.js +56 -3
- package/dist/common/trace-action.d.ts +92 -0
- package/dist/common/trace-action.js +187 -0
- package/dist/common/transform.d.ts +87 -0
- package/dist/common/transform.js +264 -0
- package/dist/common/types.d.ts +140 -0
- package/dist/common/types.js +11 -0
- package/dist/context.d.ts +3 -0
- package/dist/context.js +9 -0
- package/dist/contracts/oracle/redstone-onchain-oracle/index.js +2 -0
- package/dist/contracts/oracle/redstone-onchain-oracle/type.d.ts +1 -1
- package/dist/contracts/vault/strategy-vault/index.d.ts +2 -1
- package/dist/contracts/vault/strategy-vault/index.js +1 -0
- package/dist/factorial.d.ts +18 -8
- package/dist/factorial.js +26 -14
- package/dist/index.d.ts +4 -2
- package/dist/index.js +18 -2
- package/dist/lib/send-msg.d.ts +16 -2
- package/dist/lib/send-msg.js +23 -2
- package/dist/monitor.js +2 -0
- package/dist/monitorCacheV1.js +3 -2
- package/dist/oracle/oracle.d.ts +3 -2
- package/dist/oracle/oracle.js +25 -2
- package/dist/pool.d.ts +41 -0
- package/dist/pool.js +146 -178
- package/dist/poolCacheV1.js +1 -0
- package/dist/rfq-auction.d.ts +2 -2
- package/dist/rfq-auction.js +6 -6
- package/dist/rfq-batch.d.ts +21 -11
- package/dist/rfq-batch.js +81 -9
- package/dist/services/composite-oracle/codec.d.ts +76 -0
- package/dist/services/composite-oracle/codec.js +281 -0
- package/dist/services/composite-oracle/computation.d.ts +8 -19
- package/dist/services/composite-oracle/computation.js +119 -76
- package/dist/services/composite-oracle/index.d.ts +2 -2
- package/dist/services/composite-oracle/index.js +5 -6
- package/dist/services/composite-oracle/query.d.ts +6 -6
- package/dist/services/composite-oracle/query.js +6 -47
- package/dist/services/pool/computation.d.ts +3 -9
- package/dist/services/pool/computation.js +12 -78
- package/dist/services/pool/index.d.ts +10 -59
- package/dist/services/pool/index.js +55 -8
- package/dist/services/pool/oracle.d.ts +2 -2
- package/dist/services/pool/query.d.ts +1 -1
- package/dist/services/pool/query.js +1 -1
- package/dist/services/pool/user/trace.d.ts +90 -0
- package/dist/services/pool/user/trace.js +168 -0
- package/dist/services/rfq-auction/index.d.ts +7 -23
- package/dist/services/rfq-auction/index.js +45 -6
- package/dist/services/rfq-auction/oracle.d.ts +2 -2
- package/dist/services/rfq-auction/user/index.js +1 -1
- package/dist/services/rfq-auction/user/trace.d.ts +53 -0
- package/dist/services/rfq-auction/user/trace.js +68 -0
- package/dist/services/rfq-batch/index.d.ts +16 -13
- package/dist/services/rfq-batch/index.js +34 -10
- package/dist/services/rfq-batch/oracle.d.ts +2 -2
- package/dist/services/rfq-batch/user/trace.d.ts +49 -0
- package/dist/services/rfq-batch/user/trace.js +67 -0
- package/dist/services/share-vault/index.d.ts +14 -88
- package/dist/services/share-vault/index.js +37 -10
- package/dist/services/share-vault/query.d.ts +5 -32
- package/dist/services/share-vault/query.js +25 -12
- package/dist/services/share-vault/user/trace.d.ts +54 -0
- package/dist/services/share-vault/user/trace.js +84 -0
- package/dist/services/strategy-vault/index.d.ts +77 -2115
- package/dist/services/strategy-vault/index.js +119 -54
- package/dist/services/strategy-vault/oracle.d.ts +3 -3
- package/dist/services/strategy-vault/oracle.js +1 -0
- package/dist/services/strategy-vault/owner/index.d.ts +2 -2
- package/dist/services/strategy-vault/owner/index.js +1 -1
- package/dist/services/strategy-vault/owner/types.d.ts +4 -0
- package/dist/services/strategy-vault/query.d.ts +14 -143
- package/dist/services/strategy-vault/query.js +28 -40
- package/dist/services/strategy-vault/user/trace.d.ts +156 -0
- package/dist/services/strategy-vault/user/trace.js +264 -0
- package/dist/share-vault.d.ts +164 -8
- package/dist/share-vault.js +222 -67
- package/dist/strategy_vault/base.d.ts +521 -105
- package/dist/strategy_vault/base.js +493 -41
- package/dist/strategy_vault/steps.d.ts +120 -3
- package/dist/strategy_vault/steps.js +161 -0
- package/dist/types/sender.d.ts +1 -0
- package/dist/utils/_parse_temp/StrategyVault.d.ts +9 -9
- package/dist/utils/_parse_temp/StrategyVault.js +48 -40
- package/dist/utils/_parse_temp/parseMsgBody.d.ts +2 -2
- package/dist/utils/_parse_temp/parseMsgBody.js +84 -84
- package/dist/utils/external-message-hash.d.ts +7 -3
- package/dist/utils/external-message-hash.js +20 -7
- package/dist/utils/oracle/redstone/helper.js +2 -0
- package/dist/utils/oracle/redstone/redstoneHelper.d.ts +7 -0
- package/dist/utils/oracle/redstone/redstoneHelper.js +103 -1
- package/dist/utils/pending-tracker/trackable-sender.d.ts +37 -4
- package/dist/utils/pending-tracker/trackable-sender.js +47 -8
- package/dist/utils/pending-tracker/v3-client.d.ts +16 -0
- package/dist/utils/pending-tracker/v3-client.js +80 -2
- package/dist/utils/risk_calculator/risk_calculator.d.ts +3 -3
- package/dist/utils/toncenter/index.d.ts +1 -0
- package/dist/utils/toncenter/index.js +17 -0
- package/dist/utils/toncenter/transform.d.ts +11 -0
- package/dist/utils/toncenter/transform.js +40 -0
- package/dist/utils/toncenter/type.d.ts +227 -0
- package/dist/utils/toncenter/type.js +2 -0
- package/package.json +3 -3
package/dist/rfq-batch.js
CHANGED
|
@@ -16,7 +16,7 @@ const oracle_1 = require("./oracle/oracle");
|
|
|
16
16
|
class RFQBatchV1 extends service_1.ServiceBaseV1 {
|
|
17
17
|
async sendAddOrder(sender, params, value) {
|
|
18
18
|
const rfqBatch = this.getByContract(rfq_batch_1.RFQBatch, (0, utils_1.toAddress)(params.rfqBatchAddress));
|
|
19
|
-
const rfqData = await rfqBatch.
|
|
19
|
+
const rfqData = await rfqBatch.getRFQData();
|
|
20
20
|
const forwardPayload = rfq_batch_1.RFQBatch.createOrderMsg((0, utils_1.toAddress)(params.senderAddress), params.forwardTonAmount, params.forwardPayload);
|
|
21
21
|
const assetContract = this.getByContract(jetton_minter_1.JettonMinter, (0, utils_1.toAddress)(params.assetAddress));
|
|
22
22
|
const assetWalletAddress = await assetContract.getWalletAddress((0, utils_1.toAddress)(params.senderAddress));
|
|
@@ -33,7 +33,7 @@ class RFQBatchV1 extends service_1.ServiceBaseV1 {
|
|
|
33
33
|
}
|
|
34
34
|
async sendSettle(sender, params, value) {
|
|
35
35
|
const rfqBatch = this.getByContract(rfq_batch_1.RFQBatch, (0, utils_1.toAddress)(params.rfqBatchAddress));
|
|
36
|
-
const rfqBatchData = await rfqBatch.
|
|
36
|
+
const rfqBatchData = await rfqBatch.getRFQData();
|
|
37
37
|
const oracleLibs = new oracle_1.OracleLibs(this.client, this.network);
|
|
38
38
|
const { oracleParams, bucket } = await oracleLibs.getOracleParamsForRFQBatch(rfqBatchData);
|
|
39
39
|
const forwardPayload = rfq_batch_1.RFQBatch.createSettleMsg(oracleParams);
|
|
@@ -149,7 +149,7 @@ class RFQBatchV1 extends service_1.ServiceBaseV1 {
|
|
|
149
149
|
}
|
|
150
150
|
async getOracleParams(rfqBatchAddress) {
|
|
151
151
|
const rfqBatch = this.getByContract(rfq_batch_1.RFQBatch, (0, utils_1.toAddress)(rfqBatchAddress));
|
|
152
|
-
const rfqBatchData = await rfqBatch.
|
|
152
|
+
const rfqBatchData = await rfqBatch.getRFQData();
|
|
153
153
|
const oracleConfig = rfqBatchData.oracleConfig;
|
|
154
154
|
let { assets, vaultAssets } = await this.separateAssets([
|
|
155
155
|
rfqBatchData.asset0Address.toString(),
|
|
@@ -191,7 +191,7 @@ class RFQBatchV1 extends service_1.ServiceBaseV1 {
|
|
|
191
191
|
}
|
|
192
192
|
async sendCancelOrder(sender, params, value) {
|
|
193
193
|
const rfqBatch = this.getByContract(rfq_batch_1.RFQBatch, (0, utils_1.toAddress)(params.rfqBatchAddress));
|
|
194
|
-
const rfqBatchData = await rfqBatch.
|
|
194
|
+
const rfqBatchData = await rfqBatch.getRFQData();
|
|
195
195
|
const assetIdx = rfqBatchData.asset0Address.toString() ===
|
|
196
196
|
(0, utils_1.toAddress)(params.assetAddress).toString()
|
|
197
197
|
? 0
|
|
@@ -207,7 +207,7 @@ class RFQBatchV1 extends service_1.ServiceBaseV1 {
|
|
|
207
207
|
}
|
|
208
208
|
async sendPayout(sender, params, value) {
|
|
209
209
|
const rfqBatch = this.getByContract(rfq_batch_1.RFQBatch, (0, utils_1.toAddress)(params.rfqBatchAddress));
|
|
210
|
-
const rfqBatchData = await rfqBatch.
|
|
210
|
+
const rfqBatchData = await rfqBatch.getRFQData();
|
|
211
211
|
const assetIdx = rfqBatchData.asset0Address.toString() ===
|
|
212
212
|
(0, utils_1.toAddress)(params.assetAddress).toString()
|
|
213
213
|
? 0
|
|
@@ -222,6 +222,40 @@ class RFQBatchV1 extends service_1.ServiceBaseV1 {
|
|
|
222
222
|
count: params.count,
|
|
223
223
|
});
|
|
224
224
|
}
|
|
225
|
+
async sendBatchPayout(sender, params, value) {
|
|
226
|
+
const rfqBatch = this.getByContract(rfq_batch_1.RFQBatch, (0, utils_1.toAddress)(params.rfqBatchAddress));
|
|
227
|
+
const rfqBatchData = await rfqBatch.getRFQData();
|
|
228
|
+
const msgs = [];
|
|
229
|
+
const maxCount = 99n;
|
|
230
|
+
// Asset0
|
|
231
|
+
const order0Count = BigInt(Object.values(rfqBatchData.asset0Orders).length);
|
|
232
|
+
if (order0Count > 0n) {
|
|
233
|
+
const batchCount = (order0Count + maxCount - 1n) / maxCount;
|
|
234
|
+
for (let i = 0; i < batchCount; i++) {
|
|
235
|
+
msgs.push(await this.createInternalPayout({
|
|
236
|
+
rfqBatchAddress: params.rfqBatchAddress,
|
|
237
|
+
assetAddress: rfqBatchData.asset0Address,
|
|
238
|
+
count: maxCount,
|
|
239
|
+
}));
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
// Asset1
|
|
243
|
+
const order1Count = BigInt(Object.values(rfqBatchData.asset1Orders).length);
|
|
244
|
+
if (order1Count > 0n) {
|
|
245
|
+
const batchCount = (order1Count + maxCount - 1n) / maxCount;
|
|
246
|
+
for (let i = 0; i < batchCount; i++) {
|
|
247
|
+
msgs.push(await this.createInternalPayout({
|
|
248
|
+
rfqBatchAddress: params.rfqBatchAddress,
|
|
249
|
+
assetAddress: rfqBatchData.asset1Address,
|
|
250
|
+
count: maxCount,
|
|
251
|
+
}));
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
await sender.multiSend(msgs.map((msg) => ({
|
|
255
|
+
...msg,
|
|
256
|
+
sendMode: ton_1.SendMode.PAY_GAS_SEPARATELY,
|
|
257
|
+
})));
|
|
258
|
+
}
|
|
225
259
|
async sendReopen(sender, params, value) {
|
|
226
260
|
const rfqBatch = this.getByContract(rfq_batch_1.RFQBatch, (0, utils_1.toAddress)(params.rfqBatchAddress));
|
|
227
261
|
await rfqBatch.sendReopen(sender, value ?? (0, ton_1.toNano)(0.1));
|
|
@@ -230,13 +264,28 @@ class RFQBatchV1 extends service_1.ServiceBaseV1 {
|
|
|
230
264
|
const rfqBatch = this.getByContract(rfq_batch_1.RFQBatch, (0, utils_1.toAddress)(params.rfqBatchAddress));
|
|
231
265
|
await rfqBatch.sendCollectFee(sender, value ?? (0, ton_1.toNano)(0.1));
|
|
232
266
|
}
|
|
233
|
-
async
|
|
267
|
+
async getRFQBatchData(rfqBatchAddress) {
|
|
234
268
|
const rfqBatch = this.getByContract(rfq_batch_1.RFQBatch, (0, utils_1.toAddress)(rfqBatchAddress));
|
|
235
|
-
return await rfqBatch.
|
|
269
|
+
return await rfqBatch.getRFQData();
|
|
270
|
+
}
|
|
271
|
+
async getLockedAmount(params) {
|
|
272
|
+
const rfqBatch = this.getByContract(rfq_batch_1.RFQBatch, (0, utils_1.toAddress)(params.rfqBatchAddress));
|
|
273
|
+
const rfqBatchData = await rfqBatch.getRFQData();
|
|
274
|
+
const tokenAddr = (0, utils_1.toAddress)(params.tokenAddress).toString();
|
|
275
|
+
const userAddr = (0, utils_1.toAddress)(params.userAddress).toString();
|
|
276
|
+
if (rfqBatchData.asset0Address.toString() === tokenAddr) {
|
|
277
|
+
return rfqBatchData.asset0Orders[userAddr]?.amount ?? 0n;
|
|
278
|
+
}
|
|
279
|
+
else if (rfqBatchData.asset1Address.toString() === tokenAddr) {
|
|
280
|
+
return rfqBatchData.asset1Orders[userAddr]?.amount ?? 0n;
|
|
281
|
+
}
|
|
282
|
+
else {
|
|
283
|
+
throw new Error("Token not found in RFQ batch");
|
|
284
|
+
}
|
|
236
285
|
}
|
|
237
286
|
async createInternalAddOrder(sender, params, value) {
|
|
238
287
|
const rfqBatch = this.getByContract(rfq_batch_1.RFQBatch, (0, utils_1.toAddress)(params.rfqBatchAddress));
|
|
239
|
-
const rfqData = await rfqBatch.
|
|
288
|
+
const rfqData = await rfqBatch.getRFQData();
|
|
240
289
|
const forwardPayload = rfq_batch_1.RFQBatch.createOrderMsg((0, utils_1.toAddress)(params.senderAddress), params.forwardTonAmount, params.forwardPayload);
|
|
241
290
|
const assetContract = this.getByContract(jetton_minter_1.JettonMinter, (0, utils_1.toAddress)(params.assetAddress));
|
|
242
291
|
const assetWalletAddress = await assetContract.getWalletAddress((0, utils_1.toAddress)(params.senderAddress));
|
|
@@ -260,7 +309,7 @@ class RFQBatchV1 extends service_1.ServiceBaseV1 {
|
|
|
260
309
|
}
|
|
261
310
|
async createInternalCancelOrder(params, value) {
|
|
262
311
|
const rfqBatch = this.getByContract(rfq_batch_1.RFQBatch, (0, utils_1.toAddress)(params.rfqBatchAddress));
|
|
263
|
-
const rfqBatchData = await rfqBatch.
|
|
312
|
+
const rfqBatchData = await rfqBatch.getRFQData();
|
|
264
313
|
const assetIdx = rfqBatchData.asset0Address.toString() ===
|
|
265
314
|
(0, utils_1.toAddress)(params.assetAddress).toString()
|
|
266
315
|
? 0
|
|
@@ -280,5 +329,28 @@ class RFQBatchV1 extends service_1.ServiceBaseV1 {
|
|
|
280
329
|
.endCell(),
|
|
281
330
|
};
|
|
282
331
|
}
|
|
332
|
+
async createInternalPayout(params, value) {
|
|
333
|
+
const rfqBatch = this.getByContract(rfq_batch_1.RFQBatch, (0, utils_1.toAddress)(params.rfqBatchAddress));
|
|
334
|
+
const rfqBatchData = await rfqBatch.getRFQData();
|
|
335
|
+
const assetIdx = rfqBatchData.asset0Address.toString() ===
|
|
336
|
+
(0, utils_1.toAddress)(params.assetAddress).toString()
|
|
337
|
+
? 0
|
|
338
|
+
: rfqBatchData.asset1Address.toString() ===
|
|
339
|
+
(0, utils_1.toAddress)(params.assetAddress).toString()
|
|
340
|
+
? 1
|
|
341
|
+
: -1;
|
|
342
|
+
if (assetIdx === -1)
|
|
343
|
+
throw new Error("Asset not found");
|
|
344
|
+
return {
|
|
345
|
+
to: (0, utils_1.toAddress)(params.rfqBatchAddress),
|
|
346
|
+
value: value ?? (0, ton_1.toNano)(0.1),
|
|
347
|
+
body: (0, ton_1.beginCell)()
|
|
348
|
+
.storeUint(rfq_batch_1.RFQBatch.Op.Payout, 32)
|
|
349
|
+
.storeUint(0, 64)
|
|
350
|
+
.storeUint(assetIdx, 1)
|
|
351
|
+
.storeCoins(params.count)
|
|
352
|
+
.endCell(),
|
|
353
|
+
};
|
|
354
|
+
}
|
|
283
355
|
}
|
|
284
356
|
exports.RFQBatchV1 = RFQBatchV1;
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { PoolState } from "../../contracts/core/pool/type";
|
|
2
|
+
import { ShareVaultState } from "../../contracts/vault/share-vault";
|
|
3
|
+
import { StrategyVaultState } from "../../contracts/vault/strategy-vault";
|
|
4
|
+
import { Prices } from "./types";
|
|
5
|
+
export declare class Position {
|
|
6
|
+
supply: bigint;
|
|
7
|
+
borrow: bigint;
|
|
8
|
+
constructor(supply: bigint, borrow: bigint);
|
|
9
|
+
apply(supply: bigint, borrow: bigint): this;
|
|
10
|
+
applyPosition(position: typeof this): this;
|
|
11
|
+
clone(): typeof this;
|
|
12
|
+
}
|
|
13
|
+
export declare class SharePosition extends Position {
|
|
14
|
+
toAmountPosition(PoolContext: PoolAsset): AmountPosition;
|
|
15
|
+
}
|
|
16
|
+
export declare class ValuePosition extends Position {
|
|
17
|
+
net(): bigint;
|
|
18
|
+
}
|
|
19
|
+
export declare class AmountPosition extends ValuePosition {
|
|
20
|
+
toValuePosition(price: bigint): ValuePosition;
|
|
21
|
+
net(): bigint;
|
|
22
|
+
}
|
|
23
|
+
export declare class PoolAsset {
|
|
24
|
+
amount: AmountPosition;
|
|
25
|
+
share: SharePosition;
|
|
26
|
+
constructor(amount: AmountPosition, share: SharePosition);
|
|
27
|
+
}
|
|
28
|
+
export type PoolAssets = Record<string, PoolAsset>;
|
|
29
|
+
export declare class PoolContext {
|
|
30
|
+
readonly address: string;
|
|
31
|
+
readonly assets: PoolAssets;
|
|
32
|
+
constructor(address: string, assets?: PoolAssets);
|
|
33
|
+
static fromPoolState(poolState: PoolState): PoolContext;
|
|
34
|
+
}
|
|
35
|
+
export type PoolContexts = Record<string, PoolContext>;
|
|
36
|
+
export type AmountPositions = Record<string, AmountPosition>;
|
|
37
|
+
export type SharePositions = Record<string, SharePosition>;
|
|
38
|
+
export type ValuePositions = Record<string, ValuePosition>;
|
|
39
|
+
export type PoolAmountPositions = Record<string, AmountPositions>;
|
|
40
|
+
export type PoolValuePositions = Record<string, ValuePositions>;
|
|
41
|
+
export type PoolSharePositions = Record<string, SharePositions>;
|
|
42
|
+
export declare class VaultStateContext {
|
|
43
|
+
readonly address: string;
|
|
44
|
+
readonly totalSupply: bigint;
|
|
45
|
+
readonly assets: AmountPositions;
|
|
46
|
+
readonly pools: PoolSharePositions;
|
|
47
|
+
constructor(address: string, totalSupply: bigint, assets: AmountPositions, // amount
|
|
48
|
+
pools: PoolSharePositions);
|
|
49
|
+
static fromShareVaultState(vaultState: ShareVaultState): VaultStateContext;
|
|
50
|
+
static fromStrategyVaultState(vaultState: StrategyVaultState): VaultStateContext;
|
|
51
|
+
toAmountContext(poolContexts: PoolContexts): VaultAmountContext;
|
|
52
|
+
}
|
|
53
|
+
export declare class VaultAmountContext {
|
|
54
|
+
readonly address: string;
|
|
55
|
+
readonly totalSupply: bigint;
|
|
56
|
+
readonly assets: AmountPositions;
|
|
57
|
+
readonly pools: PoolAmountPositions;
|
|
58
|
+
constructor(address: string, totalSupply: bigint, assets: AmountPositions, pools: PoolAmountPositions);
|
|
59
|
+
static fromVaultStateContext(VaultStateContext: VaultStateContext, poolContexts: PoolContexts): VaultAmountContext;
|
|
60
|
+
toValueContext(prices: Prices): VaultValueContext;
|
|
61
|
+
net(): AmountPositions;
|
|
62
|
+
}
|
|
63
|
+
export declare class VaultValueContext {
|
|
64
|
+
readonly address: string;
|
|
65
|
+
readonly assets: ValuePositions;
|
|
66
|
+
readonly pools: PoolValuePositions;
|
|
67
|
+
constructor(address: string, assets: ValuePositions, pools: PoolValuePositions);
|
|
68
|
+
static fromAmountContext(amountContext: VaultAmountContext, prices: Prices): VaultValueContext;
|
|
69
|
+
get totalValue(): bigint;
|
|
70
|
+
net(): ValuePositions;
|
|
71
|
+
nativeDecompose(prices: Prices): VaultNativeDecomposedValueContext;
|
|
72
|
+
}
|
|
73
|
+
export declare class VaultNativeDecomposedValueContext extends VaultValueContext {
|
|
74
|
+
static fromValueContext(valueContext: VaultValueContext, prices: Prices): VaultNativeDecomposedValueContext;
|
|
75
|
+
static decomposeContext(valueContext: ValuePositions, prices: Prices, nativeAssets: ValuePositions): ValuePositions;
|
|
76
|
+
}
|
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.VaultNativeDecomposedValueContext = exports.VaultValueContext = exports.VaultAmountContext = exports.VaultStateContext = exports.PoolContext = exports.PoolAsset = exports.AmountPosition = exports.ValuePosition = exports.SharePosition = exports.Position = void 0;
|
|
4
|
+
const pool_1 = require("../../contracts/core/pool");
|
|
5
|
+
const types_1 = require("./types");
|
|
6
|
+
class Position {
|
|
7
|
+
supply;
|
|
8
|
+
borrow;
|
|
9
|
+
constructor(supply, borrow) {
|
|
10
|
+
this.supply = supply;
|
|
11
|
+
this.borrow = borrow;
|
|
12
|
+
}
|
|
13
|
+
apply(supply, borrow) {
|
|
14
|
+
this.supply += supply;
|
|
15
|
+
this.borrow += borrow;
|
|
16
|
+
return this;
|
|
17
|
+
}
|
|
18
|
+
applyPosition(position) {
|
|
19
|
+
this.supply += position.supply;
|
|
20
|
+
this.borrow += position.borrow;
|
|
21
|
+
return this;
|
|
22
|
+
}
|
|
23
|
+
clone() {
|
|
24
|
+
return new this.constructor(this.supply, this.borrow);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
exports.Position = Position;
|
|
28
|
+
class SharePosition extends Position {
|
|
29
|
+
toAmountPosition(PoolContext) {
|
|
30
|
+
return new AmountPosition((0, pool_1.shareToAmount)(this.supply, PoolContext.amount.supply, PoolContext.share.supply), (0, pool_1.shareToAmount)(this.borrow, PoolContext.amount.borrow, PoolContext.share.borrow));
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
exports.SharePosition = SharePosition;
|
|
34
|
+
class ValuePosition extends Position {
|
|
35
|
+
// TODO: price?
|
|
36
|
+
net() {
|
|
37
|
+
return this.supply - this.borrow;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
exports.ValuePosition = ValuePosition;
|
|
41
|
+
class AmountPosition extends ValuePosition {
|
|
42
|
+
toValuePosition(price) {
|
|
43
|
+
return new ValuePosition(this.supply * price, this.borrow * price);
|
|
44
|
+
}
|
|
45
|
+
net() {
|
|
46
|
+
return this.supply - this.borrow;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
exports.AmountPosition = AmountPosition;
|
|
50
|
+
class PoolAsset {
|
|
51
|
+
amount;
|
|
52
|
+
share;
|
|
53
|
+
constructor(amount, share) {
|
|
54
|
+
this.amount = amount;
|
|
55
|
+
this.share = share;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
exports.PoolAsset = PoolAsset;
|
|
59
|
+
;
|
|
60
|
+
class PoolContext {
|
|
61
|
+
address;
|
|
62
|
+
assets;
|
|
63
|
+
constructor(address, assets = {}) {
|
|
64
|
+
this.address = address;
|
|
65
|
+
this.assets = assets;
|
|
66
|
+
}
|
|
67
|
+
static fromPoolState(poolState) {
|
|
68
|
+
const assets = {};
|
|
69
|
+
Object.entries(poolState.assets)
|
|
70
|
+
.forEach(([asset, assetState]) => {
|
|
71
|
+
assets[asset] = {
|
|
72
|
+
amount: new AmountPosition(assetState.totalSupplyAmount, assetState.totalBorrowAmount),
|
|
73
|
+
share: new SharePosition(assetState.totalSupplyShare, assetState.totalBorrowShare),
|
|
74
|
+
};
|
|
75
|
+
});
|
|
76
|
+
return new this(poolState.address.toString(), assets);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
exports.PoolContext = PoolContext;
|
|
80
|
+
class VaultStateContext {
|
|
81
|
+
address;
|
|
82
|
+
totalSupply;
|
|
83
|
+
assets;
|
|
84
|
+
pools;
|
|
85
|
+
constructor(address, totalSupply, assets, // amount
|
|
86
|
+
pools // share
|
|
87
|
+
) {
|
|
88
|
+
this.address = address;
|
|
89
|
+
this.totalSupply = totalSupply;
|
|
90
|
+
this.assets = assets;
|
|
91
|
+
this.pools = pools;
|
|
92
|
+
}
|
|
93
|
+
static fromShareVaultState(vaultState) {
|
|
94
|
+
const asset = vaultState.asset.toString();
|
|
95
|
+
const assets = {
|
|
96
|
+
[asset]: new AmountPosition(vaultState.cash, 0n),
|
|
97
|
+
};
|
|
98
|
+
const pools = {};
|
|
99
|
+
Object.entries(vaultState.whitelistedPools)
|
|
100
|
+
.forEach(([pool, poolState]) => {
|
|
101
|
+
pools[pool] = {
|
|
102
|
+
[asset]: new SharePosition(poolState.supply, 0n),
|
|
103
|
+
};
|
|
104
|
+
});
|
|
105
|
+
return new this(vaultState.address.toString(), vaultState.totalSupply, assets, pools);
|
|
106
|
+
}
|
|
107
|
+
static fromStrategyVaultState(vaultState) {
|
|
108
|
+
const assets = {};
|
|
109
|
+
Object.entries(vaultState.assets)
|
|
110
|
+
.forEach(([asset, assetState]) => {
|
|
111
|
+
assets[asset] = new AmountPosition(assetState.cash, 0n);
|
|
112
|
+
});
|
|
113
|
+
const pools = {};
|
|
114
|
+
Object.entries(vaultState.factorialPools)
|
|
115
|
+
.forEach(([pool, poolState]) => {
|
|
116
|
+
const poolEntry = {};
|
|
117
|
+
Object.entries(poolState)
|
|
118
|
+
.forEach(([asset, assetState]) => {
|
|
119
|
+
poolEntry[asset] = new SharePosition(assetState.supply, assetState.borrow);
|
|
120
|
+
});
|
|
121
|
+
pools[pool] = poolEntry;
|
|
122
|
+
});
|
|
123
|
+
return new this(vaultState.address.toString(), vaultState.totalSupply, assets, pools);
|
|
124
|
+
}
|
|
125
|
+
toAmountContext(poolContexts) {
|
|
126
|
+
return VaultAmountContext.fromVaultStateContext(this, poolContexts);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
exports.VaultStateContext = VaultStateContext;
|
|
130
|
+
class VaultAmountContext {
|
|
131
|
+
address;
|
|
132
|
+
totalSupply;
|
|
133
|
+
assets;
|
|
134
|
+
pools;
|
|
135
|
+
constructor(address, totalSupply, assets, pools) {
|
|
136
|
+
this.address = address;
|
|
137
|
+
this.totalSupply = totalSupply;
|
|
138
|
+
this.assets = assets;
|
|
139
|
+
this.pools = pools;
|
|
140
|
+
}
|
|
141
|
+
static fromVaultStateContext(VaultStateContext, poolContexts) {
|
|
142
|
+
const assets = {};
|
|
143
|
+
Object.entries(VaultStateContext.assets)
|
|
144
|
+
.forEach(([asset, amount]) => assets[asset] = amount.clone());
|
|
145
|
+
const pools = {};
|
|
146
|
+
Object.entries(VaultStateContext.pools)
|
|
147
|
+
.forEach(([pool, poolState]) => {
|
|
148
|
+
const poolEntry = {};
|
|
149
|
+
Object.entries(poolState)
|
|
150
|
+
.forEach(([asset, assetState]) => {
|
|
151
|
+
poolEntry[asset] = new AmountPosition((0, pool_1.shareToAmount)(assetState.supply, poolContexts[pool].assets[asset].amount.supply, poolContexts[pool].assets[asset].share.supply), (0, pool_1.shareToAmount)(assetState.borrow, poolContexts[pool].assets[asset].amount.borrow, poolContexts[pool].assets[asset].share.borrow));
|
|
152
|
+
});
|
|
153
|
+
pools[pool] = poolEntry;
|
|
154
|
+
});
|
|
155
|
+
return new this(VaultStateContext.address, VaultStateContext.totalSupply, assets, pools);
|
|
156
|
+
}
|
|
157
|
+
toValueContext(prices) {
|
|
158
|
+
return VaultValueContext.fromAmountContext(this, prices);
|
|
159
|
+
}
|
|
160
|
+
net() {
|
|
161
|
+
const net = {};
|
|
162
|
+
Object.entries(this.assets)
|
|
163
|
+
.forEach(([asset, position]) => {
|
|
164
|
+
net[asset] = position.clone();
|
|
165
|
+
});
|
|
166
|
+
Object.entries(this.pools)
|
|
167
|
+
.forEach(([pool, poolState]) => {
|
|
168
|
+
Object.entries(poolState)
|
|
169
|
+
.forEach(([asset, assetState]) => {
|
|
170
|
+
net[asset] = (net[asset] ?? new ValuePosition(0n, 0n)).applyPosition(assetState);
|
|
171
|
+
});
|
|
172
|
+
});
|
|
173
|
+
return net;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
exports.VaultAmountContext = VaultAmountContext;
|
|
177
|
+
class VaultValueContext {
|
|
178
|
+
address;
|
|
179
|
+
assets;
|
|
180
|
+
pools;
|
|
181
|
+
constructor(address, assets, pools) {
|
|
182
|
+
this.address = address;
|
|
183
|
+
this.assets = assets;
|
|
184
|
+
this.pools = pools;
|
|
185
|
+
}
|
|
186
|
+
static fromAmountContext(amountContext, prices) {
|
|
187
|
+
const assets = {};
|
|
188
|
+
Object.entries(amountContext.assets)
|
|
189
|
+
.forEach(([asset, position]) => assets[asset] = position.toValuePosition(prices[asset].price));
|
|
190
|
+
const pools = {};
|
|
191
|
+
Object.entries(amountContext.pools)
|
|
192
|
+
.forEach(([pool, poolState]) => {
|
|
193
|
+
const poolEntry = {};
|
|
194
|
+
Object.entries(poolState)
|
|
195
|
+
.forEach(([asset, assetState]) => poolEntry[asset] = assetState.toValuePosition(prices[asset].price));
|
|
196
|
+
pools[pool] = poolEntry;
|
|
197
|
+
});
|
|
198
|
+
return new this(amountContext.address, assets, pools);
|
|
199
|
+
}
|
|
200
|
+
get totalValue() {
|
|
201
|
+
let totalValue = 0n;
|
|
202
|
+
Object.entries(this.assets)
|
|
203
|
+
.forEach(([asset, position]) => {
|
|
204
|
+
totalValue += position.supply;
|
|
205
|
+
});
|
|
206
|
+
Object.entries(this.pools)
|
|
207
|
+
.forEach(([pool, poolState]) => {
|
|
208
|
+
Object.entries(poolState)
|
|
209
|
+
.forEach(([asset, assetState]) => {
|
|
210
|
+
totalValue += assetState.supply - assetState.borrow;
|
|
211
|
+
});
|
|
212
|
+
});
|
|
213
|
+
return totalValue;
|
|
214
|
+
}
|
|
215
|
+
net() {
|
|
216
|
+
const net = {};
|
|
217
|
+
Object.entries(this.assets)
|
|
218
|
+
.forEach(([asset, position]) => {
|
|
219
|
+
net[asset] = position.clone();
|
|
220
|
+
});
|
|
221
|
+
Object.entries(this.pools)
|
|
222
|
+
.forEach(([pool, poolState]) => {
|
|
223
|
+
Object.entries(poolState)
|
|
224
|
+
.forEach(([asset, assetState]) => {
|
|
225
|
+
net[asset] = (net[asset] ?? new ValuePosition(0n, 0n)).applyPosition(assetState);
|
|
226
|
+
});
|
|
227
|
+
});
|
|
228
|
+
return net;
|
|
229
|
+
}
|
|
230
|
+
nativeDecompose(prices) {
|
|
231
|
+
return VaultNativeDecomposedValueContext.fromValueContext(this, prices);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
exports.VaultValueContext = VaultValueContext;
|
|
235
|
+
class VaultNativeDecomposedValueContext extends VaultValueContext {
|
|
236
|
+
static fromValueContext(valueContext, prices) {
|
|
237
|
+
const assetsNativePositions = {};
|
|
238
|
+
VaultNativeDecomposedValueContext.decomposeContext(valueContext.assets, prices, assetsNativePositions);
|
|
239
|
+
const poolsNativePositions = {};
|
|
240
|
+
Object.entries(valueContext.pools)
|
|
241
|
+
.forEach(([pool, poolPosition]) => {
|
|
242
|
+
const nativePoolPosition = {};
|
|
243
|
+
VaultNativeDecomposedValueContext.decomposeContext(poolPosition, prices, nativePoolPosition);
|
|
244
|
+
poolsNativePositions[pool] = nativePoolPosition;
|
|
245
|
+
});
|
|
246
|
+
return new this(valueContext.address, assetsNativePositions, poolsNativePositions);
|
|
247
|
+
}
|
|
248
|
+
static decomposeContext(valueContext, prices, nativeAssets) {
|
|
249
|
+
Object.entries(valueContext)
|
|
250
|
+
.forEach(([asset, position]) => {
|
|
251
|
+
const price = prices[asset];
|
|
252
|
+
if (price.type === types_1.CompositeAssetType.NATIVE) {
|
|
253
|
+
nativeAssets[asset] = (nativeAssets[asset] ?? new ValuePosition(0n, 0n)).applyPosition(position);
|
|
254
|
+
}
|
|
255
|
+
else if (price.type === types_1.CompositeAssetType.SYNTHETIC1) {
|
|
256
|
+
if (prices[price.additionalData.underlying].type !== types_1.CompositeAssetType.NATIVE)
|
|
257
|
+
throw new Error(`Invalid underlying asset type: ${price.additionalData.underlying}`);
|
|
258
|
+
nativeAssets[price.additionalData.underlying] = (nativeAssets[price.additionalData.underlying] ?? new ValuePosition(0n, 0n))
|
|
259
|
+
.apply(position.supply * price.additionalData.reserve / price.additionalData.totalSupply, position.borrow * price.additionalData.reserve / price.additionalData.totalSupply);
|
|
260
|
+
}
|
|
261
|
+
else if (price.type === types_1.CompositeAssetType.SYNTHETIC2) {
|
|
262
|
+
if (prices[price.additionalData0.underlying].type !== types_1.CompositeAssetType.NATIVE)
|
|
263
|
+
throw new Error(`Invalid underlying asset type: ${price.additionalData0.underlying}`);
|
|
264
|
+
if (prices[price.additionalData1.underlying].type !== types_1.CompositeAssetType.NATIVE)
|
|
265
|
+
throw new Error(`Invalid underlying asset type: ${price.additionalData1.underlying}`);
|
|
266
|
+
nativeAssets[price.additionalData0.underlying] = (nativeAssets[price.additionalData0.underlying] ?? new ValuePosition(0n, 0n))
|
|
267
|
+
.apply(position.supply * price.additionalData0.reserve / price.additionalData0.totalSupply, position.borrow * price.additionalData0.reserve / price.additionalData0.totalSupply);
|
|
268
|
+
nativeAssets[price.additionalData1.underlying] = (nativeAssets[price.additionalData1.underlying] ?? new ValuePosition(0n, 0n))
|
|
269
|
+
.apply(position.supply * price.additionalData1.reserve / price.additionalData1.totalSupply, position.borrow * price.additionalData1.reserve / price.additionalData1.totalSupply);
|
|
270
|
+
}
|
|
271
|
+
else if (price.type === types_1.CompositeAssetType.VAULT) {
|
|
272
|
+
VaultNativeDecomposedValueContext.decomposeContext(valueContext, prices, nativeAssets);
|
|
273
|
+
}
|
|
274
|
+
else {
|
|
275
|
+
throw new Error(`Invalid asset type: ${price}`);
|
|
276
|
+
}
|
|
277
|
+
});
|
|
278
|
+
return nativeAssets;
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
exports.VaultNativeDecomposedValueContext = VaultNativeDecomposedValueContext;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Cell } from "@ton/core";
|
|
2
|
-
import { Prices, SyntheticCompositions,
|
|
3
|
-
import {
|
|
2
|
+
import { Prices, SyntheticCompositions, VaultPrices, CompositeOracleResult, CompositeOracleInput, IVaultStateContext, IPoolContext, IPosition } from "../../common/types";
|
|
3
|
+
import { AssetPrices, OnchainDataInfo } from "../../contracts/oracle/redstone-onchain-oracle/type";
|
|
4
4
|
export type OracleExposures = {
|
|
5
5
|
assets: Set<string>;
|
|
6
6
|
pools: Set<string>;
|
|
@@ -8,27 +8,16 @@ export type OracleExposures = {
|
|
|
8
8
|
totalSize(): number;
|
|
9
9
|
};
|
|
10
10
|
export declare function createOracleExposures(): OracleExposures;
|
|
11
|
-
export declare function applyPrice(amount: bigint, price: bigint): bigint;
|
|
12
11
|
export declare function calculateSyntheticPrices(rawPrices: Prices, syntheticCompositions: SyntheticCompositions): Prices;
|
|
13
|
-
export declare function
|
|
14
|
-
totalValue: bigint;
|
|
15
|
-
decomposed: Record<string, {
|
|
16
|
-
supply: bigint;
|
|
17
|
-
borrow: bigint;
|
|
18
|
-
}>;
|
|
19
|
-
assetAmounts: Record<string, bigint>;
|
|
20
|
-
};
|
|
21
|
-
export declare function computeVaultValue(vault: VaultPosition, poolInfos: PoolInfos, prices: Prices): {
|
|
12
|
+
export declare function computeVaultValue(vault: IVaultStateContext, poolInfos: Record<string, IPoolContext>, prices: Prices): {
|
|
22
13
|
totalValue: bigint;
|
|
23
14
|
price: bigint;
|
|
24
|
-
|
|
25
|
-
supply: bigint;
|
|
26
|
-
borrow: bigint;
|
|
27
|
-
}>;
|
|
15
|
+
amountContext: import("../../common/types").IVaultAmountContext;
|
|
28
16
|
};
|
|
29
|
-
export declare function calculateVaultPrices(vaultPositions:
|
|
17
|
+
export declare function calculateVaultPrices(vaultPositions: Record<string, IVaultStateContext>, poolInfos: Record<string, IPoolContext>, assetPrices: Prices): VaultPrices;
|
|
18
|
+
export declare function nativeDecomposeContext<P extends IPosition>(amountContext: Record<string, P>, prices: Prices): Record<string, P>;
|
|
30
19
|
export declare function computeCompositePrices(input: CompositeOracleInput): CompositeOracleResult;
|
|
31
|
-
export declare function getExposures(vaultPositions:
|
|
20
|
+
export declare function getExposures(vaultPositions: Record<string, IVaultStateContext>): {
|
|
32
21
|
assets: string[];
|
|
33
22
|
pools: string[];
|
|
34
23
|
};
|
|
@@ -43,7 +32,7 @@ export declare function separateAssets(allAssets: string[], oracleConfig: {
|
|
|
43
32
|
assets: string[];
|
|
44
33
|
vaultAssets: string[];
|
|
45
34
|
};
|
|
46
|
-
export declare function updateOnchainInternalData(prices:
|
|
35
|
+
export declare function updateOnchainInternalData(prices: AssetPrices, onchainData: OnchainDataInfo): AssetPrices;
|
|
47
36
|
/**
|
|
48
37
|
* Build oracle params Cell from pre-fetched Redstone payload
|
|
49
38
|
* Pure computation - no network calls
|