bitpro가 원문에서 글자만 모은 내용이에요. 표·그림·수식은 빠지거나 깨질 수 있어요. 전체 75쪽 중 24쪽
er, self, uamount)
else:
assert_modifiable(ERC20(self.underlying_coins[i])\
.transferFrom(msg.sender, self, uamount))
# Mint if needed
ERC20(self.underlying_coins[i]).approve(self.coins[i], uamount)
yERC20(self.coins[i]).deposit(uamount)
amounts[i] = yERC20(self.coins[i]).balanceOf(self)
ERC20(self.coins[i]).approve(self.curve, amounts[i])
Curve(self.curve).add_liquidity(amounts, min_mint_amount)
tokens: uint256 = ERC20(self.token).balanceOf(self)
assert_modifiable(ERC20(self.token).transfer(msg.sender, tokens))
```
```vyper
@external
@nonreentrant('lock')
def add_liquidity(amounts: uint256[N_COINS], min_mint_amount: uint256) -> uint256:
"""
@notice Deposit coins into the pool
@param amounts List of amounts of coins to deposit
@param min_mint_amount Minimum amount of LP tokens to mint from the deposit
@return Amount of LP tokens received by depositing
"""
assert not self.is_killed # dev: is killed
amp: uint256 = self._A()
_lp_token: address = self.lp_token
token_supply: uint256 = ERC20(_lp_token).totalSupply()
# Initial invariant
D0: uint256 = 0
old_balances: uint256[N_COINS] = self.balances
if token_supply > 0:
D0 = self.get_D_mem(old_balances, amp)
new_balances: uint256[N_COINS] = old_balances
for i in range(N_COINS):
if token_supply == 0:
assert amounts[i] > 0 # dev: initial deposit requires all coins
# balances store amounts of c-tokens
new_balances[i] = old_balances[i] + amounts[i]
# Invariant after change
D1: uint256 = self.get_D_mem(new_balances, amp)
assert D1 > D0
# We need to recalculate the invariant accounting for fees
# to calculate fair user's share
D2: uint256 = D1
fees: uint256[N_COINS] = empty(uint256[N_COINS])
if token_supply > 0:
# Only account for fees if we are not the first to deposit
_fee: uint256 = self.fee * N_COINS / (4 * (N_COINS - 1))
_admin_fee: uint256 = self.admin_fee
for i in range(N_COINS):
ideal_balance: uint256 = D1 * old_balances[i] / D0
difference: uint256 = 0
if ideal_balance > new_balances[i]:
difference = ideal_balance - new_balances[i]
else:
difference = new_balances[i] - ideal_balance
fees[i] = _fee * difference / FEE_DENOMINATOR
self.balances[i] = new_balances[i] - (fees[i] * _admin_fee / FEE_DENOMINATOR)
new_balances[i] -= fees[i]
D2 = self.get_D_mem(new_balances, amp)
else:
self.balances = new_balances
# Calculate, how much pool tokens to mint
mint_amount: uint256 = 0
if token_supply == 0:
mint_amount = D1 # Take the dust if there was any
else:
mint_amount = token_supply * (D2 - D0) / D0
assert mint_amount >= min_mint_amount, "Slippage screwed you"
# Take coins from the sender
for i in range(N_COINS):
if amounts[i] > 0:
# "safeTransferFrom" which works for ERC20s which return bool or not
_response: Bytes[32] = raw_call(
self.coins[i],
concat(
method_id("transferFrom(address,address,uint256)"),
convert(msg.sender, bytes32),
convert(self, bytes32),
convert(amounts[i], bytes32),
),
max_outsize=32,
) # dev: failed transfer
if len(_response) > 0:
assert convert(_response, bool)
# Mint pool tokens
CurveToken(_lp_token).mint(msg.sender, mint_amount)
log AddLiquidity(msg.sender, amounts, fees, D1, token_supply + mint_amount)
return mint_amount
```
**▶Example▼**
```shell
>>> todo:
```
<a id="depositzapremove_liquidity-1"></a>
### `DepositZap.remove_liquidity`
`DepositZap.remove_liquidity(_amount: uint256, _min_underlying_amounts: uint256[N_COINS]) -> uint256[N_COINS]`
Withdraw and unwrap coins from the pool. Returns list of amounts of underlying coins that were withdrawn.
| Input | Type | Description |
| --- | --- | --- |
| `_amount` | `uint256` | Quantity of LP tokens to burn in the withdrawal |
| `_min_underlying_amounts` | `uint256[N_COINS]` | Minimum amounts of underlying coins to receive |
Emits: Transfer RemoveLiquidity
**<>Source code▼**
```vyper
@internal
def _unwrap_and_transfer(_addr: address, _min_amounts: uint256[N_COINS]) -> uint256[N_COINS]:
# unwrap coins and transfer them to the sender
use_lending: bool[N_COINS] = USE_LENDING
_amounts: uint256[N_COINS] = empty(uint256[N_COINS])
for i in range(N_COINS):
if use_lending[i]:
_coin: address = self.coins[i]
_balance: uint256 = ERC20(_coin).balanceOf(self)
if _balance == 0: # Do nothing if there are 0 coins
continue
yERC20(_coin).withdraw(_balance)
_ucoin: address = self.underlying_coins[i]
_uamount: uint256 = ERC20(_ucoin).balanceOf(self)
assert _uamount >= _min_amounts[i], "Not enough coins withdrawn"
# Send only if we have something to send
if _uamount != 0:
_response: Bytes[32] = raw_call(
_ucoin,
concat(
method_id("transfer(address,uint256)"),
convert(_addr, bytes32),
convert(_uamount, bytes32)
),
max_outsize=32
)
if len(_response) > 0:
assert convert(_response, bool)
_amounts[i] = _uamount
return _amounts
@external
@nonreentrant('lock')
def remove_liquidity(
_amount: uint256,
_min_underlying_amounts: uint256[N_COINS]
) -> uint256[N_COINS]:
"""
@notice Withdraw and unwrap coins from the pool
@dev Withdrawal amounts are based on current deposit ratios
@param _amount Quantity of LP tokens to burn in the withdrawal
@param _min_underlying_amounts Minimum amounts of underlying coins to receive
@return List of amounts of underlying coins that were withdrawn
"""
assert ERC20(self.lp_token).transferFrom(msg.sender, self, _amount)
Curve(self.curve).remove_liquidity(_amount, empty(uint256[N_COINS]))
return self._unwrap_and_transfer(msg.sender, _min_underlying_amounts)
```
```vyper
@external
@nonreentrant('lock')
def remove_liquidity(_amount: uint256, min_amounts: uint256[N_COINS]) -> uint256[N_COINS]:
"""
@notice Withdraw coins from the pool
@dev Withdrawal amounts are based on current deposit ratios
@param _amount Quantity of LP tokens to burn in the withdrawal
@param min_amounts Minimum amounts of underlying coins to receive
@return List of amounts of coins that were withdrawn
"""
_lp_token: address = self.lp_token
total_supply: uint256 = ERC20(_lp_token).totalSupply()
amounts: uint256[N_COINS] = empty(uint256[N_COINS])
fees: uint256[N_COINS] = empty(uint256[N_COINS]) # Fees are unused but we've got them historically in event
for i in range(N_COINS):
value: uint256 = self.balances[i] * _amount / total_supply
assert value >= min_amounts[i], "Withdrawal resulted in fewer coins than expected"
self.balances[i] -= value
amounts[i] = value
_response: Bytes[32] = raw_call(
self.coins[i],
concat(
method_id("transfer(address,uint256)"),
convert(msg.sender, bytes32),
convert(value, bytes32),
),
max_outsize=32,
) # dev: failed transfer
if len(_response) > 0:
assert convert(_response, bool)
CurveToken(_lp_token).burnFrom(msg.sender, _amount) # dev: insufficient funds
log RemoveLiquidity(msg.sender, amounts, fees, total_supply - _amount)
return amounts
```
**▶Example▼**
```shell
>>> todo:
```
<a id="depositzapremove_liquidity_imbalance-1"></a>
### `DepositZap.remove_liquidity_imbalance`
`DepositZap.remove_liquidity_imbalance(_underlying_amounts: uint256[N_COINS], _max_burn_amount: uint256)`
Withdraw and unwrap coins from the pool in an imbalanced amount. Amounts in `_underlying_amounts` correspond to withdrawn amounts before any fees charge for unwrapping Returns list of amounts of underlying coins that were withdrawn.
| Input | Type | Description |
| --- | --- | --- |
| `_underlying_amounts` | `uint256[N_COINS]` | List of amounts of underlying coins to withdraw |
| `_max_burn_amount` | `uint256` | Maximum amount of LP token to burn in the withdrawal |
Emits: Transfer RemoveLiquidityImbalance
**<>Source code▼**
```vyper
@internal
def _unwrap_and_transfer(_addr: address, _min_amounts: uint256[N_COINS]) -> uint256[N_COINS]:
# unwrap coins and transfer them to the sender
use_lending: bool[N_COINS] = USE_LENDING
_amounts: uint256[N_COINS] = empty(uint256[N_COINS])
for i in range(N_COINS):
if use_lending[i]:
_coin: address = self.coins[i]
_balance: uint256 = ERC20(_coin).balanceOf(self)
if _balance == 0: # Do nothing if there are 0 coins
continue
yERC20(_coin).withdraw(_balance)
_ucoin: address = self.underlying_coins[i]
_uamount: uint256 = ERC20(_ucoin).balanceOf(self)
assert _uamount >= _min_amounts[i], "Not enough coins withdrawn"
# Send only if we have something to send
if _uamount != 0:
_response: Bytes[32] = raw_call(
_ucoin,
concat(
method_id("transfer(address,uint256)"),
convert(_addr, bytes32),
convert(_uamount, bytes32)
),
max_outsize=32
)
if len(_response) > 0:
assert convert(_response, bool)
_amounts[i] = _uamount
return _amounts
@external
@nonreentrant('lock')
def remove_liquidity_imbalance(
_underlying_amounts: uint256[N_COINS],
_max_burn_amount: uint256
) -> uint256[N_COINS]:
"""
@notice Withdraw and unwrap coins from the pool in an imbalanced amount
@dev Amounts in `_underlying_amounts` correspond to withdrawn amounts
before any fees charge for unwrapping.
@param _underlying_amounts List of amounts of underlying coins to withdraw
@param _max_burn_amount Maximum amount of LP token to burn in the withdrawal
@return List of amounts of underlying coins that were withdrawn
"""
use_lending: bool[N_COINS] = USE_LENDING
lp_token: address = self.lp_token
amounts: uint256[N_COINS] = _underlying_amounts
for i in range(N_COINS):
_amount: uint256 = amounts[i]
if use_lending[i] and _amount > 0:
rate: uint256 = yERC20(self.coins[i]).getPricePerFullShare()
amounts[i] = _amount * LENDING_PRECISION / rate
# if not use_lending - all good already
# Transfer max tokens in
_lp_amount: uint256 = ERC20(lp_token).balanceOf(msg.sender)
if _lp_amount > _max_burn_amount:
_lp_amount = _max_burn_amount
assert ERC20(lp_token).transferFrom(msg.sender, self, _lp_amount)
Curve(self.curve).remove_liquidity_imbalance(amounts, _max_burn_amount)
# Transfer unused LP tokens back
_lp_amount = ERC20(lp_token).balanceOf(self)
if _lp_amount != 0:
assert ERC20(lp_token).transfer(msg.sender, _lp_amount)
# Unwrap and transfer all the coins we've got
return self._unwrap_and_transfer(msg.sender, empty(uint256[N_COINS]))
```
```vyper
@external
@nonreentrant('lock')
def remove_liquidity_imbalance(amounts: uint256[N_COINS], max_burn_amount: uint256) -> uint256:
"""
@notice Withdraw coins from the pool in an imbalanced amount
@param amounts List of amounts of underlying coins to withdraw
@param max_burn_amount Maximum amount of LP token to burn in the withdrawal
@return Actual amount of the LP token burned in the withdrawal
"""
assert not self.is_killed # dev: is killed
amp: uint256 = self._A()
old_balances: uint256[N_COINS] = self.balances
new_balances: uint256[N_COINS] = old_balances
D0: uint256 = self.get_D_mem(old_balances, amp)
for i in range(N_COINS):
new_balances[i] -= amounts[i]
D1: uint256 = self.get_D_mem(new_balances, amp)
_lp_token: address = self.lp_token
token_supply: uint256 = ERC20(_lp_token).totalSupply()
assert token_supply != 0 # dev: zero total supply
_fee: uint256 = self.fee * N_COINS / (4 * (N_COINS - 1))
_admin_fee: uint256 = self.admin_fee
fees: uint256[N_COINS] = empty(uint256[N_COINS])
for i in range(N_COINS):
ideal_balance: uint256 = D1 * old_balances[i] / D0
difference: uint256 = 0
if ideal_balance > new_balances[i]:
difference = ideal_balance - new_balances[i]
else:
difference = new_balances[i] - ideal_balance
fees[i] = _fee * difference / FEE_DENOMINATOR
self.balances[i] = new_balances[i] - (fees[i] * _admin_fee / FEE_DENOMINATOR)
new_balances[i] -= fees[i]
D2: uint256 = self.get_D_mem(new_balances, amp)
token_amount: uint256 = (D0 - D2) * token_supply / D0
assert token_amount != 0 # dev: zero tokens burned
token_amount += 1 # In case of rounding errors - make it unfavorable for the "attacker"
assert token_amount <= max_burn_amount, "Slippage screwed you"
CurveToken(_lp_token).burnFrom(msg.sender, token_amount) # dev: insufficient funds
for i in range(N_COINS):
if amounts[i] != 0:
_response: Bytes[32] = raw_call(
self.coins[i],
concat(
method_id("transfer(address,uint256)"),
convert(msg.sender, bytes32),
convert(amounts[i], bytes32),
),
max_outsize=32,
) # dev: failed transfer
if len(_response) > 0:
assert convert(_response, bool)
log RemoveLiquidityImbalance(msg.sender, amounts, fees, D1, token_supply - token_amount)
return token_amount
```
**▶Example▼**
```shell
>>> todo:
```
<a id="depositzapremove_liquidity_one_coin-1"></a>
### `DepositZap.remove_liquidity_one_coin`
`DepositZap.remove_liquidity_one_coin(_token_amount: uint256, i: int128, min_uamount: uint256, donate_dust: bool = False)`
Withdraw and unwrap a single coin from the pool. Returns amount of underlying coin received.
| Input | Type | Description |
| --- | --- | --- |
| `_amount` | `uint256` | Amount of LP tokens to burn in the withdrawal |
| `i` | `int128` | Index value of the coin to withdraw |
| `_min_underlying_amount` | `uint256` | Minimum amount of underlying coin to receive |
Emits: Transfer RemoveLiquidityOne
**<>Source code▼**
```vyper
@external
@nonreentrant('lock')
def remove_liquidity_one_coin(
_amount: uint256,
i: int128,
_min_underlying_amount: uint256
) -> uint256:
"""
@notice Withdraw and unwrap a single coin from the pool
@param _amount Amount of LP tokens to burn in the withdrawal
@param i Index value of the coin to withdraw
@param _min_underlying_amount Minimum amount of underlying coin to receive
@return Amount of underlying coin received
"""
assert ERC20(self.lp_token).transferFrom(msg.sender, self, _amount)
Curve(self.curve).remove_liquidity_one_coin(_amount, i, 0)
use_lending: bool[N_COINS] = USE_LENDING
if use_lending[i]:
coin: address = self.coins[i]
_balance: uint256 = ERC20(coin).balanceOf(self)
yERC20(coin).withdraw(_balance)
coin: address = self.underlying_coins[i]
_balance: uint256 = ERC20(coin).balanceOf(self)
assert _balance >= _min_underlying_amount, "Not enough coins removed"
_response: Bytes[32] = raw_call(
coin,
concat(
method_id("transfer(address,uint256)"),
convert(msg.sender, bytes32),
convert(_balance, bytes32),
),
max_outsize=32,
)
if len(_response) > 0:
assert convert(_response, bool)
return _balance
```
```vyper
@external
@nonreentrant('lock')
def remove_liquidity_one_coin(_token_amount: uint256, i: int128, _min_amount: uint256) -> uint256:
"""
@notice Withdraw a single coin from the pool
@param _token_amount Amount of LP tokens to burn in the withdrawal
@param i Index value of the coin to withdraw
@param _min_amount Minimum amount of coin to receive
@return Amount of coin received
"""
assert not self.is_killed # dev: is killed
dy: uint256 = 0
dy_fee: uint256 = 0
total_supply: uint256 = 0
dy, dy_fee, total_supply = self._calc_withdraw_one_coin(_token_amount, i)
assert dy >= _min_amount, "Not enough coins removed"
self.balances[i] -= (dy + dy_fee * self.admin_fee / FEE_DENOMINATOR)
CurveToken(self.lp_token).burnFrom(msg.sender, _token_amount) # dev: insufficient funds
_response: Bytes[32] = raw_call(
self.coins[i],
concat(
method_id("transfer(address,uint256)"),
convert(msg.sender, bytes32),
convert(dy, bytes32),
),
max_outsize=32,
) # dev: failed transfer
if len(_response) > 0:
assert convert(_response, bool)
log RemoveLiquidityOne(msg.sender, _token_amount, dy, total_supply - _token_amount)
return dy
```
**▶Example▼**
```shell
>>> todo:
```
---
# metapool-deposits
Source: [https://docs.curve.finance/developer/amm/legacy/stableswap/deposit-contracts/metapool-deposits](https://docs.curve.finance/developer/amm/legacy/stableswap/deposit-contracts/metapool-deposits)
While Curve metapools support swaps between base pool coins, the base pool LP token and metapool coins, they do not allow liquidity providers to deposit and/or withdraw base pool coins.
For example, the `GUSD` metapool is a pool consisting of `GUSD` and `3CRV` (the LP token of the `3Pool`) and allows for swaps between `GUSD`, `DAI`, `USDC`, `USDT` and `3CRV`. However, liquidity providers are not able to deposit `DAI`, `USDC` or `USDT` to the pool directly. The main reason why this is not possible lies in the maximum byte code size of contracts. Metapools are complex and can therefore end up being very close to the contract byte code size limit. In order to overcome this restriction, liquidity can be added and removed to and from a metapool in the base pool’s coins through a metapool deposit zap.
The template source code for a metapool deposit “zap” may be viewed on [GitHub](https://github.com/curvefi/curve-contract/blob/master/contracts/pool-templates/meta/DepositTemplateMeta.vy).
note
Metapool deposit zaps contain the following private and hardcoded constants:
- `N_COINS`: Number of coins in the metapool (excluding base pool coins)
- `BASE_N_COINS`: Number of coins in the base pool
- `N_ALL_COINS`: All coins in the metapool, excluding the base pool LP token (`N_COINS + BASE_N_COINS - 1`)
<a id="get-deposit-zap-information"></a>
## Get Deposit Zap Information
<a id="depositzappool"></a>
### `DepositZap.pool`
`DepositZap.pool() → address: view`
Getter for the metapool associated with this deposit contract.
**<>Source code▼**
```vyper
pool: public(address)
...
@external
def __init__(_pool: address, _token: address):
"""
@notice Contract constructor
@param _pool Metapool address
@param _token Pool LP token address
"""
self.pool = _pool
...
```
**▶Example▼**
```shell
>>> todo:
```
<a id="depositzapbase_pool"></a>
### `DepositZap.base_pool`
`DepositZap.base_pool() → address: view`
Getter for the base pool of the metapool associated with this deposit contract.
**<>Source code▼**
```vyper
base_pool: public(address)
...
@external
def __init__(_pool: address, _token: address):
"""
@notice Contract constructor
@param _pool Metapool address
@param _token Pool LP token address
"""
self.pool = _pool
self.token = _token
base_pool: address = CurveMeta(_pool).base_pool()
self.base_pool = base_pool
...
```
**▶Example▼**
```shell
>>> todo:
```
<a id="depositzapbase_coins"></a>
### `DepositZap.base_coins`
`DepositZap.base_coins(i: uint256) → address: view`
Getter for the array of the coins of the metapool’s base pool.
| Input | Type | Description |
| --- | --- | --- |
| `i` | `int128` | Index of the coin for which to get the address |
**<>Source code▼**
```vyper
base_coins: public(address[BASE_N_COINS])
...
@external
def __init__(_pool: address, _token: address):
"""
@notice Contract constructor
@param _pool Metapool address
@param _token Pool LP token address
"""
self.pool = _pool
self.token = _token
base_pool: address = CurveMeta(_pool).base_pool()
self.base_pool = base_pool
for i in range(N_COINS):
coin: address = CurveMeta(_pool).coins(i)
self.coins[i] = coin
# approve coins for infinite transfers
_response: Bytes[32] = raw_call(
coin,
concat(
method_id("approve(address,uint256)"),
convert(_pool, bytes32),
convert(MAX_UINT256, bytes32),
),
max_outsize=32,
)
if len(_response) > 0:
assert convert(_response, bool)
for i in range(BASE_N_COINS):
coin: address = CurveBase(base_pool).coins(i)
self.base_coins[i] = coin
# approve underlying coins for infinite transfers
_response: Bytes[32] = raw_call(
coin,
concat(
method_id("approve(address,uint256)"),
convert(base_pool, bytes32),
convert(MAX_UINT256, bytes32),
),
max_outsize=32,
)
if len(_response) > 0:
assert convert(_response, bool)
...
```
**▶Example▼**
```shell
>>> todo:
```
<a id="depositzaptoken"></a>
### `DepositZap.token`
`DepositZap.token() → address: view`
Getter for the LP token of the associated metapool.
**<>Source code▼**
```vyper
token: public(address)
...
@external
def __init__(_pool: address, _token: address):
"""
@notice Contract constructor
@param _pool Metapool address
@param _token Pool LP token address
"""
self.pool = _pool
self.token = _token
...
```
**▶Example▼**
```shell
>>> todo:
```
<a id="addremove-li프로젝트로 돌아가기