Exchange Assets
Easily swap between assets through the API

Video Summary

Check Exchange Price

Before exchanging to a different asset, you must first call the price endpoint to determine the price for swapping the assets and to obtain the exchange address in order to check approval state for the token. The allowanceTarget property returned is the contract address that the user must approve their tokens too.
Note: The zero address is used as the token address for protocol native gas tokens i.e.
  • 0x0000000000000000000000000000000000000000 swapping ETH on Ethereum
  • 0x0000000000000000000000000000000000000000 swapping BSC on Bianance SC
  • 0x0000000000000000000000000000000000000000 swapping MATIC on Polygon SC
get
https://api.zapper.fi
/v1/exchange/price
Get Exchange Price

Check Exchange Allowance

Before swapping assets, check that the exchange contract has approval to spend the tokens you are sending.
If a protocol native gas token (ETH on Ethereum or BSC on Binanance SC) is being sold then this step can be skipped as it does not require approval to the exchange contract.
get
https://api.zapper.fi
/v1/approval-state
Get Approval State

Set Allowance if Needed

If isApprove is false and the asset being sold is not a protocol native gas token, then this endpoint can be used to assemble an approval transaction. This transaction will grant the exchange contract (i.e. the spenderAddress from the previous step) an allowance, enabling it to transfer tokens from the ownerAddress to the exchange contract. This step is required in order for the exchange contract to proceed if it does not yet have an allowance.
get
https://api.zapper.fi
/v1​/approval-transaction
Get Approval Transaction

Exchange Asset Transaction

This endpoint should only be used once the user has approved the contract address and is ready to submit a transaction.
get
https://api.zapper.fi
/v1/exchange/quote
Get Exchange Quote

Submit the Transaction

You can use parts from the quote response once you are ready to submit a transaction object like the one below which can be submitted to the blockchain via web3 or ethers.
1
{
2
"data": quote.data,
3
"to": quote.to,
4
"from": "0xfc759...",
5
"gasPrice": quote.gasPrice
6
}
Copied!
After initializing these libraries with a provider capable of signing the transaction (e.g. Metamask), a user can sign and broadcast the transaction in the same familiar way that they interact with any other DeFi protocol. For more details see the web3 documentation on sending transactions.
Last modified 8mo ago