@mento-protocol/mento-sdk 1.10.2 → 2.0.0-beta.10

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 (254) hide show
  1. package/README.md +114 -48
  2. package/dist/abis/bipoolmanager.d.ts +15 -0
  3. package/dist/abis/bipoolmanager.d.ts.map +1 -0
  4. package/dist/abis/bipoolmanager.js +26 -0
  5. package/dist/abis/bipoolmanager.js.map +1 -0
  6. package/dist/abis/broker.d.ts +2 -0
  7. package/dist/abis/broker.d.ts.map +1 -0
  8. package/dist/abis/broker.js +8 -0
  9. package/dist/abis/broker.js.map +1 -0
  10. package/dist/abis/erc20.d.ts +2 -0
  11. package/dist/abis/erc20.d.ts.map +1 -0
  12. package/dist/abis/erc20.js +11 -0
  13. package/dist/abis/erc20.js.map +1 -0
  14. package/dist/abis/index.d.ts +5 -0
  15. package/dist/abis/index.d.ts.map +1 -0
  16. package/dist/{cjs/constants → abis}/index.js +5 -2
  17. package/dist/abis/index.js.map +1 -0
  18. package/dist/abis/pricingmodule.d.ts +2 -0
  19. package/dist/abis/pricingmodule.d.ts.map +1 -0
  20. package/dist/abis/pricingmodule.js +5 -0
  21. package/dist/abis/pricingmodule.js.map +1 -0
  22. package/dist/abis/reserve.d.ts +2 -0
  23. package/dist/abis/reserve.d.ts.map +1 -0
  24. package/dist/abis/reserve.js +9 -0
  25. package/dist/abis/reserve.js.map +1 -0
  26. package/dist/abis/uniswapV3.d.ts +4 -0
  27. package/dist/abis/uniswapV3.d.ts.map +1 -0
  28. package/dist/abis/uniswapV3.js +15 -0
  29. package/dist/abis/uniswapV3.js.map +1 -0
  30. package/dist/adapters/implementations/ethersAdapter.d.ts +9 -0
  31. package/dist/adapters/implementations/ethersAdapter.d.ts.map +1 -0
  32. package/dist/adapters/implementations/ethersAdapter.js +19 -0
  33. package/dist/adapters/implementations/ethersAdapter.js.map +1 -0
  34. package/dist/adapters/implementations/ethersV5Adapter.d.ts +9 -0
  35. package/dist/adapters/implementations/ethersV5Adapter.d.ts.map +1 -0
  36. package/dist/adapters/implementations/ethersV5Adapter.js +19 -0
  37. package/dist/adapters/implementations/ethersV5Adapter.js.map +1 -0
  38. package/dist/adapters/implementations/viemAdapter.d.ts +9 -0
  39. package/dist/adapters/implementations/viemAdapter.d.ts.map +1 -0
  40. package/dist/adapters/implementations/viemAdapter.js +26 -0
  41. package/dist/adapters/implementations/viemAdapter.js.map +1 -0
  42. package/dist/adapters/index.d.ts +5 -0
  43. package/dist/adapters/index.d.ts.map +1 -0
  44. package/dist/adapters/index.js +10 -0
  45. package/dist/adapters/index.js.map +1 -0
  46. package/dist/adapters/proxies/ethersAdapterProxy.d.ts +25 -0
  47. package/dist/adapters/proxies/ethersAdapterProxy.d.ts.map +1 -0
  48. package/dist/adapters/proxies/ethersAdapterProxy.js +71 -0
  49. package/dist/adapters/proxies/ethersAdapterProxy.js.map +1 -0
  50. package/dist/adapters/proxies/ethersV5AdapterProxy.d.ts +25 -0
  51. package/dist/adapters/proxies/ethersV5AdapterProxy.d.ts.map +1 -0
  52. package/dist/adapters/proxies/ethersV5AdapterProxy.js +71 -0
  53. package/dist/adapters/proxies/ethersV5AdapterProxy.js.map +1 -0
  54. package/dist/adapters/proxies/viemAdapterProxy.d.ts +25 -0
  55. package/dist/adapters/proxies/viemAdapterProxy.d.ts.map +1 -0
  56. package/dist/adapters/proxies/viemAdapterProxy.js +71 -0
  57. package/dist/adapters/proxies/viemAdapterProxy.js.map +1 -0
  58. package/dist/constants/aaveConfig.d.ts +7 -0
  59. package/dist/constants/aaveConfig.d.ts.map +1 -0
  60. package/dist/constants/aaveConfig.js +22 -0
  61. package/dist/constants/aaveConfig.js.map +1 -0
  62. package/dist/constants/addresses.d.ts +14 -0
  63. package/dist/constants/addresses.d.ts.map +1 -0
  64. package/dist/{esm/constants → constants}/addresses.js +17 -16
  65. package/dist/constants/addresses.js.map +1 -0
  66. package/dist/{cjs/enums → constants}/chainId.d.ts +1 -0
  67. package/dist/constants/chainId.d.ts.map +1 -0
  68. package/dist/{cjs/enums → constants}/chainId.js +2 -1
  69. package/dist/constants/chainId.js.map +1 -0
  70. package/dist/constants/contractNames.d.ts +21 -0
  71. package/dist/constants/contractNames.d.ts.map +1 -0
  72. package/dist/constants/contractNames.js +24 -0
  73. package/dist/constants/contractNames.js.map +1 -0
  74. package/dist/constants/index.d.ts +9 -0
  75. package/dist/constants/index.d.ts.map +1 -0
  76. package/dist/constants/index.js +25 -0
  77. package/dist/constants/index.js.map +1 -0
  78. package/dist/constants/mentoAddresses.d.ts +6 -0
  79. package/dist/constants/mentoAddresses.d.ts.map +1 -0
  80. package/dist/constants/mentoAddresses.js +9 -0
  81. package/dist/constants/mentoAddresses.js.map +1 -0
  82. package/dist/{cjs/enums → constants}/proposalState.d.ts +1 -0
  83. package/dist/constants/proposalState.d.ts.map +1 -0
  84. package/dist/{cjs/enums → constants}/proposalState.js +2 -1
  85. package/dist/constants/proposalState.js.map +1 -0
  86. package/dist/constants/protocolAddresses.d.ts +5 -0
  87. package/dist/constants/protocolAddresses.d.ts.map +1 -0
  88. package/dist/constants/protocolAddresses.js +8 -0
  89. package/dist/constants/protocolAddresses.js.map +1 -0
  90. package/dist/constants/stableTokenMetadata.d.ts +12 -0
  91. package/dist/constants/stableTokenMetadata.d.ts.map +1 -0
  92. package/dist/constants/stableTokenMetadata.js +14 -0
  93. package/dist/constants/stableTokenMetadata.js.map +1 -0
  94. package/dist/index.d.ts +67 -0
  95. package/dist/index.d.ts.map +1 -0
  96. package/dist/index.js +122 -0
  97. package/dist/index.js.map +1 -0
  98. package/dist/services/collateralAssetService.d.ts +8 -0
  99. package/dist/services/collateralAssetService.d.ts.map +1 -0
  100. package/dist/services/collateralAssetService.js +47 -0
  101. package/dist/services/collateralAssetService.js.map +1 -0
  102. package/dist/services/index.d.ts +6 -0
  103. package/dist/services/index.d.ts.map +1 -0
  104. package/dist/{cjs/interfaces → services}/index.js +6 -4
  105. package/dist/services/index.js.map +1 -0
  106. package/dist/services/stableTokenService.d.ts +9 -0
  107. package/dist/services/stableTokenService.d.ts.map +1 -0
  108. package/dist/services/stableTokenService.js +40 -0
  109. package/dist/services/stableTokenService.js.map +1 -0
  110. package/dist/services/supply/ISupplyCalculator.d.ts +14 -0
  111. package/dist/services/supply/ISupplyCalculator.d.ts.map +1 -0
  112. package/dist/{cjs/interfaces/tradingLimitsState.js → services/supply/ISupplyCalculator.js} +1 -0
  113. package/dist/services/supply/ISupplyCalculator.js.map +1 -0
  114. package/dist/services/supply/aaveSupplyCalculator.d.ts +21 -0
  115. package/dist/services/supply/aaveSupplyCalculator.d.ts.map +1 -0
  116. package/dist/services/supply/aaveSupplyCalculator.js +46 -0
  117. package/dist/services/supply/aaveSupplyCalculator.js.map +1 -0
  118. package/dist/services/supply/calculatorFactory.d.ts +8 -0
  119. package/dist/services/supply/calculatorFactory.d.ts.map +1 -0
  120. package/dist/services/supply/calculatorFactory.js +3 -0
  121. package/dist/services/supply/calculatorFactory.js.map +1 -0
  122. package/dist/services/supply/defaultCalculatorFactory.d.ts +9 -0
  123. package/dist/services/supply/defaultCalculatorFactory.d.ts.map +1 -0
  124. package/dist/services/supply/defaultCalculatorFactory.js +27 -0
  125. package/dist/services/supply/defaultCalculatorFactory.js.map +1 -0
  126. package/dist/services/supply/index.d.ts +7 -0
  127. package/dist/services/supply/index.d.ts.map +1 -0
  128. package/dist/{cjs → services/supply}/index.js +7 -6
  129. package/dist/services/supply/index.js.map +1 -0
  130. package/dist/services/supply/multisigSupplyCalculator.d.ts +9 -0
  131. package/dist/services/supply/multisigSupplyCalculator.d.ts.map +1 -0
  132. package/dist/services/supply/multisigSupplyCalculator.js +33 -0
  133. package/dist/services/supply/multisigSupplyCalculator.js.map +1 -0
  134. package/dist/services/supply/uniV3SupplyCalculator.d.ts +21 -0
  135. package/dist/services/supply/uniV3SupplyCalculator.d.ts.map +1 -0
  136. package/dist/services/supply/uniV3SupplyCalculator.js +233 -0
  137. package/dist/services/supply/uniV3SupplyCalculator.js.map +1 -0
  138. package/dist/services/supplyAdjustmentService.d.ts +14 -0
  139. package/dist/services/supplyAdjustmentService.d.ts.map +1 -0
  140. package/dist/services/supplyAdjustmentService.js +46 -0
  141. package/dist/services/supplyAdjustmentService.js.map +1 -0
  142. package/dist/services/tokenMetadataService.d.ts +8 -0
  143. package/dist/services/tokenMetadataService.d.ts.map +1 -0
  144. package/dist/services/tokenMetadataService.js +44 -0
  145. package/dist/services/tokenMetadataService.js.map +1 -0
  146. package/dist/{cjs/types → types}/contractAddresses.d.ts +1 -1
  147. package/dist/types/contractAddresses.d.ts.map +1 -0
  148. package/dist/{cjs/types → types}/contractAddresses.js +1 -0
  149. package/dist/types/contractAddresses.js.map +1 -0
  150. package/dist/types/exchange.d.ts +6 -0
  151. package/dist/types/exchange.d.ts.map +1 -0
  152. package/dist/{cjs/interfaces/tradingLimit.js → types/exchange.js} +1 -0
  153. package/dist/types/exchange.js.map +1 -0
  154. package/dist/types/index.d.ts +5 -0
  155. package/dist/types/index.d.ts.map +1 -0
  156. package/dist/{cjs/types → types}/index.js +4 -1
  157. package/dist/types/index.js.map +1 -0
  158. package/dist/types/provider.d.ts +11 -0
  159. package/dist/types/provider.d.ts.map +1 -0
  160. package/dist/{cjs/interfaces/tradingLimitsConfig.js → types/provider.js} +1 -0
  161. package/dist/types/provider.js.map +1 -0
  162. package/dist/types/token.d.ts +25 -0
  163. package/dist/types/token.d.ts.map +1 -0
  164. package/dist/{cjs/interfaces/IChainClient.js → types/token.js} +1 -0
  165. package/dist/types/token.js.map +1 -0
  166. package/dist/utils/index.d.ts +2 -0
  167. package/dist/utils/index.d.ts.map +1 -0
  168. package/dist/{cjs/enums → utils}/index.js +2 -2
  169. package/dist/utils/index.js.map +1 -0
  170. package/dist/utils/retry.d.ts +12 -0
  171. package/dist/utils/retry.d.ts.map +1 -0
  172. package/dist/utils/retry.js +28 -0
  173. package/dist/utils/retry.js.map +1 -0
  174. package/package.json +33 -61
  175. package/dist/cjs/ChainClient.d.ts +0 -9
  176. package/dist/cjs/ChainClient.js +0 -58
  177. package/dist/cjs/TestChainClient.d.ts +0 -7
  178. package/dist/cjs/TestChainClient.js +0 -41
  179. package/dist/cjs/constants/addresses.d.ts +0 -4
  180. package/dist/cjs/constants/addresses.js +0 -64
  181. package/dist/cjs/constants/index.d.ts +0 -2
  182. package/dist/cjs/constants/tradablePairs.42220.d.ts +0 -2
  183. package/dist/cjs/constants/tradablePairs.42220.js +0 -6407
  184. package/dist/cjs/constants/tradablePairs.44787.d.ts +0 -2
  185. package/dist/cjs/constants/tradablePairs.44787.js +0 -7391
  186. package/dist/cjs/constants/tradablePairs.d.ts +0 -16
  187. package/dist/cjs/constants/tradablePairs.js +0 -53
  188. package/dist/cjs/enums/index.d.ts +0 -2
  189. package/dist/cjs/governance.d.ts +0 -62
  190. package/dist/cjs/governance.js +0 -151
  191. package/dist/cjs/index.d.ts +0 -6
  192. package/dist/cjs/interfaces/IChainClient.d.ts +0 -6
  193. package/dist/cjs/interfaces/index.d.ts +0 -4
  194. package/dist/cjs/interfaces/tradingLimit.d.ts +0 -7
  195. package/dist/cjs/interfaces/tradingLimitsConfig.d.ts +0 -10
  196. package/dist/cjs/interfaces/tradingLimitsState.d.ts +0 -9
  197. package/dist/cjs/limits.d.ts +0 -33
  198. package/dist/cjs/limits.js +0 -130
  199. package/dist/cjs/mento.d.ts +0 -238
  200. package/dist/cjs/mento.js +0 -559
  201. package/dist/cjs/routeUtils.d.ts +0 -304
  202. package/dist/cjs/routeUtils.js +0 -372
  203. package/dist/cjs/types/contractAddressMap.d.ts +0 -4
  204. package/dist/cjs/types/contractAddressMap.js +0 -2
  205. package/dist/cjs/types/index.d.ts +0 -2
  206. package/dist/cjs/utils.d.ts +0 -50
  207. package/dist/cjs/utils.js +0 -129
  208. package/dist/esm/ChainClient.d.ts +0 -9
  209. package/dist/esm/ChainClient.js +0 -54
  210. package/dist/esm/TestChainClient.d.ts +0 -7
  211. package/dist/esm/TestChainClient.js +0 -37
  212. package/dist/esm/constants/addresses.d.ts +0 -4
  213. package/dist/esm/constants/index.d.ts +0 -2
  214. package/dist/esm/constants/index.js +0 -2
  215. package/dist/esm/constants/tradablePairs.42220.d.ts +0 -2
  216. package/dist/esm/constants/tradablePairs.42220.js +0 -6404
  217. package/dist/esm/constants/tradablePairs.44787.d.ts +0 -2
  218. package/dist/esm/constants/tradablePairs.44787.js +0 -7388
  219. package/dist/esm/constants/tradablePairs.d.ts +0 -16
  220. package/dist/esm/constants/tradablePairs.js +0 -26
  221. package/dist/esm/enums/chainId.d.ts +0 -4
  222. package/dist/esm/enums/chainId.js +0 -5
  223. package/dist/esm/enums/index.d.ts +0 -2
  224. package/dist/esm/enums/index.js +0 -2
  225. package/dist/esm/enums/proposalState.d.ts +0 -10
  226. package/dist/esm/enums/proposalState.js +0 -11
  227. package/dist/esm/governance.d.ts +0 -62
  228. package/dist/esm/governance.js +0 -147
  229. package/dist/esm/index.d.ts +0 -6
  230. package/dist/esm/index.js +0 -6
  231. package/dist/esm/interfaces/IChainClient.d.ts +0 -6
  232. package/dist/esm/interfaces/IChainClient.js +0 -1
  233. package/dist/esm/interfaces/index.d.ts +0 -4
  234. package/dist/esm/interfaces/index.js +0 -4
  235. package/dist/esm/interfaces/tradingLimit.d.ts +0 -7
  236. package/dist/esm/interfaces/tradingLimit.js +0 -1
  237. package/dist/esm/interfaces/tradingLimitsConfig.d.ts +0 -10
  238. package/dist/esm/interfaces/tradingLimitsConfig.js +0 -1
  239. package/dist/esm/interfaces/tradingLimitsState.d.ts +0 -9
  240. package/dist/esm/interfaces/tradingLimitsState.js +0 -1
  241. package/dist/esm/limits.d.ts +0 -33
  242. package/dist/esm/limits.js +0 -123
  243. package/dist/esm/mento.d.ts +0 -238
  244. package/dist/esm/mento.js +0 -555
  245. package/dist/esm/routeUtils.d.ts +0 -304
  246. package/dist/esm/routeUtils.js +0 -362
  247. package/dist/esm/types/contractAddressMap.d.ts +0 -4
  248. package/dist/esm/types/contractAddressMap.js +0 -1
  249. package/dist/esm/types/contractAddresses.d.ts +0 -23
  250. package/dist/esm/types/contractAddresses.js +0 -1
  251. package/dist/esm/types/index.d.ts +0 -2
  252. package/dist/esm/types/index.js +0 -2
  253. package/dist/esm/utils.d.ts +0 -50
  254. package/dist/esm/utils.js +0 -119
package/dist/cjs/mento.js DELETED
@@ -1,559 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.Mento = void 0;
13
- const mento_core_ts_1 = require("@mento-protocol/mento-core-ts");
14
- const ethers_1 = require("ethers");
15
- const limits_1 = require("./limits");
16
- const utils_1 = require("./utils");
17
- const assert_1 = require("assert");
18
- const mento_router_ts_1 = require("mento-router-ts");
19
- const addresses_1 = require("./constants/addresses");
20
- const tradablePairs_1 = require("./constants/tradablePairs");
21
- const routeUtils_1 = require("./routeUtils");
22
- class Mento {
23
- /**
24
- * This constructor is private, use the static create or createWithParams methods
25
- * to create a new Mento instance
26
- * @param signerOrProvider an ethers provider or connected signer
27
- * @param brokerAddress the address of the broker contract
28
- * @param exchanges exchange data for the broker
29
- */
30
- constructor(signerOrProvider, brokerAddress, routerAddress, exchanges) {
31
- this.signerOrProvider = signerOrProvider;
32
- this.broker = mento_core_ts_1.IBroker__factory.connect(brokerAddress, signerOrProvider);
33
- this.router = mento_router_ts_1.IMentoRouter__factory.connect(routerAddress, signerOrProvider);
34
- this.exchanges = exchanges || [];
35
- }
36
- /**
37
- * Creates a new Mento object instance.
38
- * When constructed with only a Provider only read-only operations are supported
39
- * @param signerOrProvider an ethers signer or provider. A signer is required to execute swaps
40
- * @returns a new Mento object instance
41
- */
42
- static create(signerOrProvider) {
43
- return __awaiter(this, void 0, void 0, function* () {
44
- (0, utils_1.validateSignerOrProvider)(signerOrProvider);
45
- return new Mento(signerOrProvider, yield (0, utils_1.getBrokerAddressFromRegistry)(signerOrProvider), yield (0, addresses_1.getAddress)('MentoRouter', yield (0, utils_1.getChainId)(signerOrProvider)));
46
- });
47
- }
48
- /**
49
- * Create a new Mento object instance given a broker address and optional exchanges data
50
- * When constructed with a Provider, only read-only operations are supported
51
- * @param signerOrProvider an ethers signer or provider. A signer is required to execute swaps
52
- * @param brokerAddr the address of the broker contract
53
- * @param exchanges the exchanges data for the broker
54
- * @returns a new Mento object instance
55
- */
56
- static createWithParams(signerOrProvider, brokerAddr, routerAddr, exchanges) {
57
- (0, utils_1.validateSignerOrProvider)(signerOrProvider);
58
- return new Mento(signerOrProvider, brokerAddr, routerAddr, exchanges);
59
- }
60
- /**
61
- * Returns a new Mento instance connected to the given signer
62
- * @param signer an ethers signer
63
- * @returns new Mento object instance
64
- */
65
- connectSigner(signer) {
66
- (0, utils_1.validateSigner)(signer);
67
- return new Mento(signer, this.broker.address, this.router.address, this.exchanges);
68
- }
69
- /**
70
- * Get tradable pairs for backwards compatibility
71
- * @returns an array of Asset pairs
72
- */
73
- getTradablePairs({ cached = true, } = {}) {
74
- return __awaiter(this, void 0, void 0, function* () {
75
- return (yield this.getTradablePairsWithPath({ cached })).map((pair) => pair.assets);
76
- });
77
- }
78
- /**
79
- * Returns a list of all tradable pairs on Mento via direct exchanges.
80
- * Each pair is represented using the TradablePair interface, with its id
81
- * (a concatenation of the two asset symbols in alphabetical order),
82
- * the two Asset objects, and a path (an array with a single direct exchange hop).
83
- * @returns An array of direct TradablePair objects.
84
- */
85
- getDirectPairs() {
86
- return __awaiter(this, void 0, void 0, function* () {
87
- const exchanges = yield this.getExchanges();
88
- // Map from pair id (symbol-symbol) to its TradablePair
89
- const directPairsMap = new Map();
90
- for (const exchange of exchanges) {
91
- const [token0, token1] = exchange.assets;
92
- const [symbol0, symbol1] = yield Promise.all([
93
- (0, utils_1.getSymbolFromTokenAddress)(token0, this.signerOrProvider),
94
- (0, utils_1.getSymbolFromTokenAddress)(token1, this.signerOrProvider),
95
- ]);
96
- // Determine canonical order by symbol
97
- let assets;
98
- let pairId;
99
- if (symbol0 <= symbol1) {
100
- assets = [
101
- { address: token0, symbol: symbol0 },
102
- { address: token1, symbol: symbol1 },
103
- ];
104
- pairId = `${symbol0}-${symbol1}`;
105
- }
106
- else {
107
- assets = [
108
- { address: token1, symbol: symbol1 },
109
- { address: token0, symbol: symbol0 },
110
- ];
111
- pairId = `${symbol1}-${symbol0}`;
112
- }
113
- const pathEntry = {
114
- providerAddr: exchange.providerAddr,
115
- id: exchange.id,
116
- assets: exchange.assets,
117
- };
118
- if (directPairsMap.has(pairId)) {
119
- directPairsMap.get(pairId).path.push(pathEntry);
120
- }
121
- else {
122
- directPairsMap.set(pairId, { id: pairId, assets, path: [pathEntry] });
123
- }
124
- }
125
- return Array.from(directPairsMap.values());
126
- });
127
- }
128
- /**
129
- * Returns a list of all tradable pairs on Mento, including those achievable
130
- * via two-hop routes. For two-hop pairs, the path will contain two exchange hops.
131
- * Each TradablePair contains an id (the concatenation of the two asset symbols in alphabetical order),
132
- * the two Asset objects, and an array of exchange details for each hop.
133
- * @param options - Optional parameters
134
- * @param options.cached - Whether to use cached data (default: true)
135
- * @param options.returnAllRoutes - Whether to return all possible routes or just the best one per pair (default: false)
136
- * @returns An array of TradablePair objects representing available trade routes.
137
- */
138
- getTradablePairsWithPath({ cached = true, returnAllRoutes = false, } = {}) {
139
- return __awaiter(this, void 0, void 0, function* () {
140
- // Try to get cached data first
141
- if (cached) {
142
- const cachedPairs = yield this.getCachedTradablePairs();
143
- if (cachedPairs)
144
- return cachedPairs;
145
- }
146
- // Generate routes from scratch
147
- const directPairs = yield this.getDirectPairs();
148
- const connectivity = (0, routeUtils_1.buildConnectivityStructures)(directPairs);
149
- const allRoutes = (0, routeUtils_1.generateAllRoutes)(connectivity);
150
- return (0, routeUtils_1.selectOptimalRoutes)(allRoutes, returnAllRoutes, connectivity.addrToSymbol);
151
- });
152
- }
153
- /**
154
- * Attempts to get cached tradable pairs for the current chain
155
- */
156
- getCachedTradablePairs() {
157
- return __awaiter(this, void 0, void 0, function* () {
158
- const chainId = yield (0, utils_1.getChainId)(this.signerOrProvider);
159
- return yield (0, tradablePairs_1.getCachedTradablePairs)(chainId);
160
- });
161
- }
162
- /**
163
- * Returns the amount of tokenIn to be sold to buy amountOut of tokenOut.
164
- * If the provided tradablePair has a single (direct) pricing path, then direct pricing is used.
165
- * Otherwise, routed pricing via the MentoRouter is applied.
166
- * @param tokenIn the token to be sold
167
- * @param tokenOut the token to be bought
168
- * @param amountOut the desired amount of tokenOut to be obtained
169
- * @param tradablePair the TradablePair object containing the pricing path information
170
- * @returns the amount of tokenIn to be sold
171
- */
172
- getAmountIn(tokenIn, tokenOut, amountOut, tradablePair) {
173
- return __awaiter(this, void 0, void 0, function* () {
174
- if (!tradablePair) {
175
- tradablePair = yield this.findPairForTokens(tokenIn, tokenOut);
176
- }
177
- if (tradablePair.path.length === 1) {
178
- return this.getAmountInDirect(tokenIn, tokenOut, amountOut, tradablePair);
179
- }
180
- else {
181
- return this.getAmountInRouted(tokenIn, tokenOut, amountOut, tradablePair);
182
- }
183
- });
184
- }
185
- /**
186
- * Returns the amount of tokenOut to be bought by selling amountIn of tokenIn.
187
- * If the provided tradablePair has a single (direct) pricing path, then direct pricing is used.
188
- * Otherwise, routed pricing via the MentoRouter is applied.
189
- * @param tokenIn the token to be sold
190
- * @param tokenOut the token to be bought
191
- * @param amountIn the amount of tokenIn to be sold
192
- * @param tradablePair the TradablePair object containing the pricing path information
193
- * @returns the amount of tokenOut to be bought
194
- */
195
- getAmountOut(tokenIn, tokenOut, amountIn, tradablePair) {
196
- return __awaiter(this, void 0, void 0, function* () {
197
- if (!tradablePair) {
198
- tradablePair = yield this.findPairForTokens(tokenIn, tokenOut);
199
- }
200
- if (tradablePair.path.length === 1) {
201
- return this.getAmountOutDirect(tokenIn, tokenOut, amountIn, tradablePair);
202
- }
203
- else {
204
- return this.getAmountOutRouted(tokenIn, tokenOut, amountIn, tradablePair);
205
- }
206
- });
207
- }
208
- /**
209
- * Internal method for direct pricing: retrieves the exchange for the given tokens
210
- * and returns the amountIn using the broker.
211
- */
212
- getAmountInDirect(tokenIn, tokenOut, amountOut, tradablePair) {
213
- return __awaiter(this, void 0, void 0, function* () {
214
- return this.broker.getAmountIn(tradablePair.path[0].providerAddr, tradablePair.path[0].id, tokenIn, tokenOut, amountOut);
215
- });
216
- }
217
- /**
218
- * Internal method for direct pricing: retrieves the exchange for the given tokens
219
- * and returns the amountOut using the broker.
220
- */
221
- getAmountOutDirect(tokenIn, tokenOut, amountIn, tradablePair) {
222
- return __awaiter(this, void 0, void 0, function* () {
223
- return this.broker.getAmountOut(tradablePair.path[0].providerAddr, tradablePair.path[0].id, tokenIn, tokenOut, amountIn);
224
- });
225
- }
226
- /**
227
- * Internal method for routed pricing: uses the MentoRouter to determine the required tokenIn
228
- * for obtaining amountOut through a multi-hop route specified in tradablePair.path.
229
- */
230
- getAmountInRouted(tokenIn, tokenOut, amountOut, tradablePair) {
231
- return __awaiter(this, void 0, void 0, function* () {
232
- const steps = this.buildSteps(tokenIn, tokenOut, tradablePair);
233
- return this.router.getAmountIn(amountOut, steps);
234
- });
235
- }
236
- /**
237
- * Internal method for routed pricing: uses the MentoRouter to determine the amountOut
238
- * obtainable by selling amountIn through a multi-hop route specified in tradablePair.path.
239
- */
240
- getAmountOutRouted(tokenIn, tokenOut, amountIn, tradablePair) {
241
- return __awaiter(this, void 0, void 0, function* () {
242
- const steps = this.buildSteps(tokenIn, tokenOut, tradablePair);
243
- return this.router.getAmountOut(amountIn, steps);
244
- });
245
- }
246
- /**
247
- * Increases the broker's trading allowance for the given token
248
- * @param token the token to increase the allowance for
249
- * @param amount the amount to increase the allowance by
250
- * @returns the populated TransactionRequest object
251
- */
252
- increaseTradingAllowance(tokenIn, amount, tradablePair) {
253
- return __awaiter(this, void 0, void 0, function* () {
254
- const spender = !tradablePair || (tradablePair === null || tradablePair === void 0 ? void 0 : tradablePair.path.length) == 1
255
- ? this.broker.address
256
- : this.router.address;
257
- const tx = yield (0, utils_1.increaseAllowance)(tokenIn, spender, amount, this.signerOrProvider);
258
- if (ethers_1.Signer.isSigner(this.signerOrProvider)) {
259
- // The contract call doesn't populate all of the signer fields, so we need an extra call for the signer
260
- return this.signerOrProvider.populateTransaction(tx);
261
- }
262
- else {
263
- return tx;
264
- }
265
- });
266
- }
267
- /**
268
- * Returns a token swap populated tx object with a fixed amount of tokenIn and a minimum amount of tokenOut.
269
- * If the tradablePair contains a single-hop route, a direct swap is executed using swapExactTokensForTokens on the broker.
270
- * Otherwise, a routed swap is executed via the router.
271
- * @param tokenIn the token to be sold
272
- * @param tokenOut the token to be bought
273
- * @param amountIn the amount of tokenIn to be sold
274
- * @param amountOutMin the minimum amount of tokenOut to be bought
275
- * @param tradablePair the tradable pair details to determine routing
276
- * @returns the populated TransactionRequest object
277
- */
278
- swapIn(tokenIn, tokenOut, amountIn, amountOutMin, tradablePair) {
279
- return __awaiter(this, void 0, void 0, function* () {
280
- if (!tradablePair) {
281
- tradablePair = yield this.findPairForTokens(tokenIn, tokenOut);
282
- }
283
- if (tradablePair.path.length === 1) {
284
- return this.swapInDirect(tokenIn, tokenOut, amountIn, amountOutMin);
285
- }
286
- else {
287
- return this.swapInRouted(tokenIn, tokenOut, amountIn, amountOutMin, tradablePair);
288
- }
289
- });
290
- }
291
- swapInDirect(tokenIn, tokenOut, amountIn, amountOutMin) {
292
- return __awaiter(this, void 0, void 0, function* () {
293
- const exchange = yield this.getExchangeForTokens(tokenIn, tokenOut);
294
- const tx = yield this.broker.populateTransaction.swapIn(exchange.providerAddr, exchange.id, tokenIn, tokenOut, amountIn, amountOutMin);
295
- return ethers_1.Signer.isSigner(this.signerOrProvider)
296
- ? this.signerOrProvider.populateTransaction(tx)
297
- : tx;
298
- });
299
- }
300
- swapInRouted(tokenIn, tokenOut, amountIn, amountOutMin, tradablePair) {
301
- return __awaiter(this, void 0, void 0, function* () {
302
- const steps = this.buildSteps(tokenIn, tokenOut, tradablePair);
303
- const tx = yield this.router.populateTransaction.swapExactTokensForTokens(amountIn, amountOutMin, steps);
304
- return ethers_1.Signer.isSigner(this.signerOrProvider)
305
- ? this.signerOrProvider.populateTransaction(tx)
306
- : tx;
307
- });
308
- }
309
- /**
310
- * Returns a token swap populated tx object with a maximum amount of tokenIn and a fixed amount of tokenOut.
311
- * If the tradablePair contains a single-hop route, a direct swap is executed using swapTokensForExactTokens on the broker.
312
- * Otherwise, a routed swap is executed via the router.
313
- * @param tokenIn the token to be sold
314
- * @param tokenOut the token to be bought
315
- * @param amountOut the amount of tokenOut to be bought
316
- * @param amountInMax the maximum amount of tokenIn to be sold
317
- * @returns the populated TransactionRequest object
318
- */
319
- swapOut(tokenIn, tokenOut, amountOut, amountInMax, tradablePair) {
320
- return __awaiter(this, void 0, void 0, function* () {
321
- if (!tradablePair) {
322
- tradablePair = yield this.findPairForTokens(tokenIn, tokenOut);
323
- }
324
- if (tradablePair.path.length === 1) {
325
- return this.swapOutDirect(tokenIn, tokenOut, amountOut, amountInMax);
326
- }
327
- else {
328
- return this.swapOutRouted(tokenIn, tokenOut, amountOut, amountInMax, tradablePair);
329
- }
330
- });
331
- }
332
- swapOutDirect(tokenIn, tokenOut, amountOut, amountInMax) {
333
- return __awaiter(this, void 0, void 0, function* () {
334
- const exchange = yield this.getExchangeForTokens(tokenIn, tokenOut);
335
- const tx = yield this.broker.populateTransaction.swapOut(exchange.providerAddr, exchange.id, tokenIn, tokenOut, amountOut, amountInMax);
336
- return ethers_1.Signer.isSigner(this.signerOrProvider)
337
- ? this.signerOrProvider.populateTransaction(tx)
338
- : tx;
339
- });
340
- }
341
- swapOutRouted(tokenIn, tokenOut, amountOut, amountInMax, tradablePair) {
342
- return __awaiter(this, void 0, void 0, function* () {
343
- const steps = this.buildSteps(tokenIn, tokenOut, tradablePair);
344
- const tx = yield this.router.populateTransaction.swapTokensForExactTokens(amountOut, amountInMax, steps);
345
- return ethers_1.Signer.isSigner(this.signerOrProvider)
346
- ? this.signerOrProvider.populateTransaction(tx)
347
- : tx;
348
- });
349
- }
350
- /**
351
- * Helper method to build the steps for a routed swap, ensuring proper token ordering
352
- * through the path segments
353
- */
354
- buildSteps(tokenIn, tokenOut, tradablePair) {
355
- let path = [...tradablePair.path];
356
- if (path[0].assets.includes(tokenOut)) {
357
- path = path.reverse();
358
- }
359
- return path.map((step, idx) => {
360
- const isFirstStep = idx === 0;
361
- const isLastStep = idx === tradablePair.path.length - 1;
362
- const prevStep = idx > 0 ? tradablePair.path[idx - 1] : null;
363
- // For first step, ensure assetIn is tokenIn
364
- // For middle steps, ensure assetIn matches previous step's assetOut
365
- // For last step, ensure assetOut is tokenOut
366
- let [assetIn, assetOut] = step.assets;
367
- if (isFirstStep && assetIn !== tokenIn) {
368
- ;
369
- [assetIn, assetOut] = [assetOut, assetIn];
370
- }
371
- else if (!isFirstStep &&
372
- !isLastStep &&
373
- assetIn !== prevStep.assets[1]) {
374
- ;
375
- [assetIn, assetOut] = [assetOut, assetIn];
376
- }
377
- else if (isLastStep && assetOut !== tokenOut) {
378
- ;
379
- [assetIn, assetOut] = [assetOut, assetIn];
380
- }
381
- return {
382
- exchangeProvider: step.providerAddr,
383
- exchangeId: step.id,
384
- assetIn,
385
- assetOut,
386
- };
387
- });
388
- }
389
- /**
390
- * Returns the mento instance's broker contract
391
- * @returns broker contract
392
- */
393
- getBroker() {
394
- return this.broker;
395
- }
396
- /**
397
- * Finds a tradable pair for the given input and output tokens
398
- * @param tokenIn the input token address
399
- * @param tokenOut the output token address
400
- * @returns the tradable pair containing the path between the tokens
401
- * @throws if no path is found between the tokens
402
- */
403
- findPairForTokens(tokenIn, tokenOut) {
404
- return __awaiter(this, void 0, void 0, function* () {
405
- const allPairs = yield this.getTradablePairsWithPath();
406
- // Find the pair for these tokens
407
- const pair = allPairs.find((p) => {
408
- // Check if the pair connects tokenIn to tokenOut
409
- if (p.assets[0].address === tokenIn && p.assets[1].address === tokenOut) {
410
- return true;
411
- }
412
- // Check reverse direction
413
- if (p.assets[0].address === tokenOut && p.assets[1].address === tokenIn) {
414
- return true;
415
- }
416
- return false;
417
- });
418
- if (!pair) {
419
- throw new Error(`No pair found for tokens ${tokenIn} and ${tokenOut}. They may not have a tradable path.`);
420
- }
421
- return pair;
422
- });
423
- }
424
- /**
425
- * Returns the list of exchanges available in Mento (cached)
426
- * @returns the list of exchanges
427
- */
428
- getExchanges() {
429
- return __awaiter(this, void 0, void 0, function* () {
430
- if (this.exchanges.length > 0) {
431
- return this.exchanges;
432
- }
433
- const exchangeProvidersAddresses = yield this.broker.getExchangeProviders();
434
- const exchanges = (yield Promise.all(exchangeProvidersAddresses.map((a) => this.getExchangesForProvider(a)))).flat();
435
- this.exchanges = exchanges;
436
- return exchanges;
437
- });
438
- }
439
- /**
440
- * Returns the list of exchanges for a given exchange provider address
441
- * @returns list of exchanges
442
- */
443
- getExchangesForProvider(exchangeProviderAddr) {
444
- return __awaiter(this, void 0, void 0, function* () {
445
- const exchangeProvider = mento_core_ts_1.IExchangeProvider__factory.connect(exchangeProviderAddr, this.signerOrProvider);
446
- const exchangesInProvider = yield exchangeProvider.getExchanges();
447
- return exchangesInProvider.map((e) => {
448
- (0, assert_1.strict)(e.assets.length === 2, 'Exchange must have 2 assets');
449
- return {
450
- providerAddr: exchangeProviderAddr,
451
- id: e.exchangeId,
452
- assets: e.assets,
453
- };
454
- });
455
- });
456
- }
457
- /**
458
- * Returns the Mento exchange (if any) for a given pair of tokens
459
- * @param token0 the address of the first token
460
- * @param token1 the address of the second token
461
- * @returns exchange
462
- */
463
- getExchangeForTokens(token0, token1) {
464
- return __awaiter(this, void 0, void 0, function* () {
465
- const exchanges = (yield this.getExchanges()).filter((e) => e.assets.includes(token0) && e.assets.includes(token1));
466
- if (exchanges.length === 0) {
467
- throw Error(`No exchange found for ${token0} and ${token1}`);
468
- }
469
- (0, assert_1.strict)(exchanges.length === 1, `More than one exchange found for ${token0} and ${token1}`);
470
- return exchanges[0];
471
- });
472
- }
473
- /**
474
- * Returns the Mento exchange for a given exchange id
475
- * @param exchangeId the id of the exchange
476
- * @returns the exchange with the given id
477
- */
478
- getExchangeById(exchangeId) {
479
- return __awaiter(this, void 0, void 0, function* () {
480
- const exchanges = (yield this.getExchanges()).filter((e) => e.id === exchangeId);
481
- if (exchanges.length === 0) {
482
- throw Error(`No exchange found for id ${exchangeId}`);
483
- }
484
- (0, assert_1.strict)(exchanges.length === 1, `More than one exchange found with id ${exchangeId}`);
485
- return exchanges[0];
486
- });
487
- }
488
- /**
489
- * Returns whether trading is enabled in the given mode for a given exchange id
490
- * @param exchangeId the id of the exchange
491
- * @param mode the trading mode
492
- * @returns true if trading is enabled in the given mode, false otherwise
493
- */
494
- isTradingEnabled(exchangeId) {
495
- return __awaiter(this, void 0, void 0, function* () {
496
- const exchange = yield this.getExchangeById(exchangeId);
497
- const biPoolManager = mento_core_ts_1.BiPoolManager__factory.connect(exchange.providerAddr, this.signerOrProvider);
498
- const [breakerBoxAddr, exchangeConfig] = yield Promise.all([
499
- biPoolManager.breakerBox(),
500
- biPoolManager.getPoolExchange(exchangeId),
501
- ]);
502
- const breakerBox = mento_core_ts_1.IBreakerBox__factory.connect(breakerBoxAddr, this.signerOrProvider);
503
- const currentMode = yield breakerBox.getRateFeedTradingMode(exchangeConfig.config.referenceRateFeedID);
504
- const BI_DIRECTIONAL_TRADING_MODE = 0;
505
- return currentMode == BI_DIRECTIONAL_TRADING_MODE;
506
- });
507
- }
508
- /**
509
- * Return the trading limits for a given exchange id. Each limit is an object with the following fields:
510
- * asset: the address of the asset with the limit
511
- * maxIn: the maximum amount of the asset that can be sold
512
- * maxOut: the maximum amount of the asset that can be bought
513
- * until: the timestamp until which the limit is valid
514
- * @param exchangeId the id of the exchange
515
- * @returns the list of trading limits
516
- */
517
- getTradingLimits(exchangeId) {
518
- return __awaiter(this, void 0, void 0, function* () {
519
- const exchange = yield this.getExchangeById(exchangeId);
520
- const broker = mento_core_ts_1.Broker__factory.connect(this.broker.address, this.signerOrProvider);
521
- const asset0Limits = yield (0, limits_1.getLimits)(broker, exchangeId, exchange.assets[0]);
522
- const asset1Limits = yield (0, limits_1.getLimits)(broker, exchangeId, exchange.assets[1]);
523
- return asset0Limits.concat(asset1Limits);
524
- });
525
- }
526
- /**
527
- * Returns the trading limits configurations for a given exchange id
528
- * @param exchangeId the id of the exchange
529
- * @returns the trading limits configuration
530
- */
531
- getTradingLimitConfig(exchangeId) {
532
- return __awaiter(this, void 0, void 0, function* () {
533
- const exchange = yield this.getExchangeById(exchangeId);
534
- const broker = mento_core_ts_1.Broker__factory.connect(this.broker.address, this.signerOrProvider);
535
- const cfgs = [];
536
- for (const asset of exchange.assets) {
537
- const limitCfg = yield (0, limits_1.getLimitsConfig)(broker, exchangeId, asset);
538
- const isLimitConfigured = limitCfg.flags > 0;
539
- if (isLimitConfigured) {
540
- cfgs.push(limitCfg);
541
- }
542
- }
543
- return cfgs;
544
- });
545
- }
546
- /**
547
- * Returns the trading limits state for a given exchange id
548
- * @param exchangeId the id of the exchange
549
- * @returns the trading limits state
550
- */
551
- getTradingLimitState(exchangeId) {
552
- return __awaiter(this, void 0, void 0, function* () {
553
- const broker = mento_core_ts_1.Broker__factory.connect(this.broker.address, this.signerOrProvider);
554
- const configuredLimitCfgs = yield this.getTradingLimitConfig(exchangeId);
555
- return yield Promise.all(configuredLimitCfgs.map((cfg) => __awaiter(this, void 0, void 0, function* () { return yield (0, limits_1.getLimitsState)(broker, exchangeId, cfg.asset); })));
556
- });
557
- }
558
- }
559
- exports.Mento = Mento;