bitpro가 원문에서 글자만 모은 내용이에요. 표·그림·수식은 빠지거나 깨질 수 있어요. 전체 75쪽 중 67쪽
```
<a id="get_virtual_price"></a>
### `get_virtual_price`
`TwoCrypto.virtual_price -> uint256: view`
`get_virtual_price` ≠ `virtual_price`
`get_virtual_price` should not be confused with `virtual_price`, which is a cached virtual price.
Function to calculate the current virtual price of the pool's LP token.
Returns: virtual price (`uint256`).
**<>Source code▼**
```vyper
@external
@view
@nonreentrant("lock")
def get_virtual_price() -> uint256:
"""
@notice Calculates the current virtual price of the pool LP token.
@dev Not to be confused with `self.virtual_price` which is a cached
virtual price.
@return uint256 Virtual Price.
"""
return 10**18 * self.get_xcp(self.D, self.cached_price_scale) / self.totalSupply
```
**▶Example▼**
```shell
In [1]: Pool.get_virtual_price()
Out [1]: 1000270251060292804
```
<a id="last_prices"></a>
### `last_prices`
`TwoCrypto.last_prices -> uint256: view`
Getter for the last price. This variable is used to calculate the moving average price oracle.
Returns: last price (`uint256`).
**<>Source code▼**
```vyper
last_prices: public(uint256)
@internal
def tweak_price(
A_gamma: uint256[2],
_xp: uint256[N_COINS],
new_D: uint256,
K0_prev: uint256 = 0,
) -> uint256:
"""
@notice Updates price_oracle, last_price and conditionally adjusts
price_scale. This is called whenever there is an unbalanced
liquidity operation: _exchange, add_liquidity, or
remove_liquidity_one_coin.
@dev Contains main liquidity rebalancing logic, by tweaking `price_scale`.
@param A_gamma Array of A and gamma parameters.
@param _xp Array of current balances.
@param new_D New D value.
@param K0_prev Initial guess for `newton_D`.
"""
...
# ----------------------- Calculate last_prices --------------------------
self.last_prices = unsafe_div(
MATH.get_p(_xp, D_unadjusted, A_gamma) * price_scale,
10**18
)
...
```
**▶Example▼**
```shell
In [1]: Pool.last_prices()
Out [1]: 176068709329068
```
<a id="xcp_oracle"></a>
### `xcp_oracle`
`TwoCrypto.xcp_oracle() -> uint256`
Getter for the oracle value for xcp. The oracle is an exponential moving average, with a periodicity determined by `xcp_ma_time`.
Returns: xcp oracle value (`uint256`).
**<>Source code▼**
```vyper
cached_xcp_oracle: uint256 # <----------- EMA of totalSupply * virtual_price.
@external
@view
@nonreentrant("lock")
def xcp_oracle() -> uint256:
"""
@notice Returns the oracle value for xcp.
@dev The oracle is an exponential moving average, with a periodicity
determined by `self.xcp_ma_time`.
`TVL` is xcp, calculated as either:
1. virtual_price * total_supply, OR
2. self.get_xcp(...), OR
3. MATH.geometric_mean(xp)
@return uint256 Oracle value of xcp.
"""
last_prices_timestamp: uint256 = self._unpack_2(self.last_timestamp)[1]
cached_xcp_oracle: uint256 = self.cached_xcp_oracle
if last_prices_timestamp < block.timestamp:
alpha: uint256 = MATH.wad_exp(
-convert(
unsafe_div(
unsafe_sub(block.timestamp, last_prices_timestamp) * 10**18,
self.xcp_ma_time
),
int256,
)
)
return (self.last_xcp * (10**18 - alpha) + cached_xcp_oracle * alpha) / 10**18
return cached_xcp_oracle
```
**▶Example▼**
```shell
In [1]: Pool.xcp_oracle()
Out [1]: 3501656271269889041418
```
<a id="xcp_ma_time"></a>
### `xcp_ma_time`
`TwoCrypto.xcp_ma_time() -> uint256: view`
Getter for the moving average time window for `xcp_oracle`.
Returns: ma time (`uint256`).
**<>Source code▼**
```vyper
xcp_ma_time: public(uint256)
@external
def __init__(
_name: String[64],
_symbol: String[32],
_coins: address[N_COINS],
_math: address,
_salt: bytes32,
packed_precisions: uint256,
packed_gamma_A: uint256,
packed_fee_params: uint256,
packed_rebalancing_params: uint256,
initial_price: uint256,
):
...
self.xcp_ma_time = 62324 # <--------- 12 hours default on contract start.
...
```
**▶Example▼**
```shell
In [1]: Pool.xcp_ma_time()
Out [1]: 62324
```
<a id="d"></a>
### `D`
`TwoCrypto.D() -> uint256: view`
Getter for the D invariant.
Returns: D (`uint256`).
**<>Source code▼**
```vyper
D: public(uint256)
```
**▶Example▼**
```shell
In [1]: Pool.D()
Out [1]: 110997117004824612212
```
<a id="last_timestamp"></a>
### `last_timestamp`
`TwoCrypto.last_timestamp() -> uint256: view`
Getter for the last timestamp of prices and xcp. The two values are packed into a `uint256`. Need to unpack them: Index 0 is for prices, index 1 is for xcp.
Returns: last timestamp (`uint256`).
**<>Source code▼**
```vyper
last_timestamp: public(uint256) # idx 0 is for prices, idx 1 is for xcp.
```
**▶Example▼**
```shell
In [1]: Pool.last_timestamp()
Out [1]: 581843605731969082977825518885220002649556567303
```
* * *
<a id="contract-info-methods"></a>
## Contract Info Methods
<a id="admin"></a>
### `admin`
`TwoCrypto.admin() -> address:`
Getter for the admin of the pool. All deployed pools share the same admin, which is specified within the Factory contract.
Returns: admin (`address`).
**<>Source code▼**
```vyper
interface Factory:
def admin() -> address: view
@external
@view
def admin() -> address:
"""
@notice Returns the address of the pool's admin.
@return address Admin.
"""
return factory.admin()
```
**▶Example▼**
```shell
In [1]: Pool.admin()
Out [1]: todo
```
<a id="precisions"></a>
### `precisions`
`TwoCrypto.precisions() -> uint256[N_COINS]:`
Getter for the precisions of each coin in the pool. Precisions are used to make sure the pool is compatible with any coins with decimals up to 18.
Returns: precision of coins (`uint256[N_COINS]`).
**<>Source code▼**
```vyper
PRECISION: constant(uint256) = 10**18 # <------- The precision to convert to.
PRECISIONS: immutable(uint256[N_COINS])
@view
@external
def precisions() -> uint256[N_COINS]: # <-------------- For by view contract.
"""
@notice Returns the precisions of each coin in the pool.
@return uint256[3] precisions of coins.
"""
return PRECISIONS
```
**▶Example▼**
```shell
In [1]: Pool.precisions()
Out [1]: [1, 1]
```
<a id="math"></a>
### `MATH`
`TwoCrypto.MATH() -> address: view`
Getter for the [math utility contract](https://docs.curve.finance/developer/amm/twocrypto-ng/utility-contracts/math.md).
Returns: math contract (`address`).
**<>Source code▼**
```vyper
MATH: public(immutable(Math))
@external
def __init__(
_name: String[64],
_symbol: String[32],
_coins: address[N_COINS],
_math: address,
_salt: bytes32,
packed_precisions: uint256,
packed_gamma_A: uint256,
packed_fee_params: uint256,
packed_rebalancing_params: uint256,
initial_price: uint256,
):
MATH = Math(_math)
...
```
**▶Example▼**
```shell
In [1]: Pool.MATH()
Out [1]: '0x2005995a71243be9FB995DaB4742327dc76564Df'
```
<a id="coins"></a>
### `coins`
`TwoCrypto.coins(arg0: uint256) -> address: view`
Getter for the coin at index `arg0` in the pool.
| Input | Type | Description |
| --- | --- | --- |
| `arg0` | `uint256` | Coin index. |
Returns: precision of coins (`uint256[N_COINS]`).
**<>Source code▼**
```vyper
coins: public(immutable(address[N_COINS]))
@external
def __init__(
_name: String[64],
_symbol: String[32],
_coins: address[N_COINS],
_math: address,
_salt: bytes32,
packed_precisions: uint256,
packed_gamma_A: uint256,
packed_fee_params: uint256,
packed_rebalancing_params: uint256,
initial_price: uint256,
):
MATH = Math(_math)
factory = Factory(msg.sender)
name = _name
symbol = _symbol
coins = _coins
...
```
**▶Example▼**
```shell
In [1]: Pool.coins(0)
Out [1]: '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2'
In [2]: Pool.coins(1)
Out [2]: '0x97efFB790f2fbB701D88f89DB4521348A2B77be8'
```
<a id="factory"></a>
### `factory`
`TwoCrypto.factory() -> address: view`
Getter for the [Factory contract](https://docs.curve.finance/developer/amm/factory/twocrypto-ng/overview.md), which allows the permissionless deployment of liquidity pools and gauges.
Returns: factory (`address`).
**<>Source code▼**
```vyper
factory: public(immutable(Factory))
@external
def __init__(
_name: String[64],
_symbol: String[32],
_coins: address[N_COINS],
_math: address,
_salt: bytes32,
packed_precisions: uint256,
packed_gamma_A: uint256,
packed_fee_params: uint256,
packed_rebalancing_params: uint256,
initial_price: uint256,
):
...
factory = Factory(msg.sender)
...
```
**▶Example▼**
```shell
In [1]: Pool.factory()
Out [1]: '0x98EE851a00abeE0d95D08cF4CA2BdCE32aeaAF7F'
```
<a id="balances"></a>
### `balances`
`TwoCrypto.balances(arg0: uint256) -> uint256: view`
Getter for the current coin balances in the pool.
| Input | Type | Description |
| --- | --- | --- |
| `arg0` | `uint256` | Coin index. |
Returns: coin balances (`uint256`).
**<>Source code▼**
```vyper
balances: public(uint256[N_COINS])
```
**▶Example▼**
```shell
In [1]: Pool.balances(0)
Out [1]: 55021540803117067316
In [2]: Pool.balances(1)
Out [2]: 317141267512073253038923
```
<a id="admin-controls"></a>
## Admin Controls
Liquidity pools are deployed via the [Factory](https://docs.curve.finance/developer/amm/factory/twocrypto-ng/overview.md). All pools deployed **share the same admin** defined within the Factory contract. Transferring the ownership of a pool is only possible by changing the ownership of the Factory. Admin is the Curve DAO (OwnershipAdmin).
The same applies to the fee receiver of the pools. See [Factory Ownership](https://docs.curve.finance/developer/amm/factory/overview.md#contract-ownership).
<a id="parameter-changes"></a>
### Parameter Changes
For more information about parameters: [https://nagaking.substack.com/p/deep-dive-curve-v2-parameters](https://nagaking.substack.com/p/deep-dive-curve-v2-parameters).
The appropriate value for `A` and `gamma` is dependent upon the type of coin being used within the pool, and is subject to optimization and pool-parameter update based on the market history of the trading pair.
It is possible to modify the parameters for a pool after it has been deployed. Again, only the admin of the pool (= Factory admin) can do so.
<a id="ramp_a_gamma"></a>
### `ramp_A_gamma`
`TwoCrypto.ramp_A_gamma(future_A: uint256, future_gamma: uint256, future_time: uint256)`
Guarded Method
This function can only be called by the `admin` of the Factory contract.
Function to linearly ramp the values of `A` and `gamma`.
| Input | Type | Description |
| --- | --- | --- |
| `future_A` | `uint256` | Future value of `A` |
| `future_gamma` | `uint256` | Future value of `gamma` |
| `future_time` | `uint256` | Timestamp at which the ramping will end |
Emits: `RampAgamma`
**<>Source code▼**
```vyper
event RampAgamma:
initial_A: uint256
future_A: uint256
initial_gamma: uint256
future_gamma: uint256
initial_time: uint256
future_time: uint256
@external
def ramp_A_gamma(
future_A: uint256, future_gamma: uint256, future_time: uint256
):
"""
@notice Initialise Ramping A and gamma parameter values linearly.
@dev Only accessible by factory admin, and only
@param future_A The future A value.
@param future_gamma The future gamma value.
@param future_time The timestamp at which the ramping will end.
"""
assert msg.sender == factory.admin() # dev: only owner
assert block.timestamp > self.initial_A_gamma_time + (MIN_RAMP_TIME - 1) # dev: ramp undergoing
assert future_time > block.timestamp + MIN_RAMP_TIME - 1 # dev: insufficient time
A_gamma: uint256[2] = self._A_gamma()
initial_A_gamma: uint256 = A_gamma[0] << 128
initial_A_gamma = initial_A_gamma | A_gamma[1]
assert future_A > MIN_A - 1
assert future_A < MAX_A + 1
assert future_gamma > MIN_GAMMA - 1
assert future_gamma < MAX_GAMMA + 1
ratio: uint256 = 10**18 * future_A / A_gamma[0]
assert ratio < 10**18 * MAX_A_CHANGE + 1
assert ratio > 10**18 / MAX_A_CHANGE - 1
ratio = 10**18 * future_gamma / A_gamma[1]
assert ratio < 10**18 * MAX_A_CHANGE + 1
assert ratio > 10**18 / MAX_A_CHANGE - 1
self.initial_A_gamma = initial_A_gamma
self.initial_A_gamma_time = block.timestamp
future_A_gamma: uint256 = future_A << 128
future_A_gamma = future_A_gamma | future_gamma
self.future_A_gamma_time = future_time
self.future_A_gamma = future_A_gamma
log RampAgamma(
A_gamma[0],
future_A,
A_gamma[1],
future_gamma,
block.timestamp,
future_time,
)
```
**▶Example▼**
```shell
>>> TwoCrypto.ramp_A_gamma(2700000, 1300000000000, 1693674492)
```
<a id="stop_ramp_a_gamma"></a>
### `stop_ramp_A_gamma`
`TwoCrypto.stop_ramp_A_gamma()`
Guarded Method
This function can only be called by the `admin` of the Factory contract.
Function to immediately stop the ramping of A and gamma parameters and set them to their current values.
Emits: `StopRampA`
**<>Source code▼**
```vyper
event StopRampA:
current_A: uint256
current_gamma: uint256
time: uint256
@external
def stop_ramp_A_gamma():
"""
@notice Stop Ramping A and gamma parameters immediately.
@dev Only accessible by factory admin.
"""
assert msg.sender == factory.admin() # dev: only owner
A_gamma: uint256[2] = self._A_gamma()
current_A_gamma: uint256 = A_gamma[0] << 128
current_A_gamma = current_A_gamma | A_gamma[1]
self.initial_A_gamma = current_A_gamma
self.future_A_gamma = current_A_gamma
self.initial_A_gamma_time = block.timestamp
self.future_A_gamma_time = block.timestamp
# ------ Now (block.timestamp < t1) is always False, so we return saved A.
log StopRampA(A_gamma[0], A_gamma[1], block.timestamp)
```
**▶Example▼**
```shell
>>> TwoCrypto.stop_ramp_A_gamma()
```
<a id="apply_new_parameters"></a>
### `apply_new_parameters`
`TwoCrypto.apply_new_parameters(_new_mid_fee: uint256, _new_out_fee: uint256, _new_fee_gamma: uint256, _new_allowed_extra_profit: uint256, _new_adjustment_step: uint256, _new_ma_time: uint256, _new_xcp_ma_time: uint256)`
Guarded Method
This function can only be called by the `admin` of the Factory contract.
Function to commit new parameters. The new parameters are applied immediately.
| Input | Type | Description |
| --- | --- | --- |
| `_new_mid_fee` | `uint256` | New `mid_fee` value. |
| `_new_out_fee` | `uint256` | New `out_fee` value. |
| `_new_fee_gamma` | `uint256` | New `fee_gamma` value. |
| `_new_allowed_extra_profit` | `uint256` | New `allowed_extra_profit` value. |
| `_new_adjustment_step` | `uint256` | New `adjustment_step` value. |
| `_new_ma_time` | `uint256` | New `ma_time` value, which is time\_in\_seconds/ln(2). |
| `_new_xcp_ma_time` | `uint256` | New ma time for xcp oracles. |
Emits: `NewParameters`
**<>Source code▼**
```vyper
event NewParameters:
mid_fee: uint256
out_fee: uint256
fee_gamma: uint256
allowed_extra_profit: uint256
adjustment_step: uint256
ma_time: uint256
xcp_ma_time: uint256
@external
@nonreentrant('lock')
def apply_new_parameters(
_new_mid_fee: uint256,
_new_out_fee: uint256,
_new_fee_gamma: uint256,
_new_allowed_extra_profit: uint256,
_new_adjustment_step: uint256,
_new_ma_time: uint256,
_new_xcp_ma_time: uint256,
):
"""
@notice Commit new parameters.
@dev Only accessible by factory admin.
@param _new_mid_fee The new mid fee.
@param _new_out_fee The new out fee.
@param _new_fee_gamma The new fee gamma.
@param _new_allowed_extra_profit The new allowed extra profit.
@param _new_adjustment_step The new adjustment step.
@param _new_ma_time The new ma time. ma_time is time_in_seconds/ln(2).
@param _new_xcp_ma_time The new ma time for xcp oracle.
"""
assert msg.sender == factory.admin() # dev: only owner
# ----------------------------- Set fee params ---------------------------
new_mid_fee: uint256 = _new_mid_fee
new_out_fee: uint256 = _new_out_fee
new_fee_gamma: uint256 = _new_fee_gamma
current_fee_params: uint256[3] = self._unpack_3(self.packed_fee_params)
if new_out_fee < MAX_FEE + 1:
assert new_out_fee > MIN_FEE - 1 # dev: fee is out of range
else:
new_out_fee = current_fee_params[1]
if new_mid_fee > MAX_FEE:
new_mid_fee = current_fee_params[0]
assert new_mid_fee <= new_out_fee # dev: mid-fee is too high
if new_fee_gamma < 10**18:
assert new_fee_gamma > 0 # dev: fee_gamma out of range [1 .. 10**18]
else:
new_fee_gamma = current_fee_params[2]
self.packed_fee_params = self._pack_3([new_mid_fee, new_out_fee, new_fee_gamma])
# ----------------- Set liquidity rebalancing parameters -----------------
new_allowed_extra_profit: uint256 = _new_allowed_extra_profit
new_adjustment_step: uint256 = _new_adjustment_step
new_ma_time: uint256 = _new_ma_time
current_rebalancing_params: uint256[3] = self._unpack_3(self.packed_rebalancing_params)
if new_allowed_extra_profit > 10**18:
new_allowed_extra_profit = current_rebalancing_params[0]
if new_adjustment_step > 10**18:
new_adjustment_step = current_rebalancing_params[1]
if new_ma_time < 872542: # <----- Calculated as: 7 * 24 * 60 * 60 / ln(2)
assert new_ma_time > 86 # dev: MA time should be longer than 60/ln(2)
else:
new_ma_time = current_rebalancing_params[2]
self.packed_rebalancing_params = self._pack_3(
[new_allowed_extra_profit, new_adjustment_step, new_ma_time]
)
# Set xcp oracle moving average window time:
new_xcp_ma_time: uint256 = _new_xcp_ma_time
if new_xcp_ma_time < 872542:
assert new_xcp_ma_time > 86 # dev: xcp MA time should be longer than 60/ln(2)
else:
new_xcp_ma_time = self.xcp_ma_time
self.xcp_ma_time = new_xcp_ma_time
# ---------------------------------- LOG ---------------------------------
log NewParameters(
new_mid_fee,
new_out_fee,
new_fee_gamma,
new_allowed_extra_profit,
new_adjustment_step,
new_ma_time,
_new_xcp_ma_time,
)
```
**▶Example▼**
```shell
>>> TwoCrypto.apply_new_parameters(20000000, 45000000, 350000000000000, 100000000000, 100000000000, 1800, 1800)
```
<a id="initial_a_gamma"></a>
### `initial_A_gamma`
`TwoCrypto.initial_A_gamma -> uint256: view`
Getter for the initial A/gamma.
Returns: A/gamma (`uint256`).
**<>Source code▼**
```vyper
initial_A_gamma: public(uint256)
```
**▶Example▼**
```shell
>>> TwoCrypto.initial_A_gamma()
581076037942835227425498917514114728328226821
```
<a id="initial_a_gamma_time"></a>
### `initial_A_gamma_time`
`TwoCrypto.initial_A_gamma_time -> uint256: view`
Getter for the initial A/gamma time.
Returns: A/gamma time (`uint256`).
**<>Source code▼**
```vyper
initial_A_gamma_time: public(uint256)
```
**▶Example▼**
```shell
>>> TwoCrypto.initial_A_gamma_time()
0
```
<a id="future_a_gamma"></a>
### `future_A_gamma`
`TwoCrypto.future_A_gamma -> uint256: view`
Getter for the future A/gamma.
Returns: future A/gamma (`uint256`).
**<>Source code▼**
```vyper
future_A_gamma: public(uint256)
```
**▶Example▼**
```shell
>>> TwoCrypto.future_A_gamma()
581076037942835227425498917514114728328226821
```
<a id="future_a_gamma_time"></a>
### `future_A_gamma_time`
`TwoCrypto.future_A_gamma_time -> uint256: view`
info
This value is initially set to 0 (default) when the pool is first deployed. It only gets populated by `block.timestamp + future_time` in the `ramp_A_gamma` function when the ramping process is initiated. After ramping is finished (i.e., `self.future_A_gamma_time < block.timestamp`), the variable is left as is and not set to 0.
Getter for the future A/gamma time. This is the timestamp when the ramping process is finished.
Returns: future A/gamma time (`uint256`).
**<>Source code▼**
```vyper
future_A_gamma_time: public(uint256) # <------ Time when ramping is finished.
# This value is 0 (default) when pool is first deployed, and only gets
# populated by block.timestamp + future_time in `ramp_A_gamma` when the
# ramping process is initiated. After ramping is finished
# (i.e. self.future_A_gamma_time < block.timestamp), the variable is left
# and not set to 0.
```
**▶Example▼**
```shell
>>> TwoCrypto.future_A_gamma_time()
0
```
---
# Math Contract
Source: [https://docs.curve.finance/developer/amm/twocrypto-ng/utility-contracts/math](https://docs.curve.finance/developer/amm/twocrypto-ng/utility-contracts/math)
**The Math Contract provides AMM Math for 2-coin Curve Twocrypto-NG Pools.**
Contract Source & Deployment
Source code for프로젝트로 돌아가기