@mento-protocol/mento-sdk 1.0.0 → 1.0.2
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.
- package/dist/cjs/constants/addresses.d.ts +2 -0
- package/dist/cjs/constants/addresses.js +23 -8
- package/dist/cjs/constants/tradablePairs.d.ts +3 -0
- package/dist/cjs/constants/tradablePairs.js +3762 -0
- package/dist/cjs/mento.d.ts +88 -16
- package/dist/cjs/mento.js +337 -48
- package/dist/cjs/types/contractAddresses.d.ts +1 -0
- package/dist/cjs/utils.d.ts +6 -0
- package/dist/cjs/utils.js +15 -1
- package/dist/esm/constants/addresses.d.ts +2 -0
- package/dist/esm/constants/addresses.js +21 -7
- package/dist/esm/constants/tradablePairs.d.ts +3 -0
- package/dist/esm/constants/tradablePairs.js +3758 -0
- package/dist/esm/mento.d.ts +88 -16
- package/dist/esm/mento.js +338 -49
- package/dist/esm/types/contractAddresses.d.ts +1 -0
- package/dist/esm/utils.d.ts +6 -0
- package/dist/esm/utils.js +13 -0
- package/package.json +3 -2
package/dist/cjs/mento.d.ts
CHANGED
|
@@ -10,9 +10,19 @@ export interface Asset {
|
|
|
10
10
|
address: Address;
|
|
11
11
|
symbol: string;
|
|
12
12
|
}
|
|
13
|
+
export interface TradablePair {
|
|
14
|
+
id: string;
|
|
15
|
+
assets: [Asset, Asset];
|
|
16
|
+
path: Array<{
|
|
17
|
+
providerAddr: Address;
|
|
18
|
+
id: string;
|
|
19
|
+
assets: [Address, Address];
|
|
20
|
+
}>;
|
|
21
|
+
}
|
|
13
22
|
export declare class Mento {
|
|
14
23
|
private readonly signerOrProvider;
|
|
15
24
|
private readonly broker;
|
|
25
|
+
private readonly router;
|
|
16
26
|
private exchanges;
|
|
17
27
|
/**
|
|
18
28
|
* This constructor is private, use the static create or createWithParams methods
|
|
@@ -37,7 +47,7 @@ export declare class Mento {
|
|
|
37
47
|
* @param exchanges the exchanges data for the broker
|
|
38
48
|
* @returns a new Mento object instance
|
|
39
49
|
*/
|
|
40
|
-
static createWithParams(signerOrProvider: Signer | providers.Provider, brokerAddr: Address, exchanges?: Exchange[]): Mento;
|
|
50
|
+
static createWithParams(signerOrProvider: Signer | providers.Provider, brokerAddr: Address, routerAddr: Address, exchanges?: Exchange[]): Mento;
|
|
41
51
|
/**
|
|
42
52
|
* Returns a new Mento instance connected to the given signer
|
|
43
53
|
* @param signer an ethers signer
|
|
@@ -45,58 +55,120 @@ export declare class Mento {
|
|
|
45
55
|
*/
|
|
46
56
|
connectSigner(signer: Signer): Mento;
|
|
47
57
|
/**
|
|
48
|
-
*
|
|
49
|
-
* @returns
|
|
58
|
+
* Get tradable pairs for backwards compatibility
|
|
59
|
+
* @returns an array of Asset pairs
|
|
60
|
+
*/
|
|
61
|
+
getTradablePairs(cached?: boolean): Promise<[Asset, Asset][]>;
|
|
62
|
+
/**
|
|
63
|
+
* Returns a list of all tradable pairs on Mento via direct exchanges.
|
|
64
|
+
* Each pair is represented using the TradablePair interface, with its id
|
|
65
|
+
* (a concatenation of the two asset symbols in alphabetical order),
|
|
66
|
+
* the two Asset objects, and a path (an array with a single direct exchange hop).
|
|
67
|
+
* @returns An array of direct TradablePair objects.
|
|
68
|
+
*/
|
|
69
|
+
getDirectPairs(): Promise<TradablePair[]>;
|
|
70
|
+
/**
|
|
71
|
+
* Returns a list of all tradable pairs on Mento, including those achievable
|
|
72
|
+
* via two-hop routes. For two-hop pairs, the path will contain two exchange hops.
|
|
73
|
+
* Each TradablePair contains an id (the concatenation of the two asset symbols in alphabetical order),
|
|
74
|
+
* the two Asset objects, and an array of exchange details for each hop.
|
|
75
|
+
* @returns An array of TradablePair objects representing available trade routes.
|
|
50
76
|
*/
|
|
51
|
-
|
|
77
|
+
getTradablePairsWithPath(cached?: boolean): Promise<readonly TradablePair[]>;
|
|
52
78
|
/**
|
|
53
|
-
* Returns the amount of tokenIn to be sold to buy amountOut of tokenOut
|
|
79
|
+
* Returns the amount of tokenIn to be sold to buy amountOut of tokenOut.
|
|
80
|
+
* If the provided tradablePair has a single (direct) pricing path, then direct pricing is used.
|
|
81
|
+
* Otherwise, routed pricing via the MentoRouter is applied.
|
|
54
82
|
* @param tokenIn the token to be sold
|
|
55
83
|
* @param tokenOut the token to be bought
|
|
56
|
-
* @param amountOut the amount of tokenOut to be
|
|
84
|
+
* @param amountOut the desired amount of tokenOut to be obtained
|
|
85
|
+
* @param tradablePair the TradablePair object containing the pricing path information
|
|
57
86
|
* @returns the amount of tokenIn to be sold
|
|
58
87
|
*/
|
|
59
|
-
getAmountIn(tokenIn: Address, tokenOut: Address, amountOut: BigNumberish): Promise<BigNumber>;
|
|
88
|
+
getAmountIn(tokenIn: Address, tokenOut: Address, amountOut: BigNumberish, tradablePair?: TradablePair): Promise<BigNumber>;
|
|
60
89
|
/**
|
|
61
|
-
* Returns the amount of tokenOut to be bought by selling amountIn of tokenIn
|
|
90
|
+
* Returns the amount of tokenOut to be bought by selling amountIn of tokenIn.
|
|
91
|
+
* If the provided tradablePair has a single (direct) pricing path, then direct pricing is used.
|
|
92
|
+
* Otherwise, routed pricing via the MentoRouter is applied.
|
|
62
93
|
* @param tokenIn the token to be sold
|
|
63
94
|
* @param tokenOut the token to be bought
|
|
64
95
|
* @param amountIn the amount of tokenIn to be sold
|
|
96
|
+
* @param tradablePair the TradablePair object containing the pricing path information
|
|
65
97
|
* @returns the amount of tokenOut to be bought
|
|
66
98
|
*/
|
|
67
|
-
getAmountOut(tokenIn: Address, tokenOut: Address, amountIn: BigNumberish): Promise<BigNumber>;
|
|
99
|
+
getAmountOut(tokenIn: Address, tokenOut: Address, amountIn: BigNumberish, tradablePair?: TradablePair): Promise<BigNumber>;
|
|
100
|
+
/**
|
|
101
|
+
* Internal method for direct pricing: retrieves the exchange for the given tokens
|
|
102
|
+
* and returns the amountIn using the broker.
|
|
103
|
+
*/
|
|
104
|
+
private getAmountInDirect;
|
|
105
|
+
/**
|
|
106
|
+
* Internal method for direct pricing: retrieves the exchange for the given tokens
|
|
107
|
+
* and returns the amountOut using the broker.
|
|
108
|
+
*/
|
|
109
|
+
private getAmountOutDirect;
|
|
110
|
+
/**
|
|
111
|
+
* Internal method for routed pricing: uses the MentoRouter to determine the required tokenIn
|
|
112
|
+
* for obtaining amountOut through a multi-hop route specified in tradablePair.path.
|
|
113
|
+
*/
|
|
114
|
+
private getAmountInRouted;
|
|
115
|
+
/**
|
|
116
|
+
* Internal method for routed pricing: uses the MentoRouter to determine the amountOut
|
|
117
|
+
* obtainable by selling amountIn through a multi-hop route specified in tradablePair.path.
|
|
118
|
+
*/
|
|
119
|
+
private getAmountOutRouted;
|
|
68
120
|
/**
|
|
69
121
|
* Increases the broker's trading allowance for the given token
|
|
70
122
|
* @param token the token to increase the allowance for
|
|
71
123
|
* @param amount the amount to increase the allowance by
|
|
72
124
|
* @returns the populated TransactionRequest object
|
|
73
125
|
*/
|
|
74
|
-
increaseTradingAllowance(
|
|
126
|
+
increaseTradingAllowance(tokenIn: Address, amount: BigNumberish, tradablePair?: TradablePair): Promise<providers.TransactionRequest>;
|
|
75
127
|
/**
|
|
76
|
-
* Returns a token swap populated tx object with a fixed amount of tokenIn and a minimum amount of tokenOut
|
|
77
|
-
*
|
|
128
|
+
* Returns a token swap populated tx object with a fixed amount of tokenIn and a minimum amount of tokenOut.
|
|
129
|
+
* If the tradablePair contains a single-hop route, a direct swap is executed using swapExactTokensForTokens on the broker.
|
|
130
|
+
* Otherwise, a routed swap is executed via the router.
|
|
78
131
|
* @param tokenIn the token to be sold
|
|
79
132
|
* @param tokenOut the token to be bought
|
|
80
133
|
* @param amountIn the amount of tokenIn to be sold
|
|
81
134
|
* @param amountOutMin the minimum amount of tokenOut to be bought
|
|
135
|
+
* @param tradablePair the tradable pair details to determine routing
|
|
82
136
|
* @returns the populated TransactionRequest object
|
|
83
137
|
*/
|
|
84
|
-
swapIn(tokenIn: Address, tokenOut: Address, amountIn: BigNumberish, amountOutMin: BigNumberish): Promise<providers.TransactionRequest>;
|
|
138
|
+
swapIn(tokenIn: Address, tokenOut: Address, amountIn: BigNumberish, amountOutMin: BigNumberish, tradablePair?: TradablePair): Promise<providers.TransactionRequest>;
|
|
139
|
+
private swapInDirect;
|
|
140
|
+
private swapInRouted;
|
|
85
141
|
/**
|
|
86
|
-
* Returns a token swap populated tx object with a maximum amount of tokenIn and a fixed amount of tokenOut
|
|
87
|
-
*
|
|
142
|
+
* Returns a token swap populated tx object with a maximum amount of tokenIn and a fixed amount of tokenOut.
|
|
143
|
+
* If the tradablePair contains a single-hop route, a direct swap is executed using swapTokensForExactTokens on the broker.
|
|
144
|
+
* Otherwise, a routed swap is executed via the router.
|
|
88
145
|
* @param tokenIn the token to be sold
|
|
89
146
|
* @param tokenOut the token to be bought
|
|
90
147
|
* @param amountOut the amount of tokenOut to be bought
|
|
91
148
|
* @param amountInMax the maximum amount of tokenIn to be sold
|
|
92
149
|
* @returns the populated TransactionRequest object
|
|
93
150
|
*/
|
|
94
|
-
swapOut(tokenIn: Address, tokenOut: Address, amountOut: BigNumberish, amountInMax: BigNumberish): Promise<providers.TransactionRequest>;
|
|
151
|
+
swapOut(tokenIn: Address, tokenOut: Address, amountOut: BigNumberish, amountInMax: BigNumberish, tradablePair?: TradablePair): Promise<providers.TransactionRequest>;
|
|
152
|
+
private swapOutDirect;
|
|
153
|
+
private swapOutRouted;
|
|
154
|
+
/**
|
|
155
|
+
* Helper method to build the steps for a routed swap, ensuring proper token ordering
|
|
156
|
+
* through the path segments
|
|
157
|
+
*/
|
|
158
|
+
private buildSteps;
|
|
95
159
|
/**
|
|
96
160
|
* Returns the mento instance's broker contract
|
|
97
161
|
* @returns broker contract
|
|
98
162
|
*/
|
|
99
163
|
getBroker(): IBroker;
|
|
164
|
+
/**
|
|
165
|
+
* Finds a tradable pair for the given input and output tokens
|
|
166
|
+
* @param tokenIn the input token address
|
|
167
|
+
* @param tokenOut the output token address
|
|
168
|
+
* @returns the tradable pair containing the path between the tokens
|
|
169
|
+
* @throws if no path is found between the tokens
|
|
170
|
+
*/
|
|
171
|
+
findPairForTokens(tokenIn: Address, tokenOut: Address): Promise<TradablePair>;
|
|
100
172
|
/**
|
|
101
173
|
* Returns the list of exchanges available in Mento (cached)
|
|
102
174
|
* @returns the list of exchanges
|
package/dist/cjs/mento.js
CHANGED
|
@@ -15,6 +15,9 @@ const ethers_1 = require("ethers");
|
|
|
15
15
|
const utils_1 = require("./utils");
|
|
16
16
|
const limits_1 = require("./limits");
|
|
17
17
|
const assert_1 = require("assert");
|
|
18
|
+
const tradablePairs_1 = require("./constants/tradablePairs");
|
|
19
|
+
const mento_router_ts_1 = require("mento-router-ts");
|
|
20
|
+
const addresses_1 = require("./constants/addresses");
|
|
18
21
|
class Mento {
|
|
19
22
|
/**
|
|
20
23
|
* This constructor is private, use the static create or createWithParams methods
|
|
@@ -23,9 +26,10 @@ class Mento {
|
|
|
23
26
|
* @param brokerAddress the address of the broker contract
|
|
24
27
|
* @param exchanges exchange data for the broker
|
|
25
28
|
*/
|
|
26
|
-
constructor(signerOrProvider, brokerAddress, exchanges) {
|
|
29
|
+
constructor(signerOrProvider, brokerAddress, routerAddress, exchanges) {
|
|
27
30
|
this.signerOrProvider = signerOrProvider;
|
|
28
31
|
this.broker = mento_core_ts_1.IBroker__factory.connect(brokerAddress, signerOrProvider);
|
|
32
|
+
this.router = mento_router_ts_1.IMentoRouter__factory.connect(routerAddress, signerOrProvider);
|
|
29
33
|
this.exchanges = exchanges || [];
|
|
30
34
|
}
|
|
31
35
|
/**
|
|
@@ -37,7 +41,7 @@ class Mento {
|
|
|
37
41
|
static create(signerOrProvider) {
|
|
38
42
|
return __awaiter(this, void 0, void 0, function* () {
|
|
39
43
|
(0, utils_1.validateSignerOrProvider)(signerOrProvider);
|
|
40
|
-
return new Mento(signerOrProvider, yield (0, utils_1.getBrokerAddressFromRegistry)(signerOrProvider));
|
|
44
|
+
return new Mento(signerOrProvider, yield (0, utils_1.getBrokerAddressFromRegistry)(signerOrProvider), yield (0, addresses_1.getAddress)('MentoRouter', yield (0, utils_1.getChainId)(signerOrProvider)));
|
|
41
45
|
});
|
|
42
46
|
}
|
|
43
47
|
/**
|
|
@@ -48,9 +52,9 @@ class Mento {
|
|
|
48
52
|
* @param exchanges the exchanges data for the broker
|
|
49
53
|
* @returns a new Mento object instance
|
|
50
54
|
*/
|
|
51
|
-
static createWithParams(signerOrProvider, brokerAddr, exchanges) {
|
|
55
|
+
static createWithParams(signerOrProvider, brokerAddr, routerAddr, exchanges) {
|
|
52
56
|
(0, utils_1.validateSignerOrProvider)(signerOrProvider);
|
|
53
|
-
return new Mento(signerOrProvider, brokerAddr, exchanges);
|
|
57
|
+
return new Mento(signerOrProvider, brokerAddr, routerAddr, exchanges);
|
|
54
58
|
}
|
|
55
59
|
/**
|
|
56
60
|
* Returns a new Mento instance connected to the given signer
|
|
@@ -59,55 +63,240 @@ class Mento {
|
|
|
59
63
|
*/
|
|
60
64
|
connectSigner(signer) {
|
|
61
65
|
(0, utils_1.validateSigner)(signer);
|
|
62
|
-
return new Mento(signer, this.broker.address, this.exchanges);
|
|
66
|
+
return new Mento(signer, this.broker.address, this.router.address, this.exchanges);
|
|
63
67
|
}
|
|
64
68
|
/**
|
|
65
|
-
*
|
|
66
|
-
* @returns
|
|
69
|
+
* Get tradable pairs for backwards compatibility
|
|
70
|
+
* @returns an array of Asset pairs
|
|
67
71
|
*/
|
|
68
|
-
|
|
72
|
+
getTradablePairs(cached = true) {
|
|
73
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
74
|
+
return (yield this.getTradablePairsWithPath(cached)).map((pair) => pair.assets);
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Returns a list of all tradable pairs on Mento via direct exchanges.
|
|
79
|
+
* Each pair is represented using the TradablePair interface, with its id
|
|
80
|
+
* (a concatenation of the two asset symbols in alphabetical order),
|
|
81
|
+
* the two Asset objects, and a path (an array with a single direct exchange hop).
|
|
82
|
+
* @returns An array of direct TradablePair objects.
|
|
83
|
+
*/
|
|
84
|
+
getDirectPairs() {
|
|
69
85
|
return __awaiter(this, void 0, void 0, function* () {
|
|
70
86
|
const exchanges = yield this.getExchanges();
|
|
71
|
-
|
|
87
|
+
// Map from pair id (symbol-symbol) to its TradablePair
|
|
88
|
+
const directPairsMap = new Map();
|
|
72
89
|
for (const exchange of exchanges) {
|
|
73
|
-
const
|
|
74
|
-
const
|
|
75
|
-
|
|
76
|
-
(0, utils_1.getSymbolFromTokenAddress)(
|
|
77
|
-
(0, utils_1.getSymbolFromTokenAddress)(asset1, this.signerOrProvider),
|
|
78
|
-
]);
|
|
79
|
-
pairs.push([
|
|
80
|
-
{ address: asset0, symbol: symbols[0] },
|
|
81
|
-
{ address: asset1, symbol: symbols[1] },
|
|
90
|
+
const [token0, token1] = exchange.assets;
|
|
91
|
+
const [symbol0, symbol1] = yield Promise.all([
|
|
92
|
+
(0, utils_1.getSymbolFromTokenAddress)(token0, this.signerOrProvider),
|
|
93
|
+
(0, utils_1.getSymbolFromTokenAddress)(token1, this.signerOrProvider),
|
|
82
94
|
]);
|
|
95
|
+
// Determine canonical order by symbol
|
|
96
|
+
let assets;
|
|
97
|
+
let pairId;
|
|
98
|
+
if (symbol0 <= symbol1) {
|
|
99
|
+
assets = [
|
|
100
|
+
{ address: token0, symbol: symbol0 },
|
|
101
|
+
{ address: token1, symbol: symbol1 },
|
|
102
|
+
];
|
|
103
|
+
pairId = `${symbol0}-${symbol1}`;
|
|
104
|
+
}
|
|
105
|
+
else {
|
|
106
|
+
assets = [
|
|
107
|
+
{ address: token1, symbol: symbol1 },
|
|
108
|
+
{ address: token0, symbol: symbol0 },
|
|
109
|
+
];
|
|
110
|
+
pairId = `${symbol1}-${symbol0}`;
|
|
111
|
+
}
|
|
112
|
+
const pathEntry = {
|
|
113
|
+
providerAddr: exchange.providerAddr,
|
|
114
|
+
id: exchange.id,
|
|
115
|
+
assets: exchange.assets,
|
|
116
|
+
};
|
|
117
|
+
if (directPairsMap.has(pairId)) {
|
|
118
|
+
directPairsMap.get(pairId).path.push(pathEntry);
|
|
119
|
+
}
|
|
120
|
+
else {
|
|
121
|
+
directPairsMap.set(pairId, { id: pairId, assets, path: [pathEntry] });
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return Array.from(directPairsMap.values());
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Returns a list of all tradable pairs on Mento, including those achievable
|
|
129
|
+
* via two-hop routes. For two-hop pairs, the path will contain two exchange hops.
|
|
130
|
+
* Each TradablePair contains an id (the concatenation of the two asset symbols in alphabetical order),
|
|
131
|
+
* the two Asset objects, and an array of exchange details for each hop.
|
|
132
|
+
* @returns An array of TradablePair objects representing available trade routes.
|
|
133
|
+
*/
|
|
134
|
+
getTradablePairsWithPath(cached = true) {
|
|
135
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
136
|
+
// Get tradable pairs from cache if available.
|
|
137
|
+
if (cached) {
|
|
138
|
+
const value = (0, tradablePairs_1.getCachedTradablePairs)(yield (0, utils_1.getChainId)(this.signerOrProvider));
|
|
139
|
+
if (value) {
|
|
140
|
+
return value;
|
|
141
|
+
}
|
|
83
142
|
}
|
|
84
|
-
|
|
143
|
+
// Retrieve direct pairs first.
|
|
144
|
+
const directPairs = yield this.getDirectPairs();
|
|
145
|
+
// Build helper maps:
|
|
146
|
+
// assetMap: maps token address to its Asset details.
|
|
147
|
+
const assetMap = new Map();
|
|
148
|
+
// directPathMap: maps a sorted pair of token addresses (by address) to a direct exchange hop.
|
|
149
|
+
const directPathMap = new Map();
|
|
150
|
+
// graph: maps a token address to the set of addresses it connects to directly.
|
|
151
|
+
const graph = new Map();
|
|
152
|
+
for (const pair of directPairs) {
|
|
153
|
+
const [assetA, assetB] = pair.assets;
|
|
154
|
+
assetMap.set(assetA.address, assetA);
|
|
155
|
+
assetMap.set(assetB.address, assetB);
|
|
156
|
+
const sortedAddresses = [assetA.address, assetB.address].sort().join('-');
|
|
157
|
+
if (!directPathMap.has(sortedAddresses)) {
|
|
158
|
+
// Use the first available direct hop for the connectivity graph.
|
|
159
|
+
directPathMap.set(sortedAddresses, pair.path[0]);
|
|
160
|
+
}
|
|
161
|
+
if (!graph.has(assetA.address))
|
|
162
|
+
graph.set(assetA.address, new Set());
|
|
163
|
+
if (!graph.has(assetB.address))
|
|
164
|
+
graph.set(assetB.address, new Set());
|
|
165
|
+
graph.get(assetA.address).add(assetB.address);
|
|
166
|
+
graph.get(assetB.address).add(assetA.address);
|
|
167
|
+
}
|
|
168
|
+
// Initialize tradablePairsMap with direct pairs keyed by their id (symbol-symbol).
|
|
169
|
+
const tradablePairsMap = new Map();
|
|
170
|
+
for (const pair of directPairs) {
|
|
171
|
+
tradablePairsMap.set(pair.id, pair);
|
|
172
|
+
}
|
|
173
|
+
// Generate two-hop pairs using the connectivity graph.
|
|
174
|
+
// For each potential route: start -> intermediate -> end, add a two-hop pair
|
|
175
|
+
// only if no direct route (i.e. same symbol pair) exists.
|
|
176
|
+
for (const [start, neighbors] of graph.entries()) {
|
|
177
|
+
for (const intermediate of neighbors) {
|
|
178
|
+
const secondHopNeighbors = graph.get(intermediate);
|
|
179
|
+
if (!secondHopNeighbors)
|
|
180
|
+
continue;
|
|
181
|
+
for (const end of secondHopNeighbors) {
|
|
182
|
+
if (end === start)
|
|
183
|
+
continue; // Avoid self-loop.
|
|
184
|
+
const assetStart = assetMap.get(start);
|
|
185
|
+
const assetEnd = assetMap.get(end);
|
|
186
|
+
if (!assetStart || !assetEnd)
|
|
187
|
+
continue;
|
|
188
|
+
// Determine canonical pair id based on asset symbols.
|
|
189
|
+
const sortedSymbols = [assetStart.symbol, assetEnd.symbol].sort();
|
|
190
|
+
const pairId = `${sortedSymbols[0]}-${sortedSymbols[1]}`;
|
|
191
|
+
if (tradablePairsMap.has(pairId))
|
|
192
|
+
continue; // Skip if a direct pair exists.
|
|
193
|
+
// Retrieve the direct hops for the two segments.
|
|
194
|
+
const hop1Key = [start, intermediate].sort().join('-');
|
|
195
|
+
const hop2Key = [intermediate, end].sort().join('-');
|
|
196
|
+
const hop1 = directPathMap.get(hop1Key);
|
|
197
|
+
const hop2 = directPathMap.get(hop2Key);
|
|
198
|
+
if (!hop1 || !hop2)
|
|
199
|
+
continue;
|
|
200
|
+
let assets;
|
|
201
|
+
if (assetStart.symbol <= assetEnd.symbol) {
|
|
202
|
+
assets = [assetStart, assetEnd];
|
|
203
|
+
}
|
|
204
|
+
else {
|
|
205
|
+
assets = [assetEnd, assetStart];
|
|
206
|
+
}
|
|
207
|
+
tradablePairsMap.set(pairId, {
|
|
208
|
+
id: pairId,
|
|
209
|
+
assets,
|
|
210
|
+
path: [hop1, hop2],
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
return Array.from(tradablePairsMap.values());
|
|
85
216
|
});
|
|
86
217
|
}
|
|
87
218
|
/**
|
|
88
|
-
* Returns the amount of tokenIn to be sold to buy amountOut of tokenOut
|
|
219
|
+
* Returns the amount of tokenIn to be sold to buy amountOut of tokenOut.
|
|
220
|
+
* If the provided tradablePair has a single (direct) pricing path, then direct pricing is used.
|
|
221
|
+
* Otherwise, routed pricing via the MentoRouter is applied.
|
|
89
222
|
* @param tokenIn the token to be sold
|
|
90
223
|
* @param tokenOut the token to be bought
|
|
91
|
-
* @param amountOut the amount of tokenOut to be
|
|
224
|
+
* @param amountOut the desired amount of tokenOut to be obtained
|
|
225
|
+
* @param tradablePair the TradablePair object containing the pricing path information
|
|
92
226
|
* @returns the amount of tokenIn to be sold
|
|
93
227
|
*/
|
|
94
|
-
getAmountIn(tokenIn, tokenOut, amountOut) {
|
|
228
|
+
getAmountIn(tokenIn, tokenOut, amountOut, tradablePair) {
|
|
95
229
|
return __awaiter(this, void 0, void 0, function* () {
|
|
96
|
-
|
|
97
|
-
|
|
230
|
+
if (!tradablePair) {
|
|
231
|
+
tradablePair = yield this.findPairForTokens(tokenIn, tokenOut);
|
|
232
|
+
}
|
|
233
|
+
if (tradablePair.path.length === 1) {
|
|
234
|
+
return this.getAmountInDirect(tokenIn, tokenOut, amountOut, tradablePair);
|
|
235
|
+
}
|
|
236
|
+
else {
|
|
237
|
+
return this.getAmountInRouted(tokenIn, tokenOut, amountOut, tradablePair);
|
|
238
|
+
}
|
|
98
239
|
});
|
|
99
240
|
}
|
|
100
241
|
/**
|
|
101
|
-
* Returns the amount of tokenOut to be bought by selling amountIn of tokenIn
|
|
242
|
+
* Returns the amount of tokenOut to be bought by selling amountIn of tokenIn.
|
|
243
|
+
* If the provided tradablePair has a single (direct) pricing path, then direct pricing is used.
|
|
244
|
+
* Otherwise, routed pricing via the MentoRouter is applied.
|
|
102
245
|
* @param tokenIn the token to be sold
|
|
103
246
|
* @param tokenOut the token to be bought
|
|
104
247
|
* @param amountIn the amount of tokenIn to be sold
|
|
248
|
+
* @param tradablePair the TradablePair object containing the pricing path information
|
|
105
249
|
* @returns the amount of tokenOut to be bought
|
|
106
250
|
*/
|
|
107
|
-
getAmountOut(tokenIn, tokenOut, amountIn) {
|
|
251
|
+
getAmountOut(tokenIn, tokenOut, amountIn, tradablePair) {
|
|
108
252
|
return __awaiter(this, void 0, void 0, function* () {
|
|
109
|
-
|
|
110
|
-
|
|
253
|
+
if (!tradablePair) {
|
|
254
|
+
tradablePair = yield this.findPairForTokens(tokenIn, tokenOut);
|
|
255
|
+
}
|
|
256
|
+
if (tradablePair.path.length === 1) {
|
|
257
|
+
return this.getAmountOutDirect(tokenIn, tokenOut, amountIn, tradablePair);
|
|
258
|
+
}
|
|
259
|
+
else {
|
|
260
|
+
return this.getAmountOutRouted(tokenIn, tokenOut, amountIn, tradablePair);
|
|
261
|
+
}
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
/**
|
|
265
|
+
* Internal method for direct pricing: retrieves the exchange for the given tokens
|
|
266
|
+
* and returns the amountIn using the broker.
|
|
267
|
+
*/
|
|
268
|
+
getAmountInDirect(tokenIn, tokenOut, amountOut, tradablePair) {
|
|
269
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
270
|
+
return this.broker.getAmountIn(tradablePair.path[0].providerAddr, tradablePair.path[0].id, tokenIn, tokenOut, amountOut);
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
/**
|
|
274
|
+
* Internal method for direct pricing: retrieves the exchange for the given tokens
|
|
275
|
+
* and returns the amountOut using the broker.
|
|
276
|
+
*/
|
|
277
|
+
getAmountOutDirect(tokenIn, tokenOut, amountIn, tradablePair) {
|
|
278
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
279
|
+
return this.broker.getAmountOut(tradablePair.path[0].providerAddr, tradablePair.path[0].id, tokenIn, tokenOut, amountIn);
|
|
280
|
+
});
|
|
281
|
+
}
|
|
282
|
+
/**
|
|
283
|
+
* Internal method for routed pricing: uses the MentoRouter to determine the required tokenIn
|
|
284
|
+
* for obtaining amountOut through a multi-hop route specified in tradablePair.path.
|
|
285
|
+
*/
|
|
286
|
+
getAmountInRouted(tokenIn, tokenOut, amountOut, tradablePair) {
|
|
287
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
288
|
+
const steps = this.buildSteps(tokenIn, tokenOut, tradablePair);
|
|
289
|
+
return this.router.getAmountIn(amountOut, steps);
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
/**
|
|
293
|
+
* Internal method for routed pricing: uses the MentoRouter to determine the amountOut
|
|
294
|
+
* obtainable by selling amountIn through a multi-hop route specified in tradablePair.path.
|
|
295
|
+
*/
|
|
296
|
+
getAmountOutRouted(tokenIn, tokenOut, amountIn, tradablePair) {
|
|
297
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
298
|
+
const steps = this.buildSteps(tokenIn, tokenOut, tradablePair);
|
|
299
|
+
return this.router.getAmountOut(amountIn, steps);
|
|
111
300
|
});
|
|
112
301
|
}
|
|
113
302
|
/**
|
|
@@ -116,10 +305,10 @@ class Mento {
|
|
|
116
305
|
* @param amount the amount to increase the allowance by
|
|
117
306
|
* @returns the populated TransactionRequest object
|
|
118
307
|
*/
|
|
119
|
-
increaseTradingAllowance(
|
|
308
|
+
increaseTradingAllowance(tokenIn, amount, tradablePair) {
|
|
120
309
|
return __awaiter(this, void 0, void 0, function* () {
|
|
121
|
-
const spender = this.broker.address;
|
|
122
|
-
const tx = yield (0, utils_1.increaseAllowance)(
|
|
310
|
+
const spender = !tradablePair || (tradablePair === null || tradablePair === void 0 ? void 0 : tradablePair.path.length) == 1 ? this.broker.address : this.router.address;
|
|
311
|
+
const tx = yield (0, utils_1.increaseAllowance)(tokenIn, spender, amount, this.signerOrProvider);
|
|
123
312
|
if (ethers_1.Signer.isSigner(this.signerOrProvider)) {
|
|
124
313
|
// The contract call doesn't populate all of the signer fields, so we need an extra call for the signer
|
|
125
314
|
return this.signerOrProvider.populateTransaction(tx);
|
|
@@ -130,47 +319,125 @@ class Mento {
|
|
|
130
319
|
});
|
|
131
320
|
}
|
|
132
321
|
/**
|
|
133
|
-
* Returns a token swap populated tx object with a fixed amount of tokenIn and a minimum amount of tokenOut
|
|
134
|
-
*
|
|
322
|
+
* Returns a token swap populated tx object with a fixed amount of tokenIn and a minimum amount of tokenOut.
|
|
323
|
+
* If the tradablePair contains a single-hop route, a direct swap is executed using swapExactTokensForTokens on the broker.
|
|
324
|
+
* Otherwise, a routed swap is executed via the router.
|
|
135
325
|
* @param tokenIn the token to be sold
|
|
136
326
|
* @param tokenOut the token to be bought
|
|
137
327
|
* @param amountIn the amount of tokenIn to be sold
|
|
138
328
|
* @param amountOutMin the minimum amount of tokenOut to be bought
|
|
329
|
+
* @param tradablePair the tradable pair details to determine routing
|
|
139
330
|
* @returns the populated TransactionRequest object
|
|
140
331
|
*/
|
|
141
|
-
swapIn(tokenIn, tokenOut, amountIn, amountOutMin) {
|
|
332
|
+
swapIn(tokenIn, tokenOut, amountIn, amountOutMin, tradablePair) {
|
|
142
333
|
return __awaiter(this, void 0, void 0, function* () {
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
return this.
|
|
334
|
+
if (!tradablePair) {
|
|
335
|
+
tradablePair = yield this.findPairForTokens(tokenIn, tokenOut);
|
|
336
|
+
}
|
|
337
|
+
if (tradablePair.path.length === 1) {
|
|
338
|
+
return this.swapInDirect(tokenIn, tokenOut, amountIn, amountOutMin);
|
|
148
339
|
}
|
|
149
340
|
else {
|
|
150
|
-
return
|
|
341
|
+
return this.swapInRouted(tokenIn, tokenOut, amountIn, amountOutMin, tradablePair);
|
|
151
342
|
}
|
|
152
343
|
});
|
|
153
344
|
}
|
|
345
|
+
swapInDirect(tokenIn, tokenOut, amountIn, amountOutMin) {
|
|
346
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
347
|
+
const exchange = yield this.getExchangeForTokens(tokenIn, tokenOut);
|
|
348
|
+
const tx = yield this.broker.populateTransaction.swapIn(exchange.providerAddr, exchange.id, tokenIn, tokenOut, amountIn, amountOutMin);
|
|
349
|
+
return ethers_1.Signer.isSigner(this.signerOrProvider)
|
|
350
|
+
? this.signerOrProvider.populateTransaction(tx)
|
|
351
|
+
: tx;
|
|
352
|
+
});
|
|
353
|
+
}
|
|
354
|
+
swapInRouted(tokenIn, tokenOut, amountIn, amountOutMin, tradablePair) {
|
|
355
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
356
|
+
const steps = this.buildSteps(tokenIn, tokenOut, tradablePair);
|
|
357
|
+
const tx = yield this.router.populateTransaction.swapExactTokensForTokens(amountIn, amountOutMin, steps);
|
|
358
|
+
return ethers_1.Signer.isSigner(this.signerOrProvider)
|
|
359
|
+
? this.signerOrProvider.populateTransaction(tx)
|
|
360
|
+
: tx;
|
|
361
|
+
});
|
|
362
|
+
}
|
|
154
363
|
/**
|
|
155
|
-
* Returns a token swap populated tx object with a maximum amount of tokenIn and a fixed amount of tokenOut
|
|
156
|
-
*
|
|
364
|
+
* Returns a token swap populated tx object with a maximum amount of tokenIn and a fixed amount of tokenOut.
|
|
365
|
+
* If the tradablePair contains a single-hop route, a direct swap is executed using swapTokensForExactTokens on the broker.
|
|
366
|
+
* Otherwise, a routed swap is executed via the router.
|
|
157
367
|
* @param tokenIn the token to be sold
|
|
158
368
|
* @param tokenOut the token to be bought
|
|
159
369
|
* @param amountOut the amount of tokenOut to be bought
|
|
160
370
|
* @param amountInMax the maximum amount of tokenIn to be sold
|
|
161
371
|
* @returns the populated TransactionRequest object
|
|
162
372
|
*/
|
|
163
|
-
swapOut(tokenIn, tokenOut, amountOut, amountInMax) {
|
|
373
|
+
swapOut(tokenIn, tokenOut, amountOut, amountInMax, tradablePair) {
|
|
374
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
375
|
+
if (!tradablePair) {
|
|
376
|
+
tradablePair = yield this.findPairForTokens(tokenIn, tokenOut);
|
|
377
|
+
}
|
|
378
|
+
if (tradablePair.path.length === 1) {
|
|
379
|
+
return this.swapOutDirect(tokenIn, tokenOut, amountOut, amountInMax);
|
|
380
|
+
}
|
|
381
|
+
else {
|
|
382
|
+
return this.swapOutRouted(tokenIn, tokenOut, amountOut, amountInMax, tradablePair);
|
|
383
|
+
}
|
|
384
|
+
});
|
|
385
|
+
}
|
|
386
|
+
swapOutDirect(tokenIn, tokenOut, amountOut, amountInMax) {
|
|
164
387
|
return __awaiter(this, void 0, void 0, function* () {
|
|
165
388
|
const exchange = yield this.getExchangeForTokens(tokenIn, tokenOut);
|
|
166
389
|
const tx = yield this.broker.populateTransaction.swapOut(exchange.providerAddr, exchange.id, tokenIn, tokenOut, amountOut, amountInMax);
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
390
|
+
return ethers_1.Signer.isSigner(this.signerOrProvider)
|
|
391
|
+
? this.signerOrProvider.populateTransaction(tx)
|
|
392
|
+
: tx;
|
|
393
|
+
});
|
|
394
|
+
}
|
|
395
|
+
swapOutRouted(tokenIn, tokenOut, amountOut, amountInMax, tradablePair) {
|
|
396
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
397
|
+
const steps = this.buildSteps(tokenIn, tokenOut, tradablePair);
|
|
398
|
+
const tx = yield this.router.populateTransaction.swapTokensForExactTokens(amountOut, amountInMax, steps);
|
|
399
|
+
return ethers_1.Signer.isSigner(this.signerOrProvider)
|
|
400
|
+
? this.signerOrProvider.populateTransaction(tx)
|
|
401
|
+
: tx;
|
|
402
|
+
});
|
|
403
|
+
}
|
|
404
|
+
/**
|
|
405
|
+
* Helper method to build the steps for a routed swap, ensuring proper token ordering
|
|
406
|
+
* through the path segments
|
|
407
|
+
*/
|
|
408
|
+
buildSteps(tokenIn, tokenOut, tradablePair) {
|
|
409
|
+
let path = [...tradablePair.path];
|
|
410
|
+
if (path[0].assets.includes(tokenOut)) {
|
|
411
|
+
path = path.reverse();
|
|
412
|
+
}
|
|
413
|
+
return path.map((step, idx) => {
|
|
414
|
+
const isFirstStep = idx === 0;
|
|
415
|
+
const isLastStep = idx === tradablePair.path.length - 1;
|
|
416
|
+
const prevStep = idx > 0 ? tradablePair.path[idx - 1] : null;
|
|
417
|
+
// For first step, ensure assetIn is tokenIn
|
|
418
|
+
// For middle steps, ensure assetIn matches previous step's assetOut
|
|
419
|
+
// For last step, ensure assetOut is tokenOut
|
|
420
|
+
let [assetIn, assetOut] = step.assets;
|
|
421
|
+
if (isFirstStep && assetIn !== tokenIn) {
|
|
422
|
+
;
|
|
423
|
+
[assetIn, assetOut] = [assetOut, assetIn];
|
|
170
424
|
}
|
|
171
|
-
else
|
|
172
|
-
|
|
425
|
+
else if (!isFirstStep &&
|
|
426
|
+
!isLastStep &&
|
|
427
|
+
assetIn !== prevStep.assets[1]) {
|
|
428
|
+
;
|
|
429
|
+
[assetIn, assetOut] = [assetOut, assetIn];
|
|
430
|
+
}
|
|
431
|
+
else if (isLastStep && assetOut !== tokenOut) {
|
|
432
|
+
;
|
|
433
|
+
[assetIn, assetOut] = [assetOut, assetIn];
|
|
173
434
|
}
|
|
435
|
+
return {
|
|
436
|
+
exchangeProvider: step.providerAddr,
|
|
437
|
+
exchangeId: step.id,
|
|
438
|
+
assetIn,
|
|
439
|
+
assetOut,
|
|
440
|
+
};
|
|
174
441
|
});
|
|
175
442
|
}
|
|
176
443
|
/**
|
|
@@ -180,6 +447,28 @@ class Mento {
|
|
|
180
447
|
getBroker() {
|
|
181
448
|
return this.broker;
|
|
182
449
|
}
|
|
450
|
+
/**
|
|
451
|
+
* Finds a tradable pair for the given input and output tokens
|
|
452
|
+
* @param tokenIn the input token address
|
|
453
|
+
* @param tokenOut the output token address
|
|
454
|
+
* @returns the tradable pair containing the path between the tokens
|
|
455
|
+
* @throws if no path is found between the tokens
|
|
456
|
+
*/
|
|
457
|
+
findPairForTokens(tokenIn, tokenOut) {
|
|
458
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
459
|
+
const pair = (yield this.getTradablePairsWithPath()).find((p) =>
|
|
460
|
+
// Direct path
|
|
461
|
+
(p.path.length === 1 && p.path[0].assets.includes(tokenIn) && p.path[0].assets.includes(tokenOut)) ||
|
|
462
|
+
// Routed path
|
|
463
|
+
(p.path.length === 2 &&
|
|
464
|
+
((p.path[0].assets.includes(tokenIn) && p.path[1].assets.includes(tokenOut)) ||
|
|
465
|
+
(p.path[0].assets.includes(tokenOut) && p.path[1].assets.includes(tokenIn)))));
|
|
466
|
+
if (!pair) {
|
|
467
|
+
throw new Error(`No tradable pair found for tokens ${tokenIn} and ${tokenOut}`);
|
|
468
|
+
}
|
|
469
|
+
return pair;
|
|
470
|
+
});
|
|
471
|
+
}
|
|
183
472
|
/**
|
|
184
473
|
* Returns the list of exchanges available in Mento (cached)
|
|
185
474
|
* @returns the list of exchanges
|