Liquidation
Swaap’s Maker (v2) LP tokens can be fully redeemed for their underlying assets according to the following logic.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
interface ISafeguardPool {
function getPoolId() external view returns (bytes32);
function balanceOf(address account) external view returns (uint256);
enum ExitKind {
EXACT_BPT_IN_FOR_TOKENS_OUT,
BPT_IN_FOR_EXACT_TOKENS_OUT
}
}
interface IVault {
function exitPool(
bytes32 poolId,
address sender,
address payable recipient,
ExitPoolRequest memory request
) external;
struct ExitPoolRequest {
address[] assets;
uint256[] minAmountsOut;
bytes userData;
bool toInternalBalance;
}
function getPoolTokens(
bytes32 poolId
)
external
view
returns (
address[] memory tokens,
uint256[] memory balances,
uint256 lastChangeBlock
);
}
contract ExitSwaap {
// Vault address on all networks EXCEPT Base & BSC: 0xd315a9c38ec871068fec378e4ce78af528c76293
// Vault address on the Base & BSC networks: 0x03c01acae3d0173a93d819efdc832c7c4f153b06
IVault public immutable VAULT;
constructor(address _vault) {
VAULT = IVault(_vault);
}
function exitAll(address pool) external {
// Get the pool ID
bytes32 poolId = ISafeguardPool(pool).getPoolId();
// Get the balance of the user (in this example the contract)
uint256 balance = ISafeguardPool(pool).balanceOf(address(this));
// Get the underlying pool tokens
(address[] memory tokens, , ) = IVault(VAULT).getPoolTokens(poolId);
// Prepare the minAmountsOut array
uint256[] memory minAmountsOut = new uint256[](tokens.length);
// userData contains the type of exit and the amount of shares to exit/burn
bytes memory userData = abi.encode(
ISafeguardPool.ExitKind.EXACT_BPT_IN_FOR_TOKENS_OUT,
balance
);
// Prepare the exit request
IVault.ExitPoolRequest memory request = IVault.ExitPoolRequest({
assets: tokens,
minAmountsOut: minAmountsOut,
userData: userData,
toInternalBalance: false
});
// Exit the pool
IVault(VAULT).exitPool(
poolId,
address(this), // sender
payable(address(this)), // receiver
request
);
}
}