@mento-protocol/mento-sdk 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +674 -0
- package/README.md +62 -0
- package/dist/cjs/index.d.ts +1 -0
- package/dist/cjs/index.js +18 -0
- package/dist/cjs/mento.d.ts +94 -0
- package/dist/cjs/mento.js +222 -0
- package/dist/cjs/utils.d.ts +23 -0
- package/dist/cjs/utils.js +67 -0
- package/dist/esm/index.d.ts +1 -0
- package/dist/esm/index.js +2 -0
- package/dist/esm/mento.d.ts +94 -0
- package/dist/esm/mento.js +218 -0
- package/dist/esm/utils.d.ts +23 -0
- package/dist/esm/utils.js +61 -0
- package/package.json +77 -0
package/README.md
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
# Mento SDK
|
|
2
|
+
|
|
3
|
+
The official Mento Protocol SDK for interacting with Multi-Collateral Mento smart contracts on the Celo network.
|
|
4
|
+
|
|
5
|
+
# Sample Usage
|
|
6
|
+
|
|
7
|
+
```javascript
|
|
8
|
+
const provider = new providers.JsonRpcProvider(
|
|
9
|
+
'https://baklava-forno.celo-testnet.org'
|
|
10
|
+
)
|
|
11
|
+
const pKey = 'privateKey'
|
|
12
|
+
const wallet = new Wallet(pKey, provider)
|
|
13
|
+
|
|
14
|
+
const mento = await Mento.create(wallet)
|
|
15
|
+
|
|
16
|
+
console.log('available pairs: ', await mento.getTradeablePairs())
|
|
17
|
+
/*
|
|
18
|
+
[
|
|
19
|
+
[
|
|
20
|
+
{
|
|
21
|
+
address: '0x62492A644A588FD904270BeD06ad52B9abfEA1aE',
|
|
22
|
+
symbol: 'cUSD'
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
address: '0xdDc9bE57f553fe75752D61606B94CBD7e0264eF8',
|
|
26
|
+
symbol: 'CELO'
|
|
27
|
+
}
|
|
28
|
+
],
|
|
29
|
+
...
|
|
30
|
+
]
|
|
31
|
+
*/
|
|
32
|
+
|
|
33
|
+
// swap 1 CELO for cUSD
|
|
34
|
+
const one = 1
|
|
35
|
+
const tokenIn = '0xdDc9bE57f553fe75752D61606B94CBD7e0264eF8' // CELO
|
|
36
|
+
const tokenOut = '0x62492A644A588FD904270BeD06ad52B9abfEA1aE' // cUSD
|
|
37
|
+
const amountIn = utils.parseUnits(one.toString(), 18)
|
|
38
|
+
const expectedAmountOut = await mento.getAmountOut(
|
|
39
|
+
tokenIn,
|
|
40
|
+
tokenOut,
|
|
41
|
+
utils.parseUnits(one.toString(), 18)
|
|
42
|
+
)
|
|
43
|
+
// 95% of the quote to allow some slippage
|
|
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
|
+
```
|
|
59
|
+
|
|
60
|
+
# In depth docs
|
|
61
|
+
|
|
62
|
+
- [ ] TODO: Adds docs link
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './mento';
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/* istanbul ignore file */
|
|
3
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
4
|
+
if (k2 === undefined) k2 = k;
|
|
5
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
6
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
7
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
8
|
+
}
|
|
9
|
+
Object.defineProperty(o, k2, desc);
|
|
10
|
+
}) : (function(o, m, k, k2) {
|
|
11
|
+
if (k2 === undefined) k2 = k;
|
|
12
|
+
o[k2] = m[k];
|
|
13
|
+
}));
|
|
14
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
15
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
16
|
+
};
|
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
__exportStar(require("./mento"), exports);
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { BigNumber, Signer, providers } from 'ethers';
|
|
2
|
+
interface Asset {
|
|
3
|
+
address: Address;
|
|
4
|
+
symbol: string;
|
|
5
|
+
}
|
|
6
|
+
export declare class Mento {
|
|
7
|
+
private readonly signerOrProvider;
|
|
8
|
+
private readonly broker;
|
|
9
|
+
private exchanges;
|
|
10
|
+
/**
|
|
11
|
+
* This constructor is private, use the static create or createWithBrokerAddress methods
|
|
12
|
+
* to create a new Mento instance
|
|
13
|
+
* @param brokerAddress the address of the broker contract
|
|
14
|
+
* @param provider an ethers provider
|
|
15
|
+
* @param signer an optional ethers signer to execute swaps (must be connected to a provider)
|
|
16
|
+
*/
|
|
17
|
+
private constructor();
|
|
18
|
+
/**
|
|
19
|
+
* Creates a new Mento object instance.
|
|
20
|
+
* When constructed with only a Provider only read-only operations are supported
|
|
21
|
+
* @param signerOrProvider an ethers signer or provider. A signer is required to execute swaps
|
|
22
|
+
* @returns a new Mento object instance
|
|
23
|
+
*/
|
|
24
|
+
static create(signerOrProvider: Signer | providers.Provider): Promise<Mento>;
|
|
25
|
+
/**
|
|
26
|
+
* Create a new Mento object instance given a specific broker address
|
|
27
|
+
* When constructed with only a Provider only read-only operations are supported
|
|
28
|
+
* @param brokerAddr the address of the broker contract
|
|
29
|
+
* @param signerOrProvider an ethers signer or provider. A signer is required to execute swaps
|
|
30
|
+
* @returns a new Mento object instance
|
|
31
|
+
*/
|
|
32
|
+
static createWithBrokerAddress(brokerAddr: Address, signerOrProvider: Signer | providers.Provider): Mento;
|
|
33
|
+
/**
|
|
34
|
+
* Returns a list of all the pairs that can be traded on Mento
|
|
35
|
+
* @returns The list of tradeable pairs in the form of [{address, symbol}]
|
|
36
|
+
*/
|
|
37
|
+
getTradeablePairs(): Promise<[Asset, Asset][]>;
|
|
38
|
+
/**
|
|
39
|
+
* Returns the amount of tokenIn to be sold to buy amountOut of tokenOut
|
|
40
|
+
* @param tokenIn the token to be sold
|
|
41
|
+
* @param tokenOut the token to be bought
|
|
42
|
+
* @param amountOut the amount of tokenOut to be bought
|
|
43
|
+
* @returns the amount of tokenIn to be sold
|
|
44
|
+
*/
|
|
45
|
+
getAmountIn(tokenIn: Address, tokenOut: Address, amountOut: BigNumber): Promise<BigNumber>;
|
|
46
|
+
/**
|
|
47
|
+
* Returns the amount of tokenOut to be bought by selling amountIn of tokenIn
|
|
48
|
+
* @param tokenIn the token to be sold
|
|
49
|
+
* @param tokenOut the token to be bought
|
|
50
|
+
* @param amountIn the amount of tokenIn to be sold
|
|
51
|
+
* @returns the amount of tokenOut to be bought
|
|
52
|
+
*/
|
|
53
|
+
getAmountOut(tokenIn: Address, tokenOut: Address, amountIn: BigNumber): Promise<BigNumber>;
|
|
54
|
+
/**
|
|
55
|
+
* Increases the broker's trading allowance for the given token
|
|
56
|
+
* @param token the token to increase the allowance for
|
|
57
|
+
* @param amount the amount to increase the allowance by
|
|
58
|
+
* @returns the populated TransactionRequest object
|
|
59
|
+
*/
|
|
60
|
+
increaseTradingAllowance(token: Address, amount: BigNumber): Promise<providers.TransactionRequest>;
|
|
61
|
+
/**
|
|
62
|
+
* Returns a token swap populated tx object with a fixed amount of tokenIn and a minimum amount of tokenOut
|
|
63
|
+
* Submitting the transaction to execute the swap is left to the consumer
|
|
64
|
+
* @param tokenIn the token to be sold
|
|
65
|
+
* @param tokenOut the token to be bought
|
|
66
|
+
* @param amountIn the amount of tokenIn to be sold
|
|
67
|
+
* @param amountOutMin the minimum amount of tokenOut to be bought
|
|
68
|
+
* @returns the populated TransactionRequest object
|
|
69
|
+
*/
|
|
70
|
+
swapIn(tokenIn: Address, tokenOut: Address, amountIn: BigNumber, amountOutMin: BigNumber): Promise<providers.TransactionRequest>;
|
|
71
|
+
/**
|
|
72
|
+
* Returns a token swap populated tx object with a maximum amount of tokenIn and a fixed amount of tokenOut
|
|
73
|
+
* Submitting the transaction to execute the swap is left to the consumer
|
|
74
|
+
* @param tokenIn the token to be sold
|
|
75
|
+
* @param tokenOut the token to be bought
|
|
76
|
+
* @param amountOut the amount of tokenOut to be bought
|
|
77
|
+
* @param amountInMax the maximum amount of tokenIn to be sold
|
|
78
|
+
* @returns the populated TransactionRequest object
|
|
79
|
+
*/
|
|
80
|
+
swapOut(tokenIn: Address, tokenOut: Address, amountOut: BigNumber, amountInMax: BigNumber): Promise<providers.TransactionRequest>;
|
|
81
|
+
/**
|
|
82
|
+
* Returns the list of exchanges available in Mento (cached)
|
|
83
|
+
* @returns the list of exchanges
|
|
84
|
+
*/
|
|
85
|
+
private getExchanges;
|
|
86
|
+
/**
|
|
87
|
+
* Returns the Mento exchange (if any) for a given pair of tokens
|
|
88
|
+
* @param token0 the first token
|
|
89
|
+
* @param token1 the second token
|
|
90
|
+
* @returns
|
|
91
|
+
*/
|
|
92
|
+
private getExchangeForTokens;
|
|
93
|
+
}
|
|
94
|
+
export {};
|
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.Mento = void 0;
|
|
13
|
+
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
14
|
+
const ethers_1 = require("ethers");
|
|
15
|
+
const mento_core_ts_1 = require("@mento-protocol/mento-core-ts");
|
|
16
|
+
const utils_1 = require("./utils");
|
|
17
|
+
const assert_1 = require("assert");
|
|
18
|
+
class Mento {
|
|
19
|
+
/**
|
|
20
|
+
* This constructor is private, use the static create or createWithBrokerAddress methods
|
|
21
|
+
* to create a new Mento instance
|
|
22
|
+
* @param brokerAddress the address of the broker contract
|
|
23
|
+
* @param provider an ethers provider
|
|
24
|
+
* @param signer an optional ethers signer to execute swaps (must be connected to a provider)
|
|
25
|
+
*/
|
|
26
|
+
constructor(brokerAddress, signerOrProvider) {
|
|
27
|
+
this.broker = mento_core_ts_1.IBroker__factory.connect(brokerAddress, signerOrProvider);
|
|
28
|
+
this.signerOrProvider = signerOrProvider;
|
|
29
|
+
this.exchanges = new Array();
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Creates a new Mento object instance.
|
|
33
|
+
* When constructed with only a Provider only read-only operations are supported
|
|
34
|
+
* @param signerOrProvider an ethers signer or provider. A signer is required to execute swaps
|
|
35
|
+
* @returns a new Mento object instance
|
|
36
|
+
*/
|
|
37
|
+
static create(signerOrProvider) {
|
|
38
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
39
|
+
const isSigner = ethers_1.Signer.isSigner(signerOrProvider);
|
|
40
|
+
const isProvider = ethers_1.providers.Provider.isProvider(signerOrProvider);
|
|
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);
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Create a new Mento object instance given a specific broker address
|
|
54
|
+
* When constructed with only a Provider only read-only operations are supported
|
|
55
|
+
* @param brokerAddr the address of the broker contract
|
|
56
|
+
* @param signerOrProvider an ethers signer or provider. A signer is required to execute swaps
|
|
57
|
+
* @returns a new Mento object instance
|
|
58
|
+
*/
|
|
59
|
+
static createWithBrokerAddress(brokerAddr, signerOrProvider) {
|
|
60
|
+
const isSigner = ethers_1.Signer.isSigner(signerOrProvider);
|
|
61
|
+
const isProvider = ethers_1.providers.Provider.isProvider(signerOrProvider);
|
|
62
|
+
if (!isSigner && !isProvider) {
|
|
63
|
+
throw new Error('A valid signer or provider must be provided');
|
|
64
|
+
}
|
|
65
|
+
if (isSigner) {
|
|
66
|
+
if (!ethers_1.providers.Provider.isProvider(signerOrProvider.provider)) {
|
|
67
|
+
throw new Error('Signer must be connected to a provider');
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return new Mento(brokerAddr, signerOrProvider);
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Returns a list of all the pairs that can be traded on Mento
|
|
74
|
+
* @returns The list of tradeable pairs in the form of [{address, symbol}]
|
|
75
|
+
*/
|
|
76
|
+
getTradeablePairs() {
|
|
77
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
78
|
+
const exchanges = yield this.getExchanges();
|
|
79
|
+
const pairs = [];
|
|
80
|
+
for (const exchange of exchanges) {
|
|
81
|
+
const asset0 = exchange.assets[0];
|
|
82
|
+
const asset1 = exchange.assets[1];
|
|
83
|
+
const symbols = yield Promise.all([
|
|
84
|
+
(0, utils_1.getSymbolFromTokenAddress)(asset0, this.signerOrProvider),
|
|
85
|
+
(0, utils_1.getSymbolFromTokenAddress)(asset1, this.signerOrProvider),
|
|
86
|
+
]);
|
|
87
|
+
pairs.push([
|
|
88
|
+
{ address: asset0, symbol: symbols[0] },
|
|
89
|
+
{ address: asset1, symbol: symbols[1] },
|
|
90
|
+
]);
|
|
91
|
+
}
|
|
92
|
+
return pairs;
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Returns the amount of tokenIn to be sold to buy amountOut of tokenOut
|
|
97
|
+
* @param tokenIn the token to be sold
|
|
98
|
+
* @param tokenOut the token to be bought
|
|
99
|
+
* @param amountOut the amount of tokenOut to be bought
|
|
100
|
+
* @returns the amount of tokenIn to be sold
|
|
101
|
+
*/
|
|
102
|
+
getAmountIn(tokenIn, tokenOut, amountOut) {
|
|
103
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
104
|
+
const exchange = yield this.getExchangeForTokens(tokenIn, tokenOut);
|
|
105
|
+
return this.broker.getAmountIn(exchange.providerAddr, exchange.id, tokenIn, tokenOut, amountOut);
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Returns the amount of tokenOut to be bought by selling amountIn of tokenIn
|
|
110
|
+
* @param tokenIn the token to be sold
|
|
111
|
+
* @param tokenOut the token to be bought
|
|
112
|
+
* @param amountIn the amount of tokenIn to be sold
|
|
113
|
+
* @returns the amount of tokenOut to be bought
|
|
114
|
+
*/
|
|
115
|
+
getAmountOut(tokenIn, tokenOut, amountIn) {
|
|
116
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
117
|
+
const exchange = yield this.getExchangeForTokens(tokenIn, tokenOut);
|
|
118
|
+
return this.broker.getAmountOut(exchange.providerAddr, exchange.id, tokenIn, tokenOut, amountIn);
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Increases the broker's trading allowance for the given token
|
|
123
|
+
* @param token the token to increase the allowance for
|
|
124
|
+
* @param amount the amount to increase the allowance by
|
|
125
|
+
* @returns the populated TransactionRequest object
|
|
126
|
+
*/
|
|
127
|
+
increaseTradingAllowance(token, amount) {
|
|
128
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
129
|
+
if (!ethers_1.Signer.isSigner(this.signerOrProvider)) {
|
|
130
|
+
throw new Error('A signer is required to populate the increaseAllowance tx object');
|
|
131
|
+
}
|
|
132
|
+
const spender = this.broker.address;
|
|
133
|
+
const tx = yield (0, utils_1.increaseAllowance)(token, spender, amount, this.signerOrProvider);
|
|
134
|
+
// The contract call doesn't populate all of the signer fields, so we need an extra call for the signer
|
|
135
|
+
return this.signerOrProvider.populateTransaction(tx);
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Returns a token swap populated tx object with a fixed amount of tokenIn and a minimum amount of tokenOut
|
|
140
|
+
* Submitting the transaction to execute the swap is left to the consumer
|
|
141
|
+
* @param tokenIn the token to be sold
|
|
142
|
+
* @param tokenOut the token to be bought
|
|
143
|
+
* @param amountIn the amount of tokenIn to be sold
|
|
144
|
+
* @param amountOutMin the minimum amount of tokenOut to be bought
|
|
145
|
+
* @returns the populated TransactionRequest object
|
|
146
|
+
*/
|
|
147
|
+
swapIn(tokenIn, tokenOut, amountIn, amountOutMin) {
|
|
148
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
149
|
+
if (!ethers_1.Signer.isSigner(this.signerOrProvider)) {
|
|
150
|
+
throw new Error('A signer is required to populate the swapIn tx object');
|
|
151
|
+
}
|
|
152
|
+
const exchange = yield this.getExchangeForTokens(tokenIn, tokenOut);
|
|
153
|
+
const tx = yield this.broker.populateTransaction.swapIn(exchange.providerAddr, exchange.id, tokenIn, tokenOut, amountIn, amountOutMin);
|
|
154
|
+
// The broker's call doesn't populate all of the signer fields, so we need an extra call for the signer
|
|
155
|
+
return this.signerOrProvider.populateTransaction(tx);
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Returns a token swap populated tx object with a maximum amount of tokenIn and a fixed amount of tokenOut
|
|
160
|
+
* Submitting the transaction to execute the swap is left to the consumer
|
|
161
|
+
* @param tokenIn the token to be sold
|
|
162
|
+
* @param tokenOut the token to be bought
|
|
163
|
+
* @param amountOut the amount of tokenOut to be bought
|
|
164
|
+
* @param amountInMax the maximum amount of tokenIn to be sold
|
|
165
|
+
* @returns the populated TransactionRequest object
|
|
166
|
+
*/
|
|
167
|
+
swapOut(tokenIn, tokenOut, amountOut, amountInMax) {
|
|
168
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
169
|
+
if (!ethers_1.Signer.isSigner(this.signerOrProvider)) {
|
|
170
|
+
throw new Error('A signer is required to populate the swapOut tx object');
|
|
171
|
+
}
|
|
172
|
+
const exchange = yield this.getExchangeForTokens(tokenIn, tokenOut);
|
|
173
|
+
const tx = yield this.broker.populateTransaction.swapOut(exchange.providerAddr, exchange.id, tokenIn, tokenOut, amountOut, amountInMax);
|
|
174
|
+
// The broker's call doesn't populate all of the signer fields, so we need an extra call for the signer
|
|
175
|
+
return this.signerOrProvider.populateTransaction(tx);
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Returns the list of exchanges available in Mento (cached)
|
|
180
|
+
* @returns the list of exchanges
|
|
181
|
+
*/
|
|
182
|
+
getExchanges() {
|
|
183
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
184
|
+
if (this.exchanges.length > 0) {
|
|
185
|
+
return this.exchanges;
|
|
186
|
+
}
|
|
187
|
+
const exchanges = [];
|
|
188
|
+
const exchangeProvidersAddresses = yield this.broker.getExchangeProviders();
|
|
189
|
+
for (const exchangeProviderAddr of exchangeProvidersAddresses) {
|
|
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
|
+
}
|
|
201
|
+
this.exchanges = exchanges;
|
|
202
|
+
return exchanges;
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* Returns the Mento exchange (if any) for a given pair of tokens
|
|
207
|
+
* @param token0 the first token
|
|
208
|
+
* @param token1 the second token
|
|
209
|
+
* @returns
|
|
210
|
+
*/
|
|
211
|
+
getExchangeForTokens(token0, token1) {
|
|
212
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
213
|
+
const exchanges = (yield this.getExchanges()).filter((e) => e.assets.includes(token0) && e.assets.includes(token1));
|
|
214
|
+
if (exchanges.length === 0) {
|
|
215
|
+
throw Error(`No exchange found for ${token0} and ${token1}`);
|
|
216
|
+
}
|
|
217
|
+
(0, assert_1.strict)(exchanges.length === 1, `More than one exchange found for ${token0} and ${token1}`);
|
|
218
|
+
return exchanges[0];
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
exports.Mento = Mento;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { BigNumber, Signer, providers } from 'ethers';
|
|
2
|
+
/**
|
|
3
|
+
* Returns the broker address from the Celo registry
|
|
4
|
+
* @param signerOrProvider an ethers provider or signer
|
|
5
|
+
* @returns the broker address
|
|
6
|
+
*/
|
|
7
|
+
export declare function getBrokerAddressFromRegistry(signerOrProvider: Signer | providers.Provider): Promise<Address>;
|
|
8
|
+
/**
|
|
9
|
+
* Returns the symbol of an erc20 token
|
|
10
|
+
* @param tokenAddr the address of the erc20 token
|
|
11
|
+
* @param signerOrProvider an ethers provider or signer
|
|
12
|
+
* @returns the symbol of the erc20 token
|
|
13
|
+
*/
|
|
14
|
+
export declare function getSymbolFromTokenAddress(tokenAddr: Address, signerOrProvider: Signer | providers.Provider): Promise<string>;
|
|
15
|
+
/**
|
|
16
|
+
* Returns a populated tx obj for increasing the allowance of a spender for a given erc20 token by a given amount
|
|
17
|
+
* @param tokenAddr the address of the erc20 token
|
|
18
|
+
* @param spender the address of the spender
|
|
19
|
+
* @param amount the amount to increase the allowance by
|
|
20
|
+
* @param signer an ethers signer
|
|
21
|
+
* @returns the populated TransactionRequest object
|
|
22
|
+
*/
|
|
23
|
+
export declare function increaseAllowance(tokenAddr: string, spender: string, amount: BigNumber, signer: Signer): Promise<providers.TransactionRequest>;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
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
|
|
14
|
+
const ethers_1 = require("ethers");
|
|
15
|
+
/**
|
|
16
|
+
* Returns the broker address from the Celo registry
|
|
17
|
+
* @param signerOrProvider an ethers provider or signer
|
|
18
|
+
* @returns the broker address
|
|
19
|
+
*/
|
|
20
|
+
function getBrokerAddressFromRegistry(signerOrProvider) {
|
|
21
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
22
|
+
const celoRegistryAddress = '0x000000000000000000000000000000000000ce10';
|
|
23
|
+
const brokerIdentifier = 'Broker';
|
|
24
|
+
const registryAbi = [
|
|
25
|
+
'function getAddressForString(string calldata identifier) external view returns (address)',
|
|
26
|
+
];
|
|
27
|
+
const contract = new ethers_1.Contract(celoRegistryAddress, registryAbi, signerOrProvider);
|
|
28
|
+
const brokerAddress = yield contract.getAddressForString(brokerIdentifier);
|
|
29
|
+
if (brokerAddress === ethers_1.constants.AddressZero) {
|
|
30
|
+
throw Error('Broker address not found in the registry');
|
|
31
|
+
}
|
|
32
|
+
return brokerAddress;
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
exports.getBrokerAddressFromRegistry = getBrokerAddressFromRegistry;
|
|
36
|
+
/**
|
|
37
|
+
* Returns the symbol of an erc20 token
|
|
38
|
+
* @param tokenAddr the address of the erc20 token
|
|
39
|
+
* @param signerOrProvider an ethers provider or signer
|
|
40
|
+
* @returns the symbol of the erc20 token
|
|
41
|
+
*/
|
|
42
|
+
function getSymbolFromTokenAddress(tokenAddr, signerOrProvider) {
|
|
43
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
44
|
+
const erc20Abi = ['function symbol() external view returns (string memory)'];
|
|
45
|
+
const contract = new ethers_1.Contract(tokenAddr, erc20Abi, signerOrProvider);
|
|
46
|
+
return contract.symbol();
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
exports.getSymbolFromTokenAddress = getSymbolFromTokenAddress;
|
|
50
|
+
/**
|
|
51
|
+
* Returns a populated tx obj for increasing the allowance of a spender for a given erc20 token by a given amount
|
|
52
|
+
* @param tokenAddr the address of the erc20 token
|
|
53
|
+
* @param spender the address of the spender
|
|
54
|
+
* @param amount the amount to increase the allowance by
|
|
55
|
+
* @param signer an ethers signer
|
|
56
|
+
* @returns the populated TransactionRequest object
|
|
57
|
+
*/
|
|
58
|
+
function increaseAllowance(tokenAddr, spender, amount, signer) {
|
|
59
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
60
|
+
const abi = [
|
|
61
|
+
'function increaseAllowance(address spender, uint256 value) external returns (bool)',
|
|
62
|
+
];
|
|
63
|
+
const contract = new ethers_1.Contract(tokenAddr, abi, signer);
|
|
64
|
+
return yield contract.populateTransaction.increaseAllowance(spender, amount);
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
exports.increaseAllowance = increaseAllowance;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './mento';
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { BigNumber, Signer, providers } from 'ethers';
|
|
2
|
+
interface Asset {
|
|
3
|
+
address: Address;
|
|
4
|
+
symbol: string;
|
|
5
|
+
}
|
|
6
|
+
export declare class Mento {
|
|
7
|
+
private readonly signerOrProvider;
|
|
8
|
+
private readonly broker;
|
|
9
|
+
private exchanges;
|
|
10
|
+
/**
|
|
11
|
+
* This constructor is private, use the static create or createWithBrokerAddress methods
|
|
12
|
+
* to create a new Mento instance
|
|
13
|
+
* @param brokerAddress the address of the broker contract
|
|
14
|
+
* @param provider an ethers provider
|
|
15
|
+
* @param signer an optional ethers signer to execute swaps (must be connected to a provider)
|
|
16
|
+
*/
|
|
17
|
+
private constructor();
|
|
18
|
+
/**
|
|
19
|
+
* Creates a new Mento object instance.
|
|
20
|
+
* When constructed with only a Provider only read-only operations are supported
|
|
21
|
+
* @param signerOrProvider an ethers signer or provider. A signer is required to execute swaps
|
|
22
|
+
* @returns a new Mento object instance
|
|
23
|
+
*/
|
|
24
|
+
static create(signerOrProvider: Signer | providers.Provider): Promise<Mento>;
|
|
25
|
+
/**
|
|
26
|
+
* Create a new Mento object instance given a specific broker address
|
|
27
|
+
* When constructed with only a Provider only read-only operations are supported
|
|
28
|
+
* @param brokerAddr the address of the broker contract
|
|
29
|
+
* @param signerOrProvider an ethers signer or provider. A signer is required to execute swaps
|
|
30
|
+
* @returns a new Mento object instance
|
|
31
|
+
*/
|
|
32
|
+
static createWithBrokerAddress(brokerAddr: Address, signerOrProvider: Signer | providers.Provider): Mento;
|
|
33
|
+
/**
|
|
34
|
+
* Returns a list of all the pairs that can be traded on Mento
|
|
35
|
+
* @returns The list of tradeable pairs in the form of [{address, symbol}]
|
|
36
|
+
*/
|
|
37
|
+
getTradeablePairs(): Promise<[Asset, Asset][]>;
|
|
38
|
+
/**
|
|
39
|
+
* Returns the amount of tokenIn to be sold to buy amountOut of tokenOut
|
|
40
|
+
* @param tokenIn the token to be sold
|
|
41
|
+
* @param tokenOut the token to be bought
|
|
42
|
+
* @param amountOut the amount of tokenOut to be bought
|
|
43
|
+
* @returns the amount of tokenIn to be sold
|
|
44
|
+
*/
|
|
45
|
+
getAmountIn(tokenIn: Address, tokenOut: Address, amountOut: BigNumber): Promise<BigNumber>;
|
|
46
|
+
/**
|
|
47
|
+
* Returns the amount of tokenOut to be bought by selling amountIn of tokenIn
|
|
48
|
+
* @param tokenIn the token to be sold
|
|
49
|
+
* @param tokenOut the token to be bought
|
|
50
|
+
* @param amountIn the amount of tokenIn to be sold
|
|
51
|
+
* @returns the amount of tokenOut to be bought
|
|
52
|
+
*/
|
|
53
|
+
getAmountOut(tokenIn: Address, tokenOut: Address, amountIn: BigNumber): Promise<BigNumber>;
|
|
54
|
+
/**
|
|
55
|
+
* Increases the broker's trading allowance for the given token
|
|
56
|
+
* @param token the token to increase the allowance for
|
|
57
|
+
* @param amount the amount to increase the allowance by
|
|
58
|
+
* @returns the populated TransactionRequest object
|
|
59
|
+
*/
|
|
60
|
+
increaseTradingAllowance(token: Address, amount: BigNumber): Promise<providers.TransactionRequest>;
|
|
61
|
+
/**
|
|
62
|
+
* Returns a token swap populated tx object with a fixed amount of tokenIn and a minimum amount of tokenOut
|
|
63
|
+
* Submitting the transaction to execute the swap is left to the consumer
|
|
64
|
+
* @param tokenIn the token to be sold
|
|
65
|
+
* @param tokenOut the token to be bought
|
|
66
|
+
* @param amountIn the amount of tokenIn to be sold
|
|
67
|
+
* @param amountOutMin the minimum amount of tokenOut to be bought
|
|
68
|
+
* @returns the populated TransactionRequest object
|
|
69
|
+
*/
|
|
70
|
+
swapIn(tokenIn: Address, tokenOut: Address, amountIn: BigNumber, amountOutMin: BigNumber): Promise<providers.TransactionRequest>;
|
|
71
|
+
/**
|
|
72
|
+
* Returns a token swap populated tx object with a maximum amount of tokenIn and a fixed amount of tokenOut
|
|
73
|
+
* Submitting the transaction to execute the swap is left to the consumer
|
|
74
|
+
* @param tokenIn the token to be sold
|
|
75
|
+
* @param tokenOut the token to be bought
|
|
76
|
+
* @param amountOut the amount of tokenOut to be bought
|
|
77
|
+
* @param amountInMax the maximum amount of tokenIn to be sold
|
|
78
|
+
* @returns the populated TransactionRequest object
|
|
79
|
+
*/
|
|
80
|
+
swapOut(tokenIn: Address, tokenOut: Address, amountOut: BigNumber, amountInMax: BigNumber): Promise<providers.TransactionRequest>;
|
|
81
|
+
/**
|
|
82
|
+
* Returns the list of exchanges available in Mento (cached)
|
|
83
|
+
* @returns the list of exchanges
|
|
84
|
+
*/
|
|
85
|
+
private getExchanges;
|
|
86
|
+
/**
|
|
87
|
+
* Returns the Mento exchange (if any) for a given pair of tokens
|
|
88
|
+
* @param token0 the first token
|
|
89
|
+
* @param token1 the second token
|
|
90
|
+
* @returns
|
|
91
|
+
*/
|
|
92
|
+
private getExchangeForTokens;
|
|
93
|
+
}
|
|
94
|
+
export {};
|