@openocean.finance/openocean-sdk 1.2.1 → 1.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/api/index.js +139 -0
- package/lib/api/vo/RequestVo.js +411 -0
- package/lib/asset/abi/ERC20_abi.js +119 -0
- package/lib/asset/abi/aggregator.js +391 -0
- package/lib/config/index.js +33 -0
- package/lib/index.d.ts +7 -1
- package/lib/index.js +50 -2
- package/lib/swapSdk/Approve.js +342 -0
- package/lib/swapSdk/NotoMobile.js +141 -0
- package/lib/swapSdk/RequestVo.js +94 -0
- package/lib/swapSdk/Swap.js +1123 -0
- package/lib/swapSdk/getAllowance.js +97 -0
- package/lib/swapSdk/getBalance.js +280 -0
- package/lib/swapSdk/index.js +257 -0
- package/lib/swapSdk/qrcode.d.ts +6 -0
- package/lib/swapSdk/qrcode.js +969 -0
- package/lib/utils/ajx.js +150 -0
- package/lib/utils/index.js +369 -0
- package/lib/utils/web3.js +9 -0
- package/lib/v1/abis/ERC20.d.ts +16 -0
- package/lib/v1/abis/ERC20.js +22 -0
- package/lib/v1/abis/IUniswapV2Pair.d.ts +83 -0
- package/lib/v1/abis/IUniswapV2Pair.js +1434 -0
- package/lib/v1/constants.d.ts +58 -0
- package/lib/v1/constants.js +71 -0
- package/lib/v1/entities/currency.d.ts +27 -0
- package/lib/v1/entities/currency.js +39 -0
- package/lib/v1/entities/fractions/currencyAmount.d.ts +19 -0
- package/lib/v1/entities/fractions/currencyAmount.js +83 -0
- package/lib/v1/entities/fractions/fraction.d.ts +19 -0
- package/lib/v1/entities/fractions/fraction.js +109 -0
- package/lib/v1/entities/fractions/index.d.ts +5 -0
- package/lib/v1/entities/fractions/index.js +21 -0
- package/lib/v1/entities/fractions/percent.d.ts +6 -0
- package/lib/v1/entities/fractions/percent.js +37 -0
- package/lib/v1/entities/fractions/price.d.ts +18 -0
- package/lib/v1/entities/fractions/price.js +90 -0
- package/lib/v1/entities/fractions/tokenAmount.d.ts +9 -0
- package/lib/v1/entities/fractions/tokenAmount.js +43 -0
- package/lib/v1/entities/index.d.ts +6 -0
- package/lib/v1/entities/index.js +22 -0
- package/lib/v1/entities/pair.d.ts +41 -0
- package/lib/v1/entities/pair.js +210 -0
- package/lib/v1/entities/route.d.ts +14 -0
- package/lib/v1/entities/route.js +43 -0
- package/lib/v1/entities/token.d.ts +27 -0
- package/lib/v1/entities/token.js +87 -0
- package/lib/v1/entities/trade.d.ts +106 -0
- package/lib/v1/entities/trade.js +336 -0
- package/lib/v1/errors.d.ts +16 -0
- package/lib/v1/errors.js +56 -0
- package/lib/v1/fetcher.d.ts +28 -0
- package/lib/v1/fetcher.js +140 -0
- package/lib/v1/router.d.ts +56 -0
- package/lib/v1/router.js +97 -0
- package/lib/v1/utils.d.ts +7 -0
- package/lib/v1/utils.js +87 -0
- package/package.json +4 -1
- package/lib/index.js.LICENSE.txt +0 -279
|
@@ -0,0 +1,43 @@
|
|
|
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
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
18
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
19
|
+
};
|
|
20
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
21
|
+
exports.TokenAmount = void 0;
|
|
22
|
+
var currencyAmount_1 = require("./currencyAmount");
|
|
23
|
+
var tiny_invariant_1 = __importDefault(require("tiny-invariant"));
|
|
24
|
+
var jsbi_1 = __importDefault(require("jsbi"));
|
|
25
|
+
var TokenAmount = /** @class */ (function (_super) {
|
|
26
|
+
__extends(TokenAmount, _super);
|
|
27
|
+
// amount _must_ be raw, i.e. in the native representation
|
|
28
|
+
function TokenAmount(token, amount) {
|
|
29
|
+
var _this = _super.call(this, token, amount) || this;
|
|
30
|
+
_this.token = token;
|
|
31
|
+
return _this;
|
|
32
|
+
}
|
|
33
|
+
TokenAmount.prototype.add = function (other) {
|
|
34
|
+
(0, tiny_invariant_1.default)(this.token.equals(other.token), 'TOKEN');
|
|
35
|
+
return new TokenAmount(this.token, jsbi_1.default.add(this.raw, other.raw));
|
|
36
|
+
};
|
|
37
|
+
TokenAmount.prototype.subtract = function (other) {
|
|
38
|
+
(0, tiny_invariant_1.default)(this.token.equals(other.token), 'TOKEN');
|
|
39
|
+
return new TokenAmount(this.token, jsbi_1.default.subtract(this.raw, other.raw));
|
|
40
|
+
};
|
|
41
|
+
return TokenAmount;
|
|
42
|
+
}(currencyAmount_1.CurrencyAmount));
|
|
43
|
+
exports.TokenAmount = TokenAmount;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./token"), exports);
|
|
18
|
+
__exportStar(require("./pair"), exports);
|
|
19
|
+
__exportStar(require("./route"), exports);
|
|
20
|
+
__exportStar(require("./trade"), exports);
|
|
21
|
+
__exportStar(require("./currency"), exports);
|
|
22
|
+
__exportStar(require("./fractions"), exports);
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { Price } from './fractions/price';
|
|
2
|
+
import { TokenAmount } from './fractions/tokenAmount';
|
|
3
|
+
import { BigintIsh, ChainId } from '../constants';
|
|
4
|
+
import { Token } from './token';
|
|
5
|
+
export declare class Pair {
|
|
6
|
+
readonly liquidityToken: Token;
|
|
7
|
+
private readonly tokenAmounts;
|
|
8
|
+
static getAddress(tokenA: Token, tokenB: Token): string;
|
|
9
|
+
constructor(tokenAmountA: TokenAmount, tokenAmountB: TokenAmount);
|
|
10
|
+
/**
|
|
11
|
+
* Returns true if the token is either token0 or token1
|
|
12
|
+
* @param token to check
|
|
13
|
+
*/
|
|
14
|
+
involvesToken(token: Token): boolean;
|
|
15
|
+
/**
|
|
16
|
+
* Returns the current mid price of the pair in terms of token0, i.e. the ratio of reserve1 to reserve0
|
|
17
|
+
*/
|
|
18
|
+
get token0Price(): Price;
|
|
19
|
+
/**
|
|
20
|
+
* Returns the current mid price of the pair in terms of token1, i.e. the ratio of reserve0 to reserve1
|
|
21
|
+
*/
|
|
22
|
+
get token1Price(): Price;
|
|
23
|
+
/**
|
|
24
|
+
* Return the price of the given token in terms of the other token in the pair.
|
|
25
|
+
* @param token token to return price of
|
|
26
|
+
*/
|
|
27
|
+
priceOf(token: Token): Price;
|
|
28
|
+
/**
|
|
29
|
+
* Returns the chain ID of the tokens in the pair.
|
|
30
|
+
*/
|
|
31
|
+
get chainId(): ChainId;
|
|
32
|
+
get token0(): Token;
|
|
33
|
+
get token1(): Token;
|
|
34
|
+
get reserve0(): TokenAmount;
|
|
35
|
+
get reserve1(): TokenAmount;
|
|
36
|
+
reserveOf(token: Token): TokenAmount;
|
|
37
|
+
getOutputAmount(inputAmount: TokenAmount): [TokenAmount, Pair];
|
|
38
|
+
getInputAmount(outputAmount: TokenAmount): [TokenAmount, Pair];
|
|
39
|
+
getLiquidityMinted(totalSupply: TokenAmount, tokenAmountA: TokenAmount, tokenAmountB: TokenAmount): TokenAmount;
|
|
40
|
+
getLiquidityValue(token: Token, totalSupply: TokenAmount, liquidity: TokenAmount, feeOn?: boolean, kLast?: BigintIsh): TokenAmount;
|
|
41
|
+
}
|
|
@@ -0,0 +1,210 @@
|
|
|
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 __importDefault = (this && this.__importDefault) || function (mod) {
|
|
14
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.Pair = void 0;
|
|
18
|
+
var price_1 = require("./fractions/price");
|
|
19
|
+
var tokenAmount_1 = require("./fractions/tokenAmount");
|
|
20
|
+
var tiny_invariant_1 = __importDefault(require("tiny-invariant"));
|
|
21
|
+
var jsbi_1 = __importDefault(require("jsbi"));
|
|
22
|
+
var solidity_1 = require("@ethersproject/solidity");
|
|
23
|
+
var address_1 = require("@ethersproject/address");
|
|
24
|
+
var constants_1 = require("../constants");
|
|
25
|
+
var utils_1 = require("../utils");
|
|
26
|
+
var errors_1 = require("../errors");
|
|
27
|
+
var token_1 = require("./token");
|
|
28
|
+
var PAIR_ADDRESS_CACHE = {};
|
|
29
|
+
var Pair = /** @class */ (function () {
|
|
30
|
+
function Pair(tokenAmountA, tokenAmountB) {
|
|
31
|
+
var tokenAmounts = tokenAmountA.token.sortsBefore(tokenAmountB.token) // does safety checks
|
|
32
|
+
? [tokenAmountA, tokenAmountB]
|
|
33
|
+
: [tokenAmountB, tokenAmountA];
|
|
34
|
+
this.liquidityToken = new token_1.Token(tokenAmounts[0].token.chainId, Pair.getAddress(tokenAmounts[0].token, tokenAmounts[1].token), 18, 'UNI-V2', 'Uniswap V2');
|
|
35
|
+
this.tokenAmounts = tokenAmounts;
|
|
36
|
+
}
|
|
37
|
+
Pair.getAddress = function (tokenA, tokenB) {
|
|
38
|
+
var _a, _b;
|
|
39
|
+
var _c;
|
|
40
|
+
var tokens = tokenA.sortsBefore(tokenB) ? [tokenA, tokenB] : [tokenB, tokenA]; // does safety checks
|
|
41
|
+
if (((_c = PAIR_ADDRESS_CACHE === null || PAIR_ADDRESS_CACHE === void 0 ? void 0 : PAIR_ADDRESS_CACHE[tokens[0].address]) === null || _c === void 0 ? void 0 : _c[tokens[1].address]) === undefined) {
|
|
42
|
+
var chainId = tokenA.chainId;
|
|
43
|
+
var factory_address = constants_1.FACTORY_ADDRESS[chainId];
|
|
44
|
+
var init_code_hash = constants_1.INIT_CODE_HASH[chainId];
|
|
45
|
+
PAIR_ADDRESS_CACHE = __assign(__assign({}, PAIR_ADDRESS_CACHE), (_a = {}, _a[tokens[0].address] = __assign(__assign({}, PAIR_ADDRESS_CACHE === null || PAIR_ADDRESS_CACHE === void 0 ? void 0 : PAIR_ADDRESS_CACHE[tokens[0].address]), (_b = {}, _b[tokens[1].address] = (0, address_1.getCreate2Address)(factory_address, (0, solidity_1.keccak256)(['bytes'], [(0, solidity_1.pack)(['address', 'address'], [tokens[0].address, tokens[1].address])]), init_code_hash), _b)), _a));
|
|
46
|
+
}
|
|
47
|
+
return PAIR_ADDRESS_CACHE[tokens[0].address][tokens[1].address];
|
|
48
|
+
};
|
|
49
|
+
/**
|
|
50
|
+
* Returns true if the token is either token0 or token1
|
|
51
|
+
* @param token to check
|
|
52
|
+
*/
|
|
53
|
+
Pair.prototype.involvesToken = function (token) {
|
|
54
|
+
return token.equals(this.token0) || token.equals(this.token1);
|
|
55
|
+
};
|
|
56
|
+
Object.defineProperty(Pair.prototype, "token0Price", {
|
|
57
|
+
/**
|
|
58
|
+
* Returns the current mid price of the pair in terms of token0, i.e. the ratio of reserve1 to reserve0
|
|
59
|
+
*/
|
|
60
|
+
get: function () {
|
|
61
|
+
return new price_1.Price(this.token0, this.token1, this.tokenAmounts[0].raw, this.tokenAmounts[1].raw);
|
|
62
|
+
},
|
|
63
|
+
enumerable: false,
|
|
64
|
+
configurable: true
|
|
65
|
+
});
|
|
66
|
+
Object.defineProperty(Pair.prototype, "token1Price", {
|
|
67
|
+
/**
|
|
68
|
+
* Returns the current mid price of the pair in terms of token1, i.e. the ratio of reserve0 to reserve1
|
|
69
|
+
*/
|
|
70
|
+
get: function () {
|
|
71
|
+
return new price_1.Price(this.token1, this.token0, this.tokenAmounts[1].raw, this.tokenAmounts[0].raw);
|
|
72
|
+
},
|
|
73
|
+
enumerable: false,
|
|
74
|
+
configurable: true
|
|
75
|
+
});
|
|
76
|
+
/**
|
|
77
|
+
* Return the price of the given token in terms of the other token in the pair.
|
|
78
|
+
* @param token token to return price of
|
|
79
|
+
*/
|
|
80
|
+
Pair.prototype.priceOf = function (token) {
|
|
81
|
+
(0, tiny_invariant_1.default)(this.involvesToken(token), 'TOKEN');
|
|
82
|
+
return token.equals(this.token0) ? this.token0Price : this.token1Price;
|
|
83
|
+
};
|
|
84
|
+
Object.defineProperty(Pair.prototype, "chainId", {
|
|
85
|
+
/**
|
|
86
|
+
* Returns the chain ID of the tokens in the pair.
|
|
87
|
+
*/
|
|
88
|
+
get: function () {
|
|
89
|
+
return this.token0.chainId;
|
|
90
|
+
},
|
|
91
|
+
enumerable: false,
|
|
92
|
+
configurable: true
|
|
93
|
+
});
|
|
94
|
+
Object.defineProperty(Pair.prototype, "token0", {
|
|
95
|
+
get: function () {
|
|
96
|
+
return this.tokenAmounts[0].token;
|
|
97
|
+
},
|
|
98
|
+
enumerable: false,
|
|
99
|
+
configurable: true
|
|
100
|
+
});
|
|
101
|
+
Object.defineProperty(Pair.prototype, "token1", {
|
|
102
|
+
get: function () {
|
|
103
|
+
return this.tokenAmounts[1].token;
|
|
104
|
+
},
|
|
105
|
+
enumerable: false,
|
|
106
|
+
configurable: true
|
|
107
|
+
});
|
|
108
|
+
Object.defineProperty(Pair.prototype, "reserve0", {
|
|
109
|
+
get: function () {
|
|
110
|
+
return this.tokenAmounts[0];
|
|
111
|
+
},
|
|
112
|
+
enumerable: false,
|
|
113
|
+
configurable: true
|
|
114
|
+
});
|
|
115
|
+
Object.defineProperty(Pair.prototype, "reserve1", {
|
|
116
|
+
get: function () {
|
|
117
|
+
return this.tokenAmounts[1];
|
|
118
|
+
},
|
|
119
|
+
enumerable: false,
|
|
120
|
+
configurable: true
|
|
121
|
+
});
|
|
122
|
+
Pair.prototype.reserveOf = function (token) {
|
|
123
|
+
(0, tiny_invariant_1.default)(this.involvesToken(token), 'TOKEN');
|
|
124
|
+
return token.equals(this.token0) ? this.reserve0 : this.reserve1;
|
|
125
|
+
};
|
|
126
|
+
Pair.prototype.getOutputAmount = function (inputAmount) {
|
|
127
|
+
(0, tiny_invariant_1.default)(this.involvesToken(inputAmount.token), 'TOKEN');
|
|
128
|
+
if (jsbi_1.default.equal(this.reserve0.raw, constants_1.ZERO) || jsbi_1.default.equal(this.reserve1.raw, constants_1.ZERO)) {
|
|
129
|
+
throw new errors_1.InsufficientReservesError();
|
|
130
|
+
}
|
|
131
|
+
var inputReserve = this.reserveOf(inputAmount.token);
|
|
132
|
+
var outputReserve = this.reserveOf(inputAmount.token.equals(this.token0) ? this.token1 : this.token0);
|
|
133
|
+
var inputAmountWithFee = jsbi_1.default.multiply(inputAmount.raw, constants_1._998);
|
|
134
|
+
var numerator = jsbi_1.default.multiply(inputAmountWithFee, outputReserve.raw);
|
|
135
|
+
var denominator = jsbi_1.default.add(jsbi_1.default.multiply(inputReserve.raw, constants_1._1000), inputAmountWithFee);
|
|
136
|
+
var outputAmount = new tokenAmount_1.TokenAmount(inputAmount.token.equals(this.token0) ? this.token1 : this.token0, jsbi_1.default.divide(numerator, denominator));
|
|
137
|
+
if (jsbi_1.default.equal(outputAmount.raw, constants_1.ZERO)) {
|
|
138
|
+
throw new errors_1.InsufficientInputAmountError();
|
|
139
|
+
}
|
|
140
|
+
return [outputAmount, new Pair(inputReserve.add(inputAmount), outputReserve.subtract(outputAmount))];
|
|
141
|
+
};
|
|
142
|
+
Pair.prototype.getInputAmount = function (outputAmount) {
|
|
143
|
+
(0, tiny_invariant_1.default)(this.involvesToken(outputAmount.token), 'TOKEN');
|
|
144
|
+
if (jsbi_1.default.equal(this.reserve0.raw, constants_1.ZERO) ||
|
|
145
|
+
jsbi_1.default.equal(this.reserve1.raw, constants_1.ZERO) ||
|
|
146
|
+
jsbi_1.default.greaterThanOrEqual(outputAmount.raw, this.reserveOf(outputAmount.token).raw)) {
|
|
147
|
+
throw new errors_1.InsufficientReservesError();
|
|
148
|
+
}
|
|
149
|
+
var outputReserve = this.reserveOf(outputAmount.token);
|
|
150
|
+
var inputReserve = this.reserveOf(outputAmount.token.equals(this.token0) ? this.token1 : this.token0);
|
|
151
|
+
var numerator = jsbi_1.default.multiply(jsbi_1.default.multiply(inputReserve.raw, outputAmount.raw), constants_1._1000);
|
|
152
|
+
var denominator = jsbi_1.default.multiply(jsbi_1.default.subtract(outputReserve.raw, outputAmount.raw), constants_1._998);
|
|
153
|
+
var inputAmount = new tokenAmount_1.TokenAmount(outputAmount.token.equals(this.token0) ? this.token1 : this.token0, jsbi_1.default.add(jsbi_1.default.divide(numerator, denominator), constants_1.ONE));
|
|
154
|
+
return [inputAmount, new Pair(inputReserve.add(inputAmount), outputReserve.subtract(outputAmount))];
|
|
155
|
+
};
|
|
156
|
+
Pair.prototype.getLiquidityMinted = function (totalSupply, tokenAmountA, tokenAmountB) {
|
|
157
|
+
(0, tiny_invariant_1.default)(totalSupply.token.equals(this.liquidityToken), 'LIQUIDITY');
|
|
158
|
+
var tokenAmounts = tokenAmountA.token.sortsBefore(tokenAmountB.token) // does safety checks
|
|
159
|
+
? [tokenAmountA, tokenAmountB]
|
|
160
|
+
: [tokenAmountB, tokenAmountA];
|
|
161
|
+
(0, tiny_invariant_1.default)(tokenAmounts[0].token.equals(this.token0) && tokenAmounts[1].token.equals(this.token1), 'TOKEN');
|
|
162
|
+
var liquidity;
|
|
163
|
+
if (jsbi_1.default.equal(totalSupply.raw, constants_1.ZERO)) {
|
|
164
|
+
liquidity = jsbi_1.default.subtract((0, utils_1.sqrt)(jsbi_1.default.multiply(tokenAmounts[0].raw, tokenAmounts[1].raw)), constants_1.MINIMUM_LIQUIDITY);
|
|
165
|
+
}
|
|
166
|
+
else {
|
|
167
|
+
var amount0 = jsbi_1.default.divide(jsbi_1.default.multiply(tokenAmounts[0].raw, totalSupply.raw), this.reserve0.raw);
|
|
168
|
+
var amount1 = jsbi_1.default.divide(jsbi_1.default.multiply(tokenAmounts[1].raw, totalSupply.raw), this.reserve1.raw);
|
|
169
|
+
liquidity = jsbi_1.default.lessThanOrEqual(amount0, amount1) ? amount0 : amount1;
|
|
170
|
+
}
|
|
171
|
+
if (!jsbi_1.default.greaterThan(liquidity, constants_1.ZERO)) {
|
|
172
|
+
throw new errors_1.InsufficientInputAmountError();
|
|
173
|
+
}
|
|
174
|
+
return new tokenAmount_1.TokenAmount(this.liquidityToken, liquidity);
|
|
175
|
+
};
|
|
176
|
+
Pair.prototype.getLiquidityValue = function (token, totalSupply, liquidity, feeOn, kLast) {
|
|
177
|
+
if (feeOn === void 0) { feeOn = false; }
|
|
178
|
+
(0, tiny_invariant_1.default)(this.involvesToken(token), 'TOKEN');
|
|
179
|
+
(0, tiny_invariant_1.default)(totalSupply.token.equals(this.liquidityToken), 'TOTAL_SUPPLY');
|
|
180
|
+
(0, tiny_invariant_1.default)(liquidity.token.equals(this.liquidityToken), 'LIQUIDITY');
|
|
181
|
+
(0, tiny_invariant_1.default)(jsbi_1.default.lessThanOrEqual(liquidity.raw, totalSupply.raw), 'LIQUIDITY');
|
|
182
|
+
var totalSupplyAdjusted;
|
|
183
|
+
if (!feeOn) {
|
|
184
|
+
totalSupplyAdjusted = totalSupply;
|
|
185
|
+
}
|
|
186
|
+
else {
|
|
187
|
+
(0, tiny_invariant_1.default)(!!kLast, 'K_LAST');
|
|
188
|
+
var kLastParsed = (0, utils_1.parseBigintIsh)(kLast);
|
|
189
|
+
if (!jsbi_1.default.equal(kLastParsed, constants_1.ZERO)) {
|
|
190
|
+
var rootK = (0, utils_1.sqrt)(jsbi_1.default.multiply(this.reserve0.raw, this.reserve1.raw));
|
|
191
|
+
var rootKLast = (0, utils_1.sqrt)(kLastParsed);
|
|
192
|
+
if (jsbi_1.default.greaterThan(rootK, rootKLast)) {
|
|
193
|
+
var numerator = jsbi_1.default.multiply(totalSupply.raw, jsbi_1.default.subtract(rootK, rootKLast));
|
|
194
|
+
var denominator = jsbi_1.default.add(jsbi_1.default.multiply(rootK, constants_1.FIVE), rootKLast);
|
|
195
|
+
var feeLiquidity = jsbi_1.default.divide(numerator, denominator);
|
|
196
|
+
totalSupplyAdjusted = totalSupply.add(new tokenAmount_1.TokenAmount(this.liquidityToken, feeLiquidity));
|
|
197
|
+
}
|
|
198
|
+
else {
|
|
199
|
+
totalSupplyAdjusted = totalSupply;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
else {
|
|
203
|
+
totalSupplyAdjusted = totalSupply;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
return new tokenAmount_1.TokenAmount(token, jsbi_1.default.divide(jsbi_1.default.multiply(liquidity.raw, this.reserveOf(token).raw), totalSupplyAdjusted.raw));
|
|
207
|
+
};
|
|
208
|
+
return Pair;
|
|
209
|
+
}());
|
|
210
|
+
exports.Pair = Pair;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { ChainId } from '../constants';
|
|
2
|
+
import { Currency } from './currency';
|
|
3
|
+
import { Token } from './token';
|
|
4
|
+
import { Pair } from './pair';
|
|
5
|
+
import { Price } from './fractions/price';
|
|
6
|
+
export declare class Route {
|
|
7
|
+
readonly pairs: Pair[];
|
|
8
|
+
readonly path: Token[];
|
|
9
|
+
readonly input: Currency;
|
|
10
|
+
readonly output: Currency;
|
|
11
|
+
readonly midPrice: Price;
|
|
12
|
+
constructor(pairs: any, input: Currency, output?: Currency);
|
|
13
|
+
get chainId(): ChainId;
|
|
14
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
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.Route = void 0;
|
|
7
|
+
var tiny_invariant_1 = __importDefault(require("tiny-invariant"));
|
|
8
|
+
var currency_1 = require("./currency");
|
|
9
|
+
var token_1 = require("./token");
|
|
10
|
+
var price_1 = require("./fractions/price");
|
|
11
|
+
var Route = /** @class */ (function () {
|
|
12
|
+
function Route(pairs, input, output) {
|
|
13
|
+
(0, tiny_invariant_1.default)(pairs.length > 0, 'PAIRS');
|
|
14
|
+
(0, tiny_invariant_1.default)(pairs.every(function (pair) { return pair.chainId === pairs[0].chainId; }), 'CHAIN_IDS');
|
|
15
|
+
(0, tiny_invariant_1.default)((input instanceof token_1.Token && pairs[0].involvesToken(input)) ||
|
|
16
|
+
(input === currency_1.ETHER && pairs[0].involvesToken(token_1.WETH[pairs[0].chainId])), 'INPUT');
|
|
17
|
+
(0, tiny_invariant_1.default)(typeof output === 'undefined' ||
|
|
18
|
+
(output instanceof token_1.Token && pairs[pairs.length - 1].involvesToken(output)) ||
|
|
19
|
+
(output === currency_1.ETHER && pairs[pairs.length - 1].involvesToken(token_1.WETH[pairs[0].chainId])), 'OUTPUT');
|
|
20
|
+
var path = [input instanceof token_1.Token ? input : token_1.WETH[pairs[0].chainId]];
|
|
21
|
+
for (var _i = 0, _a = pairs.entries(); _i < _a.length; _i++) {
|
|
22
|
+
var _b = _a[_i], i = _b[0], pair = _b[1];
|
|
23
|
+
var currentInput = path[i];
|
|
24
|
+
(0, tiny_invariant_1.default)(currentInput.equals(pair.token0) || currentInput.equals(pair.token1), 'PATH');
|
|
25
|
+
var output_1 = currentInput.equals(pair.token0) ? pair.token1 : pair.token0;
|
|
26
|
+
path.push(output_1);
|
|
27
|
+
}
|
|
28
|
+
this.pairs = pairs;
|
|
29
|
+
this.path = path;
|
|
30
|
+
this.midPrice = price_1.Price.fromRoute(this);
|
|
31
|
+
this.input = input;
|
|
32
|
+
this.output = output !== null && output !== void 0 ? output : path[path.length - 1];
|
|
33
|
+
}
|
|
34
|
+
Object.defineProperty(Route.prototype, "chainId", {
|
|
35
|
+
get: function () {
|
|
36
|
+
return this.pairs[0].chainId;
|
|
37
|
+
},
|
|
38
|
+
enumerable: false,
|
|
39
|
+
configurable: true
|
|
40
|
+
});
|
|
41
|
+
return Route;
|
|
42
|
+
}());
|
|
43
|
+
exports.Route = Route;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { ChainId } from '../constants';
|
|
2
|
+
import { Currency } from './currency';
|
|
3
|
+
/**
|
|
4
|
+
* Represents an ERC20 token with a unique address and some metadata.
|
|
5
|
+
*/
|
|
6
|
+
export declare class Token extends Currency {
|
|
7
|
+
readonly chainId: ChainId;
|
|
8
|
+
readonly address: string;
|
|
9
|
+
constructor(chainId: ChainId, address: string, decimals: number, symbol?: string, name?: string);
|
|
10
|
+
/**
|
|
11
|
+
* Returns true if the two tokens are equivalent, i.e. have the same chainId and address.
|
|
12
|
+
* @param other other token to compare
|
|
13
|
+
*/
|
|
14
|
+
equals(other: Token): boolean;
|
|
15
|
+
/**
|
|
16
|
+
* Returns true if the address of this token sorts before the address of the other token
|
|
17
|
+
* @param other other token to compare
|
|
18
|
+
* @throws if the tokens have the same address
|
|
19
|
+
* @throws if the tokens are on different chains
|
|
20
|
+
*/
|
|
21
|
+
sortsBefore(other: Token): boolean;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Compares two currencies for equality
|
|
25
|
+
*/
|
|
26
|
+
export declare function currencyEquals(currencyA: Currency, currencyB: Currency): boolean;
|
|
27
|
+
export declare const WETH: any;
|
|
@@ -0,0 +1,87 @@
|
|
|
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
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
18
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
19
|
+
};
|
|
20
|
+
var _a;
|
|
21
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
22
|
+
exports.WETH = exports.currencyEquals = exports.Token = void 0;
|
|
23
|
+
var tiny_invariant_1 = __importDefault(require("tiny-invariant"));
|
|
24
|
+
var constants_1 = require("../constants");
|
|
25
|
+
var utils_1 = require("../utils");
|
|
26
|
+
var currency_1 = require("./currency");
|
|
27
|
+
/**
|
|
28
|
+
* Represents an ERC20 token with a unique address and some metadata.
|
|
29
|
+
*/
|
|
30
|
+
var Token = /** @class */ (function (_super) {
|
|
31
|
+
__extends(Token, _super);
|
|
32
|
+
function Token(chainId, address, decimals, symbol, name) {
|
|
33
|
+
var _this = _super.call(this, decimals, symbol, name) || this;
|
|
34
|
+
_this.chainId = chainId;
|
|
35
|
+
_this.address = (0, utils_1.validateAndParseAddress)(address);
|
|
36
|
+
return _this;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Returns true if the two tokens are equivalent, i.e. have the same chainId and address.
|
|
40
|
+
* @param other other token to compare
|
|
41
|
+
*/
|
|
42
|
+
Token.prototype.equals = function (other) {
|
|
43
|
+
// short circuit on reference equality
|
|
44
|
+
if (this === other) {
|
|
45
|
+
return true;
|
|
46
|
+
}
|
|
47
|
+
return this.chainId === other.chainId && this.address === other.address;
|
|
48
|
+
};
|
|
49
|
+
/**
|
|
50
|
+
* Returns true if the address of this token sorts before the address of the other token
|
|
51
|
+
* @param other other token to compare
|
|
52
|
+
* @throws if the tokens have the same address
|
|
53
|
+
* @throws if the tokens are on different chains
|
|
54
|
+
*/
|
|
55
|
+
Token.prototype.sortsBefore = function (other) {
|
|
56
|
+
(0, tiny_invariant_1.default)(this.chainId === other.chainId, 'CHAIN_IDS');
|
|
57
|
+
(0, tiny_invariant_1.default)(this.address !== other.address, 'ADDRESSES');
|
|
58
|
+
return this.address.toLowerCase() < other.address.toLowerCase();
|
|
59
|
+
};
|
|
60
|
+
return Token;
|
|
61
|
+
}(currency_1.Currency));
|
|
62
|
+
exports.Token = Token;
|
|
63
|
+
/**
|
|
64
|
+
* Compares two currencies for equality
|
|
65
|
+
*/
|
|
66
|
+
function currencyEquals(currencyA, currencyB) {
|
|
67
|
+
if (currencyA instanceof Token && currencyB instanceof Token) {
|
|
68
|
+
return currencyA.equals(currencyB);
|
|
69
|
+
}
|
|
70
|
+
else if (currencyA instanceof Token) {
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
else if (currencyB instanceof Token) {
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
return currencyA === currencyB;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
exports.currencyEquals = currencyEquals;
|
|
81
|
+
exports.WETH = (_a = {},
|
|
82
|
+
_a[constants_1.ChainId.ETH] = new Token(constants_1.ChainId.ETH, '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2', 18, 'WETH', 'Wrapped ETH'),
|
|
83
|
+
_a[constants_1.ChainId.MAINNET] = new Token(constants_1.ChainId.MAINNET, '0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c', 18, 'WBNB', 'Wrapped BNB'),
|
|
84
|
+
_a[constants_1.ChainId.BSCTESTNET] = new Token(constants_1.ChainId.BSCTESTNET, '0xaE8E19eFB41e7b96815649A6a60785e1fbA84C1e', 18, 'WBNB', 'Wrapped BNB'),
|
|
85
|
+
_a[constants_1.ChainId.POLYGON] = new Token(constants_1.ChainId.POLYGON, '0x0d500B1d8E8eF31E21C99d1Db9A6444d3ADf1270', 18, 'WMATIC', 'Wrapped MATIC'),
|
|
86
|
+
_a[constants_1.ChainId.AVAX] = new Token(constants_1.ChainId.AVAX, '0xb31f66aa3c1e785363f0875a1b74e27b85fd66c7', 18, 'WAVAX', 'Avalanche Token'),
|
|
87
|
+
_a);
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { TradeType } from '../constants';
|
|
2
|
+
import { Currency } from './currency';
|
|
3
|
+
import { CurrencyAmount } from './fractions/currencyAmount';
|
|
4
|
+
import { Percent } from './fractions/percent';
|
|
5
|
+
import { Price } from './fractions/price';
|
|
6
|
+
import { Pair } from './pair';
|
|
7
|
+
import { Route } from './route';
|
|
8
|
+
interface InputOutput {
|
|
9
|
+
readonly inputAmount: CurrencyAmount;
|
|
10
|
+
readonly outputAmount: CurrencyAmount;
|
|
11
|
+
}
|
|
12
|
+
export declare function inputOutputComparator(a: InputOutput, b: InputOutput): number;
|
|
13
|
+
export declare function tradeComparator(a: Trade, b: Trade): number;
|
|
14
|
+
export interface BestTradeOptions {
|
|
15
|
+
maxNumResults?: number;
|
|
16
|
+
maxHops?: number;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Represents a trade executed against a list of pairs.
|
|
20
|
+
* Does not account for slippage, i.e. trades that front run this trade and move the price.
|
|
21
|
+
*/
|
|
22
|
+
export declare class Trade {
|
|
23
|
+
/**
|
|
24
|
+
* The route of the trade, i.e. which pairs the trade goes through.
|
|
25
|
+
*/
|
|
26
|
+
readonly route: Route;
|
|
27
|
+
/**
|
|
28
|
+
* The type of the trade, either exact in or exact out.
|
|
29
|
+
*/
|
|
30
|
+
readonly tradeType: TradeType;
|
|
31
|
+
/**
|
|
32
|
+
* The input amount for the trade assuming no slippage.
|
|
33
|
+
*/
|
|
34
|
+
readonly inputAmount: CurrencyAmount;
|
|
35
|
+
/**
|
|
36
|
+
* The output amount for the trade assuming no slippage.
|
|
37
|
+
*/
|
|
38
|
+
readonly outputAmount: CurrencyAmount;
|
|
39
|
+
/**
|
|
40
|
+
* The price expressed in terms of output amount/input amount.
|
|
41
|
+
*/
|
|
42
|
+
readonly executionPrice: Price;
|
|
43
|
+
/**
|
|
44
|
+
* The mid price after the trade executes assuming no slippage.
|
|
45
|
+
*/
|
|
46
|
+
readonly nextMidPrice: Price;
|
|
47
|
+
/**
|
|
48
|
+
* The percent difference between the mid price before the trade and the trade execution price.
|
|
49
|
+
*/
|
|
50
|
+
readonly priceImpact: Percent;
|
|
51
|
+
/**
|
|
52
|
+
* Constructs an exact in trade with the given amount in and route
|
|
53
|
+
* @param route route of the exact in trade
|
|
54
|
+
* @param amountIn the amount being passed in
|
|
55
|
+
*/
|
|
56
|
+
static exactIn(route: Route, amountIn: CurrencyAmount): Trade;
|
|
57
|
+
/**
|
|
58
|
+
* Constructs an exact out trade with the given amount out and route
|
|
59
|
+
* @param route route of the exact out trade
|
|
60
|
+
* @param amountOut the amount returned by the trade
|
|
61
|
+
*/
|
|
62
|
+
static exactOut(route: Route, amountOut: CurrencyAmount): Trade;
|
|
63
|
+
constructor(route: Route, amount: CurrencyAmount, tradeType: TradeType);
|
|
64
|
+
/**
|
|
65
|
+
* Get the minimum amount that must be received from this trade for the given slippage tolerance
|
|
66
|
+
* @param slippageTolerance tolerance of unfavorable slippage from the execution price of this trade
|
|
67
|
+
*/
|
|
68
|
+
minimumAmountOut(slippageTolerance: Percent): CurrencyAmount;
|
|
69
|
+
/**
|
|
70
|
+
* Get the maximum amount in that can be spent via this trade for the given slippage tolerance
|
|
71
|
+
* @param slippageTolerance tolerance of unfavorable slippage from the execution price of this trade
|
|
72
|
+
*/
|
|
73
|
+
maximumAmountIn(slippageTolerance: Percent): CurrencyAmount;
|
|
74
|
+
/**
|
|
75
|
+
* Given a list of pairs, and a fixed amount in, returns the top `maxNumResults` trades that go from an input token
|
|
76
|
+
* amount to an output token, making at most `maxHops` hops.
|
|
77
|
+
* Note this does not consider aggregation, as routes are linear. It's possible a better route exists by splitting
|
|
78
|
+
* the amount in among multiple routes.
|
|
79
|
+
* @param pairs the pairs to consider in finding the best trade
|
|
80
|
+
* @param currencyAmountIn exact amount of input currency to spend
|
|
81
|
+
* @param currencyOut the desired currency out
|
|
82
|
+
* @param maxNumResults maximum number of results to return
|
|
83
|
+
* @param maxHops maximum number of hops a returned trade can make, e.g. 1 hop goes through a single pair
|
|
84
|
+
* @param currentPairs used in recursion; the current list of pairs
|
|
85
|
+
* @param originalAmountIn used in recursion; the original value of the currencyAmountIn parameter
|
|
86
|
+
* @param bestTrades used in recursion; the current list of best trades
|
|
87
|
+
*/
|
|
88
|
+
static bestTradeExactIn(pairs: Pair[], currencyAmountIn: CurrencyAmount, currencyOut: Currency, { maxNumResults, maxHops }?: BestTradeOptions, currentPairs?: Pair[], originalAmountIn?: CurrencyAmount, bestTrades?: Trade[]): Trade[];
|
|
89
|
+
/**
|
|
90
|
+
* similar to the above method but instead targets a fixed output amount
|
|
91
|
+
* given a list of pairs, and a fixed amount out, returns the top `maxNumResults` trades that go from an input token
|
|
92
|
+
* to an output token amount, making at most `maxHops` hops
|
|
93
|
+
* note this does not consider aggregation, as routes are linear. it's possible a better route exists by splitting
|
|
94
|
+
* the amount in among multiple routes.
|
|
95
|
+
* @param pairs the pairs to consider in finding the best trade
|
|
96
|
+
* @param currencyIn the currency to spend
|
|
97
|
+
* @param currencyAmountOut the exact amount of currency out
|
|
98
|
+
* @param maxNumResults maximum number of results to return
|
|
99
|
+
* @param maxHops maximum number of hops a returned trade can make, e.g. 1 hop goes through a single pair
|
|
100
|
+
* @param currentPairs used in recursion; the current list of pairs
|
|
101
|
+
* @param originalAmountOut used in recursion; the original value of the currencyAmountOut parameter
|
|
102
|
+
* @param bestTrades used in recursion; the current list of best trades
|
|
103
|
+
*/
|
|
104
|
+
static bestTradeExactOut(pairs: Pair[], currencyIn: Currency, currencyAmountOut: CurrencyAmount, { maxNumResults, maxHops }?: BestTradeOptions, currentPairs?: Pair[], originalAmountOut?: CurrencyAmount, bestTrades?: Trade[]): Trade[];
|
|
105
|
+
}
|
|
106
|
+
export {};
|