Skip to content

Commit

Permalink
create the mint function and send message to ccip
Browse files Browse the repository at this point in the history
  • Loading branch information
Ginowine committed Dec 5, 2023
1 parent 0450544 commit 38f22ca
Showing 1 changed file with 87 additions and 31 deletions.
118 changes: 87 additions & 31 deletions contracts/CrossNftSourceMinter.sol
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.19;

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

contract CrossNftSourceMinter is Withdraw {
Expand Down Expand Up @@ -55,40 +57,94 @@ contract CrossNftSourceMinter is Withdraw {
}

function mint(
uint64 destinationChainSelector,
address receiver,
PayFeesIn payFeesIn
) external {
uint64 _destinationChainSelector,
address _receiver,
address _token,
uint256 _amount,
string _metadataURL
)
external
onlyWhitelistedChain(_destinationChainSelector)
returns (bytes32 messageId)
{
Client.EVMTokenAmount[]
memory tokenAmounts = new Client.EVMTokenAmount[](1);
Client.EVMTokenAmount memory tokenAmount = Client.EVMTokenAmount({
token: _token,
amount: _amount
});
tokenAmounts[0] = tokenAmount;

// Build the CCIP Message
Client.EVM2AnyMessage memory message = Client.EVM2AnyMessage({
receiver: abi.encode(receiver),
data: abi.encodeWithSignature("mint(address)", msg.sender),
tokenAmounts: new Client.EVMTokenAmount[](0),
extraArgs: "",
feeToken: payFeesIn == PayFeesIn.LINK ? i_link : address(0)
receiver: abi.encode(_receiver),
data: abi.encodeWithSignature("mint(address, _metadataURL)", msg.sender),
tokenAmounts: tokenAmounts,
extraArgs: Client._argsToBytes(
Client.EVMExtraArgsV1({gasLimit: 200_000, strict: false})
),
feeToken: address(linkToken)
});

uint256 fee = IRouterClient(i_router).getFee(
destinationChainSelector,
message

// CCIP Fees Management
uint256 fees = router.getFee(_destinationChainSelector, message);

if (fees > linkToken.balanceOf(address(this)))
revert NotEnoughBalance(linkToken.balanceOf(address(this)), fees);

linkToken.approve(address(router), fees);

// Approve Router to spend CCIP-BnM tokens we send
IERC20(_token).approve(address(router), _amount);

// Send CCIP Message
messageId = router.ccipSend(_destinationChainSelector, message);

emit TokensTransferred(
messageId,
_destinationChainSelector,
_receiver,
_token,
_amount,
fees
);

bytes32 messageId;

if (payFeesIn == PayFeesIn.LINK) {
// LinkTokenInterface(i_link).approve(i_router, fee);
messageId = IRouterClient(i_router).ccipSend(
destinationChainSelector,
message
);
} else {
messageId = IRouterClient(i_router).ccipSend{value: fee}(
destinationChainSelector,
message
);
}

emit MessageSent(messageId);
}

// function mint(
// uint64 destinationChainSelector,
// address receiver,
// PayFeesIn payFeesIn
// ) external {
// Client.EVM2AnyMessage memory message = Client.EVM2AnyMessage({
// receiver: abi.encode(receiver),
// data: abi.encodeWithSignature("mint(address)", msg.sender),
// tokenAmounts: new Client.EVMTokenAmount[](0),
// extraArgs: "",
// feeToken: payFeesIn == PayFeesIn.LINK ? i_link : address(0)
// });

// uint256 fee = IRouterClient(i_router).getFee(
// destinationChainSelector,
// message
// );

// bytes32 messageId;

// if (payFeesIn == PayFeesIn.LINK) {
// // LinkTokenInterface(i_link).approve(i_router, fee);
// messageId = IRouterClient(i_router).ccipSend(
// destinationChainSelector,
// message
// );
// } else {
// messageId = IRouterClient(i_router).ccipSend{value: fee}(
// destinationChainSelector,
// message
// );
// }

// emit MessageSent(messageId);
// }

receive() external payable {}
}

0 comments on commit 38f22ca

Please sign in to comment.