@suilend/sdk 1.1.61 → 1.1.63

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.61","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.63","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.1","@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
@@ -5,7 +5,6 @@ import { RouterCompleteTradeRoute as AftermathQuote, Aftermath as AftermathSdk }
5
5
  import BigNumber from "bignumber.js";
6
6
  import { Token } from "@suilend/sui-fe";
7
7
  import { OkxDexQuote } from "./okxDex";
8
- export declare const getPoolProviders: (standardizedQuote: StandardizedQuote) => string[];
9
8
  export declare enum QuoteProvider {
10
9
  AFTERMATH = "aftermath",
11
10
  CETUS = "cetus",
@@ -72,8 +71,14 @@ export type StandardizedQuote = {
72
71
  provider: QuoteProvider.OKX_DEX;
73
72
  quote: OkxDexQuote;
74
73
  });
74
+ export declare const getPoolProviders: (standardizedQuote: StandardizedQuote) => string[];
75
75
  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 | null)[]) => 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
@@ -1,4 +1,37 @@
1
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 __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
2
35
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
36
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
37
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -12,21 +45,15 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
12
45
  return (mod && mod.__esModule) ? mod : { "default": mod };
13
46
  };
14
47
  Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.fetchAggQuotes = exports.QUOTE_PROVIDER_NAME_MAP = exports.QuoteProvider = exports.getPoolProviders = void 0;
48
+ exports.fetchAggQuotesAll = exports.fetchAggQuotes = exports.getPoolProviders = exports.QUOTE_PROVIDER_NAME_MAP = exports.QuoteProvider = void 0;
16
49
  const cjs_1 = require("@7kprotocol/sdk-ts/cjs");
17
50
  const utils_1 = require("@mysten/sui/utils");
51
+ const Sentry = __importStar(require("@sentry/nextjs"));
18
52
  const bignumber_js_1 = __importDefault(require("bignumber.js"));
19
53
  const bn_js_1 = __importDefault(require("bn.js"));
20
54
  const uuid_1 = require("uuid");
21
55
  const lib_1 = require("../lib");
22
56
  const okxDex_1 = require("./okxDex");
23
- const getPoolProviders = (standardizedQuote) => {
24
- return Array.from(new Set(standardizedQuote.routes.reduce((acc, route) => [
25
- ...acc,
26
- ...route.path.reduce((acc2, p) => [...acc2, p.provider], []),
27
- ], [])));
28
- };
29
- exports.getPoolProviders = getPoolProviders;
30
57
  var QuoteProvider;
31
58
  (function (QuoteProvider) {
32
59
  QuoteProvider["AFTERMATH"] = "aftermath";
@@ -42,299 +69,302 @@ exports.QUOTE_PROVIDER_NAME_MAP = {
42
69
  [QuoteProvider.FLOWX]: "FlowX",
43
70
  [QuoteProvider.OKX_DEX]: "OKX DEX",
44
71
  };
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 = {
72
+ const getPoolProviders = (standardizedQuote) => {
73
+ return Array.from(new Set(standardizedQuote.routes.reduce((acc, route) => [
74
+ ...acc,
75
+ ...route.path.reduce((acc2, p) => [...acc2, p.provider], []),
76
+ ], [])));
77
+ };
78
+ exports.getPoolProviders = getPoolProviders;
79
+ const fetchAggQuoteWrapper = (provider, fetchAggQuote) => __awaiter(void 0, void 0, void 0, function* () {
80
+ console.log(`[fetchAggQuoteWrapper] fetching ${provider} quote`);
81
+ try {
82
+ const standardizedQuote = yield fetchAggQuote();
83
+ if (!standardizedQuote)
84
+ throw new Error("No quote returned");
85
+ console.log(`[fetchAggQuoteWrapper] fetched ${provider} quote`, +standardizedQuote.out.amount, "pool providers:", (0, exports.getPoolProviders)(standardizedQuote), "quote:", standardizedQuote.quote);
86
+ return standardizedQuote;
87
+ }
88
+ catch (err) {
89
+ Sentry.captureException(err, { provider });
90
+ console.error(err);
91
+ return null;
92
+ }
93
+ });
94
+ const fetchAftermathQuote = (sdk, tokenIn, tokenOut, amountIn) => __awaiter(void 0, void 0, void 0, function* () {
95
+ const quote = yield sdk.Router().getCompleteTradeRouteGivenAmountIn({
96
+ coinInType: tokenIn.coinType,
97
+ coinOutType: tokenOut.coinType,
98
+ coinInAmount: BigInt(amountIn),
99
+ });
100
+ const standardizedQuote = {
101
+ id: (0, uuid_1.v4)(),
102
+ provider: QuoteProvider.AFTERMATH,
103
+ in: {
104
+ coinType: tokenIn.coinType,
105
+ amount: new bignumber_js_1.default(quote.coinIn.amount.toString()).div(10 ** tokenIn.decimals),
106
+ },
107
+ out: {
108
+ coinType: tokenOut.coinType,
109
+ amount: new bignumber_js_1.default(quote.coinOut.amount.toString()).div(10 ** tokenOut.decimals),
110
+ },
111
+ routes: quote.routes.map((route, routeIndex) => ({
112
+ percent: new bignumber_js_1.default(route.portion.toString()).div(lib_1.WAD).times(100),
113
+ path: route.paths.map((path) => ({
114
+ id: (0, uuid_1.v4)(),
115
+ poolId: path.poolId,
116
+ routeIndex,
117
+ provider: path.protocolName,
118
+ in: {
119
+ coinType: (0, utils_1.normalizeStructTag)(path.coinIn.type),
120
+ amount: new bignumber_js_1.default(path.coinIn.amount.toString()).div(10 ** tokenIn.decimals),
121
+ },
122
+ out: {
123
+ coinType: (0, utils_1.normalizeStructTag)(path.coinOut.type),
124
+ amount: new bignumber_js_1.default(path.coinOut.amount.toString()).div(10 ** tokenOut.decimals),
125
+ },
126
+ })),
127
+ })),
128
+ quote,
129
+ };
130
+ return standardizedQuote;
131
+ });
132
+ const fetchCetusQuote = (sdk, tokenIn, tokenOut, amountIn) => __awaiter(void 0, void 0, void 0, function* () {
133
+ const quote = yield sdk.findRouters({
134
+ from: tokenIn.coinType,
135
+ target: tokenOut.coinType,
136
+ amount: new bn_js_1.default(amountIn),
137
+ byAmountIn: true,
138
+ });
139
+ if (!quote)
140
+ return null;
141
+ const standardizedQuote = {
142
+ id: (0, uuid_1.v4)(),
143
+ provider: QuoteProvider.CETUS,
144
+ in: {
145
+ coinType: tokenIn.coinType,
146
+ amount: new bignumber_js_1.default(quote.amountIn.toString()).div(10 ** tokenIn.decimals),
147
+ },
148
+ out: {
149
+ coinType: tokenOut.coinType,
150
+ amount: new bignumber_js_1.default(quote.amountOut.toString()).div(10 ** tokenOut.decimals),
151
+ },
152
+ routes: quote.routes.map((route, routeIndex) => ({
153
+ percent: new bignumber_js_1.default(route.amountIn.toString())
154
+ .div(quote.amountIn.toString())
155
+ .times(100),
156
+ path: route.path.map((path) => ({
157
+ id: (0, uuid_1.v4)(),
158
+ poolId: path.id,
159
+ routeIndex,
160
+ provider: path.provider,
161
+ in: {
162
+ coinType: (0, utils_1.normalizeStructTag)(path.from),
163
+ amount: new bignumber_js_1.default(path.amountIn.toString()).div(10 ** tokenIn.decimals),
164
+ },
165
+ out: {
166
+ coinType: (0, utils_1.normalizeStructTag)(path.target),
167
+ amount: new bignumber_js_1.default(path.amountOut.toString()).div(10 ** tokenOut.decimals),
168
+ },
169
+ })),
170
+ })),
171
+ quote,
172
+ };
173
+ return standardizedQuote;
174
+ });
175
+ const fetch7kQuote = (tokenIn, tokenOut, amountIn) => __awaiter(void 0, void 0, void 0, function* () {
176
+ var _a;
177
+ const quote = yield (0, cjs_1.getQuote)({
178
+ tokenIn: tokenIn.coinType,
179
+ tokenOut: tokenOut.coinType,
180
+ amountIn,
181
+ });
182
+ const standardizedQuote = {
183
+ id: (0, uuid_1.v4)(),
184
+ provider: QuoteProvider._7K,
185
+ in: {
186
+ coinType: tokenIn.coinType,
187
+ amount: new bignumber_js_1.default(quote.swapAmount),
188
+ },
189
+ out: {
190
+ coinType: tokenOut.coinType,
191
+ amount: new bignumber_js_1.default(quote.returnAmount),
192
+ },
193
+ routes: ((_a = quote.routes) !== null && _a !== void 0 ? _a : []).map((route, routeIndex) => ({
194
+ percent: new bignumber_js_1.default(route.tokenInAmount)
195
+ .div(quote.swapAmount)
196
+ .times(100),
197
+ path: route.hops.map((hop) => ({
198
+ id: (0, uuid_1.v4)(),
199
+ poolId: hop.poolId,
200
+ routeIndex,
201
+ provider: hop.pool.type,
202
+ in: {
203
+ coinType: (0, utils_1.normalizeStructTag)(hop.tokenIn),
204
+ amount: new bignumber_js_1.default(hop.tokenInAmount),
205
+ },
206
+ out: {
207
+ coinType: (0, utils_1.normalizeStructTag)(hop.tokenOut),
208
+ amount: new bignumber_js_1.default(hop.tokenOutAmount),
209
+ },
210
+ })),
211
+ })),
212
+ quote,
213
+ };
214
+ return standardizedQuote;
215
+ });
216
+ const fetchFlowXQuote = (sdk, tokenIn, tokenOut, amountIn) => __awaiter(void 0, void 0, void 0, function* () {
217
+ var _a;
218
+ const quote = yield sdk.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
+ return standardizedQuote;
256
+ });
257
+ const fetchOkxDexQuote = (amountIn, tokenIn, tokenOut) => __awaiter(void 0, void 0, void 0, function* () {
258
+ const quote = yield (0, okxDex_1.getOkxDexQuote)(amountIn, tokenIn.coinType, tokenOut.coinType);
259
+ const flattenedDexRouterList = [];
260
+ for (const dexRouter of quote.dexRouterList) {
261
+ const indexes = [];
262
+ for (const subRouter of dexRouter.subRouterList) {
263
+ indexes.push(Array.from({ length: subRouter.dexProtocol.length }, (_, j) => j));
264
+ }
265
+ const combinations = (0, okxDex_1.cartesianProduct)(indexes);
266
+ for (const combination of combinations) {
267
+ const flattenedRouter = Object.assign(Object.assign({}, dexRouter), { routerPercent: "", subRouterList: dexRouter.subRouterList.map((subRouter, index) => (Object.assign(Object.assign({}, subRouter), { dexProtocol: [subRouter.dexProtocol[combination[index]]] }))) });
268
+ let routerPercent = new bignumber_js_1.default(dexRouter.routerPercent);
269
+ for (const subRouter of flattenedRouter.subRouterList) {
270
+ const dexProtocol = subRouter.dexProtocol[0];
271
+ routerPercent = routerPercent.times(new bignumber_js_1.default(dexProtocol.percent).div(100));
272
+ }
273
+ flattenedRouter.routerPercent = routerPercent.toString();
274
+ flattenedDexRouterList.push(flattenedRouter);
275
+ }
276
+ }
277
+ const standardizedQuote = {
278
+ id: (0, uuid_1.v4)(),
279
+ provider: QuoteProvider.OKX_DEX,
280
+ in: {
281
+ coinType: tokenIn.coinType,
282
+ amount: new bignumber_js_1.default(quote.fromTokenAmount).div(10 ** tokenIn.decimals),
283
+ },
284
+ out: {
285
+ coinType: tokenOut.coinType,
286
+ amount: new bignumber_js_1.default(quote.toTokenAmount).div(10 ** tokenOut.decimals),
287
+ },
288
+ routes: flattenedDexRouterList.map((dexRouter, routeIndex) => {
289
+ return {
290
+ percent: new bignumber_js_1.default(dexRouter.routerPercent),
291
+ path: dexRouter.subRouterList.map((subRouter) => ({
66
292
  id: (0, uuid_1.v4)(),
67
- provider: QuoteProvider.AFTERMATH,
293
+ poolId: undefined, // Missing data
294
+ routeIndex,
295
+ provider: subRouter.dexProtocol[0].dexName,
68
296
  in: {
69
- coinType: _tokenIn.coinType,
70
- amount: new bignumber_js_1.default(quote.coinIn.amount.toString()).div(10 ** _tokenIn.decimals),
297
+ coinType: (0, utils_1.normalizeStructTag)(subRouter.fromToken.tokenContractAddress),
298
+ amount: new bignumber_js_1.default(0).div(10 ** tokenIn.decimals), // Missing data
71
299
  },
72
300
  out: {
73
- coinType: _tokenOut.coinType,
74
- amount: new bignumber_js_1.default(quote.coinOut.amount.toString()).div(10 ** _tokenOut.decimals),
301
+ coinType: (0, utils_1.normalizeStructTag)(subRouter.toToken.tokenContractAddress),
302
+ amount: new bignumber_js_1.default(0).div(10 ** tokenIn.decimals), // Missing data
75
303
  },
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
- quotesForTimestamp.push(null);
104
- setQuotesForTimestamp(_timestamp, quotesForTimestamp);
105
- }
304
+ })),
305
+ };
306
+ }),
307
+ quote,
308
+ };
309
+ return standardizedQuote;
310
+ });
311
+ const fetchAggQuotes = (sdkMap, activeProviders, onFetchAggQuote, tokenIn, tokenOut, amountIn) => __awaiter(void 0, void 0, void 0, function* () {
312
+ // Fetch quotes in parallel
313
+ // Aftermath
314
+ if (activeProviders.includes(QuoteProvider.AFTERMATH)) {
315
+ (() => __awaiter(void 0, void 0, void 0, function* () {
316
+ const standardizedQuote = yield fetchAggQuoteWrapper(QuoteProvider.AFTERMATH, () => fetchAftermathQuote(sdkMap[QuoteProvider.AFTERMATH], tokenIn, tokenOut, amountIn));
317
+ onFetchAggQuote(standardizedQuote);
106
318
  }))();
107
319
  }
108
320
  // Cetus
109
321
  if (activeProviders.includes(QuoteProvider.CETUS)) {
110
322
  (() => __awaiter(void 0, void 0, void 0, function* () {
111
- console.log("[fetchAggQuotes] fetching Cetus quote");
112
- try {
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
- }
323
+ const standardizedQuote = yield fetchAggQuoteWrapper(QuoteProvider.CETUS, () => fetchCetusQuote(sdkMap[QuoteProvider.CETUS], tokenIn, tokenOut, amountIn));
324
+ onFetchAggQuote(standardizedQuote);
162
325
  }))();
163
326
  }
164
327
  // 7K
165
328
  if (activeProviders.includes(QuoteProvider._7K)) {
166
329
  (() => __awaiter(void 0, void 0, void 0, function* () {
167
- var _a;
168
- console.log("[fetchAggQuotes] fetching 7K quote");
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
- }
330
+ const standardizedQuote = yield fetchAggQuoteWrapper(QuoteProvider._7K, () => fetch7kQuote(tokenIn, tokenOut, amountIn));
331
+ onFetchAggQuote(standardizedQuote);
216
332
  }))();
217
333
  }
218
334
  // FlowX
219
335
  if (activeProviders.includes(QuoteProvider.FLOWX)) {
220
336
  (() => __awaiter(void 0, void 0, void 0, function* () {
221
- var _a;
222
- console.log("[fetchAggQuotes] fetching FlowX quote");
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
- }
337
+ const standardizedQuote = yield fetchAggQuoteWrapper(QuoteProvider.FLOWX, () => fetchFlowXQuote(sdkMap[QuoteProvider.FLOWX], tokenIn, tokenOut, amountIn));
338
+ onFetchAggQuote(standardizedQuote);
270
339
  }))();
271
340
  }
272
341
  // OKX DEX
273
342
  if (activeProviders.includes(QuoteProvider.OKX_DEX)) {
274
343
  (() => __awaiter(void 0, void 0, void 0, function* () {
275
- console.log("[fetchAggQuotes] fetching OKX DEX quote");
276
- try {
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
- }
344
+ const standardizedQuote = yield fetchAggQuoteWrapper(QuoteProvider.OKX_DEX, () => fetchOkxDexQuote(amountIn, tokenIn, tokenOut));
345
+ onFetchAggQuote(standardizedQuote);
337
346
  }))();
338
347
  }
339
348
  });
340
349
  exports.fetchAggQuotes = fetchAggQuotes;
350
+ const fetchAggQuotesAll = (sdkMap, activeProviders, tokenIn, tokenOut, amountIn) => __awaiter(void 0, void 0, void 0, function* () {
351
+ // Fetch quotes in parallel
352
+ return Promise.all([
353
+ activeProviders.includes(QuoteProvider.AFTERMATH)
354
+ ? fetchAggQuoteWrapper(QuoteProvider.AFTERMATH, () => fetchAftermathQuote(sdkMap[QuoteProvider.AFTERMATH], tokenIn, tokenOut, amountIn))
355
+ : null,
356
+ activeProviders.includes(QuoteProvider.CETUS)
357
+ ? fetchAggQuoteWrapper(QuoteProvider.CETUS, () => fetchCetusQuote(sdkMap[QuoteProvider.CETUS], tokenIn, tokenOut, amountIn))
358
+ : null,
359
+ activeProviders.includes(QuoteProvider._7K)
360
+ ? fetchAggQuoteWrapper(QuoteProvider._7K, () => fetch7kQuote(tokenIn, tokenOut, amountIn))
361
+ : null,
362
+ activeProviders.includes(QuoteProvider.FLOWX)
363
+ ? fetchAggQuoteWrapper(QuoteProvider.FLOWX, () => fetchFlowXQuote(sdkMap[QuoteProvider.FLOWX], tokenIn, tokenOut, amountIn))
364
+ : null,
365
+ activeProviders.includes(QuoteProvider.OKX_DEX)
366
+ ? fetchAggQuoteWrapper(QuoteProvider.OKX_DEX, () => fetchOkxDexQuote(amountIn, tokenIn, tokenOut))
367
+ : null,
368
+ ].filter(Boolean));
369
+ });
370
+ exports.fetchAggQuotesAll = fetchAggQuotesAll;
@@ -14,5 +14,5 @@ export declare const getSwapTransaction: (suiClient: SuiClient, address: string,
14
14
  [QuoteProvider.FLOWX]: string;
15
15
  }, transaction: Transaction, coinIn: TransactionObjectArgument | undefined) => Promise<{
16
16
  transaction: Transaction;
17
- coinOut: TransactionObjectArgument | undefined;
17
+ coinOut?: TransactionObjectArgument;
18
18
  }>;
@@ -1,4 +1,37 @@
1
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 __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
2
35
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
36
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
37
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -13,89 +46,105 @@ exports.getSwapTransaction = void 0;
13
46
  const cjs_1 = require("@7kprotocol/sdk-ts/cjs");
14
47
  const sdk_1 = require("@flowx-finance/sdk");
15
48
  const transactions_1 = require("@mysten/sui/transactions");
49
+ const Sentry = __importStar(require("@sentry/nextjs"));
16
50
  const sui_fe_1 = require("@suilend/sui-fe");
17
51
  const okxDex_1 = require("./okxDex");
18
52
  const quote_1 = require("./quote");
53
+ const getSwapTransactionWrapper = (provider, getSwapTransaction) => __awaiter(void 0, void 0, void 0, function* () {
54
+ console.log(`[getSwapTransactionWrapper] fetching transaction for ${provider} quote`);
55
+ try {
56
+ const res = getSwapTransaction();
57
+ console.log(`[getSwapTransactionWrapper] fetched transaction for ${provider} quote`, res);
58
+ return res;
59
+ }
60
+ catch (err) {
61
+ Sentry.captureException(err, { provider });
62
+ console.error(err);
63
+ throw err;
64
+ }
65
+ });
19
66
  const getSwapTransaction = (suiClient, address, quote, slippagePercent, sdkMap, partnerIdMap, transaction, coinIn) => __awaiter(void 0, void 0, void 0, function* () {
20
- if (!address)
21
- throw new Error("Wallet not connected");
22
- if (!quote)
23
- throw new Error("Quote not found");
24
67
  if (quote.provider === quote_1.QuoteProvider.AFTERMATH) {
25
- console.log("[getSwapTransaction] fetching transaction for Aftermath quote");
26
- const { tx: transaction2, coinOutId: coinOut } = yield sdkMap[quote_1.QuoteProvider.AFTERMATH]
27
- .Router()
28
- .addTransactionForCompleteTradeRoute({
29
- tx: transaction,
30
- walletAddress: address,
31
- completeRoute: quote.quote,
32
- slippage: slippagePercent / 100,
33
- coinInId: coinIn,
34
- });
35
- return { transaction: transaction2, coinOut };
68
+ return getSwapTransactionWrapper(quote_1.QuoteProvider.AFTERMATH, () => __awaiter(void 0, void 0, void 0, function* () {
69
+ const { tx: transaction2, coinOutId: coinOut } = yield sdkMap[quote_1.QuoteProvider.AFTERMATH]
70
+ .Router()
71
+ .addTransactionForCompleteTradeRoute({
72
+ tx: transaction,
73
+ walletAddress: address,
74
+ completeRoute: quote.quote,
75
+ slippage: slippagePercent / 100,
76
+ coinInId: coinIn,
77
+ });
78
+ return { transaction: transaction2, coinOut };
79
+ }));
36
80
  }
37
81
  else if (quote.provider === quote_1.QuoteProvider.CETUS) {
38
- console.log("[getSwapTransaction] fetching transaction for Cetus quote");
39
- if (!coinIn)
40
- coinIn = (0, transactions_1.coinWithBalance)({
41
- balance: BigInt(quote.quote.amountIn.toString()),
42
- type: quote.in.coinType,
43
- useGasCoin: (0, sui_fe_1.isSui)(quote.in.coinType),
44
- })(transaction);
45
- const coinOut = yield sdkMap[quote_1.QuoteProvider.CETUS].routerSwap({
46
- routers: quote.quote,
47
- inputCoin: coinIn,
48
- slippage: slippagePercent / 100,
49
- txb: transaction,
50
- partner: partnerIdMap[quote_1.QuoteProvider.CETUS],
51
- });
52
- return { transaction, coinOut };
82
+ return getSwapTransactionWrapper(quote_1.QuoteProvider.CETUS, () => __awaiter(void 0, void 0, void 0, function* () {
83
+ if (!coinIn)
84
+ coinIn = (0, transactions_1.coinWithBalance)({
85
+ balance: BigInt(quote.quote.amountIn.toString()),
86
+ type: quote.in.coinType,
87
+ useGasCoin: (0, sui_fe_1.isSui)(quote.in.coinType),
88
+ })(transaction);
89
+ const coinOut = yield sdkMap[quote_1.QuoteProvider.CETUS].routerSwap({
90
+ routers: quote.quote,
91
+ inputCoin: coinIn,
92
+ slippage: slippagePercent / 100,
93
+ txb: transaction,
94
+ partner: partnerIdMap[quote_1.QuoteProvider.CETUS],
95
+ });
96
+ return { transaction, coinOut };
97
+ }));
53
98
  }
54
99
  else if (quote.provider === quote_1.QuoteProvider._7K) {
55
- const { tx: transaction2, coinOut } = yield (0, cjs_1.buildTx)({
56
- quoteResponse: quote.quote,
57
- accountAddress: address,
58
- slippage: slippagePercent / 100,
59
- commission: {
60
- partner: partnerIdMap[quote_1.QuoteProvider._7K],
61
- commissionBps: 0,
62
- },
63
- extendTx: {
64
- tx: transaction,
65
- coinIn,
66
- },
67
- });
68
- if (transaction2 instanceof cjs_1.BluefinXTx) {
69
- // BluefinXTx
70
- throw new Error("BluefinXTx not supported");
71
- }
72
- else {
73
- return { transaction: transaction2, coinOut };
74
- }
100
+ return getSwapTransactionWrapper(quote_1.QuoteProvider._7K, () => __awaiter(void 0, void 0, void 0, function* () {
101
+ const { tx: transaction2, coinOut } = yield (0, cjs_1.buildTx)({
102
+ quoteResponse: quote.quote,
103
+ accountAddress: address,
104
+ slippage: slippagePercent / 100,
105
+ commission: {
106
+ partner: partnerIdMap[quote_1.QuoteProvider._7K],
107
+ commissionBps: 0,
108
+ },
109
+ extendTx: {
110
+ tx: transaction,
111
+ coinIn,
112
+ },
113
+ });
114
+ if (transaction2 instanceof cjs_1.BluefinXTx) {
115
+ // BluefinXTx
116
+ throw new Error("BluefinXTx not supported");
117
+ }
118
+ else {
119
+ return { transaction: transaction2, coinOut };
120
+ }
121
+ }));
75
122
  }
76
123
  else if (quote.provider === quote_1.QuoteProvider.FLOWX) {
77
- console.log("[getSwapTransaction] fetching transaction for FlowX quote");
78
- if (!coinIn)
79
- coinIn = (0, transactions_1.coinWithBalance)({
80
- balance: BigInt(quote.quote.amountIn.toString()),
81
- type: quote.in.coinType,
82
- useGasCoin: (0, sui_fe_1.isSui)(quote.in.coinType),
83
- })(transaction);
84
- const trade = new sdk_1.TradeBuilder("mainnet", quote.quote.routes)
85
- .slippage((slippagePercent / 100) * 1e6)
86
- .commission(new sdk_1.Commission(partnerIdMap[quote_1.QuoteProvider.FLOWX], new sdk_1.Coin(quote.out.coinType), sdk_1.CommissionType.PERCENTAGE, 0, true))
87
- .build();
88
- const coinOut = yield trade.swap({
89
- coinIn: coinIn,
90
- client: suiClient,
91
- tx: transaction,
92
- });
93
- return { transaction, coinOut: coinOut };
124
+ return getSwapTransactionWrapper(quote_1.QuoteProvider.FLOWX, () => __awaiter(void 0, void 0, void 0, function* () {
125
+ if (!coinIn)
126
+ coinIn = (0, transactions_1.coinWithBalance)({
127
+ balance: BigInt(quote.quote.amountIn.toString()),
128
+ type: quote.in.coinType,
129
+ useGasCoin: (0, sui_fe_1.isSui)(quote.in.coinType),
130
+ })(transaction);
131
+ const trade = new sdk_1.TradeBuilder("mainnet", quote.quote.routes)
132
+ .slippage((slippagePercent / 100) * 1e6)
133
+ .commission(new sdk_1.Commission(partnerIdMap[quote_1.QuoteProvider.FLOWX], new sdk_1.Coin(quote.out.coinType), sdk_1.CommissionType.PERCENTAGE, 0, true))
134
+ .build();
135
+ const coinOut = yield trade.swap({
136
+ coinIn: coinIn,
137
+ client: suiClient,
138
+ tx: transaction,
139
+ });
140
+ return { transaction, coinOut: coinOut };
141
+ }));
94
142
  }
95
143
  else if (quote.provider === quote_1.QuoteProvider.OKX_DEX) {
96
- console.log("[getSwapTransaction] fetching transaction for OKX DEX quote");
97
- const transaction2 = yield (0, okxDex_1.getOkxDexSwapTransaction)(quote.quote.fromTokenAmount, quote.in.coinType, quote.out.coinType, slippagePercent, address); // Does not use `transaction` or `coinIn`
98
- return { transaction: transaction2, coinOut: undefined };
144
+ return getSwapTransactionWrapper(quote_1.QuoteProvider.OKX_DEX, () => __awaiter(void 0, void 0, void 0, function* () {
145
+ const transaction2 = yield (0, okxDex_1.getOkxDexSwapTransaction)(quote.quote.fromTokenAmount, quote.in.coinType, quote.out.coinType, slippagePercent, address); // Does not use `transaction` or `coinIn`
146
+ return { transaction: transaction2, coinOut: undefined };
147
+ }));
99
148
  }
100
149
  else
101
150
  throw new Error("Unknown quote type");