Git Product home page Git Product logo

pancake-swap-sdk's Introduction

Notice

Moved to https://github.com/pancakeswap/pancake-frontend/tree/develop/packages/swap-sdk

Pancakeswap SDK

Forked from the Uniswap SDK.

You can refer to the Uniswap SDK documentation uniswap.org.

Running tests

To run the tests, follow these steps. You must have at least node v10 and yarn installed.

First clone the repository:

git clone https://github.com/pancakeswap/pancake-swap-sdk.git

Move into the pancakeswap-sdk working directory

cd pancakeswap-sdk/

Install dependencies

yarn install

Run tests

yarn test

You should see output like the following:

yarn run v1.22.4
$ tsdx test
 PASS  test/constants.test.ts
 PASS  test/pair.test.ts
 PASS  test/fraction.test.ts
 PASS  test/miscellaneous.test.ts
 PASS  test/entities.test.ts
 PASS  test/trade.test.ts

Test Suites: 1 skipped, 6 passed, 6 of 7 total
Tests:       3 skipped, 82 passed, 85 total
Snapshots:   0 total
Time:        5.091s
Ran all test suites.
✨  Done in 6.61s.

pancake-swap-sdk's People

Contributors

0xjojoex avatar bitofuniverse avatar chef-cheems avatar chefhutch avatar dependabot[bot] avatar moodysalem avatar noahzinsmeister avatar rabbitdoge avatar shaunlwm avatar ugoromi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

pancake-swap-sdk's Issues

The value returned by Price does not match the value displayed on the PancakeSwap swap screen.

Hello.
Here is an example.

  const token0 = await Fetcher.fetchTokenData(56, '0x3019BF2a2eF8040C242C9a4c5c4BD4C81678b2A1', bscProvider, 'GMT');
  const token1 = await Fetcher.fetchTokenData(56, '0xe9e7CEA3DedcA5984780Bafc599bD69ADd087D56', bscProvider, 'BUSD');
  const pair = await Fetcher.fetchPairData(token0, token1, bscProvider);

  console.log(pair.token0Price.toSignificant(6)); // GMT price
  console.log(pair.token1Price.toSignificant(6)); // BUSD price

These GMT and BUSD prices do not match the values shown on the PancakeSwap swap screen.
(The discrepancy is large for an error due to execution timing).
I am assuming that the "GMT value at 1BUSD" and "BUSD value at 1GMT" are calculated respectively, is that wrong?
If I am wrong, what should I use to display the exchange rate similar to the PancakeSwap swap screen?

InsufficentInputAmountError when trying to get execution price of trade

const trade2 = async (xTknAddr, xTknTick) => {
    // problem function for some reason
    const chainId = ChainId.MAINNET;
    const xTkn = await Fetcher.fetchTokenData(
        chainId,
        ethers.utils.getAddress(xTknAddr),
        provider
    );
    const weth = WETH[chainId];
    const pair = await Fetcher.fetchPairData(xTkn, weth, provider);
    const route = new Route([pair], xTkn);
    const trade = new Trade(
        route,
        new TokenAmount(xTkn, "1"), // trying to put 1 input token, I have tried to put the amount in wei but to no avail, the answers are very wrong
        TradeType.EXACT_INPUT
    );
    console.log(
        `1 ${xTknTick} = ${trade.executionPrice.toSignificant(
            6
        )} BNB (onexTknforWBNB2)`
    );
    return trade.executionPrice.toSignificant(6);
};

trade2("0x0e09fabb73bd3ade0a17ecc321fd13a19e81ce82", "cake"); // this doesnt work
trade2("0x7ad7242a99f21aa543f9650a56d141c57e4f6081", "jade"); // this does work

when trying the Jade trade I get the answer as expected yet when trying the cake trade I get InsufficientInputAmountError

it was my understanding that by using the fetchTokenData method I could input 1 as the token amount and it would figure itself out. this seems to be the case when working with Jade but Cake is not giving me the same luck.

Any help would be appreciated thanks.

Pair.getAddress() returns wrong address on testnet

On testnet, when calling Pair.getAddress(), it seems to be returning a new/incorrect address even though the pair already exists.

So far, this has caused me issues when doing

new Pair()
Fetcher.fetchPairData()

Tests are failing

Tests are failing after a successfull build:

`~/pancake-swap-sdk # yarn test
yarn run v1.22.5
$ tsdx test
FAIL test/constants.test.ts
● constants › INIT_CODE_HASH › matches computed bytecode hash

expect(received).toEqual(expected) // deep equality

Expected: "0x00fb7f630766e6a796048ea87d01acd3068e8ff67d078148a3fa3f4a84f69bd5"
Received: "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470"

  11 |   describe('INIT_CODE_HASH', () => {
  12 |     it('matches computed bytecode hash', () => {
> 13 |       expect(COMPUTED_INIT_CODE_HASH).toEqual(INIT_CODE_HASH)
     |                                       ^
  14 |     })
  15 |   })
  16 | })

  at Object.<anonymous> (test/constants.test.ts:13:39)

PASS test/route.test.ts
PASS test/router.test.ts
PASS test/token.test.ts
PASS test/fraction.test.ts
PASS test/miscellaneous.test.ts
PASS test/trade.test.ts
FAIL test/pair.test.ts
● Pair › #getAddress › returns the correct address

expect(received).toEqual(expected) // deep equality

Expected: "0xAE461cA67B15dc8dc81CE7615e0320dA1A9aB8D5"
Received: "0xEeC9cA661B7876d4e223C95D6f3b856238dCCDCb"

  15 |   describe('#getAddress', () => {
  16 |     it('returns the correct address', () => {
> 17 |       expect(Pair.getAddress(USDC, DAI)).toEqual('0xAE461cA67B15dc8dc81CE7615e0320dA1A9aB8D5')
     |                                          ^
  18 |     })
  19 |   })
  20 |

  at Object.<anonymous> (test/pair.test.ts:17:42)

FAIL test/entities.test.ts
● entities › decimals permutation: 0,0,0 › Trade › TradeType.EXACT_INPUT

expect(received).toEqual(expected) // deep equality

- Expected
+ Received

@@ -9,12 +9,12 @@
    "denominator": JSBI [
      -1486618624,
      232830643,
    ],
    "numerator": JSBI [
-     -996157254,
-     387080459,
+     134165932,
+     387242296,
    ],
    "token": Token {
      "address": "0xaE8E19eFB41e7b96815649A6a60785e1fbA84C1e",
      "chainId": 97,
      "decimals": 18,

  116 |           expect(trade.tradeType).toEqual(TradeType.EXACT_INPUT)
  117 |           expect(trade.inputAmount).toEqual(inputAmount)
> 118 |           expect(trade.outputAmount).toEqual(expectedOutputAmount)
      |                                      ^
  119 |
  120 |           expect(trade.executionPrice.toSignificant(18)).toEqual('1.66249791562447891')
  121 |           expect(trade.executionPrice.invert().toSignificant(18)).toEqual('0.601504513540621866')

  at Object.<anonymous> (test/entities.test.ts:118:38)

● entities › decimals permutation: 0,9,18 › Trade › TradeType.EXACT_INPUT

expect(received).toEqual(expected) // deep equality

- Expected
+ Received

@@ -9,12 +9,12 @@
    "denominator": JSBI [
      -1486618624,
      232830643,
    ],
    "numerator": JSBI [
-     -996157254,
-     387080459,
+     134165932,
+     387242296,
    ],
    "token": Token {
      "address": "0xaE8E19eFB41e7b96815649A6a60785e1fbA84C1e",
      "chainId": 97,
      "decimals": 18,

  116 |           expect(trade.tradeType).toEqual(TradeType.EXACT_INPUT)
  117 |           expect(trade.inputAmount).toEqual(inputAmount)
> 118 |           expect(trade.outputAmount).toEqual(expectedOutputAmount)
      |                                      ^
  119 |
  120 |           expect(trade.executionPrice.toSignificant(18)).toEqual('1.66249791562447891')
  121 |           expect(trade.executionPrice.invert().toSignificant(18)).toEqual('0.601504513540621866')

  at Object.<anonymous> (test/entities.test.ts:118:38)

● entities › decimals permutation: 0,9,18 › Trade › TradeType.EXACT_OUTPUT

expect(received).toEqual(expected) // deep equality

- Expected
+ Received

@@ -8,11 +8,11 @@
    },
    "denominator": JSBI [
      1000000000,
    ],
    "numerator": JSBI [
-     1000000000,
+     999498747,
    ],
    "token": Token {
      "address": "0x0000000000000000000000000000000000000002",
      "chainId": 97,
      "decimals": 9,

  136 |           expect(trade.tradeType).toEqual(TradeType.EXACT_OUTPUT)
  137 |           expect(trade.outputAmount).toEqual(outputAmount)
> 138 |           expect(trade.inputAmount).toEqual(expectedInputAmount)
      |                                     ^
  139 |
  140 |           expect(trade.executionPrice.toSignificant(18)).toEqual('1.66249791562447891')
  141 |           expect(trade.executionPrice.invert().toSignificant(18)).toEqual('0.601504513540621866')

  at Object.<anonymous> (test/entities.test.ts:138:37)

● entities › decimals permutation: 0,9,18 › Trade › minimum TradeType.EXACT_INPUT

expect(received).toEqual(expected) // deep equality

Expected: "0.300000099400899902"
Received: "0.2500001008007499"

  167 |             const trade = new Trade(route, outputAmount, TradeType.EXACT_INPUT)
  168 |
> 169 |             expect(trade.priceImpact.toSignificant(18)).toEqual(
      |                                                         ^
  170 |               tokens[1].decimals === 9 ? '0.300000099400899902' : '0.3000000000000001'
  171 |             )
  172 |           }

  at Object.<anonymous> (test/entities.test.ts:169:57)

● entities › decimals permutation: 18,18,18 › Trade › TradeType.EXACT_INPUT

expect(received).toEqual(expected) // deep equality

- Expected
+ Received

@@ -9,12 +9,12 @@
    "denominator": JSBI [
      -1486618624,
      232830643,
    ],
    "numerator": JSBI [
-     -996157254,
-     387080459,
+     134165932,
+     387242296,
    ],
    "token": Token {
      "address": "0xaE8E19eFB41e7b96815649A6a60785e1fbA84C1e",
      "chainId": 97,
      "decimals": 18,

  116 |           expect(trade.tradeType).toEqual(TradeType.EXACT_INPUT)
  117 |           expect(trade.inputAmount).toEqual(inputAmount)
> 118 |           expect(trade.outputAmount).toEqual(expectedOutputAmount)
      |                                      ^
  119 |
  120 |           expect(trade.executionPrice.toSignificant(18)).toEqual('1.66249791562447891')
  121 |           expect(trade.executionPrice.invert().toSignificant(18)).toEqual('0.601504513540621866')

  at Object.<anonymous> (test/entities.test.ts:118:38)

● entities › decimals permutation: 18,18,18 › Trade › TradeType.EXACT_OUTPUT

expect(received).toEqual(expected) // deep equality

- Expected
+ Received

@@ -9,12 +9,12 @@
    "denominator": JSBI [
      -1486618624,
      232830643,
    ],
    "numerator": JSBI [
-     -1486618624,
-     232830643,
+     -1871236432,
+     232713936,
    ],
    "token": Token {
      "address": "0x0000000000000000000000000000000000000002",
      "chainId": 97,
      "decimals": 18,

  136 |           expect(trade.tradeType).toEqual(TradeType.EXACT_OUTPUT)
  137 |           expect(trade.outputAmount).toEqual(outputAmount)
> 138 |           expect(trade.inputAmount).toEqual(expectedInputAmount)
      |                                     ^
  139 |
  140 |           expect(trade.executionPrice.toSignificant(18)).toEqual('1.66249791562447891')
  141 |           expect(trade.executionPrice.invert().toSignificant(18)).toEqual('0.601504513540621866')

  at Object.<anonymous> (test/entities.test.ts:138:37)

Test Suites: 3 failed, 1 skipped, 6 passed, 9 of 10 total
Tests: 8 failed, 3 skipped, 117 passed, 128 total
Snapshots: 0 total
Time: 1.878s, estimated 13s
Ran all test suites.
error Command failed with exit code 1`

npm released code not match current source

in released npm (bad)

  "dependencies": {
    "@pancakeswap-libs/pancake-swap-core": "^0.1.0",
    "big.js": "^5.2.2",
    "decimal.js-light": "^2.5.0",
    "jsbi": "^3.1.1",
    "tiny-invariant": "^1.1.0",
    "tiny-warning": "^1.0.3",
    "toformat": "^2.0.0"
  }

current source (good)

  "dependencies": {
    "big.js": "^5.2.2",
    "decimal.js-light": "^2.5.0",
    "jsbi": "^3.1.4",
    "tiny-invariant": "^1.1.0",
    "tiny-warning": "^1.0.3",
    "toformat": "^2.0.0"
  },

and "@pancakeswap-libs/pancake-swap-core": "^0.1.0", currently depend to @pancakeswap-libs/pancake-swap-core
which has truffle that not compatible with node 16 and lead to build failed apparently.

TLDR

Please release current source and bump version.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.