-
Notifications
You must be signed in to change notification settings - Fork 7
/
LinkedFactory.sol
142 lines (117 loc) · 4.51 KB
/
LinkedFactory.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
//SPDX-License-Identifier: MIT
pragma solidity >=0.8.0 <0.9.0;
import "@openzeppelin/contracts/access/Ownable.sol";
import { IRouterClient } from "./chainlink/ccip/interfaces/IRouterClient.sol";
import { Client } from "./chainlink/ccip/libraries/Client.sol";
import { CCIPReceiver } from "./chainlink/ccip/applications/CCIPReceiver.sol";
import { LinkedNft } from "./LinkedNft.sol";
// todo keep the selector of this chain
/**
* A smartcontract that creates new NFTs.
* @author Medet Ahmetson
*/
contract LinkedFactory is Ownable, CCIPReceiver {
address public router;
uint64 public networkSelector;
address public registrar;
struct Network {
address router; // Chainlink CCIP router
address registrar; // Registrar on another blockchain
}
// selector => Network Param
mapping(uint64 => Network) public destNetworks;
mapping(address => address) public linkedAddrs; // no need to pre-compute everytime
uint64 private tempChainId;
event X_Setup(uint64 selector, address nftAddress, bytes32 messageId);
event X_SetupOne(uint64 selector, address nftAddress, bytes32 messageId);
event Linked(address originalAddr, address nftAddress);
event Received(bytes sourceRouter, bytes32 messageId, uint64 sourceChainSelector);
event LinkNftCreated(bytes32 salt, string name, string symbol, address nftAddr, address router);
event LintedNfts(uint64[] selectors, address[] linkedNftAddrs);
event LinkError(string reason);
event UnknownError(string reason);
// Todo get chainlink receiver and pass networkParams.router
constructor (
uint64 _networkSelector,
address _router,
uint64[] memory destSelectors,
address[] memory destRouters
) Ownable(msg.sender) CCIPReceiver(_router) {
require(destSelectors.length == destRouters.length, "mismatch length");
router = _router;
networkSelector = _networkSelector;
for (uint64 i = 0; i < destSelectors.length; i++) {
destNetworks[destSelectors[i]].router = destRouters[i];
}
}
/**
* Set's the registrar on other blockchain.
*/
function setRegistrar(uint64 _selector, address _registrar) external onlyOwner {
if (_selector == networkSelector) {
registrar = _registrar;
}
require(destNetworks[_selector].router != address(0), "unsupported network");
// Enable in production
// require(destNetworks[_selector].registrar == address(0), "registrar exists");
destNetworks[_selector].registrar = _registrar;
}
function ccipReceive2(
Client.Any2EVMMessage calldata message
) external {
_ccipReceive(message);
}
function _ccipReceive(
Client.Any2EVMMessage memory message
) internal override {
// address sourceRegistrar = abi.decode(message.sender, (address));
// require(destNetworks[message.sourceChainSelector].registrar == sourceRegistrar, "not the registrar");
(bool success, ) = address(this).call(message.data);
// require(success);
emit Received(message.sender, message.messageId, message.sourceChainSelector);
}
function xSetup(
address nftAddr,
string memory name,
string memory symbol,
uint64[] memory selectors,
address[] memory linkedNftAddrs
) public {
bytes32 salt = generateSalt(address(this), nftAddr);
emit LinkNftCreated(salt, name, symbol, nftAddr, router);
emit LintedNfts(selectors, linkedNftAddrs);
try new LinkedNft{salt: salt}(name, symbol, nftAddr, router) returns (LinkedNft linkedNft) {
// first network selector is the original chain. it must be same everywhere.
// so let's keep the order in all blockchains.
linkedNft.setup(selectors, linkedNftAddrs);
linkedNft.setupOne(networkSelector, address(linkedNft));
linkedAddrs[nftAddr] = address(linkedNft);
emit Linked(nftAddr, address(linkedNft));
} catch Error(string memory reason) {
// catch failing revert() and require()
emit LinkError(string(reason));
} catch (bytes memory reason) {
// catch failing assert()
emit UnknownError(string(reason));
}
}
function generateSalt(address _registrar, address _nftAddr) public pure returns(bytes32) {
return keccak256(abi.encodePacked(_registrar, _nftAddr));
}
function precomputeLinkedNft(address _registrar, string memory _name,
string memory _symbol,
address nftAddress,
address _router) public pure returns(address) {
bytes32 salt = generateSalt(_registrar, nftAddress);
address predictedAddress = address(uint160(uint(keccak256(abi.encodePacked(
bytes1(0xff),
_registrar, // address of the smartcontract
salt,
keccak256(abi.encodePacked(
type(LinkedNft).creationCode,
abi.encode(_name, _symbol, nftAddress, _router)
))
)))));
return predictedAddress;
}
}