Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Receiver

ABI

Inherits: CCIPReceiver, IReceiver, ReentrancyGuard

State Variables

inError

bool public inError = false;

PRECISION

uint256 public constant PRECISION = 1e18;

BONUS_PRECISION

uint256 public constant BONUS_PRECISION = 100;

i_token

address public immutable i_token;

i_stTrendex

address public immutable i_stTrendex;

s_addressProvider

IAddressProvider public s_addressProvider;

s_conversionRatio

uint256 public s_conversionRatio = 1e18;

s_allowlistedSourceChains

mapping(uint64 => bool) public s_allowlistedSourceChains;

s_allowlistedSenders

mapping(address => bool) public s_allowlistedSenders;

s_processedMessages

mapping(bytes32 messageId => bool isProcessed) public s_processedMessages;

s_failedMessages

mapping(bytes32 messageId => bytes errorData) public s_failedMessages;

s_messageContents

mapping(bytes32 messageId => Client.Any2EVMMessage content) public s_messageContents;

s_amountToMint

mapping(address user => uint256 amount) public s_amountToMint;

Functions

setInError

function setInError() public;

onlyMaintainer

check that msg.sender has MAINTAINER_ROLE

modifier onlyMaintainer();

onlyOperator

check that msg.sender has OPERATOR_ROLE

modifier onlyOperator();

onlyAllowlisted

check that the source chain and the sender are allowlisted

modifier onlyAllowlisted(uint64 sourceChainSelector, address sender);

onlyReceiver

check that the caller is the receiver (address(this))

modifier onlyReceiver();

constructor

constructor(
    address stTrendex,
    address router,
    address token,
    address addressProvider,
    address sender,
    uint64 sourceChain
) CCIPReceiver(router);

ccipReceive

This function is called by the CCIP router to receive messages

It processes the message and increment the amount of tokens to the destination user address.

It catches any errors that occur during the processing of the message and handles them accordingly by storing the error data and emitting an event.

function ccipReceive(Client.Any2EVMMessage calldata any2EvmMessage)
    external
    override
    onlyRouter
    onlyAllowlisted(any2EvmMessage.sourceChainSelector, abi.decode(any2EvmMessage.sender, (address)));

Parameters

NameTypeDescription
any2EvmMessageClient.Any2EVMMessageThe message coming from the source chain

retryFailedMessage

This function allows protocole to retry failed message

function retryFailedMessage(bytes32 messageId) external override onlyOperator;

Parameters

NameTypeDescription
messageIdbytes32The ID of the message to retry

processMessage

This function processes the message received from the CCIP router

function processMessage(Client.Any2EVMMessage memory any2EvmMessage) external onlyReceiver;

Parameters

NameTypeDescription
any2EvmMessageClient.Any2EVMMessageThe message coming from the source chain

setAddressProvider

This function sets the address provider

function setAddressProvider(address addressProvider) external override onlyMaintainer;

Parameters

NameTypeDescription
addressProvideraddressThe address of the address provider

setAllowlistSourceChain

This function updates the allowlist for receiving from a source chain

function setAllowlistSourceChain(uint64 sourceChainSelector, bool allow) external override onlyMaintainer;

Parameters

NameTypeDescription
sourceChainSelectoruint64The selector of the source chain
allowboolThe permission

setAllowlistSender

This function updates the allowlist for receive message from a sender contract

function setAllowlistSender(address sender, bool allow) external override onlyMaintainer;

Parameters

NameTypeDescription
senderaddressThe address of the sender contract
allowboolThe permission

setConversionRatio

This function sets the conversion ratio

function setConversionRatio(uint256 ratio) external override onlyMaintainer;

Parameters

NameTypeDescription
ratiouint256The conversion ratio

getMessageContents

This function returns the contents of a message by its ID

function getMessageContents(bytes32 messageId) external view returns (Client.Any2EVMMessage memory);

Parameters

NameTypeDescription
messageIdbytes32The ID of the message

Returns

NameTypeDescription
<none>Client.Any2EVMMessageThe contents of the message

getConvertedAmount

This function return the conerted amount and the final amount with bonus percentage

function getConvertedAmount(uint256 amount)
    external
    view
    returns (uint256 convertedAmount, uint256 finalAmount, uint256 bonusPercentage);

Parameters

NameTypeDescription
amountuint256The amount to convert

Returns

NameTypeDescription
convertedAmountuint256The amount after conversion
finalAmountuint256The final amount after applying the bonus percentage
bonusPercentageuint256The percentage of the bonus applied

_ccipReceive

This function is called by the CCIP router to receive messages

function _ccipReceive(Client.Any2EVMMessage memory any2EvmMessage) internal virtual override;

Parameters

NameTypeDescription
any2EvmMessageClient.Any2EVMMessageThe message coming from the source chain

_handleFailedMessage

This function handles a failed message by storing the error data and emitting an event

function _handleFailedMessage(Client.Any2EVMMessage calldata any2EvmMessage, bytes memory errorData) private;

Parameters

NameTypeDescription
any2EvmMessageClient.Any2EVMMessageThe message that failed
errorDatabytesThe error data to store

_setAddressProvider

function _setAddressProvider(address addressProvider) private;

_setAllowlistSourceChain

function _setAllowlistSourceChain(uint64 sourceChainSelector, bool allow) private;

_setAllowlistSender

function _setAllowlistSender(address sender, bool allow) private;

_setConversionRatio

function _setConversionRatio(uint256 ratio) private;

_calculateConversion

This function calculates the conversion of the amount based on the conversion ratio

function _calculateConversion(uint256 amount)
    private
    view
    returns (uint256 _amount, uint256 finalAmount, uint256 bonusPercentage);

Parameters

NameTypeDescription
amountuint256The amount to convert

Returns

NameTypeDescription
_amountuint256The converted amount
finalAmountuint256The final amount after applying the bonus
bonusPercentageuint256The bonus percentage applied