Skip to content

Commit

Permalink
refactor: ConceroCCIP.sol
Browse files Browse the repository at this point in the history
  • Loading branch information
lufaque committed Apr 10, 2024
1 parent b15f740 commit 784951f
Show file tree
Hide file tree
Showing 10 changed files with 4,500 additions and 21,826 deletions.
4,004 changes: 2,633 additions & 1,371 deletions CLFunctions/yarn.lock

Large diffs are not rendered by default.

20,253 changes: 0 additions & 20,253 deletions package-lock.json

This file was deleted.

107 changes: 107 additions & 0 deletions packages/hardhat/contracts/CCIPInternal.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;

import {OwnerIsCreator} from "@chainlink/contracts-ccip/src/v0.8/shared/access/OwnerIsCreator.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 {Client} from "@chainlink/contracts-ccip/src/v0.8/ccip/libraries/Client.sol";
import {IRouterClient} from "@chainlink/contracts-ccip/src/v0.8/ccip/interfaces/IRouterClient.sol";
import {IConcero} from "./IConcero.sol";

contract CCIPInternal is CCIPReceiver, IConcero {
mapping(uint64 => bool) public allowListedDstChains;
mapping(uint64 => bool) public allowListedSrcChains;
mapping(address => bool) public allowListedSenderContracts;

address private immutable s_linkToken;
address internal externalConceroCCIP;

modifier onlyAllowListedDstChain(uint64 _dstChainSelector) {
if (!allowListedDstChains[_dstChainSelector]) {
revert DestinationChainNotAllowed(_dstChainSelector);
}
_;
}

modifier onlyAllowlistedSenderAndChainSelector(uint64 _sourceChainSelector, address _sender) {
if (!allowListedSrcChains[_sourceChainSelector]) {
revert SourceChainNotAllowed(_sourceChainSelector);
}
if (!allowListedSenderContracts[_sender]) revert SenderNotAllowed(_sender);
_;
}

modifier validateReceiver(address _receiver) {
if (_receiver == address(0)) {
revert InvalidReceiverAddress();
}
_;
}

modifier tokenAmountSufficiency(address _token, uint256 _amount) {
require(IERC20(_token).balanceOf(msg.sender) >= _amount, "Insufficient balance");
_;
}

constructor(address _link, address _ccipRouter, address _externalConceroCCIP) CCIPReceiver(_ccipRouter) {
s_linkToken = _link;
externalConceroCCIP = _externalConceroCCIP;
}

function _sendTokenPayLink(
uint64 _destinationChainSelector,
address _receiver,
address _token,
uint256 _amount
)
internal
onlyAllowListedDstChain(_destinationChainSelector)
// check validateReceiver modifier
validateReceiver(_receiver)
returns (bytes32 messageId)
{
Client.EVM2AnyMessage memory evm2AnyMessage = _buildCCIPMessage(_receiver, _token, _amount, s_linkToken);

IRouterClient router = IRouterClient(this.getRouter());
uint256 fees = router.getFee(_destinationChainSelector, evm2AnyMessage);

if (fees > address(s_linkToken).balance) {
revert NotEnoughBalance(address(s_linkToken).balance, fees);
}

IERC20(_token).approve(address(router), _amount);

messageId = router.ccipSend{value: fees}(_destinationChainSelector, evm2AnyMessage);

emit CCIPSent(messageId, msg.sender, externalConceroCCIP, _token, _amount, _destinationChainSelector);

return messageId;
}

function _buildCCIPMessage(address _receiver, address _token, uint256 _amount, address _feeToken) private view returns (Client.EVM2AnyMessage memory) {
Client.EVMTokenAmount[] memory tokenAmounts = new Client.EVMTokenAmount[](1);
tokenAmounts[0] = Client.EVMTokenAmount({token: _token, amount: _amount});

return
Client.EVM2AnyMessage({
receiver: abi.encode(externalConceroCCIP),
data: abi.encode(_receiver),
tokenAmounts: tokenAmounts,
extraArgs: Client._argsToBytes(Client.EVMExtraArgsV1({gasLimit: 200_000})),
feeToken: _feeToken
});
}

function _ccipReceive(
Client.Any2EVMMessage memory any2EvmMessage
) internal override onlyAllowlistedSenderAndChainSelector(any2EvmMessage.sourceChainSelector, abi.decode(any2EvmMessage.sender, (address))) {
emit CCIPReceived(
any2EvmMessage.messageId,
any2EvmMessage.sourceChainSelector,
abi.decode(any2EvmMessage.sender, (address)),
abi.decode(any2EvmMessage.data, (address)),
any2EvmMessage.destTokenAmounts[0].token,
any2EvmMessage.destTokenAmounts[0].amount
);
}
}
14 changes: 7 additions & 7 deletions packages/hardhat/contracts/CFunctions.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pragma solidity ^0.8.19;
import {FunctionsClient} from "@chainlink/contracts/src/v0.8/functions/v1_0_0/FunctionsClient.sol";
import {ConfirmedOwner} from "@chainlink/contracts/src/v0.8/shared/access/ConfirmedOwner.sol";
import {FunctionsRequest} from "@chainlink/contracts/src/v0.8/functions/v1_0_0/libraries/FunctionsRequest.sol";
import {ConceroBridge} from "./ConceroBridge.sol";
import {ConceroCCIP} from "./ConceroCCIP.sol";
import "@openzeppelin/contracts/utils/Strings.sol";

contract CFunctions is FunctionsClient, ConfirmedOwner {
Expand All @@ -14,7 +14,7 @@ contract CFunctions is FunctionsClient, ConfirmedOwner {
using Strings for address;
using Strings for bytes32;

ConceroBridge private conceroBridge;
ConceroCCIP private conceroCCIP;

struct Transaction {
bytes32 ccipMessageId;
Expand Down Expand Up @@ -69,7 +69,7 @@ contract CFunctions is FunctionsClient, ConfirmedOwner {
allowlist[msg.sender] = true;
externalCcipContract = _externalCcipContract;
internalCcipContract = _internalCcipContract;
conceroBridge = ConceroBridge(_internalCcipContract);
conceroCCIP = ConceroCCIP(_internalCcipContract);
chainSelector = _chainSelector;
}

Expand All @@ -93,7 +93,7 @@ contract CFunctions is FunctionsClient, ConfirmedOwner {

function setInternalCcipContract(address payable _internalCcipContract) external onlyOwner {
internalCcipContract = _internalCcipContract;
conceroBridge = ConceroBridge(_internalCcipContract);
conceroCCIP = ConceroCCIP(_internalCcipContract);
}

function setExternalCcipContract(address _externalCcipContract) external onlyOwner {
Expand Down Expand Up @@ -179,10 +179,10 @@ contract CFunctions is FunctionsClient, ConfirmedOwner {

emit TXConfirmed(ccipMessageId, transaction.sender, transaction.recipient, transaction.amount, transaction.token);

if (address(conceroBridge) == address(0)) {
revert("ConceroBridge address not set");
if (address(conceroCCIP) == address(0)) {
revert("conceroCCIP address not set");
}

conceroBridge.sendTokenToEoa(ccipMessageId, transaction.sender, transaction.recipient, transaction.token, transaction.amount);
conceroCCIP.sendTokenToEoa(ccipMessageId, transaction.sender, transaction.recipient, transaction.token, transaction.amount);
}
}
96 changes: 0 additions & 96 deletions packages/hardhat/contracts/ConceroBridge.sol

This file was deleted.

Loading

0 comments on commit 784951f

Please sign in to comment.