@evaafi/sdk 0.6.1 → 0.6.2-b

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 (73) hide show
  1. package/LICENSE.md +7 -0
  2. package/README.md +2 -1
  3. package/dist/api/liquidation.js +1 -3
  4. package/dist/api/math.d.ts +10 -1
  5. package/dist/api/math.js +45 -6
  6. package/dist/api/parser.js +49 -27
  7. package/dist/api/prices.d.ts +5 -2
  8. package/dist/api/prices.js +35 -13
  9. package/dist/constants/assets.d.ts +8 -0
  10. package/dist/constants/assets.js +31 -1
  11. package/dist/constants/general.d.ts +5 -2
  12. package/dist/constants/general.js +12 -22
  13. package/dist/constants/pools.d.ts +1 -0
  14. package/dist/constants/pools.js +19 -3
  15. package/dist/contracts/MasterContract.d.ts +5 -0
  16. package/dist/contracts/MasterContract.js +6 -0
  17. package/dist/index.d.ts +4 -4
  18. package/dist/index.js +10 -2
  19. package/dist/prices/Prices.d.ts +9 -0
  20. package/dist/prices/Prices.js +43 -0
  21. package/dist/prices/PricesCollector.d.ts +12 -0
  22. package/dist/prices/PricesCollector.js +123 -0
  23. package/dist/prices/Types.d.ts +33 -0
  24. package/dist/prices/Types.js +11 -0
  25. package/dist/prices/constants.d.ts +1 -0
  26. package/dist/prices/constants.js +4 -0
  27. package/dist/prices/index.d.ts +6 -0
  28. package/dist/prices/index.js +22 -0
  29. package/dist/prices/sources/Backend.d.ts +13 -0
  30. package/dist/prices/sources/Backend.js +52 -0
  31. package/dist/prices/sources/Icp.d.ts +10 -0
  32. package/dist/prices/sources/Icp.js +23 -0
  33. package/dist/prices/sources/Iota.d.ts +39 -0
  34. package/dist/prices/sources/Iota.js +49 -0
  35. package/dist/prices/sources/PriceSource.d.ts +14 -0
  36. package/dist/prices/sources/PriceSource.js +26 -0
  37. package/dist/prices/sources/index.d.ts +4 -0
  38. package/dist/prices/sources/index.js +20 -0
  39. package/dist/prices/utils.d.ts +23 -0
  40. package/dist/prices/utils.js +148 -0
  41. package/dist/types/Master.d.ts +2 -1
  42. package/dist/types/User.d.ts +11 -1
  43. package/dist/utils/userJettonWallet.js +42 -43
  44. package/dist/utils/utils.d.ts +1 -0
  45. package/dist/utils/utils.js +5 -1
  46. package/package.json +11 -9
  47. package/src/api/liquidation.ts +0 -1
  48. package/src/api/math.ts +66 -5
  49. package/src/api/parser.ts +56 -31
  50. package/src/api/prices.ts +20 -7
  51. package/src/constants/assets.ts +57 -0
  52. package/src/constants/general.ts +11 -23
  53. package/src/constants/pools.ts +21 -5
  54. package/src/contracts/MasterContract.ts +8 -1
  55. package/src/index.ts +9 -2
  56. package/src/prices/Prices.ts +32 -0
  57. package/src/prices/PricesCollector.ts +139 -0
  58. package/src/prices/Types.ts +44 -0
  59. package/src/prices/constants.ts +1 -0
  60. package/src/prices/index.ts +6 -0
  61. package/src/prices/sources/Backend.ts +62 -0
  62. package/src/prices/sources/Icp.ts +27 -0
  63. package/src/prices/sources/Iota.ts +90 -0
  64. package/src/prices/sources/PriceSource.ts +35 -0
  65. package/src/prices/sources/index.ts +4 -0
  66. package/src/prices/utils.ts +170 -0
  67. package/src/types/Master.ts +3 -2
  68. package/src/types/User.ts +13 -3
  69. package/src/utils/userJettonWallet.ts +43 -53
  70. package/src/utils/utils.ts +5 -1
  71. package/src/config.ts +0 -1
  72. package/src/types/Common.ts +0 -16
  73. package/src/utils/priceUtils.ts +0 -177
@@ -2,6 +2,7 @@ import { Address, Cell, Contract, ContractProvider, OpenedContract, Sender } fro
2
2
  import { Maybe } from '@ton/core/dist/utils/maybe';
3
3
  import { EvaaUser } from './UserContract';
4
4
  import { MasterData, PoolAssetConfig, PoolConfig } from '../types/Master';
5
+ import { PricesCollector, PriceSourcesConfig } from '..';
5
6
  /**
6
7
  * Parameters for the Evaa contract
7
8
  * @property testnet - true for testnet, false for mainnet
@@ -147,5 +148,9 @@ export declare class Evaa implements Contract {
147
148
  * Sync master contract data
148
149
  */
149
150
  getSync(provider: ContractProvider): Promise<void>;
151
+ /**
152
+ * @deprecated Use PriceCollector (createPriceCollector) istead of getPrices
153
+ */
150
154
  getPrices(provider: ContractProvider, endpoints?: string[]): Promise<import("..").PriceData>;
155
+ createPriceCollector(priceSourcesConfig?: PriceSourcesConfig): PricesCollector;
151
156
  }
@@ -242,6 +242,9 @@ class Evaa {
242
242
  throw Error('Master contract is not active');
243
243
  }
244
244
  }
245
+ /**
246
+ * @deprecated Use PriceCollector (createPriceCollector) istead of getPrices
247
+ */
245
248
  async getPrices(provider, endpoints) {
246
249
  if ((endpoints?.length ?? 0) > 0) {
247
250
  return await (0, __1.getPrices)(endpoints, this._poolConfig);
@@ -250,5 +253,8 @@ class Evaa {
250
253
  return await (0, __1.getPrices)(undefined, this._poolConfig);
251
254
  }
252
255
  }
256
+ createPriceCollector(priceSourcesConfig = __1.DefaultPriceSourcesConfig) {
257
+ return new __1.PricesCollector(this._poolConfig, priceSourcesConfig);
258
+ }
253
259
  }
254
260
  exports.Evaa = Evaa;
package/dist/index.d.ts CHANGED
@@ -1,17 +1,17 @@
1
- export { mulFactor, mulDiv, bigIntMin, bigIntMax, calculatePresentValue, calculateCurrentRates, calculateAssetData, calculateAssetInterest, getAvailableToBorrow, calculateMaximumWithdrawAmount, presentValue, calculateLiquidationData, predictHealthFactor, calculateHealthParams, BigMath, } from './api/math';
1
+ export { mulFactor, mulDiv, bigIntMin, bigIntMax, calculatePresentValue, calculateCurrentRates, calculateAssetData, calculateAssetInterest, getAvailableToBorrow, calculateMaximumWithdrawAmount, presentValue, calculateLiquidationData, predictHealthFactor, calculateHealthParams, calculateInterestWithSupplyBorrow, predictAPY, BigMath, } from './api/math';
2
2
  export { calculateLiquidationAmounts, calculateMinCollateralByTransferredAmount, isLiquidatable, isBadDebt, addReserve, deductReserve, addLiquidationBonus, deductLiquidationBonus, toAssetAmount, toAssetWorth, PreparedAssetInfo, PreparedAssetInfoResult, prepareAssetInfo, findAssetById, selectGreatestAssets, calculateAssetsValues, AssetsValues, SelectedAssets, } from './api/liquidation';
3
3
  export { createAssetData, createAssetConfig, parseMasterData, parseUserData, parseUserLiteData } from './api/parser';
4
4
  export { getPrices } from './api/prices';
5
5
  export { JettonWallet } from './contracts/JettonWallet';
6
6
  export { EvaaParameters, WithdrawParameters, LiquidationBaseData, LiquidationParameters, Evaa } from './contracts/MasterContract';
7
7
  export { EvaaUser } from './contracts/UserContract';
8
- export { PriceData } from './types/Common';
9
8
  export { UpgradeConfig, AssetConfig, MasterConfig, AssetData, AssetInterest, AssetApy, ExtendedAssetData, MasterData, PoolConfig, ExtendedAssetsData, ExtendedAssetsConfig, PoolAssetConfig, PoolAssetsConfig, MasterConstants } from './types/Master';
10
9
  export { BalanceType, UserBalance, UserLiqudationData, LiquidableData, NonLiquidableData, LiquidationData, UserDataInactive, UserDataActive, UserData, BalanceChangeType } from './types/User';
11
10
  export { EVAA_MASTER_MAINNET, MAINNET_VERSION, EVAA_MASTER_TESTNET, TESTNET_VERSION, LENDING_CODE, OPCODES, FEES, MASTER_CONSTANTS } from './constants/general';
12
- export { MAINNET_POOL_CONFIG, TESTNET_POOL_CONFIG, MAINNET_LP_POOL_CONFIG } from './constants/pools';
13
- export { ASSET_ID, UNDEFINED_ASSET, TON_MAINNET, USDT_MAINNET, TONUSDT_DEDUST_MAINNET, TON_STORM_MAINNET, USDT_STORM_MAINNET, JUSDT_MAINNET, JUSDC_MAINNET, STTON_MAINNET, TSTON_MAINNET, JUSDT_TESTNET, JUSDC_TESTNET, STTON_TESTNET } from './constants/assets';
11
+ export { MAINNET_POOL_CONFIG, TESTNET_POOL_CONFIG, MAINNET_LP_POOL_CONFIG, MAINNET_ALTS_POOL_CONFIG } from './constants/pools';
12
+ export { ASSET_ID, UNDEFINED_ASSET, TON_MAINNET, USDT_MAINNET, TONUSDT_DEDUST_MAINNET, TON_STORM_MAINNET, USDT_STORM_MAINNET, DOGS_MAINNET, CATI_MAINNET, UTON_MAINNET, NOT_MAINNET, JUSDT_MAINNET, JUSDC_MAINNET, STTON_MAINNET, TSTON_MAINNET, JUSDT_TESTNET, JUSDC_TESTNET, STTON_TESTNET } from './constants/assets';
14
13
  export * from './constants/assets';
15
14
  export * from './utils/utils';
15
+ export * from './prices';
16
16
  export { getLastSentBoc, getTonConnectSender } from './utils/tonConnectSender';
17
17
  export { getUserJettonWallet } from './utils/userJettonWallet';
package/dist/index.js CHANGED
@@ -14,8 +14,8 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.TESTNET_POOL_CONFIG = exports.MAINNET_POOL_CONFIG = exports.MASTER_CONSTANTS = exports.FEES = exports.OPCODES = exports.LENDING_CODE = exports.TESTNET_VERSION = exports.EVAA_MASTER_TESTNET = exports.MAINNET_VERSION = exports.EVAA_MASTER_MAINNET = exports.BalanceChangeType = exports.BalanceType = exports.EvaaUser = exports.Evaa = exports.JettonWallet = exports.getPrices = exports.parseUserLiteData = exports.parseUserData = exports.parseMasterData = exports.createAssetConfig = exports.createAssetData = exports.calculateAssetsValues = exports.selectGreatestAssets = exports.findAssetById = exports.prepareAssetInfo = exports.toAssetWorth = exports.toAssetAmount = exports.deductLiquidationBonus = exports.addLiquidationBonus = exports.deductReserve = exports.addReserve = exports.isBadDebt = exports.isLiquidatable = exports.calculateMinCollateralByTransferredAmount = exports.calculateLiquidationAmounts = exports.BigMath = exports.calculateHealthParams = exports.predictHealthFactor = exports.calculateLiquidationData = exports.presentValue = exports.calculateMaximumWithdrawAmount = exports.getAvailableToBorrow = exports.calculateAssetInterest = exports.calculateAssetData = exports.calculateCurrentRates = exports.calculatePresentValue = exports.bigIntMax = exports.bigIntMin = exports.mulDiv = exports.mulFactor = void 0;
18
- exports.getUserJettonWallet = exports.getTonConnectSender = exports.getLastSentBoc = exports.STTON_TESTNET = exports.JUSDC_TESTNET = exports.JUSDT_TESTNET = exports.TSTON_MAINNET = exports.STTON_MAINNET = exports.JUSDC_MAINNET = exports.JUSDT_MAINNET = exports.USDT_STORM_MAINNET = exports.TON_STORM_MAINNET = exports.TONUSDT_DEDUST_MAINNET = exports.USDT_MAINNET = exports.TON_MAINNET = exports.UNDEFINED_ASSET = exports.ASSET_ID = exports.MAINNET_LP_POOL_CONFIG = void 0;
17
+ exports.MASTER_CONSTANTS = exports.FEES = exports.OPCODES = exports.LENDING_CODE = exports.TESTNET_VERSION = exports.EVAA_MASTER_TESTNET = exports.MAINNET_VERSION = exports.EVAA_MASTER_MAINNET = exports.BalanceChangeType = exports.BalanceType = exports.EvaaUser = exports.Evaa = exports.JettonWallet = exports.getPrices = exports.parseUserLiteData = exports.parseUserData = exports.parseMasterData = exports.createAssetConfig = exports.createAssetData = exports.calculateAssetsValues = exports.selectGreatestAssets = exports.findAssetById = exports.prepareAssetInfo = exports.toAssetWorth = exports.toAssetAmount = exports.deductLiquidationBonus = exports.addLiquidationBonus = exports.deductReserve = exports.addReserve = exports.isBadDebt = exports.isLiquidatable = exports.calculateMinCollateralByTransferredAmount = exports.calculateLiquidationAmounts = exports.BigMath = exports.predictAPY = exports.calculateInterestWithSupplyBorrow = exports.calculateHealthParams = exports.predictHealthFactor = exports.calculateLiquidationData = exports.presentValue = exports.calculateMaximumWithdrawAmount = exports.getAvailableToBorrow = exports.calculateAssetInterest = exports.calculateAssetData = exports.calculateCurrentRates = exports.calculatePresentValue = exports.bigIntMax = exports.bigIntMin = exports.mulDiv = exports.mulFactor = void 0;
18
+ exports.getUserJettonWallet = exports.getTonConnectSender = exports.getLastSentBoc = exports.STTON_TESTNET = exports.JUSDC_TESTNET = exports.JUSDT_TESTNET = exports.TSTON_MAINNET = exports.STTON_MAINNET = exports.JUSDC_MAINNET = exports.JUSDT_MAINNET = exports.NOT_MAINNET = exports.UTON_MAINNET = exports.CATI_MAINNET = exports.DOGS_MAINNET = exports.USDT_STORM_MAINNET = exports.TON_STORM_MAINNET = exports.TONUSDT_DEDUST_MAINNET = exports.USDT_MAINNET = exports.TON_MAINNET = exports.UNDEFINED_ASSET = exports.ASSET_ID = exports.MAINNET_ALTS_POOL_CONFIG = exports.MAINNET_LP_POOL_CONFIG = exports.TESTNET_POOL_CONFIG = exports.MAINNET_POOL_CONFIG = void 0;
19
19
  // Math
20
20
  var math_1 = require("./api/math");
21
21
  Object.defineProperty(exports, "mulFactor", { enumerable: true, get: function () { return math_1.mulFactor; } });
@@ -32,6 +32,8 @@ Object.defineProperty(exports, "presentValue", { enumerable: true, get: function
32
32
  Object.defineProperty(exports, "calculateLiquidationData", { enumerable: true, get: function () { return math_1.calculateLiquidationData; } });
33
33
  Object.defineProperty(exports, "predictHealthFactor", { enumerable: true, get: function () { return math_1.predictHealthFactor; } });
34
34
  Object.defineProperty(exports, "calculateHealthParams", { enumerable: true, get: function () { return math_1.calculateHealthParams; } });
35
+ Object.defineProperty(exports, "calculateInterestWithSupplyBorrow", { enumerable: true, get: function () { return math_1.calculateInterestWithSupplyBorrow; } });
36
+ Object.defineProperty(exports, "predictAPY", { enumerable: true, get: function () { return math_1.predictAPY; } });
35
37
  Object.defineProperty(exports, "BigMath", { enumerable: true, get: function () { return math_1.BigMath; } });
36
38
  var liquidation_1 = require("./api/liquidation");
37
39
  Object.defineProperty(exports, "calculateLiquidationAmounts", { enumerable: true, get: function () { return liquidation_1.calculateLiquidationAmounts; } });
@@ -82,6 +84,7 @@ var pools_1 = require("./constants/pools");
82
84
  Object.defineProperty(exports, "MAINNET_POOL_CONFIG", { enumerable: true, get: function () { return pools_1.MAINNET_POOL_CONFIG; } });
83
85
  Object.defineProperty(exports, "TESTNET_POOL_CONFIG", { enumerable: true, get: function () { return pools_1.TESTNET_POOL_CONFIG; } });
84
86
  Object.defineProperty(exports, "MAINNET_LP_POOL_CONFIG", { enumerable: true, get: function () { return pools_1.MAINNET_LP_POOL_CONFIG; } });
87
+ Object.defineProperty(exports, "MAINNET_ALTS_POOL_CONFIG", { enumerable: true, get: function () { return pools_1.MAINNET_ALTS_POOL_CONFIG; } });
85
88
  var assets_1 = require("./constants/assets");
86
89
  Object.defineProperty(exports, "ASSET_ID", { enumerable: true, get: function () { return assets_1.ASSET_ID; } });
87
90
  Object.defineProperty(exports, "UNDEFINED_ASSET", { enumerable: true, get: function () { return assets_1.UNDEFINED_ASSET; } });
@@ -90,6 +93,10 @@ Object.defineProperty(exports, "USDT_MAINNET", { enumerable: true, get: function
90
93
  Object.defineProperty(exports, "TONUSDT_DEDUST_MAINNET", { enumerable: true, get: function () { return assets_1.TONUSDT_DEDUST_MAINNET; } });
91
94
  Object.defineProperty(exports, "TON_STORM_MAINNET", { enumerable: true, get: function () { return assets_1.TON_STORM_MAINNET; } });
92
95
  Object.defineProperty(exports, "USDT_STORM_MAINNET", { enumerable: true, get: function () { return assets_1.USDT_STORM_MAINNET; } });
96
+ Object.defineProperty(exports, "DOGS_MAINNET", { enumerable: true, get: function () { return assets_1.DOGS_MAINNET; } });
97
+ Object.defineProperty(exports, "CATI_MAINNET", { enumerable: true, get: function () { return assets_1.CATI_MAINNET; } });
98
+ Object.defineProperty(exports, "UTON_MAINNET", { enumerable: true, get: function () { return assets_1.UTON_MAINNET; } });
99
+ Object.defineProperty(exports, "NOT_MAINNET", { enumerable: true, get: function () { return assets_1.NOT_MAINNET; } });
93
100
  Object.defineProperty(exports, "JUSDT_MAINNET", { enumerable: true, get: function () { return assets_1.JUSDT_MAINNET; } });
94
101
  Object.defineProperty(exports, "JUSDC_MAINNET", { enumerable: true, get: function () { return assets_1.JUSDC_MAINNET; } });
95
102
  Object.defineProperty(exports, "STTON_MAINNET", { enumerable: true, get: function () { return assets_1.STTON_MAINNET; } });
@@ -99,6 +106,7 @@ Object.defineProperty(exports, "JUSDC_TESTNET", { enumerable: true, get: functio
99
106
  Object.defineProperty(exports, "STTON_TESTNET", { enumerable: true, get: function () { return assets_1.STTON_TESTNET; } });
100
107
  __exportStar(require("./constants/assets"), exports);
101
108
  __exportStar(require("./utils/utils"), exports);
109
+ __exportStar(require("./prices"), exports);
102
110
  // Utils
103
111
  var tonConnectSender_1 = require("./utils/tonConnectSender");
104
112
  Object.defineProperty(exports, "getLastSentBoc", { enumerable: true, get: function () { return tonConnectSender_1.getLastSentBoc; } });
@@ -0,0 +1,9 @@
1
+ import { Cell, Dictionary } from "@ton/core";
2
+ import { PoolAssetConfig } from "../types/Master";
3
+ export declare class Prices {
4
+ #private;
5
+ constructor(dict: Dictionary<bigint, bigint>, dataCell: Cell);
6
+ get dict(): Dictionary<bigint, bigint>;
7
+ get dataCell(): Cell;
8
+ getAssetPrice<T extends bigint | PoolAssetConfig>(asset: T): bigint | undefined;
9
+ }
@@ -0,0 +1,43 @@
1
+ "use strict";
2
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
3
+ if (kind === "m") throw new TypeError("Private method is not writable");
4
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
5
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
6
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
7
+ };
8
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
9
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
10
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
11
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
+ };
13
+ var _Prices_instances, _Prices_dict, _Prices_dataCell, _Prices_extractAssetId;
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.Prices = void 0;
16
+ const core_1 = require("@ton/core");
17
+ class Prices {
18
+ constructor(dict, dataCell) {
19
+ _Prices_instances.add(this);
20
+ _Prices_dict.set(this, void 0);
21
+ _Prices_dataCell.set(this, void 0);
22
+ __classPrivateFieldSet(this, _Prices_dict, dict, "f");
23
+ __classPrivateFieldSet(this, _Prices_dataCell, dataCell, "f");
24
+ }
25
+ get dict() {
26
+ const dict = core_1.Dictionary.empty();
27
+ for (const [key, value] of __classPrivateFieldGet(this, _Prices_dict, "f")) {
28
+ dict.set(key, value);
29
+ }
30
+ return dict;
31
+ }
32
+ get dataCell() {
33
+ return new core_1.Cell(__classPrivateFieldGet(this, _Prices_dataCell, "f"));
34
+ }
35
+ getAssetPrice(asset) {
36
+ const assetId = __classPrivateFieldGet(this, _Prices_instances, "m", _Prices_extractAssetId).call(this, asset);
37
+ return __classPrivateFieldGet(this, _Prices_dict, "f").get(assetId);
38
+ }
39
+ }
40
+ exports.Prices = Prices;
41
+ _Prices_dict = new WeakMap(), _Prices_dataCell = new WeakMap(), _Prices_instances = new WeakSet(), _Prices_extractAssetId = function _Prices_extractAssetId(asset) {
42
+ return typeof asset === 'bigint' ? asset : asset.assetId;
43
+ };
@@ -0,0 +1,12 @@
1
+ import { Dictionary } from "@ton/core";
2
+ import { PoolAssetConfig, PoolAssetsConfig, PoolConfig } from "../types/Master";
3
+ import { PriceSource } from "./sources";
4
+ import { PriceSourcesConfig } from "./Types";
5
+ import { Prices } from "./Prices";
6
+ export declare class PricesCollector {
7
+ #private;
8
+ constructor(poolConfig?: PoolConfig, sourcesConfig?: PriceSourcesConfig, additionalPriceSources?: PriceSource[]);
9
+ getPricesForLiquidate(realPrincipals: Dictionary<bigint, bigint>, retries?: number, timeout?: number): Promise<Prices>;
10
+ getPricesForWithdraw(realPrincipals: Dictionary<bigint, bigint>, withdrawAsset: PoolAssetConfig, collateralToDebt?: boolean, retries?: number, timeout?: number): Promise<Prices>;
11
+ getPrices(assets?: PoolAssetsConfig, retries?: number, timeout?: number): Promise<Prices>;
12
+ }
@@ -0,0 +1,123 @@
1
+ "use strict";
2
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
3
+ if (kind === "m") throw new TypeError("Private method is not writable");
4
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
5
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
6
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
7
+ };
8
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
9
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
10
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
11
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
+ };
13
+ var _PricesCollector_instances, _PricesCollector_prices, _PricesCollector_poolConfig, _PricesCollector_sourcesConfig, _PricesCollector_priceSources, _PricesCollector_getPricesByAssetList, _PricesCollector_collectPrices, _PricesCollector_filterPrices, _PricesCollector_filterEmptyPrincipalsAndAssets;
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.PricesCollector = void 0;
16
+ const core_1 = require("@ton/core");
17
+ const pools_1 = require("../constants/pools");
18
+ const Types_1 = require("./Types");
19
+ const utils_1 = require("./utils");
20
+ const utils_2 = require("../utils/utils");
21
+ const Prices_1 = require("./Prices");
22
+ const math_1 = require("../api/math");
23
+ class PricesCollector {
24
+ constructor(poolConfig = pools_1.MAINNET_POOL_CONFIG, sourcesConfig = Types_1.DefaultPriceSourcesConfig, additionalPriceSources) {
25
+ _PricesCollector_instances.add(this);
26
+ _PricesCollector_prices.set(this, void 0);
27
+ _PricesCollector_poolConfig.set(this, void 0);
28
+ _PricesCollector_sourcesConfig.set(this, void 0);
29
+ _PricesCollector_priceSources.set(this, void 0);
30
+ __classPrivateFieldSet(this, _PricesCollector_poolConfig, poolConfig, "f");
31
+ __classPrivateFieldSet(this, _PricesCollector_sourcesConfig, sourcesConfig, "f");
32
+ __classPrivateFieldSet(this, _PricesCollector_priceSources, (0, utils_1.generatePriceSources)(__classPrivateFieldGet(this, _PricesCollector_sourcesConfig, "f"), __classPrivateFieldGet(this, _PricesCollector_poolConfig, "f").oracles), "f");
33
+ if (additionalPriceSources) {
34
+ __classPrivateFieldGet(this, _PricesCollector_priceSources, "f").push(...additionalPriceSources);
35
+ }
36
+ __classPrivateFieldSet(this, _PricesCollector_prices, [], "f");
37
+ }
38
+ // TODO Make UserData class and incapsulate raw bigintegers
39
+ async getPricesForLiquidate(realPrincipals, retries = 1, timeout = 3000) {
40
+ const assets = __classPrivateFieldGet(this, _PricesCollector_instances, "m", _PricesCollector_filterEmptyPrincipalsAndAssets).call(this, realPrincipals);
41
+ if (assets.includes(undefined)) {
42
+ throw new Error("User from another pool");
43
+ }
44
+ return await this.getPrices(assets.map(x => x), retries, timeout);
45
+ }
46
+ async getPricesForWithdraw(realPrincipals, withdrawAsset, collateralToDebt = false, retries = 1, timeout = 3000) {
47
+ let assets = __classPrivateFieldGet(this, _PricesCollector_instances, "m", _PricesCollector_filterEmptyPrincipalsAndAssets).call(this, realPrincipals);
48
+ if ((0, math_1.checkNotInDebtAtAll)(realPrincipals) && (realPrincipals.get(withdrawAsset.assetId) ?? 0n) > 0n && !collateralToDebt) {
49
+ return new Prices_1.Prices(core_1.Dictionary.empty(), core_1.Cell.EMPTY);
50
+ }
51
+ if (assets.includes(undefined)) {
52
+ throw new Error("User from another pool");
53
+ }
54
+ if (!assets.includes(withdrawAsset)) {
55
+ assets.push(withdrawAsset);
56
+ }
57
+ if (collateralToDebt && assets.length == 1) {
58
+ throw new Error("Cannot debt only one supplied asset");
59
+ }
60
+ return await this.getPrices(assets.map(x => x), retries, timeout);
61
+ }
62
+ async getPrices(assets = __classPrivateFieldGet(this, _PricesCollector_poolConfig, "f").poolAssetsConfig, retries = 1, timeout = 3000) {
63
+ console.debug('[getPrices] Assets length', assets.length);
64
+ if (assets.length == 0) {
65
+ return new Prices_1.Prices(core_1.Dictionary.empty(), core_1.Cell.EMPTY);
66
+ }
67
+ for (let i = 0; i <= retries; i++) { // attemts = retries + 1
68
+ if (!__classPrivateFieldGet(this, _PricesCollector_prices, "f") || __classPrivateFieldGet(this, _PricesCollector_instances, "m", _PricesCollector_filterPrices).call(this) < __classPrivateFieldGet(this, _PricesCollector_poolConfig, "f").minimalOracles) {
69
+ //console.debug('[getPrices] Load prices attemp', i + 1)
70
+ if (i > 0) {
71
+ await (0, utils_2.delay)(timeout);
72
+ }
73
+ await __classPrivateFieldGet(this, _PricesCollector_instances, "m", _PricesCollector_collectPrices).call(this);
74
+ }
75
+ else {
76
+ break;
77
+ }
78
+ }
79
+ if (__classPrivateFieldGet(this, _PricesCollector_prices, "f").length < __classPrivateFieldGet(this, _PricesCollector_poolConfig, "f").minimalOracles) {
80
+ throw new Error(`Error per updating prices, valid ${__classPrivateFieldGet(this, _PricesCollector_prices, "f").length} of ${__classPrivateFieldGet(this, _PricesCollector_poolConfig, "f").minimalOracles}`); // if still not enough data after retries
81
+ }
82
+ const prices = __classPrivateFieldGet(this, _PricesCollector_instances, "m", _PricesCollector_getPricesByAssetList).call(this, assets);
83
+ return new Prices_1.Prices(prices.dict, prices.dataCell);
84
+ }
85
+ }
86
+ exports.PricesCollector = PricesCollector;
87
+ _PricesCollector_prices = new WeakMap(), _PricesCollector_poolConfig = new WeakMap(), _PricesCollector_sourcesConfig = new WeakMap(), _PricesCollector_priceSources = new WeakMap(), _PricesCollector_instances = new WeakSet(), _PricesCollector_getPricesByAssetList = function _PricesCollector_getPricesByAssetList(assets) {
88
+ //console.debug('[getPricesByAssetList] start')
89
+ let pricesFiltered = __classPrivateFieldGet(this, _PricesCollector_prices, "f"); // for strict check this.#prices.filter(x => assets.every(asset => x.dict.has(asset.assetId)));
90
+ if (pricesFiltered.length < __classPrivateFieldGet(this, _PricesCollector_poolConfig, "f").minimalOracles) {
91
+ throw new Error("Not enough price data");
92
+ }
93
+ if (pricesFiltered.length > __classPrivateFieldGet(this, _PricesCollector_poolConfig, "f").minimalOracles) {
94
+ const sortedByTimestamp = pricesFiltered.slice().sort((a, b) => b.timestamp - a.timestamp);
95
+ const newerPrices = sortedByTimestamp.slice(0, __classPrivateFieldGet(this, _PricesCollector_poolConfig, "f").minimalOracles);
96
+ pricesFiltered = newerPrices.sort((a, b) => a.oracleId - b.oracleId);
97
+ }
98
+ const medianData = assets.map(asset => ({ assetId: asset.assetId, medianPrice: (0, utils_1.getMedianPrice)(__classPrivateFieldGet(this, _PricesCollector_prices, "f"), asset.assetId) }));
99
+ const nonEmptymedianData = medianData.filter(x => x.medianPrice != null);
100
+ const packedMedianData = (0, utils_1.packAssetsData)(nonEmptymedianData);
101
+ const oraclesData = __classPrivateFieldGet(this, _PricesCollector_prices, "f").map(x => ({ oracle: { id: x.oracleId, pubkey: x.pubkey }, data: { timestamp: x.timestamp, prices: x.dict }, signature: x.signature }));
102
+ const packedOracleData = (0, utils_1.packOraclesData)(oraclesData, nonEmptymedianData.map(x => x.assetId));
103
+ const dict = core_1.Dictionary.empty();
104
+ for (const medianDataAsset of nonEmptymedianData) {
105
+ dict.set(medianDataAsset.assetId, medianDataAsset.medianPrice);
106
+ }
107
+ return {
108
+ dict: dict,
109
+ dataCell: (0, utils_1.packPrices)(packedMedianData, packedOracleData)
110
+ };
111
+ }, _PricesCollector_collectPrices = async function _PricesCollector_collectPrices() {
112
+ try {
113
+ __classPrivateFieldSet(this, _PricesCollector_prices, await Promise.any(__classPrivateFieldGet(this, _PricesCollector_priceSources, "f").map(x => (0, utils_1.collectAndFilterPrices)(x, __classPrivateFieldGet(this, _PricesCollector_poolConfig, "f")))), "f");
114
+ return true;
115
+ }
116
+ catch { }
117
+ return false;
118
+ }, _PricesCollector_filterPrices = function _PricesCollector_filterPrices() {
119
+ __classPrivateFieldSet(this, _PricesCollector_prices, __classPrivateFieldGet(this, _PricesCollector_prices, "f").filter((0, utils_1.verifyPricesTimestamp)()), "f");
120
+ return __classPrivateFieldGet(this, _PricesCollector_prices, "f").length;
121
+ }, _PricesCollector_filterEmptyPrincipalsAndAssets = function _PricesCollector_filterEmptyPrincipalsAndAssets(principals) {
122
+ return principals.keys().filter(x => principals.get(x) != 0n).map(x => __classPrivateFieldGet(this, _PricesCollector_poolConfig, "f").poolAssetsConfig.find(asset => asset.assetId == x));
123
+ };
@@ -0,0 +1,33 @@
1
+ /// <reference types="node" />
2
+ import { Cell, Dictionary } from '@ton/core';
3
+ /**
4
+ * Configuration for price source endpoints.
5
+ */
6
+ export type PriceSourcesConfig = {
7
+ /** Endpoints for backend price data */
8
+ backendEndpoints: string[];
9
+ /** Endpoints for IOTA price data */
10
+ iotaEndpoints: string[];
11
+ /** Endpoints for ICP price data */
12
+ icpEndpoints: string[];
13
+ };
14
+ /**
15
+ * Default configuration for price source endpoints.
16
+ */
17
+ export declare const DefaultPriceSourcesConfig: PriceSourcesConfig;
18
+ export type RawPriceData = {
19
+ dict: Dictionary<bigint, bigint>;
20
+ dataCell: Cell;
21
+ oracleId: number;
22
+ signature: Buffer;
23
+ pubkey: Buffer;
24
+ timestamp: number;
25
+ };
26
+ export type PriceData = {
27
+ dict: Dictionary<bigint, bigint>;
28
+ dataCell: Cell;
29
+ };
30
+ export type OraclePricesData = {
31
+ timestamp: number;
32
+ prices: Dictionary<bigint, bigint>;
33
+ };
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DefaultPriceSourcesConfig = void 0;
4
+ /**
5
+ * Default configuration for price source endpoints.
6
+ */
7
+ exports.DefaultPriceSourcesConfig = {
8
+ backendEndpoints: ['evaa.space'],
9
+ iotaEndpoints: ['api.stardust-mainnet.iotaledger.net'],
10
+ icpEndpoints: ['6khmc-aiaaa-aaaap-ansfq-cai.raw.icp0.io'],
11
+ };
@@ -0,0 +1 @@
1
+ export declare const TTL_ORACLE_DATA_SEC = 120;
@@ -0,0 +1,4 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TTL_ORACLE_DATA_SEC = void 0;
4
+ exports.TTL_ORACLE_DATA_SEC = 120;
@@ -0,0 +1,6 @@
1
+ export * from './sources';
2
+ export * from './Types';
3
+ export * from './constants';
4
+ export * from './utils';
5
+ export * from './Prices';
6
+ export * from './PricesCollector';
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./sources"), exports);
18
+ __exportStar(require("./Types"), exports);
19
+ __exportStar(require("./constants"), exports);
20
+ __exportStar(require("./utils"), exports);
21
+ __exportStar(require("./Prices"), exports);
22
+ __exportStar(require("./PricesCollector"), exports);
@@ -0,0 +1,13 @@
1
+ import { RawPriceData } from "..";
2
+ import { PriceSource } from "./PriceSource";
3
+ export declare class BackendPriceSource extends PriceSource {
4
+ protected priceSourceName: string;
5
+ getPrices(): Promise<RawPriceData[]>;
6
+ loadOracleData(): Promise<OutputData[]>;
7
+ parsePrices(outputData: OutputData): RawPriceData;
8
+ }
9
+ type OutputData = {
10
+ oracleId: number;
11
+ data: string;
12
+ };
13
+ export {};
@@ -0,0 +1,52 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BackendPriceSource = void 0;
4
+ const core_1 = require("@ton/core");
5
+ const PriceSource_1 = require("./PriceSource");
6
+ class BackendPriceSource extends PriceSource_1.PriceSource {
7
+ constructor() {
8
+ super(...arguments);
9
+ this.priceSourceName = 'BackendPriceSource';
10
+ }
11
+ async getPrices() {
12
+ const data = await this.loadOracleData();
13
+ return data.map(outputData => this.parsePrices(outputData));
14
+ }
15
+ async loadOracleData() {
16
+ let response = await fetch(`https://${this._endpoint}/api/prices`, {
17
+ headers: { accept: 'application/json' },
18
+ signal: AbortSignal.timeout(5000)
19
+ });
20
+ const resp = (await response.json());
21
+ const data = resp;
22
+ let outputData = [];
23
+ for (const nft of this._nfts) {
24
+ outputData.push({ oracleId: nft.id, data: data[nft.address] });
25
+ }
26
+ return outputData;
27
+ }
28
+ parsePrices(outputData) {
29
+ try {
30
+ //console.debug('outputData', outputData);
31
+ //console.debug(outputData.data.replace('0x', '').replace(/[0-9a-f]{2}/g, '%$&'))
32
+ const data = JSON.parse(decodeURIComponent(outputData.data.replace('0x', '').replace(/[0-9a-f]{2}/g, '%$&')));
33
+ const pricesCell = core_1.Cell.fromBoc(Buffer.from(data['packedPrices'], 'hex'))[0];
34
+ const signature = Buffer.from(data['signature'], 'hex');
35
+ const publicKey = Buffer.from(data['publicKey'], 'hex');
36
+ const timestamp = Number(data['timestamp']);
37
+ return {
38
+ dict: pricesCell.beginParse().loadRef().beginParse().loadDictDirect(core_1.Dictionary.Keys.BigUint(256), core_1.Dictionary.Values.BigVarUint(4)),
39
+ dataCell: (0, core_1.beginCell)().storeRef(pricesCell).storeBuffer(signature).endCell(),
40
+ oracleId: outputData.oracleId,
41
+ signature: signature,
42
+ pubkey: publicKey,
43
+ timestamp: timestamp,
44
+ };
45
+ }
46
+ catch (error) {
47
+ //console.debug(`Price source error ${this.priceSourceName} ${outputData.oracleId} ${outputData.data} ${error}`);
48
+ throw error;
49
+ }
50
+ }
51
+ }
52
+ exports.BackendPriceSource = BackendPriceSource;
@@ -0,0 +1,10 @@
1
+ import { BackendPriceSource } from ".";
2
+ export declare class IcpPriceSource extends BackendPriceSource {
3
+ protected priceSourceName: string;
4
+ loadOracleData(): Promise<OutputData[]>;
5
+ }
6
+ type OutputData = {
7
+ oracleId: number;
8
+ data: string;
9
+ };
10
+ export {};
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.IcpPriceSource = void 0;
4
+ const _1 = require(".");
5
+ class IcpPriceSource extends _1.BackendPriceSource {
6
+ constructor() {
7
+ super(...arguments);
8
+ this.priceSourceName = 'IcpPriceSource';
9
+ }
10
+ async loadOracleData() {
11
+ let response = await fetch(`https://${this._endpoint}/prices`, {
12
+ headers: { accept: 'application/json' },
13
+ signal: AbortSignal.timeout(5000)
14
+ });
15
+ const data = (await response.json());
16
+ let outputData = [];
17
+ for (const nft of this._nfts) {
18
+ outputData.push({ oracleId: nft.id, data: data[nft.address] });
19
+ }
20
+ return outputData;
21
+ }
22
+ }
23
+ exports.IcpPriceSource = IcpPriceSource;
@@ -0,0 +1,39 @@
1
+ import { PriceSource } from "./PriceSource";
2
+ import { RawPriceData } from "..";
3
+ export declare class IotaPriceSource extends PriceSource {
4
+ protected priceSourceName: string;
5
+ getPrices(): Promise<RawPriceData[]>;
6
+ loadOracleData(oracleNftId: String): Promise<OutputData>;
7
+ parsePrices(outputData: OutputData, oracleId: number): RawPriceData;
8
+ }
9
+ type OutputData = {
10
+ metadata: {
11
+ blockId: string;
12
+ transactionId: string;
13
+ outputIndex: number;
14
+ isSpent: boolean;
15
+ milestoneIndexSpent: number;
16
+ milestoneTimestampSpent: number;
17
+ transactionIdSpent: string;
18
+ milestoneIndexBooked: number;
19
+ milestoneTimestampBooked: number;
20
+ ledgerIndex: number;
21
+ };
22
+ output: {
23
+ type: number;
24
+ amount: string;
25
+ nftId: string;
26
+ unlockConditions: {
27
+ type: number;
28
+ address: {
29
+ type: number;
30
+ pubKeyHash: string;
31
+ };
32
+ }[];
33
+ features: {
34
+ type: number;
35
+ data: string;
36
+ }[];
37
+ };
38
+ };
39
+ export {};
@@ -0,0 +1,49 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.IotaPriceSource = void 0;
4
+ const core_1 = require("@ton/core");
5
+ const PriceSource_1 = require("./PriceSource");
6
+ class IotaPriceSource extends PriceSource_1.PriceSource {
7
+ constructor() {
8
+ super(...arguments);
9
+ this.priceSourceName = 'IotaPriceSource';
10
+ }
11
+ async getPrices() {
12
+ return await Promise.all(this.nfts.map(nft => this.loadOracleData(nft.address)
13
+ .then(x => this.parsePrices(x, nft.id))));
14
+ }
15
+ async loadOracleData(oracleNftId) {
16
+ let result = await fetch(`https://${this._endpoint}/api/indexer/v1/outputs/nft/${oracleNftId}`, {
17
+ headers: { accept: 'application/json' },
18
+ signal: AbortSignal.timeout(5000)
19
+ });
20
+ let outputId = (await result.json());
21
+ result = await fetch(`https://${this._endpoint}/api/core/v2/outputs/${outputId.items[0]}`, {
22
+ headers: { accept: 'application/json' },
23
+ signal: AbortSignal.timeout(5000)
24
+ });
25
+ return await result.json();
26
+ }
27
+ parsePrices(outputData, oracleId) {
28
+ const data = JSON.parse(decodeURIComponent(outputData.output.features[0].data.replace('0x', '').replace(/[0-9a-f]{2}/g, '%$&')));
29
+ try {
30
+ const pricesCell = core_1.Cell.fromBoc(Buffer.from(data['packedPrices'], 'hex'))[0];
31
+ const signature = Buffer.from(data['signature'], 'hex');
32
+ const publicKey = Buffer.from(data['publicKey'], 'hex');
33
+ const timestamp = Number(data['timestamp']);
34
+ return {
35
+ dict: pricesCell.beginParse().loadRef().beginParse().loadDictDirect(core_1.Dictionary.Keys.BigUint(256), core_1.Dictionary.Values.BigVarUint(4)),
36
+ dataCell: (0, core_1.beginCell)().storeRef(pricesCell).storeBuffer(signature).endCell(),
37
+ oracleId: oracleId,
38
+ signature: signature,
39
+ pubkey: publicKey,
40
+ timestamp: timestamp,
41
+ };
42
+ }
43
+ catch (error) {
44
+ //console.debug(`Price source error ${this.priceSourceName} ${outputData} ${error}`);
45
+ throw error;
46
+ }
47
+ }
48
+ }
49
+ exports.IotaPriceSource = IotaPriceSource;
@@ -0,0 +1,14 @@
1
+ import { RawPriceData } from "..";
2
+ import { OracleNFT } from "../../types/Master";
3
+ export declare abstract class PriceSource {
4
+ protected priceSourceName: string;
5
+ protected _endpoint: string;
6
+ protected _nfts: OracleNFT[];
7
+ constructor(endpoint: string, nfts: OracleNFT[]);
8
+ get sourceName(): string;
9
+ get endpoint(): string;
10
+ get nfts(): OracleNFT[];
11
+ set endpoint(endpoint: string);
12
+ set nfts(nfts: OracleNFT[]);
13
+ abstract getPrices(): Promise<RawPriceData[]>;
14
+ }
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PriceSource = void 0;
4
+ class PriceSource {
5
+ constructor(endpoint, nfts) {
6
+ this.priceSourceName = 'BackendPriceSource';
7
+ this._endpoint = endpoint;
8
+ this._nfts = nfts;
9
+ }
10
+ get sourceName() {
11
+ return this.priceSourceName;
12
+ }
13
+ get endpoint() {
14
+ return this._endpoint;
15
+ }
16
+ get nfts() {
17
+ return this._nfts;
18
+ }
19
+ set endpoint(endpoint) {
20
+ this._endpoint = endpoint;
21
+ }
22
+ set nfts(nfts) {
23
+ this._nfts = nfts;
24
+ }
25
+ }
26
+ exports.PriceSource = PriceSource;
@@ -0,0 +1,4 @@
1
+ export * from './Backend';
2
+ export * from './Iota';
3
+ export * from './Icp';
4
+ export * from './PriceSource';