bitpro가 원문에서 글자만 모은 내용이에요. 표·그림·수식은 빠지거나 깨질 수 있어요. 전체 75쪽 중 28쪽
d liquidity is in the wrapped or underlying token.
<a id="add_liquidity"></a>
### `add_liquidity`
`StableSwap.add_liquidity(_amounts: uint256[N_COINS], _min_mint_amount: uint256, _use_underlying: bool = False) → uint256`
Perform an exchange between two underlying tokens. Index values can be found via the `underlying_coins` public getter method. Returns amount of LP tokens received in exchange for the deposited tokens.
| Input | Type | Description |
| --- | --- | --- |
| `_amounts` | `uint256[N_COINS]` | List of amounts of coins to deposit |
| `_min_mint_amount` | `uint256` | Minimum amount of LP tokens to mint from the deposit |
| `_use_underlying` | `bool` | If `True`, deposit underlying assets instead of wrapped assets |
Emits: AddLiquidity
**<>Source code▼**
```vyper
@external
@nonreentrant('lock')
def add_liquidity(_amounts: uint256[N_COINS], _min_mint_amount: uint256, _use_underlying: bool = False) -> 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
@param _use_underlying If True, deposit underlying assets instead of aTokens
@return Amount of LP tokens received by depositing
"""
assert not self.is_killed # dev: is killed
# Initial invariant
amp: uint256 = self._A()
old_balances: uint256[N_COINS] = self._balances()
lp_token: address = self.lp_token
token_supply: uint256 = ERC20(lp_token).totalSupply()
D0: uint256 = 0
if token_supply != 0:
D0 = self.get_D_precisions(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
new_balances[i] += _amounts[i]
# Invariant after change
D1: uint256 = self.get_D_precisions(new_balances, amp)
assert D1 > D0
# We need to recalculate the invariant accounting for fees
# to calculate fair user's share
fees: uint256[N_COINS] = empty(uint256[N_COINS])
mint_amount: uint256 = 0
if token_supply != 0:
# Only account for fees if we are not the first to deposit
ys: uint256 = (D0 + D1) / N_COINS
_fee: uint256 = self.fee * N_COINS / (4 * (N_COINS - 1))
_feemul: uint256 = self.offpeg_fee_multiplier
_admin_fee: uint256 = self.admin_fee
difference: uint256 = 0
for i in range(N_COINS):
ideal_balance: uint256 = D1 * old_balances[i] / D0
new_balance: uint256 = new_balances[i]
if ideal_balance > new_balance:
difference = ideal_balance - new_balance
else:
difference = new_balance - ideal_balance
xs: uint256 = old_balances[i] + new_balance
fees[i] = self._dynamic_fee(xs, ys, _fee, _feemul) * difference / FEE_DENOMINATOR
if _admin_fee != 0:
self.admin_balances[i] += fees[i] * _admin_fee / FEE_DENOMINATOR
new_balances[i] = new_balance - fees[i]
D2: uint256 = self.get_D_precisions(new_balances, amp)
mint_amount = token_supply * (D2 - D0) / D0
else:
mint_amount = D1 # Take the dust if there was any
assert mint_amount >= _min_mint_amount, "Slippage screwed you"
# Take coins from the sender
if _use_underlying:
lending_pool: address = self.aave_lending_pool
aave_referral: bytes32 = convert(self.aave_referral, bytes32)
# Take coins from the sender
for i in range(N_COINS):
amount: uint256 = _amounts[i]
if amount != 0:
coin: address = self.underlying_coins[i]
# transfer underlying coin from msg.sender to self
_response: Bytes[32] = raw_call(
coin,
concat(
method_id("transferFrom(address,address,uint256)"),
convert(msg.sender, bytes32),
convert(self, bytes32),
convert(amount, bytes32)
),
max_outsize=32
)
if len(_response) != 0:
assert convert(_response, bool)
# deposit to aave lending pool
raw_call(
lending_pool,
concat(
method_id("deposit(address,uint256,address,uint16)"),
convert(coin, bytes32),
convert(amount, bytes32),
convert(self, bytes32),
aave_referral,
)
)
else:
for i in range(N_COINS):
amount: uint256 = _amounts[i]
if amount != 0:
assert ERC20(self.coins[i]).transferFrom(msg.sender, self, amount) # dev: failed transfer
# 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
>>> lending_pool.add_liquidity()
todo: console output
```
---
# metapools
Source: [https://docs.curve.finance/developer/amm/legacy/stableswap/pools/metapools](https://docs.curve.finance/developer/amm/legacy/stableswap/pools/metapools)
A metapool is a pool where a stablecoin is paired against the LP token from another pool, a so-called *base* pool.
For example, a liquidity provider may deposit `DAI` into [3Pool](https://etherscan.io/address/0xbebc44782c7db0a1a60cb6fe97d0b483032ff1c7#code) and in exchange receive the pool’s LP token `3CRV`. The `3CRV` LP token may then be deposited into the [GUSD metapool](https://etherscan.io/address/0x4f062658EaAF2C1ccf8C8e36D6824CDf41167956), which contains the coins `GUSD` and `3CRV`, in exchange for the metapool’s LP token gusd3CRV. The obtained LP token may then be staked in the metapool’s liquidity gauge for `CRV` rewards.
Metapools provide an opportunity for the base pool liquidity providers to earn additional trading fees by depositing their LP tokens into the metapool. Note that the `CRV` rewards received for staking LP tokens into the pool’s liquidity gauge may differ for the base pool’s liquidity gauge and the metapool’s liquidity gauge. For details on liquidity gauges and protocol rewards, please refer to Liquidity Gauges and Minting CRV.
note
Metapools also implement the ABI from plain pools. The template source code for metapools may be viewed on [GitHub](https://github.com/curvefi/curve-contract/blob/master/contracts/pool-templates/meta/SwapTemplateMeta.vy).
<a id="pool-info-methods"></a>
## Pool Info Methods
<a id="base_coins"></a>
### `base_coins`
`StableSwap.base_coins(i: uint256) → address: view`
Get the coins of the base pool. Returns `address` of the coin at index `i`.
| Input | Type | Description |
| --- | --- | --- |
| `i` | `uint256` | Coin index |
**<>Source code▼**
```vyper
# Token corresponding to the pool is always the last one
BASE_POOL_COINS: constant(int128) = 3
...
base_coins: public(address[BASE_POOL_COINS])
...
@external
def __init__(
_owner: address,
_coins: address[N_COINS],
_pool_token: address,
_base_pool: address,
_A: uint256,
_fee: uint256,
_admin_fee: uint256
):
"""
@notice Contract constructor
@param _owner Contract owner address
@param _coins Addresses of ERC20 conracts of coins
@param _pool_token Address of the token representing LP share
@param _base_pool Address of the base pool (which will have a virtual price)
@param _A Amplification coefficient multiplied by n * (n - 1)
@param _fee Fee to charge for exchanges
@param _admin_fee Admin fee
"""
for i in range(N_COINS):
assert _coins[i] != ZERO_ADDRESS
self.coins = _coins
self.initial_A = _A * A_PRECISION
self.future_A = _A * A_PRECISION
self.fee = _fee
self.admin_fee = _admin_fee
self.owner = _owner
self.kill_deadline = block.timestamp + KILL_DEADLINE_DT
self.token = CurveToken(_pool_token)
self.base_pool = _base_pool
self.base_virtual_price = Curve(_base_pool).get_virtual_price()
self.base_cache_updated = block.timestamp
for i in range(BASE_POOL_COINS):
_base_coin: address = Curve(_base_pool).coins(convert(i, uint256))
self.base_coins[i] = _base_coin
# approve underlying coins for infinite transfers
_response: Bytes[32] = raw_call(
_base_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
>>> metapool.base_coins(0)
'0x6B175474E89094C44Da98b954EedeAC495271d0F'
>>> metapool.base_coins(1)
'0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48'
>>> metapool.base_coins(2)
'0xdAC17F958D2ee523a2206206994597C13D831ec7'
```
<a id="stableswapcoins"></a>
### `StableSwap.coins`
`StableSwap.coins(i: uint256) → address: view`
Get the coins of the metapool. Returns `address` of coin at index `i`.
| Input | Type | Description |
| --- | --- | --- |
| `i` | `uint256` | Coin index |
**<>Source code▼**
```vyper
N_COINS: constant(int128) = 2
...
coins: public(address[N_COINS])
...
@external
def __init__(
_owner: address,
_coins: address[N_COINS],
_pool_token: address,
_base_pool: address,
_A: uint256,
_fee: uint256,
_admin_fee: uint256
):
"""
@notice Contract constructor
@param _owner Contract owner address
@param _coins Addresses of ERC20 conracts of coins
@param _pool_token Address of the token representing LP share
@param _base_pool Address of the base pool (which will have a virtual price)
@param _A Amplification coefficient multiplied by n * (n - 1)
@param _fee Fee to charge for exchanges
@param _admin_fee Admin fee
"""
for i in range(N_COINS):
assert _coins[i] != ZERO_ADDRESS
self.coins = _coins
self.initial_A = _A * A_PRECISION
self.future_A = _A * A_PRECISION
self.fee = _fee
self.admin_fee = _admin_fee
self.owner = _owner
self.kill_deadline = block.timestamp + KILL_DEADLINE_DT
self.token = CurveToken(_pool_token)
self.base_pool = _base_pool
self.base_virtual_price = Curve(_base_pool).get_virtual_price()
self.base_cache_updated = block.timestamp
for i in range(BASE_POOL_COINS):
_base_coin: address = Curve(_base_pool).coins(convert(i, uint256))
self.base_coins[i] = _base_coin
# approve underlying coins for infinite transfers
_response: Bytes[32] = raw_call(
_base_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
>>> metapool.coins(0)
'0x056Fd409E1d7A124BD7017459dFEa2F387b6d5Cd'
>>> metapool.coins(1)
'0x6c3F90f043a72FA612cbac8115EE7e52BDe6E490'
```
In this console example, `coins(0)` is the metapool’s coin (`GUSD`) and `coins(1)` is the LP token of the base pool (`3CRV`).
<a id="stableswapbase_pool"></a>
### `StableSwap.base_pool`
`StableSwap.base_pool() → address: view`
Get the address of the base pool. Returns `address` of the base pool implementation.
**<>Source code▼**
```vyper
base_pool: public(address)
...
@external
def __init__(
_owner: address,
_coins: address[N_COINS],
_pool_token: address,
_base_pool: address,
_A: uint256,
_fee: uint256,
_admin_fee: uint256
):
"""
@notice Contract constructor
@param _owner Contract owner address
@param _coins Addresses of ERC20 conracts of coins
@param _pool_token Address of the token representing LP share
@param _base_pool Address of the base pool (which will have a virtual price)
@param _A Amplification coefficient multiplied by n * (n - 1)
@param _fee Fee to charge for exchanges
@param _admin_fee Admin fee
"""
for i in range(N_COINS):
assert _coins[i] != ZERO_ADDRESS
self.coins = _coins
self.initial_A = _A * A_PRECISION
self.future_A = _A * A_PRECISION
self.fee = _fee
self.admin_fee = _admin_fee
self.owner = _owner
self.kill_deadline = block.timestamp + KILL_DEADLINE_DT
self.token = CurveToken(_pool_token)
self.base_pool = _base_pool
self.base_virtual_price = Curve(_base_pool).get_virtual_price()
self.base_cache_updated = block.timestamp
for i in range(BASE_POOL_COINS):
_base_coin: address = Curve(_base_pool).coins(convert(i, uint256))
self.base_coins[i] = _base_coin
# approve underlying coins for infinite transfers
_response: Bytes[32] = raw_call(
_base_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
>>> metapool.base_pool()
'0xbEbc44782C7dB0a1A60Cb6fe97d0b483032FF1C7'
```
<a id="stableswapbase_virtual_price"></a>
### `StableSwap.base_virtual_price`
`StableSwap.base_virtual_price() → uint256: view`
Get the current price of the base pool LP token relative to the underlying base pool assets.
**<>Source code▼**
```vyper
base_virtual_price: public(uint256)
...
@external
def __init__(
_owner: address,
_coins: address[N_COINS],
_pool_token: address,
_base_pool: address,
_A: uint256,
_fee: uint256,
_admin_fee: uint256
):
"""
@notice Contract constructor
@param _owner Contract owner address
@param _coins Addresses of ERC20 conracts of coins
@param _pool_token Address of the token representing LP share
@param _base_pool Address of the base pool (which will have a virtual price)
@param _A Amplification coefficient multiplied by n * (n - 1)
@param _fee Fee to charge for exchanges
@param _admin_fee Admin fee
"""
for i in range(N_COINS):
assert _coins[i] != ZERO_ADDRESS
self.coins = _coins
self.initial_A = _A * A_PRECISION
self.future_A = _A * A_PRECISION
self.fee = _fee
self.admin_fee = _admin_fee
self.owner = _owner
self.kill_deadline = block.timestamp + KILL_DEADLINE_DT
self.token = CurveToken(_pool_token)
self.base_pool = _base_pool
self.base_virtual_price = Curve(_base_pool).get_virtual_price()
self.base_cache_updated = block.timestamp
for i in range(BASE_POOL_COINS):
_base_coin: address = Curve(_base_pool).coins(convert(i, uint256))
self.base_coins[i] = _base_coin
# approve underlying coins for infinite transfers
_response: Bytes[32] = raw_call(
_base_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
>>> metapool.base_virtual_price()
1014750545929625438
```
note
The base pool’s virtual price is only fetched from the base pool if the cached price has expired. A fetched based pool virtual price is cached for 10 minutes (`BASE_CACHE_EXPIRES: constant(int128) = 10 * 60`).
<a id="stableswapbase_cache_update"></a>
### `StableSwap.base_cache_update()`
`StableSwap.base_cache_update() → uint256: view`
Get the timestamp at which the base pool virtual price was last cached.
**<>Source code▼**
```vyper
base_cache_updated: public(uint256)
...
BASE_CACHE_EXPIRES: constant(int128) = 10 * 60 # 10 min
...
@external
def __init__(
_owner: address,
_coins: address[N_COINS],
_pool_token: address,
_base_pool: address,
_A: uint256,
_fee: uint256,
_admin_fee: uint256
):
"""
@notice Contract constructor
@param _owner Contract owner address
@param _coins Addresses of ERC20 conracts of coins
@param _pool_token Address of the token representing LP share
@param _base_pool Address of the base pool (which will have a virtual price)
@param _A Amplification coefficient multiplied by n * (n - 1)
@param _fee Fee to charge for exchanges
@param _admin_fee Admin fee
"""
for i in range(N_COINS):
assert _coins[i] != ZERO_ADDRESS
self.coins = _coins
self.initial_A = _A * A_PRECISION
self.future_A = _A * A_PRECISION
self.fee = _fee
self.admin_fee = _admin_fee
self.owner = _owner
self.kill_deadline = block.timestamp + KILL_DEADLINE_DT
self.token = CurveToken(_pool_token)
self.base_pool = _base_pool
self.base_virtual_price = Curve(_base_pool).get_virtual_price()
self.base_cache_updated = block.timestamp
for i in range(BASE_POOL_COINS):
_base_coin: address = Curve(_base_pool).coins(convert(i, uint256))
self.base_coins[i] = _base_coin
# approve underlying coins for infinite transfers
_response: Bytes[32] = raw_call(
_base_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)
...
@internal
def _vp_rate() -> uint256:
if block.timestamp > self.base_cache_updated + BASE_CACHE_EXPIRES:
vprice: uint256 = Curve(self.base_pool).get_virtual_price()
self.base_virtual_price = vprice
self.base_cache_updated = block.timestamp
return vprice
else:
return self.base_virtual_price
@internal
@view
def _vp_rate_ro() -> uint256:
if block.timestamp > self.base_cache_updated + BASE_CACHE_EXPIRES:
return Curve(self.base_pool).get_virtual_price()
else:
return self.base_virtual_price
```
**▶Example▼**
```shell
>>> metapool.base_cache_updated()
1616583340
```
<a id="exchange-methods"></a>
## Exchange Methods
Similar to lending pools, on metapools exchanges can be made either between the coins the metapool actually holds (another pool’s LP token and some other coin) or between the metapool’s underlying coins. In the context of a metapool, **underlying**coins refers to the metapool’s coin and any of the base pool’s coins. The base pool’s LP token is **not**included as an underlying coin.
For example, the GUSD metapool would have the following:
Coins: `GUSD`, `3CRV` (3Pool LP)
Underlying coins: `GUSD`, `DAI`, `USDC`, `USDT`
note
While metapools contain public getters for `coins` and `base_coins`, there exists no getter for obtaining a list of all underlying coins.
<a id="stableswapexchange"></a>
### `StableSwap.exchange`
`StableSwap.exchange(i: int128, j: int128, _dx: uint256, _min_dy: uint256) → uint256`
Perform an exchange between two (non-underlying) coins in the metapool. Index values can be found via the `coins` public getter method.
Returns: the actual amount of coin `j` received.
| Input | Type | Description |
| --- | --- | --- |
| `i` | `int128` | Index value for the coin to send |
| `j` | `int128` | Index value of the coin to receive |
| `_dx` | `uint256` | Amount of `i` being exchanged |
| `_min_dy` | `uint256` | Minimum amount of `j` to receive |
Emits: TokenExchange
todo: explain how fee is calculated
**<>Source code▼**
```vyper
@external
@nonreentrant('lock')
def exchange(i: int128, j: int128, dx: uint256, min_dy: uint256) -> uint256:
"""
@notice Perform an exchange between two coins
@dev Index values can be found via the `coins` public getter method
@param i Index value for the coin to send
@param j Index valie of the coin to recieve
@param dx Amount of `i` being exchanged
@param min_dy Minimum amount of `j` to receive
@return Actual amount of `j` received
"""
assert not self.is_killed # dev: is killed
rates: uint256[N_COINS] = RATES
rates[MAX_COIN] = self._vp_rate()
old_balances: uint256[N_COINS] = self.balances
xp: uint256[N_COINS] = self._xp_mem(rates[MAX_COIN], old_balances)
x: uint256 = xp[i] + dx * rates[i] / PRECISION
y: uint256 = self.get_y(i, j, x, xp)
dy: uint256 = xp[j] - y - 1 # -1 just in case there were some rounding errors
dy_fee: uint256 = dy * self.fee / FEE_DENOMINATOR
# Convert all to real units
dy = (dy - dy_fee) * PRECISION / rates[j]
assert dy >= min_dy, "Too few coins in result"
dy_admin_fee: uint256 = dy_fee * self.admin_fee / FEE_DENOMINATOR
dy_admin_fee = dy_admin_fee * PRECISION / rates[j]
# Change balances exactly in same way as we change actual ERC20 coin amounts
self.balances[i] = old_balances[i] + dx
# When rounding errors happen, we undercharge admin fee in favor of LP
self.balances[j] = old_balances[j] - dy - dy_admin_fee
assert ERC20(self.coins[i]).transferFrom(msg.sender, self, dx)
assert ERC20(self.coins[j]).transfer(msg.sender, dy)
log TokenExchange(msg.sender, i, dx, j, dy)
return dy
```
**▶Example▼**
```shell
>>> lending_pool.exchange()
todo: console output
```
<a id="stableswapexchange_underlying"></a>
### `StableSwap.exchange_underlying`
Stableswap.exchange\_underlying(i: int128, j: int128, \_dx: uint256, \_min\_dy: uint256) → uint256
Perform an exchange between two underlying tokens. Index values are the `coins` followed by the `base_coins`, where the base pool LP token is **not**included as a value.
Returns: the actual amount of coin `j` received.
| Input | Type | Description |
| --- | --- | --- |
| `i` | `int128` | Index value for the coin to send |
| `j` | `int128` | Index value of the coin to receive |
| `_dx` | `uint256` | Amount of `i` b프로젝트로 돌아가기