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

[VEN-2999]: add transient storage to Resilient oracle #239

Open
wants to merge 16 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 13 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
12 changes: 4 additions & 8 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 All @@ -114,8 +111,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: Export deployments
run: |
Expand Down
65 changes: 55 additions & 10 deletions contracts/ResilientOracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ contract ResilientOracle is PausableUpgradeable, AccessControlledV8, ResilientOr
/// @custom:oz-upgrades-unsafe-allow state-variable-immutable
BoundValidatorInterface public immutable boundValidator;

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

mapping(address => TokenConfig) private tokenConfigs;

event TokenConfigAdded(
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,8 +324,58 @@ 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);
chechu marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* @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) {
chechu marked this conversation as resolved.
Show resolved Hide resolved
bytes32 slot = keccak256(abi.encode(CACHE_SLOT, key));
assembly ("memory-safe") {
value := tload(slot)
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could extract these functions to an external file, to reuse it in other oracles caching prices

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


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

price = _readCachedPrice(asset);
if (price != 0) {
return price;
}

// Get pivot oracle price, Invalid price if not available or error
(address pivotOracle, bool pivotOracleEnabled) = getOracle(asset, OracleRole.PIVOT);
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