@mento-protocol/mento-sdk 1.17.1-beta.0 → 1.18.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 (118) hide show
  1. package/README.md +19 -19
  2. package/package.json +1 -1
  3. package/dist/cjs/ChainClient.d.ts +0 -9
  4. package/dist/cjs/ChainClient.js +0 -58
  5. package/dist/cjs/TestChainClient.d.ts +0 -7
  6. package/dist/cjs/TestChainClient.js +0 -41
  7. package/dist/cjs/constants/addresses.d.ts +0 -4
  8. package/dist/cjs/constants/addresses.js +0 -59
  9. package/dist/cjs/constants/currencies.d.ts +0 -5
  10. package/dist/cjs/constants/currencies.js +0 -29
  11. package/dist/cjs/constants/index.d.ts +0 -4
  12. package/dist/cjs/constants/index.js +0 -20
  13. package/dist/cjs/constants/tokens.11142220.d.ts +0 -2
  14. package/dist/cjs/constants/tokens.11142220.js +0 -128
  15. package/dist/cjs/constants/tokens.42220.d.ts +0 -2
  16. package/dist/cjs/constants/tokens.42220.js +0 -128
  17. package/dist/cjs/constants/tokens.d.ts +0 -65
  18. package/dist/cjs/constants/tokens.js +0 -170
  19. package/dist/cjs/constants/tradablePairs.11142220.d.ts +0 -2
  20. package/dist/cjs/constants/tradablePairs.11142220.js +0 -7391
  21. package/dist/cjs/constants/tradablePairs.42220.d.ts +0 -2
  22. package/dist/cjs/constants/tradablePairs.42220.js +0 -7391
  23. package/dist/cjs/constants/tradablePairs.d.ts +0 -16
  24. package/dist/cjs/constants/tradablePairs.js +0 -53
  25. package/dist/cjs/enums/chainId.d.ts +0 -4
  26. package/dist/cjs/enums/chainId.js +0 -8
  27. package/dist/cjs/enums/index.d.ts +0 -3
  28. package/dist/cjs/enums/index.js +0 -19
  29. package/dist/cjs/enums/proposalState.d.ts +0 -10
  30. package/dist/cjs/enums/proposalState.js +0 -14
  31. package/dist/cjs/enums/tradingMode.d.ts +0 -17
  32. package/dist/cjs/enums/tradingMode.js +0 -21
  33. package/dist/cjs/governance.d.ts +0 -62
  34. package/dist/cjs/governance.js +0 -151
  35. package/dist/cjs/index.d.ts +0 -7
  36. package/dist/cjs/index.js +0 -23
  37. package/dist/cjs/interfaces/IChainClient.d.ts +0 -6
  38. package/dist/cjs/interfaces/IChainClient.js +0 -2
  39. package/dist/cjs/interfaces/index.d.ts +0 -4
  40. package/dist/cjs/interfaces/index.js +0 -20
  41. package/dist/cjs/interfaces/tradingLimit.d.ts +0 -7
  42. package/dist/cjs/interfaces/tradingLimit.js +0 -2
  43. package/dist/cjs/interfaces/tradingLimitsConfig.d.ts +0 -10
  44. package/dist/cjs/interfaces/tradingLimitsConfig.js +0 -2
  45. package/dist/cjs/interfaces/tradingLimitsState.d.ts +0 -9
  46. package/dist/cjs/interfaces/tradingLimitsState.js +0 -2
  47. package/dist/cjs/limits.d.ts +0 -33
  48. package/dist/cjs/limits.js +0 -130
  49. package/dist/cjs/mento.d.ts +0 -287
  50. package/dist/cjs/mento.js +0 -699
  51. package/dist/cjs/routeUtils.d.ts +0 -304
  52. package/dist/cjs/routeUtils.js +0 -372
  53. package/dist/cjs/types/contractAddressMap.d.ts +0 -4
  54. package/dist/cjs/types/contractAddressMap.js +0 -2
  55. package/dist/cjs/types/contractAddresses.d.ts +0 -23
  56. package/dist/cjs/types/contractAddresses.js +0 -2
  57. package/dist/cjs/types/index.d.ts +0 -2
  58. package/dist/cjs/types/index.js +0 -18
  59. package/dist/cjs/utils.d.ts +0 -80
  60. package/dist/cjs/utils.js +0 -177
  61. package/dist/esm/ChainClient.d.ts +0 -9
  62. package/dist/esm/ChainClient.js +0 -54
  63. package/dist/esm/TestChainClient.d.ts +0 -7
  64. package/dist/esm/TestChainClient.js +0 -37
  65. package/dist/esm/constants/addresses.d.ts +0 -4
  66. package/dist/esm/constants/addresses.js +0 -55
  67. package/dist/esm/constants/currencies.d.ts +0 -5
  68. package/dist/esm/constants/currencies.js +0 -26
  69. package/dist/esm/constants/index.d.ts +0 -4
  70. package/dist/esm/constants/index.js +0 -4
  71. package/dist/esm/constants/tokens.11142220.d.ts +0 -2
  72. package/dist/esm/constants/tokens.11142220.js +0 -125
  73. package/dist/esm/constants/tokens.42220.d.ts +0 -2
  74. package/dist/esm/constants/tokens.42220.js +0 -125
  75. package/dist/esm/constants/tokens.d.ts +0 -65
  76. package/dist/esm/constants/tokens.js +0 -142
  77. package/dist/esm/constants/tradablePairs.11142220.d.ts +0 -2
  78. package/dist/esm/constants/tradablePairs.11142220.js +0 -7388
  79. package/dist/esm/constants/tradablePairs.42220.d.ts +0 -2
  80. package/dist/esm/constants/tradablePairs.42220.js +0 -7388
  81. package/dist/esm/constants/tradablePairs.d.ts +0 -16
  82. package/dist/esm/constants/tradablePairs.js +0 -26
  83. package/dist/esm/enums/chainId.d.ts +0 -4
  84. package/dist/esm/enums/chainId.js +0 -5
  85. package/dist/esm/enums/index.d.ts +0 -3
  86. package/dist/esm/enums/index.js +0 -3
  87. package/dist/esm/enums/proposalState.d.ts +0 -10
  88. package/dist/esm/enums/proposalState.js +0 -11
  89. package/dist/esm/enums/tradingMode.d.ts +0 -17
  90. package/dist/esm/enums/tradingMode.js +0 -18
  91. package/dist/esm/governance.d.ts +0 -62
  92. package/dist/esm/governance.js +0 -147
  93. package/dist/esm/index.d.ts +0 -7
  94. package/dist/esm/index.js +0 -7
  95. package/dist/esm/interfaces/IChainClient.d.ts +0 -6
  96. package/dist/esm/interfaces/IChainClient.js +0 -1
  97. package/dist/esm/interfaces/index.d.ts +0 -4
  98. package/dist/esm/interfaces/index.js +0 -4
  99. package/dist/esm/interfaces/tradingLimit.d.ts +0 -7
  100. package/dist/esm/interfaces/tradingLimit.js +0 -1
  101. package/dist/esm/interfaces/tradingLimitsConfig.d.ts +0 -10
  102. package/dist/esm/interfaces/tradingLimitsConfig.js +0 -1
  103. package/dist/esm/interfaces/tradingLimitsState.d.ts +0 -9
  104. package/dist/esm/interfaces/tradingLimitsState.js +0 -1
  105. package/dist/esm/limits.d.ts +0 -33
  106. package/dist/esm/limits.js +0 -123
  107. package/dist/esm/mento.d.ts +0 -287
  108. package/dist/esm/mento.js +0 -671
  109. package/dist/esm/routeUtils.d.ts +0 -304
  110. package/dist/esm/routeUtils.js +0 -362
  111. package/dist/esm/types/contractAddressMap.d.ts +0 -4
  112. package/dist/esm/types/contractAddressMap.js +0 -1
  113. package/dist/esm/types/contractAddresses.d.ts +0 -23
  114. package/dist/esm/types/contractAddresses.js +0 -1
  115. package/dist/esm/types/index.d.ts +0 -2
  116. package/dist/esm/types/index.js +0 -2
  117. package/dist/esm/utils.d.ts +0 -80
  118. package/dist/esm/utils.js +0 -162
package/dist/cjs/mento.js DELETED
@@ -1,699 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
25
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
- return new (P || (P = Promise))(function (resolve, reject) {
28
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
30
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
31
- step((generator = generator.apply(thisArg, _arguments || [])).next());
32
- });
33
- };
34
- Object.defineProperty(exports, "__esModule", { value: true });
35
- exports.Mento = exports.TokenSymbol = void 0;
36
- const mento_core_ts_1 = require("@mento-protocol/mento-core-ts");
37
- const ethers_1 = require("ethers");
38
- const limits_1 = require("./limits");
39
- const utils_1 = require("./utils");
40
- const assert_1 = require("assert");
41
- const mento_router_ts_1 = require("mento-router-ts");
42
- const addresses_1 = require("./constants/addresses");
43
- const tradablePairs_1 = require("./constants/tradablePairs");
44
- const enums_1 = require("./enums");
45
- const routeUtils_1 = require("./routeUtils");
46
- // Re-export TokenSymbol for use in auto-generated files and consuming packages
47
- var tokens_1 = require("./constants/tokens");
48
- Object.defineProperty(exports, "TokenSymbol", { enumerable: true, get: function () { return tokens_1.TokenSymbol; } });
49
- class Mento {
50
- /**
51
- * This constructor is private, use the static create or createWithParams methods
52
- * to create a new Mento instance
53
- * @param signerOrProvider an ethers provider or connected signer
54
- * @param brokerAddress the address of the broker contract
55
- * @param exchanges exchange data for the broker
56
- */
57
- constructor(signerOrProvider, brokerAddress, routerAddress, exchanges) {
58
- this.cachedChainId = null;
59
- this.signerOrProvider = signerOrProvider;
60
- this.broker = mento_core_ts_1.IBroker__factory.connect(brokerAddress, signerOrProvider);
61
- this.router = mento_router_ts_1.IMentoRouter__factory.connect(routerAddress, signerOrProvider);
62
- this.exchanges = exchanges || [];
63
- }
64
- /**
65
- * Creates a new Mento object instance.
66
- * When constructed with only a Provider only read-only operations are supported
67
- * @param signerOrProvider an ethers signer or provider. A signer is required to execute swaps
68
- * @returns a new Mento object instance
69
- */
70
- static create(signerOrProvider) {
71
- return __awaiter(this, void 0, void 0, function* () {
72
- (0, utils_1.validateSignerOrProvider)(signerOrProvider);
73
- const chainId = yield (0, utils_1.getChainId)(signerOrProvider);
74
- const instance = new Mento(signerOrProvider, (0, addresses_1.getAddress)('Broker', chainId), (0, addresses_1.getAddress)('MentoRouter', chainId));
75
- instance.cachedChainId = chainId;
76
- return instance;
77
- });
78
- }
79
- /**
80
- * Create a new Mento object instance given a broker address and optional exchanges data
81
- * When constructed with a Provider, only read-only operations are supported
82
- * @param signerOrProvider an ethers signer or provider. A signer is required to execute swaps
83
- * @param brokerAddr the address of the broker contract
84
- * @param exchanges the exchanges data for the broker
85
- * @returns a new Mento object instance
86
- */
87
- static createWithParams(signerOrProvider, brokerAddr, routerAddr, exchanges) {
88
- (0, utils_1.validateSignerOrProvider)(signerOrProvider);
89
- return new Mento(signerOrProvider, brokerAddr, routerAddr, exchanges);
90
- }
91
- /**
92
- * Returns a new Mento instance connected to the given signer
93
- * @param signer an ethers signer
94
- * @returns new Mento object instance
95
- */
96
- connectSigner(signer) {
97
- (0, utils_1.validateSigner)(signer);
98
- return new Mento(signer, this.broker.address, this.router.address, this.exchanges);
99
- }
100
- /**
101
- * Get tradable pairs for backwards compatibility
102
- * @returns an array of Asset pairs
103
- */
104
- getTradablePairs({ cached = true, } = {}) {
105
- return __awaiter(this, void 0, void 0, function* () {
106
- return (yield this.getTradablePairsWithPath({ cached })).map((pair) => pair.assets);
107
- });
108
- }
109
- /**
110
- * Returns a list of all tradable pairs on Mento via direct exchanges.
111
- * Each pair is represented using the TradablePair interface, with its id
112
- * (a concatenation of the two asset symbols in alphabetical order),
113
- * the two Asset objects, and a path (an array with a single direct exchange hop).
114
- * @returns An array of direct TradablePair objects.
115
- */
116
- getDirectPairs() {
117
- return __awaiter(this, void 0, void 0, function* () {
118
- const exchanges = yield this.getExchanges();
119
- // Map from pair id (symbol-symbol) to its TradablePair
120
- const directPairsMap = new Map();
121
- for (const exchange of exchanges) {
122
- const [token0, token1] = exchange.assets;
123
- const [symbol0, symbol1] = yield Promise.all([
124
- (0, utils_1.getSymbolFromTokenAddress)(token0, this.signerOrProvider),
125
- (0, utils_1.getSymbolFromTokenAddress)(token1, this.signerOrProvider),
126
- ]);
127
- // Determine canonical order by symbol
128
- let assets;
129
- let pairId;
130
- if (symbol0 <= symbol1) {
131
- assets = [
132
- { address: token0, symbol: symbol0 },
133
- { address: token1, symbol: symbol1 },
134
- ];
135
- pairId = `${symbol0}-${symbol1}`;
136
- }
137
- else {
138
- assets = [
139
- { address: token1, symbol: symbol1 },
140
- { address: token0, symbol: symbol0 },
141
- ];
142
- pairId = `${symbol1}-${symbol0}`;
143
- }
144
- const pathEntry = {
145
- providerAddr: exchange.providerAddr,
146
- id: exchange.id,
147
- assets: exchange.assets,
148
- };
149
- if (directPairsMap.has(pairId)) {
150
- directPairsMap.get(pairId).path.push(pathEntry);
151
- }
152
- else {
153
- directPairsMap.set(pairId, { id: pairId, assets, path: [pathEntry] });
154
- }
155
- }
156
- return Array.from(directPairsMap.values());
157
- });
158
- }
159
- /**
160
- * Returns a list of all tradable pairs on Mento, including those achievable
161
- * via two-hop routes. For two-hop pairs, the path will contain two exchange hops.
162
- * Each TradablePair contains an id (the concatenation of the two asset symbols in alphabetical order),
163
- * the two Asset objects, and an array of exchange details for each hop.
164
- * @param options - Optional parameters
165
- * @param options.cached - Whether to use cached data (default: true)
166
- * @param options.returnAllRoutes - Whether to return all possible routes or just the best one per pair (default: false)
167
- * @returns An array of TradablePair objects representing available trade routes.
168
- */
169
- getTradablePairsWithPath({ cached = true, returnAllRoutes = false, } = {}) {
170
- return __awaiter(this, void 0, void 0, function* () {
171
- // Try to get cached data first
172
- if (cached) {
173
- const cachedPairs = yield this.getCachedTradablePairs();
174
- if (cachedPairs)
175
- return cachedPairs;
176
- }
177
- // Generate routes from scratch
178
- const directPairs = yield this.getDirectPairs();
179
- const connectivity = (0, routeUtils_1.buildConnectivityStructures)(directPairs);
180
- const allRoutes = (0, routeUtils_1.generateAllRoutes)(connectivity);
181
- return (0, routeUtils_1.selectOptimalRoutes)(allRoutes, returnAllRoutes, connectivity.addrToSymbol);
182
- });
183
- }
184
- /**
185
- * Attempts to get cached tradable pairs for the current chain
186
- */
187
- getCachedTradablePairs() {
188
- return __awaiter(this, void 0, void 0, function* () {
189
- const chainId = yield (0, utils_1.getChainId)(this.signerOrProvider);
190
- return yield (0, tradablePairs_1.getCachedTradablePairs)(chainId);
191
- });
192
- }
193
- /**
194
- * Returns a list of all unique tokens available on the current chain.
195
- * This method is synchronous and uses pre-cached token data.
196
- * For runtime fetching from the blockchain, use getTokensAsync().
197
- *
198
- * @returns An array of unique Token objects from the static cache.
199
- * @throws Error if no cached tokens are available for the current chain or if chainId is not yet initialized
200
- */
201
- getTokens() {
202
- if (!this.cachedChainId) {
203
- throw new Error('Chain ID not yet initialized. Use Mento.create() to initialize the SDK, or use getTokensAsync() instead.');
204
- }
205
- // eslint-disable-next-line @typescript-eslint/no-var-requires
206
- const { getCachedTokensSync } = require('./constants/tokens');
207
- return Array.from(getCachedTokensSync(this.cachedChainId));
208
- }
209
- /**
210
- * Fetches token metadata from the blockchain at runtime.
211
- * This method is async and makes blockchain calls to get fresh token data.
212
- * For synchronous access using cached data, use getTokens().
213
- *
214
- * @param options - Optional parameters
215
- * @param options.cached - Whether to use cached data (default: true).
216
- * If true, attempts to load from static cache first.
217
- * @returns A Promise resolving to an array of unique Token objects.
218
- */
219
- getTokensAsync({ cached = true, } = {}) {
220
- return __awaiter(this, void 0, void 0, function* () {
221
- // If cached is true, try to use the static cache first
222
- if (cached) {
223
- const { getCachedTokens } = yield Promise.resolve().then(() => __importStar(require('./constants/tokens')));
224
- const chainId = yield this.chainId();
225
- const cachedTokens = yield getCachedTokens(chainId);
226
- if (cachedTokens) {
227
- return Array.from(cachedTokens);
228
- }
229
- }
230
- // Fall back to fetching from blockchain
231
- const tradablePairs = yield this.getTradablePairsWithPath({ cached: false });
232
- // Collect unique token addresses
233
- const uniqueAddresses = new Set(tradablePairs.flatMap((pair) => pair.assets.map((asset) => asset.address)));
234
- // Fetch token metadata for each unique address
235
- const tokens = yield Promise.all(Array.from(uniqueAddresses).map((address) => __awaiter(this, void 0, void 0, function* () {
236
- const [symbol, name, decimals] = yield Promise.all([
237
- (0, utils_1.getSymbolFromTokenAddress)(address, this.signerOrProvider),
238
- (0, utils_1.getNameFromTokenAddress)(address, this.signerOrProvider),
239
- (0, utils_1.getDecimalsFromTokenAddress)(address, this.signerOrProvider),
240
- ]);
241
- return {
242
- address,
243
- symbol,
244
- name,
245
- decimals,
246
- };
247
- })));
248
- // Sort by symbol
249
- return tokens.sort((a, b) => a.symbol.localeCompare(b.symbol));
250
- });
251
- }
252
- /**
253
- * Returns the amount of tokenIn to be sold to buy amountOut of tokenOut.
254
- * If the provided tradablePair has a single (direct) pricing path, then direct pricing is used.
255
- * Otherwise, routed pricing via the MentoRouter is applied.
256
- * @param tokenIn the token to be sold
257
- * @param tokenOut the token to be bought
258
- * @param amountOut the desired amount of tokenOut to be obtained
259
- * @param tradablePair the TradablePair object containing the pricing path information
260
- * @returns the amount of tokenIn to be sold
261
- */
262
- getAmountIn(tokenIn, tokenOut, amountOut, tradablePair) {
263
- return __awaiter(this, void 0, void 0, function* () {
264
- if (!tradablePair) {
265
- tradablePair = yield this.findPairForTokens(tokenIn, tokenOut);
266
- }
267
- if (tradablePair.path.length === 1) {
268
- return this.getAmountInDirect(tokenIn, tokenOut, amountOut, tradablePair);
269
- }
270
- else {
271
- return this.getAmountInRouted(tokenIn, tokenOut, amountOut, tradablePair);
272
- }
273
- });
274
- }
275
- /**
276
- * Returns the amount of tokenOut to be bought by selling amountIn of tokenIn.
277
- * If the provided tradablePair has a single (direct) pricing path, then direct pricing is used.
278
- * Otherwise, routed pricing via the MentoRouter is applied.
279
- * @param tokenIn the token to be sold
280
- * @param tokenOut the token to be bought
281
- * @param amountIn the amount of tokenIn to be sold
282
- * @param tradablePair the TradablePair object containing the pricing path information
283
- * @returns the amount of tokenOut to be bought
284
- */
285
- getAmountOut(tokenIn, tokenOut, amountIn, tradablePair) {
286
- return __awaiter(this, void 0, void 0, function* () {
287
- if (!tradablePair) {
288
- tradablePair = yield this.findPairForTokens(tokenIn, tokenOut);
289
- }
290
- if (tradablePair.path.length === 1) {
291
- return this.getAmountOutDirect(tokenIn, tokenOut, amountIn, tradablePair);
292
- }
293
- else {
294
- return this.getAmountOutRouted(tokenIn, tokenOut, amountIn, tradablePair);
295
- }
296
- });
297
- }
298
- /**
299
- * Internal method for direct pricing: retrieves the exchange for the given tokens
300
- * and returns the amountIn using the broker.
301
- */
302
- getAmountInDirect(tokenIn, tokenOut, amountOut, tradablePair) {
303
- return __awaiter(this, void 0, void 0, function* () {
304
- return this.broker.getAmountIn(tradablePair.path[0].providerAddr, tradablePair.path[0].id, tokenIn, tokenOut, amountOut);
305
- });
306
- }
307
- /**
308
- * Internal method for direct pricing: retrieves the exchange for the given tokens
309
- * and returns the amountOut using the broker.
310
- */
311
- getAmountOutDirect(tokenIn, tokenOut, amountIn, tradablePair) {
312
- return __awaiter(this, void 0, void 0, function* () {
313
- return this.broker.getAmountOut(tradablePair.path[0].providerAddr, tradablePair.path[0].id, tokenIn, tokenOut, amountIn);
314
- });
315
- }
316
- /**
317
- * Internal method for routed pricing: uses the MentoRouter to determine the required tokenIn
318
- * for obtaining amountOut through a multi-hop route specified in tradablePair.path.
319
- */
320
- getAmountInRouted(tokenIn, tokenOut, amountOut, tradablePair) {
321
- return __awaiter(this, void 0, void 0, function* () {
322
- const steps = this.buildSteps(tokenIn, tokenOut, tradablePair);
323
- return this.router.getAmountIn(amountOut, steps);
324
- });
325
- }
326
- /**
327
- * Internal method for routed pricing: uses the MentoRouter to determine the amountOut
328
- * obtainable by selling amountIn through a multi-hop route specified in tradablePair.path.
329
- */
330
- getAmountOutRouted(tokenIn, tokenOut, amountIn, tradablePair) {
331
- return __awaiter(this, void 0, void 0, function* () {
332
- const steps = this.buildSteps(tokenIn, tokenOut, tradablePair);
333
- return this.router.getAmountOut(amountIn, steps);
334
- });
335
- }
336
- /**
337
- * Increases the broker's trading allowance for the given token
338
- * @param token the token to increase the allowance for
339
- * @param amount the amount to increase the allowance by
340
- * @returns the populated TransactionRequest object
341
- */
342
- increaseTradingAllowance(tokenIn, amount, tradablePair) {
343
- return __awaiter(this, void 0, void 0, function* () {
344
- const spender = !tradablePair || (tradablePair === null || tradablePair === void 0 ? void 0 : tradablePair.path.length) == 1
345
- ? this.broker.address
346
- : this.router.address;
347
- const tx = yield (0, utils_1.increaseAllowance)(tokenIn, spender, amount, this.signerOrProvider);
348
- if (ethers_1.Signer.isSigner(this.signerOrProvider)) {
349
- // The contract call doesn't populate all of the signer fields, so we need an extra call for the signer
350
- return this.signerOrProvider.populateTransaction(tx);
351
- }
352
- else {
353
- return tx;
354
- }
355
- });
356
- }
357
- /**
358
- * Returns a token swap populated tx object with a fixed amount of tokenIn and a minimum amount of tokenOut.
359
- * If the tradablePair contains a single-hop route, a direct swap is executed using swapExactTokensForTokens on the broker.
360
- * Otherwise, a routed swap is executed via the router.
361
- * @param tokenIn the token to be sold
362
- * @param tokenOut the token to be bought
363
- * @param amountIn the amount of tokenIn to be sold
364
- * @param amountOutMin the minimum amount of tokenOut to be bought
365
- * @param tradablePair the tradable pair details to determine routing
366
- * @returns the populated TransactionRequest object
367
- */
368
- swapIn(tokenIn, tokenOut, amountIn, amountOutMin, tradablePair) {
369
- return __awaiter(this, void 0, void 0, function* () {
370
- if (!tradablePair) {
371
- tradablePair = yield this.findPairForTokens(tokenIn, tokenOut);
372
- }
373
- if (tradablePair.path.length === 1) {
374
- return this.swapInDirect(tokenIn, tokenOut, amountIn, amountOutMin);
375
- }
376
- else {
377
- return this.swapInRouted(tokenIn, tokenOut, amountIn, amountOutMin, tradablePair);
378
- }
379
- });
380
- }
381
- swapInDirect(tokenIn, tokenOut, amountIn, amountOutMin) {
382
- return __awaiter(this, void 0, void 0, function* () {
383
- const exchange = yield this.getExchangeForTokens(tokenIn, tokenOut);
384
- const tx = yield this.broker.populateTransaction.swapIn(exchange.providerAddr, exchange.id, tokenIn, tokenOut, amountIn, amountOutMin);
385
- return ethers_1.Signer.isSigner(this.signerOrProvider)
386
- ? this.signerOrProvider.populateTransaction(tx)
387
- : tx;
388
- });
389
- }
390
- swapInRouted(tokenIn, tokenOut, amountIn, amountOutMin, tradablePair) {
391
- return __awaiter(this, void 0, void 0, function* () {
392
- const steps = this.buildSteps(tokenIn, tokenOut, tradablePair);
393
- const tx = yield this.router.populateTransaction.swapExactTokensForTokens(amountIn, amountOutMin, steps);
394
- return ethers_1.Signer.isSigner(this.signerOrProvider)
395
- ? this.signerOrProvider.populateTransaction(tx)
396
- : tx;
397
- });
398
- }
399
- /**
400
- * Returns a token swap populated tx object with a maximum amount of tokenIn and a fixed amount of tokenOut.
401
- * If the tradablePair contains a single-hop route, a direct swap is executed using swapTokensForExactTokens on the broker.
402
- * Otherwise, a routed swap is executed via the router.
403
- * @param tokenIn the token to be sold
404
- * @param tokenOut the token to be bought
405
- * @param amountOut the amount of tokenOut to be bought
406
- * @param amountInMax the maximum amount of tokenIn to be sold
407
- * @returns the populated TransactionRequest object
408
- */
409
- swapOut(tokenIn, tokenOut, amountOut, amountInMax, tradablePair) {
410
- return __awaiter(this, void 0, void 0, function* () {
411
- if (!tradablePair) {
412
- tradablePair = yield this.findPairForTokens(tokenIn, tokenOut);
413
- }
414
- if (tradablePair.path.length === 1) {
415
- return this.swapOutDirect(tokenIn, tokenOut, amountOut, amountInMax);
416
- }
417
- else {
418
- return this.swapOutRouted(tokenIn, tokenOut, amountOut, amountInMax, tradablePair);
419
- }
420
- });
421
- }
422
- swapOutDirect(tokenIn, tokenOut, amountOut, amountInMax) {
423
- return __awaiter(this, void 0, void 0, function* () {
424
- const exchange = yield this.getExchangeForTokens(tokenIn, tokenOut);
425
- const tx = yield this.broker.populateTransaction.swapOut(exchange.providerAddr, exchange.id, tokenIn, tokenOut, amountOut, amountInMax);
426
- return ethers_1.Signer.isSigner(this.signerOrProvider)
427
- ? this.signerOrProvider.populateTransaction(tx)
428
- : tx;
429
- });
430
- }
431
- swapOutRouted(tokenIn, tokenOut, amountOut, amountInMax, tradablePair) {
432
- return __awaiter(this, void 0, void 0, function* () {
433
- const steps = this.buildSteps(tokenIn, tokenOut, tradablePair);
434
- const tx = yield this.router.populateTransaction.swapTokensForExactTokens(amountOut, amountInMax, steps);
435
- return ethers_1.Signer.isSigner(this.signerOrProvider)
436
- ? this.signerOrProvider.populateTransaction(tx)
437
- : tx;
438
- });
439
- }
440
- /**
441
- * Helper method to build the steps for a routed swap, ensuring proper token ordering
442
- * through the path segments
443
- */
444
- buildSteps(tokenIn, tokenOut, tradablePair) {
445
- let path = [...tradablePair.path];
446
- if (path[0].assets.includes(tokenOut)) {
447
- path = path.reverse();
448
- }
449
- return path.map((step, idx) => {
450
- const isFirstStep = idx === 0;
451
- const isLastStep = idx === tradablePair.path.length - 1;
452
- const prevStep = idx > 0 ? tradablePair.path[idx - 1] : null;
453
- // For first step, ensure assetIn is tokenIn
454
- // For middle steps, ensure assetIn matches previous step's assetOut
455
- // For last step, ensure assetOut is tokenOut
456
- let [assetIn, assetOut] = step.assets;
457
- if (isFirstStep && assetIn !== tokenIn) {
458
- ;
459
- [assetIn, assetOut] = [assetOut, assetIn];
460
- }
461
- else if (!isFirstStep &&
462
- !isLastStep &&
463
- assetIn !== prevStep.assets[1]) {
464
- ;
465
- [assetIn, assetOut] = [assetOut, assetIn];
466
- }
467
- else if (isLastStep && assetOut !== tokenOut) {
468
- ;
469
- [assetIn, assetOut] = [assetOut, assetIn];
470
- }
471
- return {
472
- exchangeProvider: step.providerAddr,
473
- exchangeId: step.id,
474
- assetIn,
475
- assetOut,
476
- };
477
- });
478
- }
479
- /**
480
- * Returns the mento instance's broker contract
481
- * @returns broker contract
482
- */
483
- getBroker() {
484
- return this.broker;
485
- }
486
- /**
487
- * Finds a tradable pair for the given input and output tokens
488
- * @param tokenIn the input token address
489
- * @param tokenOut the output token address
490
- * @returns the tradable pair containing the path between the tokens
491
- * @throws if no path is found between the tokens
492
- */
493
- findPairForTokens(tokenIn, tokenOut) {
494
- return __awaiter(this, void 0, void 0, function* () {
495
- const allPairs = yield this.getTradablePairsWithPath();
496
- // Find the pair for these tokens
497
- const pair = allPairs.find((p) => {
498
- // Check if the pair connects tokenIn to tokenOut
499
- if (p.assets[0].address === tokenIn && p.assets[1].address === tokenOut) {
500
- return true;
501
- }
502
- // Check reverse direction
503
- if (p.assets[0].address === tokenOut && p.assets[1].address === tokenIn) {
504
- return true;
505
- }
506
- return false;
507
- });
508
- if (!pair) {
509
- throw new Error(`No pair found for tokens ${tokenIn} and ${tokenOut}. They may not have a tradable path.`);
510
- }
511
- return pair;
512
- });
513
- }
514
- /**
515
- * Returns the list of exchanges available in Mento (cached)
516
- * @returns the list of exchanges
517
- */
518
- getExchanges() {
519
- return __awaiter(this, void 0, void 0, function* () {
520
- if (this.exchanges.length > 0) {
521
- return this.exchanges;
522
- }
523
- const exchangeProvidersAddresses = yield this.broker.getExchangeProviders();
524
- const exchanges = (yield Promise.all(exchangeProvidersAddresses.map((a) => this.getExchangesForProvider(a)))).flat();
525
- this.exchanges = exchanges;
526
- return exchanges;
527
- });
528
- }
529
- /**
530
- * Returns the list of exchanges for a given exchange provider address
531
- * @returns list of exchanges
532
- */
533
- getExchangesForProvider(exchangeProviderAddr) {
534
- return __awaiter(this, void 0, void 0, function* () {
535
- const exchangeProvider = mento_core_ts_1.IExchangeProvider__factory.connect(exchangeProviderAddr, this.signerOrProvider);
536
- const exchangesInProvider = yield exchangeProvider.getExchanges();
537
- return exchangesInProvider.map((e) => {
538
- (0, assert_1.strict)(e.assets.length === 2, 'Exchange must have 2 assets');
539
- return {
540
- providerAddr: exchangeProviderAddr,
541
- id: e.exchangeId,
542
- assets: e.assets,
543
- };
544
- });
545
- });
546
- }
547
- /**
548
- * Returns the Mento exchange (if any) for a given pair of tokens
549
- * @param token0 the address of the first token
550
- * @param token1 the address of the second token
551
- * @returns exchange
552
- */
553
- getExchangeForTokens(token0, token1) {
554
- return __awaiter(this, void 0, void 0, function* () {
555
- const exchanges = (yield this.getExchanges()).filter((e) => e.assets.includes(token0) && e.assets.includes(token1));
556
- if (exchanges.length === 0) {
557
- throw Error(`No exchange found for ${token0} and ${token1}`);
558
- }
559
- (0, assert_1.strict)(exchanges.length === 1, `More than one exchange found for ${token0} and ${token1}`);
560
- return exchanges[0];
561
- });
562
- }
563
- /**
564
- * Returns the Mento exchange for a given exchange id
565
- * @param exchangeId the id of the exchange
566
- * @returns the exchange with the given id
567
- */
568
- getExchangeById(exchangeId) {
569
- return __awaiter(this, void 0, void 0, function* () {
570
- const exchanges = (yield this.getExchanges()).filter((e) => e.id === exchangeId);
571
- if (exchanges.length === 0) {
572
- throw Error(`No exchange found for id ${exchangeId}`);
573
- }
574
- (0, assert_1.strict)(exchanges.length === 1, `More than one exchange found with id ${exchangeId}`);
575
- return exchanges[0];
576
- });
577
- }
578
- /**
579
- * Returns whether trading is enabled in the given mode for a given exchange id
580
- * @param exchangeId the id of the exchange
581
- * @param mode the trading mode
582
- * @returns true if trading is enabled in the given mode, false otherwise
583
- */
584
- isTradingEnabled(exchangeId) {
585
- return __awaiter(this, void 0, void 0, function* () {
586
- const exchange = yield this.getExchangeById(exchangeId);
587
- const biPoolManager = mento_core_ts_1.BiPoolManager__factory.connect(exchange.providerAddr, this.signerOrProvider);
588
- const [breakerBoxAddr, exchangeConfig] = yield Promise.all([
589
- biPoolManager.breakerBox(),
590
- biPoolManager.getPoolExchange(exchangeId),
591
- ]);
592
- const breakerBox = mento_core_ts_1.IBreakerBox__factory.connect(breakerBoxAddr, this.signerOrProvider);
593
- const currentMode = yield breakerBox.getRateFeedTradingMode(exchangeConfig.config.referenceRateFeedID);
594
- const BI_DIRECTIONAL_TRADING_MODE = 0;
595
- return currentMode == BI_DIRECTIONAL_TRADING_MODE;
596
- });
597
- }
598
- /**
599
- * Returns the current trading mode for a given rate feed
600
- * @param rateFeedId the address of the rate feed
601
- * @returns the current trading mode (BIDIRECTIONAL, HALTED, or DISABLED)
602
- */
603
- getRateFeedTradingMode(rateFeedId) {
604
- return __awaiter(this, void 0, void 0, function* () {
605
- const breakerBoxAddr = yield this.getAddress('BreakerBox');
606
- const breakerBox = mento_core_ts_1.IBreakerBox__factory.connect(breakerBoxAddr, this.signerOrProvider);
607
- const currentMode = yield breakerBox.getRateFeedTradingMode(rateFeedId);
608
- return currentMode;
609
- });
610
- }
611
- /**
612
- * Checks if a trading pair is currently tradable (i.e., all rate feeds in the path are in BIDIRECTIONAL mode)
613
- * For multi-hop routes (e.g., CELO → cUSD → USDT), checks that all intermediate rate feeds are tradable
614
- * @param tokenIn the address of the token to sell
615
- * @param tokenOut the address of the token to buy
616
- * @returns true if the pair is tradable (all rate feeds in BIDIRECTIONAL mode), false otherwise
617
- */
618
- isPairTradable(tokenIn, tokenOut) {
619
- return __awaiter(this, void 0, void 0, function* () {
620
- // Find the tradable pair (which includes the routing path)
621
- const pair = yield this.findPairForTokens(tokenIn, tokenOut);
622
- // For each hop in the path, check if the rate feed is tradable
623
- const biPoolManager = mento_core_ts_1.BiPoolManager__factory.connect(pair.path[0].providerAddr, this.signerOrProvider);
624
- // Get all rate feed IDs for each hop in the path
625
- const rateFeedChecks = yield Promise.all(pair.path.map((hop) => __awaiter(this, void 0, void 0, function* () {
626
- const exchangeConfig = yield biPoolManager.getPoolExchange(hop.id);
627
- const rateFeedId = exchangeConfig.config.referenceRateFeedID;
628
- const tradingMode = yield this.getRateFeedTradingMode(rateFeedId);
629
- return tradingMode === enums_1.TradingMode.BIDIRECTIONAL;
630
- })));
631
- // All rate feeds must be in BIDIRECTIONAL mode for the pair to be tradable
632
- return rateFeedChecks.every((isTradable) => isTradable);
633
- });
634
- }
635
- /**
636
- * Return the trading limits for a given exchange id. Each limit is an object with the following fields:
637
- * asset: the address of the asset with the limit
638
- * maxIn: the maximum amount of the asset that can be sold
639
- * maxOut: the maximum amount of the asset that can be bought
640
- * until: the timestamp until which the limit is valid
641
- * @param exchangeId the id of the exchange
642
- * @returns the list of trading limits
643
- */
644
- getTradingLimits(exchangeId) {
645
- return __awaiter(this, void 0, void 0, function* () {
646
- const exchange = yield this.getExchangeById(exchangeId);
647
- const broker = mento_core_ts_1.Broker__factory.connect(this.broker.address, this.signerOrProvider);
648
- const asset0Limits = yield (0, limits_1.getLimits)(broker, exchangeId, exchange.assets[0]);
649
- const asset1Limits = yield (0, limits_1.getLimits)(broker, exchangeId, exchange.assets[1]);
650
- return asset0Limits.concat(asset1Limits);
651
- });
652
- }
653
- /**
654
- * Returns the trading limits configurations for a given exchange id
655
- * @param exchangeId the id of the exchange
656
- * @returns the trading limits configuration
657
- */
658
- getTradingLimitConfig(exchangeId) {
659
- return __awaiter(this, void 0, void 0, function* () {
660
- const exchange = yield this.getExchangeById(exchangeId);
661
- const broker = mento_core_ts_1.Broker__factory.connect(this.broker.address, this.signerOrProvider);
662
- const cfgs = [];
663
- for (const asset of exchange.assets) {
664
- const limitCfg = yield (0, limits_1.getLimitsConfig)(broker, exchangeId, asset);
665
- const isLimitConfigured = limitCfg.flags > 0;
666
- if (isLimitConfigured) {
667
- cfgs.push(limitCfg);
668
- }
669
- }
670
- return cfgs;
671
- });
672
- }
673
- /**
674
- * Returns the trading limits state for a given exchange id
675
- * @param exchangeId the id of the exchange
676
- * @returns the trading limits state
677
- */
678
- getTradingLimitState(exchangeId) {
679
- return __awaiter(this, void 0, void 0, function* () {
680
- const broker = mento_core_ts_1.Broker__factory.connect(this.broker.address, this.signerOrProvider);
681
- const configuredLimitCfgs = yield this.getTradingLimitConfig(exchangeId);
682
- return yield Promise.all(configuredLimitCfgs.map((cfg) => __awaiter(this, void 0, void 0, function* () { return yield (0, limits_1.getLimitsState)(broker, exchangeId, cfg.asset); })));
683
- });
684
- }
685
- getAddress(identifier) {
686
- return __awaiter(this, void 0, void 0, function* () {
687
- return (0, addresses_1.getAddress)(identifier, yield this.chainId());
688
- });
689
- }
690
- chainId() {
691
- return __awaiter(this, void 0, void 0, function* () {
692
- if (!this.cachedChainId) {
693
- this.cachedChainId = yield (0, utils_1.getChainId)(this.signerOrProvider);
694
- }
695
- return this.cachedChainId;
696
- });
697
- }
698
- }
699
- exports.Mento = Mento;