-
Notifications
You must be signed in to change notification settings - Fork 7
/
WrappedNft.sol
305 lines (250 loc) · 10.6 KB
/
WrappedNft.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import { ERC721 } from "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import { ERC721URIStorage } from "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/IERC721Metadata.sol";
import "@openzeppelin/contracts/token/ERC721/IERC721Receiver.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";
contract WrappedNft is ERC721URIStorage, IERC721Receiver, CCIPReceiver {
ERC721 public originalNft;
address public registrar;
address public router;
// chain id => linked nft|wrapped nft.
uint64[] public nftSupportedChains;
mapping(uint64 => address) public linkedNfts;
// NFT that sent the transaction
address private sender;
event NftReceived(address operator, address from, uint256 tokenId, bytes data);
event X_Bridge(uint64 destSelector, uint256 nftId, address owner, bytes32 messageId);
event X_SetupOne(uint64 selector, address nftAddress, bytes32 messageId);
modifier nftOwner(uint256 nftId) {
/*require(originalNft.ownerOf(nftId) == msg.sender, "not owner");
require(originalNft.isApprovedForAll(msg.sender, address(this)), "wrapping has no permission");*/
_;
}
modifier onlySource {
/*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 onlyFactory() {
// require(msg.sender == registrar);
_;
}
modifier validDestination(uint64 _selector) {
// require(linkedNfts[_selector] != address(0), "not linked");
_;
}
/**
* @param _source is the original NFT that is wrapped to bridge
*/
constructor(
string memory _name,
string memory _symbol,
address _source,
address _router) ERC721(_name, _symbol) CCIPReceiver(_router) {
require(_source != address(0), "ZERO_ADDRESS");
originalNft = ERC721(_source);
registrar = msg.sender;
router = _router;
}
/// @notice registers itself as the first element of NFTs supported chains.
/// @dev this method must be called first.
function setupOne(uint64 linkedSelector) external onlyFactory {
require(nftSupportedChains.length == 0, "call it only once");
nftSupportedChains.push(linkedSelector);
linkedNfts[linkedSelector] = address(this);
}
/// @notice registers a linked nft.
/// @dev This method must be called after calling setting up wrapped nft as the first supported nft.
/// @param linkedSelector the destination blockchain.
/// @param linkedNftAddr the linked nft address at the destination.
function setupOne(uint64 linkedSelector, address linkedNftAddr) external onlyFactory {
require(nftSupportedChains.length > 0, "set wrapped nft itself first");
nftSupportedChains.push(linkedSelector);
linkedNfts[linkedSelector] = linkedNftAddr;
}
/// @notice Lint the last added nft across all blockchains.
/// It works if there are at least three supported nfts.
function lintLast(uint256 budget) external onlyFactory returns(uint256) {
if (nftSupportedChains.length <= 2) {
return budget;
}
// the first selector is this contract.
// the last one added and linted automatically to previous ones.
uint64 lastSelector = nftSupportedChains[nftSupportedChains.length - 1];
address lastNftAddr = linkedNfts[lastSelector];
for (uint256 i = 1; i < nftSupportedChains.length - 1; i++) {
uint64 destSelector = nftSupportedChains[i];
// Linked NFT will accept this method to add nft on a new blockchain.
bytes memory data = abi.encodeWithSignature("setupOne(uint64,address)", lastSelector, lastNftAddr);
Client.EVM2AnyMessage memory message = Client.EVM2AnyMessage({
receiver: abi.encode(linkedNfts[destSelector]),
data: data,
tokenAmounts: new Client.EVMTokenAmount[](0),
extraArgs: "",
feeToken: address(0)
});
uint256 fee = IRouterClient(router).getFee(
destSelector,
message
);
require(budget >= fee, "insufficient linting balance");
budget -= fee;
bytes32 messageId = IRouterClient(router).ccipSend{value: fee}(
destSelector,
message
);
emit X_SetupOne(destSelector, lastNftAddr, messageId);
}
return budget;
}
function calculateLinting() external view returns(uint256) {
if (nftSupportedChains.length <= 1) {
return 0;
}
// the first selector is this contract.
// the last one added and linted automatically to previous ones.
uint64 lastSelector = nftSupportedChains[nftSupportedChains.length-1];
address lastNftAddr = linkedNfts[lastSelector];
uint256 totalFee = 0;
for (uint256 i = 1; i < nftSupportedChains.length; i++) {
uint64 destSelector = nftSupportedChains[i];
// Linked NFT will accept this method to add nft on a new blockchain.
bytes memory data = abi.encodeWithSignature("setupOne(uint64,address)", lastSelector, lastNftAddr);
Client.EVM2AnyMessage memory message = Client.EVM2AnyMessage({
receiver: abi.encode(linkedNfts[destSelector]),
data: data,
tokenAmounts: new Client.EVMTokenAmount[](0),
extraArgs: "",
feeToken: address(0)
});
totalFee += IRouterClient(router).getFee(
destSelector,
message
);
}
return totalFee;
}
function allNfts() external view returns(uint64[] memory, address[] memory) {
address[] memory linkedNftAddrs = new address[](nftSupportedChains.length);
for (uint256 i = 0; i < nftSupportedChains.length; i++) {
linkedNftAddrs[i] = linkedNfts[nftSupportedChains[i]];
}
return (nftSupportedChains, linkedNftAddrs);
}
////////////////////////////////////////////////////////////////////////////
//
// Bridging process
//
////////////////////////////////////////////////////////////////////////////
// Unlock the nft
function bridge(uint256 nftId, address to) public onlySource {
_burn(nftId);
originalNft.safeTransferFrom(address(this), to, nftId);
}
function bridge(uint256 nftId, uint64 chainSelector) external nftOwner(nftId) validDestination(chainSelector) payable {
require(_ownerOf(nftId) == address(0), "wrapped nft exists");
originalNft.safeTransferFrom(msg.sender, address(this), nftId);
string memory uri = originalNft.tokenURI(nftId);
// pre-compute the linked address
address linkedAddr = linkedNfts[chainSelector];
Client.EVM2AnyMessage memory message = Client.EVM2AnyMessage({
receiver: abi.encode(linkedAddr),
data: abi.encodeWithSignature("bridge(uint256,address,string)", nftId, msg.sender, uri),
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
);
_mint(msg.sender, nftId);
_setTokenURI(nftId, uri);
emit X_Bridge(chainSelector, nftId, msg.sender, messageId);
}
function calculateBridgeFee(uint256 nftId, uint64 chainSelector) external view returns(uint256) {
string memory uri = originalNft.tokenURI(nftId);
// pre-compute the linked address
address linkedAddr = linkedNfts[chainSelector];
Client.EVM2AnyMessage memory message = Client.EVM2AnyMessage({
receiver: abi.encode(linkedAddr),
data: abi.encodeWithSignature("bridge(uint256,address,string)", nftId, msg.sender, uri),
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(sender == linkedNfts[message.sourceChainSelector], "not valid source");
(bool success, ) = address(this).call(message.data);
require(success);
sender = address(0);
}
////////////////////////////////////////////////////////////////////////////
//
// It's a locked nft.
//
////////////////////////////////////////////////////////////////////////////
function _safeTransfer(address, address, uint256, bytes memory) internal pure override {
revert();
}
function transferFrom(address, address, uint256) public pure override(ERC721, IERC721) {
revert();
}
function approve(address, uint256) public pure override(ERC721, IERC721) {
revert();
}
function setApprovalForAll(address, bool) public pure override(ERC721, IERC721) {
revert();
}
/// @dev encrypt token data
function onERC721Received(
address operator,
address from,
uint256 tokenId,
bytes memory data
)
public
override
returns (bytes4)
{
//only receive the _nft staff
if (address(this) != operator) {
//invalid from nft
return 0;
}
//success
emit NftReceived(operator, from, tokenId, data);
return bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"));
}
function supportsInterface(bytes4 interfaceId) public view override(ERC721URIStorage, CCIPReceiver) returns (bool) {
if (ERC721URIStorage.supportsInterface(interfaceId)) {
return true;
}
return CCIPReceiver.supportsInterface(interfaceId);
}
}