@suilend/sdk 1.1.61 → 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 +1 -1
- package/swap/quote.d.ts +6 -1
- package/swap/quote.js +263 -268
package/package.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"name":"@suilend/sdk","version":"1.1.
|
|
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[],
|
|
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,299 +42,294 @@ exports.QUOTE_PROVIDER_NAME_MAP = {
|
|
|
42
42
|
[QuoteProvider.FLOWX]: "FlowX",
|
|
43
43
|
[QuoteProvider.OKX_DEX]: "OKX DEX",
|
|
44
44
|
};
|
|
45
|
-
const
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
.
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
(
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
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
|
-
|
|
258
|
+
poolId: undefined, // Missing data
|
|
259
|
+
routeIndex,
|
|
260
|
+
provider: subRouter.dexProtocol[0].dexName,
|
|
68
261
|
in: {
|
|
69
|
-
coinType:
|
|
70
|
-
amount: new bignumber_js_1.default(
|
|
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:
|
|
74
|
-
amount: new bignumber_js_1.default(
|
|
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
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
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
|
-
quotesForTimestamp.push(null);
|
|
104
|
-
setQuotesForTimestamp(_timestamp, quotesForTimestamp);
|
|
105
|
-
}
|
|
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);
|
|
106
283
|
}))();
|
|
107
284
|
}
|
|
108
285
|
// Cetus
|
|
109
286
|
if (activeProviders.includes(QuoteProvider.CETUS)) {
|
|
110
287
|
(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
const quote = yield sdkMap[QuoteProvider.CETUS].findRouters({
|
|
114
|
-
from: _tokenIn.coinType,
|
|
115
|
-
target: _tokenOut.coinType,
|
|
116
|
-
amount: new bn_js_1.default(amountIn),
|
|
117
|
-
byAmountIn: true,
|
|
118
|
-
});
|
|
119
|
-
if (!quote)
|
|
120
|
-
return;
|
|
121
|
-
const standardizedQuote = {
|
|
122
|
-
id: (0, uuid_1.v4)(),
|
|
123
|
-
provider: QuoteProvider.CETUS,
|
|
124
|
-
in: {
|
|
125
|
-
coinType: _tokenIn.coinType,
|
|
126
|
-
amount: new bignumber_js_1.default(quote.amountIn.toString()).div(10 ** _tokenIn.decimals),
|
|
127
|
-
},
|
|
128
|
-
out: {
|
|
129
|
-
coinType: _tokenOut.coinType,
|
|
130
|
-
amount: new bignumber_js_1.default(quote.amountOut.toString()).div(10 ** _tokenOut.decimals),
|
|
131
|
-
},
|
|
132
|
-
routes: quote.routes.map((route, routeIndex) => ({
|
|
133
|
-
percent: new bignumber_js_1.default(route.amountIn.toString())
|
|
134
|
-
.div(quote.amountIn.toString())
|
|
135
|
-
.times(100),
|
|
136
|
-
path: route.path.map((path) => ({
|
|
137
|
-
id: (0, uuid_1.v4)(),
|
|
138
|
-
poolId: path.id,
|
|
139
|
-
routeIndex,
|
|
140
|
-
provider: path.provider,
|
|
141
|
-
in: {
|
|
142
|
-
coinType: (0, utils_1.normalizeStructTag)(path.from),
|
|
143
|
-
amount: new bignumber_js_1.default(path.amountIn.toString()).div(10 ** _tokenIn.decimals),
|
|
144
|
-
},
|
|
145
|
-
out: {
|
|
146
|
-
coinType: (0, utils_1.normalizeStructTag)(path.target),
|
|
147
|
-
amount: new bignumber_js_1.default(path.amountOut.toString()).div(10 ** _tokenOut.decimals),
|
|
148
|
-
},
|
|
149
|
-
})),
|
|
150
|
-
})),
|
|
151
|
-
quote,
|
|
152
|
-
};
|
|
153
|
-
quotesForTimestamp.push(standardizedQuote);
|
|
154
|
-
setQuotesForTimestamp(_timestamp, quotesForTimestamp);
|
|
155
|
-
console.log("[fetchAggQuotes] set Cetus quote", +standardizedQuote.out.amount, "pool providers:", (0, exports.getPoolProviders)(standardizedQuote), "quote:", quote);
|
|
156
|
-
}
|
|
157
|
-
catch (err) {
|
|
158
|
-
console.error(err);
|
|
159
|
-
quotesForTimestamp.push(null);
|
|
160
|
-
setQuotesForTimestamp(_timestamp, quotesForTimestamp);
|
|
161
|
-
}
|
|
288
|
+
const standardizedQuote = yield fetchAggQuoteWrapper(QuoteProvider.CETUS, () => fetchCetusQuote(sdkMap[QuoteProvider.CETUS], tokenIn, tokenOut, amountIn));
|
|
289
|
+
onFetchAggQuote(standardizedQuote);
|
|
162
290
|
}))();
|
|
163
291
|
}
|
|
164
292
|
// 7K
|
|
165
293
|
if (activeProviders.includes(QuoteProvider._7K)) {
|
|
166
294
|
(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
try {
|
|
170
|
-
const quote = yield (0, cjs_1.getQuote)({
|
|
171
|
-
tokenIn: _tokenIn.coinType,
|
|
172
|
-
tokenOut: _tokenOut.coinType,
|
|
173
|
-
amountIn,
|
|
174
|
-
});
|
|
175
|
-
const standardizedQuote = {
|
|
176
|
-
id: (0, uuid_1.v4)(),
|
|
177
|
-
provider: QuoteProvider._7K,
|
|
178
|
-
in: {
|
|
179
|
-
coinType: _tokenIn.coinType,
|
|
180
|
-
amount: new bignumber_js_1.default(quote.swapAmount),
|
|
181
|
-
},
|
|
182
|
-
out: {
|
|
183
|
-
coinType: _tokenOut.coinType,
|
|
184
|
-
amount: new bignumber_js_1.default(quote.returnAmount),
|
|
185
|
-
},
|
|
186
|
-
routes: ((_a = quote.routes) !== null && _a !== void 0 ? _a : []).map((route, routeIndex) => ({
|
|
187
|
-
percent: new bignumber_js_1.default(route.tokenInAmount)
|
|
188
|
-
.div(quote.swapAmount)
|
|
189
|
-
.times(100),
|
|
190
|
-
path: route.hops.map((hop) => ({
|
|
191
|
-
id: (0, uuid_1.v4)(),
|
|
192
|
-
poolId: hop.poolId,
|
|
193
|
-
routeIndex,
|
|
194
|
-
provider: hop.pool.type,
|
|
195
|
-
in: {
|
|
196
|
-
coinType: (0, utils_1.normalizeStructTag)(hop.tokenIn),
|
|
197
|
-
amount: new bignumber_js_1.default(hop.tokenInAmount),
|
|
198
|
-
},
|
|
199
|
-
out: {
|
|
200
|
-
coinType: (0, utils_1.normalizeStructTag)(hop.tokenOut),
|
|
201
|
-
amount: new bignumber_js_1.default(hop.tokenOutAmount),
|
|
202
|
-
},
|
|
203
|
-
})),
|
|
204
|
-
})),
|
|
205
|
-
quote,
|
|
206
|
-
};
|
|
207
|
-
quotesForTimestamp.push(standardizedQuote);
|
|
208
|
-
setQuotesForTimestamp(_timestamp, quotesForTimestamp);
|
|
209
|
-
console.log("[fetchAggQuotes] set 7K quote", +standardizedQuote.out.amount, "pool providers:", (0, exports.getPoolProviders)(standardizedQuote), "quote:", quote);
|
|
210
|
-
}
|
|
211
|
-
catch (err) {
|
|
212
|
-
console.error(err);
|
|
213
|
-
quotesForTimestamp.push(null);
|
|
214
|
-
setQuotesForTimestamp(_timestamp, quotesForTimestamp);
|
|
215
|
-
}
|
|
295
|
+
const standardizedQuote = yield fetchAggQuoteWrapper(QuoteProvider._7K, () => fetch7kQuote(tokenIn, tokenOut, amountIn));
|
|
296
|
+
onFetchAggQuote(standardizedQuote);
|
|
216
297
|
}))();
|
|
217
298
|
}
|
|
218
299
|
// FlowX
|
|
219
300
|
if (activeProviders.includes(QuoteProvider.FLOWX)) {
|
|
220
301
|
(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
try {
|
|
224
|
-
const quote = yield sdkMap[QuoteProvider.FLOWX].getRoutes({
|
|
225
|
-
tokenIn: _tokenIn.coinType,
|
|
226
|
-
tokenOut: _tokenOut.coinType,
|
|
227
|
-
amountIn: amountIn,
|
|
228
|
-
});
|
|
229
|
-
const standardizedQuote = {
|
|
230
|
-
id: (0, uuid_1.v4)(),
|
|
231
|
-
provider: QuoteProvider.FLOWX,
|
|
232
|
-
in: {
|
|
233
|
-
coinType: _tokenIn.coinType,
|
|
234
|
-
amount: new bignumber_js_1.default(quote.amountIn.toString()).div(10 ** _tokenIn.decimals),
|
|
235
|
-
},
|
|
236
|
-
out: {
|
|
237
|
-
coinType: _tokenOut.coinType,
|
|
238
|
-
amount: new bignumber_js_1.default(quote.amountOut.toString()).div(10 ** _tokenOut.decimals),
|
|
239
|
-
},
|
|
240
|
-
routes: ((_a = quote.routes) !== null && _a !== void 0 ? _a : []).map((route, routeIndex) => ({
|
|
241
|
-
percent: new bignumber_js_1.default(route.amountIn.toString())
|
|
242
|
-
.div(quote.amountIn.toString())
|
|
243
|
-
.times(100),
|
|
244
|
-
path: route.paths.map((hop) => ({
|
|
245
|
-
id: (0, uuid_1.v4)(),
|
|
246
|
-
poolId: hop.pool.id,
|
|
247
|
-
routeIndex,
|
|
248
|
-
provider: hop.protocol(),
|
|
249
|
-
in: {
|
|
250
|
-
coinType: (0, utils_1.normalizeStructTag)(hop.input.coinType),
|
|
251
|
-
amount: new bignumber_js_1.default(hop.amountIn.toString()).div(10 ** _tokenIn.decimals),
|
|
252
|
-
},
|
|
253
|
-
out: {
|
|
254
|
-
coinType: (0, utils_1.normalizeStructTag)(hop.output.coinType),
|
|
255
|
-
amount: new bignumber_js_1.default(hop.amountOut.toString()).div(10 ** _tokenOut.decimals),
|
|
256
|
-
},
|
|
257
|
-
})),
|
|
258
|
-
})),
|
|
259
|
-
quote,
|
|
260
|
-
};
|
|
261
|
-
quotesForTimestamp.push(standardizedQuote);
|
|
262
|
-
setQuotesForTimestamp(_timestamp, quotesForTimestamp);
|
|
263
|
-
console.log("[fetchAggQuotes] set FlowX quote", +standardizedQuote.out.amount, "pool providers:", (0, exports.getPoolProviders)(standardizedQuote), "quote:", quote);
|
|
264
|
-
}
|
|
265
|
-
catch (err) {
|
|
266
|
-
console.error(err);
|
|
267
|
-
quotesForTimestamp.push(null);
|
|
268
|
-
setQuotesForTimestamp(_timestamp, quotesForTimestamp);
|
|
269
|
-
}
|
|
302
|
+
const standardizedQuote = yield fetchAggQuoteWrapper(QuoteProvider.FLOWX, () => fetchFlowXQuote(sdkMap[QuoteProvider.FLOWX], tokenIn, tokenOut, amountIn));
|
|
303
|
+
onFetchAggQuote(standardizedQuote);
|
|
270
304
|
}))();
|
|
271
305
|
}
|
|
272
306
|
// OKX DEX
|
|
273
307
|
if (activeProviders.includes(QuoteProvider.OKX_DEX)) {
|
|
274
308
|
(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
const quote = yield (0, okxDex_1.getOkxDexQuote)(amountIn, _tokenIn.coinType, _tokenOut.coinType);
|
|
278
|
-
const flattenedDexRouterList = [];
|
|
279
|
-
for (const dexRouter of quote.dexRouterList) {
|
|
280
|
-
const indexes = [];
|
|
281
|
-
for (const subRouter of dexRouter.subRouterList) {
|
|
282
|
-
indexes.push(Array.from({ length: subRouter.dexProtocol.length }, (_, j) => j));
|
|
283
|
-
}
|
|
284
|
-
const combinations = (0, okxDex_1.cartesianProduct)(indexes);
|
|
285
|
-
for (const combination of combinations) {
|
|
286
|
-
const flattenedRouter = Object.assign(Object.assign({}, dexRouter), { routerPercent: "", subRouterList: dexRouter.subRouterList.map((subRouter, index) => (Object.assign(Object.assign({}, subRouter), { dexProtocol: [subRouter.dexProtocol[combination[index]]] }))) });
|
|
287
|
-
let routerPercent = new bignumber_js_1.default(dexRouter.routerPercent);
|
|
288
|
-
for (const subRouter of flattenedRouter.subRouterList) {
|
|
289
|
-
const dexProtocol = subRouter.dexProtocol[0];
|
|
290
|
-
routerPercent = routerPercent.times(new bignumber_js_1.default(dexProtocol.percent).div(100));
|
|
291
|
-
}
|
|
292
|
-
flattenedRouter.routerPercent = routerPercent.toString();
|
|
293
|
-
flattenedDexRouterList.push(flattenedRouter);
|
|
294
|
-
}
|
|
295
|
-
}
|
|
296
|
-
const standardizedQuote = {
|
|
297
|
-
id: (0, uuid_1.v4)(),
|
|
298
|
-
provider: QuoteProvider.OKX_DEX,
|
|
299
|
-
in: {
|
|
300
|
-
coinType: _tokenIn.coinType,
|
|
301
|
-
amount: new bignumber_js_1.default(quote.fromTokenAmount).div(10 ** _tokenIn.decimals),
|
|
302
|
-
},
|
|
303
|
-
out: {
|
|
304
|
-
coinType: _tokenOut.coinType,
|
|
305
|
-
amount: new bignumber_js_1.default(quote.toTokenAmount).div(10 ** _tokenOut.decimals),
|
|
306
|
-
},
|
|
307
|
-
routes: flattenedDexRouterList.map((dexRouter, routeIndex) => {
|
|
308
|
-
return {
|
|
309
|
-
percent: new bignumber_js_1.default(dexRouter.routerPercent),
|
|
310
|
-
path: dexRouter.subRouterList.map((subRouter) => ({
|
|
311
|
-
id: (0, uuid_1.v4)(),
|
|
312
|
-
poolId: undefined, // Missing data
|
|
313
|
-
routeIndex,
|
|
314
|
-
provider: subRouter.dexProtocol[0].dexName,
|
|
315
|
-
in: {
|
|
316
|
-
coinType: (0, utils_1.normalizeStructTag)(subRouter.fromToken.tokenContractAddress),
|
|
317
|
-
amount: new bignumber_js_1.default(0).div(10 ** _tokenIn.decimals), // Missing data
|
|
318
|
-
},
|
|
319
|
-
out: {
|
|
320
|
-
coinType: (0, utils_1.normalizeStructTag)(subRouter.toToken.tokenContractAddress),
|
|
321
|
-
amount: new bignumber_js_1.default(0).div(10 ** _tokenIn.decimals), // Missing data
|
|
322
|
-
},
|
|
323
|
-
})),
|
|
324
|
-
};
|
|
325
|
-
}),
|
|
326
|
-
quote,
|
|
327
|
-
};
|
|
328
|
-
quotesForTimestamp.push(standardizedQuote);
|
|
329
|
-
setQuotesForTimestamp(_timestamp, quotesForTimestamp);
|
|
330
|
-
console.log("Swap - set OKX DEX quote", +standardizedQuote.out.amount, "pool providers:", (0, exports.getPoolProviders)(standardizedQuote), "quote:", quote);
|
|
331
|
-
}
|
|
332
|
-
catch (err) {
|
|
333
|
-
console.error(err);
|
|
334
|
-
quotesForTimestamp.push(null);
|
|
335
|
-
setQuotesForTimestamp(_timestamp, quotesForTimestamp);
|
|
336
|
-
}
|
|
309
|
+
const standardizedQuote = yield fetchAggQuoteWrapper(QuoteProvider.OKX_DEX, () => fetchOkxDexQuote(amountIn, tokenIn, tokenOut));
|
|
310
|
+
onFetchAggQuote(standardizedQuote);
|
|
337
311
|
}))();
|
|
338
312
|
}
|
|
339
313
|
});
|
|
340
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;
|