Skip to content

Commit

Permalink
Merge pull request #11 from Unstoppable-DeFi/fix-104
Browse files Browse the repository at this point in the history
fix-104: individual oracle freshness thresholds
  • Loading branch information
Unstoppable-DeFi authored Jul 26, 2023
2 parents 849e2a3 + 05bc53b commit 621278f
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
11 changes: 8 additions & 3 deletions contracts/margin-dex/Vault.vy
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ PERCENTAGE_BASE: constant(uint256) = 100_00 # == 100%
PERCENTAGE_BASE_HIGH_PRECISION: constant(uint256) = 100_00_000 # == 100%

ARBITRUM_SEQUENCER_UPTIME_FEED: constant(address) = 0xFdB631F5EE196F0ed6FAa767959853A9F217697D
ORACLE_FRESHNESS_THRESHOLD: constant(uint256) = 24*60*60 # 24h

WETH: constant(address) = 0x82aF49447D8a07e3bd95BD0d56f35241523fBab1

Expand All @@ -77,6 +76,7 @@ is_enabled_market: HashMap[address, HashMap[address, bool]]
max_leverage: public(HashMap[address, HashMap[address, uint256]])
# token -> Chainlink oracle
to_usd_oracle: public(HashMap[address, address])
oracle_freshness_threshold: public(HashMap[address, uint256])
# token_in -> # token_out -> slippage
liquidate_slippage: public(HashMap[address, HashMap[address, uint256]])

Expand Down Expand Up @@ -587,7 +587,7 @@ def _to_usd_oracle_price(_token: address) -> uint256:
self.to_usd_oracle[_token]
).latestRoundData()

assert (block.timestamp - updated_at) < ORACLE_FRESHNESS_THRESHOLD, "oracle not fresh"
assert (block.timestamp - updated_at) < self.oracle_freshness_threshold[self.to_usd_oracle[_token]], "oracle not fresh"

usd_price: uint256 = convert(answer, uint256) # 8 dec
return usd_price
Expand Down Expand Up @@ -1486,12 +1486,17 @@ def set_is_accepting_new_orders(_is_accepting_new_orders: bool):
# allowed tokens & markets
#
@external
def whitelist_token(_token: address, _token_to_usd_oracle: address) -> uint256:
def whitelist_token(
_token: address,
_token_to_usd_oracle: address,
_oracle_freshness_threshold: uint256) -> uint256:
assert msg.sender == self.admin, "unauthorized"
assert self.is_whitelisted_token[_token] == False, "already whitelisted"
assert _oracle_freshness_threshold > 0, "invalid oracle freshness threshold"

self.is_whitelisted_token[_token] = True
self.to_usd_oracle[_token] = _token_to_usd_oracle
self.oracle_freshness_threshold[_token_to_usd_oracle] = _oracle_freshness_threshold

return self._to_usd_oracle_price(_token)

Expand Down
6 changes: 3 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ def dex():
@pytest.fixture(scope="session", autouse=True)
def vault(eth_usd_oracle, usdc_usd_oracle, wbtc_usd_oracle):
vault = boa.load("contracts/margin-dex/Vault.vy")
vault.whitelist_token(pytest.WETH, pytest.ETH_USD_ORACLE)
vault.whitelist_token(pytest.USDC, pytest.USDC_USD_ORACLE)
vault.whitelist_token(pytest.WBTC, pytest.WBTC_USD_ORACLE)
vault.whitelist_token(pytest.WETH, pytest.ETH_USD_ORACLE, 60*60*24)
vault.whitelist_token(pytest.USDC, pytest.USDC_USD_ORACLE, 60*60*24)
vault.whitelist_token(pytest.WBTC, pytest.WBTC_USD_ORACLE, 60*60*24)
vault.enable_market(pytest.WETH, pytest.USDC, 50)
vault.enable_market(pytest.USDC, pytest.WETH, 50)
vault.set_variable_interest_parameters(pytest.WETH, 0, 0, 0, 80_00_000)
Expand Down
Binary file added tintinweb.vscode-vyper-0.0.15.vsix
Binary file not shown.

0 comments on commit 621278f

Please sign in to comment.