Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
dorianbayart committed Mar 9, 2024
2 parents e9c3d67 + 47e727d commit 96a3687
Show file tree
Hide file tree
Showing 83 changed files with 3,301 additions and 714 deletions.
2 changes: 2 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"IERC",
"Linea",
"Mainnets",
"Merkle",
"MOONRIVER",
"MOVR",
"Numberish",
Expand All @@ -55,6 +56,7 @@
"typechain",
"WAVAX",
"WBNB",
"whatsnext",
"WMATIC",
"XDAI"
]
Expand Down
747 changes: 647 additions & 100 deletions package-lock.json

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
"scripts": {
"dev": "astro dev",
"start": "astro dev",
"build": "astro build",
"build": "astro build && npm run pin-sol-version",
"preview": "astro preview",
"format": "prettier --write .",
"pin-sol-version": "npx ts-node -r tsconfig-paths/register src/scripts/helper/pin-solver-dist.ts",
"linkcheckWrapper": "npx ts-node -r tsconfig-paths/register src/scripts/link-check/linkcheckWrapper.ts",
"linkcheck-internal": "npx ts-node -r tsconfig-paths/register src/scripts/link-check/linkcheck.ts",
"linkcheck-external": "npx ts-node -r tsconfig-paths/register src/scripts/link-check/linkcheck.ts --mode=external",
Expand Down Expand Up @@ -38,7 +39,7 @@
"@astrojs/sitemap": "^3.0.3",
"@chainlink/components": "^0.4.5",
"@chainlink/contracts": "^0.8.0",
"@chainlink/contracts-ccip": "^1.2.1",
"@chainlink/contracts-ccip": "^1.4.0",
"@chainlink/design-system": "^0.1.50",
"@chainlink/solana-sdk": "^0.2.2",
"@metamask/detect-provider": "^2.0.0",
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/ccip/manual-execution-status.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/ccip/manual-execution.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
220 changes: 220 additions & 0 deletions public/samples/CCIP/Acknowledger.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.19;

import {IRouterClient} from "@chainlink/contracts-ccip/src/v0.8/ccip/interfaces/IRouterClient.sol";
import {OwnerIsCreator} from "@chainlink/contracts-ccip/src/v0.8/shared/access/OwnerIsCreator.sol";
import {Client} from "@chainlink/contracts-ccip/src/v0.8/ccip/libraries/Client.sol";
import {CCIPReceiver} from "@chainlink/contracts-ccip/src/v0.8/ccip/applications/CCIPReceiver.sol";
import {IERC20} from "@chainlink/contracts-ccip/src/v0.8/vendor/openzeppelin-solidity/v4.8.3/contracts/token/ERC20/IERC20.sol";
import {SafeERC20} from "@chainlink/contracts-ccip/src/v0.8/vendor/openzeppelin-solidity/v4.8.3/contracts/token/ERC20/utils/SafeERC20.sol";

using SafeERC20 for IERC20;

/**
* THIS IS AN EXAMPLE CONTRACT THAT USES HARDCODED VALUES FOR CLARITY.
* THIS IS AN EXAMPLE CONTRACT THAT USES UN-AUDITED CODE.
* DO NOT USE THIS CODE IN PRODUCTION.
*/

/// @title - A simple acknowledger contract for receiving data and sending acknowledgement of receipt messages across chains.
contract Acknowledger is CCIPReceiver, OwnerIsCreator {
// Custom errors to provide more descriptive revert messages.
error NotEnoughBalance(uint256 currentBalance, uint256 calculatedFees); // Used to make sure contract has enough balance.
error NothingToWithdraw(); // Used when trying to withdraw Ether but there's nothing to withdraw.
error DestinationChainNotAllowlisted(uint64 destinationChainSelector); // Used when the destination chain has not been allowlisted by the contract owner.
error InvalidReceiverAddress(); // Used when the receiver address is 0.
error SourceChainNotAllowlisted(uint64 sourceChainSelector); // Used when the source chain has not been allowlisted by the contract owner.
error SenderNotAllowlisted(address sender); // Used when the sender has not been allowlisted by the contract owner.

string private s_lastReceivedText; // Store the last received text.

// Mapping to keep track of allowlisted destination chains.
mapping(uint64 => bool) public allowlistedDestinationChains;

// Mapping to keep track of allowlisted source chains.
mapping(uint64 => bool) public allowlistedSourceChains;

// Mapping to keep track of allowlisted senders.
mapping(address => bool) public allowlistedSenders;

// Emitted when an acknowledgment message is successfully sent back to the sender contract.
// This event signifies that the Acknowledger contract has recognized the receipt of an initial message
// and has informed the original sender contract by sending an acknowledgment message,
// including the original message ID.
event AcknowledgmentSent(
bytes32 indexed messageId, // The unique ID of the CCIP message.
uint64 indexed destinationChainSelector, // The chain selector of the destination chain.
address indexed receiver, // The address of the receiver on the destination chain.
bytes32 data, // The data being sent back, containing the message ID of the initial message to acknowledge.
address feeToken, // The token address used to pay CCIP fees for sending the acknowledgment.
uint256 fees // The fees paid for sending the acknowledgment message via CCIP.
);

IERC20 private s_linkToken;

/// @notice Constructor initializes the contract with the router address.
/// @param _router The address of the router contract.
/// @param _link The address of the link contract.
constructor(address _router, address _link) CCIPReceiver(_router) {
s_linkToken = IERC20(_link);
}

/// @dev Modifier that checks if the chain with the given destinationChainSelector is allowlisted.
/// @param _destinationChainSelector The selector of the destination chain.
modifier onlyAllowlistedDestinationChain(uint64 _destinationChainSelector) {
if (!allowlistedDestinationChains[_destinationChainSelector])
revert DestinationChainNotAllowlisted(_destinationChainSelector);
_;
}

/// @dev Modifier that checks if the chain with the given sourceChainSelector is allowlisted and if the sender is allowlisted.
/// @param _sourceChainSelector The selector of the destination chain.
/// @param _sender The address of the sender.
modifier onlyAllowlisted(uint64 _sourceChainSelector, address _sender) {
if (!allowlistedSourceChains[_sourceChainSelector])
revert SourceChainNotAllowlisted(_sourceChainSelector);
if (!allowlistedSenders[_sender]) revert SenderNotAllowlisted(_sender);
_;
}

/// @dev Updates the allowlist status of a destination chain for transactions.
function allowlistDestinationChain(
uint64 _destinationChainSelector,
bool allowed
) external onlyOwner {
allowlistedDestinationChains[_destinationChainSelector] = allowed;
}

/// @dev Updates the allowlist status of a source chain for transactions.
function allowlistSourceChain(
uint64 _sourceChainSelector,
bool allowed
) external onlyOwner {
allowlistedSourceChains[_sourceChainSelector] = allowed;
}

/// @dev Updates the allowlist status of a sender for transactions.
function allowlistSender(address _sender, bool allowed) external onlyOwner {
allowlistedSenders[_sender] = allowed;
}

/// @notice Sends an acknowledgment message back to the sender contract on the source chain
/// and pays the fees using LINK tokens.
/// @dev This function constructs and sends an acknowledgment message using CCIP,
/// indicating the receipt and processing of an initial message. It emits the `AcknowledgmentSent` event
/// upon successful sending. This function should be called after processing the received message
/// to inform the sender contract about the successful message reception.
/// @param _messageIdToAcknowledge The message ID of the initial message being acknowledged.
/// @param _messageTrackerAddress The address of the message tracker contract on the source chain.
/// @param _messageTrackerChainSelector The chain selector of the source chain.
function _acknowledgePayLINK(
bytes32 _messageIdToAcknowledge,
address _messageTrackerAddress,
uint64 _messageTrackerChainSelector
) private {
if (_messageTrackerAddress == address(0))
revert InvalidReceiverAddress();

// Construct the CCIP message for acknowledgment, including the message ID of the initial message.
Client.EVM2AnyMessage memory acknowledgment = Client.EVM2AnyMessage({
receiver: abi.encode(_messageTrackerAddress), // ABI-encoded receiver address
data: abi.encode(_messageIdToAcknowledge), // ABI-encoded message ID to acknowledge
tokenAmounts: new Client.EVMTokenAmount[](0), // Empty array aas no tokens are transferred
extraArgs: Client._argsToBytes(
// Additional arguments, setting gas limit
Client.EVMExtraArgsV1({gasLimit: 200_000})
),
// Set the feeToken to a feeTokenAddress, indicating specific asset will be used for fees
feeToken: address(s_linkToken)
});

// Initialize a router client instance to interact with the cross-chain router.
IRouterClient router = IRouterClient(this.getRouter());

// Calculate the fee required to send the CCIP acknowledgment message.
uint256 fees = router.getFee(
_messageTrackerChainSelector, // The chain selector for routing the message.
acknowledgment // The acknowledgment message data.
);

// Ensure the contract has sufficient balance to cover the message sending fees.
if (fees > s_linkToken.balanceOf(address(this))) {
revert NotEnoughBalance(s_linkToken.balanceOf(address(this)), fees);
}

// Approve the router to transfer LINK tokens on behalf of this contract to cover the sending fees.
s_linkToken.approve(address(router), fees);

// Send the acknowledgment message via the CCIP router and capture the resulting message ID.
bytes32 messageId = router.ccipSend(
_messageTrackerChainSelector, // The destination chain selector.
acknowledgment // The CCIP message payload for acknowledgment.
);

// Emit an event detailing the acknowledgment message sending, for external tracking and verification.
emit AcknowledgmentSent(
messageId, // The ID of the sent acknowledgment message.
_messageTrackerChainSelector, // The destination chain selector.
_messageTrackerAddress, // The receiver of the acknowledgment, typically the original sender.
_messageIdToAcknowledge, // The original message ID that was acknowledged.
address(s_linkToken), // The fee token used.
fees // The fees paid for sending the message.
);
}

/// @dev Handles a received CCIP message, processes it, and acknowledges its receipt.
/// This internal function is called upon the receipt of a new message via CCIP from an allowlisted source chain and sender.
/// It decodes the message and acknowledges its receipt by calling `_acknowledgePayLINK`.
/// @param any2EvmMessage The CCIP message received
function _ccipReceive(
Client.Any2EVMMessage memory any2EvmMessage
)
internal
override
onlyAllowlisted(
any2EvmMessage.sourceChainSelector,
abi.decode(any2EvmMessage.sender, (address))
) // Make sure source chain and sender are allowlisted
{
bytes32 messageIdToAcknowledge = any2EvmMessage.messageId; // The message ID of the received message to acknowledge
address messageTrackerAddress = abi.decode(
any2EvmMessage.sender,
(address)
); // ABI-decoding of the message tracker address
uint64 messageTrackerChainSelector = any2EvmMessage.sourceChainSelector; // The chain selector of the received message
s_lastReceivedText = abi.decode(any2EvmMessage.data, (string)); // abi-decoding of the sent text

_acknowledgePayLINK(
messageIdToAcknowledge,
messageTrackerAddress,
messageTrackerChainSelector
);
}

/// @notice Fetches the details of the last received message.
/// @return text The last received text.
function getLastReceivedMessage()
external
view
returns (string memory text)
{
return (s_lastReceivedText);
}

/// @notice Allows the owner of the contract to withdraw all tokens of a specific ERC20 token.
/// @dev This function reverts with a 'NothingToWithdraw' error if there are no tokens to withdraw.
/// @param _beneficiary The address to which the tokens will be sent.
/// @param _token The contract address of the ERC20 token to be withdrawn.
function withdrawToken(
address _beneficiary,
address _token
) public onlyOwner {
// Retrieve the balance of this contract
uint256 amount = IERC20(_token).balanceOf(address(this));

// Revert if there is nothing to withdraw
if (amount == 0) revert NothingToWithdraw();

IERC20(_token).safeTransfer(_beneficiary, amount);
}
}
Loading

0 comments on commit 96a3687

Please sign in to comment.