GasDataLib

Git Source

Library for encoding and decoding GasData and ChainGas structs.

GasData

GasData is a struct to store the "basic information about gas prices", that could be later used to approximate the cost of a message execution, and thus derive the minimal tip values for sending a message to the chain.

  • GasData is supposed to be cached by GasOracle contract, allowing to store the approximates instead of the exact values, and thus save on storage costs.
  • For instance, if GasOracle only updates the values on +- 10% change, having an 0.4% error on the approximates would be acceptable. GasData is supposed to be included in the Origin's state, which are synced across chains using Agent-signed snapshots and attestations.

GasData stack layout (from highest bits to lowest)

PositionFieldTypeBytesDescription
(012..010]gasPriceuint162Gas price for the chain (in Wei per gas unit)
(010..008]dataPriceuint162Calldata price (in Wei per byte of content)
(008..006]execBufferuint162Tx fee safety buffer for message execution (in Wei)
(006..004]amortAttCostuint162Amortized cost for attestation submission (in Wei)
(004..002]etherPriceuint162Chain's Ether Price / Mainnet Ether Price (in BWAD)
(002..000]markupuint162Markup for the message execution (in BWAD)

See Number.sol for more details on Number type and BWAD (binary WAD) math.

ChainGas stack layout (from highest bits to lowest)

PositionFieldTypeBytesDescription
(016..004]gasDatauint9612Chain's gas data
(004..000]domainuint324Chain's domain

State Variables

SHIFT_GAS_PRICE

Amount of bits to shift to gasPrice field

uint96 private constant SHIFT_GAS_PRICE = 10 * 8;

SHIFT_DATA_PRICE

Amount of bits to shift to dataPrice field

uint96 private constant SHIFT_DATA_PRICE = 8 * 8;

SHIFT_EXEC_BUFFER

Amount of bits to shift to execBuffer field

uint96 private constant SHIFT_EXEC_BUFFER = 6 * 8;

SHIFT_AMORT_ATT_COST

Amount of bits to shift to amortAttCost field

uint96 private constant SHIFT_AMORT_ATT_COST = 4 * 8;

SHIFT_ETHER_PRICE

Amount of bits to shift to etherPrice field

uint96 private constant SHIFT_ETHER_PRICE = 2 * 8;

SHIFT_GAS_DATA

Amount of bits to shift to gasData field

uint128 private constant SHIFT_GAS_DATA = 4 * 8;

Functions

encodeGasData

Returns an encoded GasData struct with the given fields.

function encodeGasData(
    Number gasPrice_,
    Number dataPrice_,
    Number execBuffer_,
    Number amortAttCost_,
    Number etherPrice_,
    Number markup_
) internal pure returns (GasData);

Parameters

NameTypeDescription
gasPrice_NumberGas price for the chain (in Wei per gas unit)
dataPrice_NumberCalldata price (in Wei per byte of content)
execBuffer_NumberTx fee safety buffer for message execution (in Wei)
amortAttCost_NumberAmortized cost for attestation submission (in Wei)
etherPrice_NumberRatio of Chain's Ether Price / Mainnet Ether Price (in BWAD)
markup_NumberMarkup for the message execution (in BWAD)

wrapGasData

Wraps padded uint256 value into GasData struct.

function wrapGasData(uint256 paddedGasData) internal pure returns (GasData);

gasPrice

Returns the gas price, in Wei per gas unit.

function gasPrice(GasData data) internal pure returns (Number);

dataPrice

Returns the calldata price, in Wei per byte of content.

function dataPrice(GasData data) internal pure returns (Number);

execBuffer

Returns the tx fee safety buffer for message execution, in Wei.

function execBuffer(GasData data) internal pure returns (Number);

amortAttCost

Returns the amortized cost for attestation submission, in Wei.

function amortAttCost(GasData data) internal pure returns (Number);

etherPrice

Returns the ratio of Chain's Ether Price / Mainnet Ether Price, in BWAD math.

function etherPrice(GasData data) internal pure returns (Number);

markup

Returns the markup for the message execution, in BWAD math.

function markup(GasData data) internal pure returns (Number);

encodeChainGas

Returns an encoded ChainGas struct with the given fields.

function encodeChainGas(GasData gasData_, uint32 domain_) internal pure returns (ChainGas);

Parameters

NameTypeDescription
gasData_GasDataChain's gas data
domain_uint32Chain's domain

wrapChainGas

Wraps padded uint256 value into ChainGas struct.

function wrapChainGas(uint256 paddedChainGas) internal pure returns (ChainGas);

gasData

Returns the chain's gas data.

function gasData(ChainGas data) internal pure returns (GasData);

domain

Returns the chain's domain.

function domain(ChainGas data) internal pure returns (uint32);

snapGasHash

Returns the hash for the list of ChainGas structs.

function snapGasHash(ChainGas[] memory snapGas) internal pure returns (bytes32 snapGasHash_);