Curve DAO 백서

Curve DAO

원문

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

ypto.allowed_extra_profit() 100000000 ``` <a id="adjustment_step"></a> ### `adjustment_step` `TriCrypto.adjustment_step() -> uint256:` Getter for the adjustment step value. Returns: adjustment step (`uint256`). **<>Source code▼** ```vyper packed_rebalancing_params: public(uint256) # <---------- Contains rebalancing # parameters allowed_extra_profit, adjustment_step, and ma_time. @view @external def adjustment_step() -> uint256: """ @notice Returns the current adjustment step @return uint256 adjustment_step value. """ return self._unpack(self.packed_rebalancing_params)[1] ``` **▶Example▼** ```shell >>> TriCrypto.adjustment_step() 100000000000 ``` <a id="packed_rebalancing_params"></a> ### `packed_rebalancing_params` `TriCrypto.packed_rebalancing_params() -> uint256: view` Getter for the packed rebalancing parameters, consisting of `allowed_extra_profit`, `adjustment_step`, and `ma_time`. Returns: packed rebalancing parameters (`uint256`). **<>Source code▼** ```vyper packed_rebalancing_params: public(uint256) # <---------- Contains rebalancing # parameters allowed_extra_profit, adjustment_step, and ma_time. ``` **▶Example▼** ```shell >>> TriCrypto.packed_rebalancing_params() 34028236692093848191011868114131982745600000866 ``` * * * <a id="bonding-curve-parameters"></a> ## Bonding Curve Parameters A bonding curve is used to determine asset prices according to the pool's supply of each asset, more [here](https://docs.curve.finance/developer/amm/legacy/cryptoswap-overview.md#bonding-curve-parameters). Bonding curve parameters `A` and `gamma` values are [upgradable](https://docs.curve.finance/developer/amm/tricrypto-ng/pools/tricrypto.md#amplification-coefficient-and-gamma) by the the pools admin. <a id="a"></a> ### `A` `CryptoSwap.A() -> uint256:` Getter for the current pool amplification parameter. Returns: A (`uint256`). **<>Source code▼** ```vyper @view @external def A() -> uint256: """ @notice Returns the current pool amplification parameter. @return uint256 A param. """ return self._A_gamma()[0] @view @internal def _A_gamma() -> uint256[2]: t1: uint256 = self.future_A_gamma_time A_gamma_1: uint256 = self.future_A_gamma gamma1: uint256 = A_gamma_1 & 2**128 - 1 A1: uint256 = A_gamma_1 >> 128 if block.timestamp < t1: # --------------- Handle ramping up and down of A -------------------- A_gamma_0: uint256 = self.initial_A_gamma t0: uint256 = self.initial_A_gamma_time t1 -= t0 t0 = block.timestamp - t0 t2: uint256 = t1 - t0 A1 = ((A_gamma_0 >> 128) * t2 + A1 * t0) / t1 gamma1 = ((A_gamma_0 & 2**128 - 1) * t2 + gamma1 * t0) / t1 return [A1, gamma1] ``` **▶Example▼** ```shell >>> CryptoSwap.A() 1707629 ``` <a id="gamma"></a> ### `gamma` `CryptoSwap.gamma() -> uint256:` Getter for the current pool gamma parameter. Returns: gamma (`uint256`). **<>Source code▼** ```vyper @view @external def gamma() -> uint256: """ @notice Returns the current pool gamma parameter. @return uint256 gamma param. """ return self._A_gamma()[1] @view @internal def _A_gamma() -> uint256[2]: t1: uint256 = self.future_A_gamma_time A_gamma_1: uint256 = self.future_A_gamma gamma1: uint256 = A_gamma_1 & 2**128 - 1 A1: uint256 = A_gamma_1 >> 128 if block.timestamp < t1: # --------------- Handle ramping up and down of A -------------------- A_gamma_0: uint256 = self.initial_A_gamma t0: uint256 = self.initial_A_gamma_time t1 -= t0 t0 = block.timestamp - t0 t2: uint256 = t1 - t0 A1 = ((A_gamma_0 >> 128) * t2 + A1 * t0) / t1 gamma1 = ((A_gamma_0 & 2**128 - 1) * t2 + gamma1 * t0) / t1 return [A1, gamma1] ``` **▶Example▼** ```shell >>> CryptoSwap.gamma() 11809167828997 ``` * * * <a id="contract-info-methods"></a> ## Contract Info Methods <a id="coins"></a> ### `coins` `TriCrypto.coins(arg0: uint256) -> uint256: view` Getter for the coin at index `arg0`. | Input | Type | Description | | --- | --- | --- | | `k` | `uint256` | Index of the coin. | Returns: coin (`address`). **<>Source code▼** ```vyper coins: public(immutable(address[N_COINS])) ``` **▶Example▼** ```shell >>> TriCrypto.coins(0) '0xdAC17F958D2ee523a2206206994597C13D831ec7' ``` <a id="balances"></a> ### `balances` `TriCrypto.balances(arg0: uint256) -> uint256: view` Getter for the coin balance at index `arg0`. | Input | Type | Description | | --- | --- | --- | | `k` | `uint256` | Index of the coin. | Returns: coin balance (`address`). **<>Source code▼** ```vyper balances: public(uint256[N_COINS]) ``` **▶Example▼** ```shell >>> TriCrypto.balances(0) 16193303272455 ``` <a id="precisions"></a> ### `precisions` `TriCrypto.precisions() -> uint256[N_COINS]: view` Getter for the precision of each coin in the pool. Returns: precisions (`uint256[N_COINS]`). **<>Source code▼** ```vyper N_COINS: constant(uint256) = 3 PRECISION: constant(uint256) = 10**18 # <------- The precision to convert to. A_MULTIPLIER: constant(uint256) = 10000 packed_precisions: uint256 @view @external def precisions() -> uint256[N_COINS]: # <-------------- For by view contract. """ @notice Returns the precisions of each coin in the pool. @return uint256[3] precisions of coins. """ return self._unpack(self.packed_precisions) ``` **▶Example▼** ```shell >>> TriCrypto.precisions() 1000000000000, 10000000000, 1 ``` <a id="factory"></a> ### `factory` `TriCrypto.factory() -> address: view` Getter for the Factory contract. Returns: Factory (`address`) **<>Source code▼** ```vyper factory: public(address) ``` **▶Example▼** ```shell >>> TriCrypto.factory() '0x0c0e5f2fF0ff18a3be9b835635039256dC4B4963' ``` <a id="math"></a> ### `MATH` `TriCrypto.MATH() -> address: view` Getter for the [math utility contract](https://docs.curve.finance/developer/amm/tricrypto-ng/utility-contracts/math.md). Returns: math contract (`address`). **<>Source code▼** ```vyper factory: public(address) ``` **▶Example▼** ```shell >>> TriCrypto.MATH() '0xcBFf3004a20dBfE2731543AA38599A526e0fD6eE' ``` <a id="weth20"></a> ### `WETH20` `TriCrypto.WETH20() -> address: view` Getter for the wETH contract. Returns: wETH contract (`address`). **<>Source code▼** ```vyper WETH20: public(immutable(address)) ``` **▶Example▼** ```shell >>> TriCrypto.WETH20() '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2' ``` <a id="admin-controls"></a> ## Admin Controls 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`. Applying new parameters involves a **two-step model**. In the first step, changes need to be committed. The second step involves applying these changes. <a id="amplification-coefficient-and-gamma"></a> ### Amplification Coefficient and Gamma More information 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` `TriCrypto.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. | Input | Type | Description | | --- | --- | --- | | `future_A` | `uint256` | Future value of `A` | | `future_gamma` | `uint256` | Future value of `gamma` | | `future_time` | `uint256` | Timestamp at which the ramping will end | Emits: `RampAgamma` **<>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 >>> TriCrypto.ramp_A_gamma(2700000, 1300000000000, 1693674492) ``` <a id="stop_ramp_a_gamma"></a> ### `stop_ramp_A_gamma` `TriCrypto.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 >>> TriCrypto.stop_ramp_A_gamma() ``` <a id="changing-parameters"></a> ### Changing Parameters <a id="commit_new_parameters"></a> ### `commit_new_parameters` `TriCrypto.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 immediate 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 >>> TriCrypto.commit_new_parameters(20000000, 45000000, 350000000000000, 100000000000, 100000000000, 1800) ``` <a id="apply_new_parameters"></a> ### `apply_new_parameters` `TriCrypto.apply_new_parameters()` Guarded Method This function can only be called after the `admin_actions_deadline` has passed. The deadline is set when new parameters are committed via [`commit_new_parameters`](https://docs.curve.finance/developer/amm/tricrypto-ng/pools/tricrypto.md#commit_new_parameters). Function to apply the parameters from [`commit_new_parameters`](https://docs.curve.finance/developer/amm/tricrypto-ng/pools/tricrypto.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 >>> TriCrypto.apply_new_parameters() ``` <a id="revert_new_parameters"></a> ### `revert_new_parameters` `TriCrypto.revert_new_parameters()` 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 >>> TriCrypto.revert_new_parameters() ``` <a id="admin_actions_deadline"></a> ### `admin_actions_deadline` `TriCrypto.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 >>> TriCrypto.admin_actions_deadline() 0 ``` <a id="initial_a_gamma"></a> ### `initial_A_gamma` `TriCrypto.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 >>> TriCrypto.initial_A_gamma() 581076037942835227425498917514114728328226821 ``` <a id="initial_a_gamma_time"></a> ### `initial_A_gamma_time` `TriCrypto.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 >>> TriCrypto.initial_A_gamma_time() 0 ``` <a id="future_a_gamma"></a> ### `future_A_gamma` `TriCrypto.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 >>> TriCrypto.future_A_gamma() 581076037942835227425498917514114728328226821 ``` <a id="future_a_gamma_time"></a> ### `future_A_gamma_time` `TriCrypto.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 >>> TriCrypto.future_A_gamma_time() 0 ``` --- # Math Contract Source: [https://docs.curve.finance/developer/amm/tricrypto-ng/utility-contracts/math](https://docs.curve.finance/developer/amm/tricrypto-ng/utility-contracts/math) **The Math Contract provides AMM Math for 3-coin Curve Cryptoswap Pools.** Contract Source & Deployment Source code for this contract is available on [Github](https://github.com/curvefi/tricrypto-ng/blob/main/contracts/main/CurveCryptoMathOptimized3.vy). Full list of all deployments can be found [here](https://docs.curve.finance/developer/deployments.md). * * * <a id="amm-math-functions"></a> ## AMM Math Functi
프로젝트로 돌아가기