Zap Out

Construct a Sushiswap LP Zap Out Transaction with the Transactions API

Overview

Ensure you have read the Zapper API section for a brief overview of the API types and to acquire an API key. This Zap Out guide uses the Zapper Transactions API.

The Sushiswap Zap Out removes liquidity from Sushiswap pools. The latest currently deployed Sushiswap Zap Out can be found here. The Zap accepts Sushiswap LP Tokens and converts them into ETH, any arbitrary ERC20 token, or the pair of underling tokens. Any swaps that are required are done so in a manner such that the output is maximized with as little slippage as possible. This is done via intelligent pathing to ensure that the exchange is routed optimally, leveraging PMMs if necessary.

This guide is also applicable to Uniswap V2. Simply substitute references ofsushiswap withuniswap.For example, in the Get Zap Allowance request, .../zap-out/sushiswap/approval-state/... can be replaced with .../zap-out/uniswap/approval-state/...

*For the Get Zap Out Transaction query, replacesushiswapwithuniswap-v2

Check Zap Allowance

Before Zapping Out, check that the Zap contract has approval to spend the LP tokens you are sending.

get
Get Approval State

https://api.zapper.fi/v1/zap-out/sushiswap/approval-state
This endpoint checks if the Zap has approval to interact with the ownerAddress' LP tokens. If isApproved is false, the Zap (i.e. the spenderAddress) has less than the amount property in allowance and will require an approval transaction before proceeding. The amount property represents the total LP balance of the tokenAddress held by the ownerAddress
Request
Response
Request
Query Parameters
api_key
required
string
Authentication token
ownerAddress
required
string
Address of the owner
sellTokenAddress
required
string
Address of the LP token
gasPrice
required
string
Gas price in wei
network
optional
string
Retrieve approval info for a specific network
Response
200: OK
{
"spenderAddress": "0x69090d6968B12b79CD403Ee33E871284dC7E92F6",
"tokenAddress": "0xa478c2975ab1ea89e8196811f51a7b7ade33eb11",
"ownerAddress": "0x2a4...",
"allowance": "0",
"amount": "423802387456",
"isApproved": false
}
400: Bad Request
Error response from approval-state endpoint
{
statusCode: 400,
message: [
"A helpful error message"
],
error: "Bad Request"
}

Set Allowance if Needed

If isApprove is false, this endpoint can be used to assemble an approval transaction to submit to the Ethereum network for the LP token. This transaction will grant the Sushiswap Zap Out contract (i.e. the spenderAddress from the previous step) an allowance, enabling it to transfer tokens from the ownerAddress to the Zap contract. This step is required in order for the Zap to proceed if it does not yet have an allowance.

get
Get Approval Transaction

https://api.zapper.fi/v1/zap-out/sushiswap/approval-transaction
The allowance granted to the Zap is 79228162514.26(i.e. effectively infinite).
Request
Response
Request
Query Parameters
api_key
required
string
Authentication token
gasPrice
required
string
Gas price in wei
sellTokenAddress
required
string
Address of the LP token
ownerAddress
required
string
Address of the owner
network
optional
string
Retrieve approval transaction for a specific network
Response
200: OK
Successful response from approval-transaction endpoint.
{
"data": "0x095ea7b3000000...",
"to": "0xa478c2975ab1ea89e8196811f51a7b7ade33eb11",
"from": "0x2a4d...",
"gasPrice": "97000001459"
}
400: Bad Request
Error response from approval-transaction endpoint.
{
statusCode: 400,
message: [
"A Helpful error messasge."
],
error: "Bad Request"
}

Zap Out

After the approval step has been completed, this endpoint can be used to create a transaction for the Zap Out. This is the value transaction, which if signed and broadcasted, will transfer LP tokens from the ownerAddress to the Zap contract (the to property in the response). Subsequently, the proceeds of the Zap out (i.e. ETH, tokens, or both) will be transferred to the ownerAddress. If the transaction reverts, the sender will pay for the gas consumed, however, the LP tokens will not be deducted from the ownerAddress.

The slippagePercentage encapsulates the entire atomic transaction and will cause the transaction to revert if it is exceeded. For example, if the slippagePercentage is 3% (0.03) and the Zap requires multiple swaps (exchanges), the transaction will revert if cumulative slippage exceeds 3%. This represents the maximum acceptable slippage and does not imply that the Zap will actually experience this much slippage upon execution.

The poolAddress represents the Sushiswap pool to exit from. This address can be obtained from Zapper's Data API as shown here

The gasPrice recommended by Zapper is returned from Zapper's Gas Price API. Alternatively, you can choose to use any gas price you'd like. The gasPrice should be in WEI

get
Get Zap Out Transaction

https://api.zapper.fi/v1/zap-out/sushiswap/transaction
Recommended slippagePercentage is 0.01 to 0.03 (i.e. 1-3%). The sellAmount should be in the base units of the sell token. For example for 3.14159 Sushiswap LP tokens it should be 3.14159 / 10 ** 18 = 3141590000000000000 If Ether is being received, the toTokenAddress should be the zero address (i.e. 0x0000000000000000000000000000000000000000) shouldSellEntire balance should always be set to true. This flag ensures that the transaction does not revert in cases where the reserves change significantly (i.e. up to the slippagePercentage) due to slow transaction confirmation times.
Request
Response
Request
Query Parameters
api_key
required
string
Authentication Token
slippagePercentage
required
string
Slippage percentage as a decimal value
gasPrice
required
string
Gas price in wei
poolAddress
required
string
Address of the pool contract
shouldSellEntireBalance
required
boolean
Flag to sell entire contract balance
sellAmount
required
string
Amount to sell in base units of the LP token
ownerAddress
required
string
Address of the owner
toTokenAddress
required
string
Address of the token to receive
network
optional
string
Retrieve zap out transaction for a specific network
Response
200: OK
Successful response from transaction endpoint
{
"to": "0xb832cc0e8ed40ae42eddc63d9d07ebaf022994e8",
"from": "0x2a4d...",
"data": "0xbb0abba7000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005b99b82a033ddb700000000000000000000000019d3364a399d251e894ac732651be8b0e4e85001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002737641f7304d7d4e9000000000000000000000000def1c0ded9bec7f1a1670819833240f027b25eff000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000128d9627aa4000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000005b99b82a033ddb7000000000000000000000000000000000000000000000027eb0781bcc6ad968d00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee0000000000000000000000006b175474e89094c44da98b954eedeac495271d0f869584cd000000000000000000000000f4e386b070a18419b5d3af56699f8a438dd18e890000000000000000000000000000000000000000000000bd5d54886f60470580000000000000000000000000000000000000000000000000",
"value": "0",
"gasPrice": "153800000000",
"gas": "316245"
}
400: Bad Request
Error response from transaction endpoint
{
statusCode: 400,
message: [
"A helpful error message."
],
error: "Bad Request"
}

Send It!

With Web3/Ethers

The transaction objects returned from the Get Zap Out Transaction and Get Approval Transaction endpoints are ready to be consumed by web3 or ethers. 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.

With smart contracts

To consume the transaction object in your smart contract, it must have a balance of the poolAddress (i.e. pool tokens) representing the sellAmount. Therefore, because your smart contract holds this balance, it is also the ownerAddress. When assembling the transaction in the Get Zap Out Transaction request, care should be taken to assemble it with these parameters in mind.

A function in a smart contract that consumes a Zap Out transaction might resemble this:

// Zaps out of a Sushiswap pool into ETH or ERC20 Tokens or both using a Zapper Transaction Object
// The contract must have a fallback function to receive ETH proceeds from a Zap Out
function ZapOut(
// The `sellTokenAddress` field from the API response.
address sellToken,
// The `buyTokenAddress` field from the API response.
address buyToken,
// The `to` field from the API response.
address payable ZapContract,
// The `data` field from the API response.
bytes calldata zapCallData
)
external
onlyOwner
returns (uint256 tokensRec)
{
// ...
// Give the Zap an infinite allowance to spend this contract's `sellToken`.
require(sellToken.approve(ZapContract, uint256(-1)));
// Check this contract's initial balance
uint256 initialBalance = buyToken == address(0)
? address(this).balance
: IERC20(buyToken).balanceOf(address(this));
// Call the encoded Zap Out function call on the contract at `ZapContract`,
// NOTE: You should restrict calls to trusted ZapContracts and never tokens!
(bool success,) = ZapContract.call(zapCallData);
require(success, 'Zap Out Failed');
uint256 finlBal = buyToken == address(0)
? address(this).balance
: IERC20(buyToken).balanceOf(address(this));
tokensRec = finlBal.sub(initialBalance);
// ...
}