npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

paraswap-sdk

v5.6.0-alpha.0

Published

ParaSwap SDK

Downloads

100

Readme

Accompanion SDK of the ParaSwap API

Refer to the documentation of the ParaSwap API: https://developers.paraswap.network

Features

Versatility: it works with both web3 and ethers and without direct dependency

Canonicity: bring only the functions you actually need

Lightweight: 400B Gzipped for the most minimalistic variant

Installing ParaSwap SDK

yarn add @paraswap/sdk

Using ParaSwap SDK

Import the necessary functions

import { constructSDK, constructAxiosFetcher, constructEthersContractCaller } from '@paraswap/sdk';

Construct the ParaSwap object

const signer = ethers.Wallet.fromMnmemonic('__your_mnemonic__') // or any other signer/provider 
const account = '__signer_address__'

const contractCaller = constructEthersContractCaller(signer, account); // alternatively constructWeb3ContractCaller
const fetcher = constructAxiosFetcher(axios); // alternatively constructFetchFetcher

const paraswap = constructSDK({
  network: 1,
  fetcher,
  contractCaller,
});

To approve ParaSwap contracts to swap a ERC20 token

const txHash = await paraSwap.approveToken(amount, tokenAddress);

To get the rate of a token pair

const srcToken = '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee'; // ETH
const destToken = '0xcAfE001067cDEF266AfB7Eb5A286dCFD277f3dE5'; // PSP
const srcAmount = '1000000000000000000'; //The source amount multiplied by its decimals: 10 ** 18 here
const srcDecimals = 18
const destDecimals = 18

const priceRoute = await paraSwap.getRate(
  {
    srcToken,
    destToken,
    amount,
    srcDecimals,
    destDecimals,
  }
);

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

To build a transaction

const srcToken = '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee';
const srcDecimals = 18
const srcAmount = '1000000000000000000'; // The source amount multiplied by its decimals
const destToken = '0xcAfE001067cDEF266AfB7Eb5A286dCFD277f3dE5';
const destDecimals = 18
const destAmount = priceRoute.destAmount // price route being output of paraSwap.getRate()
const senderAddress = '__sender_address__'; // mandatory
const receiver = '__receiver_address__'; // optional: for swap and transfer
const partnerAddress = '__fee_receiver_address__' // optional: for permission-less monetization
const partnerFeeBps = 50 // optional: fee in base point, for permission-less monetization


const txParams = await paraSwap.buildTx(
  {
    srcAmount,
    srcToken,
    srcDecimals,
    destAmount,
    destToken,
    destDecimals,
    priceRoute,
    senderAddress,
    receiver,
    partnerAddress,
    partnerFeeBps,
  }
);

const transactionResponse = await signer.sendTransaction(txParams);
const transactionReceipt = await transactionResponse.wait();

Bundle Optimization

For bundle-size savvy developers, you can construct a lightweight version of this sdk to bring only the functions you need.

For e.g. for fetching rate and allowance only

import { constructPartialSDK, constructFetchFetcher, constructGetRate, constructGetAllowance } from '@paraswap/sdk';

const fetcher = constructFetchFetcher(window.fetch)

const minParaSwap = constructPartialSDK({
  network: 1,
  fetcher,
}, constructGetRate, constructGetAllowance)

const priceRoute = await minParaSwap.getRate(params)
const allowance = await minParaSwap.getAllowance(userAddress, tokenAddress);

Legacy

The ParaSwap class is exposed with some effort on backward compatibility with previous versions.

import { ParaSwap } from '@paraswap/sdk'
import axios from 'axios'
import Web3 from 'web3'

const web3Provider = new Web3(window.ethereum)
const account = '__user_address__'

const paraswap = new ParaSwap(
  1, 
  undefined, 
  web3Provider, 
  undefined, 
  account, 
  axios
)

By analogy to constructPartialSDK, you can leverage a lightweight version of the sdk for fetching only

import { ParaSwap } from '@paraswap/sdk'

const paraswap = new ParaSwap(
  1, 
  undefined, 
  undefined, 
  undefined, 
  undefined, 
  undefined,
  window.fetch
)

Refer to this README for depecreated documentation for functions usage.