@suilend/sdk 1.1.60 → 1.1.62

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/package.json CHANGED
@@ -1 +1 @@
1
- {"name":"@suilend/sdk","version":"1.1.60","private":false,"description":"A TypeScript SDK for interacting with the Suilend program","author":"Suilend","license":"MIT","main":"./index.js","exports":{".":"./index.js","./client":"./client.js","./api/events":"./api/events.js","./api":"./api/index.js","./lib/constants":"./lib/constants.js","./lib":"./lib/index.js","./lib/initialize":"./lib/initialize.js","./lib/liquidityMining":"./lib/liquidityMining.js","./lib/transactions":"./lib/transactions.js","./lib/types":"./lib/types.js","./parsers/apiReserveAssetDataEvent":"./parsers/apiReserveAssetDataEvent.js","./parsers":"./parsers/index.js","./parsers/lendingMarket":"./parsers/lendingMarket.js","./parsers/obligation":"./parsers/obligation.js","./parsers/rateLimiter":"./parsers/rateLimiter.js","./parsers/reserve":"./parsers/reserve.js","./swap":"./swap/index.js","./swap/okxDex":"./swap/okxDex.js","./swap/quote":"./swap/quote.js","./swap/transaction":"./swap/transaction.js","./utils/events":"./utils/events.js","./utils":"./utils/index.js","./utils/obligation":"./utils/obligation.js","./utils/simulate":"./utils/simulate.js","./_generated/_framework/reified":"./_generated/_framework/reified.js","./_generated/_framework/util":"./_generated/_framework/util.js","./_generated/_framework/vector":"./_generated/_framework/vector.js","./_generated/suilend":"./_generated/suilend/index.js","./_generated/suilend/cell/structs":"./_generated/suilend/cell/structs.js","./_generated/suilend/decimal/structs":"./_generated/suilend/decimal/structs.js","./_generated/suilend/lending-market/functions":"./_generated/suilend/lending-market/functions.js","./_generated/suilend/lending-market/structs":"./_generated/suilend/lending-market/structs.js","./_generated/suilend/lending-market-registry/functions":"./_generated/suilend/lending-market-registry/functions.js","./_generated/suilend/liquidity-mining/structs":"./_generated/suilend/liquidity-mining/structs.js","./_generated/suilend/obligation/structs":"./_generated/suilend/obligation/structs.js","./_generated/suilend/rate-limiter/functions":"./_generated/suilend/rate-limiter/functions.js","./_generated/suilend/rate-limiter/structs":"./_generated/suilend/rate-limiter/structs.js","./_generated/suilend/reserve/structs":"./_generated/suilend/reserve/structs.js","./_generated/suilend/reserve-config/functions":"./_generated/suilend/reserve-config/functions.js","./_generated/suilend/reserve-config/structs":"./_generated/suilend/reserve-config/structs.js","./_generated/_dependencies/source/0x1":"./_generated/_dependencies/source/0x1/index.js","./_generated/_dependencies/source/0x2":"./_generated/_dependencies/source/0x2/index.js","./_generated/_dependencies/source/0x8d97f1cd6ac663735be08d1d2b6d02a159e711586461306ce60a2b7a6a565a9e":"./_generated/_dependencies/source/0x8d97f1cd6ac663735be08d1d2b6d02a159e711586461306ce60a2b7a6a565a9e/index.js","./_generated/_dependencies/source/0x1/ascii/structs":"./_generated/_dependencies/source/0x1/ascii/structs.js","./_generated/_dependencies/source/0x1/option/structs":"./_generated/_dependencies/source/0x1/option/structs.js","./_generated/_dependencies/source/0x1/type-name/structs":"./_generated/_dependencies/source/0x1/type-name/structs.js","./_generated/_dependencies/source/0x2/bag/structs":"./_generated/_dependencies/source/0x2/bag/structs.js","./_generated/_dependencies/source/0x2/balance/structs":"./_generated/_dependencies/source/0x2/balance/structs.js","./_generated/_dependencies/source/0x2/object/structs":"./_generated/_dependencies/source/0x2/object/structs.js","./_generated/_dependencies/source/0x2/object-table/structs":"./_generated/_dependencies/source/0x2/object-table/structs.js","./_generated/_dependencies/source/0x8d97f1cd6ac663735be08d1d2b6d02a159e711586461306ce60a2b7a6a565a9e/i64/structs":"./_generated/_dependencies/source/0x8d97f1cd6ac663735be08d1d2b6d02a159e711586461306ce60a2b7a6a565a9e/i64/structs.js","./_generated/_dependencies/source/0x8d97f1cd6ac663735be08d1d2b6d02a159e711586461306ce60a2b7a6a565a9e/price/structs":"./_generated/_dependencies/source/0x8d97f1cd6ac663735be08d1d2b6d02a159e711586461306ce60a2b7a6a565a9e/price/structs.js","./_generated/_dependencies/source/0x8d97f1cd6ac663735be08d1d2b6d02a159e711586461306ce60a2b7a6a565a9e/price-feed/structs":"./_generated/_dependencies/source/0x8d97f1cd6ac663735be08d1d2b6d02a159e711586461306ce60a2b7a6a565a9e/price-feed/structs.js","./_generated/_dependencies/source/0x8d97f1cd6ac663735be08d1d2b6d02a159e711586461306ce60a2b7a6a565a9e/price-identifier/structs":"./_generated/_dependencies/source/0x8d97f1cd6ac663735be08d1d2b6d02a159e711586461306ce60a2b7a6a565a9e/price-identifier/structs.js","./_generated/_dependencies/source/0x8d97f1cd6ac663735be08d1d2b6d02a159e711586461306ce60a2b7a6a565a9e/price-info/structs":"./_generated/_dependencies/source/0x8d97f1cd6ac663735be08d1d2b6d02a159e711586461306ce60a2b7a6a565a9e/price-info/structs.js"},"types":"./index.js","scripts":{"build":"rm -rf ./dist && bun tsc","eslint":"eslint --fix \"./src/**/*.ts\"","prettier":"prettier --write \"./src/**/*\"","lint":"bun eslint && bun prettier && bun tsc --noEmit","release":"bun run build && bun ts-node ./release.ts && cd ./dist && npm publish --access public"},"repository":{"type":"git","url":"git+https://github.com/suilend/suilend-fe-public.git"},"bugs":{"url":"https://github.com/suilend/suilend-fe-public/issues"},"dependencies":{"@7kprotocol/sdk-ts":"^3.0.2","@cetusprotocol/aggregator-sdk":"^0.12.0","@flowx-finance/sdk":"^1.11.0","@pythnetwork/pyth-sui-js":"^2.1.0","aftermath-ts-sdk":"^1.3.11","bignumber.js":"^9.1.2","bn.js":"^5.2.2","crypto-js":"^4.2.0","p-limit":"3.1.0","uuid":"^11.0.3"},"devDependencies":{"ts-node":"^10.9.2"},"peerDependencies":{"@mysten/bcs":"1.6.0","@mysten/sui":"1.28.2","@suilend/sui-fe":"^0.2.80"}}
1
+ {"name":"@suilend/sdk","version":"1.1.62","private":false,"description":"A TypeScript SDK for interacting with the Suilend program","author":"Suilend","license":"MIT","main":"./index.js","exports":{".":"./index.js","./client":"./client.js","./api/events":"./api/events.js","./api":"./api/index.js","./lib/constants":"./lib/constants.js","./lib":"./lib/index.js","./lib/initialize":"./lib/initialize.js","./lib/liquidityMining":"./lib/liquidityMining.js","./lib/transactions":"./lib/transactions.js","./lib/types":"./lib/types.js","./parsers/apiReserveAssetDataEvent":"./parsers/apiReserveAssetDataEvent.js","./parsers":"./parsers/index.js","./parsers/lendingMarket":"./parsers/lendingMarket.js","./parsers/obligation":"./parsers/obligation.js","./parsers/rateLimiter":"./parsers/rateLimiter.js","./parsers/reserve":"./parsers/reserve.js","./swap":"./swap/index.js","./swap/okxDex":"./swap/okxDex.js","./swap/quote":"./swap/quote.js","./swap/transaction":"./swap/transaction.js","./utils/events":"./utils/events.js","./utils":"./utils/index.js","./utils/obligation":"./utils/obligation.js","./utils/simulate":"./utils/simulate.js","./_generated/_framework/reified":"./_generated/_framework/reified.js","./_generated/_framework/util":"./_generated/_framework/util.js","./_generated/_framework/vector":"./_generated/_framework/vector.js","./_generated/suilend":"./_generated/suilend/index.js","./_generated/suilend/cell/structs":"./_generated/suilend/cell/structs.js","./_generated/suilend/decimal/structs":"./_generated/suilend/decimal/structs.js","./_generated/suilend/lending-market/functions":"./_generated/suilend/lending-market/functions.js","./_generated/suilend/lending-market/structs":"./_generated/suilend/lending-market/structs.js","./_generated/suilend/lending-market-registry/functions":"./_generated/suilend/lending-market-registry/functions.js","./_generated/suilend/liquidity-mining/structs":"./_generated/suilend/liquidity-mining/structs.js","./_generated/suilend/obligation/structs":"./_generated/suilend/obligation/structs.js","./_generated/suilend/rate-limiter/functions":"./_generated/suilend/rate-limiter/functions.js","./_generated/suilend/rate-limiter/structs":"./_generated/suilend/rate-limiter/structs.js","./_generated/suilend/reserve/structs":"./_generated/suilend/reserve/structs.js","./_generated/suilend/reserve-config/functions":"./_generated/suilend/reserve-config/functions.js","./_generated/suilend/reserve-config/structs":"./_generated/suilend/reserve-config/structs.js","./_generated/_dependencies/source/0x1":"./_generated/_dependencies/source/0x1/index.js","./_generated/_dependencies/source/0x2":"./_generated/_dependencies/source/0x2/index.js","./_generated/_dependencies/source/0x8d97f1cd6ac663735be08d1d2b6d02a159e711586461306ce60a2b7a6a565a9e":"./_generated/_dependencies/source/0x8d97f1cd6ac663735be08d1d2b6d02a159e711586461306ce60a2b7a6a565a9e/index.js","./_generated/_dependencies/source/0x1/ascii/structs":"./_generated/_dependencies/source/0x1/ascii/structs.js","./_generated/_dependencies/source/0x1/option/structs":"./_generated/_dependencies/source/0x1/option/structs.js","./_generated/_dependencies/source/0x1/type-name/structs":"./_generated/_dependencies/source/0x1/type-name/structs.js","./_generated/_dependencies/source/0x2/bag/structs":"./_generated/_dependencies/source/0x2/bag/structs.js","./_generated/_dependencies/source/0x2/balance/structs":"./_generated/_dependencies/source/0x2/balance/structs.js","./_generated/_dependencies/source/0x2/object/structs":"./_generated/_dependencies/source/0x2/object/structs.js","./_generated/_dependencies/source/0x2/object-table/structs":"./_generated/_dependencies/source/0x2/object-table/structs.js","./_generated/_dependencies/source/0x8d97f1cd6ac663735be08d1d2b6d02a159e711586461306ce60a2b7a6a565a9e/i64/structs":"./_generated/_dependencies/source/0x8d97f1cd6ac663735be08d1d2b6d02a159e711586461306ce60a2b7a6a565a9e/i64/structs.js","./_generated/_dependencies/source/0x8d97f1cd6ac663735be08d1d2b6d02a159e711586461306ce60a2b7a6a565a9e/price/structs":"./_generated/_dependencies/source/0x8d97f1cd6ac663735be08d1d2b6d02a159e711586461306ce60a2b7a6a565a9e/price/structs.js","./_generated/_dependencies/source/0x8d97f1cd6ac663735be08d1d2b6d02a159e711586461306ce60a2b7a6a565a9e/price-feed/structs":"./_generated/_dependencies/source/0x8d97f1cd6ac663735be08d1d2b6d02a159e711586461306ce60a2b7a6a565a9e/price-feed/structs.js","./_generated/_dependencies/source/0x8d97f1cd6ac663735be08d1d2b6d02a159e711586461306ce60a2b7a6a565a9e/price-identifier/structs":"./_generated/_dependencies/source/0x8d97f1cd6ac663735be08d1d2b6d02a159e711586461306ce60a2b7a6a565a9e/price-identifier/structs.js","./_generated/_dependencies/source/0x8d97f1cd6ac663735be08d1d2b6d02a159e711586461306ce60a2b7a6a565a9e/price-info/structs":"./_generated/_dependencies/source/0x8d97f1cd6ac663735be08d1d2b6d02a159e711586461306ce60a2b7a6a565a9e/price-info/structs.js"},"types":"./index.js","scripts":{"build":"rm -rf ./dist && bun tsc","eslint":"eslint --fix \"./src/**/*.ts\"","prettier":"prettier --write \"./src/**/*\"","lint":"bun eslint && bun prettier && bun tsc --noEmit","release":"bun run build && bun ts-node ./release.ts && cd ./dist && npm publish --access public"},"repository":{"type":"git","url":"git+https://github.com/suilend/suilend-fe-public.git"},"bugs":{"url":"https://github.com/suilend/suilend-fe-public/issues"},"dependencies":{"@7kprotocol/sdk-ts":"^3.0.2","@cetusprotocol/aggregator-sdk":"^0.12.0","@flowx-finance/sdk":"^1.11.0","@pythnetwork/pyth-sui-js":"^2.1.0","aftermath-ts-sdk":"^1.3.11","bignumber.js":"^9.1.2","bn.js":"^5.2.2","crypto-js":"^4.2.0","p-limit":"3.1.0","uuid":"^11.0.3"},"devDependencies":{"ts-node":"^10.9.2"},"peerDependencies":{"@mysten/bcs":"1.6.0","@mysten/sui":"1.28.2","@suilend/sui-fe":"^0.2.80"}}
package/swap/quote.d.ts CHANGED
@@ -76,4 +76,9 @@ export declare const fetchAggQuotes: (sdkMap: {
76
76
  [QuoteProvider.AFTERMATH]: AftermathSdk;
77
77
  [QuoteProvider.CETUS]: CetusSdk;
78
78
  [QuoteProvider.FLOWX]: FlowXAggregatorQuoter;
79
- }, activeProviders: QuoteProvider[], setQuotesForTimestamp: (timestamp: number, quotes: StandardizedQuote[]) => void, _tokenIn: Token, _tokenOut: Token, _value: string, _timestamp?: number) => Promise<void>;
79
+ }, activeProviders: QuoteProvider[], onFetchAggQuote: (quote: StandardizedQuote | null) => void, tokenIn: Token, tokenOut: Token, amountIn: string) => Promise<void>;
80
+ export declare const fetchAggQuotesAll: (sdkMap: {
81
+ [QuoteProvider.AFTERMATH]: AftermathSdk;
82
+ [QuoteProvider.CETUS]: CetusSdk;
83
+ [QuoteProvider.FLOWX]: FlowXAggregatorQuoter;
84
+ }, activeProviders: QuoteProvider[], tokenIn: Token, tokenOut: Token, amountIn: string) => Promise<(StandardizedQuote | null)[]>;
package/swap/quote.js CHANGED
@@ -12,7 +12,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
12
12
  return (mod && mod.__esModule) ? mod : { "default": mod };
13
13
  };
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.fetchAggQuotes = exports.QUOTE_PROVIDER_NAME_MAP = exports.QuoteProvider = exports.getPoolProviders = void 0;
15
+ exports.fetchAggQuotesAll = exports.fetchAggQuotes = exports.QUOTE_PROVIDER_NAME_MAP = exports.QuoteProvider = exports.getPoolProviders = void 0;
16
16
  const cjs_1 = require("@7kprotocol/sdk-ts/cjs");
17
17
  const utils_1 = require("@mysten/sui/utils");
18
18
  const bignumber_js_1 = __importDefault(require("bignumber.js"));
@@ -42,289 +42,294 @@ exports.QUOTE_PROVIDER_NAME_MAP = {
42
42
  [QuoteProvider.FLOWX]: "FlowX",
43
43
  [QuoteProvider.OKX_DEX]: "OKX DEX",
44
44
  };
45
- const fetchAggQuotes = (sdkMap_1, activeProviders_1, setQuotesForTimestamp_1, _tokenIn_1, _tokenOut_1, _value_1, ...args_1) => __awaiter(void 0, [sdkMap_1, activeProviders_1, setQuotesForTimestamp_1, _tokenIn_1, _tokenOut_1, _value_1, ...args_1], void 0, function* (sdkMap, activeProviders, setQuotesForTimestamp, _tokenIn, _tokenOut, _value, _timestamp = new Date().getTime()) {
46
- const quotesForTimestamp = [];
47
- setQuotesForTimestamp(_timestamp, quotesForTimestamp);
48
- const amountIn = new bignumber_js_1.default(_value)
49
- .times(10 ** _tokenIn.decimals)
50
- .integerValue(bignumber_js_1.default.ROUND_DOWN)
51
- .toString();
52
- // Fetch quotes in parallel
53
- // Aftermath
54
- if (activeProviders.includes(QuoteProvider.AFTERMATH)) {
55
- (() => __awaiter(void 0, void 0, void 0, function* () {
56
- console.log("[fetchAggQuotes] fetching Aftermath quote");
57
- try {
58
- const quote = yield sdkMap[QuoteProvider.AFTERMATH]
59
- .Router()
60
- .getCompleteTradeRouteGivenAmountIn({
61
- coinInType: _tokenIn.coinType,
62
- coinOutType: _tokenOut.coinType,
63
- coinInAmount: BigInt(amountIn),
64
- });
65
- const standardizedQuote = {
45
+ const fetchAggQuoteWrapper = (provider, fetchAggQuote) => __awaiter(void 0, void 0, void 0, function* () {
46
+ console.log(`[fetchAggQuoteWrapper] fetching ${provider} quote`);
47
+ try {
48
+ const standardizedQuote = yield fetchAggQuote();
49
+ if (!standardizedQuote)
50
+ throw new Error("No quote returned");
51
+ console.log(`[fetchAggQuoteWrapper] fetched ${provider} quote`, +standardizedQuote.out.amount, "pool providers:", (0, exports.getPoolProviders)(standardizedQuote), "quote:", standardizedQuote.quote);
52
+ return standardizedQuote;
53
+ }
54
+ catch (err) {
55
+ console.error(err);
56
+ return null;
57
+ }
58
+ });
59
+ const fetchAftermathQuote = (sdk, tokenIn, tokenOut, amountIn) => __awaiter(void 0, void 0, void 0, function* () {
60
+ const quote = yield sdk.Router().getCompleteTradeRouteGivenAmountIn({
61
+ coinInType: tokenIn.coinType,
62
+ coinOutType: tokenOut.coinType,
63
+ coinInAmount: BigInt(amountIn),
64
+ });
65
+ const standardizedQuote = {
66
+ id: (0, uuid_1.v4)(),
67
+ provider: QuoteProvider.AFTERMATH,
68
+ in: {
69
+ coinType: tokenIn.coinType,
70
+ amount: new bignumber_js_1.default(quote.coinIn.amount.toString()).div(10 ** tokenIn.decimals),
71
+ },
72
+ out: {
73
+ coinType: tokenOut.coinType,
74
+ amount: new bignumber_js_1.default(quote.coinOut.amount.toString()).div(10 ** tokenOut.decimals),
75
+ },
76
+ routes: quote.routes.map((route, routeIndex) => ({
77
+ percent: new bignumber_js_1.default(route.portion.toString()).div(lib_1.WAD).times(100),
78
+ path: route.paths.map((path) => ({
79
+ id: (0, uuid_1.v4)(),
80
+ poolId: path.poolId,
81
+ routeIndex,
82
+ provider: path.protocolName,
83
+ in: {
84
+ coinType: (0, utils_1.normalizeStructTag)(path.coinIn.type),
85
+ amount: new bignumber_js_1.default(path.coinIn.amount.toString()).div(10 ** tokenIn.decimals),
86
+ },
87
+ out: {
88
+ coinType: (0, utils_1.normalizeStructTag)(path.coinOut.type),
89
+ amount: new bignumber_js_1.default(path.coinOut.amount.toString()).div(10 ** tokenOut.decimals),
90
+ },
91
+ })),
92
+ })),
93
+ quote,
94
+ };
95
+ return standardizedQuote;
96
+ });
97
+ const fetchCetusQuote = (sdk, tokenIn, tokenOut, amountIn) => __awaiter(void 0, void 0, void 0, function* () {
98
+ const quote = yield sdk.findRouters({
99
+ from: tokenIn.coinType,
100
+ target: tokenOut.coinType,
101
+ amount: new bn_js_1.default(amountIn),
102
+ byAmountIn: true,
103
+ });
104
+ if (!quote)
105
+ return null;
106
+ const standardizedQuote = {
107
+ id: (0, uuid_1.v4)(),
108
+ provider: QuoteProvider.CETUS,
109
+ in: {
110
+ coinType: tokenIn.coinType,
111
+ amount: new bignumber_js_1.default(quote.amountIn.toString()).div(10 ** tokenIn.decimals),
112
+ },
113
+ out: {
114
+ coinType: tokenOut.coinType,
115
+ amount: new bignumber_js_1.default(quote.amountOut.toString()).div(10 ** tokenOut.decimals),
116
+ },
117
+ routes: quote.routes.map((route, routeIndex) => ({
118
+ percent: new bignumber_js_1.default(route.amountIn.toString())
119
+ .div(quote.amountIn.toString())
120
+ .times(100),
121
+ path: route.path.map((path) => ({
122
+ id: (0, uuid_1.v4)(),
123
+ poolId: path.id,
124
+ routeIndex,
125
+ provider: path.provider,
126
+ in: {
127
+ coinType: (0, utils_1.normalizeStructTag)(path.from),
128
+ amount: new bignumber_js_1.default(path.amountIn.toString()).div(10 ** tokenIn.decimals),
129
+ },
130
+ out: {
131
+ coinType: (0, utils_1.normalizeStructTag)(path.target),
132
+ amount: new bignumber_js_1.default(path.amountOut.toString()).div(10 ** tokenOut.decimals),
133
+ },
134
+ })),
135
+ })),
136
+ quote,
137
+ };
138
+ return standardizedQuote;
139
+ });
140
+ const fetch7kQuote = (tokenIn, tokenOut, amountIn) => __awaiter(void 0, void 0, void 0, function* () {
141
+ var _a;
142
+ const quote = yield (0, cjs_1.getQuote)({
143
+ tokenIn: tokenIn.coinType,
144
+ tokenOut: tokenOut.coinType,
145
+ amountIn,
146
+ });
147
+ const standardizedQuote = {
148
+ id: (0, uuid_1.v4)(),
149
+ provider: QuoteProvider._7K,
150
+ in: {
151
+ coinType: tokenIn.coinType,
152
+ amount: new bignumber_js_1.default(quote.swapAmount),
153
+ },
154
+ out: {
155
+ coinType: tokenOut.coinType,
156
+ amount: new bignumber_js_1.default(quote.returnAmount),
157
+ },
158
+ routes: ((_a = quote.routes) !== null && _a !== void 0 ? _a : []).map((route, routeIndex) => ({
159
+ percent: new bignumber_js_1.default(route.tokenInAmount)
160
+ .div(quote.swapAmount)
161
+ .times(100),
162
+ path: route.hops.map((hop) => ({
163
+ id: (0, uuid_1.v4)(),
164
+ poolId: hop.poolId,
165
+ routeIndex,
166
+ provider: hop.pool.type,
167
+ in: {
168
+ coinType: (0, utils_1.normalizeStructTag)(hop.tokenIn),
169
+ amount: new bignumber_js_1.default(hop.tokenInAmount),
170
+ },
171
+ out: {
172
+ coinType: (0, utils_1.normalizeStructTag)(hop.tokenOut),
173
+ amount: new bignumber_js_1.default(hop.tokenOutAmount),
174
+ },
175
+ })),
176
+ })),
177
+ quote,
178
+ };
179
+ return standardizedQuote;
180
+ });
181
+ const fetchFlowXQuote = (sdk, tokenIn, tokenOut, amountIn) => __awaiter(void 0, void 0, void 0, function* () {
182
+ var _a;
183
+ const quote = yield sdk.getRoutes({
184
+ tokenIn: tokenIn.coinType,
185
+ tokenOut: tokenOut.coinType,
186
+ amountIn: amountIn,
187
+ });
188
+ const standardizedQuote = {
189
+ id: (0, uuid_1.v4)(),
190
+ provider: QuoteProvider.FLOWX,
191
+ in: {
192
+ coinType: tokenIn.coinType,
193
+ amount: new bignumber_js_1.default(quote.amountIn.toString()).div(10 ** tokenIn.decimals),
194
+ },
195
+ out: {
196
+ coinType: tokenOut.coinType,
197
+ amount: new bignumber_js_1.default(quote.amountOut.toString()).div(10 ** tokenOut.decimals),
198
+ },
199
+ routes: ((_a = quote.routes) !== null && _a !== void 0 ? _a : []).map((route, routeIndex) => ({
200
+ percent: new bignumber_js_1.default(route.amountIn.toString())
201
+ .div(quote.amountIn.toString())
202
+ .times(100),
203
+ path: route.paths.map((hop) => ({
204
+ id: (0, uuid_1.v4)(),
205
+ poolId: hop.pool.id,
206
+ routeIndex,
207
+ provider: hop.protocol(),
208
+ in: {
209
+ coinType: (0, utils_1.normalizeStructTag)(hop.input.coinType),
210
+ amount: new bignumber_js_1.default(hop.amountIn.toString()).div(10 ** tokenIn.decimals),
211
+ },
212
+ out: {
213
+ coinType: (0, utils_1.normalizeStructTag)(hop.output.coinType),
214
+ amount: new bignumber_js_1.default(hop.amountOut.toString()).div(10 ** tokenOut.decimals),
215
+ },
216
+ })),
217
+ })),
218
+ quote,
219
+ };
220
+ return standardizedQuote;
221
+ });
222
+ const fetchOkxDexQuote = (amountIn, tokenIn, tokenOut) => __awaiter(void 0, void 0, void 0, function* () {
223
+ const quote = yield (0, okxDex_1.getOkxDexQuote)(amountIn, tokenIn.coinType, tokenOut.coinType);
224
+ const flattenedDexRouterList = [];
225
+ for (const dexRouter of quote.dexRouterList) {
226
+ const indexes = [];
227
+ for (const subRouter of dexRouter.subRouterList) {
228
+ indexes.push(Array.from({ length: subRouter.dexProtocol.length }, (_, j) => j));
229
+ }
230
+ const combinations = (0, okxDex_1.cartesianProduct)(indexes);
231
+ for (const combination of combinations) {
232
+ const flattenedRouter = Object.assign(Object.assign({}, dexRouter), { routerPercent: "", subRouterList: dexRouter.subRouterList.map((subRouter, index) => (Object.assign(Object.assign({}, subRouter), { dexProtocol: [subRouter.dexProtocol[combination[index]]] }))) });
233
+ let routerPercent = new bignumber_js_1.default(dexRouter.routerPercent);
234
+ for (const subRouter of flattenedRouter.subRouterList) {
235
+ const dexProtocol = subRouter.dexProtocol[0];
236
+ routerPercent = routerPercent.times(new bignumber_js_1.default(dexProtocol.percent).div(100));
237
+ }
238
+ flattenedRouter.routerPercent = routerPercent.toString();
239
+ flattenedDexRouterList.push(flattenedRouter);
240
+ }
241
+ }
242
+ const standardizedQuote = {
243
+ id: (0, uuid_1.v4)(),
244
+ provider: QuoteProvider.OKX_DEX,
245
+ in: {
246
+ coinType: tokenIn.coinType,
247
+ amount: new bignumber_js_1.default(quote.fromTokenAmount).div(10 ** tokenIn.decimals),
248
+ },
249
+ out: {
250
+ coinType: tokenOut.coinType,
251
+ amount: new bignumber_js_1.default(quote.toTokenAmount).div(10 ** tokenOut.decimals),
252
+ },
253
+ routes: flattenedDexRouterList.map((dexRouter, routeIndex) => {
254
+ return {
255
+ percent: new bignumber_js_1.default(dexRouter.routerPercent),
256
+ path: dexRouter.subRouterList.map((subRouter) => ({
66
257
  id: (0, uuid_1.v4)(),
67
- provider: QuoteProvider.AFTERMATH,
258
+ poolId: undefined, // Missing data
259
+ routeIndex,
260
+ provider: subRouter.dexProtocol[0].dexName,
68
261
  in: {
69
- coinType: _tokenIn.coinType,
70
- amount: new bignumber_js_1.default(quote.coinIn.amount.toString()).div(10 ** _tokenIn.decimals),
262
+ coinType: (0, utils_1.normalizeStructTag)(subRouter.fromToken.tokenContractAddress),
263
+ amount: new bignumber_js_1.default(0).div(10 ** tokenIn.decimals), // Missing data
71
264
  },
72
265
  out: {
73
- coinType: _tokenOut.coinType,
74
- amount: new bignumber_js_1.default(quote.coinOut.amount.toString()).div(10 ** _tokenOut.decimals),
266
+ coinType: (0, utils_1.normalizeStructTag)(subRouter.toToken.tokenContractAddress),
267
+ amount: new bignumber_js_1.default(0).div(10 ** tokenIn.decimals), // Missing data
75
268
  },
76
- routes: quote.routes.map((route, routeIndex) => ({
77
- percent: new bignumber_js_1.default(route.portion.toString())
78
- .div(lib_1.WAD)
79
- .times(100),
80
- path: route.paths.map((path) => ({
81
- id: (0, uuid_1.v4)(),
82
- poolId: path.poolId,
83
- routeIndex,
84
- provider: path.protocolName,
85
- in: {
86
- coinType: (0, utils_1.normalizeStructTag)(path.coinIn.type),
87
- amount: new bignumber_js_1.default(path.coinIn.amount.toString()).div(10 ** _tokenIn.decimals),
88
- },
89
- out: {
90
- coinType: (0, utils_1.normalizeStructTag)(path.coinOut.type),
91
- amount: new bignumber_js_1.default(path.coinOut.amount.toString()).div(10 ** _tokenOut.decimals),
92
- },
93
- })),
94
- })),
95
- quote,
96
- };
97
- quotesForTimestamp.push(standardizedQuote);
98
- setQuotesForTimestamp(_timestamp, quotesForTimestamp);
99
- console.log("[fetchAggQuotes] set Aftermath quote", +standardizedQuote.out.amount, "pool providers:", (0, exports.getPoolProviders)(standardizedQuote), "quote:", quote);
100
- }
101
- catch (err) {
102
- console.error(err);
103
- }
269
+ })),
270
+ };
271
+ }),
272
+ quote,
273
+ };
274
+ return standardizedQuote;
275
+ });
276
+ const fetchAggQuotes = (sdkMap, activeProviders, onFetchAggQuote, tokenIn, tokenOut, amountIn) => __awaiter(void 0, void 0, void 0, function* () {
277
+ // Fetch quotes in parallel
278
+ // Aftermath
279
+ if (activeProviders.includes(QuoteProvider.AFTERMATH)) {
280
+ (() => __awaiter(void 0, void 0, void 0, function* () {
281
+ const standardizedQuote = yield fetchAggQuoteWrapper(QuoteProvider.AFTERMATH, () => fetchAftermathQuote(sdkMap[QuoteProvider.AFTERMATH], tokenIn, tokenOut, amountIn));
282
+ onFetchAggQuote(standardizedQuote);
104
283
  }))();
105
284
  }
106
285
  // Cetus
107
286
  if (activeProviders.includes(QuoteProvider.CETUS)) {
108
287
  (() => __awaiter(void 0, void 0, void 0, function* () {
109
- console.log("[fetchAggQuotes] fetching Cetus quote");
110
- try {
111
- const quote = yield sdkMap[QuoteProvider.CETUS].findRouters({
112
- from: _tokenIn.coinType,
113
- target: _tokenOut.coinType,
114
- amount: new bn_js_1.default(amountIn),
115
- byAmountIn: true,
116
- });
117
- if (!quote)
118
- return;
119
- const standardizedQuote = {
120
- id: (0, uuid_1.v4)(),
121
- provider: QuoteProvider.CETUS,
122
- in: {
123
- coinType: _tokenIn.coinType,
124
- amount: new bignumber_js_1.default(quote.amountIn.toString()).div(10 ** _tokenIn.decimals),
125
- },
126
- out: {
127
- coinType: _tokenOut.coinType,
128
- amount: new bignumber_js_1.default(quote.amountOut.toString()).div(10 ** _tokenOut.decimals),
129
- },
130
- routes: quote.routes.map((route, routeIndex) => ({
131
- percent: new bignumber_js_1.default(route.amountIn.toString())
132
- .div(quote.amountIn.toString())
133
- .times(100),
134
- path: route.path.map((path) => ({
135
- id: (0, uuid_1.v4)(),
136
- poolId: path.id,
137
- routeIndex,
138
- provider: path.provider,
139
- in: {
140
- coinType: (0, utils_1.normalizeStructTag)(path.from),
141
- amount: new bignumber_js_1.default(path.amountIn.toString()).div(10 ** _tokenIn.decimals),
142
- },
143
- out: {
144
- coinType: (0, utils_1.normalizeStructTag)(path.target),
145
- amount: new bignumber_js_1.default(path.amountOut.toString()).div(10 ** _tokenOut.decimals),
146
- },
147
- })),
148
- })),
149
- quote,
150
- };
151
- quotesForTimestamp.push(standardizedQuote);
152
- setQuotesForTimestamp(_timestamp, quotesForTimestamp);
153
- console.log("[fetchAggQuotes] set Cetus quote", +standardizedQuote.out.amount, "pool providers:", (0, exports.getPoolProviders)(standardizedQuote), "quote:", quote);
154
- }
155
- catch (err) {
156
- console.error(err);
157
- }
288
+ const standardizedQuote = yield fetchAggQuoteWrapper(QuoteProvider.CETUS, () => fetchCetusQuote(sdkMap[QuoteProvider.CETUS], tokenIn, tokenOut, amountIn));
289
+ onFetchAggQuote(standardizedQuote);
158
290
  }))();
159
291
  }
160
292
  // 7K
161
293
  if (activeProviders.includes(QuoteProvider._7K)) {
162
294
  (() => __awaiter(void 0, void 0, void 0, function* () {
163
- var _a;
164
- console.log("[fetchAggQuotes] fetching 7K quote");
165
- try {
166
- const quote = yield (0, cjs_1.getQuote)({
167
- tokenIn: _tokenIn.coinType,
168
- tokenOut: _tokenOut.coinType,
169
- amountIn,
170
- });
171
- const standardizedQuote = {
172
- id: (0, uuid_1.v4)(),
173
- provider: QuoteProvider._7K,
174
- in: {
175
- coinType: _tokenIn.coinType,
176
- amount: new bignumber_js_1.default(quote.swapAmount),
177
- },
178
- out: {
179
- coinType: _tokenOut.coinType,
180
- amount: new bignumber_js_1.default(quote.returnAmount),
181
- },
182
- routes: ((_a = quote.routes) !== null && _a !== void 0 ? _a : []).map((route, routeIndex) => ({
183
- percent: new bignumber_js_1.default(route.tokenInAmount)
184
- .div(quote.swapAmount)
185
- .times(100),
186
- path: route.hops.map((hop) => ({
187
- id: (0, uuid_1.v4)(),
188
- poolId: hop.poolId,
189
- routeIndex,
190
- provider: hop.pool.type,
191
- in: {
192
- coinType: (0, utils_1.normalizeStructTag)(hop.tokenIn),
193
- amount: new bignumber_js_1.default(hop.tokenInAmount),
194
- },
195
- out: {
196
- coinType: (0, utils_1.normalizeStructTag)(hop.tokenOut),
197
- amount: new bignumber_js_1.default(hop.tokenOutAmount),
198
- },
199
- })),
200
- })),
201
- quote,
202
- };
203
- quotesForTimestamp.push(standardizedQuote);
204
- setQuotesForTimestamp(_timestamp, quotesForTimestamp);
205
- console.log("[fetchAggQuotes] set 7K quote", +standardizedQuote.out.amount, "pool providers:", (0, exports.getPoolProviders)(standardizedQuote), "quote:", quote);
206
- }
207
- catch (err) {
208
- console.error(err);
209
- }
295
+ const standardizedQuote = yield fetchAggQuoteWrapper(QuoteProvider._7K, () => fetch7kQuote(tokenIn, tokenOut, amountIn));
296
+ onFetchAggQuote(standardizedQuote);
210
297
  }))();
211
298
  }
212
299
  // FlowX
213
300
  if (activeProviders.includes(QuoteProvider.FLOWX)) {
214
301
  (() => __awaiter(void 0, void 0, void 0, function* () {
215
- var _a;
216
- console.log("[fetchAggQuotes] fetching FlowX quote");
217
- try {
218
- const quote = yield sdkMap[QuoteProvider.FLOWX].getRoutes({
219
- tokenIn: _tokenIn.coinType,
220
- tokenOut: _tokenOut.coinType,
221
- amountIn: amountIn,
222
- });
223
- const standardizedQuote = {
224
- id: (0, uuid_1.v4)(),
225
- provider: QuoteProvider.FLOWX,
226
- in: {
227
- coinType: _tokenIn.coinType,
228
- amount: new bignumber_js_1.default(quote.amountIn.toString()).div(10 ** _tokenIn.decimals),
229
- },
230
- out: {
231
- coinType: _tokenOut.coinType,
232
- amount: new bignumber_js_1.default(quote.amountOut.toString()).div(10 ** _tokenOut.decimals),
233
- },
234
- routes: ((_a = quote.routes) !== null && _a !== void 0 ? _a : []).map((route, routeIndex) => ({
235
- percent: new bignumber_js_1.default(route.amountIn.toString())
236
- .div(quote.amountIn.toString())
237
- .times(100),
238
- path: route.paths.map((hop) => ({
239
- id: (0, uuid_1.v4)(),
240
- poolId: hop.pool.id,
241
- routeIndex,
242
- provider: hop.protocol(),
243
- in: {
244
- coinType: (0, utils_1.normalizeStructTag)(hop.input.coinType),
245
- amount: new bignumber_js_1.default(hop.amountIn.toString()).div(10 ** _tokenIn.decimals),
246
- },
247
- out: {
248
- coinType: (0, utils_1.normalizeStructTag)(hop.output.coinType),
249
- amount: new bignumber_js_1.default(hop.amountOut.toString()).div(10 ** _tokenOut.decimals),
250
- },
251
- })),
252
- })),
253
- quote,
254
- };
255
- quotesForTimestamp.push(standardizedQuote);
256
- setQuotesForTimestamp(_timestamp, quotesForTimestamp);
257
- console.log("[fetchAggQuotes] set FlowX quote", +standardizedQuote.out.amount, "pool providers:", (0, exports.getPoolProviders)(standardizedQuote), "quote:", quote);
258
- }
259
- catch (err) {
260
- console.error(err);
261
- }
302
+ const standardizedQuote = yield fetchAggQuoteWrapper(QuoteProvider.FLOWX, () => fetchFlowXQuote(sdkMap[QuoteProvider.FLOWX], tokenIn, tokenOut, amountIn));
303
+ onFetchAggQuote(standardizedQuote);
262
304
  }))();
263
305
  }
264
306
  // OKX DEX
265
307
  if (activeProviders.includes(QuoteProvider.OKX_DEX)) {
266
308
  (() => __awaiter(void 0, void 0, void 0, function* () {
267
- console.log("[fetchAggQuotes] fetching OKX DEX quote");
268
- try {
269
- const quote = yield (0, okxDex_1.getOkxDexQuote)(amountIn, _tokenIn.coinType, _tokenOut.coinType);
270
- const flattenedDexRouterList = [];
271
- for (const dexRouter of quote.dexRouterList) {
272
- const indexes = [];
273
- for (const subRouter of dexRouter.subRouterList) {
274
- indexes.push(Array.from({ length: subRouter.dexProtocol.length }, (_, j) => j));
275
- }
276
- const combinations = (0, okxDex_1.cartesianProduct)(indexes);
277
- for (const combination of combinations) {
278
- const flattenedRouter = Object.assign(Object.assign({}, dexRouter), { routerPercent: "", subRouterList: dexRouter.subRouterList.map((subRouter, index) => (Object.assign(Object.assign({}, subRouter), { dexProtocol: [subRouter.dexProtocol[combination[index]]] }))) });
279
- let routerPercent = new bignumber_js_1.default(dexRouter.routerPercent);
280
- for (const subRouter of flattenedRouter.subRouterList) {
281
- const dexProtocol = subRouter.dexProtocol[0];
282
- routerPercent = routerPercent.times(new bignumber_js_1.default(dexProtocol.percent).div(100));
283
- }
284
- flattenedRouter.routerPercent = routerPercent.toString();
285
- flattenedDexRouterList.push(flattenedRouter);
286
- }
287
- }
288
- const standardizedQuote = {
289
- id: (0, uuid_1.v4)(),
290
- provider: QuoteProvider.OKX_DEX,
291
- in: {
292
- coinType: _tokenIn.coinType,
293
- amount: new bignumber_js_1.default(quote.fromTokenAmount).div(10 ** _tokenIn.decimals),
294
- },
295
- out: {
296
- coinType: _tokenOut.coinType,
297
- amount: new bignumber_js_1.default(quote.toTokenAmount).div(10 ** _tokenOut.decimals),
298
- },
299
- routes: flattenedDexRouterList.map((dexRouter, routeIndex) => {
300
- return {
301
- percent: new bignumber_js_1.default(dexRouter.routerPercent),
302
- path: dexRouter.subRouterList.map((subRouter) => ({
303
- id: (0, uuid_1.v4)(),
304
- poolId: undefined, // Missing data
305
- routeIndex,
306
- provider: subRouter.dexProtocol[0].dexName,
307
- in: {
308
- coinType: (0, utils_1.normalizeStructTag)(subRouter.fromToken.tokenContractAddress),
309
- amount: new bignumber_js_1.default(0).div(10 ** _tokenIn.decimals), // Missing data
310
- },
311
- out: {
312
- coinType: (0, utils_1.normalizeStructTag)(subRouter.toToken.tokenContractAddress),
313
- amount: new bignumber_js_1.default(0).div(10 ** _tokenIn.decimals), // Missing data
314
- },
315
- })),
316
- };
317
- }),
318
- quote,
319
- };
320
- quotesForTimestamp.push(standardizedQuote);
321
- setQuotesForTimestamp(_timestamp, quotesForTimestamp);
322
- console.log("Swap - set OKX DEX quote", +standardizedQuote.out.amount, "pool providers:", (0, exports.getPoolProviders)(standardizedQuote), "quote:", quote);
323
- }
324
- catch (err) {
325
- console.error(err);
326
- }
309
+ const standardizedQuote = yield fetchAggQuoteWrapper(QuoteProvider.OKX_DEX, () => fetchOkxDexQuote(amountIn, tokenIn, tokenOut));
310
+ onFetchAggQuote(standardizedQuote);
327
311
  }))();
328
312
  }
329
313
  });
330
314
  exports.fetchAggQuotes = fetchAggQuotes;
315
+ const fetchAggQuotesAll = (sdkMap, activeProviders, tokenIn, tokenOut, amountIn) => __awaiter(void 0, void 0, void 0, function* () {
316
+ // Fetch quotes in parallel
317
+ return Promise.all([
318
+ activeProviders.includes(QuoteProvider.AFTERMATH)
319
+ ? fetchAggQuoteWrapper(QuoteProvider.AFTERMATH, () => fetchAftermathQuote(sdkMap[QuoteProvider.AFTERMATH], tokenIn, tokenOut, amountIn))
320
+ : null,
321
+ activeProviders.includes(QuoteProvider.CETUS)
322
+ ? fetchAggQuoteWrapper(QuoteProvider.CETUS, () => fetchCetusQuote(sdkMap[QuoteProvider.CETUS], tokenIn, tokenOut, amountIn))
323
+ : null,
324
+ activeProviders.includes(QuoteProvider._7K)
325
+ ? fetchAggQuoteWrapper(QuoteProvider._7K, () => fetch7kQuote(tokenIn, tokenOut, amountIn))
326
+ : null,
327
+ activeProviders.includes(QuoteProvider.FLOWX)
328
+ ? fetchAggQuoteWrapper(QuoteProvider.FLOWX, () => fetchFlowXQuote(sdkMap[QuoteProvider.FLOWX], tokenIn, tokenOut, amountIn))
329
+ : null,
330
+ activeProviders.includes(QuoteProvider.OKX_DEX)
331
+ ? fetchAggQuoteWrapper(QuoteProvider.OKX_DEX, () => fetchOkxDexQuote(amountIn, tokenIn, tokenOut))
332
+ : null,
333
+ ].filter(Boolean));
334
+ });
335
+ exports.fetchAggQuotesAll = fetchAggQuotesAll;