@pancakeswap/sdk 3.2.1 → 4.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/entities/pair.d.ts.map +1 -1
- package/dist/entities/route.d.ts.map +1 -1
- package/dist/index.d.ts +0 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +30 -40
- package/dist/index.mjs +31 -37
- package/package.json +11 -14
package/README.md
CHANGED
|
@@ -6,7 +6,7 @@ You can refer to the Uniswap SDK documentation [uniswap.org](https://docs.uniswa
|
|
|
6
6
|
|
|
7
7
|
## Running tests
|
|
8
8
|
|
|
9
|
-
To run the tests, follow these steps. You must have at least node v10 and [
|
|
9
|
+
To run the tests, follow these steps. You must have at least node v10 and [pnpm](https://pnpm.io) installed.
|
|
10
10
|
|
|
11
11
|
First clone the repository:
|
|
12
12
|
|
|
@@ -23,19 +23,19 @@ cd pancakeswap-sdk/
|
|
|
23
23
|
Install dependencies
|
|
24
24
|
|
|
25
25
|
```sh
|
|
26
|
-
|
|
26
|
+
pnpm install
|
|
27
27
|
```
|
|
28
28
|
|
|
29
29
|
Run tests
|
|
30
30
|
|
|
31
31
|
```sh
|
|
32
|
-
|
|
32
|
+
pnpm test
|
|
33
33
|
```
|
|
34
34
|
|
|
35
35
|
You should see output like the following:
|
|
36
36
|
|
|
37
37
|
```sh
|
|
38
|
-
|
|
38
|
+
pnpm run v1.22.4
|
|
39
39
|
$ tsdx test
|
|
40
40
|
PASS test/constants.test.ts
|
|
41
41
|
PASS test/pair.test.ts
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pair.d.ts","sourceRoot":"","sources":["../../src/entities/pair.ts"],"names":[],"mappings":"AAAA,OAAO,EAIL,cAAc,EACd,KAAK,EAML,SAAS,EAEV,MAAM,4BAA4B,CAAA;
|
|
1
|
+
{"version":3,"file":"pair.d.ts","sourceRoot":"","sources":["../../src/entities/pair.ts"],"names":[],"mappings":"AAAA,OAAO,EAIL,cAAc,EACd,KAAK,EAML,SAAS,EAEV,MAAM,4BAA4B,CAAA;AAMnC,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAA;AAMpC,eAAO,MAAM,kBAAkB;oBAKb,MAAM;YACd,UAAU;YACV,UAAU;MAChB,MAgBH,CAAA;AAED,qBAAa,IAAI;IACf,SAAgB,cAAc,EAAE,UAAU,CAAA;IAE1C,OAAO,CAAC,QAAQ,CAAC,YAAY,CAA0D;WAEzE,UAAU,CAAC,MAAM,EAAE,UAAU,EAAE,MAAM,EAAE,UAAU,GAAG,MAAM;gBAIrD,eAAe,EAAE,cAAc,CAAC,UAAU,CAAC,EAAE,YAAY,EAAE,cAAc,CAAC,UAAU,CAAC;IAcxG;;;OAGG;IACI,aAAa,CAAC,KAAK,EAAE,UAAU,GAAG,OAAO;IAIhD;;OAEG;IACH,IAAW,WAAW,IAAI,KAAK,CAAC,UAAU,EAAE,UAAU,CAAC,CAGtD;IAED;;OAEG;IACH,IAAW,WAAW,IAAI,KAAK,CAAC,UAAU,EAAE,UAAU,CAAC,CAGtD;IAED;;;OAGG;IACI,OAAO,CAAC,KAAK,EAAE,UAAU,GAAG,KAAK,CAAC,UAAU,EAAE,UAAU,CAAC;IAKhE;;OAEG;IACH,IAAW,OAAO,IAAI,MAAM,CAE3B;IAED,IAAW,MAAM,IAAI,UAAU,CAE9B;IAED,IAAW,MAAM,IAAI,UAAU,CAE9B;IAED,IAAW,QAAQ,IAAI,cAAc,CAAC,UAAU,CAAC,CAEhD;IAED,IAAW,QAAQ,IAAI,cAAc,CAAC,UAAU,CAAC,CAEhD;IAEM,SAAS,CAAC,KAAK,EAAE,UAAU,GAAG,cAAc,CAAC,UAAU,CAAC;IAKxD,eAAe,CAAC,WAAW,EAAE,cAAc,CAAC,UAAU,CAAC,GAAG,CAAC,cAAc,CAAC,UAAU,CAAC,EAAE,IAAI,CAAC;IAoB5F,cAAc,CAAC,YAAY,EAAE,cAAc,CAAC,UAAU,CAAC,GAAG,CAAC,cAAc,CAAC,UAAU,CAAC,EAAE,IAAI,CAAC;IAqB5F,kBAAkB,CACvB,WAAW,EAAE,cAAc,CAAC,UAAU,CAAC,EACvC,YAAY,EAAE,cAAc,CAAC,UAAU,CAAC,EACxC,YAAY,EAAE,cAAc,CAAC,UAAU,CAAC,GACvC,cAAc,CAAC,UAAU,CAAC;IAqBtB,iBAAiB,CACtB,KAAK,EAAE,UAAU,EACjB,WAAW,EAAE,cAAc,CAAC,UAAU,CAAC,EACvC,SAAS,EAAE,cAAc,CAAC,UAAU,CAAC,EACrC,KAAK,UAAQ,EACb,KAAK,CAAC,EAAE,SAAS,GAChB,cAAc,CAAC,UAAU,CAAC;CAiC9B"}
|
|
@@ -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/index.d.ts
CHANGED
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"}
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var JSBI = require('jsbi');
|
|
4
3
|
var swapSdkCore = require('@pancakeswap/swap-sdk-core');
|
|
5
4
|
var address = require('@ethersproject/address');
|
|
6
5
|
var invariant4 = require('tiny-invariant');
|
|
@@ -12,11 +11,10 @@ var providers = require('@ethersproject/providers');
|
|
|
12
11
|
|
|
13
12
|
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
14
13
|
|
|
15
|
-
var JSBI__default = /*#__PURE__*/_interopDefault(JSBI);
|
|
16
14
|
var invariant4__default = /*#__PURE__*/_interopDefault(invariant4);
|
|
17
15
|
var warning__default = /*#__PURE__*/_interopDefault(warning);
|
|
18
16
|
|
|
19
|
-
// src/
|
|
17
|
+
// src/constants.ts
|
|
20
18
|
function validateAndParseAddress(address$1) {
|
|
21
19
|
try {
|
|
22
20
|
const checksummedAddress = address.getAddress(address$1);
|
|
@@ -166,7 +164,7 @@ var computePairAddress = ({
|
|
|
166
164
|
}) => {
|
|
167
165
|
const [token0, token1] = tokenA.sortsBefore(tokenB) ? [tokenA, tokenB] : [tokenB, tokenA];
|
|
168
166
|
const key = composeKey(token0, token1);
|
|
169
|
-
if (
|
|
167
|
+
if (PAIR_ADDRESS_CACHE?.[key] === void 0) {
|
|
170
168
|
PAIR_ADDRESS_CACHE = {
|
|
171
169
|
...PAIR_ADDRESS_CACHE,
|
|
172
170
|
[key]: address.getCreate2Address(
|
|
@@ -246,35 +244,35 @@ var Pair = class {
|
|
|
246
244
|
}
|
|
247
245
|
getOutputAmount(inputAmount) {
|
|
248
246
|
invariant4__default.default(this.involvesToken(inputAmount.currency), "TOKEN");
|
|
249
|
-
if (
|
|
247
|
+
if (this.reserve0.quotient === swapSdkCore.ZERO || this.reserve1.quotient === swapSdkCore.ZERO) {
|
|
250
248
|
throw new swapSdkCore.InsufficientReservesError();
|
|
251
249
|
}
|
|
252
250
|
const inputReserve = this.reserveOf(inputAmount.currency);
|
|
253
251
|
const outputReserve = this.reserveOf(inputAmount.currency.equals(this.token0) ? this.token1 : this.token0);
|
|
254
|
-
const inputAmountWithFee =
|
|
255
|
-
const numerator =
|
|
256
|
-
const denominator =
|
|
252
|
+
const inputAmountWithFee = inputAmount.quotient * swapSdkCore._9975;
|
|
253
|
+
const numerator = inputAmountWithFee * outputReserve.quotient;
|
|
254
|
+
const denominator = inputReserve.quotient * swapSdkCore._10000 + inputAmountWithFee;
|
|
257
255
|
const outputAmount = swapSdkCore.CurrencyAmount.fromRawAmount(
|
|
258
256
|
inputAmount.currency.equals(this.token0) ? this.token1 : this.token0,
|
|
259
|
-
|
|
257
|
+
numerator / denominator
|
|
260
258
|
);
|
|
261
|
-
if (
|
|
259
|
+
if (outputAmount.quotient === swapSdkCore.ZERO) {
|
|
262
260
|
throw new swapSdkCore.InsufficientInputAmountError();
|
|
263
261
|
}
|
|
264
262
|
return [outputAmount, new Pair(inputReserve.add(inputAmount), outputReserve.subtract(outputAmount))];
|
|
265
263
|
}
|
|
266
264
|
getInputAmount(outputAmount) {
|
|
267
265
|
invariant4__default.default(this.involvesToken(outputAmount.currency), "TOKEN");
|
|
268
|
-
if (
|
|
266
|
+
if (this.reserve0.quotient === swapSdkCore.ZERO || this.reserve1.quotient === swapSdkCore.ZERO || outputAmount.quotient >= this.reserveOf(outputAmount.currency).quotient) {
|
|
269
267
|
throw new swapSdkCore.InsufficientReservesError();
|
|
270
268
|
}
|
|
271
269
|
const outputReserve = this.reserveOf(outputAmount.currency);
|
|
272
270
|
const inputReserve = this.reserveOf(outputAmount.currency.equals(this.token0) ? this.token1 : this.token0);
|
|
273
|
-
const numerator =
|
|
274
|
-
const denominator =
|
|
271
|
+
const numerator = inputReserve.quotient * outputAmount.quotient * swapSdkCore._10000;
|
|
272
|
+
const denominator = (outputReserve.quotient - outputAmount.quotient) * swapSdkCore._9975;
|
|
275
273
|
const inputAmount = swapSdkCore.CurrencyAmount.fromRawAmount(
|
|
276
274
|
outputAmount.currency.equals(this.token0) ? this.token1 : this.token0,
|
|
277
|
-
|
|
275
|
+
numerator / denominator + swapSdkCore.ONE
|
|
278
276
|
);
|
|
279
277
|
return [inputAmount, new Pair(inputReserve.add(inputAmount), outputReserve.subtract(outputAmount))];
|
|
280
278
|
}
|
|
@@ -283,17 +281,14 @@ var Pair = class {
|
|
|
283
281
|
const tokenAmounts = tokenAmountA.currency.sortsBefore(tokenAmountB.currency) ? [tokenAmountA, tokenAmountB] : [tokenAmountB, tokenAmountA];
|
|
284
282
|
invariant4__default.default(tokenAmounts[0].currency.equals(this.token0) && tokenAmounts[1].currency.equals(this.token1), "TOKEN");
|
|
285
283
|
let liquidity;
|
|
286
|
-
if (
|
|
287
|
-
liquidity =
|
|
288
|
-
swapSdkCore.sqrt(JSBI__default.default.multiply(tokenAmounts[0].quotient, tokenAmounts[1].quotient)),
|
|
289
|
-
swapSdkCore.MINIMUM_LIQUIDITY
|
|
290
|
-
);
|
|
284
|
+
if (totalSupply.quotient === swapSdkCore.ZERO) {
|
|
285
|
+
liquidity = swapSdkCore.sqrt(tokenAmounts[0].quotient * tokenAmounts[1].quotient) - swapSdkCore.MINIMUM_LIQUIDITY;
|
|
291
286
|
} else {
|
|
292
|
-
const amount0 =
|
|
293
|
-
const amount1 =
|
|
294
|
-
liquidity =
|
|
287
|
+
const amount0 = tokenAmounts[0].quotient * totalSupply.quotient / this.reserve0.quotient;
|
|
288
|
+
const amount1 = tokenAmounts[1].quotient * totalSupply.quotient / this.reserve1.quotient;
|
|
289
|
+
liquidity = amount0 <= amount1 ? amount0 : amount1;
|
|
295
290
|
}
|
|
296
|
-
if (!
|
|
291
|
+
if (!(liquidity > swapSdkCore.ZERO)) {
|
|
297
292
|
throw new swapSdkCore.InsufficientInputAmountError();
|
|
298
293
|
}
|
|
299
294
|
return swapSdkCore.CurrencyAmount.fromRawAmount(this.liquidityToken, liquidity);
|
|
@@ -302,20 +297,20 @@ var Pair = class {
|
|
|
302
297
|
invariant4__default.default(this.involvesToken(token), "TOKEN");
|
|
303
298
|
invariant4__default.default(totalSupply.currency.equals(this.liquidityToken), "TOTAL_SUPPLY");
|
|
304
299
|
invariant4__default.default(liquidity.currency.equals(this.liquidityToken), "LIQUIDITY");
|
|
305
|
-
invariant4__default.default(
|
|
300
|
+
invariant4__default.default(liquidity.quotient <= totalSupply.quotient, "LIQUIDITY");
|
|
306
301
|
let totalSupplyAdjusted;
|
|
307
302
|
if (!feeOn) {
|
|
308
303
|
totalSupplyAdjusted = totalSupply;
|
|
309
304
|
} else {
|
|
310
305
|
invariant4__default.default(!!kLast, "K_LAST");
|
|
311
|
-
const kLastParsed =
|
|
312
|
-
if (!
|
|
313
|
-
const rootK = swapSdkCore.sqrt(
|
|
306
|
+
const kLastParsed = BigInt(kLast);
|
|
307
|
+
if (!(kLastParsed === swapSdkCore.ZERO)) {
|
|
308
|
+
const rootK = swapSdkCore.sqrt(this.reserve0.quotient * this.reserve1.quotient);
|
|
314
309
|
const rootKLast = swapSdkCore.sqrt(kLastParsed);
|
|
315
|
-
if (
|
|
316
|
-
const numerator =
|
|
317
|
-
const denominator =
|
|
318
|
-
const feeLiquidity =
|
|
310
|
+
if (rootK > rootKLast) {
|
|
311
|
+
const numerator = totalSupply.quotient * (rootK - rootKLast);
|
|
312
|
+
const denominator = rootK * swapSdkCore.FIVE + rootKLast;
|
|
313
|
+
const feeLiquidity = numerator / denominator;
|
|
319
314
|
totalSupplyAdjusted = totalSupply.add(swapSdkCore.CurrencyAmount.fromRawAmount(this.liquidityToken, feeLiquidity));
|
|
320
315
|
} else {
|
|
321
316
|
totalSupplyAdjusted = totalSupply;
|
|
@@ -326,7 +321,7 @@ var Pair = class {
|
|
|
326
321
|
}
|
|
327
322
|
return swapSdkCore.CurrencyAmount.fromRawAmount(
|
|
328
323
|
token,
|
|
329
|
-
|
|
324
|
+
liquidity.quotient * this.reserveOf(token).quotient / totalSupplyAdjusted.quotient
|
|
330
325
|
);
|
|
331
326
|
}
|
|
332
327
|
};
|
|
@@ -334,7 +329,7 @@ var Route = class {
|
|
|
334
329
|
constructor(pairs, input, output) {
|
|
335
330
|
this._midPrice = null;
|
|
336
331
|
invariant4__default.default(pairs.length > 0, "PAIRS");
|
|
337
|
-
const chainId = pairs[0]
|
|
332
|
+
const { chainId } = pairs[0];
|
|
338
333
|
invariant4__default.default(
|
|
339
334
|
pairs.every((pair) => pair.chainId === chainId),
|
|
340
335
|
"CHAIN_IDS"
|
|
@@ -1491,12 +1486,11 @@ var Fetcher = class {
|
|
|
1491
1486
|
* @param name optional name of the token
|
|
1492
1487
|
*/
|
|
1493
1488
|
static async fetchTokenData(chainId, address, provider = providers.getDefaultProvider(networks.getNetwork(chainId)), symbol, name) {
|
|
1494
|
-
|
|
1495
|
-
const parsedDecimals = typeof ((_a = TOKEN_DECIMALS_CACHE == null ? void 0 : TOKEN_DECIMALS_CACHE[chainId]) == null ? void 0 : _a[address]) === "number" ? TOKEN_DECIMALS_CACHE[chainId][address] : await new contracts.Contract(address, ERC20_default, provider).decimals().then((decimals) => {
|
|
1489
|
+
const parsedDecimals = typeof TOKEN_DECIMALS_CACHE?.[chainId]?.[address] === "number" ? TOKEN_DECIMALS_CACHE[chainId][address] : await new contracts.Contract(address, ERC20_default, provider).decimals().then((decimals) => {
|
|
1496
1490
|
TOKEN_DECIMALS_CACHE = {
|
|
1497
1491
|
...TOKEN_DECIMALS_CACHE,
|
|
1498
1492
|
[chainId]: {
|
|
1499
|
-
...TOKEN_DECIMALS_CACHE
|
|
1493
|
+
...TOKEN_DECIMALS_CACHE?.[chainId],
|
|
1500
1494
|
[address]: decimals
|
|
1501
1495
|
}
|
|
1502
1496
|
};
|
|
@@ -1522,10 +1516,6 @@ var Fetcher = class {
|
|
|
1522
1516
|
}
|
|
1523
1517
|
};
|
|
1524
1518
|
|
|
1525
|
-
Object.defineProperty(exports, 'JSBI', {
|
|
1526
|
-
enumerable: true,
|
|
1527
|
-
get: function () { return JSBI__default.default; }
|
|
1528
|
-
});
|
|
1529
1519
|
exports.ChainId = ChainId;
|
|
1530
1520
|
exports.ERC20Token = ERC20Token;
|
|
1531
1521
|
exports.Ether = Ether;
|
package/dist/index.mjs
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
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
3
|
import { getAddress, getCreate2Address } from '@ethersproject/address';
|
|
6
4
|
import invariant4 from 'tiny-invariant';
|
|
@@ -10,7 +8,7 @@ import { Contract } from '@ethersproject/contracts';
|
|
|
10
8
|
import { getNetwork } from '@ethersproject/networks';
|
|
11
9
|
import { getDefaultProvider } from '@ethersproject/providers';
|
|
12
10
|
|
|
13
|
-
// src/
|
|
11
|
+
// src/constants.ts
|
|
14
12
|
function validateAndParseAddress(address) {
|
|
15
13
|
try {
|
|
16
14
|
const checksummedAddress = getAddress(address);
|
|
@@ -160,7 +158,7 @@ var computePairAddress = ({
|
|
|
160
158
|
}) => {
|
|
161
159
|
const [token0, token1] = tokenA.sortsBefore(tokenB) ? [tokenA, tokenB] : [tokenB, tokenA];
|
|
162
160
|
const key = composeKey(token0, token1);
|
|
163
|
-
if (
|
|
161
|
+
if (PAIR_ADDRESS_CACHE?.[key] === void 0) {
|
|
164
162
|
PAIR_ADDRESS_CACHE = {
|
|
165
163
|
...PAIR_ADDRESS_CACHE,
|
|
166
164
|
[key]: getCreate2Address(
|
|
@@ -240,35 +238,35 @@ var Pair = class {
|
|
|
240
238
|
}
|
|
241
239
|
getOutputAmount(inputAmount) {
|
|
242
240
|
invariant4(this.involvesToken(inputAmount.currency), "TOKEN");
|
|
243
|
-
if (
|
|
241
|
+
if (this.reserve0.quotient === ZERO || this.reserve1.quotient === ZERO) {
|
|
244
242
|
throw new InsufficientReservesError();
|
|
245
243
|
}
|
|
246
244
|
const inputReserve = this.reserveOf(inputAmount.currency);
|
|
247
245
|
const outputReserve = this.reserveOf(inputAmount.currency.equals(this.token0) ? this.token1 : this.token0);
|
|
248
|
-
const inputAmountWithFee =
|
|
249
|
-
const numerator =
|
|
250
|
-
const denominator =
|
|
246
|
+
const inputAmountWithFee = inputAmount.quotient * _9975;
|
|
247
|
+
const numerator = inputAmountWithFee * outputReserve.quotient;
|
|
248
|
+
const denominator = inputReserve.quotient * _10000 + inputAmountWithFee;
|
|
251
249
|
const outputAmount = CurrencyAmount.fromRawAmount(
|
|
252
250
|
inputAmount.currency.equals(this.token0) ? this.token1 : this.token0,
|
|
253
|
-
|
|
251
|
+
numerator / denominator
|
|
254
252
|
);
|
|
255
|
-
if (
|
|
253
|
+
if (outputAmount.quotient === ZERO) {
|
|
256
254
|
throw new InsufficientInputAmountError();
|
|
257
255
|
}
|
|
258
256
|
return [outputAmount, new Pair(inputReserve.add(inputAmount), outputReserve.subtract(outputAmount))];
|
|
259
257
|
}
|
|
260
258
|
getInputAmount(outputAmount) {
|
|
261
259
|
invariant4(this.involvesToken(outputAmount.currency), "TOKEN");
|
|
262
|
-
if (
|
|
260
|
+
if (this.reserve0.quotient === ZERO || this.reserve1.quotient === ZERO || outputAmount.quotient >= this.reserveOf(outputAmount.currency).quotient) {
|
|
263
261
|
throw new InsufficientReservesError();
|
|
264
262
|
}
|
|
265
263
|
const outputReserve = this.reserveOf(outputAmount.currency);
|
|
266
264
|
const inputReserve = this.reserveOf(outputAmount.currency.equals(this.token0) ? this.token1 : this.token0);
|
|
267
|
-
const numerator =
|
|
268
|
-
const denominator =
|
|
265
|
+
const numerator = inputReserve.quotient * outputAmount.quotient * _10000;
|
|
266
|
+
const denominator = (outputReserve.quotient - outputAmount.quotient) * _9975;
|
|
269
267
|
const inputAmount = CurrencyAmount.fromRawAmount(
|
|
270
268
|
outputAmount.currency.equals(this.token0) ? this.token1 : this.token0,
|
|
271
|
-
|
|
269
|
+
numerator / denominator + ONE
|
|
272
270
|
);
|
|
273
271
|
return [inputAmount, new Pair(inputReserve.add(inputAmount), outputReserve.subtract(outputAmount))];
|
|
274
272
|
}
|
|
@@ -277,17 +275,14 @@ var Pair = class {
|
|
|
277
275
|
const tokenAmounts = tokenAmountA.currency.sortsBefore(tokenAmountB.currency) ? [tokenAmountA, tokenAmountB] : [tokenAmountB, tokenAmountA];
|
|
278
276
|
invariant4(tokenAmounts[0].currency.equals(this.token0) && tokenAmounts[1].currency.equals(this.token1), "TOKEN");
|
|
279
277
|
let liquidity;
|
|
280
|
-
if (
|
|
281
|
-
liquidity =
|
|
282
|
-
sqrt(JSBI.multiply(tokenAmounts[0].quotient, tokenAmounts[1].quotient)),
|
|
283
|
-
MINIMUM_LIQUIDITY
|
|
284
|
-
);
|
|
278
|
+
if (totalSupply.quotient === ZERO) {
|
|
279
|
+
liquidity = sqrt(tokenAmounts[0].quotient * tokenAmounts[1].quotient) - MINIMUM_LIQUIDITY;
|
|
285
280
|
} else {
|
|
286
|
-
const amount0 =
|
|
287
|
-
const amount1 =
|
|
288
|
-
liquidity =
|
|
281
|
+
const amount0 = tokenAmounts[0].quotient * totalSupply.quotient / this.reserve0.quotient;
|
|
282
|
+
const amount1 = tokenAmounts[1].quotient * totalSupply.quotient / this.reserve1.quotient;
|
|
283
|
+
liquidity = amount0 <= amount1 ? amount0 : amount1;
|
|
289
284
|
}
|
|
290
|
-
if (!
|
|
285
|
+
if (!(liquidity > ZERO)) {
|
|
291
286
|
throw new InsufficientInputAmountError();
|
|
292
287
|
}
|
|
293
288
|
return CurrencyAmount.fromRawAmount(this.liquidityToken, liquidity);
|
|
@@ -296,20 +291,20 @@ var Pair = class {
|
|
|
296
291
|
invariant4(this.involvesToken(token), "TOKEN");
|
|
297
292
|
invariant4(totalSupply.currency.equals(this.liquidityToken), "TOTAL_SUPPLY");
|
|
298
293
|
invariant4(liquidity.currency.equals(this.liquidityToken), "LIQUIDITY");
|
|
299
|
-
invariant4(
|
|
294
|
+
invariant4(liquidity.quotient <= totalSupply.quotient, "LIQUIDITY");
|
|
300
295
|
let totalSupplyAdjusted;
|
|
301
296
|
if (!feeOn) {
|
|
302
297
|
totalSupplyAdjusted = totalSupply;
|
|
303
298
|
} else {
|
|
304
299
|
invariant4(!!kLast, "K_LAST");
|
|
305
|
-
const kLastParsed =
|
|
306
|
-
if (!
|
|
307
|
-
const rootK = sqrt(
|
|
300
|
+
const kLastParsed = BigInt(kLast);
|
|
301
|
+
if (!(kLastParsed === ZERO)) {
|
|
302
|
+
const rootK = sqrt(this.reserve0.quotient * this.reserve1.quotient);
|
|
308
303
|
const rootKLast = sqrt(kLastParsed);
|
|
309
|
-
if (
|
|
310
|
-
const numerator =
|
|
311
|
-
const denominator =
|
|
312
|
-
const feeLiquidity =
|
|
304
|
+
if (rootK > rootKLast) {
|
|
305
|
+
const numerator = totalSupply.quotient * (rootK - rootKLast);
|
|
306
|
+
const denominator = rootK * FIVE + rootKLast;
|
|
307
|
+
const feeLiquidity = numerator / denominator;
|
|
313
308
|
totalSupplyAdjusted = totalSupply.add(CurrencyAmount.fromRawAmount(this.liquidityToken, feeLiquidity));
|
|
314
309
|
} else {
|
|
315
310
|
totalSupplyAdjusted = totalSupply;
|
|
@@ -320,7 +315,7 @@ var Pair = class {
|
|
|
320
315
|
}
|
|
321
316
|
return CurrencyAmount.fromRawAmount(
|
|
322
317
|
token,
|
|
323
|
-
|
|
318
|
+
liquidity.quotient * this.reserveOf(token).quotient / totalSupplyAdjusted.quotient
|
|
324
319
|
);
|
|
325
320
|
}
|
|
326
321
|
};
|
|
@@ -328,7 +323,7 @@ var Route = class {
|
|
|
328
323
|
constructor(pairs, input, output) {
|
|
329
324
|
this._midPrice = null;
|
|
330
325
|
invariant4(pairs.length > 0, "PAIRS");
|
|
331
|
-
const chainId = pairs[0]
|
|
326
|
+
const { chainId } = pairs[0];
|
|
332
327
|
invariant4(
|
|
333
328
|
pairs.every((pair) => pair.chainId === chainId),
|
|
334
329
|
"CHAIN_IDS"
|
|
@@ -1485,12 +1480,11 @@ var Fetcher = class {
|
|
|
1485
1480
|
* @param name optional name of the token
|
|
1486
1481
|
*/
|
|
1487
1482
|
static async fetchTokenData(chainId, address, provider = getDefaultProvider(getNetwork(chainId)), symbol, name) {
|
|
1488
|
-
|
|
1489
|
-
const parsedDecimals = typeof ((_a = TOKEN_DECIMALS_CACHE == null ? void 0 : TOKEN_DECIMALS_CACHE[chainId]) == null ? void 0 : _a[address]) === "number" ? TOKEN_DECIMALS_CACHE[chainId][address] : await new Contract(address, ERC20_default, provider).decimals().then((decimals) => {
|
|
1483
|
+
const parsedDecimals = typeof TOKEN_DECIMALS_CACHE?.[chainId]?.[address] === "number" ? TOKEN_DECIMALS_CACHE[chainId][address] : await new Contract(address, ERC20_default, provider).decimals().then((decimals) => {
|
|
1490
1484
|
TOKEN_DECIMALS_CACHE = {
|
|
1491
1485
|
...TOKEN_DECIMALS_CACHE,
|
|
1492
1486
|
[chainId]: {
|
|
1493
|
-
...TOKEN_DECIMALS_CACHE
|
|
1487
|
+
...TOKEN_DECIMALS_CACHE?.[chainId],
|
|
1494
1488
|
[address]: decimals
|
|
1495
1489
|
}
|
|
1496
1490
|
};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pancakeswap/sdk",
|
|
3
3
|
"license": "MIT",
|
|
4
|
-
"version": "
|
|
4
|
+
"version": "4.0.0",
|
|
5
5
|
"description": "🛠 An SDK for building applications on top of Pancakeswap.",
|
|
6
6
|
"main": "dist/index.js",
|
|
7
7
|
"typings": "dist/index.d.ts",
|
|
@@ -26,21 +26,13 @@
|
|
|
26
26
|
},
|
|
27
27
|
"./package.json": "./package.json"
|
|
28
28
|
},
|
|
29
|
-
"scripts": {
|
|
30
|
-
"build": "tsup",
|
|
31
|
-
"dev": "tsup --watch",
|
|
32
|
-
"test": "jest",
|
|
33
|
-
"prepublishOnly": "yarn run build",
|
|
34
|
-
"clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist"
|
|
35
|
-
},
|
|
36
29
|
"dependencies": {
|
|
37
|
-
"@pancakeswap/swap-sdk-core": "*",
|
|
38
30
|
"big.js": "^5.2.2",
|
|
39
31
|
"decimal.js-light": "^2.5.0",
|
|
40
|
-
"jsbi": "^3.1.4",
|
|
41
32
|
"tiny-invariant": "^1.1.0",
|
|
42
33
|
"tiny-warning": "^1.0.3",
|
|
43
|
-
"toformat": "^2.0.0"
|
|
34
|
+
"toformat": "^2.0.0",
|
|
35
|
+
"@pancakeswap/swap-sdk-core": "1.0.0"
|
|
44
36
|
},
|
|
45
37
|
"peerDependencies": {
|
|
46
38
|
"@ethersproject/address": "^5.0.0",
|
|
@@ -58,8 +50,7 @@
|
|
|
58
50
|
"@swc/core": "^1.2.215",
|
|
59
51
|
"@swc/jest": "^0.2.21",
|
|
60
52
|
"@types/big.js": "^4.0.5",
|
|
61
|
-
"
|
|
62
|
-
"babel-plugin-transform-jsbi-to-bigint": "^1.3.1"
|
|
53
|
+
"tsup": "^6.7.0"
|
|
63
54
|
},
|
|
64
55
|
"engines": {
|
|
65
56
|
"node": ">=10"
|
|
@@ -68,5 +59,11 @@
|
|
|
68
59
|
"printWidth": 120,
|
|
69
60
|
"semi": false,
|
|
70
61
|
"singleQuote": true
|
|
62
|
+
},
|
|
63
|
+
"scripts": {
|
|
64
|
+
"build": "tsup",
|
|
65
|
+
"dev": "tsup --watch",
|
|
66
|
+
"test": "vitest --run --globals",
|
|
67
|
+
"clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist"
|
|
71
68
|
}
|
|
72
|
-
}
|
|
69
|
+
}
|