Curve DAO 백서

Curve DAO

원문

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

s to withdraw | | `max_burn_amount` | `uint256` | Maximum amount of LP token to burn in the withdrawal | Emits: Transfer RemoveLiquidityImbalance **<>Source code▼** ```vyper @private def _send_all(_addr: address, min_uamounts: uint256[N_COINS], one: int128): use_lending: bool[N_COINS] = USE_LENDING tethered: bool[N_COINS] = TETHERED for i in range(N_COINS): if (one < 0) or (i == one): if use_lending[i]: _coin: address = self.coins[i] _balance: uint256 = cERC20(_coin).balanceOf(self) if _balance == 0: # Do nothing if there are 0 coins continue ok: uint256 = cERC20(_coin).redeem(_balance) if ok > 0: raise "Could not redeem coin" _ucoin: address = self.underlying_coins[i] _uamount: uint256 = ERC20(_ucoin).balanceOf(self) assert _uamount >= min_uamounts[i], "Not enough coins withdrawn" # Send only if we have something to send if _uamount >= 0: if tethered[i]: USDT(_ucoin).transfer(_addr, _uamount) else: assert_modifiable(ERC20(_ucoin).transfer(_addr, _uamount)) @public @nonreentrant('lock') def remove_liquidity_imbalance(uamounts: uint256[N_COINS], max_burn_amount: uint256): """ Get max_burn_amount in, remove requested liquidity and transfer back what is left """ use_lending: bool[N_COINS] = USE_LENDING tethered: bool[N_COINS] = TETHERED _token: address = self.token amounts: uint256[N_COINS] = uamounts for i in range(N_COINS): if use_lending[i] and amounts[i] > 0: rate: uint256 = cERC20(self.coins[i]).exchangeRateCurrent() amounts[i] = amounts[i] * LENDING_PRECISION / rate # if not use_lending - all good already # Transfrer max tokens in _tokens: uint256 = ERC20(_token).balanceOf(msg.sender) if _tokens > max_burn_amount: _tokens = max_burn_amount assert_modifiable(ERC20(_token).transferFrom(msg.sender, self, _tokens)) Curve(self.curve).remove_liquidity_imbalance(amounts, max_burn_amount) # Transfer unused tokens back _tokens = ERC20(_token).balanceOf(self) assert_modifiable(ERC20(_token).transfer(msg.sender, _tokens)) # Unwrap and transfer all the coins we've got self._send_all(msg.sender, ZEROS, -1) ``` ```vyper @public @nonreentrant('lock') def remove_liquidity_imbalance(amounts: uint256[N_COINS], max_burn_amount: uint256): assert not self.is_killed tethered: bool[N_COINS] = TETHERED use_lending: bool[N_COINS] = USE_LENDING token_supply: uint256 = self.token.totalSupply() assert token_supply > 0 _fee: uint256 = self.fee * N_COINS / (4 * (N_COINS - 1)) _admin_fee: uint256 = self.admin_fee rates: uint256[N_COINS] = self._current_rates() old_balances: uint256[N_COINS] = self.balances new_balances: uint256[N_COINS] = old_balances D0: uint256 = self.get_D_mem(rates, old_balances) for i in range(N_COINS): new_balances[i] -= amounts[i] D1: uint256 = self.get_D_mem(rates, new_balances) fees: uint256[N_COINS] = ZEROS for i in range(N_COINS): ideal_balance: uint256 = D1 * old_balances[i] / D0 difference: uint256 = 0 if ideal_balance > new_balances[i]: difference = ideal_balance - new_balances[i] else: difference = new_balances[i] - ideal_balance fees[i] = _fee * difference / FEE_DENOMINATOR self.balances[i] = new_balances[i] - (fees[i] * _admin_fee / FEE_DENOMINATOR) new_balances[i] -= fees[i] D2: uint256 = self.get_D_mem(rates, new_balances) token_amount: uint256 = (D0 - D2) * token_supply / D0 assert token_amount > 0 assert token_amount <= max_burn_amount, "Slippage screwed you" for i in range(N_COINS): if tethered[i] and not use_lending[i]: USDT(self.coins[i]).transfer(msg.sender, amounts[i]) else: assert_modifiable(cERC20(self.coins[i]).transfer(msg.sender, amounts[i])) self.token.burnFrom(msg.sender, token_amount) # Will raise if not enough log.RemoveLiquidityImbalance(msg.sender, amounts, fees, D1, token_supply - token_amount) ``` ```vyper @private def _burn(_to: address, _value: uint256): """ @dev Internal function that burns an amount of the token of a given account. @param _to The account whose tokens will be burned. @param _value The amount that will be burned. """ assert _to != ZERO_ADDRESS self.total_supply -= _value self.balanceOf[_to] -= _value log.Transfer(_to, ZERO_ADDRESS, _value) ... @public def burnFrom(_to: address, _value: uint256): """ @dev Burn an amount of the token from a given account. @param _to The account whose tokens will be burned. @param _value The amount that will be burned. """ assert msg.sender == self.minter, "Only minter is allowed to burn" self._burn(_to, _value) ``` **▶Example▼** ```shell >>> todo: ``` <a id="depositzapremove_liquidity_one_coin"></a> ### `DepositZap.remove_liquidity_one_coin` `DepositZap.remove_liquidity_one_coin(_token_amount: uint256, i: int128, min_uamount: uint256, donate_dust: bool = False)` Withdraw and unwrap a single coin from the pool. | Input | Type | Description | | --- | --- | --- | | `_token_amount` | `uint256` | Amount of LP tokens to burn in the withdrawal | | `i` | `int128` | Index value of the coin to withdraw | | `min_uamount` | `uint256` | Minimum amount of underlying coin to receive | | `donate_dust` | `bool` | Donates collected dust liquidity to `msg.sender` | Emits: Transfer RemoveLiquidityImbalance **<>Source code▼** ```vyper @public @nonreentrant('lock') def remove_liquidity_one_coin(_token_amount: uint256, i: int128, min_uamount: uint256, donate_dust: bool = False): """ Remove _amount of liquidity all in a form of coin i """ use_lending: bool[N_COINS] = USE_LENDING rates: uint256[N_COINS] = ZEROS _token: address = self.token for j in range(N_COINS): if use_lending[j]: rates[j] = cERC20(self.coins[j]).exchangeRateCurrent() else: rates[j] = LENDING_PRECISION dy: uint256 = self._calc_withdraw_one_coin(_token_amount, i, rates) assert dy >= min_uamount, "Not enough coins removed" assert_modifiable( ERC20(self.token).transferFrom(msg.sender, self, _token_amount)) amounts: uint256[N_COINS] = ZEROS amounts[i] = dy * LENDING_PRECISION / rates[i] token_amount_before: uint256 = ERC20(_token).balanceOf(self) Curve(self.curve).remove_liquidity_imbalance(amounts, _token_amount) # Unwrap and transfer all the coins we've got self._send_all(msg.sender, ZEROS, i) if not donate_dust: # Transfer unused tokens back token_amount_after: uint256 = ERC20(_token).balanceOf(self) if token_amount_after > token_amount_before: assert_modifiable(ERC20(_token).transfer( msg.sender, token_amount_after - token_amount_before) ) ``` ```vyper @public @nonreentrant('lock') def remove_liquidity_imbalance(amounts: uint256[N_COINS], max_burn_amount: uint256): assert not self.is_killed tethered: bool[N_COINS] = TETHERED use_lending: bool[N_COINS] = USE_LENDING token_supply: uint256 = self.token.totalSupply() assert token_supply > 0 _fee: uint256 = self.fee * N_COINS / (4 * (N_COINS - 1)) _admin_fee: uint256 = self.admin_fee rates: uint256[N_COINS] = self._current_rates() old_balances: uint256[N_COINS] = self.balances new_balances: uint256[N_COINS] = old_balances D0: uint256 = self.get_D_mem(rates, old_balances) for i in range(N_COINS): new_balances[i] -= amounts[i] D1: uint256 = self.get_D_mem(rates, new_balances) fees: uint256[N_COINS] = ZEROS for i in range(N_COINS): ideal_balance: uint256 = D1 * old_balances[i] / D0 difference: uint256 = 0 if ideal_balance > new_balances[i]: difference = ideal_balance - new_balances[i] else: difference = new_balances[i] - ideal_balance fees[i] = _fee * difference / FEE_DENOMINATOR self.balances[i] = new_balances[i] - (fees[i] * _admin_fee / FEE_DENOMINATOR) new_balances[i] -= fees[i] D2: uint256 = self.get_D_mem(rates, new_balances) token_amount: uint256 = (D0 - D2) * token_supply / D0 assert token_amount > 0 assert token_amount <= max_burn_amount, "Slippage screwed you" for i in range(N_COINS): if tethered[i] and not use_lending[i]: USDT(self.coins[i]).transfer(msg.sender, amounts[i]) else: assert_modifiable(cERC20(self.coins[i]).transfer(msg.sender, amounts[i])) self.token.burnFrom(msg.sender, token_amount) # Will raise if not enough log.RemoveLiquidityImbalance(msg.sender, amounts, fees, D1, token_supply - token_amount) ``` ```vyper @private def _burn(_to: address, _value: uint256): """ @dev Internal function that burns an amount of the token of a given account. @param _to The account whose tokens will be burned. @param _value The amount that will be burned. """ assert _to != ZERO_ADDRESS self.total_supply -= _value self.balanceOf[_to] -= _value log.Transfer(_to, ZERO_ADDRESS, _value) ... @public def burnFrom(_to: address, _value: uint256): """ @dev Burn an amount of the token from a given account. @param _to The account whose tokens will be burned. @param _value The amount that will be burned. """ assert msg.sender == self.minter, "Only minter is allowed to burn" self._burn(_to, _value) ``` note The underlying pool method called when the older DepositZap contract's `remove_liquidity_one_coin` is called emits RemoveLiquidityImbalance whereas the newer contract emits RemoveLiquidityOne. This is because the older contracts do not have the `remove_liquidity_one_coin`, and instead use `remove_liquidity_imbalance`. **▶Example▼** ```shell >>> todo: ``` <a id="depositzapcalc_withdraw_one_coin"></a> ### `DepositZap.calc_withdraw_one_coin` `DepositZap.calc_withdraw_one_coin(_token_amount: uint256, i: int128) → uint256` Calculate the amount received when withdrawing a single underlying coin. | Input | Type | Description | | --- | --- | --- | | `_token_amount` | `uint256` | Amount of LP tokens to burn in the withdrawal | | `i` | `int128` | Index value of the coin to withdraw | **<>Source code▼** ```vyper @private @constant def _calc_withdraw_one_coin(_token_amount: uint256, i: int128, rates: uint256[N_COINS]) -> uint256: # First, need to calculate # * Get current D # * Solve Eqn against y_i for D - _token_amount use_lending: bool[N_COINS] = USE_LENDING # tethered: bool[N_COINS] = TETHERED crv: address = self.curve A: uint256 = Curve(crv).A() fee: uint256 = Curve(crv).fee() * N_COINS / (4 * (N_COINS - 1)) fee += fee * FEE_IMPRECISION / FEE_DENOMINATOR # Overcharge to account for imprecision precisions: uint256[N_COINS] = PRECISION_MUL total_supply: uint256 = ERC20(self.token).totalSupply() xp: uint256[N_COINS] = PRECISION_MUL S: uint256 = 0 for j in range(N_COINS): xp[j] *= Curve(crv).balances(j) if use_lending[j]: # Use stored rate b/c we have imprecision anyway xp[j] = xp[j] * rates[j] / LENDING_PRECISION S += xp[j] # if not use_lending - all good already D0: uint256 = self.get_D(A, xp) D1: uint256 = D0 - _token_amount * D0 / total_supply xp_reduced: uint256[N_COINS] = xp # xp = xp - fee * | xp * D1 / D0 - (xp - S * dD / D0 * (0, ... 1, ..0))| for j in range(N_COINS): dx_expected: uint256 = 0 b_ideal: uint256 = xp[j] * D1 / D0 b_expected: uint256 = xp[j] if j == i: b_expected -= S * (D0 - D1) / D0 if b_ideal >= b_expected: dx_expected = (b_ideal - b_expected) else: dx_expected = (b_expected - b_ideal) xp_reduced[j] -= fee * dx_expected / FEE_DENOMINATOR dy: uint256 = xp_reduced[i] - self.get_y(A, i, xp_reduced, D1) dy = dy / precisions[i] return dy @public @constant def calc_withdraw_one_coin(_token_amount: uint256, i: int128) -> uint256: rates: uint256[N_COINS] = ZEROS use_lending: bool[N_COINS] = USE_LENDING for j in range(N_COINS): if use_lending[j]: rates[j] = cERC20(self.coins[j]).exchangeRateStored() else: rates[j] = 10 **18 return self._calc_withdraw_one_coin(_token_amount, i, rates) ``` **▶Example▼** ```shell >>> todo: ``` <a id="depositzapwithdraw_donated_dust"></a> ### `DepositZap.withdraw_donated_dust` `DepositZap.withdraw_donated_dust()` Donates any LP tokens of the associated pool held by this contract to the contract owner. **<>Source code▼** ```vyper @public @nonreentrant('lock') def withdraw_donated_dust(): owner: address = Curve(self.curve).owner() assert msg.sender == owner _token: address = self.token assert_modifiable( ERC20(_token).transfer(owner, ERC20(_token).balanceOf(self))) ``` **▶Example▼** ```shell >>> todo: ``` <a id="deposit-zap-new"></a> ## Deposit Zap (New) Compared to the older deposit zaps, the newer zaps mainly optimize for gas efficiency. The API is only modified in part, specifically with regards to `return` values and variable naming. <a id="get-deposit-zap-information-1"></a> ## Get Deposit Zap Information <a id="depositzapcurve-1"></a> ### `DepositZap.curve` `DepositZap.curve() → address: view` Getter for the pool associated with this deposit contract. **<>Source code▼** ```vyper @external def __init__( _coins: address[N_COINS], _underlying_coins: address[N_COINS], _curve: address, _token: address ): """ @notice Contract constructor @dev Where a token does not use wrapping, use the same address for `_coins` and `_underlying_coins` @param _coins List of wrapped coin addresses @param _underlying_coins List of underlying coin addresses @param _curve Pool address @param _token Pool LP token address """ for i in range(N_COINS): assert _coins[i] != ZERO_ADDRESS assert _underlying_coins[i] != ZERO_ADDRESS # approve underlying and wrapped coins for infinite transfers _response: Bytes[32] = raw_call( _underlying_coins[i], concat( method_id("approve(address,uint256)"), convert(_coins[i], bytes32), convert(MAX_UINT256, bytes32), ), max_outsize=32, ) if len(_response) > 0: assert convert(_response, bool) _response = raw_call( _coins[i], concat( method_id("approve(address,uint256)"), convert(_curve, bytes32), convert(MAX_UINT256, bytes32), ), max_outsize=32, ) if len(_response) > 0: assert convert(_response, bool) self.coins = _coins self.underlying_coins = _underlying_coins self.curve = _curve self.lp_token = _token ``` **▶Example▼** ```shell >>> zap.curve() '0xA2B47E3D5c44877cca798226B7B8118F9BFb7A56' ``` <a id="depositzapunderlying_coins-1"></a> ### `DepositZap.underlying_coins` `DepositZap.underlying_coins(i: int128) → address: view` Getter for the array of underlying coins within the associated pool. | Input | Type | Description | | --- | --- | --- | | `i` | `int128` | Index of the underlying coin for which to get the address | **<>Source code▼** ```vyper underlying_coins: public(address[N_COINS]) ... @external def __init__( _coins: address[N_COINS], _underlying_coins: address[N_COINS], _curve: address, _token: address ): """ @notice Contract constructor @dev Where a token does not use wrapping, use the same address for `_coins` and `_underlying_coins` @param _coins List of wrapped coin addresses @param _underlying_coins List of underlying coin addresses @param _curve Pool address @param _token Pool LP token address """ for i in range(N_COINS): assert _coins[i] != ZERO_ADDRESS assert _underlying_coins[i] != ZERO_ADDRESS # approve underlying and wrapped coins for infinite transfers _response: Bytes[32] = raw_call( _underlying_coins[i], concat( method_id("approve(address,uint256)"), convert(_coins[i], bytes32), convert(MAX_UINT256, bytes32), ), max_outsize=32, ) if len(_response) > 0: assert convert(_response, bool) _response = raw_call( _coins[i], concat( method_id("approve(address,uint256)"), convert(_curve, bytes32), convert(MAX_UINT256, bytes32), ), max_outsize=32, ) if len(_response) > 0: assert convert(_response, bool) self.coins = _coins self.underlying_coins = _underlying_coins self.curve = _curve self.lp_token = _token ``` **▶Example▼** ```shell >>> zap.underlying_coins(0) '0x6B175474E89094C44Da98b954EedeAC495271d0F' >>> zap.underlying_coins(1) '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48' ``` <a id="depositzapcoins-1"></a> ### `DepositZap.coins` `DepositZap.coins(i: int128) → address: view` Getter for the array of wrapped coins within the associated pool. | Input | Type | Description | | --- | --- | --- | | `i` | `int128` | Index of the coin for which to get the address | **<>Source code▼** ```vyper coins: public(address[N_COINS]) ... @external def __init__( _coins: address[N_COINS], _underlying_coins: address[N_COINS], _curve: address, _token: address ): """ @notice Contract constructor @dev Where a token does not use wrapping, use the same address for `_coins` and `_underlying_coins` @param _coins List of wrapped coin addresses @param _underlying_coins List of underlying coin addresses @param _curve Pool address @param _token Pool LP token address """ for i in range(N_COINS): assert _coins[i] != ZERO_ADDRESS assert _underlying_coins[i] != ZERO_ADDRESS # approve underlying and wrapped coins for infinite transfers _response: Bytes[32] = raw_call( _underlying_coins[i], concat( method_id("approve(address,uint256)"), convert(_coins[i], bytes32), convert(MAX_UINT256, bytes32), ), max_outsize=32, ) if len(_response) > 0: assert convert(_response, bool) _response = raw_call( _coins[i], concat( method_id("approve(address,uint256)"), convert(_curve, bytes32), convert(MAX_UINT256, bytes32), ), max_outsize=32, ) if len(_response) > 0: assert convert(_response, bool) self.coins = _coins self.underlying_coins = _underlying_coins self.curve = _curve self.lp_token = _token ``` **▶Example▼** ```shell >>> zap.coins(0) '0x5d3a536E4D6DbD6114cc1Ead35777bAB948E3643' >>> zap.coins(1) '0x39AA39c021dfbaE8faC545936693aC917d5E7563' ``` <a id="depositzaptoken-1"></a> ### `DepositZap.token` `DepositZap.token() → address: view` Getter for the LP token of the associated pool. **<>Source code▼** ```vyper lp_token: public(address) ... @external def __init__( _coins: address[N_COINS], _underlying_coins: address[N_COINS], _curve: address, _token: address ): """ @notice Contract constructor @dev Where a token does not use wrapping, use the same address for `_coins` and `_underlying_coins` @param _coins List of wrapped coin addresses @param _underlying_coins List of underlying coin addresses @param _curve Pool address @param _token Pool LP token address """ for i in range(N_COINS): assert _coins[i] != ZERO_ADDRESS assert _underlying_coins[i] != ZERO_ADDRESS # approve underlying and wrapped coins for infinite transfers _response: Bytes[32] = raw_call( _underlying_coins[i], concat( method_id("approve(address,uint256)"), convert(_coins[i], bytes32), convert(MAX_UINT256, bytes32), ), max_outsize=32, ) if len(_response) > 0: assert convert(_response, bool) _response = raw_call( _coins[i], concat( method_id("approve(address,uint256)"), convert(_curve, bytes32), convert(MAX_UINT256, bytes32), ), max_outsize=32, ) if len(_response) > 0: assert convert(_response, bool) self.coins = _coins self.underlying_coins = _underlying_coins self.curve = _curve self.lp_token = _token ``` **▶Example▼** ```shell >>> zap.coins(0) '0x5d3a536E4D6DbD6114cc1Ead35777bAB948E3643' >>> zap.coins(1) '0x39AA39c021dfbaE8faC545936693aC917d5E7563' ``` <a id="addremove-liquidity-1"></a> ## Add/Remove Liquidity <a id="depositzapadd_liquidity-1"></a> ### `DepositZap.add_liquidity` `DepositZap.add_liquidity(_underlying_amounts: uint256[N_COINS], _min_mint_amount: uint256) -> uint256` Wrap underlying coins and deposit them in the pool. Returns the amount of LP token received in exchange for the deposited amounts. | Input | Type | Description | | --- | --- | --- | | `_underlying_amounts` | `uint256[N_COINS]` | List of amounts of underlying coins to deposit | | `_min_mint_amount` | `uint256` | Minimum amount of LP token to mint from the deposit | Emits: Transfer AddLiquidity **<>Source code▼** ```vyper @public @nonreentrant('lock') def add_liquidity(uamounts: uint256[N_COINS], min_mint_amount: uint256): tethered: bool[N_COINS] = TETHERED amounts: uint256[N_COINS] = ZEROS for i in range(N_COINS): uamount: uint256 = uamounts[i] if uamount > 0: # Transfer the underlying coin from owner if tethered[i]: USDT(self.underlying_coins[i]).transferFrom( msg.send
프로젝트로 돌아가기