Curve DAO 백서

Curve DAO

원문

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

oin | | `pool` | `address` | pool address | **<>Source code▼** ```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('todo') 'todo' ``` --- # Tricrypto-NG Overview Source: [https://docs.curve.finance/developer/amm/tricrypto-ng/overview](https://docs.curve.finance/developer/amm/tricrypto-ng/overview) Contract Source & Deployment Source code is available on [GitHub](https://github.com/curvefi/tricrypto-ng). The following documentation covers source code up until commit number [`33707fc`](https://github.com/curvefi/tricrypto-ng/commit/33707fc8b84e08786acf184fcfdb744eb4657a99). All tricrypto-ng deployments can be found in the "Deployment Addresses" section. [↗](https://docs.curve.finance/developer/deployments.md). For an in-depth understanding of the Cryptoswap invariant design, please refer to the official [Cryptoswap whitepaper](https://docs.curve.finance/assets/files/whitepaper_cryptoswap-de196d8f95aae3164a1226234d6d96ca.pdf). * * * **The Tricrypto-NG AMM infrastructure represents a technically enhanced iteration of the previous cryptoswap implementations. It comprises the following key components:** `CurveTricryptoOptimized.vy` * * * The AMM is a **3-coin, auto-rebalancing Cryptoswap implementation** (version 2.0.0) with several optimizations. Unlike the older version, the **pool contract is an ERC20-compliant LP token**. Two implementations exist: one with **native transfers enabled** and one **disabled**. [→ CurveTricryptoOptimized.vy](https://docs.curve.finance/developer/amm/tricrypto-ng/pools/tricrypto.md) `CurveTricryptoFactory.vy` * * * The Factory allows the permissionless deployment of liquidity pools and gauges. It can accommodate **multiple blueprints of the AMM** contract. Blueprints are specified by the user while deploying the pool. [→ CurveTricryptoFactory.vy](https://docs.curve.finance/developer/factory/tricrypto-ng/overview) `CurveCryptoMathOptimized3.vy` * * * A contract which contains different **math utility functions** used in the AMM. [→ CurveCryptoMathOptimized3.vy](https://docs.curve.finance/developer/amm/tricrypto-ng/utility-contracts/math.md) `CurveCryptoViews3Optimized.vy` * * * Contains **view methods relevant for integrators** and users. The address of the deployed Views contract is stored in the Factory and is upgradeable by the Factory's admin. [→ CurveCryptoViews3Optimized.vy](https://docs.curve.finance/developer/amm/tricrypto-ng/utility-contracts/views.md) `Liquidity Gauge Blueprint` * * * A liquidity gauge blueprint implementation which deploys a liquidity gauge of a pool on Ethereum. Gauges on sidechains must be deployed via the [RootChainGaugeFactory](https://docs.curve.finance/developer/gauges/xchain-gauges/root-gauge-factory.md). `Price Oracles` * * * Exponential moving-average oracles for the prices of coins within the AMM. [→ Price Oracles](https://docs.curve.finance/developer/amm/tricrypto-ng/pools/oracles.md) --- # Source: [https://docs.curve.finance/developer/amm/tricrypto-ng/pools/admin-controls](https://docs.curve.finance/developer/amm/tricrypto-ng/pools/admin-controls) Applying new parameters or transferring the ownership of the factory involves a **two-step model**. In the first step, changes need to be committed. The second step involves applying these changes. <a id="pool-ownership"></a> ## Pool Ownership All pools created through the Factory are "owned" by the admin of the Factory, which is the Curve DAO. Ownership can only be changed within the factory contract via `commit_transfer_ownership` and `accept_transfer_ownership`. * * * <a id="amplification-coefficient-and-gamma"></a> ## Amplification Coefficient and Gamma More informations about the parameters [here](https://nagaking.substack.com/p/deep-dive-curve-v2-parameters). The appropriate value for `A` and `gamma` is dependent upon the type of coin being used within the pool, and is subject to optimisation and pool-parameter update based on the market history of the trading pair. It is possible to modify the parameters for a pool after it has been deployed. However, it requires a vote within the Curve DAO and must reach a 15% quorum. <a id="ramp_a_gamma"></a> ### `ramp_A_gamma` `CryptoSwap.ramp_A_gamma(future_A: uint256, future_gamma: uint256, future_time: uint256):` Guarded Method This function is only callable by the `admin` of the Factory contract. Function to ramp A and gamma parameter values linearly. `A` and `gamma` are packed within the same variable. Emits: `RampAgamma` | Input | Type | Description | | --- | --- | --- | | `future_A` | `uint256` | future A value | | `future_gamma` | `uint256` | future gamma value | | `future_time` | `uint256` | timestamp at which the ramping will end | **<>Source code▼** ```vyper event RampAgamma: initial_A: uint256 future_A: uint256 initial_gamma: uint256 future_gamma: uint256 initial_time: uint256 future_time: uint256 @external def ramp_A_gamma( future_A: uint256, future_gamma: uint256, future_time: uint256 ): """ @notice Initialise Ramping A and gamma parameter values linearly. @dev Only accessible by factory admin, and only @param future_A The future A value. @param future_gamma The future gamma value. @param future_time The timestamp at which the ramping will end. """ assert msg.sender == factory.admin() # dev: only owner assert block.timestamp > self.initial_A_gamma_time + (MIN_RAMP_TIME - 1) # dev: ramp undergoing assert future_time > block.timestamp + MIN_RAMP_TIME - 1 # dev: insufficient time A_gamma: uint256[2] = self._A_gamma() initial_A_gamma: uint256 = A_gamma[0] << 128 initial_A_gamma = initial_A_gamma | A_gamma[1] assert future_A > MIN_A - 1 assert future_A < MAX_A + 1 assert future_gamma > MIN_GAMMA - 1 assert future_gamma < MAX_GAMMA + 1 ratio: uint256 = 10**18 * future_A / A_gamma[0] assert ratio < 10**18 * MAX_A_CHANGE + 1 assert ratio > 10**18 / MAX_A_CHANGE - 1 ratio = 10**18 * future_gamma / A_gamma[1] assert ratio < 10**18 * MAX_A_CHANGE + 1 assert ratio > 10**18 / MAX_A_CHANGE - 1 self.initial_A_gamma = initial_A_gamma self.initial_A_gamma_time = block.timestamp future_A_gamma: uint256 = future_A << 128 future_A_gamma = future_A_gamma | future_gamma self.future_A_gamma_time = future_time self.future_A_gamma = future_A_gamma log RampAgamma( A_gamma[0], future_A, A_gamma[1], future_gamma, block.timestamp, future_time, ) ``` **▶Example▼** ```shell >>> CryptoSwap.ramp_A_gamma(2700000, 1300000000000, 1693674492) ``` <a id="stop_ramp_a_gamma"></a> ### `stop_ramp_A_gamma` `CryptoSwap.stop_ramp_A_gamma():` Guarded Method This function is only callable by the `admin` of the Factory contract. Function to immediately stop ramping A and gamma parameters and set them to the current value. Emits: `StopRampA` **<>Source code▼** ```vyper event StopRampA: current_A: uint256 current_gamma: uint256 time: uint256 @external def stop_ramp_A_gamma(): """ @notice Stop Ramping A and gamma parameters immediately. @dev Only accessible by factory admin. """ assert msg.sender == factory.admin() # dev: only owner A_gamma: uint256[2] = self._A_gamma() current_A_gamma: uint256 = A_gamma[0] << 128 current_A_gamma = current_A_gamma | A_gamma[1] self.initial_A_gamma = current_A_gamma self.future_A_gamma = current_A_gamma self.initial_A_gamma_time = block.timestamp self.future_A_gamma_time = block.timestamp # ------ Now (block.timestamp < t1) is always False, so we return saved A. log StopRampA(A_gamma[0], A_gamma[1], block.timestamp) ``` **▶Example▼** ```shell >>> CryptoSwap.stop_ramp_A_gamma() ``` <a id="changing-parameters"></a> ## Changing Parameters <a id="commit_new_parameters"></a> ### `commit_new_parameters` `CryptoSwap.commit_new_parameters(_new_mid_fee: uint256, _new_out_fee: uint256, _new_fee_gamma: uint256, _new_allowed_extra_profit: uint256, _new_adjustment_step: uint256, _new_ma_time: uint256):` Guarded Method This function is only callable by the `admin` of the Factory contract. Function to commit new parameters. The new parameters do not take immedaite effect. Emits: `CommitNewParameters` | Input | Type | Description | | --- | --- | --- | | `_new_mid_fee` | `uint256` | new `mid_fee` value | | `_new_out_fee` | `uint256` | new `out_fee` value | | `_new_fee_gamma` | `uint256` | new `fee_gamma` value | | `_new_allowed_extra_profit` | `uint256` | new `allowed_extra_profit` value | | `_new_adjustment_step` | `uint256` | new `adjustment_step` value | | `_new_ma_time` | `uint256` | new `ma_time` value | **<>Source code▼** ```vyper event CommitNewParameters: deadline: indexed(uint256) mid_fee: uint256 out_fee: uint256 fee_gamma: uint256 allowed_extra_profit: uint256 adjustment_step: uint256 ma_time: uint256 future_packed_rebalancing_params: uint256 future_packed_fee_params: uint256 ADMIN_ACTIONS_DELAY: constant(uint256) = 3 * 86400 @external def commit_new_parameters( _new_mid_fee: uint256, _new_out_fee: uint256, _new_fee_gamma: uint256, _new_allowed_extra_profit: uint256, _new_adjustment_step: uint256, _new_ma_time: uint256, ): """ @notice Commit new parameters. @dev Only accessible by factory admin. @param _new_mid_fee The new mid fee. @param _new_out_fee The new out fee. @param _new_fee_gamma The new fee gamma. @param _new_allowed_extra_profit The new allowed extra profit. @param _new_adjustment_step The new adjustment step. @param _new_ma_time The new ma time. ma_time is time_in_seconds/ln(2). """ assert msg.sender == Factory(self.factory).admin() # dev: only owner assert self.admin_actions_deadline == 0 # dev: active action _deadline: uint256 = block.timestamp + ADMIN_ACTIONS_DELAY self.admin_actions_deadline = _deadline # ----------------------------- Set fee params --------------------------- new_mid_fee: uint256 = _new_mid_fee new_out_fee: uint256 = _new_out_fee new_fee_gamma: uint256 = _new_fee_gamma current_fee_params: uint256[3] = self._unpack(self.packed_fee_params) if new_out_fee < MAX_FEE + 1: assert new_out_fee > MIN_FEE - 1 # dev: fee is out of range else: new_out_fee = current_fee_params[1] if new_mid_fee > MAX_FEE: new_mid_fee = current_fee_params[0] assert new_mid_fee <= new_out_fee # dev: mid-fee is too high if new_fee_gamma < 10**18: assert new_fee_gamma > 0 # dev: fee_gamma out of range [1 .. 10**18] else: new_fee_gamma = current_fee_params[2] self.future_packed_fee_params = self._pack( [new_mid_fee, new_out_fee, new_fee_gamma] ) # ----------------- Set liquidity rebalancing parameters ----------------- new_allowed_extra_profit: uint256 = _new_allowed_extra_profit new_adjustment_step: uint256 = _new_adjustment_step new_ma_time: uint256 = _new_ma_time current_rebalancing_params: uint256[3] = self._unpack(self.packed_rebalancing_params) if new_allowed_extra_profit > 10**18: new_allowed_extra_profit = current_rebalancing_params[0] if new_adjustment_step > 10**18: new_adjustment_step = current_rebalancing_params[1] if new_ma_time < 872542: # <----- Calculated as: 7 * 24 * 60 * 60 / ln(2) assert new_ma_time > 86 # dev: MA time should be longer than 60/ln(2) else: new_ma_time = current_rebalancing_params[2] self.future_packed_rebalancing_params = self._pack( [new_allowed_extra_profit, new_adjustment_step, new_ma_time] ) # ---------------------------------- LOG --------------------------------- log CommitNewParameters( _deadline, new_mid_fee, new_out_fee, new_fee_gamma, new_allowed_extra_profit, new_adjustment_step, new_ma_time, ) ``` **▶Example▼** ```shell >>> CryptoSwap.commit_new_parameters(20000000, 45000000, 350000000000000, 100000000000, 100000000000, 1800) ``` <a id="apply_new_parameters"></a> ### `apply_new_parameters` `CryptoSwap.apply_new_parameters()` Function to apply the parameters from [`commit_new_parameters`](https://docs.curve.finance/developer/amm/tricrypto-ng/pools/admin-controls.md#commit_new_parameters). Emits: `NewParameters` **<>Source code▼** ```vyper event NewParameters: mid_fee: uint256 out_fee: uint256 fee_gamma: uint256 allowed_extra_profit: uint256 adjustment_step: uint256 ma_time: uint256 packed_rebalancing_params: public(uint256) # <---------- Contains rebalancing # parameters allowed_extra_profit, adjustment_step, and ma_time. future_packed_rebalancing_params: uint256 packed_fee_params: public(uint256) # <---- Packs mid_fee, out_fee, fee_gamma. future_packed_fee_params: uint256 @external @nonreentrant("lock") def apply_new_parameters(): """ @notice Apply committed parameters. @dev Only callable after admin_actions_deadline. """ assert block.timestamp >= self.admin_actions_deadline # dev: insufficient time assert self.admin_actions_deadline != 0 # dev: no active action self.admin_actions_deadline = 0 packed_fee_params: uint256 = self.future_packed_fee_params self.packed_fee_params = packed_fee_params packed_rebalancing_params: uint256 = self.future_packed_rebalancing_params self.packed_rebalancing_params = packed_rebalancing_params rebalancing_params: uint256[3] = self._unpack(packed_rebalancing_params) fee_params: uint256[3] = self._unpack(packed_fee_params) log NewParameters( fee_params[0], fee_params[1], fee_params[2], rebalancing_params[0], rebalancing_params[1], rebalancing_params[2], ) ``` **▶Example▼** ```shell >>> CryptoSwap.apply_new_parameters() ``` <a id="revert_new_parameters"></a> ### `revert_new_parameters` `CryptoSwap.revert_new_parameters() -> address: view` Guarded Method This function is only callable by the `admin` of the Factory contract. Function to revert the parameters changes. **<>Source code▼** ```vyper @external def revert_new_parameters(): """ @notice Revert committed parameters @dev Only accessible by factory admin. Setting admin_actions_deadline to 0 ensures a revert in apply_new_parameters. """ assert msg.sender == Factory(self.factory).admin() # dev: only owner self.admin_actions_deadline = 0 ``` **▶Example▼** ```shell >>> CryptoSwap.revert_new_parameters() ``` <a id="admin_actions_deadline"></a> ### `admin_actions_deadline` `CryptoSwap.admin_actions_deadline() -> uint256: view` Getter for the admin actions deadline. This is the deadline until which new parameter changes can be applied. When committing new changes, there is a three-day timespan to apply them (`ADMIN_ACTIONS_DELAY`). If called later, the call will revert. Returns: timestamp (`uint256`). **<>Source code▼** ```vyper admin_actions_deadline: public(uint256) ADMIN_ACTIONS_DELAY: constant(uint256) = 3 * 86400 ``` **▶Example▼** ```shell >>> CryptoSwap.admin_actions_deadline() 0 ``` <a id="initial_a_gamma"></a> ### `initial_A_gamma` `CryptoSwap.initial_A_gamma() -> uint256: view` Getter for the initial A/gamma. Returns: A/gamma (`uint256`). **<>Source code▼** ```vyper initial_A_gamma: public(uint256) ``` **▶Example▼** ```shell >>> CryptoSwap.initial_A_gamma() 581076037942835227425498917514114728328226821 ``` <a id="initial_a_gamma_time"></a> ### `initial_A_gamma_time` `CryptoSwap.initial_A_gamma_time() -> uint256: view` Getter for the initial A/gamma time. Returns: A/gamma time (`uint256`). **<>Source code▼** ```vyper initial_A_gamma_time: public(uint256) ``` **▶Example▼** ```shell >>> CryptoSwap.initial_A_gamma_time() 0 ``` <a id="future_a_gamma"></a> ### `future_A_gamma` `CryptoSwap.future_A_gamma() -> uint256: view` Getter for the future A/gamma. Returns: future A/gamma (`uint256`). **<>Source code▼** ```vyper future_A_gamma: public(uint256) ``` **▶Example▼** ```shell >>> CryptoSwap.future_A_gamma() 581076037942835227425498917514114728328226821 ``` <a id="future_a_gamma_time"></a> ### `future_A_gamma_time` `CryptoSwap.future_A_gamma_time() -> uint256: view` Getter for the future A/gamma time. Returns: future A/gamma time (`uint256`). **<>Source code▼** ```vyper future_A_gamma_time: public(uint256) ``` **▶Example▼** ```shell >>> CryptoSwap.future_A_gamma_time() 0 ``` --- # Tricrypto-NG Oracles Source: [https://docs.curve.finance/developer/amm/tricrypto-ng/pools/oracles](https://docs.curve.finance/developer/amm/tricrypto-ng/pools/oracles) *Tricrypto-NG pools have the following oracle:* `price_oracle` * * * An exponential moving-average (EMA) price oracle with a periodicity determined by `ma_time`. It returns the price relative to the coin at index 0 in the pool. [→ Jump to section](https://docs.curve.finance/developer/amm/tricrypto-ng/pools/oracles.md#price_oracle) Example: Price Oracle for TriCRV The [`TriCRV`](https://etherscan.io/address/0x4ebdf703948ddcea3b11f675b4d1fba9d2414a14) pool consists of `crvUSD &lt;&gt; wETH &lt;&gt; CRV`. Because `crvUSD` is `coin[0]`, the prices of `wETH` and `CRV` are returned with regard to `crvUSD`. ```vyper >>> price_oracle(0) = 3670949576287168254655 3670.94957629 # price of wETH w.r.t crvUSD >>> price_oracle(1) = 724988309167051066 0.72498830916 # price of CRV w.r.t crvUSD ``` *In order to get the reverse EMA (e.g. price of `crvUSD` with regard to `wETH`):* $\frac{10^{36}}{\text{price\_oracle(0)}} = 2.7240908e+14$ * * * *The formula to calculate the exponential moving-average essentially comes down to:* **<>Source code▼** ```vyper @internal def tweak_price( A_gamma: uint256[2], _xp: uint256[N_COINS], new_D: uint256, K0_prev: uint256 = 0, ) -> uint256: """ @notice Tweaks price_oracle, last_price and conditionally adjusts price_scale. This is called whenever there is an unbalanced liquidity operation: _exchange, add_liquidity, or remove_liquidity_one_coin. @dev Contains main liquidity rebalancing logic, by tweaking `price_scale`. @param A_gamma Array of A and gamma parameters. @param _xp Array of current balances. @param new_D New D value. @param K0_prev Initial guess for `newton_D`. """ # ---------------------------- Read storage ------------------------------ rebalancing_params: uint256[3] = self._unpack( self.packed_rebalancing_params ) # <---------- Contains: allowed_extra_profit, adjustment_step, ma_time. price_oracle: uint256[N_COINS - 1] = self._unpack_prices( self.price_oracle_packed ) last_prices: uint256[N_COINS - 1] = self._unpack_prices( self.last_prices_packed ) packed_price_scale: uint256 = self.price_scale_packed price_scale: uint256[N_COINS -
프로젝트로 돌아가기