Skip to content

Commit

Permalink
test(contracts): test gas pump (#1791)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinhalliday authored Aug 30, 2024
1 parent 77f59f3 commit b41cf27
Show file tree
Hide file tree
Showing 7 changed files with 316 additions and 16 deletions.
2 changes: 1 addition & 1 deletion contracts/bindings/feeoraclev1.go

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions contracts/core/.gas-snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ OmniBridgeNative_Test:test_pauseAll() (gas: 42776)
OmniBridgeNative_Test:test_pauseBridging() (gas: 36477)
OmniBridgeNative_Test:test_pauseWithdraws() (gas: 52062)
OmniBridgeNative_Test:test_withdraw() (gas: 262456)
OmniGasPump_Test:test_fillUp() (gas: 230967)
OmniGasPump_Test:test_pause() (gas: 62653)
OmniGasPump_Test:test_setMaxSwap() (gas: 34771)
OmniGasPump_Test:test_setOmniGasStation() (gas: 36697)
OmniGasPump_Test:test_setOracle() (gas: 35014)
OmniGasPump_Test:test_setToll() (gas: 34880)
OmniGasStation_Test:test_pause() (gas: 65258)
OmniGasStation_Test:test_setPump() (gas: 83832)
OmniGasStation_Test:test_settleUp() (gas: 364838)
Expand Down
19 changes: 19 additions & 0 deletions contracts/core/src/interfaces/IConversionRateOracle.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// SPDX-License-Identifier: GPL-3.0-only
pragma solidity ^0.8.12;

/**
* @title IConversionRateOracle
* @notice Provides conversion rate from other chain's native token to this chain's native token.
*/
interface IConversionRateOracle {
/**
* @notice Returns the conversion rate (as a numerator over CONVERSION_RATE_DENOM) from `chainId`'s
* native token to this chain's native token.
*/
function toNativeRate(uint64 chainId) external view returns (uint256);

/**
* @notice Denominator used in to conversion rate calculations.
*/
function CONVERSION_RATE_DENOM() external view returns (uint256);
}
8 changes: 2 additions & 6 deletions contracts/core/src/interfaces/IFeeOracleV1.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
pragma solidity ^0.8.12;

import { IFeeOracle } from "./IFeeOracle.sol";
import { IConversionRateOracle } from "./IConversionRateOracle.sol";

/**
* @title IFeeOracleV1
* @notice Extends IFeeOracle with FeeOracleV1 methods
*/
interface IFeeOracleV1 is IFeeOracle {
interface IFeeOracleV1 is IFeeOracle, IConversionRateOracle {
/**
* @notice Emitted when fee parameters for a chain are set.
*/
Expand Down Expand Up @@ -83,9 +84,4 @@ interface IFeeOracleV1 is IFeeOracle {
* @notice Set the manager admin account.
*/
function setManager(address manager_) external;

/**
* @notice returns the conversion rate denominator, used in to
*/
function CONVERSION_RATE_DENOM() external view returns (uint256);
}
38 changes: 30 additions & 8 deletions contracts/core/src/token/OmniGasPump.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pragma solidity 0.8.24;
import { OwnableUpgradeable } from "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";
import { PausableUpgradeable } from "@openzeppelin/contracts-upgradeable/utils/PausableUpgradeable.sol";
import { XAppUpgradeable } from "src/pkg/XAppUpgradeable.sol";
import { FeeOracleV1 } from "src/xchain/FeeOracleV1.sol";
import { IConversionRateOracle } from "src/interfaces/IConversionRateOracle.sol";
import { ConfLevel } from "src/libraries/ConfLevel.sol";
import { OmniGasStation } from "./OmniGasStation.sol";

Expand All @@ -19,6 +19,9 @@ contract OmniGasPump is XAppUpgradeable, OwnableUpgradeable, PausableUpgradeable
/// @notice Emitted when the gas station is set
event GasStationSet(address station);

/// @notice Emitted when the fee oracle is set
event OracleSet(address oracle);

/// @notice Emitted when the toll is set
event TollSet(uint256 pct);

Expand All @@ -34,10 +37,13 @@ contract OmniGasPump is XAppUpgradeable, OwnableUpgradeable, PausableUpgradeable
event FilledUp(address indexed recipient, uint256 owed, uint256 amtETH, uint256 fee, uint256 toll, uint256 amtOMNI);

/// @notice Gas limit passed to OmniGasStation.settleUp xcall
uint64 internal constant SETTLE_GAS = 100_000;
uint64 public constant SETTLE_GAS = 100_000;

/// @notice Denominator for toll percentage calculations
uint256 internal constant TOLL_DENOM = 1000;
uint256 public constant TOLL_DENOM = 1000;

/// @notice Native token conversion rate oracle
IConversionRateOracle public oracle;

/// @notice Address of OmniGasStation on Omni
address public gasStation;
Expand All @@ -57,16 +63,19 @@ contract OmniGasPump is XAppUpgradeable, OwnableUpgradeable, PausableUpgradeable

struct InitParams {
address gasStation;
address oracle;
address portal;
address owner;
uint256 maxSwap;
uint256 toll;
}

function initialize(InitParams calldata p) external initializer {
_setOracle(p.oracle);
_setGasStation(p.gasStation);
_setMaxSwap(p.maxSwap);
_setToll(p.toll);

__XApp_init(p.portal, ConfLevel.Latest);
__Ownable_init(p.owner);
}
Expand Down Expand Up @@ -160,14 +169,15 @@ contract OmniGasPump is XAppUpgradeable, OwnableUpgradeable, PausableUpgradeable

/// @notice Converts `amtOMNI` to ETH, using the current conversion rate
function _toEth(uint256 amtOMNI) internal view returns (uint256) {
FeeOracleV1 o = FeeOracleV1(omni.feeOracle());
return amtOMNI * o.CONVERSION_RATE_DENOM() / o.toNativeRate(omniChainId());
// toNativeRate(omniChainId()) == ETH per OMNI
return amtOMNI * oracle.toNativeRate(omniChainId()) / oracle.CONVERSION_RATE_DENOM();
}

/// @notice Converts `amtETH` to OMNI, using the current conversion rate
function _toOmni(uint256 amtETH) internal view returns (uint256) {
FeeOracleV1 o = FeeOracleV1(omni.feeOracle());
return amtETH * o.toNativeRate(omniChainId()) / o.CONVERSION_RATE_DENOM();
// toNativeRate(omniChainId()) == ETH per OMNI
// to convert ETH to OMNI, we use 1 / toNativeRate(omniChainId())
return amtETH * oracle.CONVERSION_RATE_DENOM() / oracle.toNativeRate(omniChainId());
}

//////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -196,10 +206,15 @@ contract OmniGasPump is XAppUpgradeable, OwnableUpgradeable, PausableUpgradeable
}

/// @notice Set the address of the OmniGasStation, on Omni
function setOmniGasStation(address station) external onlyOwner {
function setGasStation(address station) external onlyOwner {
_setGasStation(station);
}

/// @notice Set the conversion rate oracle
function setOracle(address oracle_) external onlyOwner {
_setOracle(oracle_);
}

/// @notice Set the toll (as a percentage over PCT_DENOM)
function setToll(uint256 pct) external onlyOwner {
_setToll(pct);
Expand All @@ -222,4 +237,11 @@ contract OmniGasPump is XAppUpgradeable, OwnableUpgradeable, PausableUpgradeable
gasStation = station;
emit GasStationSet(station);
}

function _setOracle(address oracle_) internal {
require(oracle_ != address(0), "OmniGasPump: zero oracle");
oracle = IConversionRateOracle(oracle_);

emit OracleSet(oracle_);
}
}
Loading

0 comments on commit b41cf27

Please sign in to comment.