Curve DAO 백서

Curve DAO

원문

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

led prior to swapping if not self.is_settled[_token_id]: currency_key: bytes32 = self.currency_keys[synth] self.exchanger.settle(settler, currency_key) self.is_settled[_token_id] = True # use Curve to exchange the synth for another asset which is sent to the receiver remaining: uint256 = Settler(settler).exchange(_to, pool, _amount, _expected, _receiver) # if the balance of the synth within the NFT is now zero, burn the NFT if remaining == 0: self.id_to_owner[_token_id] = ZERO_ADDRESS self.id_to_approval[_token_id] = ZERO_ADDRESS self.is_settled[_token_id] = False self.owner_to_token_count[msg.sender] -= 1 count: uint256 = self.id_count # add 2**160 to increment the nonce for next time this settler is used self.available_token_ids[count] = _token_id + 2**160 self.id_count = count + 1 owner = ZERO_ADDRESS synth = ZERO_ADDRESS log Transfer(msg.sender, ZERO_ADDRESS, _token_id) log TokenUpdate(_token_id, owner, synth, remaining) return remaining ``` **▶Example▼** ```shell >>> wbtc = Contract('0x2260fac5e5542a773aa44fbcfedf7c193bc2c599') >>> amount = synth_swap.token_info(token_id)['underlying_balance'] >>> expected = swynth_swap.get_swap_from_synth_amount(sbtc, wbtc, amount) * 0.99 >>> synth_swap.swap_from_synth(token_id, wbtc, amount, expected, {'from': alice}) Transaction sent: 0x83b311af19be08b8ec6241c3e834ccdf3b22586971de82a76a641e43bdf2b3ee Gas price: 20 gwei Gas limit: 800000 Nonce: 6 ``` <a id="synthswapwithdraw"></a> ### `SynthSwap.withdraw` `StableSwap.withdraw(_token_id: uint256, _amount: uint256, _receiver: address = msg.sender) → uint256: nonpayable` Withdraw the underlying synth represented by an NFT. Callable by the owner or operator of `_token_id` after the synth settlement period has passed. If `_amount` is equal to the total remaining balance of the synth represented by the NFT, the NFT is burned. Returns the remaining balance of the underlying synth within the active NFT. | Input | Type | Description | | --- | --- | --- | | `_token_id` | `uint256` | The identifier for an NFT | | `_amount` | `uint256` | Amount of the underlying synth to swap | | `_receiver` | `address` | Address of the recipient of the withdrawn synth. Defaults to the `msg.sender`. | Emits: Transfer TokenUpdate **<>Source code▼** ```vyper @external def withdraw(_token_id: uint256, _amount: uint256, _receiver: address = msg.sender) -> uint256: """ @notice Withdraw the synth represented by an NFT. @dev Callable by the owner or operator of `_token_id` after the synth settlement period has passed. If `_amount` is equal to the entire balance within the NFT, the NFT is burned. @param _token_id The identifier for an NFT @param _amount Amount of the synth to withdraw @param _receiver Address of the recipient of the synth, if not given defaults to `msg.sender` @return uint256 Synth balance remaining in `_token_id` """ owner: address = self.id_to_owner[_token_id] if msg.sender != self.id_to_owner[_token_id]: assert owner != ZERO_ADDRESS, "Unknown Token ID" assert ( self.owner_to_operators[owner][msg.sender] or msg.sender == self.id_to_approval[_token_id] ), "Caller is not owner or operator" settler: address = convert(_token_id % (2**160), address) synth: address = Settler(settler).synth() # ensure the synth is settled prior to withdrawal if not self.is_settled[_token_id]: currency_key: bytes32 = self.currency_keys[synth] self.exchanger.settle(settler, currency_key) self.is_settled[_token_id] = True remaining: uint256 = Settler(settler).withdraw(_receiver, _amount) # if the balance of the synth within the NFT is now zero, burn the NFT if remaining == 0: self.id_to_owner[_token_id] = ZERO_ADDRESS self.id_to_approval[_token_id] = ZERO_ADDRESS self.is_settled[_token_id] = False self.owner_to_token_count[msg.sender] -= 1 count: uint256 = self.id_count # add 2**160 to increment the nonce for next time this settler is used self.available_token_ids[count] = _token_id + 2**160 self.id_count = count + 1 owner = ZERO_ADDRESS synth = ZERO_ADDRESS log Transfer(msg.sender, ZERO_ADDRESS, _token_id) log TokenUpdate(_token_id, owner, synth, remaining) return remaining ``` **▶Example▼** ```shell >>> amount = synth_swap.token_info(token_id)['underlying_balance'] >>> synth_swap.withdraw(token_id, amount, {'from': alice}) Transaction sent: 0x83b311af19be08b8ec6241c3e834ccdf3b22586971de82a76a641e43bdf2b3ee Gas price: 20 gwei Gas limit: 800000 Nonce: 6 ``` <a id="synthswapsettle"></a> ### `SynthSwap.settle` `StableSwap.settle(_token_id: uint256) → bool: nonpayable` Settle the synth represented in an NFT. Note that settlement is performed when swapping or withdrawing, there is no requirement to call this function separately. Returns `True`. | Input | Type | Description | | --- | --- | --- | | `_token_id` | `uint256` | The identifier for an NFT | **<>Source code▼** ```vyper @external def settle(_token_id: uint256) -> bool: """ @notice Settle the synth represented in an NFT. @dev Settlement is performed when swapping or withdrawing, there is no requirement to call this function separately. @param _token_id The identifier for an NFT @return bool Success """ if not self.is_settled[_token_id]: assert self.id_to_owner[_token_id] != ZERO_ADDRESS, "Unknown Token ID" settler: address = convert(_token_id % (2**160), address) synth: address = Settler(settler).synth() currency_key: bytes32 = self.currency_keys[synth] self.exchanger.settle(settler, currency_key) # dev: settlement failed self.is_settled[_token_id] = True return True ``` --- # lending-pool-deposits Source: [https://docs.curve.finance/developer/amm/legacy/stableswap/deposit-contracts/lending-pool-deposits](https://docs.curve.finance/developer/amm/legacy/stableswap/deposit-contracts/lending-pool-deposits) While Curve lending pools support swaps in both the wrapped *and* underlying coins, not all lending pools allow liquidity providers to deposit or withdraw the underlying coin. For example, the `Compound` Pool allows swaps between `cDai` and `cUSDC` (wrapped coins), as well as swaps between `DAI` and `USDC` (underlying coins). However, liquidity providers are not able to deposit `DAI` or `USDC` to the pool directly. The main reason for why this is not supported by all Curve lending pools lies in the [size limit of contracts](https://github.com/ethereum/EIPs/blob/master/EIPS/eip-170.md). Lending pools may differ in complexity and can end up being very close to the contract byte code size limit. In order to overcome this restriction, liquidity can be added and removed to and from a lending pool in the underlying coins via a different contract, called a deposit zap, tailored to lending pools. For an overview of the Curve lending pool implementation, please refer to the Lending Pool section. The template source code for a lending pool deposit zap may be viewed on [GitHub](https://github.com/curvefi/curve-contract/blob/master/contracts/pool-templates/y/DepositTemplateY.vy). note Lending pool deposit zaps may differ in their API. Older pools do not implement the [newer API template](https://github.com/curvefi/curve-contract/blob/master/contracts/pool-templates/y/DepositTemplateY.vy). # Deposit Zap (Old) Older Curve lending pool deposit zaps do not implement the [template API](https://github.com/curvefi/curve-contract/blob/master/contracts/pool-templates/y/DepositTemplateY.vy). The deposit zaps which employ an older API are: - `DepositBUSD`: [BUSD pool deposit zap](https://etherscan.io/address/0xb6c057591e073249f2d9d88ba59a46cfc9b59edb#code) - `DepositCompound`: [Compound pool deposit zap](https://etherscan.io/address/0xeb21209ae4c2c9ff2a86aca31e123764a3b6bc06#code) - `DepositPAX`: [PAX pool deposit zap](https://etherscan.io/address/0xa50ccc70b6a011cffddf45057e39679379187287#code) - `DepositUSDT`: [USDT pool deposit zap](https://etherscan.io/address/0xac795d2c97e60df6a99ff1c814727302fd747a80#code) - `DepositY`: [Y pool deposit zap](https://etherscan.io/address/0xbbc81d23ea2c3ec7e56d39296f0cbb648873a5d3#code) While not a lending pool, note that the following contract also implements the newer deposit zap API: - `DepositSUSD`: [SUSD pool deposit zap](https://etherscan.io/address/0xfcba3e75865d2d561be8d220616520c171f12851#code) note Getters generated for public arrays changed between Vyper `0.1.x` and `0.2.x` to accept `uint256` instead of `int128` in order to handle the lookups. Older deposit zap contracts (v1) use vyper `0.1.x...`, while newer zaps (v2) use vyper `0.2.x...`. The following Brownie console interaction examples are using the [Compound Pool Deposit Zap](https://etherscan.io/address/0xeb21209ae4c2c9ff2a86aca31e123764a3b6bc06). <a id="get-deposit-zap-information"></a> ## Get Deposit Zap Information <a id="depositzapcurve"></a> ### `DepositZap.curve` `DepositZap.curve() → address: view` Getter for the pool associated with this deposit contract. **<>Source code▼** ```vyper coins: public(address[N_COINS]) underlying_coins: public(address[N_COINS]) curve: public(address) token: public(address) ... @public def __init__(_coins: address[N_COINS], _underlying_coins: address[N_COINS], _curve: address, _token: address): self.coins = _coins self.underlying_coins = _underlying_coins self.curve = _curve self.token = _token ``` **▶Example▼** ```shell >>> zap.curve() '0xA2B47E3D5c44877cca798226B7B8118F9BFb7A56' ``` <a id="depositzapunderlying_coins"></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 N_COINS: constant(int128) = 4 ... coins: public(address[N_COINS]) underlying_coins: public(address[N_COINS]) curve: public(address) token: public(address) ... @public def __init__(_coins: address[N_COINS], _underlying_coins: address[N_COINS], _curve: address, _token: address): self.coins = _coins self.underlying_coins = _underlying_coins self.curve = _curve self.token = _token ``` **▶Example▼** ```shell >>> zap.underlying_coins(0) '0x6B175474E89094C44Da98b954EedeAC495271d0F' >>> zap.underlying_coins(1) '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48' ``` <a id="depositzapcoins"></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 N_COINS: constant(int128) = 4 ... coins: public(address[N_COINS]) underlying_coins: public(address[N_COINS]) curve: public(address) token: public(address) ... @public def __init__(_coins: address[N_COINS], _underlying_coins: address[N_COINS], _curve: address, _token: address): self.coins = _coins self.underlying_coins = _underlying_coins self.curve = _curve self.token = _token ``` **▶Example▼** ```shell >>> zap.coins(0) '0x5d3a536E4D6DbD6114cc1Ead35777bAB948E3643' >>> zap.coins(1) '0x39AA39c021dfbaE8faC545936693aC917d5E7563' ``` <a id="depositzaptoken"></a> ### `DepositZap.token` `DepositZap.token() → address: view` Getter for the LP token of the associated pool. **<>Source code▼** ```vyper coins: public(address[N_COINS]) underlying_coins: public(address[N_COINS]) curve: public(address) token: public(address) ... @public def __init__(_coins: address[N_COINS], _underlying_coins: address[N_COINS], _curve: address, _token: address): self.coins = _coins self.underlying_coins = _underlying_coins self.curve = _curve self.token = _token ``` **▶Example▼** ```shell >>> zap.coins(0) '0x5d3a536E4D6DbD6114cc1Ead35777bAB948E3643' >>> zap.coins(1) '0x39AA39c021dfbaE8faC545936693aC917d5E7563' ``` <a id="addremove-liquidity"></a> ## Add/Remove Liquidity <a id="depositzapadd_liquidity"></a> ### `DepositZap.add_liquidity` `DepositZap.add_liquidity(uamounts: uint256[N_COINS], min_mint_amount: uint256)` Wrap underlying coins and deposit them in the pool. | Input | Type | Description | | --- | --- | --- | | `uamounts` | `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: AddLiquidity Transfer **<>Source code▼** ```vyper USE_LENDING: constant(bool[N_COINS]) = [True, True] ... @public @nonreentrant('lock') def add_liquidity(uamounts: uint256[N_COINS], min_mint_amount: uint256): use_lending: bool[N_COINS] = USE_LENDING 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.sender, self, uamount) else: assert_modifiable(ERC20(self.underlying_coins[i])\ .transferFrom(msg.sender, self, uamount)) # Mint if needed if use_lending[i]: ERC20(self.underlying_coins[i]).approve(self.coins[i], uamount) ok: uint256 = cERC20(self.coins[i]).mint(uamount) if ok > 0: raise "Could not mint coin" amounts[i] = cERC20(self.coins[i]).balanceOf(self) ERC20(self.coins[i]).approve(self.curve, amounts[i]) else: amounts[i] = uamount ERC20(self.underlying_coins[i]).approve(self.curve, uamount) Curve(self.curve).add_liquidity(amounts, min_mint_amount) tokens: uint256 = ERC20(self.token).balanceOf(self) assert_modifiable(ERC20(self.token).transfer(msg.sender, tokens)) ``` ```vyper @public @nonreentrant('lock') def add_liquidity(amounts: uint256[N_COINS], min_mint_amount: uint256): # Amounts is amounts of c-tokens assert not self.is_killed tethered: bool[N_COINS] = TETHERED use_lending: bool[N_COINS] = USE_LENDING fees: uint256[N_COINS] = ZEROS _fee: uint256 = self.fee * N_COINS / (4 * (N_COINS - 1)) _admin_fee: uint256 = self.admin_fee token_supply: uint256 = self.token.totalSupply() rates: uint256[N_COINS] = self._current_rates() # Initial invariant D0: uint256 = 0 old_balances: uint256[N_COINS] = self.balances if token_supply > 0: D0 = self.get_D_mem(rates, old_balances) new_balances: uint256[N_COINS] = old_balances for i in range(N_COINS): if token_supply == 0: assert amounts[i] > 0 # balances store amounts of c-tokens new_balances[i] = old_balances[i] + amounts[i] # Invariant after change D1: uint256 = self.get_D_mem(rates, new_balances) assert D1 > D0 # We need to recalculate the invariant accounting for fees # to calculate fair user's share D2: uint256 = D1 if token_supply > 0: # Only account for fees if we are not the first to deposit 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 = self.get_D_mem(rates, new_balances) else: self.balances = new_balances # Calculate, how much pool tokens to mint mint_amount: uint256 = 0 if token_supply == 0: mint_amount = D1 # Take the dust if there was any else: mint_amount = token_supply * (D2 - D0) / D0 assert mint_amount >= min_mint_amount, "Slippage screwed you" # Take coins from the sender for i in range(N_COINS): if tethered[i] and not use_lending[i]: USDT(self.coins[i]).transferFrom(msg.sender, self, amounts[i]) else: assert_modifiable( cERC20(self.coins[i]).transferFrom(msg.sender, self, amounts[i])) # Mint pool tokens self.token.mint(msg.sender, mint_amount) log.AddLiquidity(msg.sender, amounts, fees, D1, token_supply + mint_amount) ``` ```vyper @public def mint(_to: address, _value: uint256): """ @dev Mint an amount of the token and assigns it to an account. This encapsulates the modification of balances such that the proper events are emitted. @param _to The account that will receive the created tokens. @param _value The amount that will be created. """ assert msg.sender == self.minter assert _to != ZERO_ADDRESS self.total_supply += _value self.balanceOf[_to] += _value log.Transfer(ZERO_ADDRESS, _to, _value) ``` **▶Example▼** ```shell >>> todo: ``` <a id="depositzapremove_liquidity"></a> ### `DepositZap.remove_liquidity` `DepositZap.remove_liquidity(_amount: uint256, min_uamounts: uint256[N_COINS])` Withdraw and unwrap coins from the pool. | Input | Type | Description | | --- | --- | --- | | `_amount` | `uint256` | Quantity of LP tokens to burn in the withdrawal | | `min_uamounts` | `uint256[N_COINS]` | Minimum amounts of underlying coins to receive | Emits: Transfer RemoveLiquidity **<>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(_amount: uint256, min_uamounts: uint256[N_COINS]): zeros: uint256[N_COINS] = ZEROS assert_modifiable(ERC20(self.token).transferFrom(msg.sender, self, _amount)) Curve(self.curve).remove_liquidity(_amount, zeros) self._send_all(msg.sender, min_uamounts, -1) ``` ```vyper @public @nonreentrant('lock') def remove_liquidity(_amount: uint256, min_amounts: uint256[N_COINS]): total_supply: uint256 = self.token.totalSupply() amounts: uint256[N_COINS] = ZEROS fees: uint256[N_COINS] = ZEROS tethered: bool[N_COINS] = TETHERED use_lending: bool[N_COINS] = USE_LENDING for i in range(N_COINS): value: uint256 = self.balances[i] * _amount / total_supply assert value >= min_amounts[i], "Withdrawal resulted in fewer coins than expected" self.balances[i] -= value amounts[i] = value if tethered[i] and not use_lending[i]: USDT(self.coins[i]).transfer(msg.sender, value) else: assert_modifiable(cERC20(self.coins[i]).transfer( msg.sender, value)) self.token.burnFrom(msg.sender, _amount) # Will raise if not enough log.RemoveLiquidity(msg.sender, amounts, fees, total_supply - _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_imbalance"></a> ### `DepositZap.remove_liquidity_imbalance` `DepositZap.remove_liquidity_imbalance(uamounts: uint256[N_COINS], max_burn_amount: uint256)` Withdraw and unwrap coins from the pool in an imbalanced amount. | Input | Type | Description | | --- | --- | --- | | `uamounts` | `uint256[N_COINS]` | List of amounts of underlying coin
프로젝트로 돌아가기