Skip to content
Verified contractExactMIT

ForgAIEvmAmmFeeCollector

Address0xdd6a376c1ddd18f710ac5d8e503acb03ea53357d
Balance

0 ETH

 

Transactions

0

 

Compiler

solc 0.8.36

 

Same code

Only this contract

 

Details

Compiler
solc 0.8.36+commit.8a079791
Language
solidity
Optimization
Yes, 200 runs
EVM version
cancun
Via IR
yes
License
MIT
Match
Exactcreation code: yes, runtime code: yes
Verified on Sourcify
Created in block
26,409,609
Creation transaction
0xa10f31b7dc…368f172370

contracts/multichain/ForgAIEvmAmmFeeCollector.sol

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
import {ILayerZeroEndpointV2,MessagingParams,MessagingFee,MessagingReceipt,Origin} from "@layerzerolabs/lz-evm-protocol-v2/contracts/interfaces/ILayerZeroEndpointV2.sol";
import {ILayerZeroReceiver} from "@layerzerolabs/lz-evm-protocol-v2/contracts/interfaces/ILayerZeroReceiver.sol";
import {ReentrancyGuard} from "@openzeppelin/contracts/utils/ReentrancyGuard.sol";

/// @notice Native protocol fees end as BNB at the immutable BSC beneficiary.
/// LayerZero Executor supplies the requested BNB as lzReceive msg.value, paid
/// for by the source-chain native fee quote. This is capped native-value
/// delivery, not an unrestricted asset bridge. Every lane must be admitted.
/// Deploy this identical init code at the SAME address on all four chains.
contract ForgAIEvmAmmFeeCollector is ILayerZeroReceiver,ReentrancyGuard {
    struct Config {
        address beneficiary;address operator;address delegate;uint128 receiveGas;
        address[4] endpoints;uint32[4] eids;
        uint128[4] batchFeeCaps;uint128[4] dailyFeeCaps;
        uint128 minimumBnb;uint128 maximumBnb;
    }
    struct Batch {uint256 source;uint64 nonce;uint128 bnbAmount;bytes32 id;}
    ILayerZeroEndpointV2 public immutable endpoint;
    address public immutable beneficiary;
    address public immutable operator;
    address public immutable configurationDelegate;
    uint128 public immutable receiveGas;
    uint32 public immutable bscEid;
    uint128 public immutable batchFeeCap;
    uint128 public immutable dailyFeeCap;
    uint128 public immutable minimumBnb;
    uint128 public immutable maximumBnb;
    bytes32 public immutable configurationHash;
    mapping(uint32=>uint256) public sourceChains;
    mapping(bytes32=>bytes32) public dispatched;
    mapping(bytes32=>bool) public paid;
    uint64 public nonce;
    uint256 public spendingDay;
    uint256 public spentToday;
    uint256 public totalSourceSpent;
    uint256 public totalBscPaid;
    event BatchDispatched(bytes32 indexed batchId,bytes32 indexed guid,uint256 nativeFee,uint128 requestedBnb);
    event BatchPaid(bytes32 indexed batchId,bytes32 indexed guid,uint256 indexed source,address beneficiary,uint128 bnbAmount);
    event BscFeesPaid(address indexed beneficiary,uint256 amount);
    constructor(Config memory c) {
        require(c.beneficiary!=address(0)&&c.operator!=address(0)&&c.delegate!=address(0)&&c.receiveGas>=100000,"COLLECTOR_CONFIG");
        require(c.minimumBnb>0&&c.maximumBnb>=c.minimumBnb,"BNB_BOUNDS");
        uint256[4] memory chains=[uint256(1),56,4663,8453];uint256 local=4;
        for(uint256 i;i<4;i++){
            require(c.endpoints[i]!=address(0)&&c.eids[i]>0&&c.batchFeeCaps[i]>0&&c.dailyFeeCaps[i]>=c.batchFeeCaps[i],"CHAIN_CONFIG");
            require(sourceChains[c.eids[i]]==0,"DUPLICATE_EID");sourceChains[c.eids[i]]=chains[i];
            if(chains[i]==block.chainid)local=i;
        }
        require(local<4&&c.endpoints[local].code.length>0,"LOCAL_CHAIN");
        endpoint=ILayerZeroEndpointV2(c.endpoints[local]);
        require(endpoint.eid()==c.eids[local]&&endpoint.nativeToken()==address(0),"ENDPOINT_IDENTITY");
        beneficiary=c.beneficiary;operator=c.operator;configurationDelegate=c.delegate;receiveGas=c.receiveGas;
        bscEid=c.eids[1];batchFeeCap=c.batchFeeCaps[local];dailyFeeCap=c.dailyFeeCaps[local];
        minimumBnb=c.minimumBnb;maximumBnb=c.maximumBnb;configurationHash=keccak256(abi.encode(c));
        endpoint.setDelegate(c.delegate);
    }
    // Claims from protocol fee vaults accumulate without a recipient callback.
    receive() external payable {}
    function batchId(uint256 source,uint64 n,uint128 amount) public view returns(bytes32){return keccak256(abi.encode(configurationHash,address(this),source,n,amount));}
    function _params(uint128 amount) private view returns(MessagingParams memory p){
        require(block.chainid!=56&&amount>=minimumBnb&&amount<=maximumBnb,"BNB_BOUNDS");
        Batch memory b=Batch(block.chainid,nonce+1,amount,batchId(block.chainid,nonce+1,amount));
        // Type 3 / executor 1 / lzReceive 1 / uint128 gas + uint128 msg.value.
        bytes memory options=abi.encodePacked(uint16(3),uint8(1),uint16(33),uint8(1),receiveGas,amount);
        p=MessagingParams(bscEid,bytes32(uint256(uint160(address(this)))),abi.encode(b),options,false);
    }
    function quoteBatch(uint128 amount) external view returns(MessagingFee memory){return endpoint.quote(_params(amount),address(this));}
    function dispatch(uint128 amount,uint256 maxNativeFee,uint256 deadline) external nonReentrant returns(bytes32 guid){
        require(msg.sender==operator,"OPERATOR_ONLY");
        require(block.timestamp<=deadline&&deadline<=block.timestamp+300,"EXPIRED");
        MessagingParams memory p=_params(amount);MessagingFee memory fee=endpoint.quote(p,address(this));
        uint256 day=block.timestamp/1 days;if(day!=spendingDay){spendingDay=day;spentToday=0;}
        require(fee.nativeFee>0&&fee.lzTokenFee==0&&fee.nativeFee<=maxNativeFee&&fee.nativeFee<=batchFeeCap&&spentToday+fee.nativeFee<=dailyFeeCap,"FEE_BUDGET");
        require(address(this).balance>=fee.nativeFee,"FEES_NOT_READY");
        ++nonce;bytes32 b=batchId(block.chainid,nonce,amount);spentToday+=fee.nativeFee;totalSourceSpent+=fee.nativeFee;
        MessagingReceipt memory receipt=endpoint.send{value:fee.nativeFee}(p,address(this));
        require(receipt.guid!=bytes32(0)&&receipt.nonce>0&&receipt.fee.nativeFee==fee.nativeFee&&receipt.fee.lzTokenFee==0,"SEND_RECEIPT");
        guid=receipt.guid;dispatched[b]=guid;emit BatchDispatched(b,guid,fee.nativeFee,amount);
    }
    function allowInitializePath(Origin calldata origin) external view returns(bool){return block.chainid==56&&sourceChains[origin.srcEid]!=0&&sourceChains[origin.srcEid]!=56&&origin.sender==bytes32(uint256(uint160(address(this))));}
    function nextNonce(uint32,bytes32) external pure returns(uint64){return 0;}
    function lzReceive(Origin calldata origin,bytes32 guid,bytes calldata message,address,bytes calldata) external payable nonReentrant {
        require(msg.sender==address(endpoint)&&block.chainid==56,"BSC_ENDPOINT_ONLY");
        uint256 source=sourceChains[origin.srcEid];
        require(source!=0&&source!=56&&origin.sender==bytes32(uint256(uint160(address(this))))&&origin.nonce>0&&guid!=bytes32(0),"PEER");
        require(message.length==128,"MESSAGE_LENGTH");Batch memory b=abi.decode(message,(Batch));
        require(b.source==source&&b.nonce>0&&b.bnbAmount>=minimumBnb&&b.bnbAmount<=maximumBnb&&msg.value==b.bnbAmount,"BATCH_VALUE");
        require(b.id==batchId(source,b.nonce,b.bnbAmount)&&!paid[b.id],"BATCH_REPLAY_OR_ID");
        paid[b.id]=true;totalBscPaid+=msg.value;
        (bool ok,)=payable(beneficiary).call{value:msg.value}("");require(ok,"BSC_PAYOUT_FAILED");
        emit BatchPaid(b.id,guid,source,beneficiary,b.bnbAmount);
    }
    function flushBsc() external nonReentrant {
        require(block.chainid==56,"BSC_ONLY");uint256 amount=address(this).balance;require(amount>0,"NO_FEES");
        totalBscPaid+=amount;(bool ok,)=payable(beneficiary).call{value:amount}("");require(ok,"BSC_PAYOUT_FAILED");emit BscFeesPaid(beneficiary,amount);
    }
}

@layerzerolabs/lz-evm-protocol-v2/contracts/interfaces/ILayerZeroEndpointV2.sol

// SPDX-License-Identifier: MIT

pragma solidity >=0.8.0;

import { IMessageLibManager } from "./IMessageLibManager.sol";
import { IMessagingComposer } from "./IMessagingComposer.sol";
import { IMessagingChannel } from "./IMessagingChannel.sol";
import { IMessagingContext } from "./IMessagingContext.sol";

struct MessagingParams {
    uint32 dstEid;
    bytes32 receiver;
    bytes message;
    bytes options;
    bool payInLzToken;
}

struct MessagingReceipt {
    bytes32 guid;
    uint64 nonce;
    MessagingFee fee;
}

struct MessagingFee {
    uint256 nativeFee;
    uint256 lzTokenFee;
}

struct Origin {
    uint32 srcEid;
    bytes32 sender;
    uint64 nonce;
}

interface ILayerZeroEndpointV2 is IMessageLibManager, IMessagingComposer, IMessagingChannel, IMessagingContext {
    event PacketSent(bytes encodedPayload, bytes options, address sendLibrary);

    event PacketVerified(Origin origin, address receiver, bytes32 payloadHash);

    event PacketDelivered(Origin origin, address receiver);

    event LzReceiveAlert(
        address indexed receiver,
        address indexed executor,
        Origin origin,
        bytes32 guid,
        uint256 gas,
        uint256 value,
        bytes message,
        bytes extraData,
        bytes reason
    );

    event LzTokenSet(address token);

    event DelegateSet(address sender, address delegate);

    function quote(MessagingParams calldata _params, address _sender) external view returns (MessagingFee memory);

    function send(
        MessagingParams calldata _params,
        address _refundAddress
    ) external payable returns (MessagingReceipt memory);

    function verify(Origin calldata _origin, address _receiver, bytes32 _payloadHash) external;

    function verifiable(Origin calldata _origin, address _receiver) external view returns (bool);

    function initializable(Origin calldata _origin, address _receiver) external view returns (bool);

    function lzReceive(
        Origin calldata _origin,
        address _receiver,
        bytes32 _guid,
        bytes calldata _message,
        bytes calldata _extraData
    ) external payable;

    // oapp can burn messages partially by calling this function with its own business logic if messages are verified in order
    function clear(address _oapp, Origin calldata _origin, bytes32 _guid, bytes calldata _message) external;

    function setLzToken(address _lzToken) external;

    function lzToken() external view returns (address);

    function nativeToken() external view returns (address);

    function setDelegate(address _delegate) external;
}

@layerzerolabs/lz-evm-protocol-v2/contracts/interfaces/ILayerZeroReceiver.sol

// SPDX-License-Identifier: MIT

pragma solidity >=0.8.0;

import { Origin } from "./ILayerZeroEndpointV2.sol";

interface ILayerZeroReceiver {
    function allowInitializePath(Origin calldata _origin) external view returns (bool);

    function nextNonce(uint32 _eid, bytes32 _sender) external view returns (uint64);

    function lzReceive(
        Origin calldata _origin,
        bytes32 _guid,
        bytes calldata _message,
        address _executor,
        bytes calldata _extraData
    ) external payable;
}

@layerzerolabs/lz-evm-protocol-v2/contracts/interfaces/IMessageLibManager.sol

// SPDX-License-Identifier: MIT

pragma solidity >=0.8.0;

struct SetConfigParam {
    uint32 eid;
    uint32 configType;
    bytes config;
}

interface IMessageLibManager {
    struct Timeout {
        address lib;
        uint256 expiry;
    }

    event LibraryRegistered(address newLib);
    event DefaultSendLibrarySet(uint32 eid, address newLib);
    event DefaultReceiveLibrarySet(uint32 eid, address newLib);
    event DefaultReceiveLibraryTimeoutSet(uint32 eid, address oldLib, uint256 expiry);
    event SendLibrarySet(address sender, uint32 eid, address newLib);
    event ReceiveLibrarySet(address receiver, uint32 eid, address newLib);
    event ReceiveLibraryTimeoutSet(address receiver, uint32 eid, address oldLib, uint256 timeout);

    function registerLibrary(address _lib) external;

    function isRegisteredLibrary(address _lib) external view returns (bool);

    function getRegisteredLibraries() external view returns (address[] memory);

    function setDefaultSendLibrary(uint32 _eid, address _newLib) external;

    function defaultSendLibrary(uint32 _eid) external view returns (address);

    function setDefaultReceiveLibrary(uint32 _eid, address _newLib, uint256 _gracePeriod) external;

    function defaultReceiveLibrary(uint32 _eid) external view returns (address);

    function setDefaultReceiveLibraryTimeout(uint32 _eid, address _lib, uint256 _expiry) external;

    function defaultReceiveLibraryTimeout(uint32 _eid) external view returns (address lib, uint256 expiry);

    function isSupportedEid(uint32 _eid) external view returns (bool);

    function isValidReceiveLibrary(address _receiver, uint32 _eid, address _lib) external view returns (bool);

    /// ------------------- OApp interfaces -------------------
    function setSendLibrary(address _oapp, uint32 _eid, address _newLib) external;

    function getSendLibrary(address _sender, uint32 _eid) external view returns (address lib);

    function isDefaultSendLibrary(address _sender, uint32 _eid) external view returns (bool);

    function setReceiveLibrary(address _oapp, uint32 _eid, address _newLib, uint256 _gracePeriod) external;

    function getReceiveLibrary(address _receiver, uint32 _eid) external view returns (address lib, bool isDefault);

    function setReceiveLibraryTimeout(address _oapp, uint32 _eid, address _lib, uint256 _expiry) external;

    function receiveLibraryTimeout(address _receiver, uint32 _eid) external view returns (address lib, uint256 expiry);

    function setConfig(address _oapp, address _lib, SetConfigParam[] calldata _params) external;

    function getConfig(
        address _oapp,
        address _lib,
        uint32 _eid,
        uint32 _configType
    ) external view returns (bytes memory config);
}

@layerzerolabs/lz-evm-protocol-v2/contracts/interfaces/IMessagingChannel.sol

// SPDX-License-Identifier: MIT

pragma solidity >=0.8.0;

interface IMessagingChannel {
    event InboundNonceSkipped(uint32 srcEid, bytes32 sender, address receiver, uint64 nonce);
    event PacketNilified(uint32 srcEid, bytes32 sender, address receiver, uint64 nonce, bytes32 payloadHash);
    event PacketBurnt(uint32 srcEid, bytes32 sender, address receiver, uint64 nonce, bytes32 payloadHash);

    function eid() external view returns (uint32);

    // this is an emergency function if a message cannot be verified for some reasons
    // required to provide _nextNonce to avoid race condition
    function skip(address _oapp, uint32 _srcEid, bytes32 _sender, uint64 _nonce) external;

    function nilify(address _oapp, uint32 _srcEid, bytes32 _sender, uint64 _nonce, bytes32 _payloadHash) external;

    function burn(address _oapp, uint32 _srcEid, bytes32 _sender, uint64 _nonce, bytes32 _payloadHash) external;

    function nextGuid(address _sender, uint32 _dstEid, bytes32 _receiver) external view returns (bytes32);

    function inboundNonce(address _receiver, uint32 _srcEid, bytes32 _sender) external view returns (uint64);

    function outboundNonce(address _sender, uint32 _dstEid, bytes32 _receiver) external view returns (uint64);

    function inboundPayloadHash(
        address _receiver,
        uint32 _srcEid,
        bytes32 _sender,
        uint64 _nonce
    ) external view returns (bytes32);

    function lazyInboundNonce(address _receiver, uint32 _srcEid, bytes32 _sender) external view returns (uint64);
}

@layerzerolabs/lz-evm-protocol-v2/contracts/interfaces/IMessagingComposer.sol

// SPDX-License-Identifier: MIT

pragma solidity >=0.8.0;

interface IMessagingComposer {
    event ComposeSent(address from, address to, bytes32 guid, uint16 index, bytes message);
    event ComposeDelivered(address from, address to, bytes32 guid, uint16 index);
    event LzComposeAlert(
        address indexed from,
        address indexed to,
        address indexed executor,
        bytes32 guid,
        uint16 index,
        uint256 gas,
        uint256 value,
        bytes message,
        bytes extraData,
        bytes reason
    );

    function composeQueue(
        address _from,
        address _to,
        bytes32 _guid,
        uint16 _index
    ) external view returns (bytes32 messageHash);

    function sendCompose(address _to, bytes32 _guid, uint16 _index, bytes calldata _message) external;

    function lzCompose(
        address _from,
        address _to,
        bytes32 _guid,
        uint16 _index,
        bytes calldata _message,
        bytes calldata _extraData
    ) external payable;
}

@layerzerolabs/lz-evm-protocol-v2/contracts/interfaces/IMessagingContext.sol

// SPDX-License-Identifier: MIT

pragma solidity >=0.8.0;

interface IMessagingContext {
    function isSendingMessage() external view returns (bool);

    function getSendContext() external view returns (uint32 dstEid, address sender);
}

@openzeppelin/contracts/utils/ReentrancyGuard.sol

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.1.0) (utils/ReentrancyGuard.sol)

pragma solidity ^0.8.20;

/**
 * @dev Contract module that helps prevent reentrant calls to a function.
 *
 * Inheriting from `ReentrancyGuard` will make the {nonReentrant} modifier
 * available, which can be applied to functions to make sure there are no nested
 * (reentrant) calls to them.
 *
 * Note that because there is a single `nonReentrant` guard, functions marked as
 * `nonReentrant` may not call one another. This can be worked around by making
 * those functions `private`, and then adding `external` `nonReentrant` entry
 * points to them.
 *
 * TIP: If EIP-1153 (transient storage) is available on the chain you're deploying at,
 * consider using {ReentrancyGuardTransient} instead.
 *
 * TIP: If you would like to learn more about reentrancy and alternative ways
 * to protect against it, check out our blog post
 * https://blog.openzeppelin.com/reentrancy-after-istanbul/[Reentrancy After Istanbul].
 */
abstract contract ReentrancyGuard {
    // Booleans are more expensive than uint256 or any type that takes up a full
    // word because each write operation emits an extra SLOAD to first read the
    // slot's contents, replace the bits taken up by the boolean, and then write
    // back. This is the compiler's defense against contract upgrades and
    // pointer aliasing, and it cannot be disabled.

    // The values being non-zero value makes deployment a bit more expensive,
    // but in exchange the refund on every call to nonReentrant will be lower in
    // amount. Since refunds are capped to a percentage of the total
    // transaction's gas, it is best to keep them low in cases like this one, to
    // increase the likelihood of the full refund coming into effect.
    uint256 private constant NOT_ENTERED = 1;
    uint256 private constant ENTERED = 2;

    uint256 private _status;

    /**
     * @dev Unauthorized reentrant call.
     */
    error ReentrancyGuardReentrantCall();

    constructor() {
        _status = NOT_ENTERED;
    }

    /**
     * @dev Prevents a contract from calling itself, directly or indirectly.
     * Calling a `nonReentrant` function from another `nonReentrant`
     * function is not supported. It is possible to prevent this from happening
     * by making the `nonReentrant` function external, and making it call a
     * `private` function that does the actual work.
     */
    modifier nonReentrant() {
        _nonReentrantBefore();
        _;
        _nonReentrantAfter();
    }

    function _nonReentrantBefore() private {
        // On the first call to nonReentrant, _status will be NOT_ENTERED
        if (_status == ENTERED) {
            revert ReentrancyGuardReentrantCall();
        }

        // Any calls to nonReentrant after this point will fail
        _status = ENTERED;
    }

    function _nonReentrantAfter() private {
        // By storing the original value once again, a refund is triggered (see
        // https://eips.ethereum.org/EIPS/eip-2200)
        _status = NOT_ENTERED;
    }

    /**
     * @dev Returns true if the reentrancy guard is currently set to "entered", which indicates there is a
     * `nonReentrant` function in the call stack.
     */
    function _reentrancyGuardEntered() internal view returns (bool) {
        return _status == ENTERED;
    }
}
Compiler settingsShow
{
  "viaIR": true,
  "optimizer": {
    "runs": 200,
    "enabled": true
  },
  "evmVersion": "cancun"
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.