Alluo Explained
Search
⌃K

Withdraw process with the Liquidity Handler

Example: A walkthrough of a withdrawal from IbAlluoUSD to USDC

Step 1: User calls withdraw with 100 USDC and withdraw is called from the handler to send funds directly to the user.
/// @notice Withdraws accuratel
/// @dev When called, immediately check for new interest index. Then find the adjusted amount in IbAlluo tokens
/// Then burn appropriate amount of IbAlluo tokens to receive asset token
/// @param _targetToken Asset token
/// @param _amount Amount (parsed 10**18) in asset value
​
function withdrawTo(
address _recipient,
address _targetToken,
uint256 _amount
) public {
updateRatio();
uint256 adjustedAmount = (_amount * multiplier) / growingRatio;
_burn(_msgSender(), adjustedAmount);
ILiquidityHandler handler = ILiquidityHandler(liquidityHandler);
if (supportedTokens.contains(_targetToken) == false) {
(address liquidToken,) = ILiquidityHandler(liquidityHandler).getAdapterCoreTokensFromIbAlluo(address(this));
// This just is used to revert if there is no active route.
require(IExchange(exchangeAddress).buildRoute(liquidToken, _targetToken).length > 0, "!Supported");
handler.withdraw(
_recipient,
liquidToken,
_amount,
_targetToken
);
} else {
handler.withdraw(
_recipient,
_targetToken,
_amount
);
}
​
emit TransferAssetValue(_msgSender(), address(0), adjustedAmount, _amount, growingRatio);
emit BurnedForWithdraw(_msgSender(), adjustedAmount);
}

Step 2: When withdraw is called for 100 USDC, carry out checks and either withdraw funds directly or add the user to queue.

  1. 1.
    If the buffer can be used to meet the withdrawal, simply call withdraw on the adapter to send 100 USDC tokens directly to the user.
  2. 2.
    If there is not enough funds in the buffer OR there is an existing queue of withdrawals, add the user to the queue until there are enough funds to meet it.
LiquidityHandler.sol
1
/** @notice Called by ibAlluo, withdraws tokens from the adapter.
2
* @dev Attempt to withdraw. If there are insufficient funds, you are added to the queue.
3
** @param _user Address of depositor
4
** @param _token Address of token (USDC, DAI, USDT...)
5
** @param _amount Amount of tokens in 10**18
6
*/
7
function withdraw(
8
address _user,
9
address _token,
10
uint256 _amount
11
) external whenNotPaused onlyRole(DEFAULT_ADMIN_ROLE) {
12
uint256 inAdapter = getAdapterAmount(msg.sender);
13
​
14
WithdrawalSystem storage withdrawalSystem = ibAlluoToWithdrawalSystems[
15
msg.sender
16
];
17
if (inAdapter >= _amount && withdrawalSystem.totalWithdrawalAmount == 0) {
18
uint256 adapterId = ibAlluoToAdapterId.get(msg.sender);
19
address adapter = adapterIdsToAdapterInfo[adapterId].adapterAddress;
20
IHandlerAdapter(adapter).withdraw(_user, _token, _amount);
21
emit WithdrawalSatisfied(
22
msg.sender,
23
_user,
24
_token,
25
_amount,
26
0,
27
block.timestamp
28
);
29
} else {
30
uint256 lastWithdrawalRequest = withdrawalSystem
31
.lastWithdrawalRequest;
32
withdrawalSystem.lastWithdrawalRequest++;
33
withdrawalSystem.withdrawals[
34
lastWithdrawalRequest + 1
35
] = Withdrawal({
36
user: _user,
37
token: _token,
38
amount: _amount,
39
time: block.timestamp
40
});
41
withdrawalSystem.totalWithdrawalAmount += _amount;
42
emit AddedToQueue(
43
msg.sender,
44
_user,
45
_token,
46
_amount,
47
lastWithdrawalRequest + 1,
48
block.timestamp
49
);
50
}
51
}
52
​

Step 2.5: If the user wanted a withdrawal in a token not supported by the IbAlluo (i.e WETH for IbAlluoUSD), carry out the withdrawal and do a swap through the Alluo Exchange before sending to the user

1
//// Same function as above but overload for case when you want to withdraw in a different token native to an ibAlluo.
2
// For example: Withdraw USDC from ibAlluoEth.
3
function withdraw(
4
address _user,
5
address _token,
6
uint256 _amount,
7
address _outputToken
8
) external whenNotPaused onlyRole(DEFAULT_ADMIN_ROLE) {
9
uint256 inAdapter = getAdapterAmount(msg.sender);
10
​
11
WithdrawalSystem storage withdrawalSystem = ibAlluoToWithdrawalSystems[
12
msg.sender
13
];
14
if (
15
inAdapter >= _amount && withdrawalSystem.totalWithdrawalAmount == 0
16
) {
17
uint256 adapterId = ibAlluoToAdapterId.get(msg.sender);
18
address adapter = adapterIdsToAdapterInfo[adapterId].adapterAddress;
19
if (_token != _outputToken) {
20
IHandlerAdapter(adapter).withdraw(address(this), _token, _amount);
21
_withdrawThroughExchange(_token, _outputToken, _amount, _user);
22
} else {
23
IHandlerAdapter(adapter).withdraw(_user, _token, _amount);
24
}
25
emit WithdrawalSatisfied(
26
msg.sender,
27
_user,
28
_token,
29
_amount,
30
0,
31
block.timestamp
32
);
33
} else {
34
require(
35
_token == _outputToken,
36
"Handler: Only supported tokens"
37
);
38
uint256 lastWithdrawalRequest = withdrawalSystem
39
.lastWithdrawalRequest;
40
withdrawalSystem.lastWithdrawalRequest++;
41
withdrawalSystem.withdrawals[
42
lastWithdrawalRequest + 1
43
] = Withdrawal({
44
user: _user,
45
token: _token,
46
amount: _amount,
47
time: block.timestamp
48
});
49
withdrawalSystem.totalWithdrawalAmount += _amount;
50
emit AddedToQueue(
51
msg.sender,
52
_user,
53
_token,
54
_amount,
55
lastWithdrawalRequest + 1,
56
block.timestamp
57
);
58
}
59
}
60
​
61
// Here, we are directly swapping tokens and then sending these tokens
62
function _withdrawThroughExchange(
63
address _inputToken,
64
address _targetToken,
65
uint256 _amount18,
66
address _user
67
) internal {
68
uint256 amountinInputTokens = (_amount18 *
69
10**ERC20Upgradeable(_inputToken).decimals()) / 10**18;
70
IERC20Upgradeable(_inputToken).safeIncreaseAllowance(
71
exchangeAddress,
72
amountinInputTokens
73
);
74
uint256 amountinTargetTokens = IExchange(exchangeAddress).exchange(
75
_inputToken,
76
_targetToken,
77
amountinInputTokens,
78
0
79
);
80
IERC20Upgradeable(_targetToken).safeTransfer(_user, amountinTargetTokens);
81
}
Note, if there is insufficient funds in the buffer and the user wanted to withdraw in a non-supported token (i.e WETH in IbAlluoUSD withdrawals), instead of being added to the queue, the transaction will revert with the require block in line 34 in the code snippet above. This is because of fluctuating token prices over time and meeting withdrawals in a non native token at a later date may result in users receiving significantly less tokens than they initially expected.

Step 3: When withdraw is called on the adapter, it sends the tokens directly to the user.

USDCurveAdapter.sol
1
/// @notice When called by liquidity handler, withdraws funds from liquidity pool
2
/// @dev It checks against arbitragers attempting to exploit spreads in stablecoins.
3
/// @param _user Recipient address
4
/// @param _token Deposit token address (eg. USDC)
5
/// @param _amount Amount to be withdrawn in 10*18
6
function withdraw (address _user, address _token, uint256 _amount ) external onlyRole(DEFAULT_ADMIN_ROLE) {
7
8
uint256[3] memory amounts;
9
address liquidToken = ICurvePoolUSD(curvePool).underlying_coins(liquidTokenIndex);
10
uint256 amount = _amount / 10**(18 - IERC20Metadata(liquidToken).decimals());
11
amounts[liquidTokenIndex] = amount;
12
13
if(_token == liquidToken){
14
ICurvePoolUSD(curvePool).remove_liquidity_imbalance(
15
amounts,
16
_amount * (10000 + slippage) / 10000,
17
true
18
);
19
IERC20(_token).safeTransfer(_user, amount);
20
}
21
else{
22
// We want to be save agains arbitragers so at any withraw contract checks
23
// how much will be burned curveLp by withrawing this amount in token with most liquidity
24
// and passes this burned amount to get tokens
25
uint256 toBurn = ICurvePoolUSD(curvePool).calc_token_amount(amounts, false);
26
uint256 minAmountOut = _amount / 10**(18 - IERC20Metadata(_token).decimals());
27
uint256 toUser = ICurvePoolUSD(curvePool).remove_liquidity_one_coin(
28
toBurn,
29
int128(indexes[_token]),
30
minAmountOut * (10000 - slippage) / 10000,
31
true
32
);
33
IERC20(_token).safeTransfer(_user, toUser);
34
}
35
}
36
​