@curvefi/api 1.25.2 → 2.0.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.
Files changed (213) hide show
  1. package/README.md +775 -589
  2. package/docs/v1/README.md +916 -0
  3. package/lib/boosting.d.ts +12 -12
  4. package/lib/boosting.js +23 -23
  5. package/lib/constants/abis/{json/2pool → 2pool}/swap.json +0 -0
  6. package/lib/constants/abis/{json/3pool → 3pool}/swap.json +0 -0
  7. package/lib/constants/abis/{json/4pool → 4pool}/swap.json +0 -0
  8. package/lib/constants/abis/{json/ERC20.json → ERC20.json} +0 -0
  9. package/lib/constants/abis/{json/aave → aave}/rewards.json +0 -0
  10. package/lib/constants/abis/{json/aave → aave}/swap.json +0 -0
  11. package/lib/constants/abis/{json/address_provider.json → address_provider.json} +0 -0
  12. package/lib/constants/abis/{json/ankreth → ankreth}/sCurveRewards.json +0 -0
  13. package/lib/constants/abis/{json/ankreth → ankreth}/swap.json +0 -0
  14. package/lib/constants/abis/{json/atricrypto3 → atricrypto3}/swap.json +0 -0
  15. package/lib/constants/abis/{json/atricrypto3 → atricrypto3}/zap.json +0 -0
  16. package/lib/constants/abis/{json/bbtc → bbtc}/deposit.json +0 -0
  17. package/lib/constants/abis/{json/bbtc → bbtc}/swap.json +0 -0
  18. package/lib/constants/abis/{json/busd → busd}/deposit.json +0 -0
  19. package/lib/constants/abis/{json/busd → busd}/swap.json +0 -0
  20. package/lib/constants/abis/{json/cERC20.json → cERC20.json} +0 -0
  21. package/lib/constants/abis/{json/compound → compound}/deposit.json +0 -0
  22. package/lib/constants/abis/{json/compound → compound}/swap.json +0 -0
  23. package/lib/constants/abis/{json/crveth → crveth}/swap.json +0 -0
  24. package/lib/constants/abis/{json/deposit_and_stake.json → deposit_and_stake.json} +0 -0
  25. package/lib/constants/abis/{json/dusd → dusd}/deposit.json +0 -0
  26. package/lib/constants/abis/{json/dusd → dusd}/sCurveRewards.json +0 -0
  27. package/lib/constants/abis/{json/dusd → dusd}/swap.json +0 -0
  28. package/lib/constants/abis/{json/eurs → eurs}/sCurveRewards.json +0 -0
  29. package/lib/constants/abis/{json/eurs → eurs}/swap.json +0 -0
  30. package/lib/constants/abis/{json/eursusd → eursusd}/swap.json +0 -0
  31. package/lib/constants/abis/{json/eurt → eurt}/swap.json +0 -0
  32. package/lib/constants/abis/{json/eurtusd → eurtusd}/deposit.json +0 -0
  33. package/lib/constants/abis/{json/eurtusd → eurtusd}/swap.json +0 -0
  34. package/lib/constants/abis/{json/factory-crypto → factory-crypto}/factory-crypto-pool-2.json +0 -0
  35. package/lib/constants/abis/{json/factory-crypto.json → factory-crypto.json} +0 -0
  36. package/lib/constants/abis/{json/factory-v2 → factory-v2}/DepositZapMetaBtcPolygon.json +0 -0
  37. package/lib/constants/abis/{json/factory-v2 → factory-v2}/DepositZapMetaUsdPolygon.json +0 -0
  38. package/lib/constants/abis/{json/factory-v2 → factory-v2}/MetaBTC.json +0 -0
  39. package/lib/constants/abis/{json/factory-v2 → factory-v2}/MetaBTCBalances.json +0 -0
  40. package/lib/constants/abis/{json/factory-v2 → factory-v2}/MetaBTCBalancesRen.json +0 -0
  41. package/lib/constants/abis/{json/factory-v2 → factory-v2}/MetaBTCRen.json +0 -0
  42. package/lib/constants/abis/{json/factory-v2 → factory-v2}/MetaUSD.json +0 -0
  43. package/lib/constants/abis/{json/factory-v2 → factory-v2}/MetaUSDBalances.json +0 -0
  44. package/lib/constants/abis/{json/factory-v2 → factory-v2}/Plain2Balances.json +0 -0
  45. package/lib/constants/abis/{json/factory-v2 → factory-v2}/Plain2Basic.json +0 -0
  46. package/lib/constants/abis/{json/factory-v2 → factory-v2}/Plain2ETH.json +0 -0
  47. package/lib/constants/abis/{json/factory-v2 → factory-v2}/Plain2Optimized.json +0 -0
  48. package/lib/constants/abis/{json/factory-v2 → factory-v2}/Plain3Balances.json +0 -0
  49. package/lib/constants/abis/{json/factory-v2 → factory-v2}/Plain3Basic.json +0 -0
  50. package/lib/constants/abis/{json/factory-v2 → factory-v2}/Plain3ETH.json +0 -0
  51. package/lib/constants/abis/{json/factory-v2 → factory-v2}/Plain3Optimized.json +0 -0
  52. package/lib/constants/abis/{json/factory-v2 → factory-v2}/Plain4Balances.json +0 -0
  53. package/lib/constants/abis/{json/factory-v2 → factory-v2}/Plain4Basic.json +0 -0
  54. package/lib/constants/abis/{json/factory-v2 → factory-v2}/Plain4ETH.json +0 -0
  55. package/lib/constants/abis/{json/factory-v2 → factory-v2}/Plain4Optimized.json +0 -0
  56. package/lib/constants/abis/{json/factory.json → factory.json} +0 -0
  57. package/lib/constants/abis/{json/factoryPools → factoryPools}/deposit.json +0 -0
  58. package/lib/constants/abis/{json/factoryPools → factoryPools}/rewards.json +0 -0
  59. package/lib/constants/abis/{json/factoryPools → factoryPools}/swap.json +0 -0
  60. package/lib/constants/abis/{json/gauge.json → gauge.json} +0 -0
  61. package/lib/constants/abis/{json/gauge_factory.json → gauge_factory.json} +0 -0
  62. package/lib/constants/abis/{json/gauge_rewards_only.json → gauge_rewards_only.json} +0 -0
  63. package/lib/constants/abis/{json/gauge_synthetix.json → gauge_synthetix.json} +0 -0
  64. package/lib/constants/abis/{json/gauge_v2.json → gauge_v2.json} +0 -0
  65. package/lib/constants/abis/{json/gauge_v3.json → gauge_v3.json} +0 -0
  66. package/lib/constants/abis/{json/gauge_v4.json → gauge_v4.json} +0 -0
  67. package/lib/constants/abis/{json/gauge_v5.json → gauge_v5.json} +0 -0
  68. package/lib/constants/abis/{json/gaugecontroller.json → gaugecontroller.json} +0 -0
  69. package/lib/constants/abis/{json/gusd → gusd}/deposit.json +0 -0
  70. package/lib/constants/abis/{json/gusd → gusd}/swap.json +0 -0
  71. package/lib/constants/abis/{json/hbtc → hbtc}/swap.json +0 -0
  72. package/lib/constants/abis/{json/husd → husd}/deposit.json +0 -0
  73. package/lib/constants/abis/{json/husd → husd}/swap.json +0 -0
  74. package/lib/constants/abis/{json/ib → ib}/swap.json +0 -0
  75. package/lib/constants/abis/{json/iearn → iearn}/deposit.json +0 -0
  76. package/lib/constants/abis/{json/iearn → iearn}/sCurveRewards.json +0 -0
  77. package/lib/constants/abis/{json/iearn → iearn}/swap.json +0 -0
  78. package/lib/constants/abis/{json/link → link}/swap.json +0 -0
  79. package/lib/constants/abis/{json/minter.json → minter.json} +0 -0
  80. package/lib/constants/abis/{json/musd → musd}/deposit.json +0 -0
  81. package/lib/constants/abis/{json/musd → musd}/sCurveRewards.json +0 -0
  82. package/lib/constants/abis/{json/musd → musd}/swap.json +0 -0
  83. package/lib/constants/abis/{json/obtc → obtc}/deposit.json +0 -0
  84. package/lib/constants/abis/{json/obtc → obtc}/sCurveRewards.json +0 -0
  85. package/lib/constants/abis/{json/obtc → obtc}/swap.json +0 -0
  86. package/lib/constants/abis/{json/paave → paave}/rewards.json +0 -0
  87. package/lib/constants/abis/{json/pax → pax}/deposit.json +0 -0
  88. package/lib/constants/abis/{json/pax → pax}/swap.json +0 -0
  89. package/lib/constants/abis/{json/pbtc → pbtc}/deposit.json +0 -0
  90. package/lib/constants/abis/{json/pbtc → pbtc}/sCurveRewards.json +0 -0
  91. package/lib/constants/abis/{json/pbtc → pbtc}/swap.json +0 -0
  92. package/lib/constants/abis/{json/rai → rai}/deposit.json +0 -0
  93. package/lib/constants/abis/{json/rai → rai}/swap.json +0 -0
  94. package/lib/constants/abis/{json/registry_exchange.json → registry_exchange.json} +0 -0
  95. package/lib/constants/abis/{json/ren → ren}/swap.json +0 -0
  96. package/lib/constants/abis/{json/ren-polygon → ren-polygon}/swap.json +0 -0
  97. package/lib/constants/abis/{json/reth → reth}/swap.json +0 -0
  98. package/lib/constants/abis/{json/router.json → router.json} +0 -0
  99. package/lib/constants/abis/{json/rsv → rsv}/deposit.json +0 -0
  100. package/lib/constants/abis/{json/rsv → rsv}/sCurveRewards.json +0 -0
  101. package/lib/constants/abis/{json/rsv → rsv}/swap.json +0 -0
  102. package/lib/constants/abis/{json/saave → saave}/swap.json +0 -0
  103. package/lib/constants/abis/{json/sbtc → sbtc}/sCurveRewards.json +0 -0
  104. package/lib/constants/abis/{json/sbtc → sbtc}/swap.json +0 -0
  105. package/lib/constants/abis/{json/seth → seth}/swap.json +0 -0
  106. package/lib/constants/abis/{json/steth → steth}/sCurveRewards.json +0 -0
  107. package/lib/constants/abis/{json/steth → steth}/swap.json +0 -0
  108. package/lib/constants/abis/{json/streamer.json → streamer.json} +0 -0
  109. package/lib/constants/abis/{json/susdv2 → susdv2}/deposit.json +0 -0
  110. package/lib/constants/abis/{json/susdv2 → susdv2}/sCurveRewards.json +0 -0
  111. package/lib/constants/abis/{json/susdv2 → susdv2}/swap.json +0 -0
  112. package/lib/constants/abis/{json/tbtc → tbtc}/deposit.json +0 -0
  113. package/lib/constants/abis/{json/tbtc → tbtc}/sCurveRewards.json +0 -0
  114. package/lib/constants/abis/{json/tbtc → tbtc}/swap.json +0 -0
  115. package/lib/constants/abis/{json/tricrypto2 → tricrypto2}/deposit.json +0 -0
  116. package/lib/constants/abis/{json/tricrypto2 → tricrypto2}/swap.json +0 -0
  117. package/lib/constants/abis/{json/usdk → usdk}/deposit.json +0 -0
  118. package/lib/constants/abis/{json/usdk → usdk}/swap.json +0 -0
  119. package/lib/constants/abis/{json/usdn → usdn}/deposit.json +0 -0
  120. package/lib/constants/abis/{json/usdn → usdn}/swap.json +0 -0
  121. package/lib/constants/abis/{json/usdp → usdp}/deposit.json +0 -0
  122. package/lib/constants/abis/{json/usdp → usdp}/swap.json +0 -0
  123. package/lib/constants/abis/{json/usdt → usdt}/deposit.json +0 -0
  124. package/lib/constants/abis/{json/usdt → usdt}/swap.json +0 -0
  125. package/lib/constants/abis/{json/ust → ust}/deposit.json +0 -0
  126. package/lib/constants/abis/{json/ust → ust}/swap.json +0 -0
  127. package/lib/constants/abis/{json/votingescrow.json → votingescrow.json} +0 -0
  128. package/lib/constants/abis/{json/yERC20.json → yERC20.json} +0 -0
  129. package/lib/constants/aliases.d.ts +2 -24
  130. package/lib/constants/aliases.js +5 -4
  131. package/lib/constants/{coins-ethereum.d.ts → coins/ethereum.d.ts} +0 -9
  132. package/lib/constants/{coins-ethereum.js → coins/ethereum.js} +12 -23
  133. package/lib/constants/{coins-polygon.d.ts → coins/polygon.d.ts} +0 -12
  134. package/lib/constants/{coins-polygon.js → coins/polygon.js} +8 -20
  135. package/lib/constants/pools/ethereum.d.ts +2 -0
  136. package/lib/constants/{abis/abis-ethereum.js → pools/ethereum.js} +517 -1012
  137. package/lib/constants/pools/index.d.ts +3 -0
  138. package/lib/constants/pools/index.js +7 -0
  139. package/lib/constants/pools/polygon.d.ts +4 -0
  140. package/lib/constants/{abis/abis-polygon.js → pools/polygon.js} +52 -80
  141. package/lib/constants/utils.d.ts +5 -0
  142. package/lib/constants/utils.js +69 -0
  143. package/lib/curve.d.ts +10 -32
  144. package/lib/curve.js +188 -232
  145. package/lib/external-api.d.ts +3 -2
  146. package/lib/external-api.js +18 -1
  147. package/lib/factory/constants.d.ts +15 -15
  148. package/lib/factory/constants.js +19 -19
  149. package/lib/factory/factory-api.d.ts +2 -2
  150. package/lib/factory/factory-api.js +34 -49
  151. package/lib/factory/factory-crypto.d.ts +2 -2
  152. package/lib/factory/factory-crypto.js +23 -31
  153. package/lib/factory/factory.d.ts +2 -2
  154. package/lib/factory/factory.js +53 -66
  155. package/lib/index.d.ts +36 -53
  156. package/lib/index.js +27 -35
  157. package/lib/interfaces.d.ts +25 -67
  158. package/lib/pools/PoolTemplate.d.ts +210 -0
  159. package/lib/pools/PoolTemplate.js +2292 -0
  160. package/lib/pools/index.d.ts +3 -0
  161. package/lib/pools/index.js +7 -0
  162. package/lib/pools/mixins/common.d.ts +3 -0
  163. package/lib/pools/mixins/common.js +77 -0
  164. package/lib/pools/mixins/depositBalancedAmountsMixins.d.ts +5 -0
  165. package/lib/pools/mixins/depositBalancedAmountsMixins.js +145 -0
  166. package/lib/pools/mixins/depositBonusMixins.d.ts +5 -0
  167. package/lib/pools/mixins/depositBonusMixins.js +164 -0
  168. package/lib/pools/mixins/depositMixins.d.ts +5 -0
  169. package/lib/pools/mixins/depositMixins.js +373 -0
  170. package/lib/pools/mixins/depositWrappedMixins.d.ts +3 -0
  171. package/lib/pools/mixins/depositWrappedMixins.js +243 -0
  172. package/lib/pools/mixins/poolBalancesMixin.d.ts +4 -0
  173. package/lib/pools/mixins/poolBalancesMixin.js +129 -0
  174. package/lib/pools/mixins/swapMixins.d.ts +4 -0
  175. package/lib/pools/mixins/swapMixins.js +313 -0
  176. package/lib/pools/mixins/swapWrappedMixins.d.ts +4 -0
  177. package/lib/pools/mixins/swapWrappedMixins.js +297 -0
  178. package/lib/pools/mixins/withdrawExpectedMixins.d.ts +6 -0
  179. package/lib/pools/mixins/withdrawExpectedMixins.js +164 -0
  180. package/lib/pools/mixins/withdrawImbalanceMixins.d.ts +5 -0
  181. package/lib/pools/mixins/withdrawImbalanceMixins.js +348 -0
  182. package/lib/pools/mixins/withdrawImbalanceWrappedMixins.d.ts +3 -0
  183. package/lib/pools/mixins/withdrawImbalanceWrappedMixins.js +207 -0
  184. package/lib/pools/mixins/withdrawMixins.d.ts +5 -0
  185. package/lib/pools/mixins/withdrawMixins.js +347 -0
  186. package/lib/pools/mixins/withdrawOneCoinExpectedMixins.d.ts +5 -0
  187. package/lib/pools/mixins/withdrawOneCoinExpectedMixins.js +104 -0
  188. package/lib/pools/mixins/withdrawOneCoinMixins.d.ts +5 -0
  189. package/lib/pools/mixins/withdrawOneCoinMixins.js +347 -0
  190. package/lib/pools/mixins/withdrawOneCoinWrappedExpectedMixins.d.ts +3 -0
  191. package/lib/pools/mixins/withdrawOneCoinWrappedExpectedMixins.js +72 -0
  192. package/lib/pools/mixins/withdrawOneCoinWrappedMixins.d.ts +3 -0
  193. package/lib/pools/mixins/withdrawOneCoinWrappedMixins.js +205 -0
  194. package/lib/pools/mixins/withdrawWrappedMixins.d.ts +3 -0
  195. package/lib/pools/mixins/withdrawWrappedMixins.js +206 -0
  196. package/lib/pools/poolConstructor.d.ts +2 -0
  197. package/lib/pools/poolConstructor.js +228 -0
  198. package/lib/pools/utils.d.ts +4 -0
  199. package/lib/pools/utils.js +85 -0
  200. package/lib/router.d.ts +12 -0
  201. package/lib/router.js +624 -0
  202. package/lib/utils.d.ts +10 -16
  203. package/lib/utils.js +33 -126
  204. package/package.json +1 -1
  205. package/lib/constants/abis/abis-ethereum.d.ts +0 -4
  206. package/lib/constants/abis/abis-polygon.d.ts +0 -4
  207. package/lib/constants/abis/json/aRewards.json +0 -1
  208. package/lib/constants/abis/json/compound/migration.json +0 -57
  209. package/lib/constants/abis/json/compound/oldSwap.json +0 -688
  210. package/lib/constants/abis/json/ren/adapter.json +0 -1
  211. package/lib/constants/abis/json/sbtc/adapter.json +0 -1
  212. package/lib/pools.d.ts +0 -267
  213. package/lib/pools.js +0 -4752
package/lib/router.js ADDED
@@ -0,0 +1,624 @@
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
15
+ return new (P || (P = Promise))(function (resolve, reject) {
16
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
17
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
18
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
19
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
20
+ });
21
+ };
22
+ var __generator = (this && this.__generator) || function (thisArg, body) {
23
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
24
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
25
+ function verb(n) { return function (v) { return step([n, v]); }; }
26
+ function step(op) {
27
+ if (f) throw new TypeError("Generator is already executing.");
28
+ while (_) try {
29
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
30
+ if (y = 0, t) op = [op[0] & 2, t.value];
31
+ switch (op[0]) {
32
+ case 0: case 1: t = op; break;
33
+ case 4: _.label++; return { value: op[1], done: false };
34
+ case 5: _.label++; y = op[1]; op = [0]; continue;
35
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
36
+ default:
37
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
38
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
39
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
40
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
41
+ if (t[2]) _.ops.pop();
42
+ _.trys.pop(); continue;
43
+ }
44
+ op = body.call(thisArg, _);
45
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
46
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
47
+ }
48
+ };
49
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
50
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
51
+ if (ar || !(i in from)) {
52
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
53
+ ar[i] = from[i];
54
+ }
55
+ }
56
+ return to.concat(ar || Array.prototype.slice.call(from));
57
+ };
58
+ var __importDefault = (this && this.__importDefault) || function (mod) {
59
+ return (mod && mod.__esModule) ? mod : { "default": mod };
60
+ };
61
+ Object.defineProperty(exports, "__esModule", { value: true });
62
+ exports.swap = exports.swapEstimateGas = exports.swapApprove = exports.swapApproveEstimateGas = exports.swapIsApproved = exports.swapExpected = exports.getBestRouteAndOutput = exports._findAllRoutes = void 0;
63
+ var curve_1 = require("./curve");
64
+ var ethers_1 = require("ethers");
65
+ var utils_1 = require("./utils");
66
+ var memoizee_1 = __importDefault(require("memoizee"));
67
+ var axios_1 = __importDefault(require("axios"));
68
+ var pools_1 = require("./pools");
69
+ var IMBALANCED_POOLS = [];
70
+ // Inspired by Dijkstra's algorithm
71
+ var _findAllRoutes = function (inputCoinAddress, outputCoinAddress) { return __awaiter(void 0, void 0, void 0, function () {
72
+ var ALL_POOLS, basePoolsSet, _i, ALL_POOLS_1, pool, basePoolIds, markedCoins, curCoins, nextCoins, routes, step, _a, curCoins_1, inCoin, _b, ALL_POOLS_2, _c, poolId, poolData, wrapped_coin_addresses, underlying_coin_addresses, meta_coin_addresses, token_address, is_lending, inCoinIndexes, j, swapType, _d, _e, inCoinRoute, swapType, _f, _h, inCoinRoute, j, tvl, _j, swapType, _k, _l, inCoinRoute, poolAddress, j, tvl, _m, swapType, _o, _p, inCoinRoute, j, tvl, _q, swapType, _r, _s, inCoinRoute, tvl, _t, swapType, _u, _v, inCoinRoute;
73
+ var _w;
74
+ var _x, _y, _z, _0, _1, _2, _3, _4;
75
+ return __generator(this, function (_5) {
76
+ switch (_5.label) {
77
+ case 0:
78
+ inputCoinAddress = inputCoinAddress.toLowerCase();
79
+ outputCoinAddress = outputCoinAddress.toLowerCase();
80
+ ALL_POOLS = Object.entries(__assign(__assign(__assign({}, curve_1.curve.constants.POOLS_DATA), curve_1.curve.constants.FACTORY_POOLS_DATA), curve_1.curve.constants.CRYPTO_FACTORY_POOLS_DATA));
81
+ basePoolsSet = new Set();
82
+ for (_i = 0, ALL_POOLS_1 = ALL_POOLS; _i < ALL_POOLS_1.length; _i++) {
83
+ pool = ALL_POOLS_1[_i];
84
+ if (pool[1].base_pool)
85
+ basePoolsSet.add(pool[1].base_pool);
86
+ }
87
+ basePoolIds = Array.from(basePoolsSet);
88
+ markedCoins = [];
89
+ curCoins = [inputCoinAddress];
90
+ nextCoins = new Set();
91
+ routes = (_w = {},
92
+ _w[inputCoinAddress] = [[]],
93
+ _w);
94
+ step = 0;
95
+ _5.label = 1;
96
+ case 1:
97
+ if (!(step < 4)) return [3 /*break*/, 21];
98
+ _a = 0, curCoins_1 = curCoins;
99
+ _5.label = 2;
100
+ case 2:
101
+ if (!(_a < curCoins_1.length)) return [3 /*break*/, 19];
102
+ inCoin = curCoins_1[_a];
103
+ _b = 0, ALL_POOLS_2 = ALL_POOLS;
104
+ _5.label = 3;
105
+ case 3:
106
+ if (!(_b < ALL_POOLS_2.length)) return [3 /*break*/, 18];
107
+ _c = ALL_POOLS_2[_b], poolId = _c[0], poolData = _c[1];
108
+ wrapped_coin_addresses = poolData.wrapped_coin_addresses.map(function (a) { return a.toLowerCase(); });
109
+ underlying_coin_addresses = poolData.underlying_coin_addresses.map(function (a) { return a.toLowerCase(); });
110
+ meta_coin_addresses = poolData.is_meta ?
111
+ curve_1.curve.constants.POOLS_DATA[poolData.base_pool].underlying_coin_addresses.map(function (a) { return a.toLowerCase(); }) : [];
112
+ token_address = poolData.token_address.toLowerCase();
113
+ is_lending = (_x = poolData.is_lending) !== null && _x !== void 0 ? _x : false;
114
+ inCoinIndexes = {
115
+ wrapped_coin: wrapped_coin_addresses.indexOf(inCoin),
116
+ underlying_coin: underlying_coin_addresses.indexOf(inCoin),
117
+ meta_coin: meta_coin_addresses ? meta_coin_addresses.indexOf(inCoin) : -1,
118
+ };
119
+ // Find all LP -> underlying coin "swaps" (actually remove_liquidity_one_coin)
120
+ if (basePoolIds.includes(poolId) && inCoin === token_address) {
121
+ for (j = 0; j < underlying_coin_addresses.length; j++) {
122
+ // If this coin already marked or will be marked on the current step, no need to consider it on the next step
123
+ if (markedCoins.includes(underlying_coin_addresses[j]) || curCoins.includes(underlying_coin_addresses[j]))
124
+ continue;
125
+ // Looking for outputCoinAddress only on the final step
126
+ if (step === 3 && underlying_coin_addresses[j] !== outputCoinAddress)
127
+ continue;
128
+ swapType = poolId === 'aave' ? 10 : 9;
129
+ for (_d = 0, _e = routes[inCoin]; _d < _e.length; _d++) {
130
+ inCoinRoute = _e[_d];
131
+ routes[underlying_coin_addresses[j]] = ((_y = routes[underlying_coin_addresses[j]]) !== null && _y !== void 0 ? _y : []).concat([__spreadArray(__spreadArray([], inCoinRoute, true), [
132
+ {
133
+ poolId: poolId,
134
+ poolAddress: poolData.swap_address,
135
+ outputCoinAddress: underlying_coin_addresses[j],
136
+ i: 0,
137
+ j: j,
138
+ swapType: swapType,
139
+ swapAddress: ethers_1.ethers.constants.AddressZero,
140
+ },
141
+ ], false)]);
142
+ }
143
+ nextCoins.add(underlying_coin_addresses[j]);
144
+ }
145
+ }
146
+ // Find all underlying coin -> LP "swaps" (actually add_liquidity)
147
+ if (basePoolIds.includes(poolId) && underlying_coin_addresses.includes(inCoin)) {
148
+ // If this coin already marked or will be marked on the current step, no need to consider it on the next step
149
+ if (markedCoins.includes(token_address) || curCoins.includes(token_address))
150
+ return [3 /*break*/, 17];
151
+ // Looking for outputCoinAddress only on the final step
152
+ if (step === 3 && token_address !== outputCoinAddress)
153
+ return [3 /*break*/, 17];
154
+ swapType = is_lending ? 8 : underlying_coin_addresses.length === 2 ? 6 : 7;
155
+ for (_f = 0, _h = routes[inCoin]; _f < _h.length; _f++) {
156
+ inCoinRoute = _h[_f];
157
+ routes[token_address] = ((_z = routes[token_address]) !== null && _z !== void 0 ? _z : []).concat([__spreadArray(__spreadArray([], inCoinRoute, true), [
158
+ {
159
+ poolId: poolId,
160
+ poolAddress: poolData.swap_address,
161
+ outputCoinAddress: token_address,
162
+ i: underlying_coin_addresses.indexOf(inCoin),
163
+ j: 0,
164
+ swapType: swapType,
165
+ swapAddress: ethers_1.ethers.constants.AddressZero,
166
+ },
167
+ ], false)]);
168
+ }
169
+ nextCoins.add(token_address);
170
+ }
171
+ // No input coin in this pool --> skip
172
+ if (inCoinIndexes.wrapped_coin === -1 && inCoinIndexes.underlying_coin === -1 && inCoinIndexes.meta_coin === -1)
173
+ return [3 /*break*/, 17];
174
+ if (!(inCoinIndexes.wrapped_coin >= 0 && poolId !== "atricrypto3")) return [3 /*break*/, 7];
175
+ j = 0;
176
+ _5.label = 4;
177
+ case 4:
178
+ if (!(j < wrapped_coin_addresses.length)) return [3 /*break*/, 7];
179
+ // If this coin already marked or will be marked on the current step, no need to consider it on the next step
180
+ if (markedCoins.includes(wrapped_coin_addresses[j]) || curCoins.includes(wrapped_coin_addresses[j]))
181
+ return [3 /*break*/, 6];
182
+ // Looking for outputCoinAddress only on the final step
183
+ if (step === 3 && wrapped_coin_addresses[j] !== outputCoinAddress)
184
+ return [3 /*break*/, 6];
185
+ _j = Number;
186
+ return [4 /*yield*/, ((0, pools_1.getPool)(poolId)).stats.totalLiquidity()];
187
+ case 5:
188
+ tvl = _j.apply(void 0, [_5.sent()]);
189
+ if (tvl === 0)
190
+ return [3 /*break*/, 6];
191
+ // Skip imbalanced pools
192
+ if (IMBALANCED_POOLS.includes(poolId))
193
+ return [3 /*break*/, 6];
194
+ swapType = poolData.is_crypto ? 3 : 1;
195
+ for (_k = 0, _l = routes[inCoin]; _k < _l.length; _k++) {
196
+ inCoinRoute = _l[_k];
197
+ routes[wrapped_coin_addresses[j]] = ((_0 = routes[wrapped_coin_addresses[j]]) !== null && _0 !== void 0 ? _0 : []).concat([__spreadArray(__spreadArray([], inCoinRoute, true), [
198
+ {
199
+ poolId: poolId,
200
+ poolAddress: poolData.swap_address,
201
+ outputCoinAddress: wrapped_coin_addresses[j],
202
+ i: inCoinIndexes.wrapped_coin,
203
+ j: j,
204
+ swapType: swapType,
205
+ swapAddress: ethers_1.ethers.constants.AddressZero,
206
+ },
207
+ ], false)]);
208
+ }
209
+ nextCoins.add(wrapped_coin_addresses[j]);
210
+ _5.label = 6;
211
+ case 6:
212
+ j++;
213
+ return [3 /*break*/, 4];
214
+ case 7:
215
+ poolAddress = ["eurtusd", "xautusd", "atricrypto3"].includes(poolId) ||
216
+ (curve_1.curve.chainId === 137 && poolData.is_factory) ? poolData.deposit_address : poolData.swap_address;
217
+ if (!(wrapped_coin_addresses.join("|") !== underlying_coin_addresses.join("|") && inCoinIndexes.underlying_coin >= 0)) return [3 /*break*/, 11];
218
+ j = 0;
219
+ _5.label = 8;
220
+ case 8:
221
+ if (!(j < underlying_coin_addresses.length)) return [3 /*break*/, 11];
222
+ if (poolId === "atricrypto3" && inCoinIndexes.meta_coin >= 0 && meta_coin_addresses.includes(underlying_coin_addresses[j]))
223
+ return [3 /*break*/, 10];
224
+ // If this coin already marked or will be marked on the current step, no need to consider it on the next step
225
+ if (markedCoins.includes(underlying_coin_addresses[j]) || curCoins.includes(underlying_coin_addresses[j]))
226
+ return [3 /*break*/, 10];
227
+ // Looking for outputCoinAddress only on the final step
228
+ if (step === 3 && underlying_coin_addresses[j] !== outputCoinAddress)
229
+ return [3 /*break*/, 10];
230
+ _m = Number;
231
+ return [4 /*yield*/, ((0, pools_1.getPool)(poolId)).stats.totalLiquidity()];
232
+ case 9:
233
+ tvl = _m.apply(void 0, [_5.sent()]);
234
+ if (tvl === 0)
235
+ return [3 /*break*/, 10];
236
+ // Skip imbalanced pools
237
+ if (IMBALANCED_POOLS.includes(poolId))
238
+ return [3 /*break*/, 10];
239
+ swapType = poolData.is_crypto && (poolData.is_fake || poolData.is_meta) ? 4 : poolData.is_crypto ? 3 : 2;
240
+ for (_o = 0, _p = routes[inCoin]; _o < _p.length; _o++) {
241
+ inCoinRoute = _p[_o];
242
+ routes[underlying_coin_addresses[j]] = ((_1 = routes[underlying_coin_addresses[j]]) !== null && _1 !== void 0 ? _1 : []).concat([__spreadArray(__spreadArray([], inCoinRoute, true), [
243
+ {
244
+ poolId: poolId,
245
+ poolAddress: poolAddress,
246
+ outputCoinAddress: underlying_coin_addresses[j],
247
+ i: inCoinIndexes.underlying_coin,
248
+ j: j,
249
+ swapType: swapType,
250
+ swapAddress: ethers_1.ethers.constants.AddressZero,
251
+ },
252
+ ], false)]);
253
+ }
254
+ nextCoins.add(underlying_coin_addresses[j]);
255
+ _5.label = 10;
256
+ case 10:
257
+ j++;
258
+ return [3 /*break*/, 8];
259
+ case 11:
260
+ if (!(inCoinIndexes.wrapped_coin === 0 && meta_coin_addresses.length > 0 && poolId !== "atricrypto3")) return [3 /*break*/, 15];
261
+ j = 0;
262
+ _5.label = 12;
263
+ case 12:
264
+ if (!(j < meta_coin_addresses.length)) return [3 /*break*/, 15];
265
+ // If this coin already marked or will be marked on the current step, no need to consider it on the next step
266
+ if (markedCoins.includes(meta_coin_addresses[j]) || curCoins.includes(meta_coin_addresses[j]))
267
+ return [3 /*break*/, 14];
268
+ // Looking for outputCoinAddress only on the final step
269
+ if (step === 3 && meta_coin_addresses[j] !== outputCoinAddress)
270
+ return [3 /*break*/, 14];
271
+ _q = Number;
272
+ return [4 /*yield*/, ((0, pools_1.getPool)(poolId)).stats.totalLiquidity()];
273
+ case 13:
274
+ tvl = _q.apply(void 0, [_5.sent()]);
275
+ if (tvl === 0)
276
+ return [3 /*break*/, 14];
277
+ // Skip imbalanced pools
278
+ if (IMBALANCED_POOLS.includes(poolId))
279
+ return [3 /*break*/, 14];
280
+ swapType = (curve_1.curve.chainId === 137 && poolData.is_factory) ? 5 : poolData.is_crypto ? 4 : 2;
281
+ for (_r = 0, _s = routes[inCoin]; _r < _s.length; _r++) {
282
+ inCoinRoute = _s[_r];
283
+ routes[meta_coin_addresses[j]] = ((_2 = routes[meta_coin_addresses[j]]) !== null && _2 !== void 0 ? _2 : []).concat([__spreadArray(__spreadArray([], inCoinRoute, true), [
284
+ {
285
+ poolId: poolId,
286
+ poolAddress: poolAddress,
287
+ outputCoinAddress: meta_coin_addresses[j],
288
+ i: inCoinIndexes.wrapped_coin,
289
+ j: j + 1,
290
+ swapType: swapType,
291
+ swapAddress: swapType === 5 ? poolData.swap_address : ethers_1.ethers.constants.AddressZero,
292
+ },
293
+ ], false)]);
294
+ }
295
+ nextCoins.add(meta_coin_addresses[j]);
296
+ _5.label = 14;
297
+ case 14:
298
+ j++;
299
+ return [3 /*break*/, 12];
300
+ case 15:
301
+ if (!(inCoinIndexes.meta_coin >= 0 && poolId !== "atricrypto3")) return [3 /*break*/, 17];
302
+ // If this coin already marked or will be marked on the current step, no need to consider it on the next step
303
+ if (markedCoins.includes(wrapped_coin_addresses[0]) || curCoins.includes(wrapped_coin_addresses[0]))
304
+ return [3 /*break*/, 17];
305
+ // Looking for outputCoinAddress only on the final step
306
+ if (step === 3 && wrapped_coin_addresses[0] !== outputCoinAddress)
307
+ return [3 /*break*/, 17];
308
+ _t = Number;
309
+ return [4 /*yield*/, ((0, pools_1.getPool)(poolId)).stats.totalLiquidity()];
310
+ case 16:
311
+ tvl = _t.apply(void 0, [_5.sent()]);
312
+ if (tvl === 0)
313
+ return [3 /*break*/, 17];
314
+ // Skip imbalanced pools
315
+ if (IMBALANCED_POOLS.includes(poolId))
316
+ return [3 /*break*/, 17];
317
+ swapType = (curve_1.curve.chainId === 137 && poolData.is_factory) ? 5 : poolData.is_crypto ? 4 : 2;
318
+ for (_u = 0, _v = routes[inCoin]; _u < _v.length; _u++) {
319
+ inCoinRoute = _v[_u];
320
+ routes[wrapped_coin_addresses[0]] = ((_3 = routes[wrapped_coin_addresses[0]]) !== null && _3 !== void 0 ? _3 : []).concat([__spreadArray(__spreadArray([], inCoinRoute, true), [
321
+ {
322
+ poolId: poolId,
323
+ poolAddress: poolAddress,
324
+ outputCoinAddress: wrapped_coin_addresses[0],
325
+ i: inCoinIndexes.meta_coin + 1,
326
+ j: 0,
327
+ swapType: swapType,
328
+ swapAddress: swapType === 5 ? poolData.swap_address : ethers_1.ethers.constants.AddressZero,
329
+ },
330
+ ], false)]);
331
+ nextCoins.add(wrapped_coin_addresses[0]);
332
+ }
333
+ _5.label = 17;
334
+ case 17:
335
+ _b++;
336
+ return [3 /*break*/, 3];
337
+ case 18:
338
+ _a++;
339
+ return [3 /*break*/, 2];
340
+ case 19:
341
+ // If target output coin is reached, search is finished. Assumption: the shorter route, the better.
342
+ if (outputCoinAddress in routes)
343
+ return [3 /*break*/, 21];
344
+ markedCoins.push.apply(markedCoins, curCoins);
345
+ curCoins = Array.from(nextCoins);
346
+ nextCoins = new Set();
347
+ _5.label = 20;
348
+ case 20:
349
+ step++;
350
+ return [3 /*break*/, 1];
351
+ case 21: return [2 /*return*/, (_4 = routes[outputCoinAddress]) !== null && _4 !== void 0 ? _4 : []];
352
+ }
353
+ });
354
+ }); };
355
+ exports._findAllRoutes = _findAllRoutes;
356
+ var _getRouteKey = function (route, inputCoinAddress, outputCoinAddress) {
357
+ var sortedCoins = [inputCoinAddress, outputCoinAddress].sort();
358
+ var key = "".concat(sortedCoins[0], "-->");
359
+ for (var _i = 0, _a = route.steps; _i < _a.length; _i++) {
360
+ var routeStep = _a[_i];
361
+ key += "".concat(routeStep.poolId, "-->");
362
+ }
363
+ key += sortedCoins[1];
364
+ return key;
365
+ };
366
+ var _getExchangeMultipleArgs = function (inputCoinAddress, route) {
367
+ var _route = [inputCoinAddress];
368
+ var _swapParams = [];
369
+ var _factorySwapAddresses = [];
370
+ for (var _i = 0, _a = route.steps; _i < _a.length; _i++) {
371
+ var routeStep = _a[_i];
372
+ _route.push(routeStep.poolAddress, routeStep.outputCoinAddress);
373
+ _swapParams.push([routeStep.i, routeStep.j, routeStep.swapType]);
374
+ _factorySwapAddresses.push(routeStep.swapAddress);
375
+ }
376
+ _route = _route.concat(Array(9 - _route.length).fill(ethers_1.ethers.constants.AddressZero));
377
+ _swapParams = _swapParams.concat(Array(4 - _swapParams.length).fill([0, 0, 0]));
378
+ _factorySwapAddresses = _factorySwapAddresses.concat(Array(4 - _factorySwapAddresses.length).fill(ethers_1.ethers.constants.AddressZero));
379
+ return { _route: _route, _swapParams: _swapParams, _factorySwapAddresses: _factorySwapAddresses };
380
+ };
381
+ var _estimatedGasForDifferentRoutesCache = {};
382
+ var _estimateGasForDifferentRoutes = function (routes, inputCoinAddress, outputCoinAddress, _amount) { return __awaiter(void 0, void 0, void 0, function () {
383
+ var contract, gasPromises, value, _i, routes_1, route, routeKey, gasPromise, _a, _route, _swapParams, _factorySwapAddresses, _gasAmounts_1, err_1;
384
+ var _b;
385
+ return __generator(this, function (_c) {
386
+ switch (_c.label) {
387
+ case 0:
388
+ inputCoinAddress = inputCoinAddress.toLowerCase();
389
+ outputCoinAddress = outputCoinAddress.toLowerCase();
390
+ contract = curve_1.curve.contracts[curve_1.curve.constants.ALIASES.registry_exchange].contract;
391
+ gasPromises = [];
392
+ value = (0, utils_1.isEth)(inputCoinAddress) ? _amount : ethers_1.ethers.BigNumber.from(0);
393
+ for (_i = 0, routes_1 = routes; _i < routes_1.length; _i++) {
394
+ route = routes_1[_i];
395
+ routeKey = _getRouteKey(route, inputCoinAddress, outputCoinAddress);
396
+ gasPromise = void 0;
397
+ _a = _getExchangeMultipleArgs(inputCoinAddress, route), _route = _a._route, _swapParams = _a._swapParams, _factorySwapAddresses = _a._factorySwapAddresses;
398
+ if ((((_b = _estimatedGasForDifferentRoutesCache[routeKey]) === null || _b === void 0 ? void 0 : _b.time) || 0) + 3600000 < Date.now()) {
399
+ gasPromise = contract.estimateGas.exchange_multiple(_route, _swapParams, _amount, 0, _factorySwapAddresses, __assign(__assign({}, curve_1.curve.constantOptions), { value: value }));
400
+ }
401
+ else {
402
+ gasPromise = Promise.resolve(_estimatedGasForDifferentRoutesCache[routeKey].gas);
403
+ }
404
+ gasPromises.push(gasPromise);
405
+ }
406
+ _c.label = 1;
407
+ case 1:
408
+ _c.trys.push([1, 3, , 4]);
409
+ return [4 /*yield*/, Promise.all(gasPromises)];
410
+ case 2:
411
+ _gasAmounts_1 = _c.sent();
412
+ routes.forEach(function (route, i) {
413
+ var routeKey = _getRouteKey(route, inputCoinAddress, outputCoinAddress);
414
+ _estimatedGasForDifferentRoutesCache[routeKey] = { 'gas': _gasAmounts_1[i], 'time': Date.now() };
415
+ });
416
+ return [2 /*return*/, _gasAmounts_1.map(function (_g) { return Number(ethers_1.ethers.utils.formatUnits(_g, 0)); })];
417
+ case 3:
418
+ err_1 = _c.sent();
419
+ return [2 /*return*/, routes.map(function () { return 0; })];
420
+ case 4: return [2 /*return*/];
421
+ }
422
+ });
423
+ }); };
424
+ var _getBestRouteAndOutput = (0, memoizee_1.default)(function (inputCoinAddress, outputCoinAddress, amount) { return __awaiter(void 0, void 0, void 0, function () {
425
+ var _a, inputCoinDecimals, outputCoinDecimals, _amount, routesRaw, routes, calls, promises, multicallContract, contract, _i, routesRaw_1, route, _b, _route, _swapParams, _factorySwapAddresses, calls_1, multicallContract_1, _c, routesRaw_2, route, _d, _route, _swapParams, _factorySwapAddresses, _outputAmounts, i, err_2, promises_1, contract_1, _e, routesRaw_3, route, _f, _route, _swapParams, _factorySwapAddresses, res, i, _h, gasAmounts, outputCoinUsdRate, gasData, ethUsdRate, gasPrice, expectedAmounts, expectedAmountsUsd, txCostsUsd;
426
+ return __generator(this, function (_j) {
427
+ switch (_j.label) {
428
+ case 0:
429
+ _a = (0, utils_1._getCoinDecimals)(inputCoinAddress, outputCoinAddress), inputCoinDecimals = _a[0], outputCoinDecimals = _a[1];
430
+ _amount = (0, utils_1.parseUnits)(amount, inputCoinDecimals);
431
+ return [4 /*yield*/, (0, exports._findAllRoutes)(inputCoinAddress, outputCoinAddress)];
432
+ case 1:
433
+ routesRaw = (_j.sent()).map(function (steps) { return ({ steps: steps, _output: ethers_1.ethers.BigNumber.from(0), outputUsd: 0, txCostUsd: 0 }); });
434
+ routes = [];
435
+ calls = [];
436
+ promises = [];
437
+ multicallContract = curve_1.curve.contracts[curve_1.curve.constants.ALIASES.registry_exchange].multicallContract;
438
+ contract = curve_1.curve.contracts[curve_1.curve.constants.ALIASES.registry_exchange].contract;
439
+ for (_i = 0, routesRaw_1 = routesRaw; _i < routesRaw_1.length; _i++) {
440
+ route = routesRaw_1[_i];
441
+ _b = _getExchangeMultipleArgs(inputCoinAddress, route), _route = _b._route, _swapParams = _b._swapParams, _factorySwapAddresses = _b._factorySwapAddresses;
442
+ calls.push(multicallContract.get_exchange_multiple_amount(_route, _swapParams, _amount, _factorySwapAddresses));
443
+ promises.push(contract.get_exchange_multiple_amount(_route, _swapParams, _amount, _factorySwapAddresses, curve_1.curve.constantOptions));
444
+ }
445
+ _j.label = 2;
446
+ case 2:
447
+ _j.trys.push([2, 4, , 6]);
448
+ calls_1 = [];
449
+ multicallContract_1 = curve_1.curve.contracts[curve_1.curve.constants.ALIASES.registry_exchange].multicallContract;
450
+ for (_c = 0, routesRaw_2 = routesRaw; _c < routesRaw_2.length; _c++) {
451
+ route = routesRaw_2[_c];
452
+ _d = _getExchangeMultipleArgs(inputCoinAddress, route), _route = _d._route, _swapParams = _d._swapParams, _factorySwapAddresses = _d._factorySwapAddresses;
453
+ calls_1.push(multicallContract_1.get_exchange_multiple_amount(_route, _swapParams, _amount, _factorySwapAddresses));
454
+ }
455
+ return [4 /*yield*/, curve_1.curve.multicallProvider.all(calls_1)];
456
+ case 3:
457
+ _outputAmounts = _j.sent();
458
+ for (i = 0; i < _outputAmounts.length; i++) {
459
+ routesRaw[i]._output = _outputAmounts[i];
460
+ routes.push(routesRaw[i]);
461
+ }
462
+ return [3 /*break*/, 6];
463
+ case 4:
464
+ err_2 = _j.sent();
465
+ promises_1 = [];
466
+ contract_1 = curve_1.curve.contracts[curve_1.curve.constants.ALIASES.registry_exchange].contract;
467
+ for (_e = 0, routesRaw_3 = routesRaw; _e < routesRaw_3.length; _e++) {
468
+ route = routesRaw_3[_e];
469
+ _f = _getExchangeMultipleArgs(inputCoinAddress, route), _route = _f._route, _swapParams = _f._swapParams, _factorySwapAddresses = _f._factorySwapAddresses;
470
+ promises_1.push(contract_1.get_exchange_multiple_amount(_route, _swapParams, _amount, _factorySwapAddresses, curve_1.curve.constantOptions));
471
+ }
472
+ return [4 /*yield*/, Promise.allSettled(promises_1)];
473
+ case 5:
474
+ res = _j.sent();
475
+ for (i = 0; i < res.length; i++) {
476
+ if (res[i].status === 'rejected') {
477
+ console.log("Route ".concat((routesRaw[i].steps.map(function (s) { return s.poolId; })).join(" --> "), " is anavailable"));
478
+ continue;
479
+ }
480
+ routesRaw[i]._output = res[i].value;
481
+ routes.push(routesRaw[i]);
482
+ }
483
+ return [3 /*break*/, 6];
484
+ case 6:
485
+ if (routes.length === 0) {
486
+ return [2 /*return*/, {
487
+ steps: [],
488
+ _output: ethers_1.ethers.BigNumber.from(0),
489
+ outputUsd: 0,
490
+ txCostUsd: 0,
491
+ }];
492
+ }
493
+ if (routes.length === 1)
494
+ return [2 /*return*/, routes[0]];
495
+ return [4 /*yield*/, Promise.all([
496
+ _estimateGasForDifferentRoutes(routes, inputCoinAddress, outputCoinAddress, _amount),
497
+ (0, utils_1._getUsdRate)(outputCoinAddress),
498
+ axios_1.default.get("https://api.curve.fi/api/getGas"),
499
+ (0, utils_1._getUsdRate)(curve_1.curve.chainId === 137 ? curve_1.curve.constants.COINS.matic : curve_1.curve.constants.COINS.eth),
500
+ ])];
501
+ case 7:
502
+ _h = _j.sent(), gasAmounts = _h[0], outputCoinUsdRate = _h[1], gasData = _h[2], ethUsdRate = _h[3];
503
+ gasPrice = gasData.data.data.gas.standard;
504
+ expectedAmounts = (routes).map(function (route) { return Number(ethers_1.ethers.utils.formatUnits(route._output, outputCoinDecimals)); });
505
+ expectedAmountsUsd = expectedAmounts.map(function (a) { return a * outputCoinUsdRate; });
506
+ txCostsUsd = gasAmounts.map(function (a) { return ethUsdRate * a * gasPrice / 1e18; });
507
+ routes.forEach(function (route, i) {
508
+ route.outputUsd = expectedAmountsUsd[i];
509
+ route.txCostUsd = txCostsUsd[i];
510
+ });
511
+ return [2 /*return*/, routes.reduce(function (route1, route2) { return (route1.outputUsd - route1.txCostUsd) - (route2.outputUsd - route2.txCostUsd) >= 0 ? route1 : route2; })];
512
+ }
513
+ });
514
+ }); }, {
515
+ promise: true,
516
+ maxAge: 5 * 60 * 1000, // 5m
517
+ });
518
+ var getBestRouteAndOutput = function (inputCoin, outputCoin, amount) { return __awaiter(void 0, void 0, void 0, function () {
519
+ var _a, inputCoinAddress, outputCoinAddress, outputCoinDecimals, _b, steps, _output;
520
+ return __generator(this, function (_c) {
521
+ switch (_c.label) {
522
+ case 0:
523
+ _a = (0, utils_1._getCoinAddresses)(inputCoin, outputCoin), inputCoinAddress = _a[0], outputCoinAddress = _a[1];
524
+ outputCoinDecimals = (0, utils_1._getCoinDecimals)(outputCoinAddress)[0];
525
+ return [4 /*yield*/, _getBestRouteAndOutput(inputCoinAddress, outputCoinAddress, amount)];
526
+ case 1:
527
+ _b = _c.sent(), steps = _b.steps, _output = _b._output;
528
+ return [2 /*return*/, { route: steps, output: ethers_1.ethers.utils.formatUnits(_output, outputCoinDecimals) }];
529
+ }
530
+ });
531
+ }); };
532
+ exports.getBestRouteAndOutput = getBestRouteAndOutput;
533
+ var swapExpected = function (inputCoin, outputCoin, amount) { return __awaiter(void 0, void 0, void 0, function () {
534
+ return __generator(this, function (_a) {
535
+ switch (_a.label) {
536
+ case 0: return [4 /*yield*/, (0, exports.getBestRouteAndOutput)(inputCoin, outputCoin, amount)];
537
+ case 1: return [2 /*return*/, (_a.sent())['output']];
538
+ }
539
+ });
540
+ }); };
541
+ exports.swapExpected = swapExpected;
542
+ var swapIsApproved = function (inputCoin, amount) { return __awaiter(void 0, void 0, void 0, function () {
543
+ return __generator(this, function (_a) {
544
+ switch (_a.label) {
545
+ case 0: return [4 /*yield*/, (0, utils_1.hasAllowance)([inputCoin], [amount], curve_1.curve.signerAddress, curve_1.curve.constants.ALIASES.registry_exchange)];
546
+ case 1: return [2 /*return*/, _a.sent()];
547
+ }
548
+ });
549
+ }); };
550
+ exports.swapIsApproved = swapIsApproved;
551
+ var swapApproveEstimateGas = function (inputCoin, amount) { return __awaiter(void 0, void 0, void 0, function () {
552
+ return __generator(this, function (_a) {
553
+ switch (_a.label) {
554
+ case 0: return [4 /*yield*/, (0, utils_1.ensureAllowanceEstimateGas)([inputCoin], [amount], curve_1.curve.constants.ALIASES.registry_exchange)];
555
+ case 1: return [2 /*return*/, _a.sent()];
556
+ }
557
+ });
558
+ }); };
559
+ exports.swapApproveEstimateGas = swapApproveEstimateGas;
560
+ var swapApprove = function (inputCoin, amount) { return __awaiter(void 0, void 0, void 0, function () {
561
+ return __generator(this, function (_a) {
562
+ switch (_a.label) {
563
+ case 0: return [4 /*yield*/, (0, utils_1.ensureAllowance)([inputCoin], [amount], curve_1.curve.constants.ALIASES.registry_exchange)];
564
+ case 1: return [2 /*return*/, _a.sent()];
565
+ }
566
+ });
567
+ }); };
568
+ exports.swapApprove = swapApprove;
569
+ var swapEstimateGas = function (inputCoin, outputCoin, amount) { return __awaiter(void 0, void 0, void 0, function () {
570
+ var _a, inputCoinAddress, outputCoinAddress, inputCoinDecimals, route, _amount, gas;
571
+ return __generator(this, function (_b) {
572
+ switch (_b.label) {
573
+ case 0:
574
+ _a = (0, utils_1._getCoinAddresses)(inputCoin, outputCoin), inputCoinAddress = _a[0], outputCoinAddress = _a[1];
575
+ inputCoinDecimals = (0, utils_1._getCoinDecimals)(inputCoinAddress, outputCoinAddress)[0];
576
+ return [4 /*yield*/, _getBestRouteAndOutput(inputCoinAddress, outputCoinAddress, amount)];
577
+ case 1:
578
+ route = _b.sent();
579
+ _amount = (0, utils_1.parseUnits)(amount, inputCoinDecimals);
580
+ return [4 /*yield*/, _estimateGasForDifferentRoutes([route], inputCoinAddress, outputCoinAddress, _amount)];
581
+ case 2:
582
+ gas = (_b.sent())[0];
583
+ return [2 /*return*/, gas];
584
+ }
585
+ });
586
+ }); };
587
+ exports.swapEstimateGas = swapEstimateGas;
588
+ var swap = function (inputCoin, outputCoin, amount, slippage) {
589
+ if (slippage === void 0) { slippage = 0.5; }
590
+ return __awaiter(void 0, void 0, void 0, function () {
591
+ var _a, inputCoinAddress, outputCoinAddress, _b, inputCoinDecimals, outputCoinDecimals, route, _c, _route, _swapParams, _factorySwapAddresses, _amount, minRecvAmountBN, _minRecvAmount, contract, value, gasLimit;
592
+ return __generator(this, function (_d) {
593
+ switch (_d.label) {
594
+ case 0:
595
+ _a = (0, utils_1._getCoinAddresses)(inputCoin, outputCoin), inputCoinAddress = _a[0], outputCoinAddress = _a[1];
596
+ _b = (0, utils_1._getCoinDecimals)(inputCoinAddress, outputCoinAddress), inputCoinDecimals = _b[0], outputCoinDecimals = _b[1];
597
+ return [4 /*yield*/, (0, exports.swapApprove)(inputCoin, amount)];
598
+ case 1:
599
+ _d.sent();
600
+ return [4 /*yield*/, _getBestRouteAndOutput(inputCoinAddress, outputCoinAddress, amount)];
601
+ case 2:
602
+ route = _d.sent();
603
+ if (route.steps.length === 0) {
604
+ throw new Error("This pair can't be exchanged");
605
+ }
606
+ _c = _getExchangeMultipleArgs(inputCoinAddress, route), _route = _c._route, _swapParams = _c._swapParams, _factorySwapAddresses = _c._factorySwapAddresses;
607
+ _amount = (0, utils_1.parseUnits)(amount, inputCoinDecimals);
608
+ minRecvAmountBN = (0, utils_1.toBN)(route._output, outputCoinDecimals).times(100 - slippage).div(100);
609
+ _minRecvAmount = (0, utils_1.fromBN)(minRecvAmountBN, outputCoinDecimals);
610
+ contract = curve_1.curve.contracts[curve_1.curve.constants.ALIASES.registry_exchange].contract;
611
+ value = (0, utils_1.isEth)(inputCoinAddress) ? _amount : ethers_1.ethers.BigNumber.from(0);
612
+ return [4 /*yield*/, curve_1.curve.updateFeeData()];
613
+ case 3:
614
+ _d.sent();
615
+ return [4 /*yield*/, contract.estimateGas.exchange_multiple(_route, _swapParams, _amount, _minRecvAmount, _factorySwapAddresses, __assign(__assign({}, curve_1.curve.constantOptions), { value: value }))];
616
+ case 4:
617
+ gasLimit = (_d.sent()).mul(curve_1.curve.chainId === 1 ? 130 : 160).div(100);
618
+ return [4 /*yield*/, contract.exchange_multiple(_route, _swapParams, _amount, _minRecvAmount, _factorySwapAddresses, __assign(__assign({}, curve_1.curve.options), { value: value, gasLimit: gasLimit }))];
619
+ case 5: return [2 /*return*/, (_d.sent()).hash];
620
+ }
621
+ });
622
+ });
623
+ };
624
+ exports.swap = swap;