Curve DAO 백서

Curve DAO

원문

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

-router-ng.md#get_dx). Contract Source & Deployment The `CurveRouterNG` contract has been deployed on most chains where Curve liquidity pools exist. For a full list of all deployments, see [here](https://docs.curve.finance/developer/deployments.md). Source code for the contract can be found on [GitHub](https://github.com/curvefi/curve-router-ng/tree/master/contracts). The contract utilizes **interfaces for all relevant Curve pools**, such as Stableswap, CryptoSwap, LLAMMA, and others, to execute swaps. **▶Interfaces▼** ```vyper interface StablePool: def exchange(i: int128, j: int128, dx: uint256, min_dy: uint256): payable def exchange_underlying(i: int128, j: int128, dx: uint256, min_dy: uint256): payable def get_dy(i: int128, j: int128, amount: uint256) -> uint256: view def get_dy_underlying(i: int128, j: int128, amount: uint256) -> uint256: view def coins(i: uint256) -> address: view def calc_withdraw_one_coin(token_amount: uint256, i: int128) -> uint256: view def remove_liquidity_one_coin(token_amount: uint256, i: int128, min_amount: uint256): nonpayable interface CryptoPool: def exchange(i: uint256, j: uint256, dx: uint256, min_dy: uint256): payable def exchange_underlying(i: uint256, j: uint256, dx: uint256, min_dy: uint256): payable def get_dy(i: uint256, j: uint256, amount: uint256) -> uint256: view def get_dy_underlying(i: uint256, j: uint256, amount: uint256) -> uint256: view def calc_withdraw_one_coin(token_amount: uint256, i: uint256) -> uint256: view def remove_liquidity_one_coin(token_amount: uint256, i: uint256, min_amount: uint256): nonpayable interface CryptoPoolETH: def exchange(i: uint256, j: uint256, dx: uint256, min_dy: uint256, use_eth: bool): payable interface LendingBasePoolMetaZap: def exchange_underlying(pool: address, i: int128, j: int128, dx: uint256, min_dy: uint256): nonpayable interface CryptoMetaZap: def get_dy(pool: address, i: uint256, j: uint256, dx: uint256) -> uint256: view def exchange(pool: address, i: uint256, j: uint256, dx: uint256, min_dy: uint256, use_eth: bool): payable interface StablePool2Coins: def add_liquidity(amounts: uint256[2], min_mint_amount: uint256): payable def calc_token_amount(amounts: uint256[2], is_deposit: bool) -> uint256: view interface CryptoPool2Coins: def calc_token_amount(amounts: uint256[2]) -> uint256: view interface StablePool3Coins: def add_liquidity(amounts: uint256[3], min_mint_amount: uint256): payable def calc_token_amount(amounts: uint256[3], is_deposit: bool) -> uint256: view interface CryptoPool3Coins: def calc_token_amount(amounts: uint256[3]) -> uint256: view interface StablePool4Coins: def add_liquidity(amounts: uint256[4], min_mint_amount: uint256): payable def calc_token_amount(amounts: uint256[4], is_deposit: bool) -> uint256: view interface CryptoPool4Coins: def calc_token_amount(amounts: uint256[4]) -> uint256: view interface StablePool5Coins: def add_liquidity(amounts: uint256[5], min_mint_amount: uint256): payable def calc_token_amount(amounts: uint256[5], is_deposit: bool) -> uint256: view interface CryptoPool5Coins: def calc_token_amount(amounts: uint256[5]) -> uint256: view interface LendingStablePool3Coins: def add_liquidity(amounts: uint256[3], min_mint_amount: uint256, use_underlying: bool): payable def remove_liquidity_one_coin(token_amount: uint256, i: int128, min_amount: uint256, use_underlying: bool) -> uint256: nonpayable interface Llamma: def get_dx(i: uint256, j: uint256, out_amount: uint256) -> uint256: view interface WETH: def deposit(): payable def withdraw(_amount: uint256): nonpayable interface stETH: def submit(_refferer: address): payable interface frxETHMinter: def submit(): payable interface wstETH: def getWstETHByStETH(_stETHAmount: uint256) -> uint256: view def getStETHByWstETH(_wstETHAmount: uint256) -> uint256: view def wrap(_stETHAmount: uint256) -> uint256: nonpayable def unwrap(_wstETHAmount: uint256) -> uint256: nonpayable interface sfrxETH: def convertToShares(assets: uint256) -> uint256: view def convertToAssets(shares: uint256) -> uint256: view def deposit(assets: uint256, receiver: address) -> uint256: nonpayable def redeem(shares: uint256, receiver: address, owner: address) -> uint256: nonpayable interface wBETH: def deposit(referral: address): payable def exchangeRate() -> uint256: view # SNX interface SnxCoin: def currencyKey() -> bytes32: nonpayable interface Synthetix: def exchangeAtomically(sourceCurrencyKey: bytes32, sourceAmount: uint256, destinationCurrencyKey: bytes32, trackingCode: bytes32, minAmount: uint256) -> uint256: nonpayable interface SynthetixExchanger: def getAmountsForAtomicExchange(sourceAmount: uint256, sourceCurrencyKey: bytes32, destinationCurrencyKey: bytes32) -> AtomicAmountAndFee: view interface SynthetixAddressResolver: def getAddress(name: bytes32) -> address: view # Calc zaps interface StableCalc: def calc_token_amount(pool: address, token: address, amounts: uint256[10], n_coins: uint256, deposit: bool, use_underlying: bool) -> uint256: view def get_dx(pool: address, i: int128, j: int128, dy: uint256, n_coins: uint256) -> uint256: view def get_dx_underlying(pool: address, i: int128, j: int128, dy: uint256, n_coins: uint256) -> uint256: view def get_dx_meta(pool: address, i: int128, j: int128, dy: uint256, n_coins: uint256, base_pool: address) -> uint256: view def get_dx_meta_underlying(pool: address, i: int128, j: int128, dy: uint256, n_coins: uint256, base_pool: address, base_token: address) -> uint256: view interface CryptoCalc: def get_dx(pool: address, i: uint256, j: uint256, dy: uint256, n_coins: uint256) -> uint256: view def get_dx_meta_underlying(pool: address, i: uint256, j: uint256, dy: uint256, n_coins: uint256, base_pool: address, base_token: address) -> uint256: view struct AtomicAmountAndFee: amountReceived: uint256 fee: uint256 exchangeFeeRate: uint256 ``` * * * <a id="route-and-swap-parameters"></a> ## Route and Swap Parameters The two most curcial input values when using the `CurveRouter` are `_route`, which determines the route of the exchange and `_swap_params`, which includes swap parameters such as input and output token, swap type, pool type and number of coins in the pool. <a id="_route"></a> ### `_route` The route input is an array of up to 11 addresses. When calling the function, the array must always include 11 addresses. Unused spots in the array need to be filled with `ZERO_ADDRESS`. The route consists of tokens and pools or zaps. The first address is always the input token, the last one always the output token. The addresses inbetween compose the route the user wants to trade. **▶Example▼** ```py [ '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2', # initial token: wETH '0x7f86bf177dd4f3494b841a37e810a34dd56c829b', # pool1: TricryptoUSDC (USDC, wBTC, wETH) '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48', # token in between: USDC '0x4dece678ceceb27446b35c672dc7d61f30bad69e', # pool2: crvusd/USDC '0xf939e0a03fb07f59a73314e73794be0e57ac1b4e', # output token: crvUSD '0x0000000000000000000000000000000000000000', '0x0000000000000000000000000000000000000000', '0x0000000000000000000000000000000000000000', '0x0000000000000000000000000000000000000000', '0x0000000000000000000000000000000000000000', '0x0000000000000000000000000000000000000000', ] ``` *The example demonstrates a swap of [wETH](https://etherscan.io/token/0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2) for [crvUSD](https://etherscan.io/token/0xf939E0A03FB07F59A73314E73794Be0E57ac1b4E). The process involves two stages. First, wETH is swapped for [USDC](https://etherscan.io/token/0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48) using the [tricryptoUSDC](https://etherscan.io/address/0x7f86bf177dd4f3494b841a37e810a34dd56c829b) pool. Subsequently, USDC is exchanged for crvUSD using the [crvusd/USDC](https://etherscan.io/address/0x4dece678ceceb27446b35c672dc7d61f30bad69e) pool.* * * * <a id="_swap_params"></a> ### `_swap_params` Swap parameters are defined in a multidimensional array which provides essential information about which tokens to swap using a specified pool. The array structure includes the following elements: **`[i, j, swap_type, pool_type, n_coins]`**. The first array of this multidimensional setup corresponds to the initial token exchange in the swap sequence. **`i`**: Index of the input token, fetched from `pool.coins('token address')`. **`j`**: Index of the output token, fetched from `pool.coins('token address')`. | `swap_type` | Description | | --- | --- | | 1 | Standard token `exchange` | | 2 | Underlying token `exchange_underlying` | | 3 | Underlying exchange via zap for factory stable metapools and crypto-meta pools (`exchange_underlying` for stable, `exchange` in zap for crypto) | | 4 | Coin to LP token "exchange" (effectively `add_liquidity`) | | 5 | Lending pool underlying coin to LP token "exchange" (effectively `add_liquidity`) | | 6 | LP token to coin "exchange" (effectively `remove_liquidity_one_coin`) | | 7 | LP token to lending or fake pool underlying coin "exchange" (effectively `remove_liquidity_one_coin`) | | 8 | Specialized swaps like ETH <-> WETH, ETH -> stETH or frxETH, and cross-liquidity between staked tokens (e.g., stETH <-> wstETH, frxETH <-> sfrxETH) | | 9 | SNX-related swaps (e.g., sUSD, sEUR, sETH, sBTC) | | `pool_type` | Description | | --- | --- | | 1 | Stable pools using the Stableswap algorithm | | 2 | Two-coin Crypto pools using the Cryptoswap algorithm | | 3 | Tricrypto pools with three coins using the Cryptoswap algorithm | | 4 | Llamma pools, typically used in crvUSD and lending markets | **`n_coins`**: Number of coins contained within the pool. **▶Example▼** ```py [ [2, 0, 1, 3, 3], # first swap: wETH -> USDC using tricryptoUSDC pool [0, 1, 1, 1, 2], # second swap: USDC -> crvUSD using crvusd/USDC pool [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], ] ``` *Let's take a closer look at the first array, which represents the swap parameters for the first exchange (wETH -> USDC). `i = 2` because the coin index value of wETH in the tricryptoUSDC pool is 2. This can be obtained by calling `tricryptoUSDC.coins(n)`. Similarly, `j = 0` because USDC has the coin index value of 0. The swap type is a regular exchange, represented by `1`. The `pool_type` is 3, as it is a tricrypto pool (a cryptoswap algorithm consisting of three coins: USDC, wBTC, and wETH). The last value in the array represents the number of coins in the pool, which is 3.* *The values of the second array should be set according to the crvusd/USDC pool.* * * * <a id="exchanging-tokens"></a> ## Exchanging Tokens The router has a single `exchange` function, which allows up to 5 swaps in a single transaction. Routing and swap parameters need to be determined off-chain. The exchange functionality of the router is designed for gas efficiency over ease-of-use. An accompanying JavaScript library can be found on [GitHub](https://github.com/curvefi/curve-router-js), which is used in the Curve UI to determine route and swap parameters. <a id="exchange"></a> ### `exchange` `Router.exchange(_route: address[11], _swap_params: uint256[5][5], _amount: uint256, _expected: uint256, _pools: address[5] = empty(address[5]), _receiver: address = msg.sender) -> uint256:` Function to perform a token exchange with up to 5 swaps in a single transaction. | Input | Type | Description | | --- | --- | --- | | `_route` | `address[11]` | Route data: [see here](https://docs.curve.finance/developer/amm/router/curve-router-ng.md#_route) | | `_swap_params` | `uint256[5][5]` | Swap parameters: [see here](https://docs.curve.finance/developer/amm/router/curve-router-ng.md#_swap_params) | | `_amount` | `uint256` | The amount of the input token (`_route[0]`) to be sent. | | `_expected` | `uint256` | The minimum amount received after the final swap. | | `_pools` | `address[5]` | Array of pools for swaps via zap contracts. This parameter is only needed for `swap_type = 3`. | | `receiver` | `address` | Address to transfer the final output token to. Defaults to `msg.sender`. | Returns: received amount of the final output token (`uint256`). **<>Source code▼** ```vyper event Exchange: sender: indexed(address) receiver: indexed(address) route: address[11] swap_params: uint256[5][5] pools: address[5] in_amount: uint256 out_amount: uint256 ETH_ADDRESS: constant(address) = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE STETH_ADDRESS: constant(address) = 0xae7ab96520DE3A18E5e111B5EaAb095312D7fE84 WSTETH_ADDRESS: constant(address) = 0x7f39C581F595B53c5cb19bD0b3f8dA6c935E2Ca0 FRXETH_ADDRESS: constant(address) = 0x5E8422345238F34275888049021821E8E08CAa1f SFRXETH_ADDRESS: constant(address) = 0xac3E018457B222d93114458476f3E3416Abbe38F WBETH_ADDRESS: constant(address) = 0xa2E3356610840701BDf5611a53974510Ae27E2e1 WETH_ADDRESS: immutable(address) # SNX # https://docs.synthetix.io/ SNX_ADDRESS_RESOLVER: constant(address) = 0x823bE81bbF96BEc0e25CA13170F5AaCb5B79ba83 SNX_TRACKING_CODE: constant(bytes32) = 0x4355525645000000000000000000000000000000000000000000000000000000 # CURVE SNX_EXCHANGER_NAME: constant(bytes32) = 0x45786368616E6765720000000000000000000000000000000000000000000000 # Exchanger snx_currency_keys: HashMap[address, bytes32] @external @payable @nonreentrant('lock') def exchange( _route: address[11], _swap_params: uint256[5][5], _amount: uint256, _expected: uint256, _pools: address[5]=empty(address[5]), _receiver: address=msg.sender ) -> uint256: """ @notice Performs up to 5 swaps in a single transaction. @dev Routing and swap params must be determined off-chain. This functionality is designed for gas efficiency over ease-of-use. @param _route Array of [initial token, pool or zap, token, pool or zap, token, ...] The array is iterated until a pool address of 0x00, then the last given token is transferred to `_receiver` @param _swap_params Multidimensional array of [i, j, swap type, pool_type, n_coins] where i is the index of input token j is the index of output token The swap_type should be: 1. for `exchange`, 2. for `exchange_underlying`, 3. for underlying exchange via zap: factory stable metapools with lending base pool `exchange_underlying` and factory crypto-meta pools underlying exchange (`exchange` method in zap) 4. for coin -> LP token "exchange" (actually `add_liquidity`), 5. for lending pool underlying coin -> LP token "exchange" (actually `add_liquidity`), 6. for LP token -> coin "exchange" (actually `remove_liquidity_one_coin`) 7. for LP token -> lending or fake pool underlying coin "exchange" (actually `remove_liquidity_one_coin`) 8. for ETH <-> WETH, ETH -> stETH or ETH -> frxETH, stETH <-> wstETH, frxETH <-> sfrxETH, ETH -> wBETH 9. for SNX swaps (sUSD, sEUR, sETH, sBTC) pool_type: 1 - stable, 2 - crypto, 3 - tricrypto, 4 - llamma n_coins is the number of coins in pool @param _amount The amount of input token (`_route[0]`) to be sent. @param _expected The minimum amount received after the final swap. @param _pools Array of pools for swaps via zap contracts. This parameter is only needed for swap_type = 3. @param _receiver Address to transfer the final output token to. @return Received amount of the final output token. """ input_token: address = _route[0] output_token: address = empty(address) amount: uint256 = _amount # validate / transfer initial token if input_token == ETH_ADDRESS: assert msg.value == amount else: assert msg.value == 0 assert ERC20(input_token).transferFrom(msg.sender, self, amount, default_return_value=True) for i in range(1, 6): # 5 rounds of iteration to perform up to 5 swaps swap: address = _route[i*2-1] pool: address = _pools[i-1] # Only for Polygon meta-factories underlying swap (swap_type == 6) output_token = _route[i*2] params: uint256[5] = _swap_params[i-1] # i, j, swap_type, pool_type, n_coins if not self.is_approved[input_token][swap]: assert ERC20(input_token).approve(swap, max_value(uint256), default_return_value=True, skip_contract_check=True) self.is_approved[input_token][swap] = True eth_amount: uint256 = 0 if input_token == ETH_ADDRESS: eth_amount = amount # perform the swap according to the swap type if params[2] == 1: if params[3] == 1: # stable StablePool(swap).exchange(convert(params[0], int128), convert(params[1], int128), amount, 0, value=eth_amount) else: # crypto, tricrypto or llamma if input_token == ETH_ADDRESS or output_token == ETH_ADDRESS: CryptoPoolETH(swap).exchange(params[0], params[1], amount, 0, True, value=eth_amount) else: CryptoPool(swap).exchange(params[0], params[1], amount, 0) elif params[2] == 2: if params[3] == 1: # stable StablePool(swap).exchange_underlying(convert(params[0], int128), convert(params[1], int128), amount, 0, value=eth_amount) else: # crypto or tricrypto CryptoPool(swap).exchange_underlying(params[0], params[1], amount, 0, value=eth_amount) elif params[2] == 3: # SWAP IS ZAP HERE !!! if params[3] == 1: # stable LendingBasePoolMetaZap(swap).exchange_underlying(pool, convert(params[0], int128), convert(params[1], int128), amount, 0) else: # crypto or tricrypto use_eth: bool = input_token == ETH_ADDRESS or output_token == ETH_ADDRESS CryptoMetaZap(swap).exchange(pool, params[0], params[1], amount, 0, use_eth, value=eth_amount) elif params[2] == 4: if params[4] == 2: amounts: uint256[2] = [0, 0] amounts[params[0]] = amount StablePool2Coins(swap).add_liquidity(amounts, 0, value=eth_amount) elif params[4] == 3: amounts: uint256[3] = [0, 0, 0] amounts[params[0]] = amount StablePool3Coins(swap).add_liquidity(amounts, 0, value=eth_amount) elif params[4] == 4: amounts: uint256[4] = [0, 0, 0, 0] amounts[params[0]] = amount StablePool4Coins(swap).add_liquidity(amounts, 0, value=eth_amount) elif params[4] == 5: amounts: uint256[5] = [0, 0, 0, 0, 0] amounts[params[0]] = amount StablePool5Coins(swap).add_liquidity(amounts, 0, value=eth_amount) elif params[2] == 5: amounts: uint256[3] = [0, 0, 0] amounts[params[0]] = amount LendingStablePool3Coins(swap).add_liquidity(amounts, 0, True, value=eth_amount) # example: aave on Polygon elif params[2] == 6: if params[3] == 1: # stable StablePool(swap).remove_liquidity_one_coin(amount, convert(params[1], int128), 0) else: # crypto or tricrypto CryptoPool(swap).remove_liquidity_one_coin(amount, params[1], 0) # example: atricrypto3 on Polygon elif params[2] == 7: LendingStablePool3Coins(swap).remove_liquidity_one_coin(amount, convert(params[1], int128), 0, True) # example: aave on Polygon elif params[2] == 8: if input_token == ETH_ADDRESS and output_token == WETH_ADDRESS: WETH(swap).deposit(value=amount) elif input_token == WETH_ADDRESS and output_token == ETH_ADDRESS: WETH(swap).withdraw(amount) elif input_token == ETH_ADDRESS and output_token == STETH_ADDRESS: stETH(swap).submit(0x0000000000000000000000000000000000000000, value=amount) elif input_token == ETH_ADDRESS and output_token == FRXETH_ADDRESS: frxETHMinter(swap).submit(value=amount) elif input_token == STETH_ADDRESS and output_token == WSTETH_ADDRESS: wstETH(swap).wrap(amount) elif input_token == WSTETH_ADDRESS and output_token == STETH_ADDRESS: wstETH(swap).unwrap(amount) elif input_token == FRXETH_ADDRESS and output_token == SFRXETH_ADDRESS: sfrxETH(swap).deposit(amount, self) elif input_token == SFRXETH_ADDRESS and output_token == FRXETH_ADDRESS: sfrxETH(swap).redeem(amount, self, self) elif input_token == ETH_ADDRESS and output_token == WBETH_ADDRESS: wBETH(swap).deposit(0xeCb456EA5365865EbAb8a2661B0c503410e9B347, value=amount) else: raise "Swap type 8 is only for ETH <-> WETH, ETH -> stETH or ETH -> frxETH, stETH <-> wstETH, frxETH <-> sfrxETH, ETH -> wBETH" elif params[2] == 9: Synthetix(swap).exchangeAtomically(self.snx_currency_keys[input_token], amount, self.snx_currency_keys[output_token], SNX_TRACKING_CODE, 0) else: raise "Bad swap type" # update the amount received if outpu
프로젝트로 돌아가기