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