@evaafi/sdk 0.9.0-a → 0.9.1
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/api/feeds.d.ts +15 -24
- package/dist/api/feeds.js +22 -49
- package/dist/api/math.d.ts +1 -1
- package/dist/api/math.js +55 -38
- package/dist/api/parser.d.ts +2 -2
- package/dist/api/parser.js +3 -3
- package/dist/api/parsers/PythOracleParser.d.ts +3 -3
- package/dist/api/parsers/PythOracleParser.js +2 -1
- package/dist/api/prices.js +2 -1
- package/dist/constants/general/index.d.ts +25 -11
- package/dist/constants/general/index.js +15 -1
- package/dist/constants/pools/mainnet.js +20 -18
- package/dist/constants/pools/testnet.js +14 -6
- package/dist/contracts/AbstractMaster.d.ts +239 -127
- package/dist/contracts/AbstractMaster.js +101 -16
- package/dist/contracts/ClassicMaster.d.ts +12 -12
- package/dist/contracts/PythMaster.d.ts +40 -24
- package/dist/contracts/PythMaster.js +61 -76
- package/dist/contracts/PythOracle.d.ts +16 -0
- package/dist/contracts/PythOracle.js +19 -0
- package/dist/contracts/index.d.ts +1 -0
- package/dist/contracts/index.js +1 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/{prices → oracles}/Types.d.ts +0 -4
- package/dist/oracles/collectors/AbstractCollector.d.ts +10 -0
- package/dist/oracles/collectors/AbstractCollector.js +6 -0
- package/dist/oracles/collectors/ClassicCollector.d.ts +22 -0
- package/dist/oracles/collectors/ClassicCollector.js +192 -0
- package/dist/oracles/collectors/PythCollector.d.ts +27 -0
- package/dist/oracles/collectors/PythCollector.js +252 -0
- package/dist/oracles/collectors/index.d.ts +3 -0
- package/dist/oracles/collectors/index.js +19 -0
- package/dist/{prices → oracles}/index.d.ts +2 -3
- package/dist/{prices → oracles}/index.js +2 -3
- package/dist/oracles/prices/AbstractPrices.d.ts +33 -0
- package/dist/oracles/prices/AbstractPrices.js +40 -0
- package/dist/oracles/prices/ClassicPrices.d.ts +19 -0
- package/dist/oracles/prices/ClassicPrices.js +48 -0
- package/dist/oracles/prices/PythPrices.d.ts +18 -0
- package/dist/oracles/prices/PythPrices.js +32 -0
- package/dist/oracles/prices/index.d.ts +3 -0
- package/dist/oracles/prices/index.js +19 -0
- package/dist/{prices → oracles}/utils.d.ts +6 -1
- package/dist/{prices → oracles}/utils.js +10 -1
- package/dist/types/Master.d.ts +4 -5
- package/package.json +2 -3
- package/src/api/feeds.ts +24 -60
- package/src/api/math.ts +118 -90
- package/src/api/parser.ts +5 -5
- package/src/api/parsers/PythOracleParser.ts +6 -2
- package/src/api/prices.ts +2 -1
- package/src/constants/general/index.ts +16 -1
- package/src/constants/pools/mainnet.ts +20 -35
- package/src/constants/pools/testnet.ts +17 -8
- package/src/contracts/AbstractMaster.ts +272 -144
- package/src/contracts/ClassicMaster.ts +12 -12
- package/src/contracts/PythMaster.ts +130 -123
- package/src/contracts/PythOracle.ts +20 -0
- package/src/contracts/index.ts +2 -0
- package/src/index.ts +1 -1
- package/src/{prices → oracles}/Types.ts +0 -5
- package/src/oracles/collectors/AbstractCollector.ts +22 -0
- package/src/oracles/collectors/ClassicCollector.ts +263 -0
- package/src/oracles/collectors/PythCollector.ts +358 -0
- package/src/oracles/collectors/index.ts +3 -0
- package/src/{prices → oracles}/index.ts +2 -3
- package/src/oracles/prices/AbstractPrices.ts +59 -0
- package/src/oracles/prices/ClassicPrices.ts +52 -0
- package/src/oracles/prices/PythPrices.ts +40 -0
- package/src/oracles/prices/index.ts +3 -0
- package/src/{prices → oracles}/utils.ts +12 -1
- package/src/types/Master.ts +4 -6
- package/dist/prices/Oracle.interface.d.ts +0 -9
- package/dist/prices/Oracle.interface.js +0 -2
- package/dist/prices/Prices.d.ts +0 -11
- package/dist/prices/Prices.js +0 -53
- package/dist/prices/PricesCollector.d.ts +0 -22
- package/dist/prices/PricesCollector.js +0 -146
- package/dist/prices/PythCollector.d.ts +0 -22
- package/dist/prices/PythCollector.js +0 -217
- package/src/prices/Oracle.interface.ts +0 -18
- package/src/prices/Prices.ts +0 -45
- package/src/prices/PricesCollector.ts +0 -169
- package/src/prices/PythCollector.ts +0 -294
- /package/dist/{prices → oracles}/Types.js +0 -0
- /package/dist/{prices → oracles}/constants.d.ts +0 -0
- /package/dist/{prices → oracles}/constants.js +0 -0
- /package/dist/{prices → oracles}/sources/Backend.d.ts +0 -0
- /package/dist/{prices → oracles}/sources/Backend.js +0 -0
- /package/dist/{prices → oracles}/sources/Icp.d.ts +0 -0
- /package/dist/{prices → oracles}/sources/Icp.js +0 -0
- /package/dist/{prices → oracles}/sources/PriceSource.d.ts +0 -0
- /package/dist/{prices → oracles}/sources/PriceSource.js +0 -0
- /package/dist/{prices → oracles}/sources/index.d.ts +0 -0
- /package/dist/{prices → oracles}/sources/index.js +0 -0
- /package/src/{prices → oracles}/constants.ts +0 -0
- /package/src/{prices → oracles}/sources/Backend.ts +0 -0
- /package/src/{prices → oracles}/sources/Icp.ts +0 -0
- /package/src/{prices → oracles}/sources/PriceSource.ts +0 -0
- /package/src/{prices → oracles}/sources/index.ts +0 -0
|
@@ -0,0 +1,192 @@
|
|
|
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 _ClassicCollector_instances, _ClassicCollector_prices, _ClassicCollector_poolAssetsConfig, _ClassicCollector_sourcesConfig, _ClassicCollector_priceSources, _ClassicCollector_minimalOracles, _ClassicCollector_getPricesByAssetList, _ClassicCollector_collectPrices, _ClassicCollector_collectPricesWithValidation, _ClassicCollector_filterPrices, _ClassicCollector_filterEmptyPrincipalsAndAssets, _ClassicCollector_convertToTwapAssets, _ClassicCollector_convertToSpotAssets, _ClassicCollector_getPricesWithMode;
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.ClassicCollector = void 0;
|
|
16
|
+
const core_1 = require("@ton/core");
|
|
17
|
+
const math_1 = require("../../api/math");
|
|
18
|
+
const utils_1 = require("../../utils/utils");
|
|
19
|
+
const ClassicPrices_1 = require("../prices/ClassicPrices");
|
|
20
|
+
const Types_1 = require("../Types");
|
|
21
|
+
const utils_2 = require("../utils");
|
|
22
|
+
const AbstractCollector_1 = require("./AbstractCollector");
|
|
23
|
+
class ClassicCollector extends AbstractCollector_1.AbstractCollector {
|
|
24
|
+
constructor(config) {
|
|
25
|
+
super();
|
|
26
|
+
_ClassicCollector_instances.add(this);
|
|
27
|
+
_ClassicCollector_prices.set(this, void 0);
|
|
28
|
+
_ClassicCollector_poolAssetsConfig.set(this, void 0);
|
|
29
|
+
_ClassicCollector_sourcesConfig.set(this, void 0);
|
|
30
|
+
_ClassicCollector_priceSources.set(this, void 0);
|
|
31
|
+
_ClassicCollector_minimalOracles.set(this, void 0);
|
|
32
|
+
__classPrivateFieldSet(this, _ClassicCollector_poolAssetsConfig, config.poolAssetsConfig, "f");
|
|
33
|
+
__classPrivateFieldSet(this, _ClassicCollector_sourcesConfig, config.sourcesConfig ?? Types_1.DefaultPriceSourcesConfig, "f");
|
|
34
|
+
__classPrivateFieldSet(this, _ClassicCollector_priceSources, (0, utils_2.generatePriceSources)(__classPrivateFieldGet(this, _ClassicCollector_sourcesConfig, "f"), config.evaaOracles), "f");
|
|
35
|
+
__classPrivateFieldSet(this, _ClassicCollector_minimalOracles, config.minimalOracles, "f");
|
|
36
|
+
if (config.additionalPriceSources) {
|
|
37
|
+
__classPrivateFieldGet(this, _ClassicCollector_priceSources, "f").push(...config.additionalPriceSources);
|
|
38
|
+
}
|
|
39
|
+
__classPrivateFieldSet(this, _ClassicCollector_prices, [], "f");
|
|
40
|
+
}
|
|
41
|
+
async getPricesForLiquidate(realPrincipals, fetchConfig) {
|
|
42
|
+
const assets = __classPrivateFieldGet(this, _ClassicCollector_instances, "m", _ClassicCollector_filterEmptyPrincipalsAndAssets).call(this, realPrincipals);
|
|
43
|
+
if (assets.includes(undefined)) {
|
|
44
|
+
throw new Error('User from another pool');
|
|
45
|
+
}
|
|
46
|
+
const validAssets = assets.map((x) => x);
|
|
47
|
+
const spotAssets = __classPrivateFieldGet(this, _ClassicCollector_instances, "m", _ClassicCollector_convertToSpotAssets).call(this, validAssets);
|
|
48
|
+
return await __classPrivateFieldGet(this, _ClassicCollector_instances, "m", _ClassicCollector_getPricesWithMode).call(this, spotAssets, ClassicPrices_1.ClassicPricesMode.SPOT, fetchConfig);
|
|
49
|
+
}
|
|
50
|
+
async getPricesForWithdraw(realPrincipals, withdrawAsset, collateralToDebt = false, fetchConfig) {
|
|
51
|
+
let assets = __classPrivateFieldGet(this, _ClassicCollector_instances, "m", _ClassicCollector_filterEmptyPrincipalsAndAssets).call(this, realPrincipals);
|
|
52
|
+
if ((0, math_1.checkNotInDebtAtAll)(realPrincipals) &&
|
|
53
|
+
(realPrincipals.get(withdrawAsset.assetId) ?? 0n) > 0n &&
|
|
54
|
+
!collateralToDebt) {
|
|
55
|
+
return ClassicPrices_1.ClassicPrices.createEmptyTwapPrices();
|
|
56
|
+
}
|
|
57
|
+
if (assets.includes(undefined)) {
|
|
58
|
+
throw new Error('User from another pool');
|
|
59
|
+
}
|
|
60
|
+
if (!assets.includes(withdrawAsset)) {
|
|
61
|
+
assets.push(withdrawAsset);
|
|
62
|
+
}
|
|
63
|
+
if (collateralToDebt && assets.length == 1) {
|
|
64
|
+
throw new Error('Cannot debt only one supplied asset');
|
|
65
|
+
}
|
|
66
|
+
return await this.getPrices(assets.map((x) => x), fetchConfig);
|
|
67
|
+
}
|
|
68
|
+
async getPricesForSupplyWithdraw(realPrincipals, supplyAsset, withdrawAsset, collateralToDebt, fetchConfig) {
|
|
69
|
+
let assets = __classPrivateFieldGet(this, _ClassicCollector_instances, "m", _ClassicCollector_filterEmptyPrincipalsAndAssets).call(this, realPrincipals);
|
|
70
|
+
if ((0, math_1.checkNotInDebtAtAll)(realPrincipals) &&
|
|
71
|
+
withdrawAsset &&
|
|
72
|
+
(realPrincipals.get(withdrawAsset.assetId) ?? 0n) > 0n &&
|
|
73
|
+
!collateralToDebt) {
|
|
74
|
+
return ClassicPrices_1.ClassicPrices.createEmptyTwapPrices();
|
|
75
|
+
}
|
|
76
|
+
if (assets.includes(undefined)) {
|
|
77
|
+
throw new Error('User from another pool');
|
|
78
|
+
}
|
|
79
|
+
if (withdrawAsset && !assets.includes(withdrawAsset)) {
|
|
80
|
+
assets.push(withdrawAsset);
|
|
81
|
+
}
|
|
82
|
+
if (collateralToDebt && assets.length == 1) {
|
|
83
|
+
throw new Error('Cannot debt only one supplied asset');
|
|
84
|
+
}
|
|
85
|
+
const validAssets = assets.map((x) => x);
|
|
86
|
+
const twapAssets = __classPrivateFieldGet(this, _ClassicCollector_instances, "m", _ClassicCollector_convertToTwapAssets).call(this, validAssets);
|
|
87
|
+
return await __classPrivateFieldGet(this, _ClassicCollector_instances, "m", _ClassicCollector_getPricesWithMode).call(this, twapAssets, ClassicPrices_1.ClassicPricesMode.TWAP, fetchConfig);
|
|
88
|
+
}
|
|
89
|
+
async getPrices(assets = __classPrivateFieldGet(this, _ClassicCollector_poolAssetsConfig, "f"), fetchConfig) {
|
|
90
|
+
if (assets.length == 0) {
|
|
91
|
+
return ClassicPrices_1.ClassicPrices.createEmptyPrices();
|
|
92
|
+
}
|
|
93
|
+
await __classPrivateFieldGet(this, _ClassicCollector_instances, "m", _ClassicCollector_collectPricesWithValidation).call(this, fetchConfig);
|
|
94
|
+
if (__classPrivateFieldGet(this, _ClassicCollector_prices, "f").length < __classPrivateFieldGet(this, _ClassicCollector_minimalOracles, "f")) {
|
|
95
|
+
throw new Error(`Error per updating prices, valid ${__classPrivateFieldGet(this, _ClassicCollector_prices, "f").length} of ${__classPrivateFieldGet(this, _ClassicCollector_minimalOracles, "f")}`);
|
|
96
|
+
}
|
|
97
|
+
const prices = __classPrivateFieldGet(this, _ClassicCollector_instances, "m", _ClassicCollector_getPricesByAssetList).call(this, assets);
|
|
98
|
+
return new ClassicPrices_1.ClassicPrices({
|
|
99
|
+
dict: prices.dict,
|
|
100
|
+
dataCell: prices.dataCell,
|
|
101
|
+
minPublishTime: undefined,
|
|
102
|
+
maxPublishTime: undefined,
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
exports.ClassicCollector = ClassicCollector;
|
|
107
|
+
_ClassicCollector_prices = new WeakMap(), _ClassicCollector_poolAssetsConfig = new WeakMap(), _ClassicCollector_sourcesConfig = new WeakMap(), _ClassicCollector_priceSources = new WeakMap(), _ClassicCollector_minimalOracles = new WeakMap(), _ClassicCollector_instances = new WeakSet(), _ClassicCollector_getPricesByAssetList = function _ClassicCollector_getPricesByAssetList(assets) {
|
|
108
|
+
let pricesFiltered = __classPrivateFieldGet(this, _ClassicCollector_prices, "f");
|
|
109
|
+
if (pricesFiltered.length < __classPrivateFieldGet(this, _ClassicCollector_minimalOracles, "f")) {
|
|
110
|
+
throw new Error('Not enough price data');
|
|
111
|
+
}
|
|
112
|
+
if (pricesFiltered.length > __classPrivateFieldGet(this, _ClassicCollector_minimalOracles, "f")) {
|
|
113
|
+
const sortedByTimestamp = pricesFiltered.slice().sort((a, b) => b.timestamp - a.timestamp);
|
|
114
|
+
const newerPrices = sortedByTimestamp.slice(0, __classPrivateFieldGet(this, _ClassicCollector_minimalOracles, "f"));
|
|
115
|
+
pricesFiltered = newerPrices.sort((a, b) => a.oracleId - b.oracleId);
|
|
116
|
+
}
|
|
117
|
+
const medianData = assets.map((asset) => ({
|
|
118
|
+
assetId: asset.assetId,
|
|
119
|
+
medianPrice: (0, utils_2.getMedianPrice)(pricesFiltered, asset.assetId),
|
|
120
|
+
}));
|
|
121
|
+
const nonEmptymedianData = medianData.filter((x) => x.medianPrice != null);
|
|
122
|
+
const packedMedianData = (0, utils_2.packAssetsData)(nonEmptymedianData);
|
|
123
|
+
const oraclesData = pricesFiltered.map((x) => ({
|
|
124
|
+
oracle: { id: x.oracleId, pubkey: x.pubkey },
|
|
125
|
+
data: { timestamp: x.timestamp, prices: x.dict },
|
|
126
|
+
signature: x.signature,
|
|
127
|
+
}));
|
|
128
|
+
const packedOracleData = (0, utils_2.packOraclesData)(oraclesData, nonEmptymedianData.map((x) => x.assetId));
|
|
129
|
+
const dict = core_1.Dictionary.empty();
|
|
130
|
+
for (const medianDataAsset of nonEmptymedianData) {
|
|
131
|
+
dict.set(medianDataAsset.assetId, medianDataAsset.medianPrice);
|
|
132
|
+
}
|
|
133
|
+
return {
|
|
134
|
+
dict: dict,
|
|
135
|
+
dataCell: (0, utils_2.packPrices)(packedMedianData, packedOracleData),
|
|
136
|
+
};
|
|
137
|
+
}, _ClassicCollector_collectPrices = async function _ClassicCollector_collectPrices(fetchConfig) {
|
|
138
|
+
for (const priceSource of __classPrivateFieldGet(this, _ClassicCollector_priceSources, "f")) {
|
|
139
|
+
try {
|
|
140
|
+
__classPrivateFieldSet(this, _ClassicCollector_prices, await (0, utils_1.proxyFetchRetries)((0, utils_2.collectAndFilterPrices)(priceSource, __classPrivateFieldGet(this, _ClassicCollector_minimalOracles, "f"), fetchConfig), fetchConfig), "f");
|
|
141
|
+
return true;
|
|
142
|
+
}
|
|
143
|
+
catch (error) {
|
|
144
|
+
// Try next source
|
|
145
|
+
continue;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
return false;
|
|
149
|
+
}, _ClassicCollector_collectPricesWithValidation = async function _ClassicCollector_collectPricesWithValidation(fetchConfig) {
|
|
150
|
+
if (!__classPrivateFieldGet(this, _ClassicCollector_prices, "f") || __classPrivateFieldGet(this, _ClassicCollector_instances, "m", _ClassicCollector_filterPrices).call(this) < __classPrivateFieldGet(this, _ClassicCollector_minimalOracles, "f")) {
|
|
151
|
+
const success = await __classPrivateFieldGet(this, _ClassicCollector_instances, "m", _ClassicCollector_collectPrices).call(this, fetchConfig);
|
|
152
|
+
if (!success || __classPrivateFieldGet(this, _ClassicCollector_prices, "f").length < __classPrivateFieldGet(this, _ClassicCollector_minimalOracles, "f")) {
|
|
153
|
+
throw new Error(`Failed to collect sufficient prices: ${__classPrivateFieldGet(this, _ClassicCollector_prices, "f")?.length || 0} of ${__classPrivateFieldGet(this, _ClassicCollector_minimalOracles, "f")}`);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}, _ClassicCollector_filterPrices = function _ClassicCollector_filterPrices() {
|
|
157
|
+
__classPrivateFieldSet(this, _ClassicCollector_prices, __classPrivateFieldGet(this, _ClassicCollector_prices, "f").filter((0, utils_2.verifyPricesTimestamp)()), "f");
|
|
158
|
+
return __classPrivateFieldGet(this, _ClassicCollector_prices, "f").length;
|
|
159
|
+
}, _ClassicCollector_filterEmptyPrincipalsAndAssets = function _ClassicCollector_filterEmptyPrincipalsAndAssets(principals) {
|
|
160
|
+
return principals
|
|
161
|
+
.keys()
|
|
162
|
+
.filter((x) => principals.get(x) != 0n)
|
|
163
|
+
.map((x) => __classPrivateFieldGet(this, _ClassicCollector_poolAssetsConfig, "f").find((asset) => asset.assetId == x));
|
|
164
|
+
}, _ClassicCollector_convertToTwapAssets = function _ClassicCollector_convertToTwapAssets(assets) {
|
|
165
|
+
return assets.map((asset) => ({
|
|
166
|
+
...asset,
|
|
167
|
+
assetId: asset.assetId - ClassicPrices_1.ClassicPricesOffset[ClassicPrices_1.ClassicPricesMode.TWAP],
|
|
168
|
+
}));
|
|
169
|
+
}, _ClassicCollector_convertToSpotAssets = function _ClassicCollector_convertToSpotAssets(assets) {
|
|
170
|
+
return assets.map((asset) => ({
|
|
171
|
+
...asset,
|
|
172
|
+
assetId: asset.assetId - ClassicPrices_1.ClassicPricesOffset[ClassicPrices_1.ClassicPricesMode.SPOT],
|
|
173
|
+
}));
|
|
174
|
+
}, _ClassicCollector_getPricesWithMode = async function _ClassicCollector_getPricesWithMode(assets, mode, fetchConfig) {
|
|
175
|
+
if (assets.length == 0) {
|
|
176
|
+
return ClassicPrices_1.ClassicPricesMode.TWAP
|
|
177
|
+
? ClassicPrices_1.ClassicPrices.createEmptyTwapPrices()
|
|
178
|
+
: ClassicPrices_1.ClassicPrices.createEmptySpotPrices();
|
|
179
|
+
}
|
|
180
|
+
await __classPrivateFieldGet(this, _ClassicCollector_instances, "m", _ClassicCollector_collectPricesWithValidation).call(this, fetchConfig);
|
|
181
|
+
if (__classPrivateFieldGet(this, _ClassicCollector_prices, "f").length < __classPrivateFieldGet(this, _ClassicCollector_minimalOracles, "f")) {
|
|
182
|
+
throw new Error(`Error per updating prices, valid ${__classPrivateFieldGet(this, _ClassicCollector_prices, "f").length} of ${__classPrivateFieldGet(this, _ClassicCollector_minimalOracles, "f")}`);
|
|
183
|
+
}
|
|
184
|
+
const prices = __classPrivateFieldGet(this, _ClassicCollector_instances, "m", _ClassicCollector_getPricesByAssetList).call(this, assets);
|
|
185
|
+
return new ClassicPrices_1.ClassicPrices({
|
|
186
|
+
mode,
|
|
187
|
+
dict: prices.dict,
|
|
188
|
+
dataCell: prices.dataCell,
|
|
189
|
+
minPublishTime: undefined,
|
|
190
|
+
maxPublishTime: undefined,
|
|
191
|
+
});
|
|
192
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { HexString } from '@pythnetwork/hermes-client';
|
|
2
|
+
import { Dictionary } from '@ton/core';
|
|
3
|
+
import { OracleConfig } from '../../api/parsers/PythOracleParser';
|
|
4
|
+
import { PoolAssetConfig } from '../../types/Master';
|
|
5
|
+
import { FetchConfig } from '../../utils/utils';
|
|
6
|
+
import { PythPrices } from '../prices/PythPrices';
|
|
7
|
+
import { PythPriceSourcesConfig } from '../Types';
|
|
8
|
+
import { AbstractCollector } from './AbstractCollector';
|
|
9
|
+
export type PythCollectorConfig = {
|
|
10
|
+
poolAssetsConfig: PoolAssetConfig[];
|
|
11
|
+
pythOracle: OracleConfig;
|
|
12
|
+
pythConfig: PythPriceSourcesConfig;
|
|
13
|
+
};
|
|
14
|
+
export declare class PythCollector extends AbstractCollector {
|
|
15
|
+
#private;
|
|
16
|
+
constructor(config: PythCollectorConfig);
|
|
17
|
+
getPricesForLiquidate(realPrincipals: Dictionary<bigint, bigint>, fetchConfig?: FetchConfig): Promise<PythPrices>;
|
|
18
|
+
getPricesForSupplyWithdraw(realPrincipals: Dictionary<bigint, bigint>, supplyAsset: PoolAssetConfig, withdrawAsset: PoolAssetConfig, collateralToDebt: boolean, fetchConfig?: FetchConfig): Promise<PythPrices>;
|
|
19
|
+
getPrices(assets?: PoolAssetConfig[], fetchConfig?: FetchConfig): Promise<PythPrices>;
|
|
20
|
+
getPricesForWithdraw(realPrincipals: Dictionary<bigint, bigint>, withdrawAsset: PoolAssetConfig, collateralToDebt?: boolean, fetchConfig?: FetchConfig): Promise<PythPrices>;
|
|
21
|
+
/**
|
|
22
|
+
* Creates a list of required feed IDs for the given assets
|
|
23
|
+
* @param assets - Array of pool asset configurations
|
|
24
|
+
* @returns Array of unique feed IDs required for the assets
|
|
25
|
+
*/
|
|
26
|
+
createRequiredFeedsList(assets: PoolAssetConfig[]): HexString[];
|
|
27
|
+
}
|
|
@@ -0,0 +1,252 @@
|
|
|
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 _PythCollector_instances, _PythCollector_parsedFeedsMap, _PythCollector_pythConfig, _PythCollector_poolAssetsConfig, _PythCollector_allowedRefTokens, _PythCollector_assetToFeeds, _PythCollector_getPythFeedsUpdates, _PythCollector_fetchPythUpdatesWithRetry, _PythCollector_filterEmptyPrincipalsAndAssets;
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.PythCollector = void 0;
|
|
16
|
+
const hermes_client_1 = require("@pythnetwork/hermes-client");
|
|
17
|
+
const core_1 = require("@ton/core");
|
|
18
|
+
const math_1 = require("../../api/math");
|
|
19
|
+
const prices_1 = require("../../api/prices");
|
|
20
|
+
const general_1 = require("../../constants/general");
|
|
21
|
+
const utils_1 = require("../../utils/utils");
|
|
22
|
+
const constants_1 = require("../constants");
|
|
23
|
+
const PythPrices_1 = require("../prices/PythPrices");
|
|
24
|
+
const AbstractCollector_1 = require("./AbstractCollector");
|
|
25
|
+
class PythCollector extends AbstractCollector_1.AbstractCollector {
|
|
26
|
+
constructor(config) {
|
|
27
|
+
super();
|
|
28
|
+
_PythCollector_instances.add(this);
|
|
29
|
+
_PythCollector_parsedFeedsMap.set(this, new Map());
|
|
30
|
+
_PythCollector_pythConfig.set(this, void 0);
|
|
31
|
+
_PythCollector_poolAssetsConfig.set(this, void 0);
|
|
32
|
+
_PythCollector_allowedRefTokens.set(this, void 0);
|
|
33
|
+
_PythCollector_assetToFeeds.set(this, new Map());
|
|
34
|
+
__classPrivateFieldSet(this, _PythCollector_pythConfig, config.pythConfig, "f");
|
|
35
|
+
__classPrivateFieldSet(this, _PythCollector_poolAssetsConfig, config.poolAssetsConfig, "f");
|
|
36
|
+
__classPrivateFieldSet(this, _PythCollector_allowedRefTokens, config.pythOracle.allowedRefTokens, "f");
|
|
37
|
+
for (const [feedId, feedMap] of config.pythOracle.feedsMap) {
|
|
38
|
+
__classPrivateFieldGet(this, _PythCollector_parsedFeedsMap, "f").set(feedId, feedMap);
|
|
39
|
+
}
|
|
40
|
+
for (const [feedId, connectedFeed] of __classPrivateFieldGet(this, _PythCollector_parsedFeedsMap, "f")) {
|
|
41
|
+
__classPrivateFieldGet(this, _PythCollector_assetToFeeds, "f").set(connectedFeed.assetId, [feedId, connectedFeed.feedId]);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
async getPricesForLiquidate(realPrincipals, fetchConfig) {
|
|
45
|
+
const assets = __classPrivateFieldGet(this, _PythCollector_instances, "m", _PythCollector_filterEmptyPrincipalsAndAssets).call(this, realPrincipals);
|
|
46
|
+
if (assets.includes(undefined)) {
|
|
47
|
+
throw new Error('User from another pool');
|
|
48
|
+
}
|
|
49
|
+
return await this.getPrices(assets.map((x) => x), fetchConfig);
|
|
50
|
+
}
|
|
51
|
+
async getPricesForSupplyWithdraw(realPrincipals, supplyAsset, withdrawAsset, collateralToDebt, fetchConfig) {
|
|
52
|
+
let assets = __classPrivateFieldGet(this, _PythCollector_instances, "m", _PythCollector_filterEmptyPrincipalsAndAssets).call(this, realPrincipals);
|
|
53
|
+
if (assets.includes(undefined)) {
|
|
54
|
+
throw new Error('User from another pool');
|
|
55
|
+
}
|
|
56
|
+
if (!assets.find((a) => a?.assetId === supplyAsset.assetId)) {
|
|
57
|
+
assets.push(supplyAsset);
|
|
58
|
+
}
|
|
59
|
+
if (!assets.find((a) => a?.assetId === withdrawAsset.assetId)) {
|
|
60
|
+
assets.push(withdrawAsset);
|
|
61
|
+
}
|
|
62
|
+
if (collateralToDebt && assets.length == 1) {
|
|
63
|
+
throw new Error('Cannot debt only one supplied asset');
|
|
64
|
+
}
|
|
65
|
+
return this.getPrices(assets.map((x) => x), fetchConfig);
|
|
66
|
+
}
|
|
67
|
+
async getPrices(assets = __classPrivateFieldGet(this, _PythCollector_poolAssetsConfig, "f"), fetchConfig) {
|
|
68
|
+
// Declare variables at the beginning
|
|
69
|
+
let minPublishTime;
|
|
70
|
+
let maxPublishTime;
|
|
71
|
+
if (assets.length === 0) {
|
|
72
|
+
return PythPrices_1.PythPrices.createEmptyPrices();
|
|
73
|
+
}
|
|
74
|
+
const requestedFeeds = new Set();
|
|
75
|
+
const requestedRefAssets = new Set();
|
|
76
|
+
for (const asset of assets) {
|
|
77
|
+
if (__classPrivateFieldGet(this, _PythCollector_allowedRefTokens, "f").has(asset.assetId)) {
|
|
78
|
+
const refTokenId = __classPrivateFieldGet(this, _PythCollector_allowedRefTokens, "f").get(asset.assetId);
|
|
79
|
+
if (__classPrivateFieldGet(this, _PythCollector_assetToFeeds, "f").has(refTokenId)) {
|
|
80
|
+
const [feedId, refFeedId] = __classPrivateFieldGet(this, _PythCollector_assetToFeeds, "f").get(refTokenId);
|
|
81
|
+
requestedFeeds.add(feedId);
|
|
82
|
+
if (refFeedId !== '0x0') {
|
|
83
|
+
requestedFeeds.add(refFeedId);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
requestedRefAssets.add(asset);
|
|
87
|
+
}
|
|
88
|
+
if (__classPrivateFieldGet(this, _PythCollector_assetToFeeds, "f").has(asset.assetId)) {
|
|
89
|
+
const [feedId, refFeedId] = __classPrivateFieldGet(this, _PythCollector_assetToFeeds, "f").get(asset.assetId);
|
|
90
|
+
requestedFeeds.add(feedId);
|
|
91
|
+
if (refFeedId !== '0x0') {
|
|
92
|
+
requestedFeeds.add(refFeedId);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
const targetFeeds = Array.from(requestedFeeds);
|
|
97
|
+
const refAssets = Array.from(requestedRefAssets);
|
|
98
|
+
const pythUpdates = await __classPrivateFieldGet(this, _PythCollector_instances, "m", _PythCollector_fetchPythUpdatesWithRetry).call(this, targetFeeds, fetchConfig);
|
|
99
|
+
// Calculate min and max publish times for validation
|
|
100
|
+
if (pythUpdates.parsed && pythUpdates.parsed.length > 0) {
|
|
101
|
+
let tmin = pythUpdates.parsed[0].price.publish_time;
|
|
102
|
+
let tmax = tmin;
|
|
103
|
+
for (let i = 1; i < pythUpdates.parsed.length; i++) {
|
|
104
|
+
const publishTime = pythUpdates.parsed[i].price.publish_time;
|
|
105
|
+
if (publishTime < tmin)
|
|
106
|
+
tmin = publishTime;
|
|
107
|
+
if (publishTime > tmax)
|
|
108
|
+
tmax = publishTime;
|
|
109
|
+
}
|
|
110
|
+
if (tmax - tmin > constants_1.TTL_ORACLE_DATA_SEC) {
|
|
111
|
+
throw new Error(`Price feeds don't fit in a single 3-minute window. Time span: ${tmax - tmin} seconds (max allowed: ${constants_1.TTL_ORACLE_DATA_SEC})`);
|
|
112
|
+
}
|
|
113
|
+
minPublishTime = tmin;
|
|
114
|
+
maxPublishTime = tmin + constants_1.TTL_ORACLE_DATA_SEC;
|
|
115
|
+
}
|
|
116
|
+
const pricesDict = core_1.Dictionary.empty();
|
|
117
|
+
const pythPriceUpdates = pythUpdates.parsed;
|
|
118
|
+
if (pythPriceUpdates) {
|
|
119
|
+
// Only set prices for requested assets, not all possible mapped assets
|
|
120
|
+
const requestedAssetIds = new Set(assets.map((a) => a.assetId));
|
|
121
|
+
for (const u of pythPriceUpdates) {
|
|
122
|
+
const feedId = `0x${u.id}`;
|
|
123
|
+
const price = (BigInt(u.price.price) * BigInt(10 ** 9)) / BigInt(10 ** (u.price.expo * -1));
|
|
124
|
+
// Find the feed mapping for this feedId and always set the price for the mapped asset
|
|
125
|
+
const feedMapItem = __classPrivateFieldGet(this, _PythCollector_parsedFeedsMap, "f").get(feedId);
|
|
126
|
+
if (feedMapItem) {
|
|
127
|
+
pricesDict.set(feedMapItem.assetId, price);
|
|
128
|
+
}
|
|
129
|
+
// Handle reference tokens - check if any asset uses this feed as a reference
|
|
130
|
+
for (const asset of assets) {
|
|
131
|
+
if (__classPrivateFieldGet(this, _PythCollector_allowedRefTokens, "f").has(asset.assetId)) {
|
|
132
|
+
const refTokenId = __classPrivateFieldGet(this, _PythCollector_allowedRefTokens, "f").get(asset.assetId);
|
|
133
|
+
const refFeeds = __classPrivateFieldGet(this, _PythCollector_assetToFeeds, "f").get(refTokenId);
|
|
134
|
+
if (refFeeds && (refFeeds[0] === feedId || refFeeds[1] === feedId)) {
|
|
135
|
+
if (requestedAssetIds.has(asset.assetId)) {
|
|
136
|
+
// For allowedRefTokens, both asset and reference token should have the same price
|
|
137
|
+
// Set the same price for both the asset and its reference token
|
|
138
|
+
pricesDict.set(asset.assetId, price);
|
|
139
|
+
pricesDict.set(refTokenId, price);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
// Apply dynamic scaling for liquid staking tokens based on feedsMap configuration
|
|
146
|
+
// For each asset that has a feedId (not '0x0'), apply scaling: asset_price = asset_rate * base_price / ASSET_PRICE_SCALE
|
|
147
|
+
for (const [feedId, feedMapItem] of __classPrivateFieldGet(this, _PythCollector_parsedFeedsMap, "f")) {
|
|
148
|
+
if (feedMapItem.feedId !== '0x0') {
|
|
149
|
+
const assetPrice = pricesDict.get(feedMapItem.assetId);
|
|
150
|
+
const baseFeedMapItem = __classPrivateFieldGet(this, _PythCollector_parsedFeedsMap, "f").get(feedMapItem.feedId);
|
|
151
|
+
const basePrice = baseFeedMapItem ? pricesDict.get(baseFeedMapItem.assetId) : undefined;
|
|
152
|
+
if (assetPrice && basePrice) {
|
|
153
|
+
const scaledPrice = (assetPrice * basePrice) / general_1.ASSET_PRICE_SCALE;
|
|
154
|
+
pricesDict.set(feedMapItem.assetId, scaledPrice);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
// Apply scaling for reference tokens (allowedRefTokens)
|
|
159
|
+
// For assets that reference other tokens, they should have the SAME price as their reference token
|
|
160
|
+
// This is different from feedsMap scaling where we multiply rates
|
|
161
|
+
for (const asset of assets) {
|
|
162
|
+
if (__classPrivateFieldGet(this, _PythCollector_allowedRefTokens, "f").has(asset.assetId)) {
|
|
163
|
+
const refTokenId = __classPrivateFieldGet(this, _PythCollector_allowedRefTokens, "f").get(asset.assetId);
|
|
164
|
+
const refTokenPrice = pricesDict.get(refTokenId);
|
|
165
|
+
if (refTokenPrice && requestedAssetIds.has(asset.assetId)) {
|
|
166
|
+
// For allowedRefTokens, the asset price should equal the reference token price
|
|
167
|
+
pricesDict.set(asset.assetId, refTokenPrice);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
// Check that all requested assets have prices
|
|
172
|
+
const missing = assets.map((a) => a.assetId).filter((id) => pricesDict.get(id) === undefined);
|
|
173
|
+
if (missing.length) {
|
|
174
|
+
throw new Error(`Missing prices for ${missing.length} asset(s): ${missing.map((x) => x.toString()).join(', ')}`);
|
|
175
|
+
}
|
|
176
|
+
const dataCell = (0, prices_1.packPythUpdatesData)(pythUpdates.binary);
|
|
177
|
+
return new PythPrices_1.PythPrices({
|
|
178
|
+
dict: pricesDict,
|
|
179
|
+
dataCell,
|
|
180
|
+
minPublishTime,
|
|
181
|
+
maxPublishTime,
|
|
182
|
+
refAssets,
|
|
183
|
+
targetFeeds,
|
|
184
|
+
binaryUpdate: pythUpdates.binary,
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
return PythPrices_1.PythPrices.createEmptyPrices();
|
|
188
|
+
}
|
|
189
|
+
async getPricesForWithdraw(realPrincipals, withdrawAsset, collateralToDebt = false, fetchConfig) {
|
|
190
|
+
let assets = __classPrivateFieldGet(this, _PythCollector_instances, "m", _PythCollector_filterEmptyPrincipalsAndAssets).call(this, realPrincipals);
|
|
191
|
+
if ((0, math_1.checkNotInDebtAtAll)(realPrincipals) &&
|
|
192
|
+
(realPrincipals.get(withdrawAsset.assetId) ?? 0n) > 0n &&
|
|
193
|
+
!collateralToDebt) {
|
|
194
|
+
return PythPrices_1.PythPrices.createEmptyPrices();
|
|
195
|
+
}
|
|
196
|
+
if (assets.includes(undefined)) {
|
|
197
|
+
throw new Error('User from another pool');
|
|
198
|
+
}
|
|
199
|
+
if (!assets.includes(withdrawAsset)) {
|
|
200
|
+
assets.push(withdrawAsset);
|
|
201
|
+
}
|
|
202
|
+
if (collateralToDebt && assets.length == 1) {
|
|
203
|
+
throw new Error('Cannot debt only one supplied asset');
|
|
204
|
+
}
|
|
205
|
+
return await this.getPrices(assets.map((x) => x), fetchConfig);
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* Creates a list of required feed IDs for the given assets
|
|
209
|
+
* @param assets - Array of pool asset configurations
|
|
210
|
+
* @returns Array of unique feed IDs required for the assets
|
|
211
|
+
*/
|
|
212
|
+
createRequiredFeedsList(assets) {
|
|
213
|
+
const requestedFeeds = new Set();
|
|
214
|
+
for (const asset of assets) {
|
|
215
|
+
const addFeedsForAsset = (assetId) => {
|
|
216
|
+
if (__classPrivateFieldGet(this, _PythCollector_assetToFeeds, "f").has(assetId)) {
|
|
217
|
+
const [feedId, refFeedId] = __classPrivateFieldGet(this, _PythCollector_assetToFeeds, "f").get(assetId);
|
|
218
|
+
requestedFeeds.add(feedId);
|
|
219
|
+
if (refFeedId !== '0x0') {
|
|
220
|
+
requestedFeeds.add(refFeedId);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
};
|
|
224
|
+
if (__classPrivateFieldGet(this, _PythCollector_allowedRefTokens, "f").has(asset.assetId)) {
|
|
225
|
+
const refTokenId = __classPrivateFieldGet(this, _PythCollector_allowedRefTokens, "f").get(asset.assetId);
|
|
226
|
+
addFeedsForAsset(refTokenId);
|
|
227
|
+
}
|
|
228
|
+
addFeedsForAsset(asset.assetId);
|
|
229
|
+
}
|
|
230
|
+
return Array.from(requestedFeeds);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
exports.PythCollector = PythCollector;
|
|
234
|
+
_PythCollector_parsedFeedsMap = new WeakMap(), _PythCollector_pythConfig = new WeakMap(), _PythCollector_poolAssetsConfig = new WeakMap(), _PythCollector_allowedRefTokens = new WeakMap(), _PythCollector_assetToFeeds = new WeakMap(), _PythCollector_instances = new WeakSet(), _PythCollector_getPythFeedsUpdates =
|
|
235
|
+
/**
|
|
236
|
+
* Updates feeds data from specified endpoint
|
|
237
|
+
* @param feedIds list of pyth feed ids to fetch
|
|
238
|
+
* @returns binary - buffer of feeds update, parsed - json feeds data
|
|
239
|
+
*/
|
|
240
|
+
async function _PythCollector_getPythFeedsUpdates(feedIds) {
|
|
241
|
+
const latestPriceUpdates = await Promise.any(__classPrivateFieldGet(this, _PythCollector_pythConfig, "f").pythEndpoints.map((x) => new hermes_client_1.HermesClient(x).getLatestPriceUpdates(feedIds, { encoding: 'hex' })));
|
|
242
|
+
const parsed = latestPriceUpdates['parsed'];
|
|
243
|
+
const binary = Buffer.from(latestPriceUpdates.binary.data[0], 'hex');
|
|
244
|
+
return { binary, parsed };
|
|
245
|
+
}, _PythCollector_fetchPythUpdatesWithRetry = async function _PythCollector_fetchPythUpdatesWithRetry(requiredFeeds, fetchConfig) {
|
|
246
|
+
return (0, utils_1.proxyFetchRetries)(__classPrivateFieldGet(this, _PythCollector_instances, "m", _PythCollector_getPythFeedsUpdates).call(this, requiredFeeds), fetchConfig);
|
|
247
|
+
}, _PythCollector_filterEmptyPrincipalsAndAssets = function _PythCollector_filterEmptyPrincipalsAndAssets(principals) {
|
|
248
|
+
return principals
|
|
249
|
+
.keys()
|
|
250
|
+
.filter((x) => principals.get(x) != 0n)
|
|
251
|
+
.map((x) => __classPrivateFieldGet(this, _PythCollector_poolAssetsConfig, "f").find((asset) => asset.assetId == x));
|
|
252
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
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("./AbstractCollector"), exports);
|
|
18
|
+
__exportStar(require("./ClassicCollector"), exports);
|
|
19
|
+
__exportStar(require("./PythCollector"), exports);
|
|
@@ -1,7 +1,6 @@
|
|
|
1
|
+
export * from './collectors';
|
|
1
2
|
export * from './constants';
|
|
2
|
-
export * from './
|
|
3
|
-
export * from './PricesCollector';
|
|
4
|
-
export * from './PythCollector';
|
|
3
|
+
export * from './prices';
|
|
5
4
|
export * from './sources';
|
|
6
5
|
export * from './Types';
|
|
7
6
|
export * from './utils';
|
|
@@ -14,10 +14,9 @@ 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
|
+
__exportStar(require("./collectors"), exports);
|
|
17
18
|
__exportStar(require("./constants"), exports);
|
|
18
|
-
__exportStar(require("./
|
|
19
|
-
__exportStar(require("./PricesCollector"), exports);
|
|
20
|
-
__exportStar(require("./PythCollector"), exports);
|
|
19
|
+
__exportStar(require("./prices"), exports);
|
|
21
20
|
__exportStar(require("./sources"), exports);
|
|
22
21
|
__exportStar(require("./Types"), exports);
|
|
23
22
|
__exportStar(require("./utils"), exports);
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { Cell, Dictionary } from '@ton/core';
|
|
2
|
+
import { PoolAssetConfig } from '../../types/Master';
|
|
3
|
+
/**
|
|
4
|
+
* Basic price data structure
|
|
5
|
+
* Simplified version of RawPriceData for internal processing
|
|
6
|
+
*/
|
|
7
|
+
export interface PriceData {
|
|
8
|
+
/** Dictionary mapping asset IDs to their prices */
|
|
9
|
+
readonly dict: Dictionary<bigint, bigint>;
|
|
10
|
+
/** Serialized data cell containing price information uses for smartcontract */
|
|
11
|
+
readonly dataCell: Cell;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Configuration interface for timestamp boundaries
|
|
15
|
+
* Used to filter price data based on publish time
|
|
16
|
+
*/
|
|
17
|
+
export interface TimestampBoundaries {
|
|
18
|
+
/** Maximum valid time for price data in seconds */
|
|
19
|
+
readonly minPublishTime?: number;
|
|
20
|
+
/** Minimum valid time for price data in seconds */
|
|
21
|
+
readonly maxPublishTime?: number;
|
|
22
|
+
}
|
|
23
|
+
export type PriceParameters = PriceData & TimestampBoundaries;
|
|
24
|
+
export declare abstract class AbstractPrices {
|
|
25
|
+
#private;
|
|
26
|
+
protected readonly parameters: PriceParameters;
|
|
27
|
+
constructor(parameters: PriceParameters);
|
|
28
|
+
get dict(): Dictionary<bigint, bigint>;
|
|
29
|
+
get dataCell(): Cell;
|
|
30
|
+
getAssetPrice<T extends bigint | PoolAssetConfig>(asset: T): bigint | undefined;
|
|
31
|
+
get minPublishTime(): number | undefined;
|
|
32
|
+
get maxPublishTime(): number | undefined;
|
|
33
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
3
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
4
|
+
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");
|
|
5
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
6
|
+
};
|
|
7
|
+
var _AbstractPrices_instances, _AbstractPrices_extractAssetId;
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.AbstractPrices = void 0;
|
|
10
|
+
const core_1 = require("@ton/core");
|
|
11
|
+
class AbstractPrices {
|
|
12
|
+
constructor(parameters) {
|
|
13
|
+
_AbstractPrices_instances.add(this);
|
|
14
|
+
this.parameters = parameters;
|
|
15
|
+
}
|
|
16
|
+
get dict() {
|
|
17
|
+
const dict = core_1.Dictionary.empty();
|
|
18
|
+
for (const [key, value] of this.parameters.dict) {
|
|
19
|
+
dict.set(key, value);
|
|
20
|
+
}
|
|
21
|
+
return dict;
|
|
22
|
+
}
|
|
23
|
+
get dataCell() {
|
|
24
|
+
return new core_1.Cell(this.parameters.dataCell);
|
|
25
|
+
}
|
|
26
|
+
getAssetPrice(asset) {
|
|
27
|
+
const assetId = __classPrivateFieldGet(this, _AbstractPrices_instances, "m", _AbstractPrices_extractAssetId).call(this, asset);
|
|
28
|
+
return this.parameters.dict.get(assetId);
|
|
29
|
+
}
|
|
30
|
+
get minPublishTime() {
|
|
31
|
+
return this.parameters.minPublishTime;
|
|
32
|
+
}
|
|
33
|
+
get maxPublishTime() {
|
|
34
|
+
return this.parameters.maxPublishTime;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
exports.AbstractPrices = AbstractPrices;
|
|
38
|
+
_AbstractPrices_instances = new WeakSet(), _AbstractPrices_extractAssetId = function _AbstractPrices_extractAssetId(asset) {
|
|
39
|
+
return typeof asset === 'bigint' ? asset : asset.assetId;
|
|
40
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { AbstractPrices, PriceParameters } from './AbstractPrices';
|
|
2
|
+
export declare enum ClassicPricesMode {
|
|
3
|
+
SPOT = "spot",
|
|
4
|
+
TWAP = "twap"
|
|
5
|
+
}
|
|
6
|
+
export declare const ClassicPricesOffset: {
|
|
7
|
+
spot: bigint;
|
|
8
|
+
twap: bigint;
|
|
9
|
+
};
|
|
10
|
+
export interface ClassicPricesParams extends PriceParameters {
|
|
11
|
+
readonly mode?: ClassicPricesMode;
|
|
12
|
+
}
|
|
13
|
+
export declare class ClassicPrices extends AbstractPrices {
|
|
14
|
+
private readonly params;
|
|
15
|
+
constructor(params: ClassicPricesParams);
|
|
16
|
+
static createEmptyTwapPrices(): ClassicPrices;
|
|
17
|
+
static createEmptySpotPrices(): ClassicPrices;
|
|
18
|
+
static createEmptyPrices(): ClassicPrices;
|
|
19
|
+
}
|