Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

-ng oracle #73

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 29 additions & 17 deletions contracts/price_oracles/AggregateStablePrice3.vy
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ interface Stableswap:
def coins(i: uint256) -> address: view
def get_virtual_price() -> uint256: view
def totalSupply() -> uint256: view
def D_oracle() -> uint256: view


struct PricePair:
pool: Stableswap
is_inverse: bool
include_index: bool
is_ng: bool


event AddPricePair:
Expand Down Expand Up @@ -93,7 +94,7 @@ def add_price_pair(_pool: Stableswap):
revert_on_failure=False
)
if success:
price_pair.include_index = True
price_pair.is_ng = True
coins: address[2] = [_pool.coins(0), _pool.coins(1)]
if coins[0] == STABLECOIN:
price_pair.is_inverse = True
Expand Down Expand Up @@ -169,12 +170,17 @@ def _ema_tvl() -> DynArray[uint256, MAX_PAIRS]:
for i in range(MAX_PAIRS):
if i == n_price_pairs:
break
tvl: uint256 = self.last_tvl[i]
if alpha != 10**18:
# alpha = 1.0 when dt = 0
# alpha = 0.0 when dt = inf
new_tvl: uint256 = self.price_pairs[i].pool.totalSupply() # We don't do virtual price here to save on gas
tvl = (new_tvl * (10**18 - alpha) + tvl * alpha) / 10**18
tvl: uint256 = 0
if self.price_pairs[i].is_ng:
tvl = self.price_pairs[i].pool.D_oracle()
else:
tvl = self.last_tvl[i]
if alpha != 10**18:
# alpha = 1.0 when dt = 0
# alpha = 0.0 when dt = inf
new_tvl: uint256 = self.price_pairs[i].pool.totalSupply() * 10**18 /\
self.price_pairs[i].pool.get_virtual_price()
tvl = (new_tvl * (10**18 - alpha) + tvl * alpha) / 10**18
tvls.append(tvl)

return tvls
Expand All @@ -186,6 +192,19 @@ def ema_tvl() -> DynArray[uint256, MAX_PAIRS]:
return self._ema_tvl()


@internal
@view
def _get_p(price_pair: PricePair) -> uint256:
p: uint256 = 0
if price_pair.is_ng:
p = price_pair.pool.price_oracle(0)
else:
p = price_pair.pool.price_oracle()
if price_pair.is_inverse:
p = 10**36 / p
return p


@internal
@view
def _price(tvls: DynArray[uint256, MAX_PAIRS]) -> uint256:
Expand All @@ -200,17 +219,10 @@ def _price(tvls: DynArray[uint256, MAX_PAIRS]) -> uint256:
price_pair: PricePair = self.price_pairs[i]
pool_supply: uint256 = tvls[i]
if pool_supply >= MIN_LIQUIDITY:
p: uint256 = 0
if price_pair.include_index:
p = price_pair.pool.price_oracle(0)
else:
p = price_pair.pool.price_oracle()
if price_pair.is_inverse:
p = 10**36 / p
prices[i] = p
prices[i] = self._get_p(price_pair)
D[i] = pool_supply
Dsum += pool_supply
DPsum += pool_supply * p
DPsum += pool_supply * prices[i]
if Dsum == 0:
return 10**18 # Placeholder for no active pools
p_avg: uint256 = DPsum / Dsum
Expand Down
33 changes: 33 additions & 0 deletions contracts/price_oracles/StableSwapNGAdapter.vy
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# pragma version 0.3.10
"""
@title StableSwapNGAdapter
@author Curve.Fi
@notice View contract to use -ng StableSwap in AggregateStablePrice with only old pools support
"""

interface Stableswap:
def price_oracle(i: uint256) -> uint256: view


TARGET: public(immutable(Stableswap))


@external
def __init__(_address: Stableswap):
TARGET = _address


@view
@external
def __default__() -> uint256:
# Needed methods are view and return exactly 1 slot:
# def coins(i: uint256) -> address: view
# def get_virtual_price() -> uint256: view
# def totalSupply() -> uint256: view
return convert(raw_call(TARGET.address, msg.data, max_outsize=32, is_static_call=True), uint256)


@view
@external
def price_oracle() -> uint256:
return TARGET.price_oracle(0)
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ def test_price_aggregator(stableswap_a, stableswap_b, stablecoin_a, agg, admin):
assert agg.price_pairs(0)[0].lower() == stableswap_a.address.lower()
assert agg.price_pairs(1)[0].lower() == stableswap_b.address.lower()

# Make ema_tvl and D_oracle (between old and -ng pools) equal
for i in range(2):
agg.eval(f"self.last_tvl[{i}] = self.price_pairs[{i}].pool.totalSupply()")
with boa.env.anchor():
with boa.env.prank(admin):
stablecoin_a._mint_for_testing(admin, amount)
Expand Down