-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathLinkedNft.sol
199 lines (166 loc) · 6.55 KB
/
LinkedNft.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC721/extensions/IERC721Metadata.sol";
import { ERC721 } from "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import { ERC721URIStorage } from "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import { CCIPReceiver } from "./chainlink/ccip/applications/CCIPReceiver.sol";
import { IRouterClient } from "./chainlink/ccip/interfaces/IRouterClient.sol";
import { Client } from "./chainlink/ccip/libraries/Client.sol";
/**
* @notice WrappedNft is an NFT on the Source Blockchain, which locks the original NFT, and disables transferring it.
* When an NFT is wrapped, the Listening oracles would mint the copy on target Blockchain.
*/
contract LinkedNft is ERC721URIStorage, CCIPReceiver {
address public originalNft;
address public factory;
address public router;
uint64 private selector;
// remove the selector from here, and get it from the parent.
// track routers and selectors.
uint64[] public nftSupportedChains;
mapping(uint64 => address) public linkedNfts;
address internal sender;
event X_Bridge(uint64 destSelector, uint256 nftId, address owner, bytes32 messageId);
modifier nftOwner(uint256 nftId) {
//require(_ownerOf(nftId) == msg.sender, "not owner");
_;
}
modifier onlyFactory() {
//require(msg.sender == address(factory));
_;
}
modifier onlyFactoryOrSource() {
/*if (msg.sender != address(factory)) {
require(sender != address(0), "not from ccip");
bool found = false;
for (uint256 i = 0; i < nftSupportedChains.length; i++) {
if (linkedNfts[nftSupportedChains[i]] == sender) {
found = true;
break;
}
}
require(found, "not source");
}*/
_;
}
modifier onlySource() {
/*bool found = false;
for (uint256 i = 0; i < nftSupportedChains.length; i++) {
if (linkedNfts[nftSupportedChains[i]] == sender) {
found = true;
break;
}
}
require(found, "not source");*/
_;
}
modifier validDestination(uint64 _selector) {
/*require(selector != _selector, "to itself");
bool found = false;
for (uint256 i = 0; i < nftSupportedChains.length; i++) {
if (nftSupportedChains[i] == _selector) {
found = true;
break;
}
}
require(found, "unsupported network");*/
_;
}
constructor(
string memory _name,
string memory _symbol,
address _source,
address _router)
ERC721(_name, _symbol) CCIPReceiver(_router) {
factory = msg.sender;
originalNft = _source;
router = _router;
}
function setupOne(uint64 linkedSelector, address linkedNftAddr) public onlyFactoryOrSource {
if (selector == 0) {
selector = linkedSelector;
}
nftSupportedChains.push(linkedSelector);
linkedNfts[linkedSelector] = linkedNftAddr;
}
function setup(uint64[] calldata linkedSelectors, address[] calldata linkedNftAddrs) external onlyFactory {
// Pre-calculate the nft addresses
for (uint i = 0; i < linkedSelectors.length; i++) {
nftSupportedChains.push(linkedSelectors[i]);
linkedNfts[linkedSelectors[i]] = linkedNftAddrs[i];
}
}
/// @notice Mint a new NFT from the source.
function bridge(uint256 nftId, address to, string memory uri) public onlySource {
require(_ownerOf(nftId) == address(0), "already minted");
_mint(to, nftId);
_setTokenURI(nftId, uri);
}
/// @notice Transfer the nft to another blockchain.
function bridge(uint256 nftId, uint64 chainSelector) external nftOwner(nftId) validDestination(chainSelector) payable {
// pre-compute the linked address
address linkedAddr = linkedNfts[chainSelector];
bytes memory data;
if (nftSupportedChains[0] == chainSelector) {
data = abi.encodeWithSignature("bridge(uint256,address)", nftId, msg.sender);
} else {
data = abi.encodeWithSignature("bridge(uint256,address,string)", nftId, msg.sender, tokenURI(nftId));
}
Client.EVM2AnyMessage memory message = Client.EVM2AnyMessage({
receiver: abi.encode(linkedAddr),
data: data,
tokenAmounts: new Client.EVMTokenAmount[](0),
extraArgs: "",
feeToken: address(0)
});
uint256 fee = IRouterClient(router).getFee(
chainSelector,
message
);
require(msg.value >= fee, "insufficient gas");
bytes32 messageId = IRouterClient(router).ccipSend{value: fee}(
chainSelector,
message
);
_burn(nftId);
emit X_Bridge(chainSelector, nftId, msg.sender, messageId);
}
// Calculate the required fee
function calculateBridgeFee(uint256 nftId, uint64 chainSelector) external view returns(uint256) {
// pre-compute the linked address
address linkedAddr = linkedNfts[chainSelector];
bytes memory data;
if (nftSupportedChains[0] == chainSelector) {
data = abi.encodeWithSignature("bridge(uint256,address)", nftId, msg.sender);
} else {
data = abi.encodeWithSignature("bridge(uint256,address,string)", nftId, msg.sender, tokenURI(nftId));
}
Client.EVM2AnyMessage memory message = Client.EVM2AnyMessage({
receiver: abi.encode(linkedAddr),
data: data,
tokenAmounts: new Client.EVMTokenAmount[](0),
extraArgs: "",
feeToken: address(0)
});
uint256 fee = IRouterClient(router).getFee(
chainSelector,
message
);
return fee;
}
function _ccipReceive(
Client.Any2EVMMessage memory message
) internal override {
sender = abi.decode(message.sender, (address));
require(linkedNfts[message.sourceChainSelector] == sender, "not the linked nft");
(bool success, ) = address(this).call(message.data);
require(success);
sender = address(0);
}
function supportsInterface(bytes4 interfaceId) public view override(ERC721URIStorage, CCIPReceiver) returns (bool) {
if (ERC721URIStorage.supportsInterface(interfaceId)) {
return true;
}
return CCIPReceiver.supportsInterface(interfaceId);
}
}