Curve DAO 백서

Curve DAO

원문

bitpro가 원문에서 글자만 모은 내용이에요. 표·그림·수식은 빠지거나 깨질 수 있어요. 전체 75쪽 중 44쪽

Description | | --- | --- | --- | | `_amounts` | `DynArray[uint256, MAX_COINS]` | List of amounts of coins to withdraw. | | `_max_burn_amount` | `uint256` | Maximum amount of LP tokens to burn. | | `_receiver` | `address` | Receiver of the coins; defaults to `msg.sender`. | **<>Source code▼** ```vyper event RemoveLiquidityImbalance: provider: indexed(address) token_amounts: DynArray[uint256, MAX_COINS] fees: DynArray[uint256, MAX_COINS] invariant: uint256 token_supply: uint256 @external @nonreentrant('lock') def remove_liquidity_imbalance( _amounts: DynArray[uint256, MAX_COINS], _max_burn_amount: uint256, _receiver: address = msg.sender ) -> 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 @param _receiver Address that receives the withdrawn coins @return Actual amount of the LP token burned in the withdrawal """ amp: uint256 = self._A() rates: DynArray[uint256, MAX_COINS] = self._stored_rates() old_balances: DynArray[uint256, MAX_COINS] = self._balances() D0: uint256 = self.get_D_mem(rates, old_balances, amp) new_balances: DynArray[uint256, MAX_COINS] = old_balances for i in range(MAX_COINS_128): if i == N_COINS_128: break if _amounts[i] != 0: new_balances[i] -= _amounts[i] self._transfer_out(i, _amounts[i], _receiver) D1: uint256 = self.get_D_mem(rates, new_balances, amp) base_fee: uint256 = self.fee * N_COINS / (4 * (N_COINS - 1)) ys: uint256 = (D0 + D1) / N_COINS fees: DynArray[uint256, MAX_COINS] = empty(DynArray[uint256, MAX_COINS]) dynamic_fee: uint256 = 0 xs: uint256 = 0 ideal_balance: uint256 = 0 difference: uint256 = 0 new_balance: uint256 = 0 for i in range(MAX_COINS_128): if i == N_COINS_128: break ideal_balance = D1 * old_balances[i] / D0 difference = 0 new_balance = new_balances[i] if ideal_balance > new_balance: difference = ideal_balance - new_balance else: difference = new_balance - ideal_balance xs = unsafe_div(rates[i] * (old_balances[i] + new_balance), PRECISION) dynamic_fee = self._dynamic_fee(xs, ys, base_fee) fees.append(dynamic_fee * difference / FEE_DENOMINATOR) self.admin_balances[i] += fees[i] * admin_fee / FEE_DENOMINATOR new_balances[i] -= fees[i] D1 = self.get_D_mem(rates, new_balances, amp) # dev: reuse D1 for new D. self.upkeep_oracles(self._xp_mem(rates, new_balances), amp, D1) total_supply: uint256 = self.total_supply burn_amount: uint256 = ((D0 - D1) * total_supply / D0) + 1 assert burn_amount > 1 # dev: zero tokens burned assert burn_amount <= _max_burn_amount, "Slippage screwed you" total_supply -= burn_amount self._burnFrom(msg.sender, burn_amount) log RemoveLiquidityImbalance(msg.sender, _amounts, fees, D1, total_supply) return burn_amount ``` **▶Example▼** ```shell >>> StableSwap.remove_liquidity_imbalance([10**18, 10**6] 10**19) 1999880816717294817 ``` <a id="calc_token_amount"></a> ### `calc_token_amount` `StableSwap.calc_token_amount(_amounts: DynArray[uint256, MAX_COINS], _is_deposit: bool) -> uint256:` Function to calculate the addition or reduction of token supply from a deposit (add liquidity) or withdrawal (remove liquidity). This function does take fees into consideration. Returns: amount of LP tokens (`uint256`). | Input | Type | Description | | --- | --- | --- | | `_amounts` | `DynArray[uint256, MAX_COINS]` | Amount of coins being deposited/withdrawn. | | `_is_deposit` | `bool` | `true` = deposit, `false` = withdraw. | **<>Source code▼** ```vyper interface Factory: def fee_receiver() -> address: view def admin() -> address: view def views_implementation() -> address: view @view @external def calc_token_amount( _amounts: DynArray[uint256, MAX_COINS], _is_deposit: bool ) -> uint256: """ @notice Calculate addition or reduction in token supply from a deposit or withdrawal @param _amounts Amount of each coin being deposited @param _is_deposit set True for deposits, False for withdrawals @return Expected amount of LP tokens received """ return StableSwapViews(factory.views_implementation()).calc_token_amount(_amounts, _is_deposit, self) ``` ```vyper @view @external def calc_token_amount( _amounts: DynArray[uint256, MAX_COINS], _is_deposit: bool, pool: address ) -> uint256: """ @notice Calculate addition or reduction in token supply from a deposit or withdrawal @param _amounts Amount of each coin being deposited @param _is_deposit set True for deposits, False for withdrawals @return Expected amount of LP tokens received """ amp: uint256 = StableSwapNG(pool).A() * A_PRECISION N_COINS: uint256 = StableSwapNG(pool).N_COINS() rates: DynArray[uint256, MAX_COINS] = empty(DynArray[uint256, MAX_COINS]) old_balances: DynArray[uint256, MAX_COINS] = empty(DynArray[uint256, MAX_COINS]) xp: DynArray[uint256, MAX_COINS] = empty(DynArray[uint256, MAX_COINS]) rates, old_balances, xp = self._get_rates_balances_xp(pool, N_COINS) # Initial invariant D0: uint256 = self.get_D(xp, amp, N_COINS) total_supply: uint256 = StableSwapNG(pool).totalSupply() new_balances: DynArray[uint256, MAX_COINS] = old_balances for i in range(MAX_COINS): if i == N_COINS: break amount: uint256 = _amounts[i] if _is_deposit: new_balances[i] += amount else: new_balances[i] -= amount # Invariant after change for idx in range(MAX_COINS): if idx == N_COINS: break xp[idx] = rates[idx] * new_balances[idx] / PRECISION D1: uint256 = self.get_D(xp, amp, N_COINS) # We need to recalculate the invariant accounting for fees # to calculate fair user's share D2: uint256 = D1 if total_supply > 0: # Only account for fees if we are not the first to deposit base_fee: uint256 = StableSwapNG(pool).fee() * N_COINS / (4 * (N_COINS - 1)) fee_multiplier: uint256 = StableSwapNG(pool).offpeg_fee_multiplier() _dynamic_fee_i: uint256 = 0 xs: uint256 = 0 ys: uint256 = (D0 + D1) / N_COINS for i in range(MAX_COINS): if i == N_COINS: break ideal_balance: uint256 = D1 * old_balances[i] / D0 difference: uint256 = 0 new_balance: uint256 = new_balances[i] if ideal_balance > new_balance: difference = ideal_balance - new_balance else: difference = new_balance - ideal_balance xs = old_balances[i] + new_balance _dynamic_fee_i = self._dynamic_fee(xs, ys, base_fee, fee_multiplier) new_balances[i] -= _dynamic_fee_i * difference / FEE_DENOMINATOR for idx in range(MAX_COINS): if idx == N_COINS: break xp[idx] = rates[idx] * new_balances[idx] / PRECISION D2 = self.get_D(xp, amp, N_COINS) else: return D1 # Take the dust if there was any diff: uint256 = 0 if _is_deposit: diff = D2 - D0 else: diff = D0 - D2 return diff * total_supply / D0 ``` **▶Example▼** ```shell >>> StableSwap.calc_token_amount([10**18, 0], True) # deposit (coin[0]) 999701503692424994 >>> StableSwap.calc_token_amount([0, 10**6], True) # deposit (coin[1]) 999875942505458416 >>> StableSwap.calc_token_amount([10**18, 10**6], True) # deposit (coin[0] and coin[1]) 1999863130101592370 >>> StableSwap.calc_token_amount([10**18, 0], False) # withdraw (coin[1]) 999987187514411723 >>> StableSwap.calc_token_amount([10**18, 0], False) # withdraw (coin[0]) 1000188312578139610 >>> StableSwap.calc_token_amount([10**18, 10**6], False) # withdraw (coin[0] and coin[1]) 1999889816188803581 ``` note If `_is_deposit` is True, the method calculates the increase in LP token supply when adding `_amounts` of tokens to the pool. Conversely, when `_is_deposit` is False, the method calculates the decrease in LP token supply when removing `_amounts` of tokens from the pool. This is a `view` function and does not actually alter any states. <a id="calc_withdraw_one_coin"></a> ### `calc_withdraw_one_coin` `StableSwap.calc_withdraw_one_coin(_burn_amount: uint256, i: int128) -> uint256:` Function to calculate the amount of single token `i` withdrawn when burning `_burn_amount` LP tokens. Returns: amount of tokens withdrawn (`uint256`). | Input | Type | Description | | --- | --- | --- | | `_burn_amount` | `uint256` | Amount of LP tokens to burn. | | `i` | `int128` | Index value of the coin to withdraw. | **<>Source code▼** ```vyper @view @external def calc_withdraw_one_coin(_burn_amount: uint256, i: int128) -> uint256: """ @notice Calculate the amount received when withdrawing a single coin @param _burn_amount Amount of LP tokens to burn in the withdrawal @param i Index value of the coin to withdraw @return Amount of coin received """ return self._calc_withdraw_one_coin(_burn_amount, i)[0] @view @internal def _calc_withdraw_one_coin( _burn_amount: uint256, i: int128 ) -> ( uint256, uint256, DynArray[uint256, MAX_COINS], uint256, uint256 ): # First, need to calculate # * Get current D # * Solve Eqn against y_i for D - _token_amount amp: uint256 = self._A() rates: DynArray[uint256, MAX_COINS] = self._stored_rates() xp: DynArray[uint256, MAX_COINS] = self._xp_mem(rates, self._balances()) D0: uint256 = self.get_D(xp, amp) total_supply: uint256 = self.total_supply D1: uint256 = D0 - _burn_amount * D0 / total_supply new_y: uint256 = self.get_y_D(amp, i, xp, D1) base_fee: uint256 = self.fee * N_COINS / (4 * (N_COINS - 1)) ys: uint256 = (D0 + D1) / (2 * N_COINS) xp_reduced: DynArray[uint256, MAX_COINS] = xp dx_expected: uint256 = 0 xp_j: uint256 = 0 xavg: uint256 = 0 dynamic_fee: uint256 = 0 for j in range(MAX_COINS_128): if j == N_COINS_128: break dx_expected = 0 xp_j = xp[j] if j == i: dx_expected = xp_j * D1 / D0 - new_y xavg = (xp_j + new_y) / 2 else: dx_expected = xp_j - xp_j * D1 / D0 xavg = xp_j dynamic_fee = self._dynamic_fee(xavg, ys, base_fee) xp_reduced[j] = xp_j - dynamic_fee * dx_expected / FEE_DENOMINATOR dy: uint256 = xp_reduced[i] - self.get_y_D(amp, i, xp_reduced, D1) dy_0: uint256 = (xp[i] - new_y) * PRECISION / rates[i] # w/o fees dy = (dy - 1) * PRECISION / rates[i] # Withdraw less to account for rounding errors # update xp with new_y for p calculations. xp[i] = new_y return dy, dy_0 - dy, xp, amp, D1 ``` **▶Example▼** ```shell >>> StableSwap.calc_withdraw_one_coin(10**18, 0) 1000107987022361129 >>> StableSwap.calc_withdraw_one_coin(10**18, 1) 999915 >>> StableSwap.get_balances() [1156160050449617680048138, 1052703857609] ``` * * * <a id="fee-methods"></a> ## Fee Methods Stableswap-ng introduces a dynamic fee based on the imbalance of the coins within the pool and their pegs: **▶\`\_dynamic\_fee\`▼** ```vyper offpeg_fee_multiplier: public(uint256) # * 1e10 @view @internal def _dynamic_fee(xpi: uint256, xpj: uint256, _fee: uint256) -> uint256: _offpeg_fee_multiplier: uint256 = self.offpeg_fee_multiplier if _offpeg_fee_multiplier <= FEE_DENOMINATOR: return _fee xps2: uint256 = (xpi + xpj) **2 return ( (_offpeg_fee_multiplier * _fee) / ((_offpeg_fee_multiplier - FEE_DENOMINATOR) * 4 * xpi * xpj / xps2 + FEE_DENOMINATOR) ) ``` More on dynamic fees [here](https://docs.curve.finance/developer/amm/stableswap-ng/overview.md#dynamic-fees). <a id="fee"></a> ### `fee` `StableSwap.fee() -> uint256: view` Getter method for the fee of the pool. This is the value set when initializing the contract and can be changed via [`set_new_fee`](https://docs.curve.finance/developer/amm/stableswap-ng/pools/plainpool.md#set_new_fee). Returns: fee (`uint256`). **<>Source code▼** ```vyper fee: public(uint256) # fee * 1e10 @external def __init__( _name: String[32], _symbol: String[10], _A: uint256, _fee: uint256, _offpeg_fee_multiplier: uint256, _ma_exp_time: uint256, _coins: DynArray[address, MAX_COINS], _rate_multipliers: DynArray[uint256, MAX_COINS], _asset_types: DynArray[uint8, MAX_COINS], _method_ids: DynArray[bytes4, MAX_COINS], _oracles: DynArray[address, MAX_COINS], ): ... self.fee = _fee ... ``` **▶Example▼** ```shell >>> StableSwap.fee() 1000000 ``` note The method returns an integer with with 1e10 precision. <a id="dynamic_fee"></a> ### `dynamic_fee` `StableSwap.dynamic_fee(i: int128, j: int128) -> uint256:` Getter for the swap fee when exchanging between `i` and `j`. The swap fee is expressed as an integer with a 1e10 precision. Returns: dynamic fee (`uint256`). | Input | Type | Description | | --- | --- | --- | | `i` | `int128` | Index value of input coin. | | `j` | `int128` | Index value of output coin. | **<>Source code▼** ```vyper @view @external def dynamic_fee(i: int128, j: int128) -> uint256: """ @notice Return the fee for swapping between `i` and `j` @param i Index value for the coin to send @param j Index value of the coin to recieve @return Swap fee expressed as an integer with 1e10 precision """ return StableSwapViews(factory.views_implementation()).dynamic_fee(i, j, self) ``` ```vyper @view @external def dynamic_fee(i: int128, j: int128, pool:address) -> uint256: """ @notice Return the fee for swapping between `i` and `j` @param i Index value for the coin to send @param j Index value of the coin to recieve @return Swap fee expressed as an integer with 1e10 precision """ N_COINS: uint256 = StableSwapNG(pool).N_COINS() fee: uint256 = StableSwapNG(pool).fee() fee_multiplier: uint256 = StableSwapNG(pool).offpeg_fee_multiplier() rates: DynArray[uint256, MAX_COINS] = empty(DynArray[uint256, MAX_COINS]) balances: DynArray[uint256, MAX_COINS] = empty(DynArray[uint256, MAX_COINS]) xp: DynArray[uint256, MAX_COINS] = empty(DynArray[uint256, MAX_COINS]) rates, balances, xp = self._get_rates_balances_xp(pool, N_COINS) return self._dynamic_fee(xp[i], xp[j], fee, fee_multiplier) @view @internal def _dynamic_fee(xpi: uint256, xpj: uint256, _fee: uint256, _fee_multiplier: uint256) -> uint256: if _fee_multiplier <= FEE_DENOMINATOR: return _fee xps2: uint256 = (xpi + xpj) **2 return ( (_fee_multiplier * _fee) / ((_fee_multiplier - FEE_DENOMINATOR) * 4 * xpi * xpj / xps2 + FEE_DENOMINATOR) ) @view @internal def _get_rates_balances_xp(pool: address, N_COINS: uint256) -> ( DynArray[uint256, MAX_COINS], DynArray[uint256, MAX_COINS], DynArray[uint256, MAX_COINS], ): rates: DynArray[uint256, MAX_COINS] = StableSwapNG(pool).stored_rates() balances: DynArray[uint256, MAX_COINS] = StableSwapNG(pool).get_balances() xp: DynArray[uint256, MAX_COINS] = empty(DynArray[uint256, MAX_COINS]) for idx in range(MAX_COINS): if idx == N_COINS: break xp.append(rates[idx] * balances[idx] / PRECISION) return rates, balances, xp ``` **▶Example▼** ```shell >>> StableSwap.dynamic_fee(0, 1) 1001758 ``` note The method returns an integer with with 1e10 precision. <a id="admin_fee"></a> ### `admin_fee` `StableSwap.admin_fee() -> uint256: view` Getter for the admin fee. It is a constant and is set to 50% (5000000000). Returns: admin fee (`uint256`). **<>Source code▼** ```vyper admin_fee: public(constant(uint256)) = 5000000000 ``` **▶Example▼** ```shell >>> StableSwap.admin_fee() 5000000000 ``` note The method returns an integer with with 1e10 precision. <a id="offpeg_fee_multiplier"></a> ### `offpeg_fee_multiplier` `StableSwap.offpeg_fee_multiplier() -> uint256: view` Getter method for the off-peg fee multiplier. This value determines how much the fee increases when assets within the AMM depeg. This value can be changed via [`set_new_fee`](https://docs.curve.finance/developer/amm/stableswap-ng/pools/plainpool.md#set_new_fee). Returns: offpeg fee multiplier (`uint256`) **<>Source code▼** ```vyper offpeg_fee_multiplier: public(uint256) # * 1e10 @external def __init__( _name: String[32], _symbol: String[10], _A: uint256, _fee: uint256, _offpeg_fee_multiplier: uint256, _ma_exp_time: uint256, _coins: DynArray[address, MAX_COINS], _rate_multipliers: DynArray[uint256, MAX_COINS], _asset_types: DynArray[uint8, MAX_COINS], _method_ids: DynArray[bytes4, MAX_COINS], _oracles: DynArray[address, MAX_COINS], ): ... self.offpeg_fee_multiplier = _offpeg_fee_multiplier ... ``` **▶Example▼** ```shell >>> StableSwap.offpeg_fee_multiplier() 50000000000 ``` note The method returns an integer with with 1e10 precision. <a id="stored_rates"></a> ### `stored_rates` `StableSwap.stored_rates() -> DynArray[uint256, MAX_COINS]:` Getter for the rate multiplier of each coin. Returns: stored rates (`DynArray[uint256, MAX_COINS]`). info If the coin has a rate oracle that has been properly initialized, this method queries that rate by static-calling an external contract. **<>Source code▼** ```vyper rate_multipliers: immutable(DynArray[uint256, MAX_COINS]) # [bytes4 method_id][bytes8 <empty>][bytes20 oracle] oracles: DynArray[uint256, MAX_COINS] @view @external def stored_rates() -> DynArray[uint256, MAX_COINS]: return self._stored_rates() @view @internal def _stored_rates() -> DynArray[uint256, MAX_COINS]: """ @notice Gets rate multipliers for each coin. @dev If the coin has a rate oracle that has been properly initialised, this method queries that rate by static-calling an external contract. """ rates: DynArray[uint256, MAX_COINS] = rate_multipliers oracles: DynArray[uint256, MAX_COINS] = self.oracles for i in range(MAX_COINS_128): if i == N_COINS_128: break if asset_types[i] == 1 and not oracles[i] == 0: # NOTE: fetched_rate is assumed to be 10**18 precision fetched_rate: uint256 = convert( raw_call( convert(oracles[i] % 2**160, address), _abi_encode(oracles[i] & ORACLE_BIT_MASK), max_outsize=32, is_static_call=True, ), uint256 ) rates[i] = unsafe_div(rates[i] * fetched_rate, PRECISION) elif asset_types[i] == 3: # ERC4626 # fetched_rate: uint256 = ERC4626(coins[i]).convertToAssets(call_amount[i]) * scale_factor[i] # here: call_amount has ERC4626 precision, but the returned value is scaled up to 18 # using scale_factor which is (18 - n) if underlying asset has n decimals. rates[i] = unsafe_div( rates[i] * ERC4626(coins[i]).convertToAssets(call_amount[i]) * scale_factor[i], PRECISION ) # 1e18 precision return rates ``` **▶Example▼** ```shell >>> StableSwap.stored_rates() [1000000000000000000, 1000000000000000000000000000000] ``` <a id="admin_balances"></a> ### `admin_balances` `StableSwap.admin_balances(arg0: uint256) -> uint256: view` Getter for the accumulated admin balance of the pool for a coin. These values essentially represent the claimable admin fee. Returns: admin balances (`uint256`). | Input | Type | Description | | --- | --- | --- | | `arg0` | `uint256` | Index value of the coin. | **<>Source code▼** ```vyper admin_balances: public(DynArray[uint256, MAX_COINS]) ``` **▶Example▼** ```shell >>> StableSwap.admin_balances(0) 38117658162246205676 >>> StableSwap.admin_balances(1) 10683574 ``` <a id="withdraw_admin_fees"></a> ### `withdraw_admin_fees` `StableSwap.withdraw_admin_fees():` Function to withdraw accumulated admin fees from the pool and send them to the `fee_receiver` set in the Factory. **<>Source code▼** ```vyper interface Factory: def fee_receiver() -> address: view def admin() -> address: view def views_implementation() -> address: view admin_balances: public(DynArray[uint256, MAX_COINS]) @external def withdraw_admin_fees(): """ @notice Claim admin fees. Callable by anyone. """ self._withdraw_admin_fees() @internal def _withdraw_admin_fees(): fee_receiver: address = factory.fee_receiver() assert fee_receiver != empty(address) # dev: fee receiver not set admin_balances: DynArray[uint256, MAX_COINS] = self.admin_balances for i in range(MAX_COINS_128): if i == N_COINS_128: break if admin_balances[i] > 0: self._transfer_out(i, admin_balances[i], fee_receiver) admin_balances[i] = 0 self.admin_balances = admin_balances ``` **▶Example▼** ```shell >>> StableSwap.withdraw_admin_fees() ``` * * * <a id="amplification-coefficient"></a> ## Amplification Coefficient The amplification coefficient **`A`** determines a pool’s tolerance for imbalance between the assets within it. A higher value means that trades will incur slippage sooner as t
프로젝트로 돌아가기