Skip to content

Commit 140e513

Browse files
YashYash
authored andcommitted
added bundleId variable, and some checks
1 parent 3279e56 commit 140e513

File tree

6 files changed

+757
-640
lines changed

6 files changed

+757
-640
lines changed

contracts/feature/TokenBundle.sol

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

66
abstract contract TokenBundle is ITokenBundle {
7-
uint256 public nextTokenIdToMint;
7+
uint256 public bundleId;
88
mapping(uint256=>BundleInfo) public bundle;
9+
mapping(uint256=>bool) public deletedBundle;
910

10-
function getTokenCount(uint256 tokenId) public view returns (uint256) {
11-
return bundle[tokenId].count;
11+
// function getNextTokenId() public virtual returns (uint256);
12+
13+
function getTokenCount(uint256 _bundleId) public view returns (uint256) {
14+
return bundle[_bundleId].count;
1215
}
1316

14-
function getToken(uint256 tokenId, uint256 index) public view returns (Token memory) {
15-
return bundle[tokenId].tokens[index];
17+
function getToken(uint256 _bundleId, uint256 index) public view returns (Token memory) {
18+
return bundle[_bundleId].tokens[index];
1619
}
1720

18-
function getUri(uint256 tokenId) public view returns (string memory) {
19-
return bundle[tokenId].uri;
21+
function getUri(uint256 _bundleId) public view returns (string memory) {
22+
return bundle[_bundleId].uri;
2023
}
2124

22-
function _getNextTokenId() internal returns (uint256 nextTokenId) {
23-
nextTokenId = nextTokenIdToMint;
24-
nextTokenIdToMint += 1;
25+
function getNextBundleId() public view returns (uint256) {
26+
return bundleId;
2527
}
2628

27-
function _setBundle(Token[] calldata _tokensToBind, uint256 tokenId) internal {
29+
function _setBundle(Token[] calldata _tokensToBind) internal returns (uint256 _bundleId) {
30+
require(_tokensToBind.length > 0, "no tokens to bind");
2831
for (uint256 i = 0; i < _tokensToBind.length; i += 1) {
29-
bundle[tokenId].tokens[i] = _tokensToBind[i];
32+
bundle[bundleId].tokens[i] = _tokensToBind[i];
3033
}
31-
bundle[tokenId].count = _tokensToBind.length;
34+
bundle[bundleId].count = _tokensToBind.length;
35+
_bundleId = bundleId;
36+
bundleId += 1;
3237
}
3338

34-
function _setBundleToken(Token memory _tokenToBind, uint256 tokenId, uint256 index) internal {
35-
bundle[tokenId].tokens[index] = _tokenToBind;
36-
bundle[tokenId].count += 1;
39+
function _resetBundle(Token[] calldata _tokensToBind, uint256 _bundleId) internal {
40+
require(_bundleId < bundleId, "bundle doesn't exist");
41+
require(!deletedBundle[_bundleId], "this bundle was deleted");
42+
require(_tokensToBind.length > 0, "no tokens to bind");
43+
for (uint256 i = 0; i < _tokensToBind.length; i += 1) {
44+
bundle[_bundleId].tokens[i] = _tokensToBind[i];
45+
}
46+
bundle[_bundleId].count = _tokensToBind.length;
3747
}
3848

39-
function _updateBundleToken(Token memory _tokenToBind, uint256 tokenId, uint256 index) internal {
40-
bundle[tokenId].tokens[index] = _tokenToBind;
49+
function _setBundleToken(Token memory _tokenToBind, uint256 _bundleId, uint256 index, bool update) internal {
50+
require(_bundleId < bundleId, "bundle doesn't exist");
51+
require(!deletedBundle[_bundleId], "this bundle was deleted");
52+
bundle[_bundleId].tokens[index] = _tokenToBind;
53+
bundle[_bundleId].count += update ? 0 : 1;
4154
}
4255

43-
function _setUri(string calldata _uri, uint256 tokenId) internal {
44-
bundle[tokenId].uri = _uri;
56+
function _setUri(string calldata _uri, uint256 _bundleId) internal {
57+
require(_bundleId < bundleId, "bundle doesn't exist");
58+
require(!deletedBundle[_bundleId], "this bundle was deleted");
59+
bundle[_bundleId].uri = _uri;
4560
}
4661

47-
function _deleteBundle(uint256 tokenId) internal {
48-
delete bundle[tokenId];
62+
function _deleteBundle(uint256 _bundleId) internal {
63+
require(_bundleId < bundleId, "bundle doesn't exist");
64+
require(!deletedBundle[_bundleId], "already deleted");
65+
66+
delete bundle[_bundleId];
67+
deletedBundle[_bundleId] = true;
4968
}
5069
}

contracts/multiwrap/TempMultiwrap.sol

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ contract TempMultiwrap is
6464
address private immutable nativeTokenWrapper;
6565

6666
/// @dev The next token ID of the NFT to mint.
67-
// uint256 public nextTokenIdToMint;
67+
uint256 public nextTokenIdToMint;
6868
//mychange
6969

7070
/// @dev The (default) address that receives all royalty value.
@@ -94,6 +94,9 @@ contract TempMultiwrap is
9494
//mychange
9595
// mapping(uint256=>BundleInfo) private bundle;
9696

97+
//mychange
98+
mapping(uint256 => uint256) private tokenToBundle;
99+
97100
/*///////////////////////////////////////////////////////////////
98101
Constructor + initializer logic
99102
//////////////////////////////////////////////////////////////*/
@@ -171,7 +174,7 @@ contract TempMultiwrap is
171174
/// @dev Returns the URI for a given tokenId.
172175
function tokenURI(uint256 _tokenId) public view override returns (string memory) {
173176
// return uri[_tokenId]; //mychange
174-
return getUri(_tokenId);
177+
return getUri(tokenToBundle[_tokenId]);
175178
}
176179

177180
/// @dev See ERC 165
@@ -210,12 +213,12 @@ contract TempMultiwrap is
210213
string calldata _uriForWrappedToken,
211214
address _recipient
212215
) external payable nonReentrant onlyMinter returns (uint256 tokenId) {
213-
// tokenId = nextTokenIdToMint;
214-
// nextTokenIdToMint += 1;
216+
tokenId = nextTokenIdToMint;
217+
nextTokenIdToMint += 1;
215218
//mychange
216-
tokenId = _getNextTokenId();
219+
// tokenId = _getNextTokenId();
217220

218-
_setBundle(_wrappedContents, tokenId); //mychange
221+
tokenToBundle[tokenId] = _setBundle(_wrappedContents); //mychange
219222

220223
//mychange
221224
// for (uint256 i = 0; i < _wrappedContents.length; i += 1) {
@@ -226,7 +229,7 @@ contract TempMultiwrap is
226229
//mychange
227230
// uri[tokenId] = _uriForWrappedToken;
228231

229-
_setUri(_uriForWrappedToken, tokenId);
232+
_setUri(_uriForWrappedToken, tokenToBundle[tokenId]);
230233

231234
_safeMint(_recipient, tokenId);
232235

@@ -248,17 +251,17 @@ contract TempMultiwrap is
248251

249252
// uint256 count = wrappedContents[_tokenId].count; //mychange
250253

251-
uint256 count = getTokenCount(_tokenId);
254+
uint256 count = getTokenCount(tokenToBundle[_tokenId]);
252255
Token[] memory tokensUnwrapped = new Token[](count);
253256

254257
for (uint256 i = 0; i < count; i += 1) {
255258
// tokensUnwrapped[i] = wrappedContents[_tokenId].token[i]; //mychange
256-
tokensUnwrapped[i] = getToken(_tokenId, i);
259+
tokensUnwrapped[i] = getToken(tokenToBundle[_tokenId], i);
257260
transferToken(address(this), _recipient, tokensUnwrapped[i]);
258261
}
259262

260263
// delete wrappedContents[_tokenId]; //mychange
261-
_deleteBundle(_tokenId);
264+
_deleteBundle(tokenToBundle[_tokenId]);
262265

263266
emit TokensUnwrapped(_msgSender(), _recipient, _tokenId, tokensUnwrapped);
264267
}
@@ -319,12 +322,12 @@ contract TempMultiwrap is
319322
/// @dev Returns the underlygin contents of a wrapped NFT.
320323
function getWrappedContents(uint256 _tokenId) external view returns (Token[] memory contents) {
321324
//mychange
322-
uint256 total = getTokenCount(_tokenId);
325+
uint256 total = getTokenCount(tokenToBundle[_tokenId]);
323326
contents = new Token[](total);
324327

325328
//mychange
326329
for(uint256 i = 0; i < total; i += 1) {
327-
contents[i] = getToken(_tokenId, i);
330+
contents[i] = getToken(tokenToBundle[_tokenId], i);
328331
}
329332
}
330333

contracts/pack/ITempPack.sol

Lines changed: 86 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,99 +1,99 @@
1-
// SPDX-License-Identifier: Apache-2.0
2-
pragma solidity ^0.8.11;
1+
// // SPDX-License-Identifier: Apache-2.0
2+
// pragma solidity ^0.8.11;
33

4-
import "../feature/interface/ITokenBundle.sol";
4+
// import "../feature/interface/ITokenBundle.sol";
55

6-
/**
7-
* The thirdweb `Pack` contract is a lootbox mechanism. An account can bundle up arbitrary ERC20, ERC721 and ERC1155 tokens into
8-
* a set of packs. A pack can then be opened in return for a selection of the tokens in the pack. The selection of tokens distributed
9-
* on opening a pack depends on the relative supply of all tokens in the packs.
10-
*/
6+
// /**
7+
// * The thirdweb `Pack` contract is a lootbox mechanism. An account can bundle up arbitrary ERC20, ERC721 and ERC1155 tokens into
8+
// * a set of packs. A pack can then be opened in return for a selection of the tokens in the pack. The selection of tokens distributed
9+
// * on opening a pack depends on the relative supply of all tokens in the packs.
10+
// */
1111

12-
interface ITempPack is ITokenBundle {
12+
// interface ITempPack is ITokenBundle {
1313

14-
/// @notice The types of tokens that can be added to packs.
15-
// enum TokenType { ERC20, ERC721, ERC1155 }
14+
// /// @notice The types of tokens that can be added to packs.
15+
// // enum TokenType { ERC20, ERC721, ERC1155 }
1616

17-
/**
18-
* @notice A unit of content i.e. a token in a pack.
19-
*
20-
* @param assetContract The contract address of the token.
21-
* @param tokenType The type of the token -- ERC20 / ERC721 / ERC1155
22-
* @param tokenId The tokenId of the the token, if applicable.
23-
* @param totalAmountPacked The total amount of this token packed in the pack.
24-
* @param amountPerUnit The amount of this token to distribute as a unit,
25-
* on opening a pack.
26-
*/
27-
// struct PackContent {
28-
// address assetContract;
29-
// TokenType tokenType;
30-
// uint256 tokenId;
31-
// uint256 totalAmountPacked;
32-
// uint256 amountPerUnit;
33-
// }
17+
// /**
18+
// * @notice A unit of content i.e. a token in a pack.
19+
// *
20+
// * @param assetContract The contract address of the token.
21+
// * @param tokenType The type of the token -- ERC20 / ERC721 / ERC1155
22+
// * @param tokenId The tokenId of the the token, if applicable.
23+
// * @param totalAmountPacked The total amount of this token packed in the pack.
24+
// * @param amountPerUnit The amount of this token to distribute as a unit,
25+
// * on opening a pack.
26+
// */
27+
// // struct PackContent {
28+
// // address assetContract;
29+
// // TokenType tokenType;
30+
// // uint256 tokenId;
31+
// // uint256 totalAmountPacked;
32+
// // uint256 amountPerUnit;
33+
// // }
3434

35-
/**
36-
* @notice All info relevant to packs.
37-
*
38-
* @param contents The reward units packed in the packs.
39-
* @param openStartTimestamp The timestamp after which packs can be opened.
40-
* @param amountDistributedPerOpen The number of reward units distributed per open.
41-
* @param packUri The metadata URI for packs.
42-
*/
43-
// struct PackInfo {
44-
// PackContent[] contents;
45-
// uint128 openStartTimestamp;
46-
// uint128 amountDistributedPerOpen;
47-
// string uri;
48-
// }
35+
// /**
36+
// * @notice All info relevant to packs.
37+
// *
38+
// * @param contents The reward units packed in the packs.
39+
// * @param openStartTimestamp The timestamp after which packs can be opened.
40+
// * @param amountDistributedPerOpen The number of reward units distributed per open.
41+
// * @param packUri The metadata URI for packs.
42+
// */
43+
// // struct PackInfo {
44+
// // PackContent[] contents;
45+
// // uint128 openStartTimestamp;
46+
// // uint128 amountDistributedPerOpen;
47+
// // string uri;
48+
// // }
4949

50-
//mychange
51-
//modified structs from IPack
52-
struct PackContent {
53-
Token token;
54-
uint256 amountPerUnit;
55-
}
50+
// //mychange
51+
// //modified structs from IPack
52+
// struct PackContent {
53+
// Token token;
54+
// uint256 amountPerUnit;
55+
// }
5656

57-
struct PackInfo {
58-
// BundleInfo bundle;
59-
uint128 openStartTimestamp;
60-
uint128 amountDistributedPerOpen;
61-
}
57+
// struct PackInfo {
58+
// // BundleInfo bundle;
59+
// uint128 openStartTimestamp;
60+
// uint128 amountDistributedPerOpen;
61+
// }
6262

63-
/// @notice Emitted when a set of packs is created.
64-
event PackCreated(uint256 indexed packId, address indexed packCreator, address recipient, PackInfo packInfo, uint256 totalPacksCreated);
63+
// /// @notice Emitted when a set of packs is created.
64+
// event PackCreated(uint256 indexed packId, address indexed packCreator, address recipient, PackInfo packInfo, uint256 totalPacksCreated);
6565

66-
/// @notice Emitted when a pack is opened.
67-
event PackOpened(uint256 indexed packId, address indexed opener, uint256 numOfPacksOpened, PackContent[] rewardUnitsDistributed);
66+
// /// @notice Emitted when a pack is opened.
67+
// event PackOpened(uint256 indexed packId, address indexed opener, uint256 numOfPacksOpened, PackContent[] rewardUnitsDistributed);
6868

69-
/// @dev Emitted when the owner is updated.
70-
event OwnerUpdated(address prevOwner, address newOwner);
69+
// /// @dev Emitted when the owner is updated.
70+
// event OwnerUpdated(address prevOwner, address newOwner);
7171

72-
/**
73-
* @notice Creates a pack with the stated contents.
74-
*
75-
* @param contents The reward units to pack in the packs.
76-
* @param packUri The (metadata) URI assigned to the packs created.
77-
* @param openStartTimestamp The timestamp after which packs can be opened.
78-
* @param amountDistributedPerOpen The number of reward units distributed per open.
79-
* @param recipient The recipient of the packs created.
80-
*
81-
* @return packId The unique identifer of the created set of packs.
82-
* @return packTotalSupply The total number of packs created.
83-
*/
84-
function createPack(
85-
PackContent[] calldata contents,
86-
string calldata packUri,
87-
uint128 openStartTimestamp,
88-
uint128 amountDistributedPerOpen,
89-
address recipient
90-
) external returns (uint256 packId, uint256 packTotalSupply);
72+
// /**
73+
// * @notice Creates a pack with the stated contents.
74+
// *
75+
// * @param contents The reward units to pack in the packs.
76+
// * @param packUri The (metadata) URI assigned to the packs created.
77+
// * @param openStartTimestamp The timestamp after which packs can be opened.
78+
// * @param amountDistributedPerOpen The number of reward units distributed per open.
79+
// * @param recipient The recipient of the packs created.
80+
// *
81+
// * @return packId The unique identifer of the created set of packs.
82+
// * @return packTotalSupply The total number of packs created.
83+
// */
84+
// function createPack(
85+
// PackContent[] calldata contents,
86+
// string calldata packUri,
87+
// uint128 openStartTimestamp,
88+
// uint128 amountDistributedPerOpen,
89+
// address recipient
90+
// ) external returns (uint256 packId, uint256 packTotalSupply);
9191

92-
/**
93-
* @notice Lets a pack owner open a pack and receive the pack's reward unit.
94-
*
95-
* @param packId The identifier of the pack to open.
96-
* @param amountToOpen The number of packs to open at once.
97-
*/
98-
function openPack(uint256 packId, uint256 amountToOpen) external;
99-
}
92+
// /**
93+
// * @notice Lets a pack owner open a pack and receive the pack's reward unit.
94+
// *
95+
// * @param packId The identifier of the pack to open.
96+
// * @param amountToOpen The number of packs to open at once.
97+
// */
98+
// function openPack(uint256 packId, uint256 amountToOpen) external;
99+
// }

0 commit comments

Comments
 (0)