@mento-protocol/mento-sdk 0.1.2 → 0.1.4
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/README.md +63 -48
- package/dist/cjs/mento.d.ts +40 -18
- package/dist/cjs/mento.js +73 -62
- package/dist/cjs/utils.d.ts +15 -3
- package/dist/cjs/utils.js +36 -4
- package/dist/esm/mento.d.ts +40 -18
- package/dist/esm/mento.js +74 -63
- package/dist/esm/utils.d.ts +15 -3
- package/dist/esm/utils.js +34 -4
- package/package.json +13 -8
package/README.md
CHANGED
|
@@ -2,57 +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
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
const
|
|
48
|
-
const
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
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)
|
|
50
|
+
|
|
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)
|
|
56
|
+
|
|
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))
|
|
58
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, BigNumberish, 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}]
|
|
@@ -43,7 +56,7 @@ export declare class Mento {
|
|
|
43
56
|
* @param amountOut the amount of tokenOut to be bought
|
|
44
57
|
* @returns the amount of tokenIn to be sold
|
|
45
58
|
*/
|
|
46
|
-
getAmountIn(tokenIn: Address, tokenOut: Address, amountOut:
|
|
59
|
+
getAmountIn(tokenIn: Address, tokenOut: Address, amountOut: BigNumberish): Promise<BigNumber>;
|
|
47
60
|
/**
|
|
48
61
|
* Returns the amount of tokenOut to be bought by selling amountIn of tokenIn
|
|
49
62
|
* @param tokenIn the token to be sold
|
|
@@ -51,14 +64,14 @@ export declare class Mento {
|
|
|
51
64
|
* @param amountIn the amount of tokenIn to be sold
|
|
52
65
|
* @returns the amount of tokenOut to be bought
|
|
53
66
|
*/
|
|
54
|
-
getAmountOut(tokenIn: Address, tokenOut: Address, amountIn:
|
|
67
|
+
getAmountOut(tokenIn: Address, tokenOut: Address, amountIn: BigNumberish): Promise<BigNumber>;
|
|
55
68
|
/**
|
|
56
69
|
* Increases the broker's trading allowance for the given token
|
|
57
70
|
* @param token the token to increase the allowance for
|
|
58
71
|
* @param amount the amount to increase the allowance by
|
|
59
72
|
* @returns the populated TransactionRequest object
|
|
60
73
|
*/
|
|
61
|
-
increaseTradingAllowance(token: Address, amount:
|
|
74
|
+
increaseTradingAllowance(token: Address, amount: BigNumberish): Promise<providers.TransactionRequest>;
|
|
62
75
|
/**
|
|
63
76
|
* Returns a token swap populated tx object with a fixed amount of tokenIn and a minimum amount of tokenOut
|
|
64
77
|
* Submitting the transaction to execute the swap is left to the consumer
|
|
@@ -68,7 +81,7 @@ export declare class Mento {
|
|
|
68
81
|
* @param amountOutMin the minimum amount of tokenOut to be bought
|
|
69
82
|
* @returns the populated TransactionRequest object
|
|
70
83
|
*/
|
|
71
|
-
swapIn(tokenIn: Address, tokenOut: Address, amountIn:
|
|
84
|
+
swapIn(tokenIn: Address, tokenOut: Address, amountIn: BigNumberish, amountOutMin: BigNumberish): Promise<providers.TransactionRequest>;
|
|
72
85
|
/**
|
|
73
86
|
* Returns a token swap populated tx object with a maximum amount of tokenIn and a fixed amount of tokenOut
|
|
74
87
|
* Submitting the transaction to execute the swap is left to the consumer
|
|
@@ -78,18 +91,27 @@ export declare class Mento {
|
|
|
78
91
|
* @param amountInMax the maximum amount of tokenIn to be sold
|
|
79
92
|
* @returns the populated TransactionRequest object
|
|
80
93
|
*/
|
|
81
|
-
swapOut(tokenIn: Address, tokenOut: Address, amountOut:
|
|
94
|
+
swapOut(tokenIn: Address, tokenOut: Address, amountOut: BigNumberish, amountInMax: BigNumberish): 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,22 +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
|
-
const ethers_1 = require("ethers");
|
|
14
13
|
const mento_core_ts_1 = require("@mento-protocol/mento-core-ts");
|
|
14
|
+
const ethers_1 = require("ethers");
|
|
15
15
|
const utils_1 = require("./utils");
|
|
16
16
|
const assert_1 = require("assert");
|
|
17
17
|
class Mento {
|
|
18
18
|
/**
|
|
19
|
-
* This constructor is private, use the static create or
|
|
19
|
+
* This constructor is private, use the static create or createWithParams methods
|
|
20
20
|
* to create a new Mento instance
|
|
21
|
+
* @param signerOrProvider an ethers provider or connected signer
|
|
21
22
|
* @param brokerAddress the address of the broker contract
|
|
22
|
-
* @param
|
|
23
|
-
* @param signer an optional ethers signer to execute swaps (must be connected to a provider)
|
|
23
|
+
* @param exchanges exchange data for the broker
|
|
24
24
|
*/
|
|
25
|
-
constructor(brokerAddress,
|
|
26
|
-
this.broker = mento_core_ts_1.IBroker__factory.connect(brokerAddress, signerOrProvider);
|
|
25
|
+
constructor(signerOrProvider, brokerAddress, exchanges) {
|
|
27
26
|
this.signerOrProvider = signerOrProvider;
|
|
28
|
-
this.
|
|
27
|
+
this.broker = mento_core_ts_1.IBroker__factory.connect(brokerAddress, signerOrProvider);
|
|
28
|
+
this.exchanges = exchanges || [];
|
|
29
29
|
}
|
|
30
30
|
/**
|
|
31
31
|
* Creates a new Mento object instance.
|
|
@@ -35,38 +35,30 @@ class Mento {
|
|
|
35
35
|
*/
|
|
36
36
|
static create(signerOrProvider) {
|
|
37
37
|
return __awaiter(this, void 0, void 0, function* () {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
if (!isSigner && !isProvider) {
|
|
41
|
-
throw new Error('A valid signer or provider must be provided');
|
|
42
|
-
}
|
|
43
|
-
if (isSigner) {
|
|
44
|
-
if (!ethers_1.providers.Provider.isProvider(signerOrProvider.provider)) {
|
|
45
|
-
throw new Error('Signer must be connected to a provider');
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
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));
|
|
49
40
|
});
|
|
50
41
|
}
|
|
51
42
|
/**
|
|
52
|
-
* Create a new Mento object instance given a
|
|
53
|
-
* When constructed with
|
|
54
|
-
* @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
|
|
55
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
|
|
56
48
|
* @returns a new Mento object instance
|
|
57
49
|
*/
|
|
58
|
-
static
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
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);
|
|
70
62
|
}
|
|
71
63
|
/**
|
|
72
64
|
* Returns a list of all the pairs that can be traded on Mento
|
|
@@ -125,13 +117,15 @@ class Mento {
|
|
|
125
117
|
*/
|
|
126
118
|
increaseTradingAllowance(token, amount) {
|
|
127
119
|
return __awaiter(this, void 0, void 0, function* () {
|
|
128
|
-
if (!ethers_1.Signer.isSigner(this.signerOrProvider)) {
|
|
129
|
-
throw new Error('A signer is required to populate the increaseAllowance tx object');
|
|
130
|
-
}
|
|
131
120
|
const spender = this.broker.address;
|
|
132
121
|
const tx = yield (0, utils_1.increaseAllowance)(token, spender, amount, this.signerOrProvider);
|
|
133
|
-
|
|
134
|
-
|
|
122
|
+
if (ethers_1.Signer.isSigner(this.signerOrProvider)) {
|
|
123
|
+
// The contract call doesn't populate all of the signer fields, so we need an extra call for the signer
|
|
124
|
+
return this.signerOrProvider.populateTransaction(tx);
|
|
125
|
+
}
|
|
126
|
+
else {
|
|
127
|
+
return tx;
|
|
128
|
+
}
|
|
135
129
|
});
|
|
136
130
|
}
|
|
137
131
|
/**
|
|
@@ -145,13 +139,15 @@ class Mento {
|
|
|
145
139
|
*/
|
|
146
140
|
swapIn(tokenIn, tokenOut, amountIn, amountOutMin) {
|
|
147
141
|
return __awaiter(this, void 0, void 0, function* () {
|
|
148
|
-
if (!ethers_1.Signer.isSigner(this.signerOrProvider)) {
|
|
149
|
-
throw new Error('A signer is required to populate the swapIn tx object');
|
|
150
|
-
}
|
|
151
142
|
const exchange = yield this.getExchangeForTokens(tokenIn, tokenOut);
|
|
152
143
|
const tx = yield this.broker.populateTransaction.swapIn(exchange.providerAddr, exchange.id, tokenIn, tokenOut, amountIn, amountOutMin);
|
|
153
|
-
|
|
154
|
-
|
|
144
|
+
if (ethers_1.Signer.isSigner(this.signerOrProvider)) {
|
|
145
|
+
// The contract call doesn't populate all of the signer fields, so we need an extra call for the signer
|
|
146
|
+
return this.signerOrProvider.populateTransaction(tx);
|
|
147
|
+
}
|
|
148
|
+
else {
|
|
149
|
+
return tx;
|
|
150
|
+
}
|
|
155
151
|
});
|
|
156
152
|
}
|
|
157
153
|
/**
|
|
@@ -165,15 +161,24 @@ class Mento {
|
|
|
165
161
|
*/
|
|
166
162
|
swapOut(tokenIn, tokenOut, amountOut, amountInMax) {
|
|
167
163
|
return __awaiter(this, void 0, void 0, function* () {
|
|
168
|
-
if (!ethers_1.Signer.isSigner(this.signerOrProvider)) {
|
|
169
|
-
throw new Error('A signer is required to populate the swapOut tx object');
|
|
170
|
-
}
|
|
171
164
|
const exchange = yield this.getExchangeForTokens(tokenIn, tokenOut);
|
|
172
165
|
const tx = yield this.broker.populateTransaction.swapOut(exchange.providerAddr, exchange.id, tokenIn, tokenOut, amountOut, amountInMax);
|
|
173
|
-
|
|
174
|
-
|
|
166
|
+
if (ethers_1.Signer.isSigner(this.signerOrProvider)) {
|
|
167
|
+
// The contract call doesn't populate all of the signer fields, so we need an extra call for the signer
|
|
168
|
+
return this.signerOrProvider.populateTransaction(tx);
|
|
169
|
+
}
|
|
170
|
+
else {
|
|
171
|
+
return tx;
|
|
172
|
+
}
|
|
175
173
|
});
|
|
176
174
|
}
|
|
175
|
+
/**
|
|
176
|
+
* Returns the mento instance's broker contract
|
|
177
|
+
* @returns broker contract
|
|
178
|
+
*/
|
|
179
|
+
getBroker() {
|
|
180
|
+
return this.broker;
|
|
181
|
+
}
|
|
177
182
|
/**
|
|
178
183
|
* Returns the list of exchanges available in Mento (cached)
|
|
179
184
|
* @returns the list of exchanges
|
|
@@ -183,29 +188,35 @@ class Mento {
|
|
|
183
188
|
if (this.exchanges.length > 0) {
|
|
184
189
|
return this.exchanges;
|
|
185
190
|
}
|
|
186
|
-
const exchanges = [];
|
|
187
191
|
const exchangeProvidersAddresses = yield this.broker.getExchangeProviders();
|
|
188
|
-
|
|
189
|
-
const exchangeManager = mento_core_ts_1.IExchangeProvider__factory.connect(exchangeProviderAddr, this.signerOrProvider);
|
|
190
|
-
const exchangesInManager = yield exchangeManager.getExchanges();
|
|
191
|
-
for (const exchange of exchangesInManager) {
|
|
192
|
-
(0, assert_1.strict)(exchange.assets.length === 2, 'Exchange must have 2 assets');
|
|
193
|
-
exchanges.push({
|
|
194
|
-
providerAddr: exchangeProviderAddr,
|
|
195
|
-
id: exchange.exchangeId,
|
|
196
|
-
assets: exchange.assets,
|
|
197
|
-
});
|
|
198
|
-
}
|
|
199
|
-
}
|
|
192
|
+
const exchanges = (yield Promise.all(exchangeProvidersAddresses.map((a) => this.getExchangesForProvider(a)))).flat();
|
|
200
193
|
this.exchanges = exchanges;
|
|
201
194
|
return exchanges;
|
|
202
195
|
});
|
|
203
196
|
}
|
|
197
|
+
/**
|
|
198
|
+
* Returns the list of exchanges for a given exchange provider address
|
|
199
|
+
* @returns list of exchanges
|
|
200
|
+
*/
|
|
201
|
+
getExchangesForProvider(exchangeProviderAddr) {
|
|
202
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
203
|
+
const exchangeProvider = mento_core_ts_1.IExchangeProvider__factory.connect(exchangeProviderAddr, this.signerOrProvider);
|
|
204
|
+
const exchangesInProvider = yield exchangeProvider.getExchanges();
|
|
205
|
+
return exchangesInProvider.map((e) => {
|
|
206
|
+
(0, assert_1.strict)(e.assets.length === 2, 'Exchange must have 2 assets');
|
|
207
|
+
return {
|
|
208
|
+
providerAddr: exchangeProviderAddr,
|
|
209
|
+
id: e.exchangeId,
|
|
210
|
+
assets: e.assets,
|
|
211
|
+
};
|
|
212
|
+
});
|
|
213
|
+
});
|
|
214
|
+
}
|
|
204
215
|
/**
|
|
205
216
|
* Returns the Mento exchange (if any) for a given pair of tokens
|
|
206
217
|
* @param token0 the first token
|
|
207
218
|
* @param token1 the second token
|
|
208
|
-
* @returns
|
|
219
|
+
* @returns exchange
|
|
209
220
|
*/
|
|
210
221
|
getExchangeForTokens(token0, token1) {
|
|
211
222
|
return __awaiter(this, void 0, void 0, function* () {
|
package/dist/cjs/utils.d.ts
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { BigNumberish, 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
|
|
@@ -18,7 +30,7 @@ export declare function getSymbolFromTokenAddress(tokenAddr: Address, signerOrPr
|
|
|
18
30
|
* @param tokenAddr the address of the erc20 token
|
|
19
31
|
* @param spender the address of the spender
|
|
20
32
|
* @param amount the amount to increase the allowance by
|
|
21
|
-
* @param
|
|
33
|
+
* @param signerOrProvider an ethers signer or provider
|
|
22
34
|
* @returns the populated TransactionRequest object
|
|
23
35
|
*/
|
|
24
|
-
export declare function increaseAllowance(tokenAddr: string, spender: string, amount:
|
|
36
|
+
export declare function increaseAllowance(tokenAddr: string, spender: string, amount: BigNumberish, signerOrProvider: Signer | providers.Provider): Promise<providers.TransactionRequest>;
|
package/dist/cjs/utils.js
CHANGED
|
@@ -9,8 +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;
|
|
12
|
+
exports.increaseAllowance = exports.getSymbolFromTokenAddress = exports.getBrokerAddressFromRegistry = exports.validateSignerOrProvider = exports.validateSigner = void 0;
|
|
13
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;
|
|
14
44
|
/**
|
|
15
45
|
* Returns the broker address from the Celo registry
|
|
16
46
|
* @param signerOrProvider an ethers provider or signer
|
|
@@ -51,15 +81,17 @@ exports.getSymbolFromTokenAddress = getSymbolFromTokenAddress;
|
|
|
51
81
|
* @param tokenAddr the address of the erc20 token
|
|
52
82
|
* @param spender the address of the spender
|
|
53
83
|
* @param amount the amount to increase the allowance by
|
|
54
|
-
* @param
|
|
84
|
+
* @param signerOrProvider an ethers signer or provider
|
|
55
85
|
* @returns the populated TransactionRequest object
|
|
56
86
|
*/
|
|
57
|
-
function increaseAllowance(tokenAddr, spender, amount,
|
|
87
|
+
function increaseAllowance(tokenAddr, spender, amount, signerOrProvider) {
|
|
58
88
|
return __awaiter(this, void 0, void 0, function* () {
|
|
59
89
|
const abi = [
|
|
60
90
|
'function increaseAllowance(address spender, uint256 value) external returns (bool)',
|
|
61
91
|
];
|
|
62
|
-
|
|
92
|
+
// TODO, not all ERC-20 contracts support increaseAllowance
|
|
93
|
+
// Add a check for that here
|
|
94
|
+
const contract = new ethers_1.Contract(tokenAddr, abi, signerOrProvider);
|
|
63
95
|
return yield contract.populateTransaction.increaseAllowance(spender, amount);
|
|
64
96
|
});
|
|
65
97
|
}
|
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, BigNumberish, 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}]
|
|
@@ -43,7 +56,7 @@ export declare class Mento {
|
|
|
43
56
|
* @param amountOut the amount of tokenOut to be bought
|
|
44
57
|
* @returns the amount of tokenIn to be sold
|
|
45
58
|
*/
|
|
46
|
-
getAmountIn(tokenIn: Address, tokenOut: Address, amountOut:
|
|
59
|
+
getAmountIn(tokenIn: Address, tokenOut: Address, amountOut: BigNumberish): Promise<BigNumber>;
|
|
47
60
|
/**
|
|
48
61
|
* Returns the amount of tokenOut to be bought by selling amountIn of tokenIn
|
|
49
62
|
* @param tokenIn the token to be sold
|
|
@@ -51,14 +64,14 @@ export declare class Mento {
|
|
|
51
64
|
* @param amountIn the amount of tokenIn to be sold
|
|
52
65
|
* @returns the amount of tokenOut to be bought
|
|
53
66
|
*/
|
|
54
|
-
getAmountOut(tokenIn: Address, tokenOut: Address, amountIn:
|
|
67
|
+
getAmountOut(tokenIn: Address, tokenOut: Address, amountIn: BigNumberish): Promise<BigNumber>;
|
|
55
68
|
/**
|
|
56
69
|
* Increases the broker's trading allowance for the given token
|
|
57
70
|
* @param token the token to increase the allowance for
|
|
58
71
|
* @param amount the amount to increase the allowance by
|
|
59
72
|
* @returns the populated TransactionRequest object
|
|
60
73
|
*/
|
|
61
|
-
increaseTradingAllowance(token: Address, amount:
|
|
74
|
+
increaseTradingAllowance(token: Address, amount: BigNumberish): Promise<providers.TransactionRequest>;
|
|
62
75
|
/**
|
|
63
76
|
* Returns a token swap populated tx object with a fixed amount of tokenIn and a minimum amount of tokenOut
|
|
64
77
|
* Submitting the transaction to execute the swap is left to the consumer
|
|
@@ -68,7 +81,7 @@ export declare class Mento {
|
|
|
68
81
|
* @param amountOutMin the minimum amount of tokenOut to be bought
|
|
69
82
|
* @returns the populated TransactionRequest object
|
|
70
83
|
*/
|
|
71
|
-
swapIn(tokenIn: Address, tokenOut: Address, amountIn:
|
|
84
|
+
swapIn(tokenIn: Address, tokenOut: Address, amountIn: BigNumberish, amountOutMin: BigNumberish): Promise<providers.TransactionRequest>;
|
|
72
85
|
/**
|
|
73
86
|
* Returns a token swap populated tx object with a maximum amount of tokenIn and a fixed amount of tokenOut
|
|
74
87
|
* Submitting the transaction to execute the swap is left to the consumer
|
|
@@ -78,18 +91,27 @@ export declare class Mento {
|
|
|
78
91
|
* @param amountInMax the maximum amount of tokenIn to be sold
|
|
79
92
|
* @returns the populated TransactionRequest object
|
|
80
93
|
*/
|
|
81
|
-
swapOut(tokenIn: Address, tokenOut: Address, amountOut:
|
|
94
|
+
swapOut(tokenIn: Address, tokenOut: Address, amountOut: BigNumberish, amountInMax: BigNumberish): 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,22 +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
|
-
import { Signer, providers } from 'ethers';
|
|
11
10
|
import { IBroker__factory, IExchangeProvider__factory, } from '@mento-protocol/mento-core-ts';
|
|
12
|
-
import {
|
|
11
|
+
import { Signer } from 'ethers';
|
|
12
|
+
import { getBrokerAddressFromRegistry, getSymbolFromTokenAddress, increaseAllowance, validateSigner, validateSignerOrProvider, } from './utils';
|
|
13
13
|
import { strict as assert } from 'assert';
|
|
14
14
|
export class Mento {
|
|
15
15
|
/**
|
|
16
|
-
* This constructor is private, use the static create or
|
|
16
|
+
* This constructor is private, use the static create or createWithParams methods
|
|
17
17
|
* to create a new Mento instance
|
|
18
|
+
* @param signerOrProvider an ethers provider or connected signer
|
|
18
19
|
* @param brokerAddress the address of the broker contract
|
|
19
|
-
* @param
|
|
20
|
-
* @param signer an optional ethers signer to execute swaps (must be connected to a provider)
|
|
20
|
+
* @param exchanges exchange data for the broker
|
|
21
21
|
*/
|
|
22
|
-
constructor(brokerAddress,
|
|
23
|
-
this.broker = IBroker__factory.connect(brokerAddress, signerOrProvider);
|
|
22
|
+
constructor(signerOrProvider, brokerAddress, exchanges) {
|
|
24
23
|
this.signerOrProvider = signerOrProvider;
|
|
25
|
-
this.
|
|
24
|
+
this.broker = IBroker__factory.connect(brokerAddress, signerOrProvider);
|
|
25
|
+
this.exchanges = exchanges || [];
|
|
26
26
|
}
|
|
27
27
|
/**
|
|
28
28
|
* Creates a new Mento object instance.
|
|
@@ -32,38 +32,30 @@ export class Mento {
|
|
|
32
32
|
*/
|
|
33
33
|
static create(signerOrProvider) {
|
|
34
34
|
return __awaiter(this, void 0, void 0, function* () {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
if (!isSigner && !isProvider) {
|
|
38
|
-
throw new Error('A valid signer or provider must be provided');
|
|
39
|
-
}
|
|
40
|
-
if (isSigner) {
|
|
41
|
-
if (!providers.Provider.isProvider(signerOrProvider.provider)) {
|
|
42
|
-
throw new Error('Signer must be connected to a provider');
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
return new Mento(yield getBrokerAddressFromRegistry(signerOrProvider), signerOrProvider);
|
|
35
|
+
validateSignerOrProvider(signerOrProvider);
|
|
36
|
+
return new Mento(signerOrProvider, yield getBrokerAddressFromRegistry(signerOrProvider));
|
|
46
37
|
});
|
|
47
38
|
}
|
|
48
39
|
/**
|
|
49
|
-
* Create a new Mento object instance given a
|
|
50
|
-
* When constructed with
|
|
51
|
-
* @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
|
|
52
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
|
|
53
45
|
* @returns a new Mento object instance
|
|
54
46
|
*/
|
|
55
|
-
static
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
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);
|
|
67
59
|
}
|
|
68
60
|
/**
|
|
69
61
|
* Returns a list of all the pairs that can be traded on Mento
|
|
@@ -122,13 +114,15 @@ export class Mento {
|
|
|
122
114
|
*/
|
|
123
115
|
increaseTradingAllowance(token, amount) {
|
|
124
116
|
return __awaiter(this, void 0, void 0, function* () {
|
|
125
|
-
if (!Signer.isSigner(this.signerOrProvider)) {
|
|
126
|
-
throw new Error('A signer is required to populate the increaseAllowance tx object');
|
|
127
|
-
}
|
|
128
117
|
const spender = this.broker.address;
|
|
129
118
|
const tx = yield increaseAllowance(token, spender, amount, this.signerOrProvider);
|
|
130
|
-
|
|
131
|
-
|
|
119
|
+
if (Signer.isSigner(this.signerOrProvider)) {
|
|
120
|
+
// The contract call doesn't populate all of the signer fields, so we need an extra call for the signer
|
|
121
|
+
return this.signerOrProvider.populateTransaction(tx);
|
|
122
|
+
}
|
|
123
|
+
else {
|
|
124
|
+
return tx;
|
|
125
|
+
}
|
|
132
126
|
});
|
|
133
127
|
}
|
|
134
128
|
/**
|
|
@@ -142,13 +136,15 @@ export class Mento {
|
|
|
142
136
|
*/
|
|
143
137
|
swapIn(tokenIn, tokenOut, amountIn, amountOutMin) {
|
|
144
138
|
return __awaiter(this, void 0, void 0, function* () {
|
|
145
|
-
if (!Signer.isSigner(this.signerOrProvider)) {
|
|
146
|
-
throw new Error('A signer is required to populate the swapIn tx object');
|
|
147
|
-
}
|
|
148
139
|
const exchange = yield this.getExchangeForTokens(tokenIn, tokenOut);
|
|
149
140
|
const tx = yield this.broker.populateTransaction.swapIn(exchange.providerAddr, exchange.id, tokenIn, tokenOut, amountIn, amountOutMin);
|
|
150
|
-
|
|
151
|
-
|
|
141
|
+
if (Signer.isSigner(this.signerOrProvider)) {
|
|
142
|
+
// The contract call doesn't populate all of the signer fields, so we need an extra call for the signer
|
|
143
|
+
return this.signerOrProvider.populateTransaction(tx);
|
|
144
|
+
}
|
|
145
|
+
else {
|
|
146
|
+
return tx;
|
|
147
|
+
}
|
|
152
148
|
});
|
|
153
149
|
}
|
|
154
150
|
/**
|
|
@@ -162,15 +158,24 @@ export class Mento {
|
|
|
162
158
|
*/
|
|
163
159
|
swapOut(tokenIn, tokenOut, amountOut, amountInMax) {
|
|
164
160
|
return __awaiter(this, void 0, void 0, function* () {
|
|
165
|
-
if (!Signer.isSigner(this.signerOrProvider)) {
|
|
166
|
-
throw new Error('A signer is required to populate the swapOut tx object');
|
|
167
|
-
}
|
|
168
161
|
const exchange = yield this.getExchangeForTokens(tokenIn, tokenOut);
|
|
169
162
|
const tx = yield this.broker.populateTransaction.swapOut(exchange.providerAddr, exchange.id, tokenIn, tokenOut, amountOut, amountInMax);
|
|
170
|
-
|
|
171
|
-
|
|
163
|
+
if (Signer.isSigner(this.signerOrProvider)) {
|
|
164
|
+
// The contract call doesn't populate all of the signer fields, so we need an extra call for the signer
|
|
165
|
+
return this.signerOrProvider.populateTransaction(tx);
|
|
166
|
+
}
|
|
167
|
+
else {
|
|
168
|
+
return tx;
|
|
169
|
+
}
|
|
172
170
|
});
|
|
173
171
|
}
|
|
172
|
+
/**
|
|
173
|
+
* Returns the mento instance's broker contract
|
|
174
|
+
* @returns broker contract
|
|
175
|
+
*/
|
|
176
|
+
getBroker() {
|
|
177
|
+
return this.broker;
|
|
178
|
+
}
|
|
174
179
|
/**
|
|
175
180
|
* Returns the list of exchanges available in Mento (cached)
|
|
176
181
|
* @returns the list of exchanges
|
|
@@ -180,29 +185,35 @@ export class Mento {
|
|
|
180
185
|
if (this.exchanges.length > 0) {
|
|
181
186
|
return this.exchanges;
|
|
182
187
|
}
|
|
183
|
-
const exchanges = [];
|
|
184
188
|
const exchangeProvidersAddresses = yield this.broker.getExchangeProviders();
|
|
185
|
-
|
|
186
|
-
const exchangeManager = IExchangeProvider__factory.connect(exchangeProviderAddr, this.signerOrProvider);
|
|
187
|
-
const exchangesInManager = yield exchangeManager.getExchanges();
|
|
188
|
-
for (const exchange of exchangesInManager) {
|
|
189
|
-
assert(exchange.assets.length === 2, 'Exchange must have 2 assets');
|
|
190
|
-
exchanges.push({
|
|
191
|
-
providerAddr: exchangeProviderAddr,
|
|
192
|
-
id: exchange.exchangeId,
|
|
193
|
-
assets: exchange.assets,
|
|
194
|
-
});
|
|
195
|
-
}
|
|
196
|
-
}
|
|
189
|
+
const exchanges = (yield Promise.all(exchangeProvidersAddresses.map((a) => this.getExchangesForProvider(a)))).flat();
|
|
197
190
|
this.exchanges = exchanges;
|
|
198
191
|
return exchanges;
|
|
199
192
|
});
|
|
200
193
|
}
|
|
194
|
+
/**
|
|
195
|
+
* Returns the list of exchanges for a given exchange provider address
|
|
196
|
+
* @returns list of exchanges
|
|
197
|
+
*/
|
|
198
|
+
getExchangesForProvider(exchangeProviderAddr) {
|
|
199
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
200
|
+
const exchangeProvider = IExchangeProvider__factory.connect(exchangeProviderAddr, this.signerOrProvider);
|
|
201
|
+
const exchangesInProvider = yield exchangeProvider.getExchanges();
|
|
202
|
+
return exchangesInProvider.map((e) => {
|
|
203
|
+
assert(e.assets.length === 2, 'Exchange must have 2 assets');
|
|
204
|
+
return {
|
|
205
|
+
providerAddr: exchangeProviderAddr,
|
|
206
|
+
id: e.exchangeId,
|
|
207
|
+
assets: e.assets,
|
|
208
|
+
};
|
|
209
|
+
});
|
|
210
|
+
});
|
|
211
|
+
}
|
|
201
212
|
/**
|
|
202
213
|
* Returns the Mento exchange (if any) for a given pair of tokens
|
|
203
214
|
* @param token0 the first token
|
|
204
215
|
* @param token1 the second token
|
|
205
|
-
* @returns
|
|
216
|
+
* @returns exchange
|
|
206
217
|
*/
|
|
207
218
|
getExchangeForTokens(token0, token1) {
|
|
208
219
|
return __awaiter(this, void 0, void 0, function* () {
|
package/dist/esm/utils.d.ts
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { BigNumberish, 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
|
|
@@ -18,7 +30,7 @@ export declare function getSymbolFromTokenAddress(tokenAddr: Address, signerOrPr
|
|
|
18
30
|
* @param tokenAddr the address of the erc20 token
|
|
19
31
|
* @param spender the address of the spender
|
|
20
32
|
* @param amount the amount to increase the allowance by
|
|
21
|
-
* @param
|
|
33
|
+
* @param signerOrProvider an ethers signer or provider
|
|
22
34
|
* @returns the populated TransactionRequest object
|
|
23
35
|
*/
|
|
24
|
-
export declare function increaseAllowance(tokenAddr: string, spender: string, amount:
|
|
36
|
+
export declare function increaseAllowance(tokenAddr: string, spender: string, amount: BigNumberish, signerOrProvider: Signer | providers.Provider): Promise<providers.TransactionRequest>;
|
package/dist/esm/utils.js
CHANGED
|
@@ -7,7 +7,35 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
7
7
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
|
-
import { Contract,
|
|
10
|
+
import { constants, Contract, providers, Signer } from 'ethers';
|
|
11
|
+
/**
|
|
12
|
+
* Ensures that given signer is truly a a connected signer
|
|
13
|
+
* @param signer an ethers signer
|
|
14
|
+
* @throws if signer is invalid or not connected
|
|
15
|
+
*/
|
|
16
|
+
export function validateSigner(signer) {
|
|
17
|
+
if (!Signer.isSigner(signer)) {
|
|
18
|
+
throw new Error('A valid signer must be provided');
|
|
19
|
+
}
|
|
20
|
+
if (!providers.Provider.isProvider(signer.provider)) {
|
|
21
|
+
throw new Error('Signer must be connected to a provider');
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Ensures that given signerOrProvider is truly a provider or a connected signer
|
|
26
|
+
* @param signerOrProvider an ethers provider or signer
|
|
27
|
+
* @throws if signerOrProvider is invalid or not connected
|
|
28
|
+
*/
|
|
29
|
+
export function validateSignerOrProvider(signerOrProvider) {
|
|
30
|
+
const isSigner = Signer.isSigner(signerOrProvider);
|
|
31
|
+
const isProvider = providers.Provider.isProvider(signerOrProvider);
|
|
32
|
+
if (!isSigner && !isProvider) {
|
|
33
|
+
throw new Error('A valid signer or provider must be provided');
|
|
34
|
+
}
|
|
35
|
+
if (isSigner && !providers.Provider.isProvider(signerOrProvider.provider)) {
|
|
36
|
+
throw new Error('Signer must be connected to a provider');
|
|
37
|
+
}
|
|
38
|
+
}
|
|
11
39
|
/**
|
|
12
40
|
* Returns the broker address from the Celo registry
|
|
13
41
|
* @param signerOrProvider an ethers provider or signer
|
|
@@ -46,15 +74,17 @@ export function getSymbolFromTokenAddress(tokenAddr, signerOrProvider) {
|
|
|
46
74
|
* @param tokenAddr the address of the erc20 token
|
|
47
75
|
* @param spender the address of the spender
|
|
48
76
|
* @param amount the amount to increase the allowance by
|
|
49
|
-
* @param
|
|
77
|
+
* @param signerOrProvider an ethers signer or provider
|
|
50
78
|
* @returns the populated TransactionRequest object
|
|
51
79
|
*/
|
|
52
|
-
export function increaseAllowance(tokenAddr, spender, amount,
|
|
80
|
+
export function increaseAllowance(tokenAddr, spender, amount, signerOrProvider) {
|
|
53
81
|
return __awaiter(this, void 0, void 0, function* () {
|
|
54
82
|
const abi = [
|
|
55
83
|
'function increaseAllowance(address spender, uint256 value) external returns (bool)',
|
|
56
84
|
];
|
|
57
|
-
|
|
85
|
+
// TODO, not all ERC-20 contracts support increaseAllowance
|
|
86
|
+
// Add a check for that here
|
|
87
|
+
const contract = new Contract(tokenAddr, abi, signerOrProvider);
|
|
58
88
|
return yield contract.populateTransaction.increaseAllowance(spender, amount);
|
|
59
89
|
});
|
|
60
90
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mento-protocol/mento-sdk",
|
|
3
3
|
"description": "Official SDK for interacting with the Mento Protocol",
|
|
4
|
-
"version": "0.1.
|
|
4
|
+
"version": "0.1.4",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Mento Labs",
|
|
7
7
|
"keywords": [
|
|
@@ -17,14 +17,16 @@
|
|
|
17
17
|
"module": "dist/esm/index.js",
|
|
18
18
|
"typings": "dist/esm/index.d.ts",
|
|
19
19
|
"files": [
|
|
20
|
-
"dist"
|
|
20
|
+
"dist",
|
|
21
|
+
"!dist/**/*.test.*"
|
|
21
22
|
],
|
|
22
23
|
"scripts": {
|
|
23
24
|
"analyze": "size-limit --why",
|
|
24
25
|
"build": "yarn build:cjs && yarn build:esm",
|
|
25
26
|
"build:cjs": "tsc --project ./tsconfig.json",
|
|
26
27
|
"build:esm": "tsc --project ./tsconfig.esm.json",
|
|
27
|
-
"
|
|
28
|
+
"clean": "rm -rf ./dist",
|
|
29
|
+
"lint": "eslint --config ./.eslintrc.json src/**/*.ts",
|
|
28
30
|
"prettier": "prettier --config ./.prettierrc.json --write **/*.{json,md,js,ts,yml}",
|
|
29
31
|
"size": "size-limit",
|
|
30
32
|
"test": "jest --runInBand --verbose",
|
|
@@ -61,21 +63,24 @@
|
|
|
61
63
|
"@tsconfig/recommended": "^1.0.1",
|
|
62
64
|
"@types/jest": "^29.4.0",
|
|
63
65
|
"@types/node": "^18.13.0",
|
|
64
|
-
"@typescript-eslint/eslint-plugin": "^5.
|
|
65
|
-
"@typescript-eslint/parser": "^5.
|
|
66
|
-
"eslint": "^8.
|
|
66
|
+
"@typescript-eslint/eslint-plugin": "^5.53.0",
|
|
67
|
+
"@typescript-eslint/parser": "^5.53.0",
|
|
68
|
+
"eslint": "^8.34.0",
|
|
69
|
+
"eslint-config-prettier": "^8.6.0",
|
|
67
70
|
"ethers": "^5.7.2",
|
|
68
71
|
"husky": "^8.0.2",
|
|
69
72
|
"jest": "^29.4.2",
|
|
70
73
|
"prettier": "^2.8.4",
|
|
71
74
|
"size-limit": "^8.1.0",
|
|
72
75
|
"ts-jest": "^29.0.5",
|
|
76
|
+
"ts-node": "^10.9.1",
|
|
73
77
|
"typescript": "^4.9.5"
|
|
74
78
|
},
|
|
75
79
|
"dependencies": {
|
|
76
80
|
"@mento-protocol/mento-core-ts": "^0.1.0"
|
|
77
81
|
},
|
|
78
82
|
"peerDependencies": {
|
|
79
|
-
"ethers": "^5.7
|
|
80
|
-
}
|
|
83
|
+
"ethers": "^5.7"
|
|
84
|
+
},
|
|
85
|
+
"packageManager": "yarn@3.3.1"
|
|
81
86
|
}
|