Refer to the documentation of the ParaSwap API: https://developers.paraswap.network
Features
Versatility: works with both web3 and ethers without direct dependency
Canonical: bring only the functions you actually need
Lightweight: 400B Gzipped for the minimal variant
Installing ParaSwap SDK
yarnadd@paraswap/sdk
Using ParaSwap SDK
There are multiple ways to use ParaSwap SDK, ranging from a simple construct-and-use approach to a fully composable bring what you need approach which allows for advanced tree-shaking and minimizes bundle size.
Simple SDK
Can be created by providing chainId and either axios or window.fetch (or alternative fetch implementation). The resulting SDK will be able to use all methods that query the API.
constsigner=ethers.Wallet.fromMnmemonic('__your_mnemonic__'); // or any other signer/provider constaccount='__signer_address__';constcontractCaller=constructEthersContractCaller({ ethersProviderOrSigner: signer, EthersContract:ethers.Contract,}, account); // alternatively constructWeb3ContractCallerconstfetcher=constructAxiosFetcher(axios); // alternatively constructFetchFetcherconstparaswap=constructSDK({ chainId:1, fetcher, contractCaller,});
To approve ParaSwap contracts to swap an ERC20 token
// if created with constructEthersContractCallerconstcontractTx:ContractTransaction=awaitparaSwap.approveToken(amount, tokenAddress);consttxReceipt=awaitcontractTx.wait();// if created with constructWeb3ContractCallerconstunpromiEvent:Web3UnpromiEvent=awaitparaSwap.approveToken(amount, tokenAddress);consttxReceipt=awaitnewPromise<Web3TransactionReceipt>((resolve, reject) => {unpromiEvent.once('receipt', resolve);unpromiEvent.once('error', reject);})
Where priceRoute contains the rate and the distribution among exchanges, checkout the OptimalRates type for more details.
To build a transaction
constsrcToken='0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee';constsrcDecimals=18;constsrcAmount='1000000000000000000'; // The source amount multiplied by its decimalsconstdestToken='0xcAfE001067cDEF266AfB7Eb5A286dCFD277f3dE5';constdestDecimals=18;constdestAmount=priceRoute.destAmount; // price route being output of paraSwap.getRate()constsenderAddress='__sender_address__'; // mandatoryconstreceiver='__receiver_address__'; // optional: for swap and transferconstpartnerAddress='__fee_receiver_address__'; // optional: for permission-less monetizationconstpartnerFeeBps=50; // optional: fee in base point, for permission-less monetizationconsttxParams=awaitparaSwap.buildTx( { srcAmount, srcToken, srcDecimals, destAmount, destToken, destDecimals, priceRoute, senderAddress, receiver, partnerAddress, partnerFeeBps, });consttransactionResponse=awaitsigner.sendTransaction(txParams);consttransactionReceipt=awaittransactionResponse.wait();
To sign a Limit Order
import { constructAxiosFetcher, constructBuildLimitOrder, constructEthersContractCaller, constructPartialSDK, constructSignLimitOrder, constructSimpleSDK} from'@paraswap/sdk'import axios from'axios'import { ethers } from'ethers'constsigner=ethers.Wallet.fromMnemonic('__your_mnemonic__') // or any other signer/providerconstaccount=signer.addressconstfetcher=constructAxiosFetcher(axios)constcontractCaller=constructEthersContractCaller( { ethersProviderOrSigner: signer, EthersContract:ethers.Contract }, account)constparaSwapLimitOrderSDK=constructPartialSDK( { chainId:1, fetcher, contractCaller }, constructBuildLimitOrder, constructSignLimitOrder)constorder= { maker: account,/** actual user taker which will go into nonceAndMeta */ taker:'0x6b175474e89094c44da98b954eedeac495271d0f', expiry:0, makerAsset:'0xcafe001067cdef266afb7eb5a286dcfd277f3de5', takerAsset:'0xcafe001067cdef266afb7eb5a286dcfd277f3de5', makerAmount:'1000000000000', takerAmount:'1000000000000',/** (optional) contract executor (Augustus or similar) that is allowed to execute the order as Order.taker */ contractTaker:'0x2260fac5e5542a773aa44fbcfedf7c193bc2c599'}constsignableOrderData=awaitparaSwapLimitOrderSDK.buildLimitOrder(order)constsignature=awaitparaSwapLimitOrderSDK.signLimitOrder(signableOrderData
Playground
Interact with the ParaSwap SDK in a CodeSandbox playground here
Bundle Optimization
For bundle-size savvy developers, you can construct a lightweight version of the SDK and bring only the functions you need.
Refer to SDK API documentation for detailed documentation on the methods provided in this SDK.
Tests
To run yarn test it is necessary to provide PROVIDER_URL=<mainnet_rpc_url> environment variable. If it is necessary to run tests against a different API endpoint, provide API_URL=url_to_API environment variable.