Skip to content

Commit

Permalink
tests wip
Browse files Browse the repository at this point in the history
  • Loading branch information
KuphJr committed May 18, 2024
1 parent 402afb7 commit 43ae009
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 38 deletions.
2 changes: 1 addition & 1 deletion contracts/src/v0.8/functions/dev/v1_X/FunctionsBilling.sol
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ abstract contract FunctionsBilling is Routable, IFunctionsBilling {

uint256 totalFeeWei = (gasPriceWei * executionGas) + l1FeeWei;
// Basis Points are 1/100th of 1%, divide by 10_000 to bring back to original units
uint256 totalFeeWeiWithOverestimate = totalFee + ((totalFee * s_config.fulfillmentGasPriceOverEstimationBP) / 10_000);
uint256 totalFeeWeiWithOverestimate = totalFeeWei + ((totalFeeWei * s_config.fulfillmentGasPriceOverEstimationBP) / 10_000);

uint96 estimatedGasReimbursementJuels = _getJuelsFromWei(totalFeeWeiWithOverestimate);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ library ChainSpecificUtil {
// ------------ Start Arbitrum Constants ------------

/// @dev ARB_L1_FEE_DATA_PADDING_SIZE is the L1 data padding for Optimism
uint256 private const ARB_L1_FEE_DATA_PADDING_SIZE = 140;
uint256 private constant ARB_L1_FEE_DATA_PADDING_SIZE = 140;
/// @dev ARBGAS_ADDR is the address of the ArbGasInfo precompile on Arbitrum.
/// @dev reference: https://github.com/OffchainLabs/nitro/blob/v2.0.14/contracts/src/precompiles/ArbGasInfo.sol#L10
address private constant ARBGAS_ADDR = address(0x000000000000000000000000000000000000006C);
Expand All @@ -24,7 +24,7 @@ library ChainSpecificUtil {

// ------------ Start Optimism Constants ------------
/// @dev OP_L1_FEE_DATA_PADDING_SIZE is the L1 data padding for Optimism
uint256 private const OP_L1_FEE_DATA_PADDING_SIZE = 35;
uint256 private constant OP_L1_FEE_DATA_PADDING_SIZE = 35;
/// @dev L1BLOCK_ADDR is the address of the L1Block precompile on Optimism.
address private constant L1BLOCK_ADDR = address(0x4200000000000000000000000000000000000015);
L1Block private constant L1BLOCK = L1Block(L1BLOCK_ADDR);
Expand All @@ -51,11 +51,11 @@ library ChainSpecificUtil {
} else if (_isOptimismChainId(chainid)) {
// https://docs.optimism.io/stack/transactions/fees#ecotone
// note we conservatively assume all non-zero bytes: tx_compressed_size = tx_data_size_bytes
uint256 l1BaseFeeWei = L1BLOCK.baseFee();
uint256 l1BaseFeeWei = L1BLOCK.basefee();
uint256 l1BaseFeeScalar = L1BLOCK.baseFeeScalar();
uint256 l1BlobBaseFeeWei = L1BLOCK.blobBaseFee();
uint256 l1BlobBaseFeeScalar = L1BLOCK.blobBaseFeeScalar();
uint256 weightedGasPrice = 16 * l1BaseFeeScalar * l1BaseFee + l1BlobBaseFeeScalar * l1BlobBaseFeeWei;
uint256 weightedGasPrice = 16 * l1BaseFeeScalar * l1BaseFeeWei + l1BlobBaseFeeScalar * l1BlobBaseFeeWei;
return weightedGasPrice * dataSizeBytes;
}
return 0;
Expand Down
103 changes: 74 additions & 29 deletions contracts/src/v0.8/functions/tests/v1_X/ChainSpecificUtil.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,29 @@ import {FunctionsResponse} from "../../dev/v1_X/libraries/FunctionsResponse.sol"
import {FunctionsFulfillmentSetup} from "./Setup.t.sol";

import {ArbGasInfo} from "../../../vendor/@arbitrum/nitro-contracts/src/precompiles/ArbGasInfo.sol";
import {OVM_GasPriceOracle} from "../../../vendor/@eth-optimism/contracts/v0.8.9/contracts/L2/predeploys/OVM_GasPriceOracle.sol";
import {L1Block} from "../../../vendor/@eth-optimism/contracts-bedrock/v0.17.1/src/L2/L1Block.sol";

/// @notice #_getCurrentTxL1GasFees Arbitrum
/// @notice #_getL1FeeUpperLimit Arbitrum
/// @dev Arbitrum gas formula = L2 Gas Price * (Gas used on L2 + Extra Buffer for L1 cost)
/// @dev where Extra Buffer for L1 cost = (L1 Estimated Cost / L2 Gas Price)
contract ChainSpecificUtil__getCurrentTxL1GasFees_Arbitrum is FunctionsFulfillmentSetup {
contract ChainSpecificUtil__getL1FeeUpperLimit_Arbitrum is FunctionsFulfillmentSetup {
address private constant ARBGAS_ADDR = address(0x000000000000000000000000000000000000006C);
uint256 private constant L1_FEE_WEI = 15_818_209_764_247;

uint96 l1FeeJuels = uint96((1e18 * L1_FEE_WEI) / uint256(LINK_ETH_RATE));

function setUp() public virtual override {
vm.mockCall(ARBGAS_ADDR, abi.encodeWithSelector(ArbGasInfo.getCurrentTxL1GasFees.selector), abi.encode(L1_FEE_WEI));
uint256 unused = 0;
uint256 gasPerL1CalldataByte = 4;
vm.mockCall(
ARBGAS_ADDR,
abi.encodeWithSelector(
ArbGasInfo.getPricesInWei.selector),
abi.encode(unused, gasPerL1CalldataByte, unused, unused, unused, unused)
);
}

function test__getCurrentTxL1GasFees_SuccessWhenArbitrumMainnet() public {
function test__getL1FeeUpperLimit_SuccessWhenArbitrumMainnet() public {
// Set the chainID
vm.chainId(42161);

Expand All @@ -43,7 +50,7 @@ contract ChainSpecificUtil__getCurrentTxL1GasFees_Arbitrum is FunctionsFulfillme
assertEq(s_responses[1].totalCostJuels, expectedTotalCostJuels);
}

function test__getCurrentTxL1GasFees_SuccessWhenArbitrumGoerli() public {
function test__getL1FeeUpperLimit_SuccessWhenArbitrumGoerli() public {
// Set the chainID
vm.chainId(421613);

Expand All @@ -60,7 +67,7 @@ contract ChainSpecificUtil__getCurrentTxL1GasFees_Arbitrum is FunctionsFulfillme
assertEq(s_responses[1].totalCostJuels, expectedTotalCostJuels);
}

function test__getCurrentTxL1GasFees_SuccessWhenArbitrumSepolia() public {
function test__getL1FeeUpperLimit_SuccessWhenArbitrumSepolia() public {
// Set the chainID
vm.chainId(421614);

Expand All @@ -78,24 +85,43 @@ contract ChainSpecificUtil__getCurrentTxL1GasFees_Arbitrum is FunctionsFulfillme
}
}

/// @notice #_getCurrentTxL1GasFees Optimism
/// @notice #_getL1FeeUpperLimit Optimism
/// @dev Optimism gas formula = ((l2_base_fee + l2_priority_fee) * l2_gas_used) + L1 data fee
/// @dev where L1 data fee = l1_gas_price * ((count_zero_bytes(tx_data) * 4 + count_non_zero_bytes(tx_data) * 16) + fixed_overhead + noncalldata_gas) * dynamic_overhead
contract ChainSpecificUtil__getCurrentTxL1GasFees_Optimism is FunctionsFulfillmentSetup {
address private constant OVM_GASPRICEORACLE_ADDR = address(0x420000000000000000000000000000000000000F);
uint256 private constant L1_FEE_WEI = 15_818_209_764_247;
contract ChainSpecificUtil__getL1FeeUpperLimit_Optimism is FunctionsFulfillmentSetup {
address private constant L1BLOCK_ADDR = address(0x4200000000000000000000000000000000000015);
L1Block private constant L1BLOCK = L1Block(L1BLOCK_ADDR);
uint256 private constant L1_BASE_FEE_WEI = 15_818_209;
uint256 private constant L1_BASE_FEE_SCALAR = 2;
uint256 private constant L1_BLOB_BASE_FEE_WEI = 15_818_209;
uint256 private constant L1_BLOB_BASE_FEE_SCALAR = 2;

uint96 l1FeeJuels = uint96((1e18 * L1_FEE_WEI) / uint256(LINK_ETH_RATE));
uint96 l1FeeJuels = uint96((1e18 * L1_BASE_FEE_WEI) / uint256(LINK_ETH_RATE));

function setUp() public virtual override {
vm.mockCall(
OVM_GASPRICEORACLE_ADDR,
abi.encodeWithSelector(OVM_GasPriceOracle.getL1Fee.selector),
abi.encode(L1_FEE_WEI)
L1BLOCK_ADDR,
abi.encodeWithSelector(L1BLOCK.basefee.selector),
abi.encode(L1_BASE_FEE_WEI)
);
vm.mockCall(
L1BLOCK_ADDR,
abi.encodeWithSelector(L1BLOCK.baseFeeScalar.selector),
abi.encode(L1_BASE_FEE_SCALAR)
);
vm.mockCall(
L1BLOCK_ADDR,
abi.encodeWithSelector(L1BLOCK.blobBaseFee.selector),
abi.encode(L1_BLOB_BASE_FEE_WEI)
);
vm.mockCall(
L1BLOCK_ADDR,
abi.encodeWithSelector(L1BLOCK.blobBaseFeeScalar.selector),
abi.encode(L1_BLOB_BASE_FEE_SCALAR)
);
}

function test__getCurrentTxL1GasFees_SuccessWhenOptimismMainnet() public {
function test__getL1FeeUpperLimit_SuccessWhenOptimismMainnet() public {
// Set the chainID
vm.chainId(10);

Expand All @@ -112,7 +138,7 @@ contract ChainSpecificUtil__getCurrentTxL1GasFees_Optimism is FunctionsFulfillme
assertEq(s_responses[1].totalCostJuels, expectedTotalCostJuels);
}

function test__getCurrentTxL1GasFees_SuccessWhenOptimismGoerli() public {
function test__getL1FeeUpperLimit_SuccessWhenOptimismGoerli() public {
// Set the chainID
vm.chainId(420);

Expand All @@ -129,7 +155,7 @@ contract ChainSpecificUtil__getCurrentTxL1GasFees_Optimism is FunctionsFulfillme
assertEq(s_responses[1].totalCostJuels, expectedTotalCostJuels);
}

function test__getCurrentTxL1GasFees_SuccessWhenOptimismSepolia() public {
function test__getL1FeeUpperLimit_SuccessWhenOptimismSepolia() public {
// Set the chainID
vm.chainId(11155420);

Expand All @@ -147,24 +173,43 @@ contract ChainSpecificUtil__getCurrentTxL1GasFees_Optimism is FunctionsFulfillme
}
}

/// @notice #_getCurrentTxL1GasFees Base
/// @notice #_getL1FeeUpperLimit Base
/// @dev Base gas formula uses Optimism formula = ((l2_base_fee + l2_priority_fee) * l2_gas_used) + L1 data fee
/// @dev where L1 data fee = l1_gas_price * ((count_zero_bytes(tx_data) * 4 + count_non_zero_bytes(tx_data) * 16) + fixed_overhead + noncalldata_gas) * dynamic_overhead
contract ChainSpecificUtil__getCurrentTxL1GasFees_Base is FunctionsFulfillmentSetup {
address private constant OVM_GASPRICEORACLE_ADDR = address(0x420000000000000000000000000000000000000F);
uint256 private constant L1_FEE_WEI = 15_818_209_764_247;
contract ChainSpecificUtil__getL1FeeUpperLimit_Base is FunctionsFulfillmentSetup {
address private constant L1BLOCK_ADDR = address(0x4200000000000000000000000000000000000015);
L1Block private constant L1BLOCK = L1Block(L1BLOCK_ADDR);
uint256 private constant L1_BASE_FEE_WEI = 15_818_209;
uint256 private constant L1_BASE_FEE_SCALAR = 2;
uint256 private constant L1_BLOB_BASE_FEE_WEI = 15_818_209;
uint256 private constant L1_BLOB_BASE_FEE_SCALAR = 2;

uint96 l1FeeJuels = uint96((1e18 * L1_FEE_WEI) / uint256(LINK_ETH_RATE));
uint96 l1FeeJuels = uint96((1e18 * L1_BASE_FEE_WEI) / uint256(LINK_ETH_RATE));

function setUp() public virtual override {
vm.mockCall(
OVM_GASPRICEORACLE_ADDR,
abi.encodeWithSelector(OVM_GasPriceOracle.getL1Fee.selector),
abi.encode(L1_FEE_WEI)
L1BLOCK_ADDR,
abi.encodeWithSelector(L1BLOCK.basefee.selector),
abi.encode(L1_BASE_FEE_WEI)
);
vm.mockCall(
L1BLOCK_ADDR,
abi.encodeWithSelector(L1BLOCK.baseFeeScalar.selector),
abi.encode(L1_BASE_FEE_SCALAR)
);
vm.mockCall(
L1BLOCK_ADDR,
abi.encodeWithSelector(L1BLOCK.blobBaseFee.selector),
abi.encode(L1_BLOB_BASE_FEE_WEI)
);
vm.mockCall(
L1BLOCK_ADDR,
abi.encodeWithSelector(L1BLOCK.blobBaseFeeScalar.selector),
abi.encode(L1_BLOB_BASE_FEE_SCALAR)
);
}

function test__getCurrentTxL1GasFees_SuccessWhenBaseMainnet() public {
function test__getL1FeeUpperLimit_SuccessWhenBaseMainnet() public {
// Set the chainID
vm.chainId(8453);

Expand All @@ -181,7 +226,7 @@ contract ChainSpecificUtil__getCurrentTxL1GasFees_Base is FunctionsFulfillmentSe
assertEq(s_responses[1].totalCostJuels, expectedTotalCostJuels);
}

function test__getCurrentTxL1GasFees_SuccessWhenBaseGoerli() public {
function test__getL1FeeUpperLimit_SuccessWhenBaseGoerli() public {
// Set the chainID
vm.chainId(84531);

Expand All @@ -198,7 +243,7 @@ contract ChainSpecificUtil__getCurrentTxL1GasFees_Base is FunctionsFulfillmentSe
assertEq(s_responses[1].totalCostJuels, expectedTotalCostJuels);
}

function test__getCurrentTxL1GasFees_SuccessWhenBaseSepolia() public {
function test__getL1FeeUpperLimit_SuccessWhenBaseSepolia() public {
// Set the chainID
vm.chainId(84532);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ contract FunctionsBilling_UpdateConfig is FunctionsRouterSetup {
fallbackNativePerUnitLink: getCoordinatorConfig().fallbackNativePerUnitLink * 2,
fallbackUsdPerUnitLink: getCoordinatorConfig().fallbackUsdPerUnitLink * 2,
fallbackUsdPerUnitLinkDecimals: getCoordinatorConfig().fallbackUsdPerUnitLinkDecimals * 2,
minimumEstimateGasPriceWei: getCoordinatorConfig().minimumEstimateGasPriceWei * 2
minimumEstimateGasPriceWei: getCoordinatorConfig().minimumEstimateGasPriceWei * 2,
transmitTxSizeBytes: getCoordinatorConfig().transmitTxSizeBytes * 2
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {FunctionsRouterSetup, FunctionsDONSetup, FunctionsSubscriptionSetup} fro
/// @notice #constructor
contract FunctionsCoordinator_Constructor is FunctionsRouterSetup {
function test_Constructor_Success() public {
assertEq(s_functionsCoordinator.typeAndVersion(), "Functions Coordinator v1.3.0");
assertEq(s_functionsCoordinator.typeAndVersion(), "Functions Coordinator v1.3.1");
assertEq(s_functionsCoordinator.owner(), OWNER_ADDRESS);
}
}
Expand Down
3 changes: 2 additions & 1 deletion contracts/src/v0.8/functions/tests/v1_X/Setup.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ contract FunctionsRouterSetup is BaseTest {
fallbackNativePerUnitLink: 5000000000000000,
fallbackUsdPerUnitLink: 1400000000,
fallbackUsdPerUnitLinkDecimals: 8,
minimumEstimateGasPriceWei: 1000000000 // 1 gwei
minimumEstimateGasPriceWei: 1000000000, // 1 gwei
transmitTxSizeBytes: 5000
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.15;
pragma solidity ^0.8.15;

import { ISemver } from "../universal/ISemver.sol";

Expand Down

0 comments on commit 43ae009

Please sign in to comment.