5.2.0 • Published 2 years ago

paraswap v5.2.0

Weekly downloads
164
License
MIT
Repository
github
Last release
2 years ago

ParaSwap SDK


API docs are available here :

https://developers.paraswap.network

To use ParaSwap SDK :

Install the lib using npm or yarn

yarn add paraswap
Then on a Javascript file:
const { ParaSwap } = require('paraswap');
const paraSwap = new ParaSwap();

ES6 or TypeScript

import { ParaSwap } from 'paraswap';
const paraSwap = new ParaSwap();
To retrieve the list all available tokens:
const tokens = await paraSwap.getTokens();
To get the rate of a token pair using the API:
const srcToken = '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee'; // ETH
const destToken = '0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359'; // DAI
const srcAmount = '1000000000000000000'; //The source amount multiplied by its decimals: 10 ** 18 here

const priceRoute: OptimalRates = await paraSwap.getRate(
  srcToken,
  destToken,
  srcAmount,
);

Where priceRoute contains the rate and the distribution among exchanges, checkout the OptimalRates type for more details.

To get the rate of a token pair using the Price Feed Contract:

This can be used for trustless integrations, the

const paraswapFeed = new ParaswapFeed(1);
const priceRoute: OptimalRates = await paraswapFeed.getRate(
  srcToken,
  destToken,
  srcAmount,
);

This is a schema that describes the data flow from price query to executing a Swap:

Also available at https://paraswap-achirecture.netlify.com

To get the allowance of an ERC20
const paraSwap = new ParaSwap().setWeb3Provider(web3Provider);

const allowance = await paraSwap.getAllowance(userAddress, tokenAddress);
To approve an ERC20
const paraSwap = new ParaSwap().setWeb3Provider(web3Provider);

const txHash = await paraSwap.approveToken(amount, userAddress, tokenAddress);
To build and sign a transaction
const srcToken = '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee';
const destToken = '0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359';
const srcAmount = '1000000000000000000'; //The source amount multiplied by its decimals
const senderAddress = '0xfceA770875E7e6f25E33CEa5188d12Ef234606b4';
const receiver = '0x8B4e846c90a2521F0D2733EaCb56760209EAd51A'; // Useful in case of swap and transfer
const referrer = 'my-company-or-nick-name';

const txParams = await paraSwap.buildTx(
  srcToken,
  destToken,
  srcAmount,
  destAmount,
  priceRoute,
  senderAddress,
  referrer,
  receiver,
);

web3.eth.sendTransaction(
  txParams,
  async (err: Error, transactionHash: string) => {
    if (err) {
      return this.setState({ error: err.toString(), loading: false });
    }
    console.log('transactionHash', transactionHash);
  },
);

To run the example locally:

Created an .env file with these 2 env variables:

PROVIDER_URL=YOUR_PROVIDRER_URL_OR_INFURA_URL
NODE_ENV=production

run

yarn install paraswap

For local developement you can run

yarn dev

For production build:

yarn build

Which will generate a production build on "dist" folder

5.2.0

2 years ago

5.1.0

2 years ago

5.0.1

3 years ago

5.0.0

3 years ago

5.0.0-alpha.1

3 years ago

5.0.0-alpha.0

3 years ago

4.28.1

3 years ago

4.28.0

3 years ago

4.27.0

3 years ago

4.26.0

3 years ago

4.25.0

3 years ago

4.24.0

3 years ago

4.23.0

3 years ago

4.22.0

3 years ago

4.21.0

3 years ago

4.20.0

3 years ago

4.18.0

3 years ago

4.19.0

3 years ago

4.19.1

3 years ago

4.17.0

3 years ago

4.16.0

3 years ago

4.15.0

3 years ago

4.14.1

3 years ago

4.14.0

3 years ago

4.13.3

3 years ago

4.13.4

3 years ago

4.13.2

3 years ago

4.13.1

3 years ago

4.9.0

3 years ago

4.4.0

3 years ago

4.10.0

3 years ago

4.8.1

3 years ago

4.8.0

3 years ago

4.8.2

3 years ago

4.7.0

3 years ago

4.3.0

3 years ago

4.13.0

3 years ago

4.6.0

3 years ago

4.2.0

3 years ago

4.12.0

3 years ago

4.5.0

3 years ago

4.1.4

3 years ago

4.1.5

3 years ago

4.11.0

3 years ago

4.1.3

3 years ago

4.1.2

3 years ago

4.1.1

3 years ago

4.1.0

3 years ago

4.0.2

3 years ago

4.0.1

3 years ago

4.0.0

3 years ago

3.2.1

3 years ago

3.2.0

3 years ago

3.1.1

3 years ago

3.1.0

3 years ago

3.0.4

3 years ago

3.0.3

3 years ago

3.0.2

3 years ago

3.0.1

3 years ago

3.0.0

3 years ago

2.7.9

3 years ago

2.7.8

3 years ago

2.7.7

3 years ago

2.7.6

3 years ago

2.7.5

3 years ago

2.7.4

3 years ago

2.7.3

3 years ago

2.7.2

3 years ago

2.7.1

3 years ago

2.7.0

3 years ago

2.6.0

3 years ago

2.5.10

3 years ago

2.5.9

3 years ago

2.5.8

3 years ago

2.5.7

3 years ago

2.5.6

3 years ago

2.5.5

3 years ago

2.5.4

4 years ago

2.5.3

4 years ago

2.5.2

4 years ago

2.5.1

4 years ago

2.4.1

4 years ago

2.4.0

4 years ago

2.3.5

4 years ago

2.3.4

4 years ago

2.3.3

4 years ago

2.3.0

4 years ago

2.3.1

4 years ago

2.2.2

4 years ago

2.2.1

4 years ago

2.2.0

4 years ago

2.1.2

4 years ago

2.1.1

4 years ago

2.1.3

4 years ago

2.1.0

4 years ago

2.0.9

4 years ago

2.0.8

4 years ago

2.0.7

4 years ago

2.0.5

4 years ago

2.0.4

4 years ago

2.0.3

4 years ago

2.0.2

4 years ago

2.0.1

4 years ago

2.0.0

4 years ago

1.3.2

4 years ago

1.3.1

4 years ago

1.2.2

4 years ago

1.2.1

4 years ago

1.1.8

4 years ago

1.1.7

4 years ago

1.1.6

4 years ago

1.1.5

4 years ago

1.1.4

4 years ago

1.1.3

4 years ago

1.0.11

4 years ago

1.0.10

5 years ago

1.0.9

5 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago