Curve DAO 백서

Curve DAO

원문

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

r 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. The fee is expressed as an integer with a 1e10 precision. 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/metapool.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, _math_implementation: address, _base_pool: address, _coins: DynArray[address, MAX_COINS], _base_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) 1043403 ``` 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/metapool.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, _math_implementation: address, _base_pool: address, _coins: DynArray[address, MAX_COINS], _base_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]:`view 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[0], StableSwap(BASE_POOL).get_virtual_price() ] oracles: DynArray[uint256, MAX_COINS] = self.oracles if not self.oracles[0] == 0: response: Bytes[32] = raw_call( convert(oracles[0] % 2**160, address), _abi_encode(oracles[0] & ORACLE_BIT_MASK), max_outsize=32, is_static_call=True, ) # rates[0] * convert(response, uint256) / PRECISION rates[0] = unsafe_div(rates[0] * convert(response, uint256), PRECISION) return rates ``` **▶Example▼** ```shell >>> StableSwap.stored_rates() [1000000000000000000000000000000, 1028532570390672122] ``` <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) 73146476 >>> StableSwap.admin_balances(1) 129624596387098161946 ``` <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 within 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(N_COINS_128): 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 the assets within the pool become imbalanced. The appropriate value for A is dependent upon the type of coin being used within the pool, and is subject to optimisation. It is possible to modify the amplification coefficient for a pool via the \*\*`ramp_A`\*\*function. See [admin controls](https://docs.curve.finance/developer/amm/stableswap-ng/pools/metapool.md#ramp_a). When a ramping of A has been initialized, the process can be stopped by calling the function [**`stop_ramp_A()`**](https://docs.curve.finance/developer/amm/stableswap-ng/pools/metapool.md#stop_ramp_a). <a id="a"></a> ### `A` `StableSwap.A() -> uint256: view` Getter for the amplification coefficient A. Returns: A (`uint256`). **<>Source code▼** ```vyper A_PRECISION: constant(uint256) = 100 MAX_A: constant(uint256) = 10 **6 MAX_A_CHANGE: constant(uint256) = 10 initial_A: public(uint256) future_A: public(uint256) initial_A_time: public(uint256) future_A_time: public(uint256) @view @external def A() -> uint256: return unsafe_div(self._A(), A_PRECISION) @view @internal def _A() -> uint256: """ Handle ramping A up or down """ t1: uint256 = self.future_A_time A1: uint256 = self.future_A if block.timestamp < t1: A0: uint256 = self.initial_A t0: uint256 = self.initial_A_time # Expressions in uint256 cannot have negative numbers, thus "if" if A1 > A0: return A0 + (A1 - A0) * (block.timestamp - t0) / (t1 - t0) else: return A0 - (A0 - A1) * (block.timestamp - t0) / (t1 - t0) else: # when t1 == 0 or block.timestamp >= t1 return A1 ``` **▶Example▼** ```shell >>> StableSwap.A() 500 ``` note The amplification coefficient is scaled by `A_precise`. <a id="a_precise"></a> ### `A_precise` `StableSwap.A_precise() -> uint256: view` Getter for the precise A value, which is not divided by `A_precise` unlike `A`. Returns: precise A (`uint256`). **<>Source code▼** ```vyper A_PRECISION: constant(uint256) = 100 MAX_A: constant(uint256) = 10 **6 MAX_A_CHANGE: constant(uint256) = 10 initial_A: public(uint256) future_A: public(uint256) initial_A_time: public(uint256) future_A_time: public(uint256) @view @external def A_precise() -> uint256: return self._A() ``` **▶Example▼** ```shell >>> StableSwap.A_precise() 50000 ``` <a id="initial_a"></a> ### `initial_A` `StableSwap.initial_A() -> uint256: view` Getter for the initial A value. This is the A value when the ramping was initialized. Returns: initial A (`uint256`). **<>Source code▼** ```vyper A_PRECISION: constant(uint256) = 100 MAX_A: constant(uint256) = 10 **6 MAX_A_CHANGE: constant(uint256) = 10 initial_A: public(uint256) future_A: public(uint256) initial_A_time: public(uint256) future_A_time: public(uint256) @external def ramp_A(_future_A: uint256, _future_time: uint256): assert msg.sender == factory.admin() # dev: only owner assert block.timestamp >= self.initial_A_time + MIN_RAMP_TIME assert _future_time >= block.timestamp + MIN_RAMP_TIME # dev: insufficient time _initial_A: uint256 = self._A() _future_A_p: uint256 = _future_A * A_PRECISION assert _future_A > 0 and _future_A < MAX_A if _future_A_p < _initial_A: assert _future_A_p * MAX_A_CHANGE >= _initial_A else: assert _future_A_p <= _initial_A * MAX_A_CHANGE self.initial_A = _initial_A self.future_A = _future_A_p self.initial_A_time = block.timestamp self.future_A_time = _future_time log RampA(_initial_A, _future_A_p, block.timestamp, _future_time) ``` **▶Example▼** ```shell >>> StableSwap.initial_A() 500 ``` <a id="future_a"></a> ### `future_A` `StableSwap.future_A() -> uint256: view` Getter for the future A value. This value is adjusted when ramping A. Returns: future A (`uint256`). **<>Source code▼** ```vyper A_PRECISION: constant(uint256) = 100 MAX_A: constant(uint256) = 10 **6 MAX_A_CHANGE: constant(uint256) = 10 initial_A: public(uint256) future_A: public(uint256) initial_A_time: public(uint256) future_A_time: public(uint256) @external def ramp_A(_future_A: uint256, _future_time: uint256): assert msg.sender == factory.admin() # dev: only owner assert block.timestamp >= self.initial_A_time + MIN_RAMP_TIME assert _future_time >= block.timestamp + MIN_RAMP_TIME # dev: insufficient time _initial_A: uint256 = self._A() _future_A_p: uint256 = _future_A * A_PRECISION assert _future_A > 0 and _future_A < MAX_A if _future_A_p < _initial_A: assert _future_A_p * MAX_A_CHANGE >= _initial_A else: assert _future_A_p <= _initial_A * MAX_A_CHANGE self.initial_A = _initial_A self.future_A = _future_A_p self.initial_A_time = block.timestamp self.future_A_time = _future_time log RampA(_initial_A, _future_A_p, block.timestamp, _future_time) ``` **▶Example▼** ```shell >>> StableSwap.future_A() 500 ``` <a id="initial_a_time"></a> ### `initial_A_time` `StableSwap.initial_A_time() -> uint256: view` Getter for the initial A time. This is the timestamp when ramping A was initialized. Returns: initial A time (`uint256`). **<>Source code▼** ```vyper A_PRECISION: constant(uint256) = 100 MAX_A: constant(uint256) = 10 **6 MAX_A_CHANGE: constant(uint256) = 10 initial_A: public(uint256) future_A: public(uint256) initial_A_time: public(uint256) future_A_time: public(uint256) @external def ramp_A(_future_A: uint256, _future_time: uint256): assert msg.sender == factory.admin() # dev: only owner assert block.timestamp >= self.initial_A_time + MIN_RAMP_TIME assert _future_time >= block.timestamp + MIN_RAMP_TIME # dev: insufficient time _initial_A: uint256 = self._A() _future_A_p: uint256 = _future_A * A_PRECISION assert _future_A > 0 and _future_A < MAX_A if _future_A_p < _initial_A: assert _future_A_p * MAX_A_CHANGE >= _initial_A else: assert _future_A_p <= _initial_A * MAX_A_CHANGE self.initial_A = _initial_A self.future_A = _future_A_p self.initial_A_time = block.timestamp self.future_A_time = _future_time log RampA(_initial_A, _future_A_p, block.timestamp, _future_time) ``` **▶Example▼** ```shell >>> StableSwap.initial_A_time() 0 ``` <a id="future_a_time"></a> ### `future_A_time` `StableSwap.future_A_time() -> uint256: view` Getter for the future A time. This is the timestamp when ramping A should be finished. Returns: future A time (`uint256`). **<>Source code▼** ```vyper A_PRECISION: constant(uint256) = 100 MAX_A: constant(uint256) = 10 **6 MAX_A_CHANGE: constant(uint256) = 10 initial_A: public(uint256) future_A: public(uint256) initial_A_time: public(uint256) future_A_time: public(uint256) @external def ramp_A(_future_A: uint256, _future_time: uint256): assert msg.sender == factory.admin() # dev: only owner assert block.timestamp >= self.initial_A_time + MIN_RAMP_TIME assert _future_time >= block.timestamp + MIN_RAMP_TIME # dev: insufficient time _initial_A: uint256 = self._A() _future_A_p: uint256 = _future_A * A_PRECISION assert _future_A > 0 and _future_A < MAX_A if _future_A_p < _initial_A: assert _future_A_p * MAX_A_CHANGE >= _initial_A else: assert _future_A_p <= _initial_A * MAX_A_CHANGE self.initial_A = _initial_A self.future_A = _future_A_p self.initial_A_time = block.timestamp self.future_A_time = _future_time log RampA(_initial_A, _future_A_p, block.timestamp, _future_time) ``` **▶Example▼** ```shell >>> StableSwap.future_A_time() 0 ``` * * * <a id="contract-info-methods"></a> ## Contract Info Methods <a id="base_pool"></a> ### `BASE_POOL` `StableSwap.BASE_POOL() -> address: view` Getter for the base pool. Returns: base pool (`address`). **<>Source code▼** ```vyper BASE_POOL: public(immutable(address)) @external def __init__( _name: String[32], _symbol: String[10], _A: uint256, _fee: uint256, _offpeg_fee_multiplier: uint256, _ma_exp_time: uint256, _math_implementation: address, _base_pool: address, _coins: DynArray[address, MAX_COINS], _base_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], ): ... BASE_POOL = _base_pool ... ``` **▶Example▼** ```shell >>> StableSwap.BASE_POOL() '0xbEbc44782C7dB0a1A60Cb6fe97d0b483032FF1C7' ``` <a id="base_n_coins"></a> ### `BASE_N_COINS` `StableSwap.BASE_N_COINS() -> uint256: view` Getter for the number of coins within the base pool. Returns: number of coins (`uint256`). **<>Source code▼** ```vyper MAX_COINS: constant(uint256) = 8 # max coins is 8 in the factory BASE_N_COINS: public(immutable(uint256)) @external def __init__( _name: String[32], _symbol: String[10], _A: uint256, _fee: uint256, _offpeg_fee_multiplier: uint256, _ma_exp_time: uint256, _math_implementation: address, _base_pool: address, _coins: DynArray[address, MAX_COINS], _base_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], ): ... BASE_N_COINS = len(_base_coins) ... ``` **▶Example▼** ```shell >>> StableSwap.BASE_N_COINS() 3 ``` <a id="base_coins"></a> ### `BASE_COINS` `StableSwap.BASE_COINS(arg0: uint256) -> address: view` Getter for the coin at index value `arg0` within the base pool. Returns: coin (`address`). | Input | Type | Description | | --- | --- | --- | | `arg0` | `uint256` | Index value of the coin. | **<>Source code▼** ```vyper BASE_COINS: public(immutable(DynArray[address, MAX_COINS])) @external def __init__( _name: String[32], _symbol: String[10], _A: uint256, _fee: uint256, _offpeg_fee_multiplier: uint256, _ma_exp_time: uint256, _math_implementation: address, _base_pool: address, _coins: DynArray[address, MAX_COINS], _base_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], ): ... BASE_COINS = _base_coins ... ``` **▶Example▼** ```shell >>> StableSwap.BASE_COINS(0) '0x6B175474E89094C44Da98b954EedeAC495271d0F' >>> StableSwap.BASE_COINS(1) '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48' >>> StableSwap.BASE_COINS(2) '0xdAC17F958D2ee523a2206206994597C13D831ec7' ``` <a id="coins"></a> ### `coins` `StableSwap.coins(arg0: uint256) -> addresss: view` Getter for the coin at index `arg0` within the metapool. coins\[0\] always return the coin paired against the basepool. Returns: coin (`address`). **<>Source code▼** ```vyper coins: public(immutable(DynArray[address, MAX_COINS])) @external def __init__( _name: String[32], _symbol: String[10], _A: uint256, _fee: uint256, _offpeg_fee_multiplier: uint256, _ma_exp_time: uint256, _math_implementation: address, _base_pool: address, _coins: DynArray[address, M
프로젝트로 돌아가기