Curve DAO 백서

Curve DAO

원문

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

of admin fees ``` **▶Example▼** ```shell >>> CryptoSwap.xcp_profit_a() 1058927586013478083 ``` <a id="admin_fee"></a> ### `admin_fee` `CryptoSwap.admin_fee() -> uint256:` Getter for the admin fee of the pool. This value is hardcoded to 50% (5000000000). Returns: admin fee (`uint256`). **<>Source code▼** ```vyper admin_fee: public(uint256) ``` **▶Example▼** ```shell >>> CryptoSwap.admin_fee() 5000000000 ``` <a id="claim_admin_fees"></a> ### `claim_admin_fees` `CryptoSwap.admin_fee() -> uint256:` Function to claim admin fees from the pool and send them to the fee receiver. `fee_receiver` is set within the [Factory](https://etherscan.io/address/0xF18056Bbd320E96A48e3Fbf8bC061322531aac99). Emits: `ClaimAdminFee` **<>Source code▼** ```vyper event ClaimAdminFee: admin: indexed(address) tokens: uint256 @external @nonreentrant('lock') def claim_admin_fees(): self._claim_admin_fees() @internal def _claim_admin_fees(): A_gamma: uint256[2] = self._A_gamma() xcp_profit: uint256 = self.xcp_profit xcp_profit_a: uint256 = self.xcp_profit_a # Gulp here for i in range(N_COINS): coin: address = self.coins[i] if coin == WETH20: self.balances[i] = self.balance else: self.balances[i] = ERC20(coin).balanceOf(self) vprice: uint256 = self.virtual_price if xcp_profit > xcp_profit_a: fees: uint256 = (xcp_profit - xcp_profit_a) * self.admin_fee / (2 * 10**10) if fees > 0: receiver: address = Factory(self.factory).fee_receiver() if receiver != ZERO_ADDRESS: frac: uint256 = vprice * 10**18 / (vprice - fees) - 10**18 claimed: uint256 = CurveToken(self.token).mint_relative(receiver, frac) xcp_profit -= fees*2 self.xcp_profit = xcp_profit log ClaimAdminFee(receiver, claimed) total_supply: uint256 = CurveToken(self.token).totalSupply() # Recalculate D b/c we gulped D: uint256 = self.newton_D(A_gamma[0], A_gamma[1], self.xp()) self.D = D self.virtual_price = 10**18 * self.get_xcp(D) / total_supply if xcp_profit > xcp_profit_a: self.xcp_profit_a = xcp_profit ``` **▶Example▼** ```shell >>> CryptoSwap.claim_admin_fees() ``` <a id="price-scaling"></a> ## Price Scaling Curve v2 pools adaptively adjust liquidity to optimize depth near prevailing market prices, thereby reducing slippage. This is achieved by maintaining a continuous EMA (exponential moving average) of the pool's recent exchange rates (termed "internal oracle"), and relocating liquidity around this EMA when it's economically sensible for LPs. You can envision this mechanism as "resetting" the bonding curve to align the peak liquidity concentration (the curve's center) with the EMA. The price with the highest liquidity focus is termed the "price scale", while the ongoing EMA is labeled as the "price oracle." The price scaling parameters can be adjusted by the admin of the pool, see [here](https://docs.curve.finance/developer/amm/legacy/cryptoswap/pools/admin-controls.md). <a id="allowed_extra_profit"></a> ### `allowed_extra_profit` `CryptoSwap.allowed_extra_profit() -> uint256: view` Getter for the allowed extra profit. Returns: extra profit allowed (`uint256`). **<>Source code▼** ```vyper allowed_extra_profit: public(uint256) # 2 * 10**12 - recommended value ``` **▶Example▼** ```shell >>> CryptoSwap.allowed_extra_profit() 2000000000000 ``` <a id="adjustment_step"></a> ### `adjustment_step` `CryptoSwap.adjustment_step() -> uint256: view` Getter for the minimum size of price scale adjustments. Returns: adjustment step (`uint256`). **<>Source code▼** ```vyper adjustment_step: public(uint256) ``` **▶Example▼** ```shell >>> CryptoSwap.adjustment_step() 146000000000000 ``` <a id="bonding-curve-parameters"></a> ## Bonding Curve Parameters Similar to many AMMs, Curve v2 employs a bonding curve to determine asset prices based on the pool's availability of each asset. To centralize liquidity near the bonding curve's midpoint, Curve v2 utilizes an invariant that sits between the Stableswap (Curve v1) and the constant-product models (like Uniswap, Balancer, and others). The bonding curve parameters can be adjusted by the admin of the pool, see [here](https://docs.curve.finance/developer/amm/legacy/cryptoswap/pools/admin-controls.md). <a id="a"></a> ### `A` `CryptoSwap.A() -> uint256:` Getter for the current pool amplification value. Returns: A (`uint256`). **<>Source code▼** ```vyper @view @external def A() -> uint256: 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 = bitwise_and(A_gamma_1, 2**128-1) A1: uint256 = shift(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 # Less readable but more compact way of writing and converting to uint256 # gamma0: uint256 = bitwise_and(A_gamma_0, 2**128-1) # A0: uint256 = shift(A_gamma_0, -128) # A1 = A0 + (A1 - A0) * (block.timestamp - t0) / (t1 - t0) # gamma1 = gamma0 + (gamma1 - gamma0) * (block.timestamp - t0) / (t1 - t0) t1 -= t0 t0 = block.timestamp - t0 t2: uint256 = t1 - t0 A1 = (shift(A_gamma_0, -128) * t2 + A1 * t0) / t1 gamma1 = (bitwise_and(A_gamma_0, 2**128-1) * t2 + gamma1 * t0) / t1 return [A1, gamma1] ``` **▶Example▼** ```shell >>> CryptoSwap.A() 400000 ``` <a id="gamma"></a> ### `gamma` `CryptoSwap.gamma() -> uint256:` Getter for the current gamma value. Returns: gamma (`uint256`). **<>Source code▼** ```vyper @view @external def gamma() -> uint256: 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 = bitwise_and(A_gamma_1, 2**128-1) A1: uint256 = shift(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 # Less readable but more compact way of writing and converting to uint256 # gamma0: uint256 = bitwise_and(A_gamma_0, 2**128-1) # A0: uint256 = shift(A_gamma_0, -128) # A1 = A0 + (A1 - A0) * (block.timestamp - t0) / (t1 - t0) # gamma1 = gamma0 + (gamma1 - gamma0) * (block.timestamp - t0) / (t1 - t0) t1 -= t0 t0 = block.timestamp - t0 t2: uint256 = t1 - t0 A1 = (shift(A_gamma_0, -128) * t2 + A1 * t0) / t1 gamma1 = (bitwise_and(A_gamma_0, 2**128-1) * t2 + gamma1 * t0) / t1 return [A1, gamma1] ``` **▶Example▼** ```shell >>> CryptoSwap.gamma() 145000000000000 ``` <a id="contract-info-methods"></a> ## Contract Info Methods <a id="coins"></a> ### `coins` `CryptoSwap.coins(arg0: uint256) -> address: view` Getter for the coin at index `arg0`. Returns: coin (`address`). | Input | Type | Description | | --- | --- | --- | | `arg0` | `uint256` | index of coin | **<>Source code▼** ```vyper coins: public(address[N_COINS]) ``` **▶Example▼** ```shell >>> CryptoSwap.coins(0) '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2' ``` <a id="balances"></a> ### `balances` `CryptoSwap.balances(arg0: uint256) -> uint256: view` Getter for the pool balance of coin at index `arg0`. Returns: coin balance (`uint256`). | Input | Type | Description | | --- | --- | --- | | `arg0` | `uint256` | index of coin | **<>Source code▼** ```vyper coins: public(address[N_COINS]) ``` **▶Example▼** ```shell >>> CryptoSwap.balances(0) 669157518183204053847 ``` <a id="d"></a> ### `D` `CryptoSwap.D() -> uint256: view` Getter for the D invariant. Returns: D (`address`). **<>Source code▼** ```vyper D: public(uint256) ``` **▶Example▼** ```shell >>> CryptoSwap.D() 1386359478656960977136 ``` <a id="token"></a> ### `token` `CryptoSwap.token() -> uint256: view` Getter for the LP token contract. Returns: lp token (`address`). **<>Source code▼** ```vyper token: public(address) ``` **▶Example▼** ```shell >>> CryptoSwap.token() '0xb34e1a3D07f9D180Bc2FDb9Fd90B8994423e33c1' ``` <a id="factory"></a> ### `factory` `CryptoSwap.factory()` Getter for the factory contract. Returns: factory (`address`). **<>Source code▼** ```vyper factory: public(address) ``` **▶Example▼** ```shell >>> CryptoSwap.factory() '0xF18056Bbd320E96A48e3Fbf8bC061322531aac99' ``` <a id="internal-math-functions"></a> ## Internal Math Functions All these math functions are interally embedded into the contract. They can not be called externally. <a id="geometric_mean"></a> ### `geometric_mean` `CryptoSwap.geometric_mean(unsorted_x: uint256[N_COINS], sort: bool) -> uint256:` Function to calculate the geometric mean of a list of numbers in 1e18 precision. Returns: gemoetric mean (`uint256`). | Input | Type | Description | | --- | --- | --- | | `unsorted_x` | `uint256[N_COINS]` | array containing two values | | `sort` | `bool` | whether to sort or not | **<>Source code▼** ```vyper N_COINS: constant(int128) = 2 @internal @pure def geometric_mean(unsorted_x: uint256[N_COINS], sort: bool) -> uint256: """ (x[0] * x[1] * ...) **(1/N) """ x: uint256[N_COINS] = unsorted_x if sort and x[0] < x[1]: x = [unsorted_x[1], unsorted_x[0]] D: uint256 = x[0] diff: uint256 = 0 for i in range(255): D_prev: uint256 = D # tmp: uint256 = 10**18 # for _x in x: # tmp = tmp * _x / D # D = D * ((N_COINS - 1) * 10**18 + tmp) / (N_COINS * 10**18) # line below makes it for 2 coins D = (D + x[0] * x[1] / D) / N_COINS if D > D_prev: diff = D - D_prev else: diff = D_prev - D if diff <= 1 or diff * 10**18 < D: return D raise "Did not converge" ``` <a id="newton_d"></a> ### `newton_D` `CryptoSwap.newton_D(ANN: uint256, gamma: uint256, x_unsorted: uint256[N_COINS]) -> uint256:` Function to find the D invariant using Newton method. Returns: D invariant (`uint256`). | Input | Type | Description | | --- | --- | --- | | `AMN` | `uint256` | `ANN = A * N**N` | | `gamma` | `uint256` | `AMM.gamma()` value | | `x_unsorted` | `uint256[N_COINS]` | unsorted array of coin balances | **<>Source code▼** ```vyper N_COINS: constant(int128) = 2 @internal @view def newton_D(ANN: uint256, gamma: uint256, x_unsorted: uint256[N_COINS]) -> uint256: """ Finding the invariant using Newton method. ANN is higher by the factor A_MULTIPLIER ANN is already A * N**N Currently uses 60k gas """ # Safety checks assert ANN > MIN_A - 1 and ANN < MAX_A + 1 # dev: unsafe values A assert gamma > MIN_GAMMA - 1 and gamma < MAX_GAMMA + 1 # dev: unsafe values gamma # Initial value of invariant D is that for constant-product invariant x: uint256[N_COINS] = x_unsorted if x[0] < x[1]: x = [x_unsorted[1], x_unsorted[0]] assert x[0] > 10**9 - 1 and x[0] < 10**15 * 10**18 + 1 # dev: unsafe values x[0] assert x[1] * 10**18 / x[0] > 10**14-1 # dev: unsafe values x[i] (input) D: uint256 = N_COINS * self.geometric_mean(x, False) S: uint256 = x[0] + x[1] for i in range(255): D_prev: uint256 = D # K0: uint256 = 10**18 # for _x in x: # K0 = K0 * _x * N_COINS / D # collapsed for 2 coins K0: uint256 = (10**18 * N_COINS**2) * x[0] / D * x[1] / D _g1k0: uint256 = gamma + 10**18 if _g1k0 > K0: _g1k0 = _g1k0 - K0 + 1 else: _g1k0 = K0 - _g1k0 + 1 # D / (A * N**N) * _g1k0**2 / gamma**2 mul1: uint256 = 10**18 * D / gamma * _g1k0 / gamma * _g1k0 * A_MULTIPLIER / ANN # 2*N*K0 / _g1k0 mul2: uint256 = (2 * 10**18) * N_COINS * K0 / _g1k0 neg_fprime: uint256 = (S + S * mul2 / 10**18) + mul1 * N_COINS / K0 - mul2 * D / 10**18 # D -= f / fprime D_plus: uint256 = D * (neg_fprime + S) / neg_fprime D_minus: uint256 = D*D / neg_fprime if 10**18 > K0: D_minus += D * (mul1 / neg_fprime) / 10**18 * (10**18 - K0) / K0 else: D_minus -= D * (mul1 / neg_fprime) / 10**18 * (K0 - 10**18) / K0 if D_plus > D_minus: D = D_plus - D_minus else: D = (D_minus - D_plus) / 2 diff: uint256 = 0 if D > D_prev: diff = D - D_prev else: diff = D_prev - D if diff * 10**14 < max(10**16, D): # Could reduce precision for gas efficiency here # Test that we are safe with the next newton_y for _x in x: frac: uint256 = _x * 10**18 / D assert (frac > 10**16 - 1) and (frac < 10**20 + 1) # dev: unsafe values x[i] return D raise "Did not converge" ``` <a id="newton_y"></a> ### `newton_y` `CryptoSwap.newton_y(ANN: uint256, gamma: uint256, x: uint256[N_COINS], D: uint256, i: uint256) -> uint256:` Function to calculate x\[i\] given balances `x` and invariant D. Returns: y (`uint256`). | Input | Type | Description | | --- | --- | --- | | `AMN` | `uint256` | `ANN = A * N**N` | | `gamma` | `uint256` | `AMM.gamma()` value | | `x` | `uint256[N_COINS]` | array containing coin balances | | `D` | `uint256` | D invariant | | `i` | `uint256` | coin index to calculate x\[i\] for | **<>Source code▼** ```vyper N_COINS: constant(int128) = 2 @internal @pure def newton_y(ANN: uint256, gamma: uint256, x: uint256[N_COINS], D: uint256, i: uint256) -> uint256: """ Calculating x[i] given other balances x[0..N_COINS-1] and invariant D ANN = A * N**N """ # Safety checks assert ANN > MIN_A - 1 and ANN < MAX_A + 1 # dev: unsafe values A assert gamma > MIN_GAMMA - 1 and gamma < MAX_GAMMA + 1 # dev: unsafe values gamma assert D > 10**17 - 1 and D < 10**15 * 10**18 + 1 # dev: unsafe values D x_j: uint256 = x[1 - i] y: uint256 = D**2 / (x_j * N_COINS**2) K0_i: uint256 = (10**18 * N_COINS) * x_j / D # S_i = x_j # frac = x_j * 1e18 / D => frac = K0_i / N_COINS assert (K0_i > 10**16*N_COINS - 1) and (K0_i < 10**20*N_COINS + 1) # dev: unsafe values x[i] # x_sorted: uint256[N_COINS] = x # x_sorted[i] = 0 # x_sorted = self.sort(x_sorted) # From high to low # x[not i] instead of x_sorted since x_soted has only 1 element convergence_limit: uint256 = max(max(x_j / 10**14, D / 10**14), 100) for j in range(255): y_prev: uint256 = y K0: uint256 = K0_i * y * N_COINS / D S: uint256 = x_j + y _g1k0: uint256 = gamma + 10**18 if _g1k0 > K0: _g1k0 = _g1k0 - K0 + 1 else: _g1k0 = K0 - _g1k0 + 1 # D / (A * N**N) * _g1k0**2 / gamma**2 mul1: uint256 = 10**18 * D / gamma * _g1k0 / gamma * _g1k0 * A_MULTIPLIER / ANN # 2*K0 / _g1k0 mul2: uint256 = 10**18 + (2 * 10**18) * K0 / _g1k0 yfprime: uint256 = 10**18 * y + S * mul2 + mul1 _dyfprime: uint256 = D * mul2 if yfprime < _dyfprime: y = y_prev / 2 continue else: yfprime -= _dyfprime fprime: uint256 = yfprime / y # y -= f / f_prime; y = (y * fprime - f) / fprime # y = (yfprime + 10**18 * D - 10**18 * S) // fprime + mul1 // fprime * (10**18 - K0) // K0 y_minus: uint256 = mul1 / fprime y_plus: uint256 = (yfprime + 10**18 * D) / fprime + y_minus * 10**18 / K0 y_minus += 10**18 * S / fprime if y_plus < y_minus: y = y_prev / 2 else: y = y_plus - y_minus diff: uint256 = 0 if y > y_prev: diff = y - y_prev else: diff = y_prev - y if diff < max(convergence_limit, y / 10**14): frac: uint256 = y * 10**18 / D assert (frac > 10**16 - 1) and (frac < 10**20 + 1) # dev: unsafe value for y return y raise "Did not converge" ``` <a id="halfpow"></a> ### `halfpow` `CryptoSwap.halfpow(power: uint256) -> uint256:` Function to calculate the halfpow. Returns: halfpow (`uint256`). | Input | Type | Description | | --- | --- | --- | | `power` | `uint256` | value | **<>Source code▼** ```vyper @internal @pure def halfpow(power: uint256) -> uint256: """ 1e18 * 0.5 **(power/1e18) Inspired by: https://github.com/balancer-labs/balancer-core/blob/master/contracts/BNum.sol#L128 """ intpow: uint256 = power / 10**18 otherpow: uint256 = power - intpow * 10**18 if intpow > 59: return 0 result: uint256 = 10**18 / (2**intpow) if otherpow == 0: return result term: uint256 = 10**18 x: uint256 = 5 * 10**17 S: uint256 = 10**18 neg: bool = False for i in range(1, 256): K: uint256 = i * 10**18 c: uint256 = K - 10**18 if otherpow > c: c = otherpow - c neg = not neg else: c -= otherpow term = term * (c * x / 10**18) / K if neg: S -= term else: S += term if term < EXP_PRECISION: return result * S / 10**18 raise "Did not converge" ``` --- # deployer-api Source: [https://docs.curve.finance/developer/amm/legacy/factory/cryptoswap/deployer-api](https://docs.curve.finance/developer/amm/legacy/factory/cryptoswap/deployer-api) <a id="liquidity-poolswarning"></a> ## Liquidity Pools:::warning The transaction will revert if the following requirements are not met. ::: <a id="deploy_pool"></a> ### `deploy_pool` The pool **deployment is permissionless**, but it must adhere to certain parameter limitations: | Parameter | Limitation | | --- | --- | | `A` | A\_min - 1 < A < A\_max + 1 | | `gamma` | gamma\_min - 1 < gamma < gamma\_max + 1 | | `mid_fee` | fee\_min - 1 < mid\_fee < fee\_max - 1 | | `out_fee` | out\_fee >= mid\_fee AND out\_fee < fee\_max - 1 | | `admin_fee` | < 10^18 + 1 | | `allowed_extra_profit` | allowed\_extra\_profit < 10^16 + 1 | | `fee_gamma` | 0 < fee\_gamma < 10^18 + 1 | | `adjustment_step` | 0 < adjustment\_step < 10^18 + 1 | | `ma_half_time` | 0 < ma\_half\_time < 604800 | | `initial_price` | 10^6 < initial\_price < 10^30 | - No duplicate coins. - Only two coins. - Maximum of 18 decimals of a coin. *With:* | Parameters | Value | | --- | --- | | n\_coins | 2 | | A\_multiplier | 10000 | | A\_min | (n\_coins^n\_coins \* A\_multiplier) / 10 = 4000 | | A\_max | n\_coins^n\_coins \* A\_multiplier \* 100000 = 4000000000 | | gamma\_min | 10^10 = 10000000000 | | gamma\_max | 2 \* 10^16 = 20000000000000000 | | fee\_min | 5 \* 10^5 = 500000 | | fee\_max | 10 \* 10^9 = 10000000000 | `Factory.deploy_pool(_name: String[32], _symbol: String[10], _coins: address[2], A: uint256, gamma: uint256, mid_fee: uint256, out_fee: uint256, allowed_extra_profit: uint256, fee_gamma: uint256, adjustment_step: uint256, admin_fee: uint256, ma_half_time: uint256, initial_price: uint256) -> address:` Function to deploy a cryptoswap pool form the `pool_implementations`. This function will also deploy the according LP token from the `token_implementation`. Returns: Deployed pool (`address`). Emits: `CryptoPoolDeployed` | Input | Type | Description | | --- | --- | --- | | `_name` | `String[32]` | Name of the new plain pool | | `_symbol` | `String[10]` | Symbol for the new metapool’s LP token. This value will be concatenated with the factory symbol. | | `_coins` | `address[2]` | List of addresses of the coins being used in the pool | | `A` | `uint256` | Amplification coefficient | | `gamma` | `uint256` | Gamma | | `mid_fee` | `uint256` | Mid fee | | `out_fee` | `uint256` | Out fee | | `allowed_extra_profit` | `uint256` | Allowed extra profit | | `fee_gamma` | `uint256` | Fee Gamma | | `adjustment_step` | `uint256` | Adjustment step | | `admin_fee` | `uint256` | Admin fee | | `ma_half_time` | `uint256` | Moving-Average half time | | `initial_price` | `uint256` | Initial price | **<>Source code▼** ```vyper event CryptoPoolDeployed: token: address coins: address[2] A: uint256 gamma: uint256 mid_fee: uint256 out_fee: uint256 allowed_extra_profit: uint256 fee_gamma: uint256 adjustment_step: uint256 admin_fee: uint256 ma_half_time: uint256 initial_price: uint256 deployer: address N_COINS: constant(int128) = 2 A_MULTIPLIER: constant(uint256) = 10000 # Limits MAX_ADMIN_FEE: constant(uint256) = 10 * 10 **9 MIN_FEE: constant(uint256) = 5 * 10 **5 # 0.5 bps MAX_FEE: constant(uint256) = 10 * 10 **9 MIN_GAMMA: constant(uint256) = 10 **10 MAX_GAMMA: constant(uint256) = 2 * 10 **16 MIN_A: constant(uint256) = N_COINS **N_COINS * A_MULTIPLIER / 10 MAX_A: constant(uint256) = N_COINS **N_COINS * A_MULTIPLIER * 100000 @external def deploy_pool( _name: String[32], _symbol: String[10], _coins: address[2], A: uint256, gamma: uint256, mid_fee: uint256, out_fee: uint256, allowed_extra_profit: uint256, fee_gamma: uint256, adjustment_step: uint256, admin_fee: uint256, ma_half_time: uint256, initial_price: uint256 ) -> address: """ @notice Deploy a new pool @param _name Name of the new plain pool @param _symbol Symbol for the new plain pool - will be concatenated with facto
프로젝트로 돌아가기