1.4.7 • Published 4 years ago

paraswap-test v1.4.7

Weekly downloads
-
License
MIT
Repository
github
Last release
4 years ago

ParaSwap SDK


You can use this SDK in order to swap or pay using ERC-20 tokens.

We've built an example dApp that can found here https://github.com/paraswap/demo-dapp

You can also check a live example at: https://paraswap-sdk.netlify.com


API docs are available here :

https://paraswapapi.docs.apiary.io

To use ParaSwap SDK :

Install the lib using npm or yarn

yarn install 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:
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 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 payTo = '0x8B4e846c90a2521F0D2733EaCb56760209EAd51A'; // Useful in case of a payment
const referrer = 'my-company-or-nick-name';

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

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

1.4.7

4 years ago

1.4.6

4 years ago

1.4.5

4 years ago

1.4.4

4 years ago

1.4.3

4 years ago

1.4.2

4 years ago

1.4.1

4 years ago

1.4.0

4 years ago