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

Add transient storage to Resilient oracle #239

Draft
wants to merge 7 commits into
base: develop
Choose a base branch
from
Draft
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
3 changes: 1 addition & 2 deletions .github/workflows/cd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ jobs:
cache: "yarn"

- name: Install dependencies
# Hack to get around failing "ethereumjs-abi The remote archive doesn't match the expected checksum" error
run: YARN_CHECKSUM_BEHAVIOR=update yarn
run: yarn

- name: Build
run: yarn build
Expand Down
9 changes: 3 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ jobs:
cache: "yarn"

- name: Install dependencies
# Hack to get around failing "ethereumjs-abi The remote archive doesn't match the expected checksum" error
run: YARN_CHECKSUM_BEHAVIOR=update yarn
run: yarn

- name: Compile contract types
run: yarn hardhat compile
Expand All @@ -49,8 +48,7 @@ jobs:
cache: "yarn"

- name: Install dependencies
# Hack to get around failing "ethereumjs-abi The remote archive doesn't match the expected checksum" error
run: YARN_CHECKSUM_BEHAVIOR=update yarn
run: yarn

- name: Run hardhat tests coverage
run: yarn hardhat:coverage
Expand Down Expand Up @@ -89,8 +87,7 @@ jobs:
cache: "yarn"

- name: Install dependencies
# Hack to get around failing "ethereumjs-abi The remote archive doesn't match the expected checksum" error
run: YARN_CHECKSUM_BEHAVIOR=update yarn
run: yarn

- name: Verify deployments work
run: yarn hardhat deploy
Expand Down
59 changes: 49 additions & 10 deletions contracts/ResilientOracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ contract ResilientOracle is PausableUpgradeable, AccessControlledV8, ResilientOr

mapping(address => TokenConfig) private tokenConfigs;

/// Slot to cache the asset's price, used for transient storage
bytes32 constant CACHE_SLOT = keccak256(abi.encode("venus-protocol/oracle/ResilientOracle/cache"));

event TokenConfigAdded(
address indexed asset,
address indexed mainOracle,
Expand Down Expand Up @@ -233,11 +236,7 @@ contract ResilientOracle is PausableUpgradeable, AccessControlledV8, ResilientOr
*/
function updatePrice(address vToken) external override {
address asset = _getUnderlyingAsset(vToken);
(address pivotOracle, bool pivotOracleEnabled) = getOracle(asset, OracleRole.PIVOT);
if (pivotOracle != address(0) && pivotOracleEnabled) {
//if pivot oracle is not TwapOracle it will revert so we need to catch the revert
try TwapInterface(pivotOracle).updateTwap(asset) {} catch {}
}
_updateAssetPrice(asset);
}

/**
Expand All @@ -246,11 +245,7 @@ contract ResilientOracle is PausableUpgradeable, AccessControlledV8, ResilientOr
* @param asset asset address
*/
function updateAssetPrice(address asset) external {
(address pivotOracle, bool pivotOracleEnabled) = getOracle(asset, OracleRole.PIVOT);
if (pivotOracle != address(0) && pivotOracleEnabled) {
//if pivot oracle is not TwapOracle it will revert so we need to catch the revert
try TwapInterface(pivotOracle).updateTwap(asset) {} catch {}
}
_updateAssetPrice(asset);
}

/**
Expand Down Expand Up @@ -329,6 +324,50 @@ contract ResilientOracle is PausableUpgradeable, AccessControlledV8, ResilientOr
enabled = tokenConfigs[asset].enableFlagsForOracles[uint256(role)];
}

/**
* @notice Updates the pivot oracle price. Currently using TWAP
* @dev Cache the asset price and return if already cached
* @param asset asset address
*/
function _updateAssetPrice(address asset) internal {
if (_readCachedPrice(asset) != 0) {
return;
}

(address pivotOracle, bool pivotOracleEnabled) = getOracle(asset, OracleRole.PIVOT);
if (pivotOracle != address(0) && pivotOracleEnabled) {
//if pivot oracle is not TwapOracle it will revert so we need to catch the revert
try TwapInterface(pivotOracle).updateTwap(asset) {} catch {}
}

uint256 price = _getPrice(asset);
_cachePrice(asset, price);
}

/**
* @notice Cache the asset price into transient storage
* @param key address of the asset
* @param value asset price
*/
function _cachePrice(address key, uint256 value) internal {
bytes32 slot = keccak256(abi.encode(CACHE_SLOT, key));
assembly ("memory-safe") {
tstore(slot, value)
}
}

/**
* @notice Read cached price from transient storage
* @param key address of the asset
* @return value cached asset price
*/
function _readCachedPrice(address key) internal view returns (uint256 value) {
bytes32 slot = keccak256(abi.encode(CACHE_SLOT, key));
assembly ("memory-safe") {
value := tload(slot)
}
}

function _getPrice(address asset) internal view returns (uint256) {
uint256 pivotPrice = INVALID_PRICE;

Expand Down
2 changes: 1 addition & 1 deletion hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ const config: HardhatUserConfig = {
yul: !process.env.CI,
},
},
evmVersion: "paris",
evmVersion: "cancun",
outputSelection: {
"*": {
"*": ["storageLayout"],
Expand Down
2 changes: 1 addition & 1 deletion hardhat.config.zksync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ const config: HardhatUserConfig = {
yul: !process.env.CI,
},
},
evmVersion: "paris",
evmVersion: "cancun",
outputSelection: {
"*": {
"*": ["storageLayout"],
Expand Down
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@
},
"dependencies": {
"@chainlink/contracts": "^0.5.1",
"@defi-wonderland/smock": "2.3.5",
"@defi-wonderland/smock": "2.4.0",
"@nomicfoundation/hardhat-network-helpers": "^1.0.8",
"@openzeppelin/contracts": "^4.6.0",
"@openzeppelin/contracts-upgradeable": "^4.7.3",
"@venusprotocol/governance-contracts": "^2.4.0",
"@venusprotocol/solidity-utilities": "^2.0.0",
"@venusprotocol/venus-protocol": "^9.1.0",
"ethers": "^5.6.8",
"hardhat": "2.19.5",
"hardhat": "2.22.15",
"hardhat-deploy": "^0.12.4",
"module-alias": "^2.2.2",
"solidity-docgen": "^0.6.0-beta.29"
Expand Down Expand Up @@ -117,5 +117,9 @@
},
"_moduleAliases": {
"@nomiclabs/hardhat-ethers": "node_modules/hardhat-deploy-ethers"
},
"resolutions": {
"hardhat": "2.22.15",
"@defi-wonderland/smock": "2.4.0"
}
}
Loading
Loading