Skip to content

Decubate-com/evm-fees

Repository files navigation

Decubate Fee Converter

An upgradeable Solidity contract for converting USD fees to native token (ETH/BNB/etc.) amounts using Chainlink price feeds.

Overview

The DecubateFeeConverter contract provides a secure and reliable way to convert USD-denominated fees into their equivalent value in the blockchain's native currency. It uses Chainlink oracles for accurate, real-time price data and includes safety mechanisms to prevent the use of stale or invalid pricing information.

Features

  • Chainlink Oracle Integration: Fetches real-time price data from Chainlink aggregators
  • UUPS Upgradeable: Implements the UUPS (Universal Upgradeable Proxy Standard) pattern for contract upgradeability
  • Safety Checks: Multiple safeguards against stale prices, invalid data, and oracle failures
  • Owner-Controlled: Price feed can be updated by the contract owner
  • Gas Optimized: Efficient calculation logic with minimal gas consumption

Contract Architecture

Key Components

  • Price Feed: Chainlink AggregatorV3Interface for fetching ETH/USD (or other native token/USD) prices
  • Ownership: Inherits from OpenZeppelin's OwnableUpgradeable
  • Upgradeability: Implements UUPSUpgradeable for safe contract upgrades
  • Storage Gap: Reserved slots for future upgrades without storage collisions

Main Functions

convertUSDFeeToWei(uint256 minFeeUSD)

Converts a USD fee amount to its equivalent in wei.

Parameters:

  • minFeeUSD: The fee amount in USD (scaled appropriately)

Returns:

  • uint256: The equivalent amount in wei, or 0 if conversion fails

Safety Mechanisms:

  • Returns 0 if price feed is not set
  • Returns 0 if USD fee is 0
  • Returns 0 if oracle price is ≤ 0
  • Returns 0 if price data is more than 24 hours old
  • Returns 0 if updatedAt timestamp is in the future (reorg protection)
  • Gracefully handles oracle call failures

setPriceFeed(address newPriceFeed)

Updates the Chainlink price feed address (owner only).

Parameters:

  • newPriceFeed: Address of the new Chainlink aggregator

Emits:

  • SetPriceFeed event with previous and new price feed addresses

Installation

npm install

Prerequisites

  • Node.js >= 24
  • Hardhat v3
  • OpenZeppelin Contracts Upgradeable
  • Chainlink Contracts

Dependencies

{
  "dependencies": {
    "@openzeppelin/contracts-upgradeable": "^5.0.0",
    "@chainlink/contracts": "^1.0.0"
  },
  "devDependencies": {
    "hardhat": "^3.0.0",
    "@nomicfoundation/hardhat-toolbox": "^5.0.0"
  }
}

Deployment

  1. Deploy:
npx hardhat run ./scripts/deployFeeConverter.ts --network <network>
  1. Verify:
npx hardhat ignition verify <deployment-id> --network <network>

Chainlink Price Feed Addresses

Mainnet

  • Ethereum: 0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419 (ETH/USD)
  • Arbitrum: 0x639Fe6ab55C921f74e7fac1ee960C0B6293ba612 (ETH/USD)
  • Avalanche: 0x0A77230d17318075983913bC2145DB16C7366156 (AVAX/USD)
  • Base: 0x71041dddad3595F9CEd3DcCFBe3D1F4b0a16Bb70 (ETH/USD)
  • BSC: 0x0567F2323251f0Aab15c8dFb1967E4e8A7D42aeE (BNB/USD)
  • Linea: 0x3c6Cd9Cc7c7a4c2Cf5a82734CD249D7D593354dA (ETH/USD)
  • Polygon: 0xAB594600376Ec9fD91F8e885dADF0CE036862dE0 (MATIC/USD)

Testnet

  • BSC Testnet: 0x2514895c72f50D8bd4B4F9b1110F0D6bD2c97526 (BNB/USD)

See Chainlink Data Feeds for more addresses.

Testing

npx hardhat test

Security Considerations

  1. Oracle Dependency: Contract relies on Chainlink oracles; ensure price feeds are actively maintained
  2. 24-Hour Staleness Check: Prices older than 24 hours are rejected
  3. Upgrade Authorization: Only the owner can authorize upgrades
  4. Zero Address Checks: Price feed can be set to zero address to disable conversions
  5. Reorg Protection: Future timestamps are rejected to prevent reorg-based attacks

Gas Optimization

  • Uses unchecked blocks where overflow is impossible
  • Minimal storage reads through caching
  • Early returns to avoid unnecessary computations

License

GPL-3.0-or-later

About

Blockchain project containing smart-contracts which interact with oracles to convert the fees

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors