@pancakeswap/v2-sdk 1.0.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/dist/abis/IPancakePair.d.ts +561 -0
- package/dist/abis/IPancakePair.d.ts.map +1 -0
- package/dist/constants.d.ts +47 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/entities/index.d.ts +4 -0
- package/dist/entities/index.d.ts.map +1 -0
- package/dist/entities/pair.d.ts +46 -0
- package/dist/entities/pair.d.ts.map +1 -0
- package/dist/entities/route.d.ts +13 -0
- package/dist/entities/route.d.ts.map +1 -0
- package/dist/entities/trade.d.ts +104 -0
- package/dist/entities/trade.d.ts.map +1 -0
- package/dist/fetcher.d.ts +30 -0
- package/dist/fetcher.d.ts.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1428 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +1409 -0
- package/dist/index.mjs.map +1 -0
- package/dist/router.d.ts +62 -0
- package/dist/router.d.ts.map +1 -0
- package/dist/trade.d.ts +5 -0
- package/dist/trade.d.ts.map +1 -0
- package/package.json +44 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,1428 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var chains = require('@pancakeswap/chains');
|
|
4
|
+
var swapSdkCore = require('@pancakeswap/swap-sdk-core');
|
|
5
|
+
var viem = require('viem');
|
|
6
|
+
var invariant3 = require('tiny-invariant');
|
|
7
|
+
var swapSdkEvm = require('@pancakeswap/swap-sdk-evm');
|
|
8
|
+
var chains$1 = require('viem/chains');
|
|
9
|
+
|
|
10
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
11
|
+
|
|
12
|
+
var invariant3__default = /*#__PURE__*/_interopDefault(invariant3);
|
|
13
|
+
|
|
14
|
+
// src/constants.ts
|
|
15
|
+
var FACTORY_ADDRESS = "0xcA143Ce32Fe78f1f7019d7d551a6402fC5350c73";
|
|
16
|
+
var FACTORY_ADDRESS_ETH = "0x1097053Fd2ea711dad45caCcc45EfF7548fCB362";
|
|
17
|
+
var FACTORY_ADDRESS_MAP = {
|
|
18
|
+
[chains.ChainId.ETHEREUM]: FACTORY_ADDRESS_ETH,
|
|
19
|
+
[chains.ChainId.GOERLI]: FACTORY_ADDRESS_ETH,
|
|
20
|
+
[chains.ChainId.BSC]: FACTORY_ADDRESS,
|
|
21
|
+
[chains.ChainId.BSC_TESTNET]: "0x6725F303b657a9451d8BA641348b6761A6CC7a17",
|
|
22
|
+
[chains.ChainId.ARBITRUM_ONE]: "0x02a84c1b3BBD7401a5f7fa98a384EBC70bB5749E",
|
|
23
|
+
[chains.ChainId.ARBITRUM_GOERLI]: "0x333EAE459075b1d7dE8eb57997b5d4eee5F1070a",
|
|
24
|
+
[chains.ChainId.POLYGON_ZKEVM]: "0x02a84c1b3BBD7401a5f7fa98a384EBC70bB5749E",
|
|
25
|
+
[chains.ChainId.POLYGON_ZKEVM_TESTNET]: "0xBA40c83026213F9cbc79998752721a0312bdB74a",
|
|
26
|
+
[chains.ChainId.ZKSYNC]: "0xd03D8D566183F0086d8D09A84E1e30b58Dd5619d",
|
|
27
|
+
[chains.ChainId.ZKSYNC_TESTNET]: "0x48a33610Cd0E130af2024D55F67aE72a8C51aC27",
|
|
28
|
+
[chains.ChainId.LINEA]: "0x02a84c1b3BBD7401a5f7fa98a384EBC70bB5749E",
|
|
29
|
+
[chains.ChainId.LINEA_TESTNET]: "0xB6FAfd4ADbCd21cF665909767e0eD0D05709abfB",
|
|
30
|
+
[chains.ChainId.OPBNB]: "0x02a84c1b3BBD7401a5f7fa98a384EBC70bB5749E",
|
|
31
|
+
[chains.ChainId.OPBNB_TESTNET]: "0x776e4bD2f72de2176A59465e316335aaf8ed4E8F",
|
|
32
|
+
[chains.ChainId.BASE]: "0x02a84c1b3BBD7401a5f7fa98a384EBC70bB5749E",
|
|
33
|
+
[chains.ChainId.BASE_TESTNET]: "0x715303D2eF7dA7FFAbF637651D71FD11d41fAf7F",
|
|
34
|
+
[chains.ChainId.SCROLL_SEPOLIA]: "0x2B3C5df29F73dbF028BA82C33e0A5A6e5800F75e",
|
|
35
|
+
[chains.ChainId.SEPOLIA]: "0x1bdc540dEB9Ed1fA29964DeEcCc524A8f5e2198e",
|
|
36
|
+
[chains.ChainId.ARBITRUM_SEPOLIA]: "0x02a84c1b3BBD7401a5f7fa98a384EBC70bB5749E",
|
|
37
|
+
[chains.ChainId.BASE_SEPOLIA]: "0x02a84c1b3BBD7401a5f7fa98a384EBC70bB5749E"
|
|
38
|
+
};
|
|
39
|
+
var INIT_CODE_HASH = "0x00fb7f630766e6a796048ea87d01acd3068e8ff67d078148a3fa3f4a84f69bd5";
|
|
40
|
+
var INIT_CODE_HASH_ETH = "0x57224589c67f3f30a6b0d7a1b54cf3153ab84563bc609ef41dfb34f8b2974d2d";
|
|
41
|
+
var INIT_CODE_HASH_MAP = {
|
|
42
|
+
[chains.ChainId.ETHEREUM]: INIT_CODE_HASH_ETH,
|
|
43
|
+
[chains.ChainId.GOERLI]: INIT_CODE_HASH_ETH,
|
|
44
|
+
[chains.ChainId.BSC]: INIT_CODE_HASH,
|
|
45
|
+
[chains.ChainId.BSC_TESTNET]: "0xd0d4c4cd0848c93cb4fd1f498d7013ee6bfb25783ea21593d5834f5d250ece66",
|
|
46
|
+
[chains.ChainId.ARBITRUM_ONE]: INIT_CODE_HASH_ETH,
|
|
47
|
+
[chains.ChainId.ARBITRUM_GOERLI]: INIT_CODE_HASH_ETH,
|
|
48
|
+
[chains.ChainId.POLYGON_ZKEVM]: INIT_CODE_HASH_ETH,
|
|
49
|
+
[chains.ChainId.POLYGON_ZKEVM_TESTNET]: INIT_CODE_HASH_ETH,
|
|
50
|
+
[chains.ChainId.ZKSYNC]: "0x0100045707a42494392b3558029b9869f865ff9df8f375dc1bf20b0555093f43",
|
|
51
|
+
[chains.ChainId.ZKSYNC_TESTNET]: "0x0100045707a42494392b3558029b9869f865ff9df8f375dc1bf20b0555093f43",
|
|
52
|
+
[chains.ChainId.LINEA]: INIT_CODE_HASH_ETH,
|
|
53
|
+
[chains.ChainId.LINEA_TESTNET]: INIT_CODE_HASH_ETH,
|
|
54
|
+
[chains.ChainId.OPBNB]: INIT_CODE_HASH_ETH,
|
|
55
|
+
[chains.ChainId.OPBNB_TESTNET]: INIT_CODE_HASH_ETH,
|
|
56
|
+
[chains.ChainId.BASE]: INIT_CODE_HASH_ETH,
|
|
57
|
+
[chains.ChainId.BASE_TESTNET]: "0xa5934690703a592a07e841ca29d5e5c79b5e22ed4749057bb216dc31100be1c0",
|
|
58
|
+
[chains.ChainId.SCROLL_SEPOLIA]: INIT_CODE_HASH_ETH,
|
|
59
|
+
[chains.ChainId.SEPOLIA]: INIT_CODE_HASH_ETH,
|
|
60
|
+
[chains.ChainId.ARBITRUM_SEPOLIA]: INIT_CODE_HASH_ETH,
|
|
61
|
+
[chains.ChainId.BASE_SEPOLIA]: INIT_CODE_HASH_ETH
|
|
62
|
+
};
|
|
63
|
+
var PAIR_ADDRESS_CACHE = {};
|
|
64
|
+
var composeKey = (token0, token1) => `${token0.chainId}-${token0.address}-${token1.address}`;
|
|
65
|
+
function getCreate2Address(from_, salt_, initCodeHash) {
|
|
66
|
+
const from = viem.toBytes(viem.getAddress(from_));
|
|
67
|
+
const salt = viem.pad(viem.isBytes(salt_) ? salt_ : viem.toBytes(salt_), {
|
|
68
|
+
size: 32
|
|
69
|
+
});
|
|
70
|
+
return viem.getAddress(viem.slice(viem.keccak256(viem.concat([viem.toBytes("0xff"), from, salt, viem.toBytes(initCodeHash)])), 12));
|
|
71
|
+
}
|
|
72
|
+
var EMPTY_INPU_HASH = "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470";
|
|
73
|
+
var ZKSYNC_PREFIX = "0x2020dba91b30cc0006188af794c2fb30dd8520db7e2c088b7fc7c103c00ca494";
|
|
74
|
+
function getCreate2AddressZkSync(from, salt, initCodeHash) {
|
|
75
|
+
return viem.getAddress(
|
|
76
|
+
`0x${viem.keccak256(viem.concat([ZKSYNC_PREFIX, viem.pad(from, { size: 32 }), salt, initCodeHash, EMPTY_INPU_HASH])).slice(26)}`
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
var computePairAddress = ({
|
|
80
|
+
factoryAddress,
|
|
81
|
+
tokenA,
|
|
82
|
+
tokenB
|
|
83
|
+
}) => {
|
|
84
|
+
const [token0, token1] = tokenA.sortsBefore(tokenB) ? [tokenA, tokenB] : [tokenB, tokenA];
|
|
85
|
+
const key = composeKey(token0, token1);
|
|
86
|
+
if (PAIR_ADDRESS_CACHE?.[key] === void 0) {
|
|
87
|
+
const getCreate2Address_ = token0.chainId === chains.ChainId.ZKSYNC_TESTNET || token1.chainId === chains.ChainId.ZKSYNC ? getCreate2AddressZkSync : getCreate2Address;
|
|
88
|
+
PAIR_ADDRESS_CACHE = {
|
|
89
|
+
...PAIR_ADDRESS_CACHE,
|
|
90
|
+
[key]: getCreate2Address_(
|
|
91
|
+
factoryAddress,
|
|
92
|
+
viem.keccak256(viem.encodePacked(["address", "address"], [token0.address, token1.address])),
|
|
93
|
+
INIT_CODE_HASH_MAP[token0.chainId]
|
|
94
|
+
)
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
return PAIR_ADDRESS_CACHE[key];
|
|
98
|
+
};
|
|
99
|
+
var Pair = class {
|
|
100
|
+
static getAddress(tokenA, tokenB) {
|
|
101
|
+
return computePairAddress({
|
|
102
|
+
factoryAddress: FACTORY_ADDRESS_MAP[tokenA.chainId],
|
|
103
|
+
tokenA,
|
|
104
|
+
tokenB
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
constructor(currencyAmountA, tokenAmountB) {
|
|
108
|
+
const tokenAmounts = currencyAmountA.currency.sortsBefore(tokenAmountB.currency) ? [currencyAmountA, tokenAmountB] : [tokenAmountB, currencyAmountA];
|
|
109
|
+
this.liquidityToken = new swapSdkEvm.ERC20Token(
|
|
110
|
+
tokenAmounts[0].currency.chainId,
|
|
111
|
+
Pair.getAddress(tokenAmounts[0].currency, tokenAmounts[1].currency),
|
|
112
|
+
18,
|
|
113
|
+
"Cake-LP",
|
|
114
|
+
"Pancake LPs"
|
|
115
|
+
);
|
|
116
|
+
this.tokenAmounts = tokenAmounts;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Returns true if the token is either token0 or token1
|
|
120
|
+
* @param token to check
|
|
121
|
+
*/
|
|
122
|
+
involvesToken(token) {
|
|
123
|
+
return token.equals(this.token0) || token.equals(this.token1);
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Returns the current mid price of the pair in terms of token0, i.e. the ratio of reserve1 to reserve0
|
|
127
|
+
*/
|
|
128
|
+
get token0Price() {
|
|
129
|
+
const result = this.tokenAmounts[1].divide(this.tokenAmounts[0]);
|
|
130
|
+
return new swapSdkCore.Price(this.token0, this.token1, result.denominator, result.numerator);
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Returns the current mid price of the pair in terms of token1, i.e. the ratio of reserve0 to reserve1
|
|
134
|
+
*/
|
|
135
|
+
get token1Price() {
|
|
136
|
+
const result = this.tokenAmounts[0].divide(this.tokenAmounts[1]);
|
|
137
|
+
return new swapSdkCore.Price(this.token1, this.token0, result.denominator, result.numerator);
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Return the price of the given token in terms of the other token in the pair.
|
|
141
|
+
* @param token token to return price of
|
|
142
|
+
*/
|
|
143
|
+
priceOf(token) {
|
|
144
|
+
invariant3__default.default(this.involvesToken(token), "TOKEN");
|
|
145
|
+
return token.equals(this.token0) ? this.token0Price : this.token1Price;
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Returns the chain ID of the tokens in the pair.
|
|
149
|
+
*/
|
|
150
|
+
get chainId() {
|
|
151
|
+
return this.token0.chainId;
|
|
152
|
+
}
|
|
153
|
+
get token0() {
|
|
154
|
+
return this.tokenAmounts[0].currency;
|
|
155
|
+
}
|
|
156
|
+
get token1() {
|
|
157
|
+
return this.tokenAmounts[1].currency;
|
|
158
|
+
}
|
|
159
|
+
get reserve0() {
|
|
160
|
+
return this.tokenAmounts[0];
|
|
161
|
+
}
|
|
162
|
+
get reserve1() {
|
|
163
|
+
return this.tokenAmounts[1];
|
|
164
|
+
}
|
|
165
|
+
reserveOf(token) {
|
|
166
|
+
invariant3__default.default(this.involvesToken(token), "TOKEN");
|
|
167
|
+
return token.equals(this.token0) ? this.reserve0 : this.reserve1;
|
|
168
|
+
}
|
|
169
|
+
getOutputAmount(inputAmount) {
|
|
170
|
+
invariant3__default.default(this.involvesToken(inputAmount.currency), "TOKEN");
|
|
171
|
+
if (this.reserve0.quotient === swapSdkCore.ZERO || this.reserve1.quotient === swapSdkCore.ZERO) {
|
|
172
|
+
throw new swapSdkCore.InsufficientReservesError();
|
|
173
|
+
}
|
|
174
|
+
const inputReserve = this.reserveOf(inputAmount.currency);
|
|
175
|
+
const outputReserve = this.reserveOf(inputAmount.currency.equals(this.token0) ? this.token1 : this.token0);
|
|
176
|
+
const inputAmountWithFee = inputAmount.quotient * swapSdkCore._9975;
|
|
177
|
+
const numerator = inputAmountWithFee * outputReserve.quotient;
|
|
178
|
+
const denominator = inputReserve.quotient * swapSdkCore._10000 + inputAmountWithFee;
|
|
179
|
+
const outputAmount = swapSdkCore.CurrencyAmount.fromRawAmount(
|
|
180
|
+
inputAmount.currency.equals(this.token0) ? this.token1 : this.token0,
|
|
181
|
+
numerator / denominator
|
|
182
|
+
);
|
|
183
|
+
if (outputAmount.quotient === swapSdkCore.ZERO) {
|
|
184
|
+
throw new swapSdkCore.InsufficientInputAmountError();
|
|
185
|
+
}
|
|
186
|
+
return [outputAmount, new Pair(inputReserve.add(inputAmount), outputReserve.subtract(outputAmount))];
|
|
187
|
+
}
|
|
188
|
+
getInputAmount(outputAmount) {
|
|
189
|
+
invariant3__default.default(this.involvesToken(outputAmount.currency), "TOKEN");
|
|
190
|
+
if (this.reserve0.quotient === swapSdkCore.ZERO || this.reserve1.quotient === swapSdkCore.ZERO || outputAmount.quotient >= this.reserveOf(outputAmount.currency).quotient) {
|
|
191
|
+
throw new swapSdkCore.InsufficientReservesError();
|
|
192
|
+
}
|
|
193
|
+
const outputReserve = this.reserveOf(outputAmount.currency);
|
|
194
|
+
const inputReserve = this.reserveOf(outputAmount.currency.equals(this.token0) ? this.token1 : this.token0);
|
|
195
|
+
const numerator = inputReserve.quotient * outputAmount.quotient * swapSdkCore._10000;
|
|
196
|
+
const denominator = (outputReserve.quotient - outputAmount.quotient) * swapSdkCore._9975;
|
|
197
|
+
const inputAmount = swapSdkCore.CurrencyAmount.fromRawAmount(
|
|
198
|
+
outputAmount.currency.equals(this.token0) ? this.token1 : this.token0,
|
|
199
|
+
numerator / denominator + swapSdkCore.ONE
|
|
200
|
+
);
|
|
201
|
+
return [inputAmount, new Pair(inputReserve.add(inputAmount), outputReserve.subtract(outputAmount))];
|
|
202
|
+
}
|
|
203
|
+
getLiquidityMinted(totalSupply, tokenAmountA, tokenAmountB) {
|
|
204
|
+
invariant3__default.default(totalSupply.currency.equals(this.liquidityToken), "LIQUIDITY");
|
|
205
|
+
const tokenAmounts = tokenAmountA.currency.sortsBefore(tokenAmountB.currency) ? [tokenAmountA, tokenAmountB] : [tokenAmountB, tokenAmountA];
|
|
206
|
+
invariant3__default.default(tokenAmounts[0].currency.equals(this.token0) && tokenAmounts[1].currency.equals(this.token1), "TOKEN");
|
|
207
|
+
let liquidity;
|
|
208
|
+
if (totalSupply.quotient === swapSdkCore.ZERO) {
|
|
209
|
+
liquidity = swapSdkCore.sqrt(tokenAmounts[0].quotient * tokenAmounts[1].quotient) - swapSdkCore.MINIMUM_LIQUIDITY;
|
|
210
|
+
} else {
|
|
211
|
+
const amount0 = tokenAmounts[0].quotient * totalSupply.quotient / this.reserve0.quotient;
|
|
212
|
+
const amount1 = tokenAmounts[1].quotient * totalSupply.quotient / this.reserve1.quotient;
|
|
213
|
+
liquidity = amount0 <= amount1 ? amount0 : amount1;
|
|
214
|
+
}
|
|
215
|
+
if (!(liquidity > swapSdkCore.ZERO)) {
|
|
216
|
+
throw new swapSdkCore.InsufficientInputAmountError();
|
|
217
|
+
}
|
|
218
|
+
return swapSdkCore.CurrencyAmount.fromRawAmount(this.liquidityToken, liquidity);
|
|
219
|
+
}
|
|
220
|
+
getLiquidityValue(token, totalSupply, liquidity, feeOn = false, kLast) {
|
|
221
|
+
invariant3__default.default(this.involvesToken(token), "TOKEN");
|
|
222
|
+
invariant3__default.default(totalSupply.currency.equals(this.liquidityToken), "TOTAL_SUPPLY");
|
|
223
|
+
invariant3__default.default(liquidity.currency.equals(this.liquidityToken), "LIQUIDITY");
|
|
224
|
+
invariant3__default.default(liquidity.quotient <= totalSupply.quotient, "LIQUIDITY");
|
|
225
|
+
let totalSupplyAdjusted;
|
|
226
|
+
if (!feeOn) {
|
|
227
|
+
totalSupplyAdjusted = totalSupply;
|
|
228
|
+
} else {
|
|
229
|
+
invariant3__default.default(!!kLast, "K_LAST");
|
|
230
|
+
const kLastParsed = BigInt(kLast);
|
|
231
|
+
if (!(kLastParsed === swapSdkCore.ZERO)) {
|
|
232
|
+
const rootK = swapSdkCore.sqrt(this.reserve0.quotient * this.reserve1.quotient);
|
|
233
|
+
const rootKLast = swapSdkCore.sqrt(kLastParsed);
|
|
234
|
+
if (rootK > rootKLast) {
|
|
235
|
+
const numerator = totalSupply.quotient * (rootK - rootKLast);
|
|
236
|
+
const denominator = rootK * swapSdkCore.FIVE + rootKLast;
|
|
237
|
+
const feeLiquidity = numerator / denominator;
|
|
238
|
+
totalSupplyAdjusted = totalSupply.add(swapSdkCore.CurrencyAmount.fromRawAmount(this.liquidityToken, feeLiquidity));
|
|
239
|
+
} else {
|
|
240
|
+
totalSupplyAdjusted = totalSupply;
|
|
241
|
+
}
|
|
242
|
+
} else {
|
|
243
|
+
totalSupplyAdjusted = totalSupply;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
return swapSdkCore.CurrencyAmount.fromRawAmount(
|
|
247
|
+
token,
|
|
248
|
+
liquidity.quotient * this.reserveOf(token).quotient / totalSupplyAdjusted.quotient
|
|
249
|
+
);
|
|
250
|
+
}
|
|
251
|
+
};
|
|
252
|
+
var Route = class {
|
|
253
|
+
constructor(pairs, input, output) {
|
|
254
|
+
this._midPrice = null;
|
|
255
|
+
invariant3__default.default(pairs.length > 0, "PAIRS");
|
|
256
|
+
const { chainId } = pairs[0];
|
|
257
|
+
invariant3__default.default(
|
|
258
|
+
pairs.every((pair) => pair.chainId === chainId),
|
|
259
|
+
"CHAIN_IDS"
|
|
260
|
+
);
|
|
261
|
+
const wrappedInput = input.wrapped;
|
|
262
|
+
invariant3__default.default(pairs[0].involvesToken(wrappedInput), "INPUT");
|
|
263
|
+
invariant3__default.default(typeof output === "undefined" || pairs[pairs.length - 1].involvesToken(output.wrapped), "OUTPUT");
|
|
264
|
+
const path = [wrappedInput];
|
|
265
|
+
for (const [i, pair] of pairs.entries()) {
|
|
266
|
+
const currentInput = path[i];
|
|
267
|
+
invariant3__default.default(currentInput.equals(pair.token0) || currentInput.equals(pair.token1), "PATH");
|
|
268
|
+
const output2 = currentInput.equals(pair.token0) ? pair.token1 : pair.token0;
|
|
269
|
+
path.push(output2);
|
|
270
|
+
}
|
|
271
|
+
this.pairs = pairs;
|
|
272
|
+
this.path = path;
|
|
273
|
+
this.input = input;
|
|
274
|
+
this.output = output;
|
|
275
|
+
}
|
|
276
|
+
get midPrice() {
|
|
277
|
+
if (this._midPrice !== null)
|
|
278
|
+
return this._midPrice;
|
|
279
|
+
const prices = [];
|
|
280
|
+
for (const [i, pair] of this.pairs.entries()) {
|
|
281
|
+
prices.push(
|
|
282
|
+
this.path[i].equals(pair.token0) ? new swapSdkCore.Price(pair.reserve0.currency, pair.reserve1.currency, pair.reserve0.quotient, pair.reserve1.quotient) : new swapSdkCore.Price(pair.reserve1.currency, pair.reserve0.currency, pair.reserve1.quotient, pair.reserve0.quotient)
|
|
283
|
+
);
|
|
284
|
+
}
|
|
285
|
+
const reduced = prices.slice(1).reduce((accumulator, currentValue) => accumulator.multiply(currentValue), prices[0]);
|
|
286
|
+
return this._midPrice = new swapSdkCore.Price(this.input, this.output, reduced.denominator, reduced.numerator);
|
|
287
|
+
}
|
|
288
|
+
get chainId() {
|
|
289
|
+
return this.pairs[0].chainId;
|
|
290
|
+
}
|
|
291
|
+
};
|
|
292
|
+
function inputOutputComparator(a, b) {
|
|
293
|
+
invariant3__default.default(a.inputAmount.currency.equals(b.inputAmount.currency), "INPUT_CURRENCY");
|
|
294
|
+
invariant3__default.default(a.outputAmount.currency.equals(b.outputAmount.currency), "OUTPUT_CURRENCY");
|
|
295
|
+
if (a.outputAmount.equalTo(b.outputAmount)) {
|
|
296
|
+
if (a.inputAmount.equalTo(b.inputAmount)) {
|
|
297
|
+
return 0;
|
|
298
|
+
}
|
|
299
|
+
if (a.inputAmount.lessThan(b.inputAmount)) {
|
|
300
|
+
return -1;
|
|
301
|
+
}
|
|
302
|
+
return 1;
|
|
303
|
+
}
|
|
304
|
+
if (a.outputAmount.lessThan(b.outputAmount)) {
|
|
305
|
+
return 1;
|
|
306
|
+
}
|
|
307
|
+
return -1;
|
|
308
|
+
}
|
|
309
|
+
function tradeComparator(a, b) {
|
|
310
|
+
const ioComp = inputOutputComparator(a, b);
|
|
311
|
+
if (ioComp !== 0) {
|
|
312
|
+
return ioComp;
|
|
313
|
+
}
|
|
314
|
+
if (a.priceImpact.lessThan(b.priceImpact)) {
|
|
315
|
+
return -1;
|
|
316
|
+
}
|
|
317
|
+
if (a.priceImpact.greaterThan(b.priceImpact)) {
|
|
318
|
+
return 1;
|
|
319
|
+
}
|
|
320
|
+
return a.route.path.length - b.route.path.length;
|
|
321
|
+
}
|
|
322
|
+
var Trade = class {
|
|
323
|
+
/**
|
|
324
|
+
* Constructs an exact in trade with the given amount in and route
|
|
325
|
+
* @param route route of the exact in trade
|
|
326
|
+
* @param amountIn the amount being passed in
|
|
327
|
+
*/
|
|
328
|
+
static exactIn(route, amountIn) {
|
|
329
|
+
return new Trade(route, amountIn, swapSdkCore.TradeType.EXACT_INPUT);
|
|
330
|
+
}
|
|
331
|
+
/**
|
|
332
|
+
* Constructs an exact out trade with the given amount out and route
|
|
333
|
+
* @param route route of the exact out trade
|
|
334
|
+
* @param amountOut the amount returned by the trade
|
|
335
|
+
*/
|
|
336
|
+
static exactOut(route, amountOut) {
|
|
337
|
+
return new Trade(route, amountOut, swapSdkCore.TradeType.EXACT_OUTPUT);
|
|
338
|
+
}
|
|
339
|
+
constructor(route, amount, tradeType) {
|
|
340
|
+
this.route = route;
|
|
341
|
+
this.tradeType = tradeType;
|
|
342
|
+
const tokenAmounts = new Array(route.path.length);
|
|
343
|
+
if (tradeType === swapSdkCore.TradeType.EXACT_INPUT) {
|
|
344
|
+
invariant3__default.default(amount.currency.equals(route.input), "INPUT");
|
|
345
|
+
tokenAmounts[0] = amount.wrapped;
|
|
346
|
+
for (let i = 0; i < route.path.length - 1; i++) {
|
|
347
|
+
const pair = route.pairs[i];
|
|
348
|
+
const [outputAmount] = pair.getOutputAmount(tokenAmounts[i]);
|
|
349
|
+
tokenAmounts[i + 1] = outputAmount;
|
|
350
|
+
}
|
|
351
|
+
this.inputAmount = swapSdkCore.CurrencyAmount.fromFractionalAmount(route.input, amount.numerator, amount.denominator);
|
|
352
|
+
this.outputAmount = swapSdkCore.CurrencyAmount.fromFractionalAmount(
|
|
353
|
+
route.output,
|
|
354
|
+
tokenAmounts[tokenAmounts.length - 1].numerator,
|
|
355
|
+
tokenAmounts[tokenAmounts.length - 1].denominator
|
|
356
|
+
);
|
|
357
|
+
} else {
|
|
358
|
+
invariant3__default.default(amount.currency.equals(route.output), "OUTPUT");
|
|
359
|
+
tokenAmounts[tokenAmounts.length - 1] = amount.wrapped;
|
|
360
|
+
for (let i = route.path.length - 1; i > 0; i--) {
|
|
361
|
+
const pair = route.pairs[i - 1];
|
|
362
|
+
const [inputAmount] = pair.getInputAmount(tokenAmounts[i]);
|
|
363
|
+
tokenAmounts[i - 1] = inputAmount;
|
|
364
|
+
}
|
|
365
|
+
this.inputAmount = swapSdkCore.CurrencyAmount.fromFractionalAmount(
|
|
366
|
+
route.input,
|
|
367
|
+
tokenAmounts[0].numerator,
|
|
368
|
+
tokenAmounts[0].denominator
|
|
369
|
+
);
|
|
370
|
+
this.outputAmount = swapSdkCore.CurrencyAmount.fromFractionalAmount(route.output, amount.numerator, amount.denominator);
|
|
371
|
+
}
|
|
372
|
+
this.executionPrice = new swapSdkCore.Price(
|
|
373
|
+
this.inputAmount.currency,
|
|
374
|
+
this.outputAmount.currency,
|
|
375
|
+
this.inputAmount.quotient,
|
|
376
|
+
this.outputAmount.quotient
|
|
377
|
+
);
|
|
378
|
+
this.priceImpact = swapSdkCore.computePriceImpact(route.midPrice, this.inputAmount, this.outputAmount);
|
|
379
|
+
}
|
|
380
|
+
/**
|
|
381
|
+
* Get the minimum amount that must be received from this trade for the given slippage tolerance
|
|
382
|
+
* @param slippageTolerance tolerance of unfavorable slippage from the execution price of this trade
|
|
383
|
+
*/
|
|
384
|
+
minimumAmountOut(slippageTolerance) {
|
|
385
|
+
invariant3__default.default(!slippageTolerance.lessThan(swapSdkCore.ZERO), "SLIPPAGE_TOLERANCE");
|
|
386
|
+
if (this.tradeType === swapSdkCore.TradeType.EXACT_OUTPUT) {
|
|
387
|
+
return this.outputAmount;
|
|
388
|
+
}
|
|
389
|
+
const slippageAdjustedAmountOut = new swapSdkCore.Fraction(swapSdkCore.ONE).add(slippageTolerance).invert().multiply(this.outputAmount.quotient).quotient;
|
|
390
|
+
return swapSdkCore.CurrencyAmount.fromRawAmount(this.outputAmount.currency, slippageAdjustedAmountOut);
|
|
391
|
+
}
|
|
392
|
+
/**
|
|
393
|
+
* Get the maximum amount in that can be spent via this trade for the given slippage tolerance
|
|
394
|
+
* @param slippageTolerance tolerance of unfavorable slippage from the execution price of this trade
|
|
395
|
+
*/
|
|
396
|
+
maximumAmountIn(slippageTolerance) {
|
|
397
|
+
invariant3__default.default(!slippageTolerance.lessThan(swapSdkCore.ZERO), "SLIPPAGE_TOLERANCE");
|
|
398
|
+
if (this.tradeType === swapSdkCore.TradeType.EXACT_INPUT) {
|
|
399
|
+
return this.inputAmount;
|
|
400
|
+
}
|
|
401
|
+
const slippageAdjustedAmountIn = new swapSdkCore.Fraction(swapSdkCore.ONE).add(slippageTolerance).multiply(this.inputAmount.quotient).quotient;
|
|
402
|
+
return swapSdkCore.CurrencyAmount.fromRawAmount(this.inputAmount.currency, slippageAdjustedAmountIn);
|
|
403
|
+
}
|
|
404
|
+
/**
|
|
405
|
+
* Given a list of pairs, and a fixed amount in, returns the top `maxNumResults` trades that go from an input token
|
|
406
|
+
* amount to an output token, making at most `maxHops` hops.
|
|
407
|
+
* Note this does not consider aggregation, as routes are linear. It's possible a better route exists by splitting
|
|
408
|
+
* the amount in among multiple routes.
|
|
409
|
+
* @param pairs the pairs to consider in finding the best trade
|
|
410
|
+
* @param nextAmountIn exact amount of input currency to spend
|
|
411
|
+
* @param currencyOut the desired currency out
|
|
412
|
+
* @param maxNumResults maximum number of results to return
|
|
413
|
+
* @param maxHops maximum number of hops a returned trade can make, e.g. 1 hop goes through a single pair
|
|
414
|
+
* @param currentPairs used in recursion; the current list of pairs
|
|
415
|
+
* @param currencyAmountIn used in recursion; the original value of the currencyAmountIn parameter
|
|
416
|
+
* @param bestTrades used in recursion; the current list of best trades
|
|
417
|
+
*/
|
|
418
|
+
static bestTradeExactIn(pairs, currencyAmountIn, currencyOut, { maxNumResults = 3, maxHops = 3 } = {}, currentPairs = [], nextAmountIn = currencyAmountIn, bestTrades = []) {
|
|
419
|
+
invariant3__default.default(pairs.length > 0, "PAIRS");
|
|
420
|
+
invariant3__default.default(maxHops > 0, "MAX_HOPS");
|
|
421
|
+
invariant3__default.default(currencyAmountIn === nextAmountIn || currentPairs.length > 0, "INVALID_RECURSION");
|
|
422
|
+
const amountIn = nextAmountIn.wrapped;
|
|
423
|
+
const tokenOut = currencyOut.wrapped;
|
|
424
|
+
for (let i = 0; i < pairs.length; i++) {
|
|
425
|
+
const pair = pairs[i];
|
|
426
|
+
if (!pair.token0.equals(amountIn.currency) && !pair.token1.equals(amountIn.currency))
|
|
427
|
+
continue;
|
|
428
|
+
if (pair.reserve0.equalTo(swapSdkCore.ZERO) || pair.reserve1.equalTo(swapSdkCore.ZERO))
|
|
429
|
+
continue;
|
|
430
|
+
let amountOut;
|
|
431
|
+
try {
|
|
432
|
+
;
|
|
433
|
+
[amountOut] = pair.getOutputAmount(amountIn);
|
|
434
|
+
} catch (error) {
|
|
435
|
+
if (error.isInsufficientInputAmountError) {
|
|
436
|
+
continue;
|
|
437
|
+
}
|
|
438
|
+
throw error;
|
|
439
|
+
}
|
|
440
|
+
if (amountOut.currency.equals(tokenOut)) {
|
|
441
|
+
swapSdkCore.sortedInsert(
|
|
442
|
+
bestTrades,
|
|
443
|
+
new Trade(
|
|
444
|
+
new Route([...currentPairs, pair], currencyAmountIn.currency, currencyOut),
|
|
445
|
+
currencyAmountIn,
|
|
446
|
+
swapSdkCore.TradeType.EXACT_INPUT
|
|
447
|
+
),
|
|
448
|
+
maxNumResults,
|
|
449
|
+
tradeComparator
|
|
450
|
+
);
|
|
451
|
+
} else if (maxHops > 1 && pairs.length > 1) {
|
|
452
|
+
const pairsExcludingThisPair = pairs.slice(0, i).concat(pairs.slice(i + 1, pairs.length));
|
|
453
|
+
Trade.bestTradeExactIn(
|
|
454
|
+
pairsExcludingThisPair,
|
|
455
|
+
currencyAmountIn,
|
|
456
|
+
currencyOut,
|
|
457
|
+
{
|
|
458
|
+
maxNumResults,
|
|
459
|
+
maxHops: maxHops - 1
|
|
460
|
+
},
|
|
461
|
+
[...currentPairs, pair],
|
|
462
|
+
amountOut,
|
|
463
|
+
bestTrades
|
|
464
|
+
);
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
return bestTrades;
|
|
468
|
+
}
|
|
469
|
+
/**
|
|
470
|
+
* Return the execution price after accounting for slippage tolerance
|
|
471
|
+
* @param slippageTolerance the allowed tolerated slippage
|
|
472
|
+
*/
|
|
473
|
+
worstExecutionPrice(slippageTolerance) {
|
|
474
|
+
return new swapSdkCore.Price(
|
|
475
|
+
this.inputAmount.currency,
|
|
476
|
+
this.outputAmount.currency,
|
|
477
|
+
this.maximumAmountIn(slippageTolerance).quotient,
|
|
478
|
+
this.minimumAmountOut(slippageTolerance).quotient
|
|
479
|
+
);
|
|
480
|
+
}
|
|
481
|
+
/**
|
|
482
|
+
* similar to the above method but instead targets a fixed output amount
|
|
483
|
+
* given a list of pairs, and a fixed amount out, returns the top `maxNumResults` trades that go from an input token
|
|
484
|
+
* to an output token amount, making at most `maxHops` hops
|
|
485
|
+
* note this does not consider aggregation, as routes are linear. it's possible a better route exists by splitting
|
|
486
|
+
* the amount in among multiple routes.
|
|
487
|
+
* @param pairs the pairs to consider in finding the best trade
|
|
488
|
+
* @param currencyIn the currency to spend
|
|
489
|
+
* @param nextAmountOut the exact amount of currency out
|
|
490
|
+
* @param maxNumResults maximum number of results to return
|
|
491
|
+
* @param maxHops maximum number of hops a returned trade can make, e.g. 1 hop goes through a single pair
|
|
492
|
+
* @param currentPairs used in recursion; the current list of pairs
|
|
493
|
+
* @param currencyAmountOut used in recursion; the original value of the currencyAmountOut parameter
|
|
494
|
+
* @param bestTrades used in recursion; the current list of best trades
|
|
495
|
+
*/
|
|
496
|
+
static bestTradeExactOut(pairs, currencyIn, currencyAmountOut, { maxNumResults = 3, maxHops = 3 } = {}, currentPairs = [], nextAmountOut = currencyAmountOut, bestTrades = []) {
|
|
497
|
+
invariant3__default.default(pairs.length > 0, "PAIRS");
|
|
498
|
+
invariant3__default.default(maxHops > 0, "MAX_HOPS");
|
|
499
|
+
invariant3__default.default(currencyAmountOut === nextAmountOut || currentPairs.length > 0, "INVALID_RECURSION");
|
|
500
|
+
const amountOut = nextAmountOut.wrapped;
|
|
501
|
+
const tokenIn = currencyIn.wrapped;
|
|
502
|
+
for (let i = 0; i < pairs.length; i++) {
|
|
503
|
+
const pair = pairs[i];
|
|
504
|
+
if (!pair.token0.equals(amountOut.currency) && !pair.token1.equals(amountOut.currency))
|
|
505
|
+
continue;
|
|
506
|
+
if (pair.reserve0.equalTo(swapSdkCore.ZERO) || pair.reserve1.equalTo(swapSdkCore.ZERO))
|
|
507
|
+
continue;
|
|
508
|
+
let amountIn;
|
|
509
|
+
try {
|
|
510
|
+
;
|
|
511
|
+
[amountIn] = pair.getInputAmount(amountOut);
|
|
512
|
+
} catch (error) {
|
|
513
|
+
if (error.isInsufficientReservesError) {
|
|
514
|
+
continue;
|
|
515
|
+
}
|
|
516
|
+
throw error;
|
|
517
|
+
}
|
|
518
|
+
if (amountIn.currency.equals(tokenIn)) {
|
|
519
|
+
swapSdkCore.sortedInsert(
|
|
520
|
+
bestTrades,
|
|
521
|
+
new Trade(
|
|
522
|
+
new Route([pair, ...currentPairs], currencyIn, currencyAmountOut.currency),
|
|
523
|
+
currencyAmountOut,
|
|
524
|
+
swapSdkCore.TradeType.EXACT_OUTPUT
|
|
525
|
+
),
|
|
526
|
+
maxNumResults,
|
|
527
|
+
tradeComparator
|
|
528
|
+
);
|
|
529
|
+
} else if (maxHops > 1 && pairs.length > 1) {
|
|
530
|
+
const pairsExcludingThisPair = pairs.slice(0, i).concat(pairs.slice(i + 1, pairs.length));
|
|
531
|
+
Trade.bestTradeExactOut(
|
|
532
|
+
pairsExcludingThisPair,
|
|
533
|
+
currencyIn,
|
|
534
|
+
currencyAmountOut,
|
|
535
|
+
{
|
|
536
|
+
maxNumResults,
|
|
537
|
+
maxHops: maxHops - 1
|
|
538
|
+
},
|
|
539
|
+
[pair, ...currentPairs],
|
|
540
|
+
amountIn,
|
|
541
|
+
bestTrades
|
|
542
|
+
);
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
return bestTrades;
|
|
546
|
+
}
|
|
547
|
+
};
|
|
548
|
+
|
|
549
|
+
// src/abis/IPancakePair.ts
|
|
550
|
+
var pancakePairV2ABI = [
|
|
551
|
+
{
|
|
552
|
+
anonymous: false,
|
|
553
|
+
inputs: [
|
|
554
|
+
{
|
|
555
|
+
indexed: true,
|
|
556
|
+
internalType: "address",
|
|
557
|
+
name: "owner",
|
|
558
|
+
type: "address"
|
|
559
|
+
},
|
|
560
|
+
{
|
|
561
|
+
indexed: true,
|
|
562
|
+
internalType: "address",
|
|
563
|
+
name: "spender",
|
|
564
|
+
type: "address"
|
|
565
|
+
},
|
|
566
|
+
{
|
|
567
|
+
indexed: false,
|
|
568
|
+
internalType: "uint256",
|
|
569
|
+
name: "value",
|
|
570
|
+
type: "uint256"
|
|
571
|
+
}
|
|
572
|
+
],
|
|
573
|
+
name: "Approval",
|
|
574
|
+
type: "event"
|
|
575
|
+
},
|
|
576
|
+
{
|
|
577
|
+
anonymous: false,
|
|
578
|
+
inputs: [
|
|
579
|
+
{
|
|
580
|
+
indexed: true,
|
|
581
|
+
internalType: "address",
|
|
582
|
+
name: "sender",
|
|
583
|
+
type: "address"
|
|
584
|
+
},
|
|
585
|
+
{
|
|
586
|
+
indexed: false,
|
|
587
|
+
internalType: "uint256",
|
|
588
|
+
name: "amount0",
|
|
589
|
+
type: "uint256"
|
|
590
|
+
},
|
|
591
|
+
{
|
|
592
|
+
indexed: false,
|
|
593
|
+
internalType: "uint256",
|
|
594
|
+
name: "amount1",
|
|
595
|
+
type: "uint256"
|
|
596
|
+
},
|
|
597
|
+
{
|
|
598
|
+
indexed: true,
|
|
599
|
+
internalType: "address",
|
|
600
|
+
name: "to",
|
|
601
|
+
type: "address"
|
|
602
|
+
}
|
|
603
|
+
],
|
|
604
|
+
name: "Burn",
|
|
605
|
+
type: "event"
|
|
606
|
+
},
|
|
607
|
+
{
|
|
608
|
+
anonymous: false,
|
|
609
|
+
inputs: [
|
|
610
|
+
{
|
|
611
|
+
indexed: true,
|
|
612
|
+
internalType: "address",
|
|
613
|
+
name: "sender",
|
|
614
|
+
type: "address"
|
|
615
|
+
},
|
|
616
|
+
{
|
|
617
|
+
indexed: false,
|
|
618
|
+
internalType: "uint256",
|
|
619
|
+
name: "amount0",
|
|
620
|
+
type: "uint256"
|
|
621
|
+
},
|
|
622
|
+
{
|
|
623
|
+
indexed: false,
|
|
624
|
+
internalType: "uint256",
|
|
625
|
+
name: "amount1",
|
|
626
|
+
type: "uint256"
|
|
627
|
+
}
|
|
628
|
+
],
|
|
629
|
+
name: "Mint",
|
|
630
|
+
type: "event"
|
|
631
|
+
},
|
|
632
|
+
{
|
|
633
|
+
anonymous: false,
|
|
634
|
+
inputs: [
|
|
635
|
+
{
|
|
636
|
+
indexed: true,
|
|
637
|
+
internalType: "address",
|
|
638
|
+
name: "sender",
|
|
639
|
+
type: "address"
|
|
640
|
+
},
|
|
641
|
+
{
|
|
642
|
+
indexed: false,
|
|
643
|
+
internalType: "uint256",
|
|
644
|
+
name: "amount0In",
|
|
645
|
+
type: "uint256"
|
|
646
|
+
},
|
|
647
|
+
{
|
|
648
|
+
indexed: false,
|
|
649
|
+
internalType: "uint256",
|
|
650
|
+
name: "amount1In",
|
|
651
|
+
type: "uint256"
|
|
652
|
+
},
|
|
653
|
+
{
|
|
654
|
+
indexed: false,
|
|
655
|
+
internalType: "uint256",
|
|
656
|
+
name: "amount0Out",
|
|
657
|
+
type: "uint256"
|
|
658
|
+
},
|
|
659
|
+
{
|
|
660
|
+
indexed: false,
|
|
661
|
+
internalType: "uint256",
|
|
662
|
+
name: "amount1Out",
|
|
663
|
+
type: "uint256"
|
|
664
|
+
},
|
|
665
|
+
{
|
|
666
|
+
indexed: true,
|
|
667
|
+
internalType: "address",
|
|
668
|
+
name: "to",
|
|
669
|
+
type: "address"
|
|
670
|
+
}
|
|
671
|
+
],
|
|
672
|
+
name: "Swap",
|
|
673
|
+
type: "event"
|
|
674
|
+
},
|
|
675
|
+
{
|
|
676
|
+
anonymous: false,
|
|
677
|
+
inputs: [
|
|
678
|
+
{
|
|
679
|
+
indexed: false,
|
|
680
|
+
internalType: "uint112",
|
|
681
|
+
name: "reserve0",
|
|
682
|
+
type: "uint112"
|
|
683
|
+
},
|
|
684
|
+
{
|
|
685
|
+
indexed: false,
|
|
686
|
+
internalType: "uint112",
|
|
687
|
+
name: "reserve1",
|
|
688
|
+
type: "uint112"
|
|
689
|
+
}
|
|
690
|
+
],
|
|
691
|
+
name: "Sync",
|
|
692
|
+
type: "event"
|
|
693
|
+
},
|
|
694
|
+
{
|
|
695
|
+
anonymous: false,
|
|
696
|
+
inputs: [
|
|
697
|
+
{
|
|
698
|
+
indexed: true,
|
|
699
|
+
internalType: "address",
|
|
700
|
+
name: "from",
|
|
701
|
+
type: "address"
|
|
702
|
+
},
|
|
703
|
+
{
|
|
704
|
+
indexed: true,
|
|
705
|
+
internalType: "address",
|
|
706
|
+
name: "to",
|
|
707
|
+
type: "address"
|
|
708
|
+
},
|
|
709
|
+
{
|
|
710
|
+
indexed: false,
|
|
711
|
+
internalType: "uint256",
|
|
712
|
+
name: "value",
|
|
713
|
+
type: "uint256"
|
|
714
|
+
}
|
|
715
|
+
],
|
|
716
|
+
name: "Transfer",
|
|
717
|
+
type: "event"
|
|
718
|
+
},
|
|
719
|
+
{
|
|
720
|
+
constant: true,
|
|
721
|
+
inputs: [],
|
|
722
|
+
name: "DOMAIN_SEPARATOR",
|
|
723
|
+
outputs: [
|
|
724
|
+
{
|
|
725
|
+
internalType: "bytes32",
|
|
726
|
+
name: "",
|
|
727
|
+
type: "bytes32"
|
|
728
|
+
}
|
|
729
|
+
],
|
|
730
|
+
payable: false,
|
|
731
|
+
stateMutability: "view",
|
|
732
|
+
type: "function"
|
|
733
|
+
},
|
|
734
|
+
{
|
|
735
|
+
constant: true,
|
|
736
|
+
inputs: [],
|
|
737
|
+
name: "MINIMUM_LIQUIDITY",
|
|
738
|
+
outputs: [
|
|
739
|
+
{
|
|
740
|
+
internalType: "uint256",
|
|
741
|
+
name: "",
|
|
742
|
+
type: "uint256"
|
|
743
|
+
}
|
|
744
|
+
],
|
|
745
|
+
payable: false,
|
|
746
|
+
stateMutability: "pure",
|
|
747
|
+
type: "function"
|
|
748
|
+
},
|
|
749
|
+
{
|
|
750
|
+
constant: true,
|
|
751
|
+
inputs: [],
|
|
752
|
+
name: "PERMIT_TYPEHASH",
|
|
753
|
+
outputs: [
|
|
754
|
+
{
|
|
755
|
+
internalType: "bytes32",
|
|
756
|
+
name: "",
|
|
757
|
+
type: "bytes32"
|
|
758
|
+
}
|
|
759
|
+
],
|
|
760
|
+
payable: false,
|
|
761
|
+
stateMutability: "pure",
|
|
762
|
+
type: "function"
|
|
763
|
+
},
|
|
764
|
+
{
|
|
765
|
+
constant: true,
|
|
766
|
+
inputs: [
|
|
767
|
+
{
|
|
768
|
+
internalType: "address",
|
|
769
|
+
name: "owner",
|
|
770
|
+
type: "address"
|
|
771
|
+
},
|
|
772
|
+
{
|
|
773
|
+
internalType: "address",
|
|
774
|
+
name: "spender",
|
|
775
|
+
type: "address"
|
|
776
|
+
}
|
|
777
|
+
],
|
|
778
|
+
name: "allowance",
|
|
779
|
+
outputs: [
|
|
780
|
+
{
|
|
781
|
+
internalType: "uint256",
|
|
782
|
+
name: "",
|
|
783
|
+
type: "uint256"
|
|
784
|
+
}
|
|
785
|
+
],
|
|
786
|
+
payable: false,
|
|
787
|
+
stateMutability: "view",
|
|
788
|
+
type: "function"
|
|
789
|
+
},
|
|
790
|
+
{
|
|
791
|
+
constant: false,
|
|
792
|
+
inputs: [
|
|
793
|
+
{
|
|
794
|
+
internalType: "address",
|
|
795
|
+
name: "spender",
|
|
796
|
+
type: "address"
|
|
797
|
+
},
|
|
798
|
+
{
|
|
799
|
+
internalType: "uint256",
|
|
800
|
+
name: "value",
|
|
801
|
+
type: "uint256"
|
|
802
|
+
}
|
|
803
|
+
],
|
|
804
|
+
name: "approve",
|
|
805
|
+
outputs: [
|
|
806
|
+
{
|
|
807
|
+
internalType: "bool",
|
|
808
|
+
name: "",
|
|
809
|
+
type: "bool"
|
|
810
|
+
}
|
|
811
|
+
],
|
|
812
|
+
payable: false,
|
|
813
|
+
stateMutability: "nonpayable",
|
|
814
|
+
type: "function"
|
|
815
|
+
},
|
|
816
|
+
{
|
|
817
|
+
constant: true,
|
|
818
|
+
inputs: [
|
|
819
|
+
{
|
|
820
|
+
internalType: "address",
|
|
821
|
+
name: "owner",
|
|
822
|
+
type: "address"
|
|
823
|
+
}
|
|
824
|
+
],
|
|
825
|
+
name: "balanceOf",
|
|
826
|
+
outputs: [
|
|
827
|
+
{
|
|
828
|
+
internalType: "uint256",
|
|
829
|
+
name: "",
|
|
830
|
+
type: "uint256"
|
|
831
|
+
}
|
|
832
|
+
],
|
|
833
|
+
payable: false,
|
|
834
|
+
stateMutability: "view",
|
|
835
|
+
type: "function"
|
|
836
|
+
},
|
|
837
|
+
{
|
|
838
|
+
constant: false,
|
|
839
|
+
inputs: [
|
|
840
|
+
{
|
|
841
|
+
internalType: "address",
|
|
842
|
+
name: "to",
|
|
843
|
+
type: "address"
|
|
844
|
+
}
|
|
845
|
+
],
|
|
846
|
+
name: "burn",
|
|
847
|
+
outputs: [
|
|
848
|
+
{
|
|
849
|
+
internalType: "uint256",
|
|
850
|
+
name: "amount0",
|
|
851
|
+
type: "uint256"
|
|
852
|
+
},
|
|
853
|
+
{
|
|
854
|
+
internalType: "uint256",
|
|
855
|
+
name: "amount1",
|
|
856
|
+
type: "uint256"
|
|
857
|
+
}
|
|
858
|
+
],
|
|
859
|
+
payable: false,
|
|
860
|
+
stateMutability: "nonpayable",
|
|
861
|
+
type: "function"
|
|
862
|
+
},
|
|
863
|
+
{
|
|
864
|
+
constant: true,
|
|
865
|
+
inputs: [],
|
|
866
|
+
name: "decimals",
|
|
867
|
+
outputs: [
|
|
868
|
+
{
|
|
869
|
+
internalType: "uint8",
|
|
870
|
+
name: "",
|
|
871
|
+
type: "uint8"
|
|
872
|
+
}
|
|
873
|
+
],
|
|
874
|
+
payable: false,
|
|
875
|
+
stateMutability: "pure",
|
|
876
|
+
type: "function"
|
|
877
|
+
},
|
|
878
|
+
{
|
|
879
|
+
constant: true,
|
|
880
|
+
inputs: [],
|
|
881
|
+
name: "factory",
|
|
882
|
+
outputs: [
|
|
883
|
+
{
|
|
884
|
+
internalType: "address",
|
|
885
|
+
name: "",
|
|
886
|
+
type: "address"
|
|
887
|
+
}
|
|
888
|
+
],
|
|
889
|
+
payable: false,
|
|
890
|
+
stateMutability: "view",
|
|
891
|
+
type: "function"
|
|
892
|
+
},
|
|
893
|
+
{
|
|
894
|
+
constant: true,
|
|
895
|
+
inputs: [],
|
|
896
|
+
name: "getReserves",
|
|
897
|
+
outputs: [
|
|
898
|
+
{
|
|
899
|
+
internalType: "uint112",
|
|
900
|
+
name: "reserve0",
|
|
901
|
+
type: "uint112"
|
|
902
|
+
},
|
|
903
|
+
{
|
|
904
|
+
internalType: "uint112",
|
|
905
|
+
name: "reserve1",
|
|
906
|
+
type: "uint112"
|
|
907
|
+
},
|
|
908
|
+
{
|
|
909
|
+
internalType: "uint32",
|
|
910
|
+
name: "blockTimestampLast",
|
|
911
|
+
type: "uint32"
|
|
912
|
+
}
|
|
913
|
+
],
|
|
914
|
+
payable: false,
|
|
915
|
+
stateMutability: "view",
|
|
916
|
+
type: "function"
|
|
917
|
+
},
|
|
918
|
+
{
|
|
919
|
+
constant: false,
|
|
920
|
+
inputs: [
|
|
921
|
+
{
|
|
922
|
+
internalType: "address",
|
|
923
|
+
name: "",
|
|
924
|
+
type: "address"
|
|
925
|
+
},
|
|
926
|
+
{
|
|
927
|
+
internalType: "address",
|
|
928
|
+
name: "",
|
|
929
|
+
type: "address"
|
|
930
|
+
}
|
|
931
|
+
],
|
|
932
|
+
name: "initialize",
|
|
933
|
+
outputs: [],
|
|
934
|
+
payable: false,
|
|
935
|
+
stateMutability: "nonpayable",
|
|
936
|
+
type: "function"
|
|
937
|
+
},
|
|
938
|
+
{
|
|
939
|
+
constant: true,
|
|
940
|
+
inputs: [],
|
|
941
|
+
name: "kLast",
|
|
942
|
+
outputs: [
|
|
943
|
+
{
|
|
944
|
+
internalType: "uint256",
|
|
945
|
+
name: "",
|
|
946
|
+
type: "uint256"
|
|
947
|
+
}
|
|
948
|
+
],
|
|
949
|
+
payable: false,
|
|
950
|
+
stateMutability: "view",
|
|
951
|
+
type: "function"
|
|
952
|
+
},
|
|
953
|
+
{
|
|
954
|
+
constant: false,
|
|
955
|
+
inputs: [
|
|
956
|
+
{
|
|
957
|
+
internalType: "address",
|
|
958
|
+
name: "to",
|
|
959
|
+
type: "address"
|
|
960
|
+
}
|
|
961
|
+
],
|
|
962
|
+
name: "mint",
|
|
963
|
+
outputs: [
|
|
964
|
+
{
|
|
965
|
+
internalType: "uint256",
|
|
966
|
+
name: "liquidity",
|
|
967
|
+
type: "uint256"
|
|
968
|
+
}
|
|
969
|
+
],
|
|
970
|
+
payable: false,
|
|
971
|
+
stateMutability: "nonpayable",
|
|
972
|
+
type: "function"
|
|
973
|
+
},
|
|
974
|
+
{
|
|
975
|
+
constant: true,
|
|
976
|
+
inputs: [],
|
|
977
|
+
name: "name",
|
|
978
|
+
outputs: [
|
|
979
|
+
{
|
|
980
|
+
internalType: "string",
|
|
981
|
+
name: "",
|
|
982
|
+
type: "string"
|
|
983
|
+
}
|
|
984
|
+
],
|
|
985
|
+
payable: false,
|
|
986
|
+
stateMutability: "pure",
|
|
987
|
+
type: "function"
|
|
988
|
+
},
|
|
989
|
+
{
|
|
990
|
+
constant: true,
|
|
991
|
+
inputs: [
|
|
992
|
+
{
|
|
993
|
+
internalType: "address",
|
|
994
|
+
name: "owner",
|
|
995
|
+
type: "address"
|
|
996
|
+
}
|
|
997
|
+
],
|
|
998
|
+
name: "nonces",
|
|
999
|
+
outputs: [
|
|
1000
|
+
{
|
|
1001
|
+
internalType: "uint256",
|
|
1002
|
+
name: "",
|
|
1003
|
+
type: "uint256"
|
|
1004
|
+
}
|
|
1005
|
+
],
|
|
1006
|
+
payable: false,
|
|
1007
|
+
stateMutability: "view",
|
|
1008
|
+
type: "function"
|
|
1009
|
+
},
|
|
1010
|
+
{
|
|
1011
|
+
constant: false,
|
|
1012
|
+
inputs: [
|
|
1013
|
+
{
|
|
1014
|
+
internalType: "address",
|
|
1015
|
+
name: "owner",
|
|
1016
|
+
type: "address"
|
|
1017
|
+
},
|
|
1018
|
+
{
|
|
1019
|
+
internalType: "address",
|
|
1020
|
+
name: "spender",
|
|
1021
|
+
type: "address"
|
|
1022
|
+
},
|
|
1023
|
+
{
|
|
1024
|
+
internalType: "uint256",
|
|
1025
|
+
name: "value",
|
|
1026
|
+
type: "uint256"
|
|
1027
|
+
},
|
|
1028
|
+
{
|
|
1029
|
+
internalType: "uint256",
|
|
1030
|
+
name: "deadline",
|
|
1031
|
+
type: "uint256"
|
|
1032
|
+
},
|
|
1033
|
+
{
|
|
1034
|
+
internalType: "uint8",
|
|
1035
|
+
name: "v",
|
|
1036
|
+
type: "uint8"
|
|
1037
|
+
},
|
|
1038
|
+
{
|
|
1039
|
+
internalType: "bytes32",
|
|
1040
|
+
name: "r",
|
|
1041
|
+
type: "bytes32"
|
|
1042
|
+
},
|
|
1043
|
+
{
|
|
1044
|
+
internalType: "bytes32",
|
|
1045
|
+
name: "s",
|
|
1046
|
+
type: "bytes32"
|
|
1047
|
+
}
|
|
1048
|
+
],
|
|
1049
|
+
name: "permit",
|
|
1050
|
+
outputs: [],
|
|
1051
|
+
payable: false,
|
|
1052
|
+
stateMutability: "nonpayable",
|
|
1053
|
+
type: "function"
|
|
1054
|
+
},
|
|
1055
|
+
{
|
|
1056
|
+
constant: true,
|
|
1057
|
+
inputs: [],
|
|
1058
|
+
name: "price0CumulativeLast",
|
|
1059
|
+
outputs: [
|
|
1060
|
+
{
|
|
1061
|
+
internalType: "uint256",
|
|
1062
|
+
name: "",
|
|
1063
|
+
type: "uint256"
|
|
1064
|
+
}
|
|
1065
|
+
],
|
|
1066
|
+
payable: false,
|
|
1067
|
+
stateMutability: "view",
|
|
1068
|
+
type: "function"
|
|
1069
|
+
},
|
|
1070
|
+
{
|
|
1071
|
+
constant: true,
|
|
1072
|
+
inputs: [],
|
|
1073
|
+
name: "price1CumulativeLast",
|
|
1074
|
+
outputs: [
|
|
1075
|
+
{
|
|
1076
|
+
internalType: "uint256",
|
|
1077
|
+
name: "",
|
|
1078
|
+
type: "uint256"
|
|
1079
|
+
}
|
|
1080
|
+
],
|
|
1081
|
+
payable: false,
|
|
1082
|
+
stateMutability: "view",
|
|
1083
|
+
type: "function"
|
|
1084
|
+
},
|
|
1085
|
+
{
|
|
1086
|
+
constant: false,
|
|
1087
|
+
inputs: [
|
|
1088
|
+
{
|
|
1089
|
+
internalType: "address",
|
|
1090
|
+
name: "to",
|
|
1091
|
+
type: "address"
|
|
1092
|
+
}
|
|
1093
|
+
],
|
|
1094
|
+
name: "skim",
|
|
1095
|
+
outputs: [],
|
|
1096
|
+
payable: false,
|
|
1097
|
+
stateMutability: "nonpayable",
|
|
1098
|
+
type: "function"
|
|
1099
|
+
},
|
|
1100
|
+
{
|
|
1101
|
+
constant: false,
|
|
1102
|
+
inputs: [
|
|
1103
|
+
{
|
|
1104
|
+
internalType: "uint256",
|
|
1105
|
+
name: "amount0Out",
|
|
1106
|
+
type: "uint256"
|
|
1107
|
+
},
|
|
1108
|
+
{
|
|
1109
|
+
internalType: "uint256",
|
|
1110
|
+
name: "amount1Out",
|
|
1111
|
+
type: "uint256"
|
|
1112
|
+
},
|
|
1113
|
+
{
|
|
1114
|
+
internalType: "address",
|
|
1115
|
+
name: "to",
|
|
1116
|
+
type: "address"
|
|
1117
|
+
},
|
|
1118
|
+
{
|
|
1119
|
+
internalType: "bytes",
|
|
1120
|
+
name: "data",
|
|
1121
|
+
type: "bytes"
|
|
1122
|
+
}
|
|
1123
|
+
],
|
|
1124
|
+
name: "swap",
|
|
1125
|
+
outputs: [],
|
|
1126
|
+
payable: false,
|
|
1127
|
+
stateMutability: "nonpayable",
|
|
1128
|
+
type: "function"
|
|
1129
|
+
},
|
|
1130
|
+
{
|
|
1131
|
+
constant: true,
|
|
1132
|
+
inputs: [],
|
|
1133
|
+
name: "symbol",
|
|
1134
|
+
outputs: [
|
|
1135
|
+
{
|
|
1136
|
+
internalType: "string",
|
|
1137
|
+
name: "",
|
|
1138
|
+
type: "string"
|
|
1139
|
+
}
|
|
1140
|
+
],
|
|
1141
|
+
payable: false,
|
|
1142
|
+
stateMutability: "pure",
|
|
1143
|
+
type: "function"
|
|
1144
|
+
},
|
|
1145
|
+
{
|
|
1146
|
+
constant: false,
|
|
1147
|
+
inputs: [],
|
|
1148
|
+
name: "sync",
|
|
1149
|
+
outputs: [],
|
|
1150
|
+
payable: false,
|
|
1151
|
+
stateMutability: "nonpayable",
|
|
1152
|
+
type: "function"
|
|
1153
|
+
},
|
|
1154
|
+
{
|
|
1155
|
+
constant: true,
|
|
1156
|
+
inputs: [],
|
|
1157
|
+
name: "token0",
|
|
1158
|
+
outputs: [
|
|
1159
|
+
{
|
|
1160
|
+
internalType: "address",
|
|
1161
|
+
name: "",
|
|
1162
|
+
type: "address"
|
|
1163
|
+
}
|
|
1164
|
+
],
|
|
1165
|
+
payable: false,
|
|
1166
|
+
stateMutability: "view",
|
|
1167
|
+
type: "function"
|
|
1168
|
+
},
|
|
1169
|
+
{
|
|
1170
|
+
constant: true,
|
|
1171
|
+
inputs: [],
|
|
1172
|
+
name: "token1",
|
|
1173
|
+
outputs: [
|
|
1174
|
+
{
|
|
1175
|
+
internalType: "address",
|
|
1176
|
+
name: "",
|
|
1177
|
+
type: "address"
|
|
1178
|
+
}
|
|
1179
|
+
],
|
|
1180
|
+
payable: false,
|
|
1181
|
+
stateMutability: "view",
|
|
1182
|
+
type: "function"
|
|
1183
|
+
},
|
|
1184
|
+
{
|
|
1185
|
+
constant: true,
|
|
1186
|
+
inputs: [],
|
|
1187
|
+
name: "totalSupply",
|
|
1188
|
+
outputs: [
|
|
1189
|
+
{
|
|
1190
|
+
internalType: "uint256",
|
|
1191
|
+
name: "",
|
|
1192
|
+
type: "uint256"
|
|
1193
|
+
}
|
|
1194
|
+
],
|
|
1195
|
+
payable: false,
|
|
1196
|
+
stateMutability: "view",
|
|
1197
|
+
type: "function"
|
|
1198
|
+
},
|
|
1199
|
+
{
|
|
1200
|
+
constant: false,
|
|
1201
|
+
inputs: [
|
|
1202
|
+
{
|
|
1203
|
+
internalType: "address",
|
|
1204
|
+
name: "to",
|
|
1205
|
+
type: "address"
|
|
1206
|
+
},
|
|
1207
|
+
{
|
|
1208
|
+
internalType: "uint256",
|
|
1209
|
+
name: "value",
|
|
1210
|
+
type: "uint256"
|
|
1211
|
+
}
|
|
1212
|
+
],
|
|
1213
|
+
name: "transfer",
|
|
1214
|
+
outputs: [
|
|
1215
|
+
{
|
|
1216
|
+
internalType: "bool",
|
|
1217
|
+
name: "",
|
|
1218
|
+
type: "bool"
|
|
1219
|
+
}
|
|
1220
|
+
],
|
|
1221
|
+
payable: false,
|
|
1222
|
+
stateMutability: "nonpayable",
|
|
1223
|
+
type: "function"
|
|
1224
|
+
},
|
|
1225
|
+
{
|
|
1226
|
+
constant: false,
|
|
1227
|
+
inputs: [
|
|
1228
|
+
{
|
|
1229
|
+
internalType: "address",
|
|
1230
|
+
name: "from",
|
|
1231
|
+
type: "address"
|
|
1232
|
+
},
|
|
1233
|
+
{
|
|
1234
|
+
internalType: "address",
|
|
1235
|
+
name: "to",
|
|
1236
|
+
type: "address"
|
|
1237
|
+
},
|
|
1238
|
+
{
|
|
1239
|
+
internalType: "uint256",
|
|
1240
|
+
name: "value",
|
|
1241
|
+
type: "uint256"
|
|
1242
|
+
}
|
|
1243
|
+
],
|
|
1244
|
+
name: "transferFrom",
|
|
1245
|
+
outputs: [
|
|
1246
|
+
{
|
|
1247
|
+
internalType: "bool",
|
|
1248
|
+
name: "",
|
|
1249
|
+
type: "bool"
|
|
1250
|
+
}
|
|
1251
|
+
],
|
|
1252
|
+
payable: false,
|
|
1253
|
+
stateMutability: "nonpayable",
|
|
1254
|
+
type: "function"
|
|
1255
|
+
}
|
|
1256
|
+
];
|
|
1257
|
+
function toHex(currencyAmount) {
|
|
1258
|
+
return `0x${currencyAmount.quotient.toString(16)}`;
|
|
1259
|
+
}
|
|
1260
|
+
var ZERO_HEX = "0x0";
|
|
1261
|
+
var Router = class {
|
|
1262
|
+
/**
|
|
1263
|
+
* Produces the on-chain method name to call and the hex encoded parameters to pass as arguments for a given trade.
|
|
1264
|
+
* @param trade to produce call parameters for
|
|
1265
|
+
* @param options options for the call parameters
|
|
1266
|
+
*/
|
|
1267
|
+
static swapCallParameters(trade, options) {
|
|
1268
|
+
const etherIn = trade.inputAmount.currency.isNative;
|
|
1269
|
+
const etherOut = trade.outputAmount.currency.isNative;
|
|
1270
|
+
invariant3__default.default(!(etherIn && etherOut), "ETHER_IN_OUT");
|
|
1271
|
+
invariant3__default.default(!("ttl" in options) || options.ttl > 0, "TTL");
|
|
1272
|
+
const to = swapSdkEvm.validateAndParseAddress(options.recipient);
|
|
1273
|
+
const amountIn = toHex(trade.maximumAmountIn(options.allowedSlippage));
|
|
1274
|
+
const amountOut = toHex(trade.minimumAmountOut(options.allowedSlippage));
|
|
1275
|
+
const path = trade.route.path.map((token) => token.address);
|
|
1276
|
+
const deadline = "ttl" in options ? `0x${(Math.floor(( new Date()).getTime() / 1e3) + options.ttl).toString(16)}` : `0x${options.deadline.toString(16)}`;
|
|
1277
|
+
const useFeeOnTransfer = Boolean(options.feeOnTransfer);
|
|
1278
|
+
let methodName;
|
|
1279
|
+
let args;
|
|
1280
|
+
let value;
|
|
1281
|
+
switch (trade.tradeType) {
|
|
1282
|
+
case swapSdkCore.TradeType.EXACT_INPUT:
|
|
1283
|
+
if (etherIn) {
|
|
1284
|
+
methodName = useFeeOnTransfer ? "swapExactETHForTokensSupportingFeeOnTransferTokens" : "swapExactETHForTokens";
|
|
1285
|
+
args = [amountOut, path, to, deadline];
|
|
1286
|
+
value = amountIn;
|
|
1287
|
+
} else if (etherOut) {
|
|
1288
|
+
methodName = useFeeOnTransfer ? "swapExactTokensForETHSupportingFeeOnTransferTokens" : "swapExactTokensForETH";
|
|
1289
|
+
args = [amountIn, amountOut, path, to, deadline];
|
|
1290
|
+
value = ZERO_HEX;
|
|
1291
|
+
} else {
|
|
1292
|
+
methodName = useFeeOnTransfer ? "swapExactTokensForTokensSupportingFeeOnTransferTokens" : "swapExactTokensForTokens";
|
|
1293
|
+
args = [amountIn, amountOut, path, to, deadline];
|
|
1294
|
+
value = ZERO_HEX;
|
|
1295
|
+
}
|
|
1296
|
+
break;
|
|
1297
|
+
case swapSdkCore.TradeType.EXACT_OUTPUT:
|
|
1298
|
+
invariant3__default.default(!useFeeOnTransfer, "EXACT_OUT_FOT");
|
|
1299
|
+
if (etherIn) {
|
|
1300
|
+
methodName = "swapETHForExactTokens";
|
|
1301
|
+
args = [amountOut, path, to, deadline];
|
|
1302
|
+
value = amountIn;
|
|
1303
|
+
} else if (etherOut) {
|
|
1304
|
+
methodName = "swapTokensForExactETH";
|
|
1305
|
+
args = [amountOut, amountIn, path, to, deadline];
|
|
1306
|
+
value = ZERO_HEX;
|
|
1307
|
+
} else {
|
|
1308
|
+
methodName = "swapTokensForExactTokens";
|
|
1309
|
+
args = [amountOut, amountIn, path, to, deadline];
|
|
1310
|
+
value = ZERO_HEX;
|
|
1311
|
+
}
|
|
1312
|
+
break;
|
|
1313
|
+
}
|
|
1314
|
+
return {
|
|
1315
|
+
methodName,
|
|
1316
|
+
args,
|
|
1317
|
+
value
|
|
1318
|
+
};
|
|
1319
|
+
}
|
|
1320
|
+
};
|
|
1321
|
+
var TOKEN_DECIMALS_CACHE = {
|
|
1322
|
+
[chains.ChainId.BSC]: {}
|
|
1323
|
+
};
|
|
1324
|
+
var ethClient = viem.createPublicClient({ chain: chains$1.mainnet, transport: viem.http() });
|
|
1325
|
+
var bscClient = viem.createPublicClient({ chain: chains$1.bsc, transport: viem.http() });
|
|
1326
|
+
var bscTestnetClient = viem.createPublicClient({ chain: chains$1.bscTestnet, transport: viem.http() });
|
|
1327
|
+
var goerliClient = viem.createPublicClient({ chain: chains$1.goerli, transport: viem.http() });
|
|
1328
|
+
var getDefaultClient = (chainId) => {
|
|
1329
|
+
switch (chainId) {
|
|
1330
|
+
case chains.ChainId.ETHEREUM:
|
|
1331
|
+
return ethClient;
|
|
1332
|
+
case chains.ChainId.BSC:
|
|
1333
|
+
return bscClient;
|
|
1334
|
+
case chains.ChainId.BSC_TESTNET:
|
|
1335
|
+
return bscTestnetClient;
|
|
1336
|
+
case chains.ChainId.GOERLI:
|
|
1337
|
+
return goerliClient;
|
|
1338
|
+
default:
|
|
1339
|
+
return bscClient;
|
|
1340
|
+
}
|
|
1341
|
+
};
|
|
1342
|
+
var Fetcher = class {
|
|
1343
|
+
/**
|
|
1344
|
+
* Cannot be constructed.
|
|
1345
|
+
*/
|
|
1346
|
+
// eslint-disable-next-line no-useless-constructor,@typescript-eslint/no-empty-function
|
|
1347
|
+
constructor() {
|
|
1348
|
+
}
|
|
1349
|
+
/**
|
|
1350
|
+
* Fetch information for a given token on the given chain, using the given viem provider.
|
|
1351
|
+
* @param chainId chain of the token
|
|
1352
|
+
* @param address address of the token on the chain
|
|
1353
|
+
* @param provider provider used to fetch the token
|
|
1354
|
+
* @param symbol symbol of the token
|
|
1355
|
+
* @param name optional name of the token
|
|
1356
|
+
*/
|
|
1357
|
+
static async fetchTokenData(chainId, address, publicClient = getDefaultClient(chainId), symbol, name) {
|
|
1358
|
+
const erc20 = viem.getContract({
|
|
1359
|
+
abi: swapSdkEvm.erc20Abi,
|
|
1360
|
+
address,
|
|
1361
|
+
client: publicClient
|
|
1362
|
+
});
|
|
1363
|
+
const parsedDecimals = typeof TOKEN_DECIMALS_CACHE?.[chainId]?.[address] === "number" ? TOKEN_DECIMALS_CACHE[chainId][address] : await erc20.read.decimals().then((decimals) => {
|
|
1364
|
+
TOKEN_DECIMALS_CACHE = {
|
|
1365
|
+
...TOKEN_DECIMALS_CACHE,
|
|
1366
|
+
[chainId]: {
|
|
1367
|
+
...TOKEN_DECIMALS_CACHE?.[chainId],
|
|
1368
|
+
[address]: decimals
|
|
1369
|
+
}
|
|
1370
|
+
};
|
|
1371
|
+
return decimals;
|
|
1372
|
+
});
|
|
1373
|
+
return new swapSdkCore.Token(chainId, address, parsedDecimals, symbol, name);
|
|
1374
|
+
}
|
|
1375
|
+
/**
|
|
1376
|
+
* Fetches information about a pair and constructs a pair from the given two tokens.
|
|
1377
|
+
* @param tokenA first token
|
|
1378
|
+
* @param tokenB second token
|
|
1379
|
+
* @param provider the provider to use to fetch the data
|
|
1380
|
+
*/
|
|
1381
|
+
static async fetchPairData(tokenA, tokenB, publicClient = getDefaultClient(tokenA.chainId)) {
|
|
1382
|
+
invariant3__default.default(tokenA.chainId === tokenB.chainId, "CHAIN_ID");
|
|
1383
|
+
const address = Pair.getAddress(tokenA, tokenB);
|
|
1384
|
+
const pairContract = viem.getContract({
|
|
1385
|
+
abi: pancakePairV2ABI,
|
|
1386
|
+
address,
|
|
1387
|
+
client: publicClient
|
|
1388
|
+
});
|
|
1389
|
+
const [reserves0, reserves1] = await pairContract.read.getReserves();
|
|
1390
|
+
const balances = tokenA.sortsBefore(tokenB) ? [reserves0, reserves1] : [reserves1, reserves0];
|
|
1391
|
+
return new Pair(
|
|
1392
|
+
swapSdkCore.CurrencyAmount.fromRawAmount(tokenA, balances[0]),
|
|
1393
|
+
swapSdkCore.CurrencyAmount.fromRawAmount(tokenB, balances[1])
|
|
1394
|
+
);
|
|
1395
|
+
}
|
|
1396
|
+
};
|
|
1397
|
+
function isTradeBetter(tradeA, tradeB, minimumDelta = swapSdkEvm.ZERO_PERCENT) {
|
|
1398
|
+
if (tradeA && !tradeB)
|
|
1399
|
+
return false;
|
|
1400
|
+
if (tradeB && !tradeA)
|
|
1401
|
+
return true;
|
|
1402
|
+
if (!tradeA || !tradeB)
|
|
1403
|
+
return void 0;
|
|
1404
|
+
if (tradeA.tradeType !== tradeB.tradeType || !tradeA.inputAmount.currency.equals(tradeB.inputAmount.currency) || !tradeA.outputAmount.currency.equals(tradeB.outputAmount.currency)) {
|
|
1405
|
+
throw new Error("Trades are not comparable");
|
|
1406
|
+
}
|
|
1407
|
+
if (minimumDelta.equalTo(swapSdkEvm.ZERO_PERCENT)) {
|
|
1408
|
+
return tradeA.executionPrice.lessThan(tradeB.executionPrice);
|
|
1409
|
+
}
|
|
1410
|
+
return tradeA.executionPrice.asFraction.multiply(minimumDelta.add(swapSdkEvm.ONE_HUNDRED_PERCENT)).lessThan(tradeB.executionPrice);
|
|
1411
|
+
}
|
|
1412
|
+
|
|
1413
|
+
exports.FACTORY_ADDRESS = FACTORY_ADDRESS;
|
|
1414
|
+
exports.FACTORY_ADDRESS_MAP = FACTORY_ADDRESS_MAP;
|
|
1415
|
+
exports.Fetcher = Fetcher;
|
|
1416
|
+
exports.INIT_CODE_HASH = INIT_CODE_HASH;
|
|
1417
|
+
exports.INIT_CODE_HASH_MAP = INIT_CODE_HASH_MAP;
|
|
1418
|
+
exports.Pair = Pair;
|
|
1419
|
+
exports.Route = Route;
|
|
1420
|
+
exports.Router = Router;
|
|
1421
|
+
exports.Trade = Trade;
|
|
1422
|
+
exports.computePairAddress = computePairAddress;
|
|
1423
|
+
exports.inputOutputComparator = inputOutputComparator;
|
|
1424
|
+
exports.isTradeBetter = isTradeBetter;
|
|
1425
|
+
exports.pancakePairV2ABI = pancakePairV2ABI;
|
|
1426
|
+
exports.tradeComparator = tradeComparator;
|
|
1427
|
+
//# sourceMappingURL=out.js.map
|
|
1428
|
+
//# sourceMappingURL=index.js.map
|