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
https://api.zapper.fi
/v1/zap-out/sushiswap/approval-state
Get Approval State

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
https://api.zapper.fi
/v1/zap-out/sushiswap/approval-transaction
Get Approval Transaction

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
https://api.zapper.fi
/v1/zap-out/sushiswap/transaction
Get Zap Out Transaction

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:
1
// Zaps out of a Sushiswap pool into ETH or ERC20 Tokens or both using a Zapper Transaction Object
2
// The contract must have a fallback function to receive ETH proceeds from a Zap Out
3
function ZapOut(
4
// The `sellTokenAddress` field from the API response.
5
address sellToken,
6
// The `buyTokenAddress` field from the API response.
7
address buyToken,
8
// The `to` field from the API response.
9
address payable ZapContract,
10
// The `data` field from the API response.
11
bytes calldata zapCallData
12
)
13
external
14
onlyOwner
15
returns (uint256 tokensRec)
16
{
17
// ...
18
19
// Give the Zap an infinite allowance to spend this contract's `sellToken`.
20
require(sellToken.approve(ZapContract, uint256(-1)));
21
// Check this contract's initial balance
22
uint256 initialBalance = buyToken == address(0)
23
? address(this).balance
24
: IERC20(buyToken).balanceOf(address(this));
25
// Call the encoded Zap Out function call on the contract at `ZapContract`,
26
// NOTE: You should restrict calls to trusted ZapContracts and never tokens!
27
(bool success,) = ZapContract.call(zapCallData);
28
require(success, 'Zap Out Failed');
29
uint256 finlBal = buyToken == address(0)
30
? address(this).balance
31
: IERC20(buyToken).balanceOf(address(this));
32
tokensRec = finlBal.sub(initialBalance);
33
34
// ...
35
}
Copied!
Last modified 3mo ago