Skip to content

Commit

Permalink
refactor _getTokenAndGasPrices
Browse files Browse the repository at this point in the history
  • Loading branch information
RyanRHall committed Oct 8, 2024
1 parent 251be4f commit 2c38141
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
19 changes: 9 additions & 10 deletions contracts/src/v0.8/ccip/FeeQuoter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ contract FeeQuoter is AuthorizedCallers, IFeeQuoter, ITypeAndVersion, IReceiver,
error DataFeedValueOutOfUint224Range();
error InvalidDestBytesOverhead(address token, uint32 destBytesOverhead);
error MessageGasLimitTooHigh();
error DestinationChainNotEnabled(uint64 destChainSelector);
error ExtraArgOutOfOrderExecutionMustBeTrue();
error InvalidExtraArgsTag();
error SourceTokenDataTooLarge(address token);
Expand Down Expand Up @@ -308,7 +307,9 @@ contract FeeQuoter is AuthorizedCallers, IFeeQuoter, ITypeAndVersion, IReceiver,
address token,
uint64 destChainSelector
) external view returns (uint224 tokenPrice, uint224 gasPriceValue) {
return _getTokenAndGasPrices(token, destChainSelector, s_destChainConfigs[destChainSelector]);
if (!s_destChainConfigs[destChainSelector].isEnabled) revert ChainNotSupported(destChainSelector);
return
_getTokenAndGasPrices(token, destChainSelector, s_destChainConfigs[destChainSelector].gasPriceStalenessThreshold);
}

/// @notice Convert a given token amount to target token amount.
Expand Down Expand Up @@ -373,22 +374,20 @@ contract FeeQuoter is AuthorizedCallers, IFeeQuoter, ITypeAndVersion, IReceiver,
/// @dev Gets the fee token price and the gas price, both denominated in dollars.
/// @param token The source token to get the price for.
/// @param destChainSelector The destination chain to get the gas price for.
/// @param destChainConfig The config struct for the dest chain selector.
/// @return tokenPrice The price of the feeToken in 1e18 dollars per base unit.
/// @return gasPriceValue The price of gas in 1e18 dollars per base unit.
function _getTokenAndGasPrices(
address token,
uint64 destChainSelector,
DestChainConfig memory destChainConfig
uint32 gasPriceStalenessThreshold
) private view returns (uint224 tokenPrice, uint224 gasPriceValue) {
if (!destChainConfig.isEnabled) revert ChainNotSupported(destChainSelector);
Internal.TimestampedPackedUint224 memory gasPrice = s_usdPerUnitGasByDestChainSelector[destChainSelector];
// If the staleness threshold is 0, we consider the gas price to be always valid
if (destChainConfig.gasPriceStalenessThreshold != 0) {
if (gasPriceStalenessThreshold != 0) {
// We do allow a gas price of 0, but no stale or unset gas prices
uint256 timePassed = block.timestamp - gasPrice.timestamp;
if (timePassed > destChainConfig.gasPriceStalenessThreshold) {
revert StaleGasPrice(destChainSelector, destChainConfig.gasPriceStalenessThreshold, timePassed);
if (timePassed > gasPriceStalenessThreshold) {
revert StaleGasPrice(destChainSelector, gasPriceStalenessThreshold, timePassed);
}
}

Expand Down Expand Up @@ -521,15 +520,15 @@ contract FeeQuoter is AuthorizedCallers, IFeeQuoter, ITypeAndVersion, IReceiver,
Client.EVM2AnyMessage calldata message
) external view returns (uint256 feeTokenAmount) {
DestChainConfig memory destChainConfig = s_destChainConfigs[destChainSelector];
if (!destChainConfig.isEnabled) revert DestinationChainNotEnabled(destChainSelector);
if (!destChainConfig.isEnabled) revert ChainNotSupported(destChainSelector);
if (!s_feeTokens.contains(message.feeToken)) revert FeeTokenNotSupported(message.feeToken);

uint256 numberOfTokens = message.tokenAmounts.length;
_validateMessage(destChainConfig, message.data.length, numberOfTokens, message.receiver);

// The below call asserts that feeToken is a supported token
(uint224 feeTokenPrice, uint224 packedGasPrice) =
_getTokenAndGasPrices(message.feeToken, destChainSelector, destChainConfig);
_getTokenAndGasPrices(message.feeToken, destChainSelector, destChainConfig.gasPriceStalenessThreshold);

// Calculate premiumFee in USD with 18 decimals precision first.
// If message-only and no token transfers, a flat network fee is charged.
Expand Down
4 changes: 2 additions & 2 deletions contracts/src/v0.8/ccip/test/feeQuoter/FeeQuoter.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -1658,8 +1658,8 @@ contract FeeQuoter_getValidatedFee is FeeQuoterFeeSetup {

// Reverts

function test_DestinationChainNotEnabled_Revert() public {
vm.expectRevert(abi.encodeWithSelector(FeeQuoter.DestinationChainNotEnabled.selector, DEST_CHAIN_SELECTOR + 1));
function test_ChainNotSupported_Revert() public {
vm.expectRevert(abi.encodeWithSelector(FeeQuoter.ChainNotSupported.selector, DEST_CHAIN_SELECTOR + 1));
s_feeQuoter.getValidatedFee(DEST_CHAIN_SELECTOR + 1, _generateEmptyMessage());
}

Expand Down

0 comments on commit 2c38141

Please sign in to comment.