ethereum: add updatePrices

This commit is contained in:
Karl Kempe 2022-09-13 20:51:57 +00:00
parent 70a7af84ee
commit dc98293ae5
2 changed files with 59 additions and 0 deletions

View File

@ -8,6 +8,12 @@ import "./GasOracleSetters.sol";
import "./GasOracleGovernance.sol";
abstract contract GasOracle is GasOracleGovernance {
struct UpdatePrice {
uint16 chainId;
uint256 gasPrice;
uint256 nativeCurrencyPrice;
}
function getPrice(uint16 targetChainId) public view returns (uint256 quote) {
uint256 srcNativeCurrencyPrice = nativeCurrencyPrice(chainId());
require(srcNativeCurrencyPrice > 0, "srcNativeCurrencyPrice == 0");
@ -27,4 +33,14 @@ abstract contract GasOracle is GasOracleGovernance {
require(updateNativeCurrencyPrice > 0, "updateNativeCurrencyPrice == 0");
setPriceInfo(updateChainId, updateGasPrice, updateNativeCurrencyPrice);
}
function updatePrices(UpdatePrice[] memory updates) public onlyOwner {
uint256 pricesLen = updates.length;
for (uint256 i = 0; i < pricesLen;) {
updatePrice(updates[i].chainId, updates[i].gasPrice, updates[i].nativeCurrencyPrice);
unchecked {
i += 1;
}
}
}
}

View File

@ -233,4 +233,47 @@ contract TestGasOracle is GasOracle, GasOracleSetup, Test {
uint256 expected = dstGasPrice * dstNativeCurrencyPrice / srcNativeCurrencyPrice;
require(getPrice(dstChainId) == expected, "getPrice(updateChainId) != expected");
}
function testUpdatePrices(
uint16 dstChainId,
uint256 dstGasPrice,
uint256 dstNativeCurrencyPrice,
uint256 srcGasPrice,
uint256 srcNativeCurrencyPrice
)
public
{
vm.assume(dstChainId > 0);
vm.assume(dstChainId != TEST_ORACLE_CHAIN_ID);
vm.assume(dstGasPrice > 0);
vm.assume(dstNativeCurrencyPrice > 0);
vm.assume(srcGasPrice > 0);
vm.assume(srcNativeCurrencyPrice > 0);
// we will also assume reasonable values for gasPrice and nativeCurrencyPrice
vm.assume(dstGasPrice < 2 ** 128);
vm.assume(dstNativeCurrencyPrice < 2 ** 128);
vm.assume(srcGasPrice < 2 ** 128);
vm.assume(srcNativeCurrencyPrice < 2 ** 128);
initializeGasOracle(
_msgSender(),
TEST_ORACLE_CHAIN_ID // chainId
);
UpdatePrice[] memory updates = new UpdatePrice[](2);
updates[0] = UpdatePrice({
chainId: TEST_ORACLE_CHAIN_ID,
gasPrice: srcGasPrice,
nativeCurrencyPrice: srcNativeCurrencyPrice
});
updates[1] =
UpdatePrice({chainId: dstChainId, gasPrice: dstGasPrice, nativeCurrencyPrice: dstNativeCurrencyPrice});
// update the prices with reasonable values
updatePrices(updates);
// verify price
uint256 expected = dstGasPrice * dstNativeCurrencyPrice / srcNativeCurrencyPrice;
require(getPrice(dstChainId) == expected, "getPrice(updateChainId) != expected");
}
}