@affluent-org/sdk 0.0.5 → 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.
Files changed (96) hide show
  1. package/dist/affluent.d.ts +3 -3
  2. package/dist/affluent.js +3 -3
  3. package/dist/common/computation.d.ts +6 -0
  4. package/dist/common/computation.js +54 -0
  5. package/dist/common/helper.d.ts +5 -0
  6. package/dist/common/helper.js +29 -0
  7. package/dist/common/service.d.ts +10 -2
  8. package/dist/common/service.js +56 -3
  9. package/dist/common/trace-action.d.ts +92 -0
  10. package/dist/common/trace-action.js +187 -0
  11. package/dist/common/transform.d.ts +15 -21
  12. package/dist/common/transform.js +63 -150
  13. package/dist/common/types.d.ts +9 -13
  14. package/dist/common/types.js +2 -2
  15. package/dist/context.d.ts +3 -0
  16. package/dist/context.js +9 -0
  17. package/dist/contracts/oracle/redstone-onchain-oracle/index.js +2 -0
  18. package/dist/contracts/vault/strategy-vault/index.d.ts +2 -1
  19. package/dist/contracts/vault/strategy-vault/index.js +1 -0
  20. package/dist/factorial.d.ts +18 -8
  21. package/dist/factorial.js +26 -14
  22. package/dist/index.d.ts +4 -2
  23. package/dist/index.js +18 -2
  24. package/dist/lib/send-msg.d.ts +16 -2
  25. package/dist/lib/send-msg.js +23 -2
  26. package/dist/monitor.js +2 -0
  27. package/dist/monitorCacheV1.js +3 -2
  28. package/dist/oracle/oracle.d.ts +3 -2
  29. package/dist/oracle/oracle.js +25 -2
  30. package/dist/pool.d.ts +41 -0
  31. package/dist/pool.js +146 -178
  32. package/dist/poolCacheV1.js +1 -0
  33. package/dist/rfq-auction.d.ts +2 -2
  34. package/dist/rfq-auction.js +6 -6
  35. package/dist/rfq-batch.d.ts +21 -11
  36. package/dist/rfq-batch.js +81 -9
  37. package/dist/services/composite-oracle/computation.d.ts +3 -8
  38. package/dist/services/composite-oracle/computation.js +91 -56
  39. package/dist/services/composite-oracle/index.js +4 -5
  40. package/dist/services/composite-oracle/query.js +1 -3
  41. package/dist/services/pool/computation.js +9 -61
  42. package/dist/services/pool/index.d.ts +10 -59
  43. package/dist/services/pool/index.js +55 -8
  44. package/dist/services/pool/query.js +1 -1
  45. package/dist/services/pool/user/trace.d.ts +90 -0
  46. package/dist/services/pool/user/trace.js +168 -0
  47. package/dist/services/rfq-auction/index.d.ts +7 -23
  48. package/dist/services/rfq-auction/index.js +45 -6
  49. package/dist/services/rfq-auction/user/index.js +1 -1
  50. package/dist/services/rfq-auction/user/trace.d.ts +53 -0
  51. package/dist/services/rfq-auction/user/trace.js +68 -0
  52. package/dist/services/rfq-batch/index.d.ts +16 -13
  53. package/dist/services/rfq-batch/index.js +34 -10
  54. package/dist/services/rfq-batch/user/trace.d.ts +49 -0
  55. package/dist/services/rfq-batch/user/trace.js +67 -0
  56. package/dist/services/share-vault/index.d.ts +12 -56
  57. package/dist/services/share-vault/index.js +37 -10
  58. package/dist/services/share-vault/query.js +1 -1
  59. package/dist/services/share-vault/user/trace.d.ts +54 -0
  60. package/dist/services/share-vault/user/trace.js +84 -0
  61. package/dist/services/strategy-vault/index.d.ts +68 -1981
  62. package/dist/services/strategy-vault/index.js +114 -53
  63. package/dist/services/strategy-vault/oracle.js +1 -0
  64. package/dist/services/strategy-vault/owner/index.d.ts +2 -2
  65. package/dist/services/strategy-vault/owner/index.js +1 -1
  66. package/dist/services/strategy-vault/owner/types.d.ts +4 -0
  67. package/dist/services/strategy-vault/query.js +1 -1
  68. package/dist/services/strategy-vault/user/trace.d.ts +156 -0
  69. package/dist/services/strategy-vault/user/trace.js +264 -0
  70. package/dist/share-vault.d.ts +164 -8
  71. package/dist/share-vault.js +222 -67
  72. package/dist/strategy_vault/base.d.ts +521 -105
  73. package/dist/strategy_vault/base.js +493 -41
  74. package/dist/strategy_vault/steps.d.ts +120 -3
  75. package/dist/strategy_vault/steps.js +161 -0
  76. package/dist/types/sender.d.ts +1 -0
  77. package/dist/utils/_parse_temp/StrategyVault.d.ts +9 -9
  78. package/dist/utils/_parse_temp/StrategyVault.js +48 -40
  79. package/dist/utils/_parse_temp/parseMsgBody.d.ts +2 -2
  80. package/dist/utils/_parse_temp/parseMsgBody.js +84 -84
  81. package/dist/utils/external-message-hash.d.ts +7 -3
  82. package/dist/utils/external-message-hash.js +20 -7
  83. package/dist/utils/oracle/redstone/helper.js +2 -0
  84. package/dist/utils/oracle/redstone/redstoneHelper.d.ts +7 -0
  85. package/dist/utils/oracle/redstone/redstoneHelper.js +103 -1
  86. package/dist/utils/pending-tracker/trackable-sender.d.ts +37 -4
  87. package/dist/utils/pending-tracker/trackable-sender.js +47 -8
  88. package/dist/utils/pending-tracker/v3-client.d.ts +16 -0
  89. package/dist/utils/pending-tracker/v3-client.js +80 -2
  90. package/dist/utils/toncenter/index.d.ts +1 -0
  91. package/dist/utils/toncenter/index.js +17 -0
  92. package/dist/utils/toncenter/transform.d.ts +11 -0
  93. package/dist/utils/toncenter/transform.js +40 -0
  94. package/dist/utils/toncenter/type.d.ts +227 -0
  95. package/dist/utils/toncenter/type.js +2 -0
  96. 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.getRfqData();
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.getRfqData();
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.getRfqData();
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.getRfqData();
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.getRfqData();
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 getRfqBatchData(rfqBatchAddress) {
267
+ async getRFQBatchData(rfqBatchAddress) {
234
268
  const rfqBatch = this.getByContract(rfq_batch_1.RFQBatch, (0, utils_1.toAddress)(rfqBatchAddress));
235
- return await rfqBatch.getRfqData();
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.getRfqData();
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.getRfqData();
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;
@@ -1,5 +1,5 @@
1
1
  import { Cell } from "@ton/core";
2
- import { Prices, SyntheticCompositions, VaultPrices, CompositeOracleResult, CompositeOracleInput, IVaultStateContext, IPoolSharePositions, IPoolContext, IAmountPosition } from "../../common/types";
2
+ import { Prices, SyntheticCompositions, VaultPrices, CompositeOracleResult, CompositeOracleInput, IVaultStateContext, IPoolContext, IPosition } from "../../common/types";
3
3
  import { AssetPrices, OnchainDataInfo } from "../../contracts/oracle/redstone-onchain-oracle/type";
4
4
  export type OracleExposures = {
5
5
  assets: Set<string>;
@@ -8,19 +8,14 @@ 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 computePoolPositionsValue(poolPositions: IPoolSharePositions, poolInfos: Record<string, IPoolContext>, prices: Prices): {
14
- totalValue: bigint;
15
- decomposed: Record<string, IAmountPosition>;
16
- assetAmounts: Record<string, bigint>;
17
- };
18
12
  export declare function computeVaultValue(vault: IVaultStateContext, poolInfos: Record<string, IPoolContext>, prices: Prices): {
19
13
  totalValue: bigint;
20
14
  price: bigint;
21
- decomposed: Record<string, IAmountPosition>;
15
+ amountContext: import("../../common/types").IVaultAmountContext;
22
16
  };
23
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>;
24
19
  export declare function computeCompositePrices(input: CompositeOracleInput): CompositeOracleResult;
25
20
  export declare function getExposures(vaultPositions: Record<string, IVaultStateContext>): {
26
21
  assets: string[];
@@ -1,11 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.createOracleExposures = createOracleExposures;
4
- exports.applyPrice = applyPrice;
5
4
  exports.calculateSyntheticPrices = calculateSyntheticPrices;
6
- exports.computePoolPositionsValue = computePoolPositionsValue;
7
5
  exports.computeVaultValue = computeVaultValue;
8
6
  exports.calculateVaultPrices = calculateVaultPrices;
7
+ exports.nativeDecomposeContext = nativeDecomposeContext;
9
8
  exports.computeCompositePrices = computeCompositePrices;
10
9
  exports.getExposures = getExposures;
11
10
  exports.separateAssets = separateAssets;
@@ -18,6 +17,8 @@ const type_1 = require("../../contracts/oracle/redstone-onchain-oracle/type");
18
17
  const redstone_onchain_oracle_1 = require("../../contracts/oracle/redstone-onchain-oracle");
19
18
  const redstoneHelper_1 = require("../../utils/oracle/redstone/redstoneHelper");
20
19
  const utils_1 = require("../../contracts/common/utils");
20
+ const transform_1 = require("../../common/transform");
21
+ const helper_1 = require("../../common/helper");
21
22
  function createOracleExposures() {
22
23
  return {
23
24
  assets: new Set(),
@@ -28,9 +29,6 @@ function createOracleExposures() {
28
29
  },
29
30
  };
30
31
  }
31
- function applyPrice(amount, price) {
32
- return (amount * price) / types_1.DECIMALS_OF_PRICE;
33
- }
34
32
  function calculateSyntheticPrices(rawPrices, syntheticCompositions) {
35
33
  const prices = { ...rawPrices };
36
34
  let pendingAssets = Object.entries(syntheticCompositions);
@@ -87,52 +85,12 @@ function calculateSyntheticPrices(rawPrices, syntheticCompositions) {
87
85
  }
88
86
  return prices;
89
87
  }
90
- function computePoolPositionsValue(poolPositions, poolInfos, prices) {
91
- const decomposed = {};
92
- const assetAmounts = {};
93
- let totalValue = 0n;
94
- for (const [poolAddress, assetPositions] of Object.entries(poolPositions)) {
95
- const poolInfo = poolInfos[poolAddress];
96
- if (!poolInfo)
97
- continue;
98
- for (const [assetAddress, position] of Object.entries(assetPositions)) {
99
- const assetInfo = poolInfo.assets[assetAddress];
100
- if (!assetInfo)
101
- continue;
102
- const amounts = position.toAmountPosition(assetInfo);
103
- if (decomposed[assetAddress])
104
- decomposed[assetAddress].applyPosition(amounts);
105
- else
106
- decomposed[assetAddress] = amounts.clone();
107
- const netAmount = amounts.net();
108
- assetAmounts[assetAddress] = (assetAmounts[assetAddress] ?? 0n) + netAmount;
109
- const price = prices[assetAddress];
110
- if (price)
111
- totalValue += applyPrice(netAmount, price.price);
112
- }
113
- }
114
- return { totalValue, decomposed, assetAmounts };
115
- }
116
88
  function computeVaultValue(vault, poolInfos, prices) {
117
- let cashValue = 0n;
118
- const decomposed = {};
119
- for (const [assetAddress, amount] of Object.entries(vault.assets)) {
120
- const price = prices[assetAddress];
121
- if (price && amount.supply > 0n) {
122
- cashValue += applyPrice(amount.supply, price.price);
123
- }
124
- decomposed[assetAddress] = amount.clone();
125
- }
126
- const poolResult = computePoolPositionsValue(vault.pools, poolInfos, prices);
127
- for (const [assetAddress, amounts] of Object.entries(poolResult.decomposed)) {
128
- if (decomposed[assetAddress])
129
- decomposed[assetAddress].applyPosition(amounts);
130
- else
131
- decomposed[assetAddress] = amounts.clone();
132
- }
133
- const totalValue = cashValue + poolResult.totalValue;
134
- const price = vault.totalSupply === 0n ? 0n : (totalValue * types_1.DECIMALS_OF_PRICE) / vault.totalSupply;
135
- return { totalValue, price, decomposed };
89
+ const amountContext = vault.toAmountContext(poolInfos);
90
+ const valueContext = amountContext.toValueContext(prices);
91
+ const totalValue = valueContext.totalValue;
92
+ const price = vault.totalSupply === 0n ? 0n : (totalValue / vault.totalSupply);
93
+ return { totalValue, price, amountContext };
136
94
  }
137
95
  function calculateVaultPrices(vaultPositions, poolInfos, assetPrices) {
138
96
  const vaultPrices = {};
@@ -171,8 +129,7 @@ function calculateVaultPrices(vaultPositions, poolInfos, assetPrices) {
171
129
  type: types_1.CompositeAssetType.VAULT,
172
130
  price: result.price,
173
131
  additionalData: {
174
- decomposed: result.decomposed,
175
- totalSupply: vault.totalSupply,
132
+ amountContext: result.amountContext,
176
133
  },
177
134
  };
178
135
  vaultPrices[vaultAddress] = vaultPriceEntry;
@@ -190,8 +147,7 @@ function calculateVaultPrices(vaultPositions, poolInfos, assetPrices) {
190
147
  type: types_1.CompositeAssetType.VAULT,
191
148
  price: 0n,
192
149
  additionalData: {
193
- decomposed: {},
194
- totalSupply: pendingVaults[vaultAddress].totalSupply,
150
+ amountContext: new transform_1.VaultAmountContext(vaultAddress, pendingVaults[vaultAddress].totalSupply, {}, {}),
195
151
  },
196
152
  };
197
153
  delete pendingVaults[vaultAddress];
@@ -200,6 +156,52 @@ function calculateVaultPrices(vaultPositions, poolInfos, assetPrices) {
200
156
  }
201
157
  return vaultPrices;
202
158
  }
159
+ function nativeDecomposeContext(amountContext, prices) {
160
+ const nativeDecomposed = {};
161
+ Object.entries(amountContext)
162
+ .forEach(([asset, position]) => {
163
+ const price = prices[asset];
164
+ if (price.type === types_1.CompositeAssetType.NATIVE) {
165
+ nativeDecomposed[asset] = position.addPosition(nativeDecomposed[asset]);
166
+ }
167
+ else if (price.type === types_1.CompositeAssetType.SYNTHETIC1) {
168
+ if (prices[price.additionalData.underlying].type !== types_1.CompositeAssetType.NATIVE)
169
+ throw new Error(`Invalid underlying asset type: ${price.additionalData.underlying}`);
170
+ const underlyingNativeDecomposed = nativeDecomposeContext({ [price.additionalData.underlying]: position.muldiv(price.additionalData.reserve, price.additionalData.totalSupply) }, prices);
171
+ (0, helper_1.applyPositions)(nativeDecomposed, underlyingNativeDecomposed);
172
+ }
173
+ else if (price.type === types_1.CompositeAssetType.SYNTHETIC2) {
174
+ if (prices[price.additionalData0.underlying].type !== types_1.CompositeAssetType.NATIVE)
175
+ throw new Error(`Invalid underlying asset type: ${price.additionalData0.underlying}`);
176
+ if (prices[price.additionalData1.underlying].type !== types_1.CompositeAssetType.NATIVE)
177
+ throw new Error(`Invalid underlying asset type: ${price.additionalData1.underlying}`);
178
+ const price0 = prices[price.additionalData0.underlying].price;
179
+ const price1 = prices[price.additionalData1.underlying].price;
180
+ const value0 = price.additionalData0.reserve * price0;
181
+ const value1 = price.additionalData1.reserve * price1;
182
+ const totalValue = value0 + value1;
183
+ const underlyingNativeDecomposed = nativeDecomposeContext({
184
+ [price.additionalData0.underlying]: position.muldiv(value0, totalValue),
185
+ [price.additionalData1.underlying]: position.muldiv(value1, totalValue)
186
+ }, prices);
187
+ (0, helper_1.applyPositions)(nativeDecomposed, underlyingNativeDecomposed);
188
+ }
189
+ else if (price.type === types_1.CompositeAssetType.VAULT) {
190
+ const depVaultValue = price.price * price.additionalData.amountContext.totalSupply;
191
+ const depDecomposed = {};
192
+ Object.entries(price.additionalData.amountContext.decompose())
193
+ .forEach(([depAsset, depPosition]) => {
194
+ depDecomposed[depAsset] = position.cross(depPosition.toValuePosition(prices[depAsset].price), depVaultValue);
195
+ });
196
+ const depNativeDecomposed = nativeDecomposeContext(depDecomposed, prices);
197
+ (0, helper_1.applyPositions)(nativeDecomposed, depNativeDecomposed);
198
+ }
199
+ else {
200
+ throw new Error(`Invalid asset type: ${price}`);
201
+ }
202
+ });
203
+ return nativeDecomposed;
204
+ }
203
205
  function computeCompositePrices(input) {
204
206
  const assetPrices = calculateSyntheticPrices(input.rawPrices, input.syntheticCompositions);
205
207
  const vaultPrices = calculateVaultPrices(input.vaultPositions, input.poolInfos, assetPrices);
@@ -295,9 +297,42 @@ function buildOracleParamsCellFromPayload(assets, oracleConfig, payload, pools,
295
297
  */
296
298
  async function buildOracleParamsCell(assets, oracleConfig, pools, vaults) {
297
299
  const requestParams = redstone_onchain_oracle_1.RedstoneOnchainOracle.getOracleRequestParams(assets, oracleConfig);
298
- // Fetch Redstone payload
299
- const helper = (0, redstoneHelper_1.createRedstoneHelper)("prod");
300
+ const signers = new Set(oracleConfig.signers
301
+ .map((s) => s.toLowerCase()));
302
+ let helper;
303
+ if (setEqual(signers, restonSigners)) {
304
+ helper = (0, redstoneHelper_1.createRedstoneHelper)("prod");
305
+ }
306
+ else if (setEqual(signers, affluentSigners)) {
307
+ helper = (0, redstoneHelper_1.createRedstoneCustomHelper)("http://aggregatora3loa-ktesxzhb-780180176.ap-northeast-2.elb.amazonaws.com");
308
+ }
309
+ else {
310
+ throw new Error(`Invalid signers: ${[...signers]}`);
311
+ }
312
+ // Fetch payload
300
313
  const payload = await helper.createPayload(requestParams.redstoneAssetNames);
301
314
  // Pure computation
302
315
  return buildOracleParamsCellFromPayload(assets, oracleConfig, payload, pools, vaults);
303
316
  }
317
+ const restonSigners = new Set([
318
+ '51Ce04Be4b3E32572C4Ec9135221d0691Ba7d202',
319
+ '8BB8F32Df04c8b654987DAaeD53D6B6091e3B774',
320
+ '9c5AE89C4Af6aA32cE58588DBaF90d18a855B6de',
321
+ 'DD682daEC5A90dD295d14DA4b0bec9281017b5bE',
322
+ 'dEB22f54738d54976C4c0fe5ce6d408E40d88499'
323
+ ].map((s) => s.toLowerCase()));
324
+ const affluentSigners = new Set([
325
+ "938b33477fdcebcb1ca4dd9502af775a0c4f2e2c",
326
+ "7d8d5e5be15338797183652d401f290c21d08e73",
327
+ "95abc793f212a11330ddafd337edb8b1903f62f5",
328
+ "4d0a8877125e889004facfdef35fc256548ce038",
329
+ "fb023d5b920b1c13be0c4cdde27e7b75ef67c627",
330
+ ].map((s) => s.toLowerCase()));
331
+ function setEqual(a, b) {
332
+ if (a.size !== b.size)
333
+ return false;
334
+ for (const v of a)
335
+ if (!b.has(v))
336
+ return false;
337
+ return true;
338
+ }
@@ -16,7 +16,6 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  exports.CompositeOracleV1 = exports.CompositeOracleFetcher = void 0;
18
18
  const utils_1 = require("../../contracts/common/utils");
19
- const types_1 = require("../../common/types");
20
19
  const computation_1 = require("./computation");
21
20
  const query_1 = require("./query");
22
21
  __exportStar(require("../../common/types"), exports);
@@ -83,10 +82,10 @@ class CompositeOracleV1 extends query_1.CompositeOracleFetcher {
83
82
  const result = this.compute(input);
84
83
  const targetVaultAddress = (0, utils_1.toAddress)(vaultAddress).toString();
85
84
  const vaultPriceEntry = result.vaultPrices[targetVaultAddress];
86
- if (!vaultPriceEntry || vaultPriceEntry.additionalData.totalSupply === 0n) {
85
+ if (!vaultPriceEntry || vaultPriceEntry.additionalData.amountContext.totalSupply === 0n) {
87
86
  return 0n;
88
87
  }
89
- return (vaultPriceEntry.price * vaultPriceEntry.additionalData.totalSupply) / types_1.DECIMALS_OF_PRICE;
88
+ return vaultPriceEntry.price * vaultPriceEntry.additionalData.amountContext.totalSupply;
90
89
  }
91
90
  // ============================================================
92
91
  // Static computation methods (no instance needed)
@@ -101,10 +100,10 @@ class CompositeOracleV1 extends query_1.CompositeOracleFetcher {
101
100
  static getTotalValueFromInput(input, vaultAddress) {
102
101
  const result = (0, computation_1.computeCompositePrices)(input);
103
102
  const vaultPriceEntry = result.vaultPrices[vaultAddress];
104
- if (!vaultPriceEntry || vaultPriceEntry.additionalData.totalSupply === 0n) {
103
+ if (!vaultPriceEntry || vaultPriceEntry.additionalData.amountContext.totalSupply === 0n) {
105
104
  return 0n;
106
105
  }
107
- return (vaultPriceEntry.price * vaultPriceEntry.additionalData.totalSupply) / types_1.DECIMALS_OF_PRICE;
106
+ return vaultPriceEntry.price * vaultPriceEntry.additionalData.amountContext.totalSupply;
108
107
  }
109
108
  }
110
109
  exports.CompositeOracleV1 = CompositeOracleV1;
@@ -59,7 +59,7 @@ class CompositeOracleFetcher {
59
59
  if (rainfo.assetType === 0) {
60
60
  rawPrices[rainfo.address.toString()] = {
61
61
  type: types_1.CompositeAssetType.NATIVE,
62
- price: (item.price * types_1.DECIMALS_OF_PRICE) / rainfo.precision,
62
+ price: (item.price * types_1.VALUE_SCALE) / rainfo.precision,
63
63
  timestamp: item.timestamp,
64
64
  };
65
65
  }
@@ -180,8 +180,6 @@ class CompositeOracleFetcher {
180
180
  if (state.state.type === "active" && state.state.data) {
181
181
  const data = core_1.Cell.fromBoc(state.state.data)[0];
182
182
  try {
183
- const vaultData = strategy_vault_1.StrategyVault.parseVaultData(data);
184
- // VaultStateContext.fromStrategyVaultState(vaultData);
185
183
  positions[addressStr] = transform_1.VaultStateContext.fromStrategyVaultStateCell((0, utils_1.toAddress)(vaultAddress), data);
186
184
  return;
187
185
  }
@@ -9,54 +9,12 @@ exports.applyDelta = applyDelta;
9
9
  exports.decomposePositions = decomposePositions;
10
10
  exports.calculateRisk = calculateRisk;
11
11
  exports.isHealthyFromData = isHealthyFromData;
12
- const core_1 = require("@ton/core");
13
12
  const pool_1 = require("../../contracts/core/pool");
14
13
  const account_1 = require("../../contracts/core/account");
15
14
  const types_1 = require("../../common/types");
16
- const utils_1 = require("../../contracts/common/utils");
17
15
  const transform_1 = require("../../common/transform");
18
- // ============================================================
19
- // Position Decomposition Helpers
20
- // ============================================================
21
- /**
22
- * Try to decompose a synthetic/vault asset to its underlying assets
23
- * Returns null if asset is native (no decomposition needed)
24
- */
25
- function tryDecomposeAsset(asset, position, syntheticCompositions, vaultCompositions) {
26
- const data = syntheticCompositions[asset];
27
- // SYNTHETIC1: single underlying asset (e.g. stTON, tsTON)
28
- if (data?.assetType === types_1.CompositeAssetType.SYNTHETIC1 &&
29
- "underlyingReserve" in data &&
30
- data.underlyingReserve &&
31
- data.totalSupply) {
32
- const ratio = data.underlyingReserve;
33
- const total = data.totalSupply;
34
- return [
35
- new transform_1.AmountPositionWithAddress((0, utils_1.toAddress)(data.underlyingAddress), (position.supply * ratio) / total, (position.borrow * ratio) / total),
36
- ];
37
- }
38
- // SYNTHETIC2: two underlying assets (e.g. LP tokens)
39
- if (data?.assetType === types_1.CompositeAssetType.SYNTHETIC2 &&
40
- "underlying0Reserve" in data &&
41
- data.underlying0Reserve &&
42
- data.underlying1Reserve &&
43
- data.totalSupply) {
44
- const total = data.totalSupply;
45
- return [
46
- new transform_1.AmountPositionWithAddress((0, utils_1.toAddress)(data.underlying0Address), (position.supply * data.underlying0Reserve) / total, (position.borrow * data.underlying0Reserve) / total),
47
- new transform_1.AmountPositionWithAddress((0, utils_1.toAddress)(data.underlying1Address), (position.supply * data.underlying1Reserve) / total, (position.borrow * data.underlying1Reserve) / total),
48
- ];
49
- }
50
- // VAULT: Strategy Vault - uses decomposed positions
51
- const vault = vaultCompositions[asset];
52
- if (vault) {
53
- const { decomposed, totalSupply } = vault;
54
- return Object.entries(decomposed).map(([addr, pos]) => new transform_1.AmountPositionWithAddress(core_1.Address.parse(addr),
55
- // Vault supply/borrow cross each other
56
- (position.supply * pos.supply + position.borrow * pos.borrow) / totalSupply, (position.supply * pos.borrow + position.borrow * pos.supply) / totalSupply));
57
- }
58
- return null; // Native asset
59
- }
16
+ const computation_1 = require("../../common/computation");
17
+ const helper_1 = require("../../common/helper");
60
18
  // ============================================================
61
19
  // Pure Computation (no I/O)
62
20
  // ============================================================
@@ -101,22 +59,12 @@ function applyDelta(accountData, delta) {
101
59
  */
102
60
  function decomposePositions(positions, syntheticCompositions, vaultCompositions = {}) {
103
61
  const accumulatedPositions = {};
104
- const queue = [...Object.values(positions)].map(position => transform_1.AmountPositionWithAddress.from(position));
105
- while (queue.length > 0) {
106
- const current = queue.shift();
107
- if (current.supply === 0n && current.borrow === 0n)
108
- continue;
109
- // Accumulate all assets (synthetic/vault/native)
110
- const asset = current.address.toString();
111
- accumulatedPositions[asset] ??= new transform_1.AmountPosition(0n, 0n);
112
- accumulatedPositions[asset].supply += current.supply;
113
- accumulatedPositions[asset].borrow += current.borrow;
114
- // If decomposable, add underlying to queue
115
- const decomposed = tryDecomposeAsset(asset, current, syntheticCompositions, vaultCompositions);
116
- if (decomposed) {
117
- queue.push(...decomposed);
118
- }
119
- }
62
+ Object.values(positions)
63
+ .forEach(position => {
64
+ const asset = position.address.toString();
65
+ const decomposed = (0, computation_1.tryNativeDecomposeAsset)(asset, new transform_1.AmountPosition(position.supply, position.borrow), syntheticCompositions, vaultCompositions);
66
+ (0, helper_1.applyPositions)(accumulatedPositions, decomposed);
67
+ });
120
68
  return accumulatedPositions;
121
69
  }
122
70
  /** @deprecated Use decomposePositions instead */
@@ -199,7 +147,7 @@ function calculateRisk(decomposedAccountStatus, prices, poolData) {
199
147
  * Takes pre-fetched pool data, account status, and prices
200
148
  */
201
149
  function isHealthyFromData(poolData, accountStatus, prices, syntheticCompositions, vaultCompositions = {}) {
202
- const simulatedPoolData = simulateAccrueInterest(poolData);
203
150
  const decomposed = decomposePositions(accountStatus, syntheticCompositions, vaultCompositions);
151
+ const simulatedPoolData = simulateAccrueInterest(poolData);
204
152
  return calculateRisk(decomposed, prices, simulatedPoolData);
205
153
  }
@@ -9,9 +9,11 @@ import * as query from "./query";
9
9
  import * as oracle from "./oracle";
10
10
  import * as owner from "./owner";
11
11
  import * as user from "./user";
12
+ import { PoolSupplyTracer, PoolWithdrawTracer, PoolBorrowTracer, PoolRepayTracer } from "./user/trace";
12
13
  export { sendMsg } from "../../lib/send-msg";
13
14
  export * from "./owner/types";
14
15
  export * from "./user/types";
16
+ export * from "./user/trace";
15
17
  export * from "./computation";
16
18
  export { computation, query, oracle, owner, user };
17
19
  export declare class PoolServiceV1 {
@@ -32,63 +34,12 @@ export declare class PoolServiceV1 {
32
34
  static convertShareToAmount(accountData: AccountState, poolData: PoolState): AccountState;
33
35
  static haveBorrow(accountData: AccountState): boolean;
34
36
  static haveAssets(accountData: AccountState): string[];
35
- sendSetOwner(sender: AddressSender, params: owner.SetOwnerParams, value?: bigint): Promise<{
36
- newOwner: Address;
37
- queryID: number | undefined;
38
- }>;
39
- sendSetIrmConfig(sender: AddressSender, params: owner.SetIrmConfigParams, value?: bigint): Promise<{
40
- asset: Address;
41
- irmConfig: import("@ton/core").Cell;
42
- queryId: number | undefined;
43
- }>;
44
- sendSetFeeConfigurer(sender: AddressSender, params: owner.SetFeeConfigurerParams, value?: bigint): Promise<{
45
- feeConfigurer: Address;
46
- queryId: number | undefined;
47
- }>;
48
- sendSupply(sender: AddressSender, params: user.SupplyParams, value?: bigint): Promise<{
49
- recipient: Address;
50
- amount: bigint;
51
- response: Address;
52
- forwardTonAmount: bigint;
53
- forwardPayload: import("@ton/core").Cell;
54
- queryId: bigint | undefined;
55
- }>;
56
- sendWithdraw(sender: AddressSender, params: user.WithdrawParams, value?: bigint): Promise<{
57
- asset: Address;
58
- isShare: boolean;
59
- amount: bigint;
60
- recipient: Address;
61
- response: Address;
62
- jettonForwardTonAmount: bigint;
63
- jettonForwardPayload: import("@ton/core").Cell | null;
64
- oracleParams: import("@ton/core").Cell | null;
65
- queryId: bigint | undefined;
66
- }>;
67
- sendBorrow(sender: AddressSender, params: user.BorrowParams, value?: bigint): Promise<{
68
- asset: Address;
69
- isShare: boolean;
70
- amount: bigint;
71
- recipient: Address;
72
- response: Address;
73
- jettonForwardTonAmount: bigint;
74
- jettonForwardPayload: import("@ton/core").Cell | null;
75
- oracleParams: import("@ton/core").Cell | null;
76
- queryId: bigint | undefined;
77
- }>;
78
- sendRepay(sender: AddressSender, params: user.RepayParams, value?: bigint): Promise<{
79
- recipient: Address;
80
- amount: bigint;
81
- response: Address;
82
- forwardTonAmount: bigint;
83
- forwardPayload: import("@ton/core").Cell;
84
- queryId: bigint | undefined;
85
- }>;
86
- sendLiquidate(sender: AddressSender, params: user.LiquidateParams, value?: bigint): Promise<{
87
- recipient: Address;
88
- amount: bigint;
89
- response: Address;
90
- forwardTonAmount: bigint;
91
- forwardPayload: import("@ton/core").Cell;
92
- queryId: bigint | undefined;
93
- }>;
37
+ sendSetOwner(sender: AddressSender, params: owner.SetOwnerParams, value?: bigint): Promise<import("../..").SimpleTracer>;
38
+ sendSetIrmConfig(sender: AddressSender, params: owner.SetIrmConfigParams, value?: bigint): Promise<import("../..").SimpleTracer>;
39
+ sendSetFeeConfigurer(sender: AddressSender, params: owner.SetFeeConfigurerParams, value?: bigint): Promise<import("../..").SimpleTracer>;
40
+ sendSupply(sender: AddressSender, params: user.SupplyParams, value?: bigint): Promise<PoolSupplyTracer>;
41
+ sendWithdraw(sender: AddressSender, params: user.WithdrawParams, value?: bigint): Promise<PoolWithdrawTracer>;
42
+ sendBorrow(sender: AddressSender, params: user.BorrowParams, value?: bigint): Promise<PoolBorrowTracer>;
43
+ sendRepay(sender: AddressSender, params: user.RepayParams, value?: bigint): Promise<PoolRepayTracer>;
44
+ sendLiquidate(sender: AddressSender, params: user.LiquidateParams, value?: bigint): Promise<import("../..").SimpleTracer>;
94
45
  }