@pancakeswap/sdk 3.2.1 → 5.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/README.md +4 -4
- package/dist/abis/ERC20.d.ts +27 -0
- package/dist/abis/ERC20.d.ts.map +1 -0
- package/dist/abis/IPancakePair.d.ts +561 -0
- package/dist/abis/IPancakePair.d.ts.map +1 -0
- package/dist/constants.d.ts +3 -2
- package/dist/constants.d.ts.map +1 -1
- package/dist/entities/native.d.ts.map +1 -1
- package/dist/entities/pair.d.ts +4 -3
- package/dist/entities/pair.d.ts.map +1 -1
- package/dist/entities/route.d.ts.map +1 -1
- package/dist/entities/token.d.ts +3 -2
- package/dist/entities/token.d.ts.map +1 -1
- package/dist/fetcher.d.ts +4 -3
- package/dist/fetcher.d.ts.map +1 -1
- package/dist/index.d.ts +1 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +82 -59
- package/dist/index.mjs +78 -52
- package/dist/router.d.ts +3 -2
- package/dist/router.d.ts.map +1 -1
- package/dist/utils.d.ts +2 -1
- package/dist/utils.d.ts.map +1 -1
- package/package.json +12 -26
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"route.d.ts","sourceRoot":"","sources":["../../src/entities/route.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAE,KAAK,EAAE,MAAM,4BAA4B,CAAA;AAEnE,OAAO,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAA;AAE7B,qBAAa,KAAK,CAAC,MAAM,SAAS,QAAQ,EAAE,OAAO,SAAS,QAAQ;IAClE,SAAgB,KAAK,EAAE,IAAI,EAAE,CAAA;
|
|
1
|
+
{"version":3,"file":"route.d.ts","sourceRoot":"","sources":["../../src/entities/route.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAE,KAAK,EAAE,MAAM,4BAA4B,CAAA;AAEnE,OAAO,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAA;AAE7B,qBAAa,KAAK,CAAC,MAAM,SAAS,QAAQ,EAAE,OAAO,SAAS,QAAQ;IAClE,SAAgB,KAAK,EAAE,IAAI,EAAE,CAAA;IAE7B,SAAgB,IAAI,EAAE,KAAK,EAAE,CAAA;IAE7B,SAAgB,KAAK,EAAE,MAAM,CAAA;IAE7B,SAAgB,MAAM,EAAE,OAAO,CAAA;gBAEZ,KAAK,EAAE,IAAI,EAAE,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO;IA2BhE,OAAO,CAAC,SAAS,CAAsC;IAEvD,IAAW,QAAQ,IAAI,KAAK,CAAC,MAAM,EAAE,OAAO,CAAC,CAa5C;IAED,IAAW,OAAO,IAAI,MAAM,CAE3B;CACF"}
|
package/dist/entities/token.d.ts
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
import { Token } from '@pancakeswap/swap-sdk-core';
|
|
2
|
+
import { Address } from 'viem';
|
|
2
3
|
export interface SerializedToken {
|
|
3
4
|
chainId: number;
|
|
4
|
-
address:
|
|
5
|
+
address: Address;
|
|
5
6
|
decimals: number;
|
|
6
7
|
symbol: string;
|
|
7
8
|
name?: string;
|
|
8
9
|
projectLink?: string;
|
|
9
10
|
}
|
|
10
11
|
export declare class ERC20Token extends Token {
|
|
11
|
-
constructor(chainId: number, address:
|
|
12
|
+
constructor(chainId: number, address: Address, decimals: number, symbol: string, name?: string, projectLink?: string);
|
|
12
13
|
}
|
|
13
14
|
//# sourceMappingURL=token.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"token.d.ts","sourceRoot":"","sources":["../../src/entities/token.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,4BAA4B,CAAA;
|
|
1
|
+
{"version":3,"file":"token.d.ts","sourceRoot":"","sources":["../../src/entities/token.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,4BAA4B,CAAA;AAClD,OAAO,EAAE,OAAO,EAAE,MAAM,MAAM,CAAA;AAG9B,MAAM,WAAW,eAAe;IAC9B,OAAO,EAAE,MAAM,CAAA;IACf,OAAO,EAAE,OAAO,CAAA;IAChB,QAAQ,EAAE,MAAM,CAAA;IAChB,MAAM,EAAE,MAAM,CAAA;IACd,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,WAAW,CAAC,EAAE,MAAM,CAAA;CACrB;AAKD,qBAAa,UAAW,SAAQ,KAAK;gBAEjC,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,OAAO,EAChB,QAAQ,EAAE,MAAM,EAChB,MAAM,EAAE,MAAM,EACd,IAAI,CAAC,EAAE,MAAM,EACb,WAAW,CAAC,EAAE,MAAM;CAIvB"}
|
package/dist/fetcher.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { Address } from 'viem';
|
|
1
2
|
import { Token } from '@pancakeswap/swap-sdk-core';
|
|
2
3
|
import { Pair } from './entities/pair';
|
|
3
4
|
import { ChainId } from './constants';
|
|
@@ -10,20 +11,20 @@ export declare abstract class Fetcher {
|
|
|
10
11
|
*/
|
|
11
12
|
private constructor();
|
|
12
13
|
/**
|
|
13
|
-
* Fetch information for a given token on the given chain, using the given
|
|
14
|
+
* Fetch information for a given token on the given chain, using the given viem provider.
|
|
14
15
|
* @param chainId chain of the token
|
|
15
16
|
* @param address address of the token on the chain
|
|
16
17
|
* @param provider provider used to fetch the token
|
|
17
18
|
* @param symbol symbol of the token
|
|
18
19
|
* @param name optional name of the token
|
|
19
20
|
*/
|
|
20
|
-
static fetchTokenData(chainId: ChainId, address:
|
|
21
|
+
static fetchTokenData(chainId: ChainId, address: Address, publicClient: any, symbol: string, name?: string): Promise<Token>;
|
|
21
22
|
/**
|
|
22
23
|
* Fetches information about a pair and constructs a pair from the given two tokens.
|
|
23
24
|
* @param tokenA first token
|
|
24
25
|
* @param tokenB second token
|
|
25
26
|
* @param provider the provider to use to fetch the data
|
|
26
27
|
*/
|
|
27
|
-
static fetchPairData(tokenA: Token, tokenB: Token,
|
|
28
|
+
static fetchPairData(tokenA: Token, tokenB: Token, publicClient?: any): Promise<Pair>;
|
|
28
29
|
}
|
|
29
30
|
//# sourceMappingURL=fetcher.d.ts.map
|
package/dist/fetcher.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fetcher.d.ts","sourceRoot":"","sources":["../src/fetcher.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"fetcher.d.ts","sourceRoot":"","sources":["../src/fetcher.ts"],"names":[],"mappings":"AAAA,OAAO,EAAuD,OAAO,EAAE,MAAM,MAAM,CAAA;AAEnF,OAAO,EAAkB,KAAK,EAAE,MAAM,4BAA4B,CAAA;AAElE,OAAO,EAAE,IAAI,EAAE,MAAM,iBAAiB,CAAA;AACtC,OAAO,EAAE,OAAO,EAAE,MAAM,aAAa,CAAA;AA4BrC;;GAEG;AACH,8BAAsB,OAAO;IAC3B;;OAEG;IAEH,OAAO;IAEP;;;;;;;OAOG;WACiB,cAAc,CAChC,OAAO,EAAE,OAAO,EAChB,OAAO,EAAE,OAAO,EAChB,YAAY,KAAiC,EAC7C,MAAM,EAAE,MAAM,EACd,IAAI,CAAC,EAAE,MAAM,GACZ,OAAO,CAAC,KAAK,CAAC;IAsBjB;;;;;OAKG;WACiB,aAAa,CAC/B,MAAM,EAAE,KAAK,EACb,MAAM,EAAE,KAAK,EACb,YAAY,GAAE,GAAsC,GACnD,OAAO,CAAC,IAAI,CAAC;CAejB"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import JSBI from 'jsbi';
|
|
2
|
-
export { JSBI };
|
|
3
1
|
export * from './constants';
|
|
4
2
|
export * from './trade';
|
|
5
3
|
export * from './entities';
|
|
@@ -8,4 +6,5 @@ export * from './utils';
|
|
|
8
6
|
export * from './ether';
|
|
9
7
|
export * from './fetcher';
|
|
10
8
|
export * from '@pancakeswap/swap-sdk-core';
|
|
9
|
+
export * from './abis/IPancakePair';
|
|
11
10
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAA;AAC3B,cAAc,SAAS,CAAA;AAEvB,cAAc,YAAY,CAAA;AAC1B,cAAc,UAAU,CAAA;AAExB,cAAc,SAAS,CAAA;AAEvB,cAAc,SAAS,CAAA;AACvB,cAAc,WAAW,CAAA;AAEzB,cAAc,4BAA4B,CAAA;AAE1C,cAAc,qBAAqB,CAAA"}
|
package/dist/index.js
CHANGED
|
@@ -1,29 +1,24 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var JSBI = require('jsbi');
|
|
4
3
|
var swapSdkCore = require('@pancakeswap/swap-sdk-core');
|
|
5
|
-
var
|
|
4
|
+
var viem = require('viem');
|
|
6
5
|
var invariant4 = require('tiny-invariant');
|
|
7
6
|
var warning = require('tiny-warning');
|
|
8
|
-
var
|
|
9
|
-
var contracts = require('@ethersproject/contracts');
|
|
10
|
-
var networks = require('@ethersproject/networks');
|
|
11
|
-
var providers = require('@ethersproject/providers');
|
|
7
|
+
var chains = require('viem/chains');
|
|
12
8
|
|
|
13
9
|
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
14
10
|
|
|
15
|
-
var JSBI__default = /*#__PURE__*/_interopDefault(JSBI);
|
|
16
11
|
var invariant4__default = /*#__PURE__*/_interopDefault(invariant4);
|
|
17
12
|
var warning__default = /*#__PURE__*/_interopDefault(warning);
|
|
18
13
|
|
|
19
|
-
// src/
|
|
20
|
-
function validateAndParseAddress(address
|
|
14
|
+
// src/constants.ts
|
|
15
|
+
function validateAndParseAddress(address) {
|
|
21
16
|
try {
|
|
22
|
-
const checksummedAddress =
|
|
23
|
-
warning__default.default(address
|
|
17
|
+
const checksummedAddress = viem.getAddress(address);
|
|
18
|
+
warning__default.default(address === checksummedAddress, `${address} is not checksummed.`);
|
|
24
19
|
return checksummedAddress;
|
|
25
20
|
} catch (error) {
|
|
26
|
-
invariant4__default.default(false, `${address
|
|
21
|
+
invariant4__default.default(false, `${address} is not a valid address.`);
|
|
27
22
|
}
|
|
28
23
|
}
|
|
29
24
|
|
|
@@ -159,6 +154,13 @@ function isTradeBetter(tradeA, tradeB, minimumDelta = ZERO_PERCENT) {
|
|
|
159
154
|
}
|
|
160
155
|
var PAIR_ADDRESS_CACHE = {};
|
|
161
156
|
var composeKey = (token0, token1) => `${token0.chainId}-${token0.address}-${token1.address}`;
|
|
157
|
+
function getCreate2Address(from_, salt_, initCodeHash) {
|
|
158
|
+
const from = viem.toBytes(viem.getAddress(from_));
|
|
159
|
+
const salt = viem.pad(viem.isBytes(salt_) ? salt_ : viem.toBytes(salt_), {
|
|
160
|
+
size: 32
|
|
161
|
+
});
|
|
162
|
+
return viem.getAddress(viem.slice(viem.keccak256(viem.concat([viem.toBytes("0xff"), from, salt, viem.toBytes(initCodeHash)])), 12));
|
|
163
|
+
}
|
|
162
164
|
var computePairAddress = ({
|
|
163
165
|
factoryAddress,
|
|
164
166
|
tokenA,
|
|
@@ -166,12 +168,12 @@ var computePairAddress = ({
|
|
|
166
168
|
}) => {
|
|
167
169
|
const [token0, token1] = tokenA.sortsBefore(tokenB) ? [tokenA, tokenB] : [tokenB, tokenA];
|
|
168
170
|
const key = composeKey(token0, token1);
|
|
169
|
-
if (
|
|
171
|
+
if (PAIR_ADDRESS_CACHE?.[key] === void 0) {
|
|
170
172
|
PAIR_ADDRESS_CACHE = {
|
|
171
173
|
...PAIR_ADDRESS_CACHE,
|
|
172
|
-
[key]:
|
|
174
|
+
[key]: getCreate2Address(
|
|
173
175
|
factoryAddress,
|
|
174
|
-
|
|
176
|
+
viem.keccak256(viem.encodePacked(["address", "address"], [token0.address, token1.address])),
|
|
175
177
|
INIT_CODE_HASH_MAP[token0.chainId]
|
|
176
178
|
)
|
|
177
179
|
};
|
|
@@ -246,35 +248,35 @@ var Pair = class {
|
|
|
246
248
|
}
|
|
247
249
|
getOutputAmount(inputAmount) {
|
|
248
250
|
invariant4__default.default(this.involvesToken(inputAmount.currency), "TOKEN");
|
|
249
|
-
if (
|
|
251
|
+
if (this.reserve0.quotient === swapSdkCore.ZERO || this.reserve1.quotient === swapSdkCore.ZERO) {
|
|
250
252
|
throw new swapSdkCore.InsufficientReservesError();
|
|
251
253
|
}
|
|
252
254
|
const inputReserve = this.reserveOf(inputAmount.currency);
|
|
253
255
|
const outputReserve = this.reserveOf(inputAmount.currency.equals(this.token0) ? this.token1 : this.token0);
|
|
254
|
-
const inputAmountWithFee =
|
|
255
|
-
const numerator =
|
|
256
|
-
const denominator =
|
|
256
|
+
const inputAmountWithFee = inputAmount.quotient * swapSdkCore._9975;
|
|
257
|
+
const numerator = inputAmountWithFee * outputReserve.quotient;
|
|
258
|
+
const denominator = inputReserve.quotient * swapSdkCore._10000 + inputAmountWithFee;
|
|
257
259
|
const outputAmount = swapSdkCore.CurrencyAmount.fromRawAmount(
|
|
258
260
|
inputAmount.currency.equals(this.token0) ? this.token1 : this.token0,
|
|
259
|
-
|
|
261
|
+
numerator / denominator
|
|
260
262
|
);
|
|
261
|
-
if (
|
|
263
|
+
if (outputAmount.quotient === swapSdkCore.ZERO) {
|
|
262
264
|
throw new swapSdkCore.InsufficientInputAmountError();
|
|
263
265
|
}
|
|
264
266
|
return [outputAmount, new Pair(inputReserve.add(inputAmount), outputReserve.subtract(outputAmount))];
|
|
265
267
|
}
|
|
266
268
|
getInputAmount(outputAmount) {
|
|
267
269
|
invariant4__default.default(this.involvesToken(outputAmount.currency), "TOKEN");
|
|
268
|
-
if (
|
|
270
|
+
if (this.reserve0.quotient === swapSdkCore.ZERO || this.reserve1.quotient === swapSdkCore.ZERO || outputAmount.quotient >= this.reserveOf(outputAmount.currency).quotient) {
|
|
269
271
|
throw new swapSdkCore.InsufficientReservesError();
|
|
270
272
|
}
|
|
271
273
|
const outputReserve = this.reserveOf(outputAmount.currency);
|
|
272
274
|
const inputReserve = this.reserveOf(outputAmount.currency.equals(this.token0) ? this.token1 : this.token0);
|
|
273
|
-
const numerator =
|
|
274
|
-
const denominator =
|
|
275
|
+
const numerator = inputReserve.quotient * outputAmount.quotient * swapSdkCore._10000;
|
|
276
|
+
const denominator = (outputReserve.quotient - outputAmount.quotient) * swapSdkCore._9975;
|
|
275
277
|
const inputAmount = swapSdkCore.CurrencyAmount.fromRawAmount(
|
|
276
278
|
outputAmount.currency.equals(this.token0) ? this.token1 : this.token0,
|
|
277
|
-
|
|
279
|
+
numerator / denominator + swapSdkCore.ONE
|
|
278
280
|
);
|
|
279
281
|
return [inputAmount, new Pair(inputReserve.add(inputAmount), outputReserve.subtract(outputAmount))];
|
|
280
282
|
}
|
|
@@ -283,17 +285,14 @@ var Pair = class {
|
|
|
283
285
|
const tokenAmounts = tokenAmountA.currency.sortsBefore(tokenAmountB.currency) ? [tokenAmountA, tokenAmountB] : [tokenAmountB, tokenAmountA];
|
|
284
286
|
invariant4__default.default(tokenAmounts[0].currency.equals(this.token0) && tokenAmounts[1].currency.equals(this.token1), "TOKEN");
|
|
285
287
|
let liquidity;
|
|
286
|
-
if (
|
|
287
|
-
liquidity =
|
|
288
|
-
swapSdkCore.sqrt(JSBI__default.default.multiply(tokenAmounts[0].quotient, tokenAmounts[1].quotient)),
|
|
289
|
-
swapSdkCore.MINIMUM_LIQUIDITY
|
|
290
|
-
);
|
|
288
|
+
if (totalSupply.quotient === swapSdkCore.ZERO) {
|
|
289
|
+
liquidity = swapSdkCore.sqrt(tokenAmounts[0].quotient * tokenAmounts[1].quotient) - swapSdkCore.MINIMUM_LIQUIDITY;
|
|
291
290
|
} else {
|
|
292
|
-
const amount0 =
|
|
293
|
-
const amount1 =
|
|
294
|
-
liquidity =
|
|
291
|
+
const amount0 = tokenAmounts[0].quotient * totalSupply.quotient / this.reserve0.quotient;
|
|
292
|
+
const amount1 = tokenAmounts[1].quotient * totalSupply.quotient / this.reserve1.quotient;
|
|
293
|
+
liquidity = amount0 <= amount1 ? amount0 : amount1;
|
|
295
294
|
}
|
|
296
|
-
if (!
|
|
295
|
+
if (!(liquidity > swapSdkCore.ZERO)) {
|
|
297
296
|
throw new swapSdkCore.InsufficientInputAmountError();
|
|
298
297
|
}
|
|
299
298
|
return swapSdkCore.CurrencyAmount.fromRawAmount(this.liquidityToken, liquidity);
|
|
@@ -302,20 +301,20 @@ var Pair = class {
|
|
|
302
301
|
invariant4__default.default(this.involvesToken(token), "TOKEN");
|
|
303
302
|
invariant4__default.default(totalSupply.currency.equals(this.liquidityToken), "TOTAL_SUPPLY");
|
|
304
303
|
invariant4__default.default(liquidity.currency.equals(this.liquidityToken), "LIQUIDITY");
|
|
305
|
-
invariant4__default.default(
|
|
304
|
+
invariant4__default.default(liquidity.quotient <= totalSupply.quotient, "LIQUIDITY");
|
|
306
305
|
let totalSupplyAdjusted;
|
|
307
306
|
if (!feeOn) {
|
|
308
307
|
totalSupplyAdjusted = totalSupply;
|
|
309
308
|
} else {
|
|
310
309
|
invariant4__default.default(!!kLast, "K_LAST");
|
|
311
|
-
const kLastParsed =
|
|
312
|
-
if (!
|
|
313
|
-
const rootK = swapSdkCore.sqrt(
|
|
310
|
+
const kLastParsed = BigInt(kLast);
|
|
311
|
+
if (!(kLastParsed === swapSdkCore.ZERO)) {
|
|
312
|
+
const rootK = swapSdkCore.sqrt(this.reserve0.quotient * this.reserve1.quotient);
|
|
314
313
|
const rootKLast = swapSdkCore.sqrt(kLastParsed);
|
|
315
|
-
if (
|
|
316
|
-
const numerator =
|
|
317
|
-
const denominator =
|
|
318
|
-
const feeLiquidity =
|
|
314
|
+
if (rootK > rootKLast) {
|
|
315
|
+
const numerator = totalSupply.quotient * (rootK - rootKLast);
|
|
316
|
+
const denominator = rootK * swapSdkCore.FIVE + rootKLast;
|
|
317
|
+
const feeLiquidity = numerator / denominator;
|
|
319
318
|
totalSupplyAdjusted = totalSupply.add(swapSdkCore.CurrencyAmount.fromRawAmount(this.liquidityToken, feeLiquidity));
|
|
320
319
|
} else {
|
|
321
320
|
totalSupplyAdjusted = totalSupply;
|
|
@@ -326,7 +325,7 @@ var Pair = class {
|
|
|
326
325
|
}
|
|
327
326
|
return swapSdkCore.CurrencyAmount.fromRawAmount(
|
|
328
327
|
token,
|
|
329
|
-
|
|
328
|
+
liquidity.quotient * this.reserveOf(token).quotient / totalSupplyAdjusted.quotient
|
|
330
329
|
);
|
|
331
330
|
}
|
|
332
331
|
};
|
|
@@ -334,7 +333,7 @@ var Route = class {
|
|
|
334
333
|
constructor(pairs, input, output) {
|
|
335
334
|
this._midPrice = null;
|
|
336
335
|
invariant4__default.default(pairs.length > 0, "PAIRS");
|
|
337
|
-
const chainId = pairs[0]
|
|
336
|
+
const { chainId } = pairs[0];
|
|
338
337
|
invariant4__default.default(
|
|
339
338
|
pairs.every((pair) => pair.chainId === chainId),
|
|
340
339
|
"CHAIN_IDS"
|
|
@@ -740,8 +739,8 @@ var _Ether = class extends swapSdkCore.NativeCurrency {
|
|
|
740
739
|
var Ether = _Ether;
|
|
741
740
|
Ether._etherCache = {};
|
|
742
741
|
|
|
743
|
-
// src/abis/ERC20.
|
|
744
|
-
var
|
|
742
|
+
// src/abis/ERC20.ts
|
|
743
|
+
var erc20ABI = [
|
|
745
744
|
{
|
|
746
745
|
constant: true,
|
|
747
746
|
inputs: [],
|
|
@@ -762,8 +761,8 @@ var ERC20_default = [
|
|
|
762
761
|
}
|
|
763
762
|
];
|
|
764
763
|
|
|
765
|
-
// src/abis/IPancakePair.
|
|
766
|
-
var
|
|
764
|
+
// src/abis/IPancakePair.ts
|
|
765
|
+
var pancakePairV2ABI = [
|
|
767
766
|
{
|
|
768
767
|
anonymous: false,
|
|
769
768
|
inputs: [
|
|
@@ -1475,6 +1474,24 @@ var IPancakePair_default = [
|
|
|
1475
1474
|
var TOKEN_DECIMALS_CACHE = {
|
|
1476
1475
|
[56 /* BSC */]: {}
|
|
1477
1476
|
};
|
|
1477
|
+
var ethClient = viem.createPublicClient({ chain: chains.mainnet, transport: viem.http() });
|
|
1478
|
+
var bscClient = viem.createPublicClient({ chain: chains.bsc, transport: viem.http() });
|
|
1479
|
+
var bscTestnetClient = viem.createPublicClient({ chain: chains.bscTestnet, transport: viem.http() });
|
|
1480
|
+
var goerliClient = viem.createPublicClient({ chain: chains.goerli, transport: viem.http() });
|
|
1481
|
+
var getDefaultClient = (chainId) => {
|
|
1482
|
+
switch (chainId) {
|
|
1483
|
+
case 1 /* ETHEREUM */:
|
|
1484
|
+
return ethClient;
|
|
1485
|
+
case 56 /* BSC */:
|
|
1486
|
+
return bscClient;
|
|
1487
|
+
case 97 /* BSC_TESTNET */:
|
|
1488
|
+
return bscTestnetClient;
|
|
1489
|
+
case 5 /* GOERLI */:
|
|
1490
|
+
return goerliClient;
|
|
1491
|
+
default:
|
|
1492
|
+
return bscClient;
|
|
1493
|
+
}
|
|
1494
|
+
};
|
|
1478
1495
|
var Fetcher = class {
|
|
1479
1496
|
/**
|
|
1480
1497
|
* Cannot be constructed.
|
|
@@ -1483,20 +1500,24 @@ var Fetcher = class {
|
|
|
1483
1500
|
constructor() {
|
|
1484
1501
|
}
|
|
1485
1502
|
/**
|
|
1486
|
-
* Fetch information for a given token on the given chain, using the given
|
|
1503
|
+
* Fetch information for a given token on the given chain, using the given viem provider.
|
|
1487
1504
|
* @param chainId chain of the token
|
|
1488
1505
|
* @param address address of the token on the chain
|
|
1489
1506
|
* @param provider provider used to fetch the token
|
|
1490
1507
|
* @param symbol symbol of the token
|
|
1491
1508
|
* @param name optional name of the token
|
|
1492
1509
|
*/
|
|
1493
|
-
static async fetchTokenData(chainId, address,
|
|
1494
|
-
|
|
1495
|
-
|
|
1510
|
+
static async fetchTokenData(chainId, address, publicClient = getDefaultClient(chainId), symbol, name) {
|
|
1511
|
+
const erc20 = viem.getContract({
|
|
1512
|
+
abi: erc20ABI,
|
|
1513
|
+
address,
|
|
1514
|
+
publicClient
|
|
1515
|
+
});
|
|
1516
|
+
const parsedDecimals = typeof TOKEN_DECIMALS_CACHE?.[chainId]?.[address] === "number" ? TOKEN_DECIMALS_CACHE[chainId][address] : await erc20.read.decimals().then((decimals) => {
|
|
1496
1517
|
TOKEN_DECIMALS_CACHE = {
|
|
1497
1518
|
...TOKEN_DECIMALS_CACHE,
|
|
1498
1519
|
[chainId]: {
|
|
1499
|
-
...TOKEN_DECIMALS_CACHE
|
|
1520
|
+
...TOKEN_DECIMALS_CACHE?.[chainId],
|
|
1500
1521
|
[address]: decimals
|
|
1501
1522
|
}
|
|
1502
1523
|
};
|
|
@@ -1510,10 +1531,15 @@ var Fetcher = class {
|
|
|
1510
1531
|
* @param tokenB second token
|
|
1511
1532
|
* @param provider the provider to use to fetch the data
|
|
1512
1533
|
*/
|
|
1513
|
-
static async fetchPairData(tokenA, tokenB,
|
|
1534
|
+
static async fetchPairData(tokenA, tokenB, publicClient = getDefaultClient(tokenA.chainId)) {
|
|
1514
1535
|
invariant4__default.default(tokenA.chainId === tokenB.chainId, "CHAIN_ID");
|
|
1515
1536
|
const address = Pair.getAddress(tokenA, tokenB);
|
|
1516
|
-
const
|
|
1537
|
+
const pairContract = viem.getContract({
|
|
1538
|
+
abi: pancakePairV2ABI,
|
|
1539
|
+
address,
|
|
1540
|
+
publicClient
|
|
1541
|
+
});
|
|
1542
|
+
const [reserves0, reserves1] = await pairContract.read.getReserves();
|
|
1517
1543
|
const balances = tokenA.sortsBefore(tokenB) ? [reserves0, reserves1] : [reserves1, reserves0];
|
|
1518
1544
|
return new Pair(
|
|
1519
1545
|
swapSdkCore.CurrencyAmount.fromRawAmount(tokenA, balances[0]),
|
|
@@ -1522,10 +1548,6 @@ var Fetcher = class {
|
|
|
1522
1548
|
}
|
|
1523
1549
|
};
|
|
1524
1550
|
|
|
1525
|
-
Object.defineProperty(exports, 'JSBI', {
|
|
1526
|
-
enumerable: true,
|
|
1527
|
-
get: function () { return JSBI__default.default; }
|
|
1528
|
-
});
|
|
1529
1551
|
exports.ChainId = ChainId;
|
|
1530
1552
|
exports.ERC20Token = ERC20Token;
|
|
1531
1553
|
exports.Ether = Ether;
|
|
@@ -1548,6 +1570,7 @@ exports.ZERO_PERCENT = ZERO_PERCENT;
|
|
|
1548
1570
|
exports.computePairAddress = computePairAddress;
|
|
1549
1571
|
exports.inputOutputComparator = inputOutputComparator;
|
|
1550
1572
|
exports.isTradeBetter = isTradeBetter;
|
|
1573
|
+
exports.pancakePairV2ABI = pancakePairV2ABI;
|
|
1551
1574
|
exports.tradeComparator = tradeComparator;
|
|
1552
1575
|
exports.validateAndParseAddress = validateAndParseAddress;
|
|
1553
1576
|
Object.keys(swapSdkCore).forEach(function (k) {
|
package/dist/index.mjs
CHANGED
|
@@ -1,16 +1,11 @@
|
|
|
1
|
-
import
|
|
2
|
-
export { default as JSBI } from 'jsbi';
|
|
3
|
-
import { Percent, Token, Price, ZERO, InsufficientReservesError, _9975, _10000, CurrencyAmount, InsufficientInputAmountError, ONE, sqrt, MINIMUM_LIQUIDITY, FIVE, TradeType, computePriceImpact, Fraction, sortedInsert, NativeCurrency } from '@pancakeswap/swap-sdk-core';
|
|
1
|
+
import { Percent, Token, Price, ZERO, InsufficientReservesError, _9975, _10000, CurrencyAmount, InsufficientInputAmountError, ONE, sqrt, MINIMUM_LIQUIDITY, TradeType, computePriceImpact, Fraction, sortedInsert, NativeCurrency, FIVE } from '@pancakeswap/swap-sdk-core';
|
|
4
2
|
export * from '@pancakeswap/swap-sdk-core';
|
|
5
|
-
import { getAddress,
|
|
3
|
+
import { createPublicClient, http, getAddress, keccak256, encodePacked, getContract, toBytes, pad, isBytes, slice, concat } from 'viem';
|
|
6
4
|
import invariant4 from 'tiny-invariant';
|
|
7
5
|
import warning from 'tiny-warning';
|
|
8
|
-
import {
|
|
9
|
-
import { Contract } from '@ethersproject/contracts';
|
|
10
|
-
import { getNetwork } from '@ethersproject/networks';
|
|
11
|
-
import { getDefaultProvider } from '@ethersproject/providers';
|
|
6
|
+
import { mainnet, bsc, bscTestnet, goerli } from 'viem/chains';
|
|
12
7
|
|
|
13
|
-
// src/
|
|
8
|
+
// src/constants.ts
|
|
14
9
|
function validateAndParseAddress(address) {
|
|
15
10
|
try {
|
|
16
11
|
const checksummedAddress = getAddress(address);
|
|
@@ -153,6 +148,13 @@ function isTradeBetter(tradeA, tradeB, minimumDelta = ZERO_PERCENT) {
|
|
|
153
148
|
}
|
|
154
149
|
var PAIR_ADDRESS_CACHE = {};
|
|
155
150
|
var composeKey = (token0, token1) => `${token0.chainId}-${token0.address}-${token1.address}`;
|
|
151
|
+
function getCreate2Address(from_, salt_, initCodeHash) {
|
|
152
|
+
const from = toBytes(getAddress(from_));
|
|
153
|
+
const salt = pad(isBytes(salt_) ? salt_ : toBytes(salt_), {
|
|
154
|
+
size: 32
|
|
155
|
+
});
|
|
156
|
+
return getAddress(slice(keccak256(concat([toBytes("0xff"), from, salt, toBytes(initCodeHash)])), 12));
|
|
157
|
+
}
|
|
156
158
|
var computePairAddress = ({
|
|
157
159
|
factoryAddress,
|
|
158
160
|
tokenA,
|
|
@@ -160,12 +162,12 @@ var computePairAddress = ({
|
|
|
160
162
|
}) => {
|
|
161
163
|
const [token0, token1] = tokenA.sortsBefore(tokenB) ? [tokenA, tokenB] : [tokenB, tokenA];
|
|
162
164
|
const key = composeKey(token0, token1);
|
|
163
|
-
if (
|
|
165
|
+
if (PAIR_ADDRESS_CACHE?.[key] === void 0) {
|
|
164
166
|
PAIR_ADDRESS_CACHE = {
|
|
165
167
|
...PAIR_ADDRESS_CACHE,
|
|
166
168
|
[key]: getCreate2Address(
|
|
167
169
|
factoryAddress,
|
|
168
|
-
keccak256(
|
|
170
|
+
keccak256(encodePacked(["address", "address"], [token0.address, token1.address])),
|
|
169
171
|
INIT_CODE_HASH_MAP[token0.chainId]
|
|
170
172
|
)
|
|
171
173
|
};
|
|
@@ -240,35 +242,35 @@ var Pair = class {
|
|
|
240
242
|
}
|
|
241
243
|
getOutputAmount(inputAmount) {
|
|
242
244
|
invariant4(this.involvesToken(inputAmount.currency), "TOKEN");
|
|
243
|
-
if (
|
|
245
|
+
if (this.reserve0.quotient === ZERO || this.reserve1.quotient === ZERO) {
|
|
244
246
|
throw new InsufficientReservesError();
|
|
245
247
|
}
|
|
246
248
|
const inputReserve = this.reserveOf(inputAmount.currency);
|
|
247
249
|
const outputReserve = this.reserveOf(inputAmount.currency.equals(this.token0) ? this.token1 : this.token0);
|
|
248
|
-
const inputAmountWithFee =
|
|
249
|
-
const numerator =
|
|
250
|
-
const denominator =
|
|
250
|
+
const inputAmountWithFee = inputAmount.quotient * _9975;
|
|
251
|
+
const numerator = inputAmountWithFee * outputReserve.quotient;
|
|
252
|
+
const denominator = inputReserve.quotient * _10000 + inputAmountWithFee;
|
|
251
253
|
const outputAmount = CurrencyAmount.fromRawAmount(
|
|
252
254
|
inputAmount.currency.equals(this.token0) ? this.token1 : this.token0,
|
|
253
|
-
|
|
255
|
+
numerator / denominator
|
|
254
256
|
);
|
|
255
|
-
if (
|
|
257
|
+
if (outputAmount.quotient === ZERO) {
|
|
256
258
|
throw new InsufficientInputAmountError();
|
|
257
259
|
}
|
|
258
260
|
return [outputAmount, new Pair(inputReserve.add(inputAmount), outputReserve.subtract(outputAmount))];
|
|
259
261
|
}
|
|
260
262
|
getInputAmount(outputAmount) {
|
|
261
263
|
invariant4(this.involvesToken(outputAmount.currency), "TOKEN");
|
|
262
|
-
if (
|
|
264
|
+
if (this.reserve0.quotient === ZERO || this.reserve1.quotient === ZERO || outputAmount.quotient >= this.reserveOf(outputAmount.currency).quotient) {
|
|
263
265
|
throw new InsufficientReservesError();
|
|
264
266
|
}
|
|
265
267
|
const outputReserve = this.reserveOf(outputAmount.currency);
|
|
266
268
|
const inputReserve = this.reserveOf(outputAmount.currency.equals(this.token0) ? this.token1 : this.token0);
|
|
267
|
-
const numerator =
|
|
268
|
-
const denominator =
|
|
269
|
+
const numerator = inputReserve.quotient * outputAmount.quotient * _10000;
|
|
270
|
+
const denominator = (outputReserve.quotient - outputAmount.quotient) * _9975;
|
|
269
271
|
const inputAmount = CurrencyAmount.fromRawAmount(
|
|
270
272
|
outputAmount.currency.equals(this.token0) ? this.token1 : this.token0,
|
|
271
|
-
|
|
273
|
+
numerator / denominator + ONE
|
|
272
274
|
);
|
|
273
275
|
return [inputAmount, new Pair(inputReserve.add(inputAmount), outputReserve.subtract(outputAmount))];
|
|
274
276
|
}
|
|
@@ -277,17 +279,14 @@ var Pair = class {
|
|
|
277
279
|
const tokenAmounts = tokenAmountA.currency.sortsBefore(tokenAmountB.currency) ? [tokenAmountA, tokenAmountB] : [tokenAmountB, tokenAmountA];
|
|
278
280
|
invariant4(tokenAmounts[0].currency.equals(this.token0) && tokenAmounts[1].currency.equals(this.token1), "TOKEN");
|
|
279
281
|
let liquidity;
|
|
280
|
-
if (
|
|
281
|
-
liquidity =
|
|
282
|
-
sqrt(JSBI.multiply(tokenAmounts[0].quotient, tokenAmounts[1].quotient)),
|
|
283
|
-
MINIMUM_LIQUIDITY
|
|
284
|
-
);
|
|
282
|
+
if (totalSupply.quotient === ZERO) {
|
|
283
|
+
liquidity = sqrt(tokenAmounts[0].quotient * tokenAmounts[1].quotient) - MINIMUM_LIQUIDITY;
|
|
285
284
|
} else {
|
|
286
|
-
const amount0 =
|
|
287
|
-
const amount1 =
|
|
288
|
-
liquidity =
|
|
285
|
+
const amount0 = tokenAmounts[0].quotient * totalSupply.quotient / this.reserve0.quotient;
|
|
286
|
+
const amount1 = tokenAmounts[1].quotient * totalSupply.quotient / this.reserve1.quotient;
|
|
287
|
+
liquidity = amount0 <= amount1 ? amount0 : amount1;
|
|
289
288
|
}
|
|
290
|
-
if (!
|
|
289
|
+
if (!(liquidity > ZERO)) {
|
|
291
290
|
throw new InsufficientInputAmountError();
|
|
292
291
|
}
|
|
293
292
|
return CurrencyAmount.fromRawAmount(this.liquidityToken, liquidity);
|
|
@@ -296,20 +295,20 @@ var Pair = class {
|
|
|
296
295
|
invariant4(this.involvesToken(token), "TOKEN");
|
|
297
296
|
invariant4(totalSupply.currency.equals(this.liquidityToken), "TOTAL_SUPPLY");
|
|
298
297
|
invariant4(liquidity.currency.equals(this.liquidityToken), "LIQUIDITY");
|
|
299
|
-
invariant4(
|
|
298
|
+
invariant4(liquidity.quotient <= totalSupply.quotient, "LIQUIDITY");
|
|
300
299
|
let totalSupplyAdjusted;
|
|
301
300
|
if (!feeOn) {
|
|
302
301
|
totalSupplyAdjusted = totalSupply;
|
|
303
302
|
} else {
|
|
304
303
|
invariant4(!!kLast, "K_LAST");
|
|
305
|
-
const kLastParsed =
|
|
306
|
-
if (!
|
|
307
|
-
const rootK = sqrt(
|
|
304
|
+
const kLastParsed = BigInt(kLast);
|
|
305
|
+
if (!(kLastParsed === ZERO)) {
|
|
306
|
+
const rootK = sqrt(this.reserve0.quotient * this.reserve1.quotient);
|
|
308
307
|
const rootKLast = sqrt(kLastParsed);
|
|
309
|
-
if (
|
|
310
|
-
const numerator =
|
|
311
|
-
const denominator =
|
|
312
|
-
const feeLiquidity =
|
|
308
|
+
if (rootK > rootKLast) {
|
|
309
|
+
const numerator = totalSupply.quotient * (rootK - rootKLast);
|
|
310
|
+
const denominator = rootK * FIVE + rootKLast;
|
|
311
|
+
const feeLiquidity = numerator / denominator;
|
|
313
312
|
totalSupplyAdjusted = totalSupply.add(CurrencyAmount.fromRawAmount(this.liquidityToken, feeLiquidity));
|
|
314
313
|
} else {
|
|
315
314
|
totalSupplyAdjusted = totalSupply;
|
|
@@ -320,7 +319,7 @@ var Pair = class {
|
|
|
320
319
|
}
|
|
321
320
|
return CurrencyAmount.fromRawAmount(
|
|
322
321
|
token,
|
|
323
|
-
|
|
322
|
+
liquidity.quotient * this.reserveOf(token).quotient / totalSupplyAdjusted.quotient
|
|
324
323
|
);
|
|
325
324
|
}
|
|
326
325
|
};
|
|
@@ -328,7 +327,7 @@ var Route = class {
|
|
|
328
327
|
constructor(pairs, input, output) {
|
|
329
328
|
this._midPrice = null;
|
|
330
329
|
invariant4(pairs.length > 0, "PAIRS");
|
|
331
|
-
const chainId = pairs[0]
|
|
330
|
+
const { chainId } = pairs[0];
|
|
332
331
|
invariant4(
|
|
333
332
|
pairs.every((pair) => pair.chainId === chainId),
|
|
334
333
|
"CHAIN_IDS"
|
|
@@ -734,8 +733,8 @@ var _Ether = class extends NativeCurrency {
|
|
|
734
733
|
var Ether = _Ether;
|
|
735
734
|
Ether._etherCache = {};
|
|
736
735
|
|
|
737
|
-
// src/abis/ERC20.
|
|
738
|
-
var
|
|
736
|
+
// src/abis/ERC20.ts
|
|
737
|
+
var erc20ABI = [
|
|
739
738
|
{
|
|
740
739
|
constant: true,
|
|
741
740
|
inputs: [],
|
|
@@ -756,8 +755,8 @@ var ERC20_default = [
|
|
|
756
755
|
}
|
|
757
756
|
];
|
|
758
757
|
|
|
759
|
-
// src/abis/IPancakePair.
|
|
760
|
-
var
|
|
758
|
+
// src/abis/IPancakePair.ts
|
|
759
|
+
var pancakePairV2ABI = [
|
|
761
760
|
{
|
|
762
761
|
anonymous: false,
|
|
763
762
|
inputs: [
|
|
@@ -1469,6 +1468,24 @@ var IPancakePair_default = [
|
|
|
1469
1468
|
var TOKEN_DECIMALS_CACHE = {
|
|
1470
1469
|
[56 /* BSC */]: {}
|
|
1471
1470
|
};
|
|
1471
|
+
var ethClient = createPublicClient({ chain: mainnet, transport: http() });
|
|
1472
|
+
var bscClient = createPublicClient({ chain: bsc, transport: http() });
|
|
1473
|
+
var bscTestnetClient = createPublicClient({ chain: bscTestnet, transport: http() });
|
|
1474
|
+
var goerliClient = createPublicClient({ chain: goerli, transport: http() });
|
|
1475
|
+
var getDefaultClient = (chainId) => {
|
|
1476
|
+
switch (chainId) {
|
|
1477
|
+
case 1 /* ETHEREUM */:
|
|
1478
|
+
return ethClient;
|
|
1479
|
+
case 56 /* BSC */:
|
|
1480
|
+
return bscClient;
|
|
1481
|
+
case 97 /* BSC_TESTNET */:
|
|
1482
|
+
return bscTestnetClient;
|
|
1483
|
+
case 5 /* GOERLI */:
|
|
1484
|
+
return goerliClient;
|
|
1485
|
+
default:
|
|
1486
|
+
return bscClient;
|
|
1487
|
+
}
|
|
1488
|
+
};
|
|
1472
1489
|
var Fetcher = class {
|
|
1473
1490
|
/**
|
|
1474
1491
|
* Cannot be constructed.
|
|
@@ -1477,20 +1494,24 @@ var Fetcher = class {
|
|
|
1477
1494
|
constructor() {
|
|
1478
1495
|
}
|
|
1479
1496
|
/**
|
|
1480
|
-
* Fetch information for a given token on the given chain, using the given
|
|
1497
|
+
* Fetch information for a given token on the given chain, using the given viem provider.
|
|
1481
1498
|
* @param chainId chain of the token
|
|
1482
1499
|
* @param address address of the token on the chain
|
|
1483
1500
|
* @param provider provider used to fetch the token
|
|
1484
1501
|
* @param symbol symbol of the token
|
|
1485
1502
|
* @param name optional name of the token
|
|
1486
1503
|
*/
|
|
1487
|
-
static async fetchTokenData(chainId, address,
|
|
1488
|
-
|
|
1489
|
-
|
|
1504
|
+
static async fetchTokenData(chainId, address, publicClient = getDefaultClient(chainId), symbol, name) {
|
|
1505
|
+
const erc20 = getContract({
|
|
1506
|
+
abi: erc20ABI,
|
|
1507
|
+
address,
|
|
1508
|
+
publicClient
|
|
1509
|
+
});
|
|
1510
|
+
const parsedDecimals = typeof TOKEN_DECIMALS_CACHE?.[chainId]?.[address] === "number" ? TOKEN_DECIMALS_CACHE[chainId][address] : await erc20.read.decimals().then((decimals) => {
|
|
1490
1511
|
TOKEN_DECIMALS_CACHE = {
|
|
1491
1512
|
...TOKEN_DECIMALS_CACHE,
|
|
1492
1513
|
[chainId]: {
|
|
1493
|
-
...TOKEN_DECIMALS_CACHE
|
|
1514
|
+
...TOKEN_DECIMALS_CACHE?.[chainId],
|
|
1494
1515
|
[address]: decimals
|
|
1495
1516
|
}
|
|
1496
1517
|
};
|
|
@@ -1504,10 +1525,15 @@ var Fetcher = class {
|
|
|
1504
1525
|
* @param tokenB second token
|
|
1505
1526
|
* @param provider the provider to use to fetch the data
|
|
1506
1527
|
*/
|
|
1507
|
-
static async fetchPairData(tokenA, tokenB,
|
|
1528
|
+
static async fetchPairData(tokenA, tokenB, publicClient = getDefaultClient(tokenA.chainId)) {
|
|
1508
1529
|
invariant4(tokenA.chainId === tokenB.chainId, "CHAIN_ID");
|
|
1509
1530
|
const address = Pair.getAddress(tokenA, tokenB);
|
|
1510
|
-
const
|
|
1531
|
+
const pairContract = getContract({
|
|
1532
|
+
abi: pancakePairV2ABI,
|
|
1533
|
+
address,
|
|
1534
|
+
publicClient
|
|
1535
|
+
});
|
|
1536
|
+
const [reserves0, reserves1] = await pairContract.read.getReserves();
|
|
1511
1537
|
const balances = tokenA.sortsBefore(tokenB) ? [reserves0, reserves1] : [reserves1, reserves0];
|
|
1512
1538
|
return new Pair(
|
|
1513
1539
|
CurrencyAmount.fromRawAmount(tokenA, balances[0]),
|
|
@@ -1516,4 +1542,4 @@ var Fetcher = class {
|
|
|
1516
1542
|
}
|
|
1517
1543
|
};
|
|
1518
1544
|
|
|
1519
|
-
export { ChainId, ERC20Token, Ether, FACTORY_ADDRESS, FACTORY_ADDRESS_MAP, Fetcher, INIT_CODE_HASH, INIT_CODE_HASH_MAP, NATIVE, Native, ONE_HUNDRED_PERCENT, Pair, Route, Router, Trade, WBNB, WETH9, WNATIVE, ZERO_PERCENT, computePairAddress, inputOutputComparator, isTradeBetter, tradeComparator, validateAndParseAddress };
|
|
1545
|
+
export { ChainId, ERC20Token, Ether, FACTORY_ADDRESS, FACTORY_ADDRESS_MAP, Fetcher, INIT_CODE_HASH, INIT_CODE_HASH_MAP, NATIVE, Native, ONE_HUNDRED_PERCENT, Pair, Route, Router, Trade, WBNB, WETH9, WNATIVE, ZERO_PERCENT, computePairAddress, inputOutputComparator, isTradeBetter, pancakePairV2ABI, tradeComparator, validateAndParseAddress };
|