@orbs-network/twap 1.7.14 → 1.8.0

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.
@@ -63,20 +63,10 @@ exports.ChainConfigs = {
63
63
  };
64
64
  exports.Configs = {
65
65
  SpiritSwap: Object.assign(Object.assign({}, exports.ChainConfigs.ftm), { partner: "SpiritSwap", exchangeAddress: "0xAd19179201be5A51D1cBd3bB2fC651BB05822404", exchangeType: "ParaswapExchange", pathfinderKey: paraswap_1.Paraswap.OnlyDex.SpiritSwap }),
66
- SpookySwap: Object.assign(Object.assign({}, exports.ChainConfigs.ftm), { partner: "SpookySwap", exchangeAddress: "0x37F427DA0D12Fe2C80aCa09EE08e7e92A1B2B114", exchangeType: "UniswapV2Exchange", pathfinderKey: paraswap_1.Paraswap.OnlyDex.SpookySwap }),
67
- Pangolin: Object.assign(Object.assign({}, exports.ChainConfigs.avax), { partner: "Pangolin", exchangeAddress: "0x25a0A78f5ad07b2474D3D42F1c1432178465936d", exchangeType: "UniswapV2Exchange", pathfinderKey: paraswap_1.Paraswap.OnlyDex.Pangolin }),
68
- QuickSwap: Object.assign(Object.assign({}, exports.ChainConfigs.poly), { partner: "QuickSwap", exchangeAddress: "0xeFE1B6096838949156e5130604434A2a13c68C68", exchangeType: "UniswapV2Exchange", pathfinderKey: paraswap_1.Paraswap.OnlyDex.QuickSwap }),
66
+ SpookySwap: Object.assign(Object.assign({}, exports.ChainConfigs.ftm), { partner: "SpookySwap", exchangeAddress: "0x4b5815D263549Ff9b54a3838693C3DC0dbE7e597", exchangeType: "UniswapV2Exchange", pathfinderKey: paraswap_1.Paraswap.OnlyDex.SpookySwap }),
67
+ Pangolin: Object.assign(Object.assign({}, exports.ChainConfigs.avax), { partner: "Pangolin", exchangeAddress: "0x72a18A408e329E7052d08aA0746243Dc30Ad2530", exchangeType: "UniswapV2Exchange", pathfinderKey: paraswap_1.Paraswap.OnlyDex.Pangolin }),
68
+ QuickSwap: Object.assign(Object.assign({}, exports.ChainConfigs.poly), { partner: "QuickSwap", exchangeAddress: "0x9369D5465d6D90AA3c06Af5B683e617EeA9078E4", exchangeType: "UniswapV2Exchange", pathfinderKey: paraswap_1.Paraswap.OnlyDex.QuickSwap }),
69
69
  };
70
- // export const UniswapV2Config: Config = {
71
- // ...ChainConfigs.eth,
72
- // partner: "UniswapV2",
73
- // exchangeAddress: "0xE83df5BfA9F14a84e550c38c4ec505cB22C6A0d7",
74
- // };
75
- // export const SushiSwapConfig: Config = {
76
- // ...ChainConfigs.eth,
77
- // partner: "SushiSwap",
78
- // exchangeAddress: "0x72a18A408e329E7052d08aA0746243Dc30Ad2530",
79
- // };
80
70
  exports.nativeTokenAddresses = [
81
71
  web3_candies_1.zeroAddress,
82
72
  "0x0000000000000000000000000000000000001010",
package/dist/src/lib.d.ts CHANGED
@@ -39,12 +39,12 @@ export declare class TWAPLib {
39
39
  getAllOrders(): Promise<Order[]>;
40
40
  parseOrder(r: any): Order;
41
41
  getToken(address: string): Promise<TokenData>;
42
- getSwapData(orderId: number): Promise<{
42
+ findSwapDataForBid(orderId: number): Promise<{
43
43
  srcToken: TokenData;
44
44
  dstToken: TokenData;
45
45
  srcAmountIn: BN;
46
46
  dstAmountOut: BN;
47
- path: any;
47
+ raw: any;
48
48
  data: string;
49
49
  }>;
50
50
  }
package/dist/src/lib.js CHANGED
@@ -203,31 +203,32 @@ class TWAPLib {
203
203
  }
204
204
  getToken(address) {
205
205
  return __awaiter(this, void 0, void 0, function* () {
206
- if ((0, configs_1.isNativeAddress)(address))
206
+ if ((0, configs_1.isNativeAddress)(address) || (0, web3_candies_1.eqIgnoreCase)(address, this.config.wToken.address))
207
207
  return this.config.wToken;
208
208
  const t = (0, web3_candies_1.erc20)("", address);
209
209
  return { address, decimals: yield t.decimals(), symbol: yield t.methods.symbol().call() };
210
210
  });
211
211
  }
212
- //TODO
213
- getSwapData(orderId) {
212
+ findSwapDataForBid(orderId) {
214
213
  return __awaiter(this, void 0, void 0, function* () {
215
214
  const order = yield this.getOrder(orderId);
216
- const srcAmountIn = order.ask.srcBidAmount;
217
- const srcToken = yield this.getToken(order.ask.srcToken);
218
- const dstToken = yield this.getToken(order.ask.dstToken);
219
- const paraswapRoute = yield paraswap_1.Paraswap.findRoute(this.config.chainId, srcToken, dstToken, srcAmountIn, this.config.pathfinderKey);
220
- const dstAmountOut = (0, bignumber_js_1.default)(paraswapRoute.destAmount);
221
- const path = paraswap_1.Paraswap.directPath(paraswapRoute, this.config.pathfinderKey);
215
+ const srcAmountIn = bignumber_js_1.default.min(order.ask.srcBidAmount, order.ask.srcAmount.minus(order.srcFilledAmount));
216
+ const [srcToken, dstToken] = yield Promise.all([
217
+ this.getToken(order.ask.srcToken),
218
+ this.getToken(order.ask.dstToken),
219
+ ]);
220
+ const route = yield paraswap_1.Paraswap.findRoute(this.config.chainId, srcToken, dstToken, srcAmountIn, this.config.pathfinderKey);
221
+ const dstAmountOut = (0, bignumber_js_1.default)(route.destAmount);
222
222
  switch (this.config.exchangeType) {
223
223
  case "UniswapV2Exchange":
224
+ const path = paraswap_1.Paraswap.getDirectPath(route, this.config.pathfinderKey);
224
225
  return {
225
226
  srcToken,
226
227
  dstToken,
227
228
  srcAmountIn,
228
229
  dstAmountOut,
229
- path,
230
- data: (0, web3_candies_1.web3)().eth.abi.encodeParameters(["bool", "address[]"], [false, path]),
230
+ raw: path,
231
+ data: (0, web3_candies_1.web3)().eth.abi.encodeParameters(["bool", "address[]"], [true, path]),
231
232
  };
232
233
  case "ParaswapExchange":
233
234
  return {
@@ -235,8 +236,8 @@ class TWAPLib {
235
236
  dstToken,
236
237
  srcAmountIn,
237
238
  dstAmountOut,
238
- path,
239
- data: yield paraswap_1.Paraswap.buildSwapData(paraswapRoute, this.config.twapAddress),
239
+ raw: route,
240
+ data: yield paraswap_1.Paraswap.buildSwapData(route, this.config.twapAddress),
240
241
  };
241
242
  default:
242
243
  throw new Error(`unhandled exchange ${this.config.exchangeType}`);
@@ -7,7 +7,7 @@ export declare namespace Paraswap {
7
7
  QuickSwap = "QuickSwap",
8
8
  SpiritSwap = "SpiritSwap,SpiritSwapV2",
9
9
  SpookySwap = "SpookySwap",
10
- Pangolin = "PangolinDEX",
10
+ Pangolin = "PangolinSwap",
11
11
  TraderJoe = "TraderJoe"
12
12
  }
13
13
  interface ParaswapRoute {
@@ -39,7 +39,7 @@ export declare namespace Paraswap {
39
39
  instant: BN;
40
40
  }>;
41
41
  function priceUsd(chainId: number, token: TokenData): Promise<BN>;
42
- function findRoute(chainId: number, src: TokenData, dst: TokenData, amountIn: BN.Value, onlyDex?: OnlyDex): Promise<ParaswapRoute>;
43
- function directPath(route: ParaswapRoute, onlyDex: OnlyDex): any;
42
+ function findRoute(chainId: number, src: TokenData, dst: TokenData, amountIn: BN.Value, onlyDex?: OnlyDex, otherExchanges?: boolean): Promise<ParaswapRoute>;
43
+ function getDirectPath(route: ParaswapRoute, onlyDex: OnlyDex): string[];
44
44
  function buildSwapData(paraswapRoute: ParaswapRoute, exchangeAdapter: string): Promise<string>;
45
45
  }
@@ -16,6 +16,7 @@ exports.Paraswap = void 0;
16
16
  const bignumber_js_1 = __importDefault(require("bignumber.js"));
17
17
  const web3_candies_1 = require("@defi.org/web3-candies");
18
18
  const configs_1 = require("./configs");
19
+ const lodash_1 = __importDefault(require("lodash"));
19
20
  var Paraswap;
20
21
  (function (Paraswap) {
21
22
  const URL = "https://apiv5.paraswap.io";
@@ -26,7 +27,7 @@ var Paraswap;
26
27
  OnlyDex["QuickSwap"] = "QuickSwap";
27
28
  OnlyDex["SpiritSwap"] = "SpiritSwap,SpiritSwapV2";
28
29
  OnlyDex["SpookySwap"] = "SpookySwap";
29
- OnlyDex["Pangolin"] = "PangolinDEX";
30
+ OnlyDex["Pangolin"] = "PangolinSwap";
30
31
  OnlyDex["TraderJoe"] = "TraderJoe";
31
32
  })(OnlyDex = Paraswap.OnlyDex || (Paraswap.OnlyDex = {}));
32
33
  function gasPrices(chainId) {
@@ -52,7 +53,7 @@ var Paraswap;
52
53
  });
53
54
  }
54
55
  Paraswap.priceUsd = priceUsd;
55
- function findRoute(chainId, src, dst, amountIn, onlyDex) {
56
+ function findRoute(chainId, src, dst, amountIn, onlyDex, otherExchanges = false) {
56
57
  var _a, _b;
57
58
  return __awaiter(this, void 0, void 0, function* () {
58
59
  const params = new URLSearchParams({
@@ -65,19 +66,31 @@ var Paraswap;
65
66
  side: "SELL",
66
67
  includeDEXS: onlyDex || "",
67
68
  partner: ((_b = (_a = onlyDex === null || onlyDex === void 0 ? void 0 : onlyDex.toLowerCase()) === null || _a === void 0 ? void 0 : _a.split(",")) === null || _b === void 0 ? void 0 : _b[0]) || "",
68
- // otherExchangePrices: "true",
69
+ otherExchangePrices: otherExchanges.toString(),
69
70
  });
70
71
  const response = yield fetch(`${URL}/prices/?${params}`);
71
72
  if (response.status < 200 || response.status >= 400)
72
73
  throw new Error(`${response.statusText}`);
73
- return (yield response.json()).priceRoute; //TODO pangolin onlyDex not working?
74
+ return (yield response.json()).priceRoute;
74
75
  });
75
76
  }
76
77
  Paraswap.findRoute = findRoute;
77
- function directPath(route, onlyDex) {
78
- return route.bestRoute[0].swaps[0].swapExchanges[0].data.path; // TODO find?
78
+ function getDirectPath(route, onlyDex) {
79
+ const bestRoute = lodash_1.default.sortBy(route.bestRoute, (r) => r.percent).reverse()[0];
80
+ if (bestRoute.swaps.length > 1)
81
+ throw new Error(`invalid direct path more than 1 path`);
82
+ if (!(0, web3_candies_1.eqIgnoreCase)(bestRoute.swaps[0].srcToken, route.srcToken) ||
83
+ !(0, web3_candies_1.eqIgnoreCase)(bestRoute.swaps[0].destToken, route.destToken))
84
+ throw new Error(`invalid direct path tokens`);
85
+ const bestSwap = lodash_1.default.sortBy(bestRoute.swaps[0].swapExchanges, (s) => s.percent).reverse()[0];
86
+ if (!onlyDex.split(",").includes(bestSwap.exchange))
87
+ throw new Error(`invalid direct path exchange`);
88
+ const path = bestSwap.data.path;
89
+ if (!path || path.length < 2)
90
+ throw new Error(`invalid direct path`);
91
+ return path;
79
92
  }
80
- Paraswap.directPath = directPath;
93
+ Paraswap.getDirectPath = getDirectPath;
81
94
  function buildSwapData(paraswapRoute, exchangeAdapter) {
82
95
  return __awaiter(this, void 0, void 0, function* () {
83
96
  const response = yield fetch(`${URL}/transactions/${paraswapRoute.network}?ignoreChecks=true`, {
@@ -13,7 +13,7 @@ export interface UniswapV2Exchange extends BaseContract {
13
13
  constructor(jsonInterface: any[], address?: string, options?: ContractOptions): UniswapV2Exchange;
14
14
  clone(): UniswapV2Exchange;
15
15
  methods: {
16
- getAmountOut(arg0: string, arg1: string, amountIn: number | string | BigNumber, data: string | number[]): NonPayableTransactionObject<string>;
16
+ getAmountOut(srcToken: string, dstToken: string, amountIn: number | string | BigNumber, data: string | number[]): NonPayableTransactionObject<string>;
17
17
  swap(_srcToken: string, arg1: string, amountIn: number | string | BigNumber, amountOutMin: number | string | BigNumber, data: string | number[]): NonPayableTransactionObject<void>;
18
18
  uniswap(): NonPayableTransactionObject<string>;
19
19
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@orbs-network/twap",
3
- "version": "1.7.14",
3
+ "version": "1.8.0",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/orbs-network/twap.git"