@mento-protocol/mento-sdk 0.1.1 → 0.1.3
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/LICENSE +21 -674
- package/README.md +62 -51
- package/dist/cjs/mento.d.ts +35 -13
- package/dist/cjs/mento.js +52 -48
- package/dist/cjs/utils.d.ts +13 -1
- package/dist/cjs/utils.js +31 -2
- package/dist/esm/mento.d.ts +35 -13
- package/dist/esm/mento.js +53 -49
- package/dist/esm/utils.d.ts +13 -1
- package/dist/esm/utils.js +29 -2
- package/package.json +17 -8
package/README.md
CHANGED
|
@@ -2,61 +2,72 @@
|
|
|
2
2
|
|
|
3
3
|
The official Mento Protocol SDK for interacting with Multi-Collateral Mento smart contracts on the Celo network.
|
|
4
4
|
|
|
5
|
-
#
|
|
5
|
+
# Example Usage
|
|
6
6
|
|
|
7
7
|
```javascript
|
|
8
|
-
|
|
9
|
-
'https://baklava-forno.celo-testnet.org'
|
|
10
|
-
)
|
|
11
|
-
const pKey = 'privateKey'
|
|
12
|
-
const wallet = new Wallet(pKey, provider)
|
|
8
|
+
import { Wallet, providers, utils } from 'ethers'
|
|
13
9
|
|
|
14
|
-
|
|
10
|
+
import { Mento } from '@mento-protocol/mento-sdk'
|
|
15
11
|
|
|
16
|
-
|
|
17
|
-
|
|
12
|
+
async function main() {
|
|
13
|
+
const provider = new providers.JsonRpcProvider(
|
|
14
|
+
'https://baklava-forno.celo-testnet.org'
|
|
15
|
+
)
|
|
16
|
+
const pKey =
|
|
17
|
+
'b214fcf9673dc1a880f8041a8c8952c2dc7daff41fb64cf7715919df095c3fce'
|
|
18
|
+
const wallet = new Wallet(pKey, provider)
|
|
19
|
+
|
|
20
|
+
const mento = await Mento.create(wallet)
|
|
21
|
+
console.log('available pairs: ', await mento.getTradeablePairs())
|
|
22
|
+
/*
|
|
23
|
+
[
|
|
18
24
|
[
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
const
|
|
35
|
-
const
|
|
36
|
-
const
|
|
37
|
-
const
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
const minAmountOut = expectedAmountOut.mul(95).div(100)
|
|
45
|
-
|
|
46
|
-
// allow the broker contract to spend CELO on behalf of the wallet
|
|
47
|
-
const allowanceTxObj = await mento.increaseTradingAllowance(tokenIn, amountIn)
|
|
48
|
-
const allowanceTx = await wallet.sendTransaction(allowanceTxObj)
|
|
49
|
-
const allowanceReceipt = await allowanceTx.wait()
|
|
50
|
-
console.log('increaseAllowance receipt', allowanceReceipt)
|
|
51
|
-
|
|
52
|
-
// execute the swap
|
|
53
|
-
const swapTxObj = await mento.swapIn(tokenIn, tokenOut, amountIn, minAmountOut)
|
|
54
|
-
const swapTx = await wallet.sendTransaction(swapTxObj)
|
|
55
|
-
const swapReceipt = await swapTx.wait()
|
|
56
|
-
|
|
57
|
-
console.log('swapIn receipt', swapReceipt)
|
|
58
|
-
```
|
|
25
|
+
{
|
|
26
|
+
address: '0x62492A644A588FD904270BeD06ad52B9abfEA1aE',
|
|
27
|
+
symbol: 'cUSD'
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
address: '0xdDc9bE57f553fe75752D61606B94CBD7e0264eF8',
|
|
31
|
+
symbol: 'CELO'
|
|
32
|
+
}
|
|
33
|
+
],
|
|
34
|
+
...
|
|
35
|
+
]
|
|
36
|
+
*/
|
|
37
|
+
|
|
38
|
+
// swap 1 CELO for cUSD
|
|
39
|
+
const one = 1
|
|
40
|
+
const tokenIn = '0xdDc9bE57f553fe75752D61606B94CBD7e0264eF8' // CELO
|
|
41
|
+
const tokenOut = '0x62492A644A588FD904270BeD06ad52B9abfEA1aE' // cUSD
|
|
42
|
+
const amountIn = utils.parseUnits(one.toString(), 18)
|
|
43
|
+
const expectedAmountOut = await mento.getAmountOut(
|
|
44
|
+
tokenIn,
|
|
45
|
+
tokenOut,
|
|
46
|
+
utils.parseUnits(one.toString(), 18)
|
|
47
|
+
)
|
|
48
|
+
// 95% of the quote to allow some slippage
|
|
49
|
+
const minAmountOut = expectedAmountOut.mul(95).div(100)
|
|
59
50
|
|
|
60
|
-
|
|
51
|
+
// allow the broker contract to spend CELO on behalf of the wallet
|
|
52
|
+
const allowanceTxObj = await mento.increaseTradingAllowance(tokenIn, amountIn)
|
|
53
|
+
const allowanceTx = await wallet.sendTransaction(allowanceTxObj)
|
|
54
|
+
const allowanceReceipt = await allowanceTx.wait()
|
|
55
|
+
console.log('increaseAllowance receipt', allowanceReceipt)
|
|
61
56
|
|
|
62
|
-
|
|
57
|
+
// execute the swap
|
|
58
|
+
const swapTxObj = await mento.swapIn(
|
|
59
|
+
tokenIn,
|
|
60
|
+
tokenOut,
|
|
61
|
+
amountIn,
|
|
62
|
+
minAmountOut
|
|
63
|
+
)
|
|
64
|
+
const swapTx = await wallet.sendTransaction(swapTxObj)
|
|
65
|
+
const swapReceipt = await swapTx.wait()
|
|
66
|
+
|
|
67
|
+
console.log('swapIn receipt', swapReceipt)
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
main()
|
|
71
|
+
.then(() => console.log('Done'))
|
|
72
|
+
.catch((e) => console.error('Error:', e))
|
|
73
|
+
```
|
package/dist/cjs/mento.d.ts
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { IBroker } from '@mento-protocol/mento-core-ts';
|
|
2
|
+
import { BigNumber, providers, Signer } from 'ethers';
|
|
2
3
|
import { Address } from './types';
|
|
3
|
-
interface
|
|
4
|
+
export interface Exchange {
|
|
5
|
+
providerAddr: Address;
|
|
6
|
+
id: string;
|
|
7
|
+
assets: Address[];
|
|
8
|
+
}
|
|
9
|
+
export interface Asset {
|
|
4
10
|
address: Address;
|
|
5
11
|
symbol: string;
|
|
6
12
|
}
|
|
@@ -9,11 +15,11 @@ export declare class Mento {
|
|
|
9
15
|
private readonly broker;
|
|
10
16
|
private exchanges;
|
|
11
17
|
/**
|
|
12
|
-
* This constructor is private, use the static create or
|
|
18
|
+
* This constructor is private, use the static create or createWithParams methods
|
|
13
19
|
* to create a new Mento instance
|
|
20
|
+
* @param signerOrProvider an ethers provider or connected signer
|
|
14
21
|
* @param brokerAddress the address of the broker contract
|
|
15
|
-
* @param
|
|
16
|
-
* @param signer an optional ethers signer to execute swaps (must be connected to a provider)
|
|
22
|
+
* @param exchanges exchange data for the broker
|
|
17
23
|
*/
|
|
18
24
|
private constructor();
|
|
19
25
|
/**
|
|
@@ -24,13 +30,20 @@ export declare class Mento {
|
|
|
24
30
|
*/
|
|
25
31
|
static create(signerOrProvider: Signer | providers.Provider): Promise<Mento>;
|
|
26
32
|
/**
|
|
27
|
-
* Create a new Mento object instance given a
|
|
28
|
-
* When constructed with
|
|
29
|
-
* @param brokerAddr the address of the broker contract
|
|
33
|
+
* Create a new Mento object instance given a broker address and optional exchanges data
|
|
34
|
+
* When constructed with a Provider, only read-only operations are supported
|
|
30
35
|
* @param signerOrProvider an ethers signer or provider. A signer is required to execute swaps
|
|
36
|
+
* @param brokerAddr the address of the broker contract
|
|
37
|
+
* @param exchanges the exchanges data for the broker
|
|
31
38
|
* @returns a new Mento object instance
|
|
32
39
|
*/
|
|
33
|
-
static
|
|
40
|
+
static createWithParams(signerOrProvider: Signer | providers.Provider, brokerAddr: Address, exchanges?: Exchange[]): Mento;
|
|
41
|
+
/**
|
|
42
|
+
* Returns a new Mento instance connected to the given signer
|
|
43
|
+
* @param signer an ethers signer
|
|
44
|
+
* @returns new Mento object instance
|
|
45
|
+
*/
|
|
46
|
+
connectSigner(signer: Signer): Mento;
|
|
34
47
|
/**
|
|
35
48
|
* Returns a list of all the pairs that can be traded on Mento
|
|
36
49
|
* @returns The list of tradeable pairs in the form of [{address, symbol}]
|
|
@@ -79,17 +92,26 @@ export declare class Mento {
|
|
|
79
92
|
* @returns the populated TransactionRequest object
|
|
80
93
|
*/
|
|
81
94
|
swapOut(tokenIn: Address, tokenOut: Address, amountOut: BigNumber, amountInMax: BigNumber): Promise<providers.TransactionRequest>;
|
|
95
|
+
/**
|
|
96
|
+
* Returns the mento instance's broker contract
|
|
97
|
+
* @returns broker contract
|
|
98
|
+
*/
|
|
99
|
+
getBroker(): IBroker;
|
|
82
100
|
/**
|
|
83
101
|
* Returns the list of exchanges available in Mento (cached)
|
|
84
102
|
* @returns the list of exchanges
|
|
85
103
|
*/
|
|
86
|
-
|
|
104
|
+
getExchanges(): Promise<Exchange[]>;
|
|
105
|
+
/**
|
|
106
|
+
* Returns the list of exchanges for a given exchange provider address
|
|
107
|
+
* @returns list of exchanges
|
|
108
|
+
*/
|
|
109
|
+
getExchangesForProvider(exchangeProviderAddr: Address): Promise<Exchange[]>;
|
|
87
110
|
/**
|
|
88
111
|
* Returns the Mento exchange (if any) for a given pair of tokens
|
|
89
112
|
* @param token0 the first token
|
|
90
113
|
* @param token1 the second token
|
|
91
|
-
* @returns
|
|
114
|
+
* @returns exchange
|
|
92
115
|
*/
|
|
93
|
-
|
|
116
|
+
getExchangeForTokens(token0: Address, token1: Address): Promise<Exchange>;
|
|
94
117
|
}
|
|
95
|
-
export {};
|
package/dist/cjs/mento.js
CHANGED
|
@@ -10,23 +10,22 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.Mento = void 0;
|
|
13
|
-
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
14
|
-
const ethers_1 = require("ethers");
|
|
15
13
|
const mento_core_ts_1 = require("@mento-protocol/mento-core-ts");
|
|
14
|
+
const ethers_1 = require("ethers");
|
|
16
15
|
const utils_1 = require("./utils");
|
|
17
16
|
const assert_1 = require("assert");
|
|
18
17
|
class Mento {
|
|
19
18
|
/**
|
|
20
|
-
* This constructor is private, use the static create or
|
|
19
|
+
* This constructor is private, use the static create or createWithParams methods
|
|
21
20
|
* to create a new Mento instance
|
|
21
|
+
* @param signerOrProvider an ethers provider or connected signer
|
|
22
22
|
* @param brokerAddress the address of the broker contract
|
|
23
|
-
* @param
|
|
24
|
-
* @param signer an optional ethers signer to execute swaps (must be connected to a provider)
|
|
23
|
+
* @param exchanges exchange data for the broker
|
|
25
24
|
*/
|
|
26
|
-
constructor(brokerAddress,
|
|
27
|
-
this.broker = mento_core_ts_1.IBroker__factory.connect(brokerAddress, signerOrProvider);
|
|
25
|
+
constructor(signerOrProvider, brokerAddress, exchanges) {
|
|
28
26
|
this.signerOrProvider = signerOrProvider;
|
|
29
|
-
this.
|
|
27
|
+
this.broker = mento_core_ts_1.IBroker__factory.connect(brokerAddress, signerOrProvider);
|
|
28
|
+
this.exchanges = exchanges || [];
|
|
30
29
|
}
|
|
31
30
|
/**
|
|
32
31
|
* Creates a new Mento object instance.
|
|
@@ -36,38 +35,30 @@ class Mento {
|
|
|
36
35
|
*/
|
|
37
36
|
static create(signerOrProvider) {
|
|
38
37
|
return __awaiter(this, void 0, void 0, function* () {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
if (!isSigner && !isProvider) {
|
|
42
|
-
throw new Error('A valid signer or provider must be provided');
|
|
43
|
-
}
|
|
44
|
-
if (isSigner) {
|
|
45
|
-
if (!ethers_1.providers.Provider.isProvider(signerOrProvider.provider)) {
|
|
46
|
-
throw new Error('Signer must be connected to a provider');
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
return new Mento(yield (0, utils_1.getBrokerAddressFromRegistry)(signerOrProvider), signerOrProvider);
|
|
38
|
+
(0, utils_1.validateSignerOrProvider)(signerOrProvider);
|
|
39
|
+
return new Mento(signerOrProvider, yield (0, utils_1.getBrokerAddressFromRegistry)(signerOrProvider));
|
|
50
40
|
});
|
|
51
41
|
}
|
|
52
42
|
/**
|
|
53
|
-
* Create a new Mento object instance given a
|
|
54
|
-
* When constructed with
|
|
55
|
-
* @param brokerAddr the address of the broker contract
|
|
43
|
+
* Create a new Mento object instance given a broker address and optional exchanges data
|
|
44
|
+
* When constructed with a Provider, only read-only operations are supported
|
|
56
45
|
* @param signerOrProvider an ethers signer or provider. A signer is required to execute swaps
|
|
46
|
+
* @param brokerAddr the address of the broker contract
|
|
47
|
+
* @param exchanges the exchanges data for the broker
|
|
57
48
|
* @returns a new Mento object instance
|
|
58
49
|
*/
|
|
59
|
-
static
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
return new Mento(
|
|
50
|
+
static createWithParams(signerOrProvider, brokerAddr, exchanges) {
|
|
51
|
+
(0, utils_1.validateSignerOrProvider)(signerOrProvider);
|
|
52
|
+
return new Mento(signerOrProvider, brokerAddr, exchanges);
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Returns a new Mento instance connected to the given signer
|
|
56
|
+
* @param signer an ethers signer
|
|
57
|
+
* @returns new Mento object instance
|
|
58
|
+
*/
|
|
59
|
+
connectSigner(signer) {
|
|
60
|
+
(0, utils_1.validateSigner)(signer);
|
|
61
|
+
return new Mento(signer, this.broker.address, this.exchanges);
|
|
71
62
|
}
|
|
72
63
|
/**
|
|
73
64
|
* Returns a list of all the pairs that can be traded on Mento
|
|
@@ -175,6 +166,13 @@ class Mento {
|
|
|
175
166
|
return this.signerOrProvider.populateTransaction(tx);
|
|
176
167
|
});
|
|
177
168
|
}
|
|
169
|
+
/**
|
|
170
|
+
* Returns the mento instance's broker contract
|
|
171
|
+
* @returns broker contract
|
|
172
|
+
*/
|
|
173
|
+
getBroker() {
|
|
174
|
+
return this.broker;
|
|
175
|
+
}
|
|
178
176
|
/**
|
|
179
177
|
* Returns the list of exchanges available in Mento (cached)
|
|
180
178
|
* @returns the list of exchanges
|
|
@@ -184,29 +182,35 @@ class Mento {
|
|
|
184
182
|
if (this.exchanges.length > 0) {
|
|
185
183
|
return this.exchanges;
|
|
186
184
|
}
|
|
187
|
-
const exchanges = [];
|
|
188
185
|
const exchangeProvidersAddresses = yield this.broker.getExchangeProviders();
|
|
189
|
-
|
|
190
|
-
const exchangeManager = mento_core_ts_1.IExchangeProvider__factory.connect(exchangeProviderAddr, this.signerOrProvider);
|
|
191
|
-
const exchangesInManager = yield exchangeManager.getExchanges();
|
|
192
|
-
for (const exchange of exchangesInManager) {
|
|
193
|
-
(0, assert_1.strict)(exchange.assets.length === 2, 'Exchange must have 2 assets');
|
|
194
|
-
exchanges.push({
|
|
195
|
-
providerAddr: exchangeProviderAddr,
|
|
196
|
-
id: exchange.exchangeId,
|
|
197
|
-
assets: exchange.assets,
|
|
198
|
-
});
|
|
199
|
-
}
|
|
200
|
-
}
|
|
186
|
+
const exchanges = (yield Promise.all(exchangeProvidersAddresses.map((a) => this.getExchangesForProvider(a)))).flat();
|
|
201
187
|
this.exchanges = exchanges;
|
|
202
188
|
return exchanges;
|
|
203
189
|
});
|
|
204
190
|
}
|
|
191
|
+
/**
|
|
192
|
+
* Returns the list of exchanges for a given exchange provider address
|
|
193
|
+
* @returns list of exchanges
|
|
194
|
+
*/
|
|
195
|
+
getExchangesForProvider(exchangeProviderAddr) {
|
|
196
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
197
|
+
const exchangeProvider = mento_core_ts_1.IExchangeProvider__factory.connect(exchangeProviderAddr, this.signerOrProvider);
|
|
198
|
+
const exchangesInProvider = yield exchangeProvider.getExchanges();
|
|
199
|
+
return exchangesInProvider.map((e) => {
|
|
200
|
+
(0, assert_1.strict)(e.assets.length === 2, 'Exchange must have 2 assets');
|
|
201
|
+
return {
|
|
202
|
+
providerAddr: exchangeProviderAddr,
|
|
203
|
+
id: e.exchangeId,
|
|
204
|
+
assets: e.assets,
|
|
205
|
+
};
|
|
206
|
+
});
|
|
207
|
+
});
|
|
208
|
+
}
|
|
205
209
|
/**
|
|
206
210
|
* Returns the Mento exchange (if any) for a given pair of tokens
|
|
207
211
|
* @param token0 the first token
|
|
208
212
|
* @param token1 the second token
|
|
209
|
-
* @returns
|
|
213
|
+
* @returns exchange
|
|
210
214
|
*/
|
|
211
215
|
getExchangeForTokens(token0, token1) {
|
|
212
216
|
return __awaiter(this, void 0, void 0, function* () {
|
package/dist/cjs/utils.d.ts
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
|
-
import { BigNumber,
|
|
1
|
+
import { BigNumber, providers, Signer } from 'ethers';
|
|
2
2
|
import { Address } from './types';
|
|
3
|
+
/**
|
|
4
|
+
* Ensures that given signer is truly a a connected signer
|
|
5
|
+
* @param signer an ethers signer
|
|
6
|
+
* @throws if signer is invalid or not connected
|
|
7
|
+
*/
|
|
8
|
+
export declare function validateSigner(signer: Signer): void;
|
|
9
|
+
/**
|
|
10
|
+
* Ensures that given signerOrProvider is truly a provider or a connected signer
|
|
11
|
+
* @param signerOrProvider an ethers provider or signer
|
|
12
|
+
* @throws if signerOrProvider is invalid or not connected
|
|
13
|
+
*/
|
|
14
|
+
export declare function validateSignerOrProvider(signerOrProvider: Signer | providers.Provider): void;
|
|
3
15
|
/**
|
|
4
16
|
* Returns the broker address from the Celo registry
|
|
5
17
|
* @param signerOrProvider an ethers provider or signer
|
package/dist/cjs/utils.js
CHANGED
|
@@ -9,9 +9,38 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.increaseAllowance = exports.getSymbolFromTokenAddress = exports.getBrokerAddressFromRegistry = void 0;
|
|
13
|
-
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
12
|
+
exports.increaseAllowance = exports.getSymbolFromTokenAddress = exports.getBrokerAddressFromRegistry = exports.validateSignerOrProvider = exports.validateSigner = void 0;
|
|
14
13
|
const ethers_1 = require("ethers");
|
|
14
|
+
/**
|
|
15
|
+
* Ensures that given signer is truly a a connected signer
|
|
16
|
+
* @param signer an ethers signer
|
|
17
|
+
* @throws if signer is invalid or not connected
|
|
18
|
+
*/
|
|
19
|
+
function validateSigner(signer) {
|
|
20
|
+
if (!ethers_1.Signer.isSigner(signer)) {
|
|
21
|
+
throw new Error('A valid signer must be provided');
|
|
22
|
+
}
|
|
23
|
+
if (!ethers_1.providers.Provider.isProvider(signer.provider)) {
|
|
24
|
+
throw new Error('Signer must be connected to a provider');
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
exports.validateSigner = validateSigner;
|
|
28
|
+
/**
|
|
29
|
+
* Ensures that given signerOrProvider is truly a provider or a connected signer
|
|
30
|
+
* @param signerOrProvider an ethers provider or signer
|
|
31
|
+
* @throws if signerOrProvider is invalid or not connected
|
|
32
|
+
*/
|
|
33
|
+
function validateSignerOrProvider(signerOrProvider) {
|
|
34
|
+
const isSigner = ethers_1.Signer.isSigner(signerOrProvider);
|
|
35
|
+
const isProvider = ethers_1.providers.Provider.isProvider(signerOrProvider);
|
|
36
|
+
if (!isSigner && !isProvider) {
|
|
37
|
+
throw new Error('A valid signer or provider must be provided');
|
|
38
|
+
}
|
|
39
|
+
if (isSigner && !ethers_1.providers.Provider.isProvider(signerOrProvider.provider)) {
|
|
40
|
+
throw new Error('Signer must be connected to a provider');
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
exports.validateSignerOrProvider = validateSignerOrProvider;
|
|
15
44
|
/**
|
|
16
45
|
* Returns the broker address from the Celo registry
|
|
17
46
|
* @param signerOrProvider an ethers provider or signer
|
package/dist/esm/mento.d.ts
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { IBroker } from '@mento-protocol/mento-core-ts';
|
|
2
|
+
import { BigNumber, providers, Signer } from 'ethers';
|
|
2
3
|
import { Address } from './types';
|
|
3
|
-
interface
|
|
4
|
+
export interface Exchange {
|
|
5
|
+
providerAddr: Address;
|
|
6
|
+
id: string;
|
|
7
|
+
assets: Address[];
|
|
8
|
+
}
|
|
9
|
+
export interface Asset {
|
|
4
10
|
address: Address;
|
|
5
11
|
symbol: string;
|
|
6
12
|
}
|
|
@@ -9,11 +15,11 @@ export declare class Mento {
|
|
|
9
15
|
private readonly broker;
|
|
10
16
|
private exchanges;
|
|
11
17
|
/**
|
|
12
|
-
* This constructor is private, use the static create or
|
|
18
|
+
* This constructor is private, use the static create or createWithParams methods
|
|
13
19
|
* to create a new Mento instance
|
|
20
|
+
* @param signerOrProvider an ethers provider or connected signer
|
|
14
21
|
* @param brokerAddress the address of the broker contract
|
|
15
|
-
* @param
|
|
16
|
-
* @param signer an optional ethers signer to execute swaps (must be connected to a provider)
|
|
22
|
+
* @param exchanges exchange data for the broker
|
|
17
23
|
*/
|
|
18
24
|
private constructor();
|
|
19
25
|
/**
|
|
@@ -24,13 +30,20 @@ export declare class Mento {
|
|
|
24
30
|
*/
|
|
25
31
|
static create(signerOrProvider: Signer | providers.Provider): Promise<Mento>;
|
|
26
32
|
/**
|
|
27
|
-
* Create a new Mento object instance given a
|
|
28
|
-
* When constructed with
|
|
29
|
-
* @param brokerAddr the address of the broker contract
|
|
33
|
+
* Create a new Mento object instance given a broker address and optional exchanges data
|
|
34
|
+
* When constructed with a Provider, only read-only operations are supported
|
|
30
35
|
* @param signerOrProvider an ethers signer or provider. A signer is required to execute swaps
|
|
36
|
+
* @param brokerAddr the address of the broker contract
|
|
37
|
+
* @param exchanges the exchanges data for the broker
|
|
31
38
|
* @returns a new Mento object instance
|
|
32
39
|
*/
|
|
33
|
-
static
|
|
40
|
+
static createWithParams(signerOrProvider: Signer | providers.Provider, brokerAddr: Address, exchanges?: Exchange[]): Mento;
|
|
41
|
+
/**
|
|
42
|
+
* Returns a new Mento instance connected to the given signer
|
|
43
|
+
* @param signer an ethers signer
|
|
44
|
+
* @returns new Mento object instance
|
|
45
|
+
*/
|
|
46
|
+
connectSigner(signer: Signer): Mento;
|
|
34
47
|
/**
|
|
35
48
|
* Returns a list of all the pairs that can be traded on Mento
|
|
36
49
|
* @returns The list of tradeable pairs in the form of [{address, symbol}]
|
|
@@ -79,17 +92,26 @@ export declare class Mento {
|
|
|
79
92
|
* @returns the populated TransactionRequest object
|
|
80
93
|
*/
|
|
81
94
|
swapOut(tokenIn: Address, tokenOut: Address, amountOut: BigNumber, amountInMax: BigNumber): Promise<providers.TransactionRequest>;
|
|
95
|
+
/**
|
|
96
|
+
* Returns the mento instance's broker contract
|
|
97
|
+
* @returns broker contract
|
|
98
|
+
*/
|
|
99
|
+
getBroker(): IBroker;
|
|
82
100
|
/**
|
|
83
101
|
* Returns the list of exchanges available in Mento (cached)
|
|
84
102
|
* @returns the list of exchanges
|
|
85
103
|
*/
|
|
86
|
-
|
|
104
|
+
getExchanges(): Promise<Exchange[]>;
|
|
105
|
+
/**
|
|
106
|
+
* Returns the list of exchanges for a given exchange provider address
|
|
107
|
+
* @returns list of exchanges
|
|
108
|
+
*/
|
|
109
|
+
getExchangesForProvider(exchangeProviderAddr: Address): Promise<Exchange[]>;
|
|
87
110
|
/**
|
|
88
111
|
* Returns the Mento exchange (if any) for a given pair of tokens
|
|
89
112
|
* @param token0 the first token
|
|
90
113
|
* @param token1 the second token
|
|
91
|
-
* @returns
|
|
114
|
+
* @returns exchange
|
|
92
115
|
*/
|
|
93
|
-
|
|
116
|
+
getExchangeForTokens(token0: Address, token1: Address): Promise<Exchange>;
|
|
94
117
|
}
|
|
95
|
-
export {};
|
package/dist/esm/mento.js
CHANGED
|
@@ -7,23 +7,22 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
7
7
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
|
-
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
11
|
-
import { Signer, providers } from 'ethers';
|
|
12
10
|
import { IBroker__factory, IExchangeProvider__factory, } from '@mento-protocol/mento-core-ts';
|
|
13
|
-
import {
|
|
11
|
+
import { Signer } from 'ethers';
|
|
12
|
+
import { getBrokerAddressFromRegistry, getSymbolFromTokenAddress, increaseAllowance, validateSigner, validateSignerOrProvider, } from './utils';
|
|
14
13
|
import { strict as assert } from 'assert';
|
|
15
14
|
export class Mento {
|
|
16
15
|
/**
|
|
17
|
-
* This constructor is private, use the static create or
|
|
16
|
+
* This constructor is private, use the static create or createWithParams methods
|
|
18
17
|
* to create a new Mento instance
|
|
18
|
+
* @param signerOrProvider an ethers provider or connected signer
|
|
19
19
|
* @param brokerAddress the address of the broker contract
|
|
20
|
-
* @param
|
|
21
|
-
* @param signer an optional ethers signer to execute swaps (must be connected to a provider)
|
|
20
|
+
* @param exchanges exchange data for the broker
|
|
22
21
|
*/
|
|
23
|
-
constructor(brokerAddress,
|
|
24
|
-
this.broker = IBroker__factory.connect(brokerAddress, signerOrProvider);
|
|
22
|
+
constructor(signerOrProvider, brokerAddress, exchanges) {
|
|
25
23
|
this.signerOrProvider = signerOrProvider;
|
|
26
|
-
this.
|
|
24
|
+
this.broker = IBroker__factory.connect(brokerAddress, signerOrProvider);
|
|
25
|
+
this.exchanges = exchanges || [];
|
|
27
26
|
}
|
|
28
27
|
/**
|
|
29
28
|
* Creates a new Mento object instance.
|
|
@@ -33,38 +32,30 @@ export class Mento {
|
|
|
33
32
|
*/
|
|
34
33
|
static create(signerOrProvider) {
|
|
35
34
|
return __awaiter(this, void 0, void 0, function* () {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
if (!isSigner && !isProvider) {
|
|
39
|
-
throw new Error('A valid signer or provider must be provided');
|
|
40
|
-
}
|
|
41
|
-
if (isSigner) {
|
|
42
|
-
if (!providers.Provider.isProvider(signerOrProvider.provider)) {
|
|
43
|
-
throw new Error('Signer must be connected to a provider');
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
return new Mento(yield getBrokerAddressFromRegistry(signerOrProvider), signerOrProvider);
|
|
35
|
+
validateSignerOrProvider(signerOrProvider);
|
|
36
|
+
return new Mento(signerOrProvider, yield getBrokerAddressFromRegistry(signerOrProvider));
|
|
47
37
|
});
|
|
48
38
|
}
|
|
49
39
|
/**
|
|
50
|
-
* Create a new Mento object instance given a
|
|
51
|
-
* When constructed with
|
|
52
|
-
* @param brokerAddr the address of the broker contract
|
|
40
|
+
* Create a new Mento object instance given a broker address and optional exchanges data
|
|
41
|
+
* When constructed with a Provider, only read-only operations are supported
|
|
53
42
|
* @param signerOrProvider an ethers signer or provider. A signer is required to execute swaps
|
|
43
|
+
* @param brokerAddr the address of the broker contract
|
|
44
|
+
* @param exchanges the exchanges data for the broker
|
|
54
45
|
* @returns a new Mento object instance
|
|
55
46
|
*/
|
|
56
|
-
static
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
return new Mento(
|
|
47
|
+
static createWithParams(signerOrProvider, brokerAddr, exchanges) {
|
|
48
|
+
validateSignerOrProvider(signerOrProvider);
|
|
49
|
+
return new Mento(signerOrProvider, brokerAddr, exchanges);
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Returns a new Mento instance connected to the given signer
|
|
53
|
+
* @param signer an ethers signer
|
|
54
|
+
* @returns new Mento object instance
|
|
55
|
+
*/
|
|
56
|
+
connectSigner(signer) {
|
|
57
|
+
validateSigner(signer);
|
|
58
|
+
return new Mento(signer, this.broker.address, this.exchanges);
|
|
68
59
|
}
|
|
69
60
|
/**
|
|
70
61
|
* Returns a list of all the pairs that can be traded on Mento
|
|
@@ -172,6 +163,13 @@ export class Mento {
|
|
|
172
163
|
return this.signerOrProvider.populateTransaction(tx);
|
|
173
164
|
});
|
|
174
165
|
}
|
|
166
|
+
/**
|
|
167
|
+
* Returns the mento instance's broker contract
|
|
168
|
+
* @returns broker contract
|
|
169
|
+
*/
|
|
170
|
+
getBroker() {
|
|
171
|
+
return this.broker;
|
|
172
|
+
}
|
|
175
173
|
/**
|
|
176
174
|
* Returns the list of exchanges available in Mento (cached)
|
|
177
175
|
* @returns the list of exchanges
|
|
@@ -181,29 +179,35 @@ export class Mento {
|
|
|
181
179
|
if (this.exchanges.length > 0) {
|
|
182
180
|
return this.exchanges;
|
|
183
181
|
}
|
|
184
|
-
const exchanges = [];
|
|
185
182
|
const exchangeProvidersAddresses = yield this.broker.getExchangeProviders();
|
|
186
|
-
|
|
187
|
-
const exchangeManager = IExchangeProvider__factory.connect(exchangeProviderAddr, this.signerOrProvider);
|
|
188
|
-
const exchangesInManager = yield exchangeManager.getExchanges();
|
|
189
|
-
for (const exchange of exchangesInManager) {
|
|
190
|
-
assert(exchange.assets.length === 2, 'Exchange must have 2 assets');
|
|
191
|
-
exchanges.push({
|
|
192
|
-
providerAddr: exchangeProviderAddr,
|
|
193
|
-
id: exchange.exchangeId,
|
|
194
|
-
assets: exchange.assets,
|
|
195
|
-
});
|
|
196
|
-
}
|
|
197
|
-
}
|
|
183
|
+
const exchanges = (yield Promise.all(exchangeProvidersAddresses.map((a) => this.getExchangesForProvider(a)))).flat();
|
|
198
184
|
this.exchanges = exchanges;
|
|
199
185
|
return exchanges;
|
|
200
186
|
});
|
|
201
187
|
}
|
|
188
|
+
/**
|
|
189
|
+
* Returns the list of exchanges for a given exchange provider address
|
|
190
|
+
* @returns list of exchanges
|
|
191
|
+
*/
|
|
192
|
+
getExchangesForProvider(exchangeProviderAddr) {
|
|
193
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
194
|
+
const exchangeProvider = IExchangeProvider__factory.connect(exchangeProviderAddr, this.signerOrProvider);
|
|
195
|
+
const exchangesInProvider = yield exchangeProvider.getExchanges();
|
|
196
|
+
return exchangesInProvider.map((e) => {
|
|
197
|
+
assert(e.assets.length === 2, 'Exchange must have 2 assets');
|
|
198
|
+
return {
|
|
199
|
+
providerAddr: exchangeProviderAddr,
|
|
200
|
+
id: e.exchangeId,
|
|
201
|
+
assets: e.assets,
|
|
202
|
+
};
|
|
203
|
+
});
|
|
204
|
+
});
|
|
205
|
+
}
|
|
202
206
|
/**
|
|
203
207
|
* Returns the Mento exchange (if any) for a given pair of tokens
|
|
204
208
|
* @param token0 the first token
|
|
205
209
|
* @param token1 the second token
|
|
206
|
-
* @returns
|
|
210
|
+
* @returns exchange
|
|
207
211
|
*/
|
|
208
212
|
getExchangeForTokens(token0, token1) {
|
|
209
213
|
return __awaiter(this, void 0, void 0, function* () {
|