Skip to content

Commit 3279e56

Browse files
YashYash
authored andcommitted
refactored TempMultiwrap and TempPack
1 parent 37f9237 commit 3279e56

File tree

9 files changed

+2136
-22
lines changed

9 files changed

+2136
-22
lines changed

contracts/feature/TokenBundle.sol

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,47 @@ pragma solidity ^0.8.0;
44
import "./interface/ITokenBundle.sol";
55

66
abstract contract TokenBundle is ITokenBundle {
7+
uint256 public nextTokenIdToMint;
8+
mapping(uint256=>BundleInfo) public bundle;
9+
10+
function getTokenCount(uint256 tokenId) public view returns (uint256) {
11+
return bundle[tokenId].count;
12+
}
13+
14+
function getToken(uint256 tokenId, uint256 index) public view returns (Token memory) {
15+
return bundle[tokenId].tokens[index];
16+
}
17+
18+
function getUri(uint256 tokenId) public view returns (string memory) {
19+
return bundle[tokenId].uri;
20+
}
721

22+
function _getNextTokenId() internal returns (uint256 nextTokenId) {
23+
nextTokenId = nextTokenIdToMint;
24+
nextTokenIdToMint += 1;
25+
}
26+
27+
function _setBundle(Token[] calldata _tokensToBind, uint256 tokenId) internal {
28+
for (uint256 i = 0; i < _tokensToBind.length; i += 1) {
29+
bundle[tokenId].tokens[i] = _tokensToBind[i];
30+
}
31+
bundle[tokenId].count = _tokensToBind.length;
32+
}
33+
34+
function _setBundleToken(Token memory _tokenToBind, uint256 tokenId, uint256 index) internal {
35+
bundle[tokenId].tokens[index] = _tokenToBind;
36+
bundle[tokenId].count += 1;
37+
}
38+
39+
function _updateBundleToken(Token memory _tokenToBind, uint256 tokenId, uint256 index) internal {
40+
bundle[tokenId].tokens[index] = _tokenToBind;
41+
}
42+
43+
function _setUri(string calldata _uri, uint256 tokenId) internal {
44+
bundle[tokenId].uri = _uri;
45+
}
46+
47+
function _deleteBundle(uint256 tokenId) internal {
48+
delete bundle[tokenId];
49+
}
850
}

contracts/feature/interface/ITokenBundle.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ interface ITokenBundle {
1313
address assetContract;
1414
TokenType tokenType;
1515
uint256 tokenId;
16-
uint256 totalAmountPacked;
16+
uint256 totalAmount;
1717

1818
}
1919

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
pragma solidity ^0.8.11;
3+
4+
import "../feature/interface/ITokenBundle.sol";
5+
6+
/**
7+
* Thirdweb's Multiwrap contract lets you wrap arbitrary ERC20, ERC721 and ERC1155
8+
* tokens you own into a single wrapped token / NFT.
9+
*
10+
* A wrapped NFT can be unwrapped i.e. burned in exchange for its underlying contents.
11+
*/
12+
13+
interface ITempMultiwrap is ITokenBundle {
14+
/// @notice The type of assets that can be wrapped.
15+
// enum TokenType {
16+
// ERC20,
17+
// ERC721,
18+
// ERC1155
19+
// }
20+
21+
/**
22+
* @notice A generic interface to describe a token to wrap.
23+
*
24+
* @param assetContract The contract address of the asset to wrap.
25+
* @param tokenType The token type (ERC20 / ERC721 / ERC1155) of the asset to wrap.
26+
* @param tokenId The token Id of the asset to wrap, if the asset is an ERC721 / ERC1155 NFT.
27+
* @param amount The amount of the asset to wrap, if the asset is an ERC20 / ERC1155 fungible token.
28+
*/
29+
// struct Token {
30+
// address assetContract;
31+
// TokenType tokenType;
32+
// uint256 tokenId;
33+
// uint256 amount;
34+
// }
35+
36+
/**
37+
* @notice An internal data structure to track the wrapped contents of a wrapped NFT.
38+
*
39+
* @param count The total kinds of assets i.e. `Token` wrapped.
40+
* @param token Mapping from a UID -> to the asset i.e. `Token` at that UID.
41+
*/
42+
// struct WrappedContents {
43+
// uint256 count;
44+
// mapping(uint256 => Token) token;
45+
// }
46+
47+
/// @dev Emitted when tokens are wrapped.
48+
event TokensWrapped(
49+
address indexed wrapper,
50+
address indexed recipientOfWrappedToken,
51+
uint256 indexed tokenIdOfWrappedToken,
52+
Token[] wrappedContents
53+
);
54+
55+
/// @dev Emitted when tokens are unwrapped.
56+
event TokensUnwrapped(
57+
address indexed unwrapper,
58+
address indexed recipientOfWrappedContents,
59+
uint256 indexed tokenIdOfWrappedToken,
60+
Token[] wrappedContents
61+
);
62+
63+
/// @dev Emitted when the contract owner is updated.
64+
event OwnerUpdated(address prevOwner, address newOwner);
65+
66+
/**
67+
* @notice Wrap multiple ERC1155, ERC721, ERC20 tokens into a single wrapped NFT.
68+
*
69+
* @param wrappedContents The tokens to wrap.
70+
* @param uriForWrappedToken The metadata URI for the wrapped NFT.
71+
* @param recipient The recipient of the wrapped NFT.
72+
*/
73+
function wrap(
74+
Token[] memory wrappedContents,
75+
string calldata uriForWrappedToken,
76+
address recipient
77+
) external payable returns (uint256 tokenId);
78+
79+
/**
80+
* @notice Unwrap a wrapped NFT to retrieve underlying ERC1155, ERC721, ERC20 tokens.
81+
*
82+
* @param tokenId The token Id of the wrapped NFT to unwrap.
83+
* @param recipient The recipient of the underlying ERC1155, ERC721, ERC20 tokens of the wrapped NFT.
84+
*/
85+
function unwrap(uint256 tokenId, address recipient) external;
86+
}

contracts/multiwrap/TempMultiwrap.sol

Lines changed: 45 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,20 @@ import "@openzeppelin/contracts-upgradeable/utils/MulticallUpgradeable.sol";
1616
import "@openzeppelin/contracts-upgradeable/access/AccessControlEnumerableUpgradeable.sol";
1717
import "@openzeppelin/contracts-upgradeable/security/ReentrancyGuardUpgradeable.sol";
1818

19+
// ========== Feature imports ==========//mychange
20+
import "../feature/TokenBundle.sol";
21+
1922
// ========== Internal imports ==========
2023

2124
import "../interfaces/IThirdwebContract.sol";
2225
import "../interfaces/IThirdwebRoyalty.sol";
2326
import "../interfaces/IThirdwebOwnable.sol";
2427

25-
import "../interfaces/IMultiwrap.sol";
28+
import "./ITempMultiwrap.sol";
2629
import "../lib/CurrencyTransferLib.sol";
2730
import "../openzeppelin-presets/metatx/ERC2771ContextUpgradeable.sol";
2831

29-
contract Multiwrap is
32+
contract TempMultiwrap is
3033
IThirdwebContract,
3134
IThirdwebOwnable,
3235
IThirdwebRoyalty,
@@ -37,8 +40,9 @@ contract Multiwrap is
3740
ERC1155HolderUpgradeable,
3841
ERC721HolderUpgradeable,
3942
ERC721Upgradeable,
40-
IMultiwrap
41-
{
43+
ITempMultiwrap,
44+
TokenBundle
45+
{//mychange TokenBundle
4246
/*///////////////////////////////////////////////////////////////
4347
State variables
4448
//////////////////////////////////////////////////////////////*/
@@ -60,7 +64,8 @@ contract Multiwrap is
6064
address private immutable nativeTokenWrapper;
6165

6266
/// @dev The next token ID of the NFT to mint.
63-
uint256 public nextTokenIdToMint;
67+
// uint256 public nextTokenIdToMint;
68+
//mychange
6469

6570
/// @dev The (default) address that receives all royalty value.
6671
address private royaltyRecipient;
@@ -85,7 +90,9 @@ contract Multiwrap is
8590
mapping(uint256 => string) private uri;
8691

8792
/// @dev Mapping from tokenId of wrapped NFT => wrapped contents of the token.
88-
mapping(uint256 => WrappedContents) private wrappedContents;
93+
// mapping(uint256 => WrappedContents) private wrappedContents;
94+
//mychange
95+
// mapping(uint256=>BundleInfo) private bundle;
8996

9097
/*///////////////////////////////////////////////////////////////
9198
Constructor + initializer logic
@@ -163,7 +170,8 @@ contract Multiwrap is
163170

164171
/// @dev Returns the URI for a given tokenId.
165172
function tokenURI(uint256 _tokenId) public view override returns (string memory) {
166-
return uri[_tokenId];
173+
// return uri[_tokenId]; //mychange
174+
return getUri(_tokenId);
167175
}
168176

169177
/// @dev See ERC 165
@@ -202,15 +210,23 @@ contract Multiwrap is
202210
string calldata _uriForWrappedToken,
203211
address _recipient
204212
) external payable nonReentrant onlyMinter returns (uint256 tokenId) {
205-
tokenId = nextTokenIdToMint;
206-
nextTokenIdToMint += 1;
213+
// tokenId = nextTokenIdToMint;
214+
// nextTokenIdToMint += 1;
215+
//mychange
216+
tokenId = _getNextTokenId();
207217

208-
for (uint256 i = 0; i < _wrappedContents.length; i += 1) {
209-
wrappedContents[tokenId].token[i] = _wrappedContents[i];
210-
}
211-
wrappedContents[tokenId].count = _wrappedContents.length;
218+
_setBundle(_wrappedContents, tokenId); //mychange
219+
220+
//mychange
221+
// for (uint256 i = 0; i < _wrappedContents.length; i += 1) {
222+
// bundle[tokenId].tokens[i] = _wrappedContents[i];
223+
// }
224+
// wrappedContents[tokenId].count = _wrappedContents.length;
212225

213-
uri[tokenId] = _uriForWrappedToken;
226+
//mychange
227+
// uri[tokenId] = _uriForWrappedToken;
228+
229+
_setUri(_uriForWrappedToken, tokenId);
214230

215231
_safeMint(_recipient, tokenId);
216232

@@ -230,15 +246,19 @@ contract Multiwrap is
230246

231247
_burn(_tokenId);
232248

233-
uint256 count = wrappedContents[_tokenId].count;
249+
// uint256 count = wrappedContents[_tokenId].count; //mychange
250+
251+
uint256 count = getTokenCount(_tokenId);
234252
Token[] memory tokensUnwrapped = new Token[](count);
235253

236254
for (uint256 i = 0; i < count; i += 1) {
237-
tokensUnwrapped[i] = wrappedContents[_tokenId].token[i];
255+
// tokensUnwrapped[i] = wrappedContents[_tokenId].token[i]; //mychange
256+
tokensUnwrapped[i] = getToken(_tokenId, i);
238257
transferToken(address(this), _recipient, tokensUnwrapped[i]);
239258
}
240259

241-
delete wrappedContents[_tokenId];
260+
// delete wrappedContents[_tokenId]; //mychange
261+
_deleteBundle(_tokenId);
242262

243263
emit TokensUnwrapped(_msgSender(), _recipient, _tokenId, tokensUnwrapped);
244264
}
@@ -254,14 +274,16 @@ contract Multiwrap is
254274
_token.assetContract,
255275
_from,
256276
_to,
257-
_token.amount,
277+
_token.totalAmount,
258278
nativeTokenWrapper
259279
);
260280
} else if (_token.tokenType == TokenType.ERC721) {
261281
IERC721Upgradeable(_token.assetContract).safeTransferFrom(_from, _to, _token.tokenId);
262282
} else if (_token.tokenType == TokenType.ERC1155) {
263-
IERC1155Upgradeable(_token.assetContract).safeTransferFrom(_from, _to, _token.tokenId, _token.amount, "");
283+
IERC1155Upgradeable(_token.assetContract).safeTransferFrom(_from, _to, _token.tokenId, _token.totalAmount, "");
264284
}
285+
286+
//mychange _token.amount now _token.totalAmount
265287
}
266288

267289
/// @dev Transfers multiple arbitrary ERC20 / ERC721 / ERC1155 tokens.
@@ -296,11 +318,13 @@ contract Multiwrap is
296318

297319
/// @dev Returns the underlygin contents of a wrapped NFT.
298320
function getWrappedContents(uint256 _tokenId) external view returns (Token[] memory contents) {
299-
uint256 total = wrappedContents[_tokenId].count;
321+
//mychange
322+
uint256 total = getTokenCount(_tokenId);
300323
contents = new Token[](total);
301324

325+
//mychange
302326
for(uint256 i = 0; i < total; i += 1) {
303-
contents[i] = wrappedContents[_tokenId].token[i];
327+
contents[i] = getToken(_tokenId, i);
304328
}
305329
}
306330

0 commit comments

Comments
 (0)