@openocean.finance/openocean-sdk 1.2.7 → 1.2.9
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/lib/api/index.d.ts +3 -3
- package/lib/index.js +2 -50
- package/lib/index.js.LICENSE.txt +1 -25
- package/lib/swapSdk/Approve.d.ts +1 -1
- package/lib/swapSdk/RequestVo.d.ts +1 -1
- package/lib/v1/constants.d.ts +1 -1
- package/package.json +4 -4
- package/lib/api/index.js +0 -139
- package/lib/api/vo/RequestVo.js +0 -398
- package/lib/asset/abi/ERC20_abi.js +0 -119
- package/lib/asset/abi/aggregator.js +0 -391
- package/lib/config/index.js +0 -33
- package/lib/swapSdk/Approve.js +0 -342
- package/lib/swapSdk/NotoMobile.js +0 -141
- package/lib/swapSdk/RequestVo.js +0 -94
- package/lib/swapSdk/Swap.js +0 -1123
- package/lib/swapSdk/getAllowance.js +0 -97
- package/lib/swapSdk/getBalance.js +0 -280
- package/lib/swapSdk/index.js +0 -257
- package/lib/swapSdk/qrcode.js +0 -969
- package/lib/utils/ajx.js +0 -150
- package/lib/utils/index.js +0 -369
- package/lib/utils/web3.js +0 -9
- package/lib/v1/abis/ERC20.js +0 -22
- package/lib/v1/abis/IUniswapV2Pair.js +0 -1434
- package/lib/v1/constants.js +0 -71
- package/lib/v1/entities/currency.js +0 -39
- package/lib/v1/entities/fractions/currencyAmount.js +0 -83
- package/lib/v1/entities/fractions/fraction.js +0 -109
- package/lib/v1/entities/fractions/index.js +0 -21
- package/lib/v1/entities/fractions/percent.js +0 -37
- package/lib/v1/entities/fractions/price.js +0 -90
- package/lib/v1/entities/fractions/tokenAmount.js +0 -43
- package/lib/v1/entities/index.js +0 -22
- package/lib/v1/entities/pair.js +0 -210
- package/lib/v1/entities/route.js +0 -43
- package/lib/v1/entities/token.js +0 -87
- package/lib/v1/entities/trade.js +0 -336
- package/lib/v1/errors.js +0 -56
- package/lib/v1/fetcher.js +0 -140
- package/lib/v1/router.js +0 -97
- package/lib/v1/utils.js +0 -87
package/lib/v1/entities/trade.js
DELETED
|
@@ -1,336 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
3
|
-
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
4
|
-
if (ar || !(i in from)) {
|
|
5
|
-
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
6
|
-
ar[i] = from[i];
|
|
7
|
-
}
|
|
8
|
-
}
|
|
9
|
-
return to.concat(ar || Array.prototype.slice.call(from));
|
|
10
|
-
};
|
|
11
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
-
};
|
|
14
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
exports.Trade = exports.tradeComparator = exports.inputOutputComparator = void 0;
|
|
16
|
-
var tiny_invariant_1 = __importDefault(require("tiny-invariant"));
|
|
17
|
-
var constants_1 = require("../constants");
|
|
18
|
-
var utils_1 = require("../utils");
|
|
19
|
-
var currency_1 = require("./currency");
|
|
20
|
-
var currencyAmount_1 = require("./fractions/currencyAmount");
|
|
21
|
-
var fraction_1 = require("./fractions/fraction");
|
|
22
|
-
var percent_1 = require("./fractions/percent");
|
|
23
|
-
var price_1 = require("./fractions/price");
|
|
24
|
-
var tokenAmount_1 = require("./fractions/tokenAmount");
|
|
25
|
-
var route_1 = require("./route");
|
|
26
|
-
var token_1 = require("./token");
|
|
27
|
-
/**
|
|
28
|
-
* Returns the percent difference between the mid price and the execution price, i.e. price impact.
|
|
29
|
-
* @param midPrice mid price before the trade
|
|
30
|
-
* @param inputAmount the input amount of the trade
|
|
31
|
-
* @param outputAmount the output amount of the trade
|
|
32
|
-
*/
|
|
33
|
-
function computePriceImpact(midPrice, inputAmount, outputAmount) {
|
|
34
|
-
var exactQuote = midPrice.raw.multiply(inputAmount.raw);
|
|
35
|
-
// calculate slippage := (exactQuote - outputAmount) / exactQuote
|
|
36
|
-
var slippage = exactQuote.subtract(outputAmount.raw).divide(exactQuote);
|
|
37
|
-
return new percent_1.Percent(slippage.numerator, slippage.denominator);
|
|
38
|
-
}
|
|
39
|
-
// comparator function that allows sorting trades by their output amounts, in decreasing order, and then input amounts
|
|
40
|
-
// in increasing order. i.e. the best trades have the most outputs for the least inputs and are sorted first
|
|
41
|
-
function inputOutputComparator(a, b) {
|
|
42
|
-
// must have same input and output token for comparison
|
|
43
|
-
(0, tiny_invariant_1.default)((0, token_1.currencyEquals)(a.inputAmount.currency, b.inputAmount.currency), 'INPUT_CURRENCY');
|
|
44
|
-
(0, tiny_invariant_1.default)((0, token_1.currencyEquals)(a.outputAmount.currency, b.outputAmount.currency), 'OUTPUT_CURRENCY');
|
|
45
|
-
if (a.outputAmount.equalTo(b.outputAmount)) {
|
|
46
|
-
if (a.inputAmount.equalTo(b.inputAmount)) {
|
|
47
|
-
return 0;
|
|
48
|
-
}
|
|
49
|
-
// trade A requires less input than trade B, so A should come first
|
|
50
|
-
if (a.inputAmount.lessThan(b.inputAmount)) {
|
|
51
|
-
return -1;
|
|
52
|
-
}
|
|
53
|
-
else {
|
|
54
|
-
return 1;
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
else {
|
|
58
|
-
// tradeA has less output than trade B, so should come second
|
|
59
|
-
if (a.outputAmount.lessThan(b.outputAmount)) {
|
|
60
|
-
return 1;
|
|
61
|
-
}
|
|
62
|
-
else {
|
|
63
|
-
return -1;
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
exports.inputOutputComparator = inputOutputComparator;
|
|
68
|
-
// extension of the input output comparator that also considers other dimensions of the trade in ranking them
|
|
69
|
-
function tradeComparator(a, b) {
|
|
70
|
-
var ioComp = inputOutputComparator(a, b);
|
|
71
|
-
if (ioComp !== 0) {
|
|
72
|
-
return ioComp;
|
|
73
|
-
}
|
|
74
|
-
// consider lowest slippage next, since these are less likely to fail
|
|
75
|
-
if (a.priceImpact.lessThan(b.priceImpact)) {
|
|
76
|
-
return -1;
|
|
77
|
-
}
|
|
78
|
-
else if (a.priceImpact.greaterThan(b.priceImpact)) {
|
|
79
|
-
return 1;
|
|
80
|
-
}
|
|
81
|
-
// finally consider the number of hops since each hop costs gas
|
|
82
|
-
return a.route.path.length - b.route.path.length;
|
|
83
|
-
}
|
|
84
|
-
exports.tradeComparator = tradeComparator;
|
|
85
|
-
/**
|
|
86
|
-
* Given a currency amount and a chain ID, returns the equivalent representation as the token amount.
|
|
87
|
-
* In other words, if the currency is ETHER, returns the WETH token amount for the given chain. Otherwise, returns
|
|
88
|
-
* the input currency amount.
|
|
89
|
-
*/
|
|
90
|
-
function wrappedAmount(currencyAmount, chainId) {
|
|
91
|
-
if (currencyAmount instanceof tokenAmount_1.TokenAmount)
|
|
92
|
-
return currencyAmount;
|
|
93
|
-
if (currencyAmount.currency === currency_1.ETHER)
|
|
94
|
-
return new tokenAmount_1.TokenAmount(token_1.WETH[chainId], currencyAmount.raw);
|
|
95
|
-
(0, tiny_invariant_1.default)(false, 'CURRENCY');
|
|
96
|
-
}
|
|
97
|
-
function wrappedCurrency(currency, chainId) {
|
|
98
|
-
if (currency instanceof token_1.Token)
|
|
99
|
-
return currency;
|
|
100
|
-
if (currency === currency_1.ETHER)
|
|
101
|
-
return token_1.WETH[chainId];
|
|
102
|
-
(0, tiny_invariant_1.default)(false, 'CURRENCY');
|
|
103
|
-
}
|
|
104
|
-
/**
|
|
105
|
-
* Represents a trade executed against a list of pairs.
|
|
106
|
-
* Does not account for slippage, i.e. trades that front run this trade and move the price.
|
|
107
|
-
*/
|
|
108
|
-
var Trade = /** @class */ (function () {
|
|
109
|
-
function Trade(route, amount, tradeType) {
|
|
110
|
-
var amounts = new Array(route.path.length);
|
|
111
|
-
var nextPairs = new Array(route.pairs.length);
|
|
112
|
-
if (tradeType === constants_1.TradeType.EXACT_INPUT) {
|
|
113
|
-
(0, tiny_invariant_1.default)((0, token_1.currencyEquals)(amount.currency, route.input), 'INPUT');
|
|
114
|
-
amounts[0] = wrappedAmount(amount, route.chainId);
|
|
115
|
-
for (var i = 0; i < route.path.length - 1; i++) {
|
|
116
|
-
var pair = route.pairs[i];
|
|
117
|
-
var _a = pair.getOutputAmount(amounts[i]), outputAmount = _a[0], nextPair = _a[1];
|
|
118
|
-
amounts[i + 1] = outputAmount;
|
|
119
|
-
nextPairs[i] = nextPair;
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
else {
|
|
123
|
-
(0, tiny_invariant_1.default)((0, token_1.currencyEquals)(amount.currency, route.output), 'OUTPUT');
|
|
124
|
-
amounts[amounts.length - 1] = wrappedAmount(amount, route.chainId);
|
|
125
|
-
for (var i = route.path.length - 1; i > 0; i--) {
|
|
126
|
-
var pair = route.pairs[i - 1];
|
|
127
|
-
var _b = pair.getInputAmount(amounts[i]), inputAmount = _b[0], nextPair = _b[1];
|
|
128
|
-
amounts[i - 1] = inputAmount;
|
|
129
|
-
nextPairs[i - 1] = nextPair;
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
this.route = route;
|
|
133
|
-
this.tradeType = tradeType;
|
|
134
|
-
this.inputAmount =
|
|
135
|
-
tradeType === constants_1.TradeType.EXACT_INPUT
|
|
136
|
-
? amount
|
|
137
|
-
: route.input === currency_1.ETHER
|
|
138
|
-
? currencyAmount_1.CurrencyAmount.ether(amounts[0].raw)
|
|
139
|
-
: amounts[0];
|
|
140
|
-
this.outputAmount =
|
|
141
|
-
tradeType === constants_1.TradeType.EXACT_OUTPUT
|
|
142
|
-
? amount
|
|
143
|
-
: route.output === currency_1.ETHER
|
|
144
|
-
? currencyAmount_1.CurrencyAmount.ether(amounts[amounts.length - 1].raw)
|
|
145
|
-
: amounts[amounts.length - 1];
|
|
146
|
-
this.executionPrice = new price_1.Price(this.inputAmount.currency, this.outputAmount.currency, this.inputAmount.raw, this.outputAmount.raw);
|
|
147
|
-
this.nextMidPrice = price_1.Price.fromRoute(new route_1.Route(nextPairs, route.input));
|
|
148
|
-
this.priceImpact = computePriceImpact(route.midPrice, this.inputAmount, this.outputAmount);
|
|
149
|
-
}
|
|
150
|
-
/**
|
|
151
|
-
* Constructs an exact in trade with the given amount in and route
|
|
152
|
-
* @param route route of the exact in trade
|
|
153
|
-
* @param amountIn the amount being passed in
|
|
154
|
-
*/
|
|
155
|
-
Trade.exactIn = function (route, amountIn) {
|
|
156
|
-
return new Trade(route, amountIn, constants_1.TradeType.EXACT_INPUT);
|
|
157
|
-
};
|
|
158
|
-
/**
|
|
159
|
-
* Constructs an exact out trade with the given amount out and route
|
|
160
|
-
* @param route route of the exact out trade
|
|
161
|
-
* @param amountOut the amount returned by the trade
|
|
162
|
-
*/
|
|
163
|
-
Trade.exactOut = function (route, amountOut) {
|
|
164
|
-
return new Trade(route, amountOut, constants_1.TradeType.EXACT_OUTPUT);
|
|
165
|
-
};
|
|
166
|
-
/**
|
|
167
|
-
* Get the minimum amount that must be received from this trade for the given slippage tolerance
|
|
168
|
-
* @param slippageTolerance tolerance of unfavorable slippage from the execution price of this trade
|
|
169
|
-
*/
|
|
170
|
-
Trade.prototype.minimumAmountOut = function (slippageTolerance) {
|
|
171
|
-
(0, tiny_invariant_1.default)(!slippageTolerance.lessThan(constants_1.ZERO), 'SLIPPAGE_TOLERANCE');
|
|
172
|
-
if (this.tradeType === constants_1.TradeType.EXACT_OUTPUT) {
|
|
173
|
-
return this.outputAmount;
|
|
174
|
-
}
|
|
175
|
-
else {
|
|
176
|
-
var slippageAdjustedAmountOut = new fraction_1.Fraction(constants_1.ONE)
|
|
177
|
-
.add(slippageTolerance)
|
|
178
|
-
.invert()
|
|
179
|
-
.multiply(this.outputAmount.raw).quotient;
|
|
180
|
-
return this.outputAmount instanceof tokenAmount_1.TokenAmount
|
|
181
|
-
? new tokenAmount_1.TokenAmount(this.outputAmount.token, slippageAdjustedAmountOut)
|
|
182
|
-
: currencyAmount_1.CurrencyAmount.ether(slippageAdjustedAmountOut);
|
|
183
|
-
}
|
|
184
|
-
};
|
|
185
|
-
/**
|
|
186
|
-
* Get the maximum amount in that can be spent via this trade for the given slippage tolerance
|
|
187
|
-
* @param slippageTolerance tolerance of unfavorable slippage from the execution price of this trade
|
|
188
|
-
*/
|
|
189
|
-
Trade.prototype.maximumAmountIn = function (slippageTolerance) {
|
|
190
|
-
(0, tiny_invariant_1.default)(!slippageTolerance.lessThan(constants_1.ZERO), 'SLIPPAGE_TOLERANCE');
|
|
191
|
-
if (this.tradeType === constants_1.TradeType.EXACT_INPUT) {
|
|
192
|
-
return this.inputAmount;
|
|
193
|
-
}
|
|
194
|
-
else {
|
|
195
|
-
var slippageAdjustedAmountIn = new fraction_1.Fraction(constants_1.ONE).add(slippageTolerance).multiply(this.inputAmount.raw).quotient;
|
|
196
|
-
return this.inputAmount instanceof tokenAmount_1.TokenAmount
|
|
197
|
-
? new tokenAmount_1.TokenAmount(this.inputAmount.token, slippageAdjustedAmountIn)
|
|
198
|
-
: currencyAmount_1.CurrencyAmount.ether(slippageAdjustedAmountIn);
|
|
199
|
-
}
|
|
200
|
-
};
|
|
201
|
-
/**
|
|
202
|
-
* Given a list of pairs, and a fixed amount in, returns the top `maxNumResults` trades that go from an input token
|
|
203
|
-
* amount to an output token, making at most `maxHops` hops.
|
|
204
|
-
* Note this does not consider aggregation, as routes are linear. It's possible a better route exists by splitting
|
|
205
|
-
* the amount in among multiple routes.
|
|
206
|
-
* @param pairs the pairs to consider in finding the best trade
|
|
207
|
-
* @param currencyAmountIn exact amount of input currency to spend
|
|
208
|
-
* @param currencyOut the desired currency out
|
|
209
|
-
* @param maxNumResults maximum number of results to return
|
|
210
|
-
* @param maxHops maximum number of hops a returned trade can make, e.g. 1 hop goes through a single pair
|
|
211
|
-
* @param currentPairs used in recursion; the current list of pairs
|
|
212
|
-
* @param originalAmountIn used in recursion; the original value of the currencyAmountIn parameter
|
|
213
|
-
* @param bestTrades used in recursion; the current list of best trades
|
|
214
|
-
*/
|
|
215
|
-
Trade.bestTradeExactIn = function (pairs, currencyAmountIn, currencyOut, _a,
|
|
216
|
-
// used in recursion.
|
|
217
|
-
currentPairs, originalAmountIn, bestTrades) {
|
|
218
|
-
var _b = _a === void 0 ? {} : _a, _c = _b.maxNumResults, maxNumResults = _c === void 0 ? 3 : _c, _d = _b.maxHops, maxHops = _d === void 0 ? 3 : _d;
|
|
219
|
-
if (currentPairs === void 0) { currentPairs = []; }
|
|
220
|
-
if (originalAmountIn === void 0) { originalAmountIn = currencyAmountIn; }
|
|
221
|
-
if (bestTrades === void 0) { bestTrades = []; }
|
|
222
|
-
(0, tiny_invariant_1.default)(pairs.length > 0, 'PAIRS');
|
|
223
|
-
(0, tiny_invariant_1.default)(maxHops > 0, 'MAX_HOPS');
|
|
224
|
-
(0, tiny_invariant_1.default)(originalAmountIn === currencyAmountIn || currentPairs.length > 0, 'INVALID_RECURSION');
|
|
225
|
-
var chainId = currencyAmountIn instanceof tokenAmount_1.TokenAmount
|
|
226
|
-
? currencyAmountIn.token.chainId
|
|
227
|
-
: currencyOut instanceof token_1.Token
|
|
228
|
-
? currencyOut.chainId
|
|
229
|
-
: undefined;
|
|
230
|
-
(0, tiny_invariant_1.default)(chainId !== undefined, 'CHAIN_ID');
|
|
231
|
-
var amountIn = wrappedAmount(currencyAmountIn, chainId);
|
|
232
|
-
var tokenOut = wrappedCurrency(currencyOut, chainId);
|
|
233
|
-
for (var i = 0; i < pairs.length; i++) {
|
|
234
|
-
var pair = pairs[i];
|
|
235
|
-
// pair irrelevant
|
|
236
|
-
if (!pair.token0.equals(amountIn.token) && !pair.token1.equals(amountIn.token))
|
|
237
|
-
continue;
|
|
238
|
-
if (pair.reserve0.equalTo(constants_1.ZERO) || pair.reserve1.equalTo(constants_1.ZERO))
|
|
239
|
-
continue;
|
|
240
|
-
var amountOut = void 0;
|
|
241
|
-
try {
|
|
242
|
-
;
|
|
243
|
-
amountOut = pair.getOutputAmount(amountIn)[0];
|
|
244
|
-
}
|
|
245
|
-
catch (error) {
|
|
246
|
-
// input too low
|
|
247
|
-
if (error.isInsufficientInputAmountError) {
|
|
248
|
-
continue;
|
|
249
|
-
}
|
|
250
|
-
throw error;
|
|
251
|
-
}
|
|
252
|
-
// we have arrived at the output token, so this is the final trade of one of the paths
|
|
253
|
-
if (amountOut.token.equals(tokenOut)) {
|
|
254
|
-
(0, utils_1.sortedInsert)(bestTrades, new Trade(new route_1.Route(__spreadArray(__spreadArray([], currentPairs, true), [pair], false), originalAmountIn.currency, currencyOut), originalAmountIn, constants_1.TradeType.EXACT_INPUT), maxNumResults, tradeComparator);
|
|
255
|
-
}
|
|
256
|
-
else if (maxHops > 1 && pairs.length > 1) {
|
|
257
|
-
var pairsExcludingThisPair = pairs.slice(0, i).concat(pairs.slice(i + 1, pairs.length));
|
|
258
|
-
// otherwise, consider all the other paths that lead from this token as long as we have not exceeded maxHops
|
|
259
|
-
Trade.bestTradeExactIn(pairsExcludingThisPair, amountOut, currencyOut, {
|
|
260
|
-
maxNumResults: maxNumResults,
|
|
261
|
-
maxHops: maxHops - 1
|
|
262
|
-
}, __spreadArray(__spreadArray([], currentPairs, true), [pair], false), originalAmountIn, bestTrades);
|
|
263
|
-
}
|
|
264
|
-
}
|
|
265
|
-
return bestTrades;
|
|
266
|
-
};
|
|
267
|
-
/**
|
|
268
|
-
* similar to the above method but instead targets a fixed output amount
|
|
269
|
-
* given a list of pairs, and a fixed amount out, returns the top `maxNumResults` trades that go from an input token
|
|
270
|
-
* to an output token amount, making at most `maxHops` hops
|
|
271
|
-
* note this does not consider aggregation, as routes are linear. it's possible a better route exists by splitting
|
|
272
|
-
* the amount in among multiple routes.
|
|
273
|
-
* @param pairs the pairs to consider in finding the best trade
|
|
274
|
-
* @param currencyIn the currency to spend
|
|
275
|
-
* @param currencyAmountOut the exact amount of currency out
|
|
276
|
-
* @param maxNumResults maximum number of results to return
|
|
277
|
-
* @param maxHops maximum number of hops a returned trade can make, e.g. 1 hop goes through a single pair
|
|
278
|
-
* @param currentPairs used in recursion; the current list of pairs
|
|
279
|
-
* @param originalAmountOut used in recursion; the original value of the currencyAmountOut parameter
|
|
280
|
-
* @param bestTrades used in recursion; the current list of best trades
|
|
281
|
-
*/
|
|
282
|
-
Trade.bestTradeExactOut = function (pairs, currencyIn, currencyAmountOut, _a,
|
|
283
|
-
// used in recursion.
|
|
284
|
-
currentPairs, originalAmountOut, bestTrades) {
|
|
285
|
-
var _b = _a === void 0 ? {} : _a, _c = _b.maxNumResults, maxNumResults = _c === void 0 ? 3 : _c, _d = _b.maxHops, maxHops = _d === void 0 ? 3 : _d;
|
|
286
|
-
if (currentPairs === void 0) { currentPairs = []; }
|
|
287
|
-
if (originalAmountOut === void 0) { originalAmountOut = currencyAmountOut; }
|
|
288
|
-
if (bestTrades === void 0) { bestTrades = []; }
|
|
289
|
-
(0, tiny_invariant_1.default)(pairs.length > 0, 'PAIRS');
|
|
290
|
-
(0, tiny_invariant_1.default)(maxHops > 0, 'MAX_HOPS');
|
|
291
|
-
(0, tiny_invariant_1.default)(originalAmountOut === currencyAmountOut || currentPairs.length > 0, 'INVALID_RECURSION');
|
|
292
|
-
var chainId = currencyAmountOut instanceof tokenAmount_1.TokenAmount
|
|
293
|
-
? currencyAmountOut.token.chainId
|
|
294
|
-
: currencyIn instanceof token_1.Token
|
|
295
|
-
? currencyIn.chainId
|
|
296
|
-
: undefined;
|
|
297
|
-
(0, tiny_invariant_1.default)(chainId !== undefined, 'CHAIN_ID');
|
|
298
|
-
var amountOut = wrappedAmount(currencyAmountOut, chainId);
|
|
299
|
-
var tokenIn = wrappedCurrency(currencyIn, chainId);
|
|
300
|
-
for (var i = 0; i < pairs.length; i++) {
|
|
301
|
-
var pair = pairs[i];
|
|
302
|
-
// pair irrelevant
|
|
303
|
-
if (!pair.token0.equals(amountOut.token) && !pair.token1.equals(amountOut.token))
|
|
304
|
-
continue;
|
|
305
|
-
if (pair.reserve0.equalTo(constants_1.ZERO) || pair.reserve1.equalTo(constants_1.ZERO))
|
|
306
|
-
continue;
|
|
307
|
-
var amountIn = void 0;
|
|
308
|
-
try {
|
|
309
|
-
;
|
|
310
|
-
amountIn = pair.getInputAmount(amountOut)[0];
|
|
311
|
-
}
|
|
312
|
-
catch (error) {
|
|
313
|
-
// not enough liquidity in this pair
|
|
314
|
-
if (error.isInsufficientReservesError) {
|
|
315
|
-
continue;
|
|
316
|
-
}
|
|
317
|
-
throw error;
|
|
318
|
-
}
|
|
319
|
-
// we have arrived at the input token, so this is the first trade of one of the paths
|
|
320
|
-
if (amountIn.token.equals(tokenIn)) {
|
|
321
|
-
(0, utils_1.sortedInsert)(bestTrades, new Trade(new route_1.Route(__spreadArray([pair], currentPairs, true), currencyIn, originalAmountOut.currency), originalAmountOut, constants_1.TradeType.EXACT_OUTPUT), maxNumResults, tradeComparator);
|
|
322
|
-
}
|
|
323
|
-
else if (maxHops > 1 && pairs.length > 1) {
|
|
324
|
-
var pairsExcludingThisPair = pairs.slice(0, i).concat(pairs.slice(i + 1, pairs.length));
|
|
325
|
-
// otherwise, consider all the other paths that arrive at this token as long as we have not exceeded maxHops
|
|
326
|
-
Trade.bestTradeExactOut(pairsExcludingThisPair, currencyIn, amountIn, {
|
|
327
|
-
maxNumResults: maxNumResults,
|
|
328
|
-
maxHops: maxHops - 1
|
|
329
|
-
}, __spreadArray([pair], currentPairs, true), originalAmountOut, bestTrades);
|
|
330
|
-
}
|
|
331
|
-
}
|
|
332
|
-
return bestTrades;
|
|
333
|
-
};
|
|
334
|
-
return Trade;
|
|
335
|
-
}());
|
|
336
|
-
exports.Trade = Trade;
|
package/lib/v1/errors.js
DELETED
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __extends = (this && this.__extends) || (function () {
|
|
3
|
-
var extendStatics = function (d, b) {
|
|
4
|
-
extendStatics = Object.setPrototypeOf ||
|
|
5
|
-
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
-
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
-
return extendStatics(d, b);
|
|
8
|
-
};
|
|
9
|
-
return function (d, b) {
|
|
10
|
-
if (typeof b !== "function" && b !== null)
|
|
11
|
-
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
-
extendStatics(d, b);
|
|
13
|
-
function __() { this.constructor = d; }
|
|
14
|
-
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
-
};
|
|
16
|
-
})();
|
|
17
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
-
exports.InsufficientInputAmountError = exports.InsufficientReservesError = void 0;
|
|
19
|
-
// see https://stackoverflow.com/a/41102306
|
|
20
|
-
var CAN_SET_PROTOTYPE = 'setPrototypeOf' in Object;
|
|
21
|
-
/**
|
|
22
|
-
* Indicates that the pair has insufficient reserves for a desired output amount. I.e. the amount of output cannot be
|
|
23
|
-
* obtained by sending any amount of input.
|
|
24
|
-
*/
|
|
25
|
-
var InsufficientReservesError = /** @class */ (function (_super) {
|
|
26
|
-
__extends(InsufficientReservesError, _super);
|
|
27
|
-
function InsufficientReservesError() {
|
|
28
|
-
var _newTarget = this.constructor;
|
|
29
|
-
var _this = _super.call(this) || this;
|
|
30
|
-
_this.isInsufficientReservesError = true;
|
|
31
|
-
_this.name = _this.constructor.name;
|
|
32
|
-
if (CAN_SET_PROTOTYPE)
|
|
33
|
-
Object.setPrototypeOf(_this, _newTarget.prototype);
|
|
34
|
-
return _this;
|
|
35
|
-
}
|
|
36
|
-
return InsufficientReservesError;
|
|
37
|
-
}(Error));
|
|
38
|
-
exports.InsufficientReservesError = InsufficientReservesError;
|
|
39
|
-
/**
|
|
40
|
-
* Indicates that the input amount is too small to produce any amount of output. I.e. the amount of input sent is less
|
|
41
|
-
* than the price of a single unit of output after fees.
|
|
42
|
-
*/
|
|
43
|
-
var InsufficientInputAmountError = /** @class */ (function (_super) {
|
|
44
|
-
__extends(InsufficientInputAmountError, _super);
|
|
45
|
-
function InsufficientInputAmountError() {
|
|
46
|
-
var _newTarget = this.constructor;
|
|
47
|
-
var _this = _super.call(this) || this;
|
|
48
|
-
_this.isInsufficientInputAmountError = true;
|
|
49
|
-
_this.name = _this.constructor.name;
|
|
50
|
-
if (CAN_SET_PROTOTYPE)
|
|
51
|
-
Object.setPrototypeOf(_this, _newTarget.prototype);
|
|
52
|
-
return _this;
|
|
53
|
-
}
|
|
54
|
-
return InsufficientInputAmountError;
|
|
55
|
-
}(Error));
|
|
56
|
-
exports.InsufficientInputAmountError = InsufficientInputAmountError;
|
package/lib/v1/fetcher.js
DELETED
|
@@ -1,140 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __assign = (this && this.__assign) || function () {
|
|
3
|
-
__assign = Object.assign || function(t) {
|
|
4
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
-
s = arguments[i];
|
|
6
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
-
t[p] = s[p];
|
|
8
|
-
}
|
|
9
|
-
return t;
|
|
10
|
-
};
|
|
11
|
-
return __assign.apply(this, arguments);
|
|
12
|
-
};
|
|
13
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
14
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
15
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
16
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
17
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
18
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
19
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
20
|
-
});
|
|
21
|
-
};
|
|
22
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
23
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
24
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
25
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
26
|
-
function step(op) {
|
|
27
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
28
|
-
while (_) try {
|
|
29
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
30
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
31
|
-
switch (op[0]) {
|
|
32
|
-
case 0: case 1: t = op; break;
|
|
33
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
34
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
35
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
36
|
-
default:
|
|
37
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
38
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
39
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
40
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
41
|
-
if (t[2]) _.ops.pop();
|
|
42
|
-
_.trys.pop(); continue;
|
|
43
|
-
}
|
|
44
|
-
op = body.call(thisArg, _);
|
|
45
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
46
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
47
|
-
}
|
|
48
|
-
};
|
|
49
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
50
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
51
|
-
};
|
|
52
|
-
var _a;
|
|
53
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
54
|
-
exports.Fetcher = void 0;
|
|
55
|
-
var contracts_1 = require("@ethersproject/contracts");
|
|
56
|
-
var networks_1 = require("@ethersproject/networks");
|
|
57
|
-
var providers_1 = require("@ethersproject/providers");
|
|
58
|
-
var tokenAmount_1 = require("./entities/fractions/tokenAmount");
|
|
59
|
-
var pair_1 = require("./entities/pair");
|
|
60
|
-
// import IPancakePair from '@uniswap/v2-core/build/IPancakePair.json'
|
|
61
|
-
var IUniswapV2Pair_1 = __importDefault(require("./abis/IUniswapV2Pair"));
|
|
62
|
-
var tiny_invariant_1 = __importDefault(require("tiny-invariant"));
|
|
63
|
-
var ERC20_1 = __importDefault(require("./abis/ERC20"));
|
|
64
|
-
var constants_1 = require("./constants");
|
|
65
|
-
var token_1 = require("./entities/token");
|
|
66
|
-
var TOKEN_DECIMALS_CACHE = (_a = {},
|
|
67
|
-
_a[constants_1.ChainId.MAINNET] = {
|
|
68
|
-
'0xE0B7927c4aF23765Cb51314A0E0521A9645F0E2A': 9 // DGD
|
|
69
|
-
},
|
|
70
|
-
_a);
|
|
71
|
-
/**
|
|
72
|
-
* Contains methods for constructing instances of pairs and tokens from on-chain data.
|
|
73
|
-
*/
|
|
74
|
-
var Fetcher = /** @class */ (function () {
|
|
75
|
-
/**
|
|
76
|
-
* Cannot be constructed.
|
|
77
|
-
*/
|
|
78
|
-
function Fetcher() {
|
|
79
|
-
}
|
|
80
|
-
/**
|
|
81
|
-
* Fetch information for a given token on the given chain, using the given ethers provider.
|
|
82
|
-
* @param chainId chain of the token
|
|
83
|
-
* @param address address of the token on the chain
|
|
84
|
-
* @param provider provider used to fetch the token
|
|
85
|
-
* @param symbol optional symbol of the token
|
|
86
|
-
* @param name optional name of the token
|
|
87
|
-
*/
|
|
88
|
-
Fetcher.fetchTokenData = function (chainId, address, provider, symbol, name) {
|
|
89
|
-
var _a;
|
|
90
|
-
if (provider === void 0) { provider = (0, providers_1.getDefaultProvider)((0, networks_1.getNetwork)(chainId)); }
|
|
91
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
92
|
-
var parsedDecimals, _b;
|
|
93
|
-
return __generator(this, function (_c) {
|
|
94
|
-
switch (_c.label) {
|
|
95
|
-
case 0:
|
|
96
|
-
if (!(typeof ((_a = TOKEN_DECIMALS_CACHE === null || TOKEN_DECIMALS_CACHE === void 0 ? void 0 : TOKEN_DECIMALS_CACHE[chainId]) === null || _a === void 0 ? void 0 : _a[address]) === 'number')) return [3 /*break*/, 1];
|
|
97
|
-
_b = TOKEN_DECIMALS_CACHE[chainId][address];
|
|
98
|
-
return [3 /*break*/, 3];
|
|
99
|
-
case 1: return [4 /*yield*/, new contracts_1.Contract(address, ERC20_1.default, provider).decimals().then(function (decimals) {
|
|
100
|
-
var _a, _b;
|
|
101
|
-
TOKEN_DECIMALS_CACHE = __assign(__assign({}, TOKEN_DECIMALS_CACHE), (_a = {}, _a[chainId] = __assign(__assign({}, TOKEN_DECIMALS_CACHE === null || TOKEN_DECIMALS_CACHE === void 0 ? void 0 : TOKEN_DECIMALS_CACHE[chainId]), (_b = {}, _b[address] = decimals, _b)), _a));
|
|
102
|
-
return decimals;
|
|
103
|
-
})];
|
|
104
|
-
case 2:
|
|
105
|
-
_b = _c.sent();
|
|
106
|
-
_c.label = 3;
|
|
107
|
-
case 3:
|
|
108
|
-
parsedDecimals = _b;
|
|
109
|
-
return [2 /*return*/, new token_1.Token(chainId, address, parsedDecimals, symbol, name)];
|
|
110
|
-
}
|
|
111
|
-
});
|
|
112
|
-
});
|
|
113
|
-
};
|
|
114
|
-
/**
|
|
115
|
-
* Fetches information about a pair and constructs a pair from the given two tokens.
|
|
116
|
-
* @param tokenA first token
|
|
117
|
-
* @param tokenB second token
|
|
118
|
-
* @param provider the provider to use to fetch the data
|
|
119
|
-
*/
|
|
120
|
-
Fetcher.fetchPairData = function (tokenA, tokenB, provider) {
|
|
121
|
-
if (provider === void 0) { provider = (0, providers_1.getDefaultProvider)((0, networks_1.getNetwork)(tokenA.chainId)); }
|
|
122
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
123
|
-
var address, _a, reserves0, reserves1, balances;
|
|
124
|
-
return __generator(this, function (_b) {
|
|
125
|
-
switch (_b.label) {
|
|
126
|
-
case 0:
|
|
127
|
-
(0, tiny_invariant_1.default)(tokenA.chainId === tokenB.chainId, 'CHAIN_ID');
|
|
128
|
-
address = pair_1.Pair.getAddress(tokenA, tokenB);
|
|
129
|
-
return [4 /*yield*/, new contracts_1.Contract(address, IUniswapV2Pair_1.default.abi, provider).getReserves()];
|
|
130
|
-
case 1:
|
|
131
|
-
_a = _b.sent(), reserves0 = _a[0], reserves1 = _a[1];
|
|
132
|
-
balances = tokenA.sortsBefore(tokenB) ? [reserves0, reserves1] : [reserves1, reserves0];
|
|
133
|
-
return [2 /*return*/, new pair_1.Pair(new tokenAmount_1.TokenAmount(tokenA, balances[0]), new tokenAmount_1.TokenAmount(tokenB, balances[1]))];
|
|
134
|
-
}
|
|
135
|
-
});
|
|
136
|
-
});
|
|
137
|
-
};
|
|
138
|
-
return Fetcher;
|
|
139
|
-
}());
|
|
140
|
-
exports.Fetcher = Fetcher;
|
package/lib/v1/router.js
DELETED
|
@@ -1,97 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.Router = void 0;
|
|
7
|
-
var constants_1 = require("./constants");
|
|
8
|
-
var tiny_invariant_1 = __importDefault(require("tiny-invariant"));
|
|
9
|
-
var utils_1 = require("./utils");
|
|
10
|
-
var entities_1 = require("./entities");
|
|
11
|
-
function toHex(currencyAmount) {
|
|
12
|
-
return "0x".concat(currencyAmount.raw.toString(16));
|
|
13
|
-
}
|
|
14
|
-
var ZERO_HEX = '0x0';
|
|
15
|
-
/**
|
|
16
|
-
* Represents the Uniswap V2 Router, and has static methods for helping execute trades.
|
|
17
|
-
*/
|
|
18
|
-
var Router = /** @class */ (function () {
|
|
19
|
-
/**
|
|
20
|
-
* Cannot be constructed.
|
|
21
|
-
*/
|
|
22
|
-
function Router() {
|
|
23
|
-
}
|
|
24
|
-
/**
|
|
25
|
-
* Produces the on-chain method name to call and the hex encoded parameters to pass as arguments for a given trade.
|
|
26
|
-
* @param trade to produce call parameters for
|
|
27
|
-
* @param options options for the call parameters
|
|
28
|
-
*/
|
|
29
|
-
Router.swapCallParameters = function (trade, options) {
|
|
30
|
-
var etherIn = trade.inputAmount.currency === entities_1.ETHER;
|
|
31
|
-
var etherOut = trade.outputAmount.currency === entities_1.ETHER;
|
|
32
|
-
// the router does not support both ether in and out
|
|
33
|
-
(0, tiny_invariant_1.default)(!(etherIn && etherOut), 'ETHER_IN_OUT');
|
|
34
|
-
(0, tiny_invariant_1.default)(options.ttl > 0, 'TTL');
|
|
35
|
-
var to = (0, utils_1.validateAndParseAddress)(options.recipient);
|
|
36
|
-
var amountIn = toHex(trade.maximumAmountIn(options.allowedSlippage));
|
|
37
|
-
var amountOut = toHex(trade.minimumAmountOut(options.allowedSlippage));
|
|
38
|
-
var path = trade.route.path.map(function (token) { return token.address; });
|
|
39
|
-
var deadline = "0x".concat((Math.floor(new Date().getTime() / 1000) + options.ttl).toString(16));
|
|
40
|
-
var useFeeOnTransfer = Boolean(options.feeOnTransfer);
|
|
41
|
-
var methodName;
|
|
42
|
-
var args;
|
|
43
|
-
var value;
|
|
44
|
-
switch (trade.tradeType) {
|
|
45
|
-
case constants_1.TradeType.EXACT_INPUT:
|
|
46
|
-
if (etherIn) {
|
|
47
|
-
methodName = useFeeOnTransfer ? 'swapExactETHForTokensSupportingFeeOnTransferTokens' : 'swapExactETHForTokens';
|
|
48
|
-
// (uint amountOutMin, address[] calldata path, address to, uint deadline)
|
|
49
|
-
args = [amountOut, path, to, deadline];
|
|
50
|
-
value = amountIn;
|
|
51
|
-
}
|
|
52
|
-
else if (etherOut) {
|
|
53
|
-
methodName = useFeeOnTransfer ? 'swapExactTokensForETHSupportingFeeOnTransferTokens' : 'swapExactTokensForETH';
|
|
54
|
-
// (uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline)
|
|
55
|
-
args = [amountIn, amountOut, path, to, deadline];
|
|
56
|
-
value = ZERO_HEX;
|
|
57
|
-
}
|
|
58
|
-
else {
|
|
59
|
-
methodName = useFeeOnTransfer
|
|
60
|
-
? 'swapExactTokensForTokensSupportingFeeOnTransferTokens'
|
|
61
|
-
: 'swapExactTokensForTokens';
|
|
62
|
-
// (uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline)
|
|
63
|
-
args = [amountIn, amountOut, path, to, deadline];
|
|
64
|
-
value = ZERO_HEX;
|
|
65
|
-
}
|
|
66
|
-
break;
|
|
67
|
-
case constants_1.TradeType.EXACT_OUTPUT:
|
|
68
|
-
(0, tiny_invariant_1.default)(!useFeeOnTransfer, 'EXACT_OUT_FOT');
|
|
69
|
-
if (etherIn) {
|
|
70
|
-
methodName = 'swapETHForExactTokens';
|
|
71
|
-
// (uint amountOut, address[] calldata path, address to, uint deadline)
|
|
72
|
-
args = [amountOut, path, to, deadline];
|
|
73
|
-
value = amountIn;
|
|
74
|
-
}
|
|
75
|
-
else if (etherOut) {
|
|
76
|
-
methodName = 'swapTokensForExactETH';
|
|
77
|
-
// (uint amountOut, uint amountInMax, address[] calldata path, address to, uint deadline)
|
|
78
|
-
args = [amountOut, amountIn, path, to, deadline];
|
|
79
|
-
value = ZERO_HEX;
|
|
80
|
-
}
|
|
81
|
-
else {
|
|
82
|
-
methodName = 'swapTokensForExactTokens';
|
|
83
|
-
// (uint amountOut, uint amountInMax, address[] calldata path, address to, uint deadline)
|
|
84
|
-
args = [amountOut, amountIn, path, to, deadline];
|
|
85
|
-
value = ZERO_HEX;
|
|
86
|
-
}
|
|
87
|
-
break;
|
|
88
|
-
}
|
|
89
|
-
return {
|
|
90
|
-
methodName: methodName,
|
|
91
|
-
args: args,
|
|
92
|
-
value: value
|
|
93
|
-
};
|
|
94
|
-
};
|
|
95
|
-
return Router;
|
|
96
|
-
}());
|
|
97
|
-
exports.Router = Router;
|