forked from solangegueiros/chainlink-bootcamp-2024
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CrossSourceMinterMumbai.sol
85 lines (68 loc) · 3.12 KB
/
CrossSourceMinterMumbai.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// SPDX-License-Identifier: MIT
pragma solidity 0.8.19;
// Deploy this contract on Mumbai
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 {LinkTokenInterface} from "@chainlink/contracts/src/v0.8/interfaces/LinkTokenInterface.sol";
/**
* 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.
*/
contract CrossSourceMinterMumbai {
// Custom errors to provide more descriptive revert messages.
error NotEnoughBalance(uint256 currentBalance, uint256 calculatedFees); // Used to make sure contract has enough balance to cover the fees.
error NothingToWithdraw(); // Used when trying to withdraw but there's nothing to withdraw.
IRouterClient public router;
LinkTokenInterface public linkToken;
uint64 public destinationChainSelector;
address public owner;
address public destinationMinter;
event MessageSent(bytes32 messageId);
constructor(address destMinterAddress) {
owner = msg.sender;
// https://docs.chain.link/ccip/supported-networks/testnet
// from Mumbai
address routerAddressMumbai = 0x1035CabC275068e0F4b745A29CEDf38E13aF41b1;
router = IRouterClient(routerAddressMumbai);
linkToken = LinkTokenInterface(0x326C977E6efc84E512bB9C30f76E30c160eD06FB);
linkToken.approve(routerAddressMumbai, type(uint256).max);
// to Sepolia
destinationChainSelector = 16015286601757825753;
destinationMinter = destMinterAddress;
}
function mintOnSepolia() external {
// Mint from Mumbai network = chain[2]
Client.EVM2AnyMessage memory message = Client.EVM2AnyMessage({
receiver: abi.encode(destinationMinter),
data: abi.encodeWithSignature("mintFrom(address,uint256)", msg.sender, 2),
tokenAmounts: new Client.EVMTokenAmount[](0),
extraArgs: Client._argsToBytes(
Client.EVMExtraArgsV1({gasLimit: 980_000})
),
feeToken: address(linkToken)
});
// Get the fee required to send the message
uint256 fees = router.getFee(destinationChainSelector, message);
if (fees > linkToken.balanceOf(address(this)))
revert NotEnoughBalance(linkToken.balanceOf(address(this)), fees);
bytes32 messageId;
// Send the message through the router and store the returned message ID
messageId = router.ccipSend(destinationChainSelector, message);
emit MessageSent(messageId);
}
modifier onlyOwner() {
require(msg.sender == owner);
_;
}
function linkBalance (address account) public view returns (uint256) {
return linkToken.balanceOf(account);
}
function withdrawLINK(
address beneficiary
) public onlyOwner {
uint256 amount = linkToken.balanceOf(address(this));
if (amount == 0) revert NothingToWithdraw();
linkToken.transfer(beneficiary, amount);
}
}