Curve DAO 백서

Curve DAO

원문

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

e(MAX_COINS): swappable_coin: address = self.base_pool_data[_base_pool].coins[i] if swappable_coin == ZERO_ADDRESS: is_finished = True swappable_coin = base_lp_token key: uint256 = bitwise_xor(convert(_coin, uint256), convert(swappable_coin, uint256)) length = self.market_counts[key] self.markets[key][length] = pool self.market_counts[key] = length + 1 if is_finished: break log MetaPoolDeployed(_coin, _base_pool, _A, _fee, msg.sender) return pool ``` **▶Example▼** ```shell >>> Factory.deploy_metapool( _base_pool: '0xbEbc44782C7dB0a1A60Cb6fe97d0b483032FF1C7', _name: "crvusd/3CRV", _symbol: "crvUSD3CRV", _coin: '0xf939E0A03FB07F59A73314E73794Be0E57ac1b4E', _A: 200, _fee: 4000000, _implementation_idx: uint256 = 0, ) 'returns address of the deployed pool' ``` <a id="liquidity-gaugeinfo"></a> ## Liquidity Gauge:::info Liquidity gauges can only be successfully deployed from the same contract from which the pool was deployed! ::: <a id="deploy_gauge"></a> ### `deploy_gauge` `Factory.deploy_gauge(_pool: address) -> address:` Function to deploy a liquidity gauge for a factory pool. Returns: Deployed gauge (`address`). Emits: `LiquidityGaugeDeployed` | Input | Type | Description | | --- | --- | --- | | `_pool` | `address` | Factory pool address to deploy a gauge for | **<>Source code▼** ```vyper event LiquidityGaugeDeployed: pool: address gauge: address @external def deploy_gauge(_pool: address) -> address: """ @notice Deploy a liquidity gauge for a factory pool @param _pool Factory pool address to deploy a gauge for @return Address of the deployed gauge """ assert self.pool_data[_pool].coins[0] != ZERO_ADDRESS, "Unknown pool" assert self.pool_data[_pool].liquidity_gauge == ZERO_ADDRESS, "Gauge already deployed" implementation: address = self.gauge_implementation assert implementation != ZERO_ADDRESS, "Gauge implementation not set" gauge: address = create_forwarder_to(implementation) LiquidityGauge(gauge).initialize(_pool) self.pool_data[_pool].liquidity_gauge = gauge log LiquidityGaugeDeployed(_pool, gauge) return gauge ``` **▶Example▼** ```shell >>> Factory.deploy_gauge("pool address") 'deployed gauge address' ``` --- # implementations Source: [https://docs.curve.finance/developer/amm/legacy/factory/stableswap/implementations](https://docs.curve.finance/developer/amm/legacy/factory/stableswap/implementations) **The Stableswap Factory utilizes the `create_forwarder_to` function to deploy its contracts from the implementations.**:::warning \*\*Implementation contracts are upgradable.\*\*They can either be replaced, or additional implementation contracts can be added. Therefore, please always make sure to check the most recent ones. ::: It utilizes three different implementations: - **`metapool_implementations`**, containing multiple contracts that are used to deploy metapools. - **`plain_implementations`**, containing multiple contracts that are used to deploy plain pools. - **`gauge_implementation`**, containing a contract which is used when deploying liquidity gauges for pools. <a id="query-implementations"></a> ## Query Implementations <a id="metapool_implementations"></a> ### `metapool_implementations` `Factory.metapool_implementations(_base_pool: address) -> address[10]:` Getter for a list of implementation contracts for metapools targetting `_base_pool`. Returns: metapool implementation contracts (`address[10]`). | Input | Type | Description | | --- | --- | --- | | `_base_pool` | `address` | Base pool | **<>Source code▼** ```vyper base_pool_data: HashMap[address, BasePoolArray] @view @external def metapool_implementations(_base_pool: address) -> address[10]: """ @notice Get a list of implementation contracts for metapools targetting the given base pool @dev A base pool is the pool for the LP token contained within the metapool @param _base_pool Address of the base pool @return List of implementation contract addresses """ return self.base_pool_data[_base_pool].implementations ``` **▶Example▼** ```shell >>> Factory.metapool_implementation('0xbebc44782c7db0a1a60cb6fe97d0b483032ff1c7') '[[0x213be373FDff327658139C7df330817DAD2d5bBE] [0x55Aa9BF126bCABF0bDC17Fa9E39Ec9239e1ce7A9] [0x0000000000000000000000000000000000000000] [0x0000000000000000000000000000000000000000] [0x0000000000000000000000000000000000000000] [0x0000000000000000000000000000000000000000] [0x0000000000000000000000000000000000000000] [0x0000000000000000000000000000000000000000] [0x0000000000000000000000000000000000000000] [0x0000000000000000000000000000000000000000]]' ``` <a id="plain_implementations"></a> ### `plain_implementations` `Factory.plain_implementations(arg0: uint256, arg1: uint256) -> address: view` Getter for the plain implementations index `arg1` for a plain pool with a number of `arg0` coins. Returns: Plain implementation (`address`). | Input | Type | Description | | --- | --- | --- | | `arg0` | `uint256` | Number of coins in pool | | `arg1` | `uint256` | Index of implementation | **<>Source code▼** ```vyper # number of coins -> implementation addresses # for "plain pools" (as opposed to metapools), implementation contracts # are organized according to the number of coins in the pool plain_implementations: public(HashMap[uint256, address[10]]) ``` **▶Example▼** ```shell >>> Factory.plain_implementations(2, 0) '0x6523Ac15EC152Cb70a334230F6c5d62C5Bd963f1' ``` <a id="gauge_implementation"></a> ### `gauge_implementation` `Factory.gauge_implementations() -> address: view` Getter for the gauge implementation of the Factory. Returns: gauge implementation (`address`). **<>Source code▼** ```vyper gauge_implementation: public(address) ``` **▶Example▼** ```shell >>> Factory.gauge_implementation() '0x5aE854b098727a9f1603A1E21c50D52DC834D846' ``` <a id="get_implementation_address"></a> ### `get_implementation_address` `Factory.get_implementation_address(_pool: address) -> address:` Getter for the address of the implementation contract used for a factory pool. Returns: Implementation (`address`). | Input | Type | Description | | --- | --- | --- | | `_pool` | `address` | Factory pool address | **<>Source code▼** ```vyper @view @external def get_implementation_address(_pool: address) -> address: """ @notice Get the address of the implementation contract used for a factory pool @param _pool Pool address @return Implementation contract address """ return self.pool_data[_pool].implementation ``` **▶Example▼** ```shell >>> Factory.gauge_implementation() '0x5aE854b098727a9f1603A1E21c50D52DC834D846' ``` <a id="set-new-implementation"></a> ## Set New Implementation *New implementations can be set via these admin-only functions:* <a id="set_metapool_implementation"></a> ### `set_metapool_implementation` `Factory.set_metapool_implementations(_base_pool: address, _implementations: address[10]):` Guarded Method This function is only callable by the `admin` of the contract. Function to set new metapool implementations. | Input | Type | Description | | --- | --- | --- | | `_base_pool` | `address` | Base pool to add implementations for | | `_implementations` | `address[10]` | New metapool implementations | **<>Source code▼** ```vyper base_pool_data: HashMap[address, BasePoolArray] @external def set_metapool_implementations( _base_pool: address, _implementations: address[10], ): """ @notice Set implementation contracts for a metapool @dev Only callable by admin @param _base_pool Pool address to add @param _implementations Implementation address to use when deploying metapools """ assert msg.sender == self.admin # dev: admin-only function assert self.base_pool_data[_base_pool].coins[0] != ZERO_ADDRESS # dev: base pool does not exist for i in range(10): new_imp: address = _implementations[i] current_imp: address = self.base_pool_data[_base_pool].implementations[i] if new_imp == current_imp: if new_imp == ZERO_ADDRESS: break else: self.base_pool_data[_base_pool].implementations[i] = new_imp ``` **▶Example▼** ```shell >>> Factory.set_metapool_implementation("todo") 'todo' ``` <a id="set_plain_implementation"></a> ### `set_plain_implementation` `Factory.set_plain_implementations(_n_coins: uint256, _implementations: address[10]):` Guarded Method This function is only callable by the `admin` of the contract. Function to set new plain pool implementations. | Input | Type | Description | | --- | --- | --- | | `_n_coins` | `uint256` | Number of coins in pool to set implementations for | | `_pool_implementation` | `address` | New plain pool implementations | **<>Source code▼** ```vyper # number of coins -> implementation addresses # for "plain pools" (as opposed to metapools), implementation contracts # are organized according to the number of coins in the pool plain_implementations: public(HashMap[uint256, address[10]]) @external def set_plain_implementations( _n_coins: uint256, _implementations: address[10], ): assert msg.sender == self.admin # dev: admin-only function for i in range(10): new_imp: address = _implementations[i] current_imp: address = self.plain_implementations[_n_coins][i] if new_imp == current_imp: if new_imp == ZERO_ADDRESS: break else: self.plain_implementations[_n_coins][i] = new_imp ``` **▶Example▼** ```shell >>> Factory.set_plain_implementation("todo") 'todo' ``` <a id="set_gauge_implementation"></a> ### `set_gauge_implementation` `Factory.set_gauge_implementation(_gauge_implementation: address):` Guarded Method This function is only callable by the `admin` of the contract. Function to set a new gauge implementation contract. | Input | Type | Description | | --- | --- | --- | | `_gauge_implementation` | `address` | New gauge implementation | **<>Source code▼** ```vyper gauge_implementation: public(address) @external def set_gauge_implementation(_gauge_implementation: address): assert msg.sender == self.admin # dev: admin-only function self.gauge_implementation = _gauge_implementation ``` **▶Example▼** ```shell >>> Factory.set_gauge_implementation("todo") 'todo' ``` --- # overview Source: [https://docs.curve.finance/developer/amm/legacy/factory/stableswap/overview](https://docs.curve.finance/developer/amm/legacy/factory/stableswap/overview) The Metapool Factory, which was the first Curve Factory, allows the permissionless deployment of regular [stableswap](https://docs.curve.finance/developer/amm/legacy/stableswap/overview.md) plain and metapools, LP tokens, and liquidity gauges. **Liquidity pool and LP token DO NOT share the same contract.**:::deploy\[Contract Source & Deployment\] The **Metapool Factory**contract is deployed to the Ethereum mainnet at: [0xb9fc157394af804a3578134a6585c0dc9cc990d4](https://etherscan.io/address/0xb9fc157394af804a3578134a6585c0dc9cc990d4#code). A list of all deployed contracts can be found [here](https://docs.curve.finance/developer/deployments.md). ::: <a id="implementations"></a> ## Implementations More on [implementations](https://docs.curve.finance/developer/amm/legacy/factory/stableswap/implementations.md). --- # Curve StableSwap Exchange: Overview Source: [https://docs.curve.finance/developer/amm/legacy/stableswap-overview](https://docs.curve.finance/developer/amm/legacy/stableswap-overview) The StableSwap algorithm integrates features of both the constant sum and constant product formulas, adjusting between these models based on the balance of assets in the pool. For a detailed overview of the StableSwap invariant design, please read the official [whitepaper](https://docs.curve.finance/assets/files/whitepaper_stableswap-fc0bb370db9fe3a91afdde8662e78206.pdf). The Curve StableSwap exchange utilizes a specific algorithm known as the **StableSwap invariant to facilitate the trading of stablecoins and other stable assets**. This method is designed to offer lower slippage in stablecoin transactions compared to other common algorithms like the constant-product invariant. In this context, "stablecoins" refer to digital assets that aim to maintain a stable value relative to another asset, including fiat-pegged stablecoins (e.g., DAI, USDC), synthetic versions of cryptocurrencies (e.g., synthetic ETH), or various forms of wrapped BTC. A Curve pool, at its core, is a smart contract that implements the StableSwap invariant, enabling the exchange of tokens. While all Curve pools are based on this invariant, they can differ in structure. The simplest form of a Curve pool involves two or more tokens, known as a **"plain pool"**. Additionally, Curve offers **"metapools"**, which are designed to \*\*facilitate exchanges between a token and the underlying pools of the LP token it is paired against.\*\*For technical implementations, the **StableSwap algorithm combines a constant sum invariant with a constant product invariant**, represented mathematically in the form of equations that account for the total amount of tokens in a pool at equilibrium. This model adjusts as the pool's balance shifts, moving towards a constant product model when significant imbalances occur. * * * <a id="implementationsgithubgithub"></a> ## Implementations:::github\[GitHub\] The source code for the Curve Finance StableSwap contracts is openly accessible on GitHub: - Genesis Contracts: The initial set of smart contracts for Curve pools can be found at [Curve's contracts repository](https://github.com/curvefi/curve-contract/tree/master/contracts/pools). - StableSwap-ng: An updated version of the StableSwap algorithm, known as StableSwap-ng (next generation), is available at [Curve's StableSwap-ng repository](https://github.com/curvefi/stableswap-ng). ::: *There have been two major on-chain implementations of the stableswap invariant across various chains:* <a id="original-stableswap"></a> ### Original StableSwap The original stableswap was the first on-chain implementation of the stableswap invariant. <a id="stableswap-ng"></a> ### StableSwap NG The Stableswap-NG AMM infrastructure marks a sophisticated evolution from its original implementation, delivering enhanced technical capabilities. This upgraded framework allows for the inclusion of up to eight tokens in standard pools and two in metapools. It extends support to a variety of token types, including rate-oracle tokens like wstETH, ERC4626 tokens such as sDAI, and rebasing tokens like stETH. However, native tokens like ETH are not directly supported within this implementation. For transactions involving ETH, its wrapped version, wETH, must be utilized. This decision is rooted in ensuring higher security standards. Additionally, pools now have built-in [moving-average oracles](https://docs.curve.finance/developer/amm/stableswap-ng/pools/oracles.md). For an in-depth exploration of the new features introduced by Stableswap-NG, please refer to the [StableSwap-NG Overview](https://docs.curve.finance/developer/amm/stableswap-ng/overview.md). --- # overview Source: [https://docs.curve.finance/developer/amm/legacy/stableswap/cross-asset-swaps/overview](https://docs.curve.finance/developer/amm/legacy/stableswap/cross-asset-swaps/overview) Curve integrates with Synthetix to allow large scale swaps between different asset classes with minimal slippage. Utilizing Synthetix’ zero-slippage synth conversions and Curve’s deep liquidity and low fees, we can perform fully on-chain cross asset swaps at scale with a 0.38% fee and minimal slippage. Cross asset swaps are performed using the `SynthSwap` contract, deployed to the mainnet at [0x58A3c68e2D3aAf316239c003779F71aCb870Ee47](https://etherscan.io/address/0x58A3c68e2D3aAf316239c003779F71aCb870Ee47). Source code and information on the technical implementation are available on [Github](https://github.com/curvefi/curve-cross-asset-swaps). <a id="how-synthswap-works"></a> ## How `SynthSwap` works As an example, suppose we have asset `A` and wish to exchange it for asset `D`. For this swap to be possible, `A` and `D` must meet the following requirements: - must be of different asset classes (e.g. USD, EUR, BTC, ETH), - must be exchangeable for a Synthetic asset within one of Curve’s pools (e.g. sUSD, sBTC). The swap can be visualized as `A -> B -> C | C -> D`: 1. The initial asset `A` is exchanged on Curve for `B`, a synth of the same asset class. 2. `B` is converted to `C`, a synth of the same asset class as `D`. 3. A [settlement period](https://docs.synthetix.io/integrations/settlement/) passes to account for sudden price movements between `B` and `C`. 4. Once the [settlement period](https://docs.synthetix.io/integrations/settlement/) has passed, `C` is exchanged on Curve for the desired asset `D`. For a more detailed reasoning behind the settlement period logic, refer to [Synthetix SIP-37](https://sips.synthetix.io/sips/sip-37/). <a id="settler-nft"></a> ## Settler NFT Swaps cannot occur atomically due to the Synthetix settlement period. Each unsettled swap is represented by an ERC721 non-fungible token. Each NFT has a unique token ID. Token IDs are never re-used. The NFT is minted upon initiating the swap and burned when the swap is completed. The NFT, and associated right to claim, is fully transferable. It is not possible to transfer the rights to a partial claim. The approved operator for an NFT also has the right to complete the swap with the underlying asset. Token IDs are not sequential. This contract does not support the enumerable ERC721 extension. This decision is based on gas efficiency. <a id="front-running-considerations"></a> ## Front-running Considerations The benefits from these swaps are most apparent when the exchange amount is greater than $1m USD equivalent. As such, the initiation of a swap gives a strong indicator other market participants that a 2nd post-settlement swap will be coming. We attempt to minimize the risks from this in several ways: `C -> D` is not declared on-chain when performing the swap from `A -> C`. It is possible to perform a partial swap from `C -> D`, and to swap into multiple final assets. The NFT persists until it has no remaining underlying balance of `C`. There is no fixed time frame for the second swap. A user can perform it immediately or wait until market conditions are more favorable. It is possible to withdraw `C` without performing a second swap. It is possible to perform additional `A -> B -> C` swaps to increase the balance of an already existing NFT. The range of available actions and time frames make it significantly more difficult to predict the outcome of a swap and trade against it. --- # synthswap-exchange Source: [https://docs.curve.finance/developer/amm/legacy/stableswap/cross-asset-swaps/synthswap-exchange](https://docs.curve.finance/developer/amm/legacy/stableswap/cross-asset-swaps/synthswap-exchange) This section discusses the different methods in the Curve [SynthSwap](https://etherscan.io/address/0x58A3c68e2D3aAf316239c003779F71aCb870Ee47) contract. <a id="adding-and-finding-swappable-assets"></a> ## Adding and Finding Swappable Assets In general, any asset that is within a Curve pool also containing a Synth may be used in a cross asset swap. <a id="synthswapadd_synth"></a> ### `SynthSwap.add_synth` `def add_synth(_synth: address, _pool: address)` Add a new swappable synth. This method is callable by anyone, however `_pool` must exist within the Curve pool registry and `_synth` must be a valid synth that is swappable within the pool. | Input | Type | Description | | --- | --- | --- | | `_synth` | `address` | Address of the synth | | `_pool` | `address` | Address of Curve pool containing the synth | Emits: NewSynth **<>Source code▼** ```vyper @external def add_synth(_synth: address, _pool: address): """ @notice Add a new swappable synth @dev Callable by anyone, however `_pool` must exist within the Curve pool registry and `_synth` must be a valid synth that is swappable within the pool @param _synth Address of the synth to add @param _pool
프로젝트로 돌아가기