2022-09-13 09:36:36 -07:00
|
|
|
// SPDX-License-Identifier: Apache 2
|
|
|
|
|
|
|
|
pragma solidity ^0.8.0;
|
|
|
|
|
|
|
|
import "../contracts/gasOracle/GasOracle.sol";
|
2022-09-14 11:26:12 -07:00
|
|
|
import {Implementation} from "../wormhole/ethereum/contracts/Implementation.sol";
|
2022-09-13 09:36:36 -07:00
|
|
|
import "forge-std/Test.sol";
|
|
|
|
|
|
|
|
import "forge-std/console.sol";
|
|
|
|
|
2022-09-13 14:44:12 -07:00
|
|
|
contract TestGasOracle is Test {
|
2022-09-13 09:36:36 -07:00
|
|
|
uint16 constant TEST_ORACLE_CHAIN_ID = 2;
|
|
|
|
|
2022-09-14 11:26:12 -07:00
|
|
|
Implementation internal wormhole;
|
2022-09-13 14:44:12 -07:00
|
|
|
GasOracle internal gasOracle;
|
|
|
|
|
2022-09-14 11:26:12 -07:00
|
|
|
function initializeGasOracle() internal {
|
|
|
|
wormhole = new Implementation();
|
2022-09-13 09:36:36 -07:00
|
|
|
|
2022-09-14 11:26:12 -07:00
|
|
|
// chainId, governanceChainId
|
|
|
|
vm.store(address(wormhole), bytes32(0), 0x0000000000000000000000000000000000000000000000000000000000010002);
|
2022-09-13 09:36:36 -07:00
|
|
|
|
2022-09-14 11:26:12 -07:00
|
|
|
gasOracle = new GasOracle(address(wormhole));
|
2022-09-13 09:36:36 -07:00
|
|
|
|
2022-09-13 14:44:12 -07:00
|
|
|
require(gasOracle.owner() == address(this), "owner() != expected");
|
2022-09-14 11:26:12 -07:00
|
|
|
require(gasOracle.chainId() == wormhole.chainId(), "chainId() != expected");
|
|
|
|
require(address(gasOracle.wormhole()) == address(wormhole), "wormhole() != expected");
|
2022-09-13 09:36:36 -07:00
|
|
|
}
|
|
|
|
|
2022-09-13 14:44:12 -07:00
|
|
|
function testCannotUpdatePriceWithChainIdZero(uint128 updateGasPrice, uint128 updateNativeCurrencyPrice) public {
|
2022-09-13 09:36:36 -07:00
|
|
|
vm.assume(updateGasPrice > 0);
|
|
|
|
vm.assume(updateNativeCurrencyPrice > 0);
|
|
|
|
|
2022-09-14 11:26:12 -07:00
|
|
|
initializeGasOracle();
|
2022-09-13 09:36:36 -07:00
|
|
|
|
|
|
|
// you shall not pass
|
|
|
|
vm.expectRevert("updateChainId == 0");
|
2022-09-13 14:44:12 -07:00
|
|
|
gasOracle.updatePrice(
|
2022-09-13 09:36:36 -07:00
|
|
|
0, // updateChainId
|
|
|
|
updateGasPrice,
|
|
|
|
updateNativeCurrencyPrice
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-09-13 14:44:12 -07:00
|
|
|
function testCannotUpdatePriceWithGasPriceZero(uint16 updateChainId, uint128 updateNativeCurrencyPrice) public {
|
2022-09-13 09:36:36 -07:00
|
|
|
vm.assume(updateChainId > 0);
|
|
|
|
vm.assume(updateNativeCurrencyPrice > 0);
|
|
|
|
|
2022-09-14 11:26:12 -07:00
|
|
|
initializeGasOracle();
|
2022-09-13 09:36:36 -07:00
|
|
|
|
|
|
|
// you shall not pass
|
|
|
|
vm.expectRevert("updateGasPrice == 0");
|
2022-09-13 14:44:12 -07:00
|
|
|
gasOracle.updatePrice(
|
2022-09-13 09:36:36 -07:00
|
|
|
updateChainId,
|
|
|
|
0, // updateGasPrice == 0
|
|
|
|
updateNativeCurrencyPrice
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-09-13 14:44:12 -07:00
|
|
|
function testCannotUpdatePriceWithNativeCurrencyPriceZero(uint16 updateChainId, uint128 updateGasPrice) public {
|
2022-09-13 09:36:36 -07:00
|
|
|
vm.assume(updateChainId > 0);
|
|
|
|
vm.assume(updateGasPrice > 0);
|
|
|
|
|
2022-09-14 11:26:12 -07:00
|
|
|
initializeGasOracle();
|
2022-09-13 09:36:36 -07:00
|
|
|
|
|
|
|
// you shall not pass
|
|
|
|
vm.expectRevert("updateNativeCurrencyPrice == 0");
|
2022-09-13 14:44:12 -07:00
|
|
|
gasOracle.updatePrice(
|
2022-09-13 09:36:36 -07:00
|
|
|
updateChainId,
|
|
|
|
updateGasPrice,
|
|
|
|
0 // updateNativeCurrencyPrice == 0
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function testCanUpdatePriceOnlyAsOwner(
|
|
|
|
address oracleOwner,
|
|
|
|
uint16 updateChainId,
|
2022-09-13 14:44:12 -07:00
|
|
|
uint128 updateGasPrice,
|
|
|
|
uint128 updateNativeCurrencyPrice
|
2022-09-13 09:36:36 -07:00
|
|
|
)
|
|
|
|
public
|
|
|
|
{
|
|
|
|
vm.assume(oracleOwner != address(0));
|
2022-09-13 14:44:12 -07:00
|
|
|
vm.assume(oracleOwner != address(this));
|
2022-09-13 09:36:36 -07:00
|
|
|
vm.assume(updateChainId > 0);
|
|
|
|
vm.assume(updateGasPrice > 0);
|
|
|
|
vm.assume(updateNativeCurrencyPrice > 0);
|
|
|
|
|
2022-09-14 11:26:12 -07:00
|
|
|
initializeGasOracle();
|
2022-09-13 09:36:36 -07:00
|
|
|
|
|
|
|
// you shall not pass
|
2022-09-13 14:44:12 -07:00
|
|
|
vm.prank(oracleOwner);
|
2022-09-13 09:36:36 -07:00
|
|
|
vm.expectRevert("owner() != _msgSender()");
|
2022-09-13 14:44:12 -07:00
|
|
|
gasOracle.updatePrice(updateChainId, updateGasPrice, updateNativeCurrencyPrice);
|
2022-09-13 09:36:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function testCannotGetPriceBeforeUpdateSrcPrice(
|
|
|
|
uint16 dstChainId,
|
2022-09-13 14:44:12 -07:00
|
|
|
uint128 dstGasPrice,
|
|
|
|
uint128 dstNativeCurrencyPrice
|
2022-09-13 09:36:36 -07:00
|
|
|
)
|
|
|
|
public
|
|
|
|
{
|
|
|
|
vm.assume(dstChainId > 0);
|
|
|
|
vm.assume(dstChainId != TEST_ORACLE_CHAIN_ID);
|
|
|
|
vm.assume(dstGasPrice > 0);
|
|
|
|
vm.assume(dstNativeCurrencyPrice > 0);
|
|
|
|
|
2022-09-14 11:26:12 -07:00
|
|
|
initializeGasOracle();
|
2022-09-13 09:36:36 -07:00
|
|
|
|
|
|
|
// update the price with reasonable values
|
2022-09-13 14:44:12 -07:00
|
|
|
gasOracle.updatePrice(dstChainId, dstGasPrice, dstNativeCurrencyPrice);
|
2022-09-13 09:36:36 -07:00
|
|
|
|
|
|
|
// you shall not pass
|
|
|
|
vm.expectRevert("srcNativeCurrencyPrice == 0");
|
2022-09-30 09:18:49 -07:00
|
|
|
gasOracle.computeGasCost(dstChainId, 1);
|
2022-09-13 09:36:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function testCannotGetPriceBeforeUpdateDstPrice(
|
|
|
|
uint16 dstChainId,
|
2022-09-13 14:44:12 -07:00
|
|
|
uint128 srcGasPrice,
|
|
|
|
uint128 srcNativeCurrencyPrice
|
2022-09-13 09:36:36 -07:00
|
|
|
)
|
|
|
|
public
|
|
|
|
{
|
|
|
|
vm.assume(dstChainId > 0);
|
|
|
|
vm.assume(dstChainId != TEST_ORACLE_CHAIN_ID);
|
|
|
|
vm.assume(srcGasPrice > 0);
|
|
|
|
vm.assume(srcNativeCurrencyPrice > 0);
|
|
|
|
|
2022-09-14 11:26:12 -07:00
|
|
|
initializeGasOracle();
|
2022-09-13 09:36:36 -07:00
|
|
|
|
|
|
|
// update the price with reasonable values
|
2022-09-13 14:44:12 -07:00
|
|
|
//vm.prank(gasOracle.owner());
|
2022-09-14 11:26:12 -07:00
|
|
|
gasOracle.updatePrice(gasOracle.chainId(), srcGasPrice, srcNativeCurrencyPrice);
|
2022-09-13 09:36:36 -07:00
|
|
|
|
|
|
|
// you shall not pass
|
|
|
|
vm.expectRevert("dstNativeCurrencyPrice == 0");
|
2022-09-30 09:18:49 -07:00
|
|
|
gasOracle.computeGasCost(dstChainId, 1);
|
2022-09-13 09:36:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function testUpdatePrice(
|
|
|
|
uint16 dstChainId,
|
2022-09-13 14:44:12 -07:00
|
|
|
uint128 dstGasPrice,
|
2022-09-30 09:18:49 -07:00
|
|
|
uint64 dstNativeCurrencyPrice,
|
2022-09-13 14:44:12 -07:00
|
|
|
uint128 srcGasPrice,
|
2022-09-30 09:18:49 -07:00
|
|
|
uint64 srcNativeCurrencyPrice,
|
|
|
|
uint64 gasLimit
|
2022-09-13 09:36:36 -07:00
|
|
|
)
|
|
|
|
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);
|
|
|
|
|
2022-09-14 11:26:12 -07:00
|
|
|
initializeGasOracle();
|
2022-09-13 09:36:36 -07:00
|
|
|
|
|
|
|
// update the prices with reasonable values
|
2022-09-13 14:44:12 -07:00
|
|
|
gasOracle.updatePrice(dstChainId, dstGasPrice, dstNativeCurrencyPrice);
|
2022-09-14 11:26:12 -07:00
|
|
|
gasOracle.updatePrice(gasOracle.chainId(), srcGasPrice, srcNativeCurrencyPrice);
|
2022-09-13 09:36:36 -07:00
|
|
|
|
|
|
|
// verify price
|
2022-09-30 09:18:49 -07:00
|
|
|
uint256 expected = (uint256(dstGasPrice) * dstNativeCurrencyPrice * gasLimit) / srcNativeCurrencyPrice;
|
|
|
|
require(gasOracle.computeGasCost(dstChainId, gasLimit) == expected, "gasOracle.computeGasCost(...) != expected");
|
2022-09-13 09:36:36 -07:00
|
|
|
}
|
2022-09-13 13:51:57 -07:00
|
|
|
|
|
|
|
function testUpdatePrices(
|
|
|
|
uint16 dstChainId,
|
2022-09-13 14:44:12 -07:00
|
|
|
uint128 dstGasPrice,
|
2022-09-30 09:18:49 -07:00
|
|
|
uint64 dstNativeCurrencyPrice,
|
2022-09-13 14:44:12 -07:00
|
|
|
uint128 srcGasPrice,
|
2022-09-30 09:18:49 -07:00
|
|
|
uint64 srcNativeCurrencyPrice,
|
|
|
|
uint64 gasLimit
|
2022-09-13 13:51:57 -07:00
|
|
|
)
|
|
|
|
public
|
|
|
|
{
|
|
|
|
vm.assume(dstChainId > 0);
|
2022-09-14 11:26:12 -07:00
|
|
|
vm.assume(dstChainId != TEST_ORACLE_CHAIN_ID); // wormhole.chainId()
|
2022-09-13 13:51:57 -07:00
|
|
|
vm.assume(dstGasPrice > 0);
|
|
|
|
vm.assume(dstNativeCurrencyPrice > 0);
|
|
|
|
vm.assume(srcGasPrice > 0);
|
|
|
|
vm.assume(srcNativeCurrencyPrice > 0);
|
|
|
|
|
2022-09-14 11:26:12 -07:00
|
|
|
initializeGasOracle();
|
2022-09-13 13:51:57 -07:00
|
|
|
|
2022-09-13 14:44:12 -07:00
|
|
|
GasOracle.UpdatePrice[] memory updates = new GasOracle.UpdatePrice[](2);
|
|
|
|
updates[0] = GasOracle.UpdatePrice({
|
2022-09-14 11:26:12 -07:00
|
|
|
chainId: gasOracle.chainId(),
|
2022-09-13 13:51:57 -07:00
|
|
|
gasPrice: srcGasPrice,
|
|
|
|
nativeCurrencyPrice: srcNativeCurrencyPrice
|
|
|
|
});
|
2022-09-13 14:44:12 -07:00
|
|
|
updates[1] = GasOracle.UpdatePrice({
|
|
|
|
chainId: dstChainId,
|
|
|
|
gasPrice: dstGasPrice,
|
|
|
|
nativeCurrencyPrice: dstNativeCurrencyPrice
|
|
|
|
});
|
2022-09-13 13:51:57 -07:00
|
|
|
|
|
|
|
// update the prices with reasonable values
|
2022-09-13 14:44:12 -07:00
|
|
|
gasOracle.updatePrices(updates);
|
2022-09-13 13:51:57 -07:00
|
|
|
|
|
|
|
// verify price
|
2022-09-30 09:18:49 -07:00
|
|
|
uint256 expected = (uint256(dstGasPrice) * dstNativeCurrencyPrice * gasLimit) / srcNativeCurrencyPrice;
|
|
|
|
require(gasOracle.computeGasCost(dstChainId, gasLimit) == expected, "gasOracle.computeGasCost(...) != expected");
|
2022-09-13 13:51:57 -07:00
|
|
|
}
|
2022-09-13 09:36:36 -07:00
|
|
|
}
|