@openocean.finance/openocean-sdk 1.2.33 → 1.2.35

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.
Files changed (38) hide show
  1. package/lib/api/index.js +205 -0
  2. package/lib/api/vo/RequestVo.js +401 -0
  3. package/lib/asset/abi/ERC20_abi.js +119 -0
  4. package/lib/asset/abi/aggregator.js +391 -0
  5. package/lib/config/index.js +33 -0
  6. package/lib/index.d.ts +1 -1
  7. package/lib/index.js +50 -2
  8. package/lib/swapSdk/Approve.js +349 -0
  9. package/lib/swapSdk/NotoMobile.js +144 -0
  10. package/lib/swapSdk/RequestVo.js +94 -0
  11. package/lib/swapSdk/Swap.js +1159 -0
  12. package/lib/swapSdk/getAllowance.js +98 -0
  13. package/lib/swapSdk/getBalance.js +280 -0
  14. package/lib/swapSdk/index.js +257 -0
  15. package/lib/swapSdk/qrcode.js +969 -0
  16. package/lib/utils/ajx.js +153 -0
  17. package/lib/utils/index.js +367 -0
  18. package/lib/utils/web3.js +9 -0
  19. package/lib/v1/abis/ERC20.js +22 -0
  20. package/lib/v1/abis/IUniswapV2Pair.js +1434 -0
  21. package/lib/v1/constants.js +71 -0
  22. package/lib/v1/entities/currency.js +39 -0
  23. package/lib/v1/entities/fractions/currencyAmount.js +83 -0
  24. package/lib/v1/entities/fractions/fraction.js +109 -0
  25. package/lib/v1/entities/fractions/index.js +21 -0
  26. package/lib/v1/entities/fractions/percent.js +37 -0
  27. package/lib/v1/entities/fractions/price.js +90 -0
  28. package/lib/v1/entities/fractions/tokenAmount.js +43 -0
  29. package/lib/v1/entities/index.js +22 -0
  30. package/lib/v1/entities/pair.js +210 -0
  31. package/lib/v1/entities/route.js +43 -0
  32. package/lib/v1/entities/token.js +87 -0
  33. package/lib/v1/entities/trade.js +336 -0
  34. package/lib/v1/errors.js +56 -0
  35. package/lib/v1/fetcher.js +140 -0
  36. package/lib/v1/router.js +97 -0
  37. package/lib/v1/utils.js +87 -0
  38. package/package.json +2 -2
@@ -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,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,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);