Skip to content

Commit

Permalink
added bundleId variable, and some checks
Browse files Browse the repository at this point in the history
  • Loading branch information
Yash authored and Yash committed May 6, 2022
1 parent 3279e56 commit 140e513
Show file tree
Hide file tree
Showing 6 changed files with 757 additions and 640 deletions.
63 changes: 41 additions & 22 deletions contracts/feature/TokenBundle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,66 @@ pragma solidity ^0.8.0;
import "./interface/ITokenBundle.sol";

abstract contract TokenBundle is ITokenBundle {
uint256 public nextTokenIdToMint;
uint256 public bundleId;
mapping(uint256=>BundleInfo) public bundle;
mapping(uint256=>bool) public deletedBundle;

function getTokenCount(uint256 tokenId) public view returns (uint256) {
return bundle[tokenId].count;
// function getNextTokenId() public virtual returns (uint256);

function getTokenCount(uint256 _bundleId) public view returns (uint256) {
return bundle[_bundleId].count;
}

function getToken(uint256 tokenId, uint256 index) public view returns (Token memory) {
return bundle[tokenId].tokens[index];
function getToken(uint256 _bundleId, uint256 index) public view returns (Token memory) {
return bundle[_bundleId].tokens[index];
}

function getUri(uint256 tokenId) public view returns (string memory) {
return bundle[tokenId].uri;
function getUri(uint256 _bundleId) public view returns (string memory) {
return bundle[_bundleId].uri;
}

function _getNextTokenId() internal returns (uint256 nextTokenId) {
nextTokenId = nextTokenIdToMint;
nextTokenIdToMint += 1;
function getNextBundleId() public view returns (uint256) {
return bundleId;
}

function _setBundle(Token[] calldata _tokensToBind, uint256 tokenId) internal {
function _setBundle(Token[] calldata _tokensToBind) internal returns (uint256 _bundleId) {
require(_tokensToBind.length > 0, "no tokens to bind");
for (uint256 i = 0; i < _tokensToBind.length; i += 1) {
bundle[tokenId].tokens[i] = _tokensToBind[i];
bundle[bundleId].tokens[i] = _tokensToBind[i];
}
bundle[tokenId].count = _tokensToBind.length;
bundle[bundleId].count = _tokensToBind.length;
_bundleId = bundleId;
bundleId += 1;
}

function _setBundleToken(Token memory _tokenToBind, uint256 tokenId, uint256 index) internal {
bundle[tokenId].tokens[index] = _tokenToBind;
bundle[tokenId].count += 1;
function _resetBundle(Token[] calldata _tokensToBind, uint256 _bundleId) internal {
require(_bundleId < bundleId, "bundle doesn't exist");
require(!deletedBundle[_bundleId], "this bundle was deleted");
require(_tokensToBind.length > 0, "no tokens to bind");
for (uint256 i = 0; i < _tokensToBind.length; i += 1) {
bundle[_bundleId].tokens[i] = _tokensToBind[i];
}
bundle[_bundleId].count = _tokensToBind.length;
}

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

function _setUri(string calldata _uri, uint256 tokenId) internal {
bundle[tokenId].uri = _uri;
function _setUri(string calldata _uri, uint256 _bundleId) internal {
require(_bundleId < bundleId, "bundle doesn't exist");
require(!deletedBundle[_bundleId], "this bundle was deleted");
bundle[_bundleId].uri = _uri;
}

function _deleteBundle(uint256 tokenId) internal {
delete bundle[tokenId];
function _deleteBundle(uint256 _bundleId) internal {
require(_bundleId < bundleId, "bundle doesn't exist");
require(!deletedBundle[_bundleId], "already deleted");

delete bundle[_bundleId];
deletedBundle[_bundleId] = true;
}
}
27 changes: 15 additions & 12 deletions contracts/multiwrap/TempMultiwrap.sol
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ contract TempMultiwrap is
address private immutable nativeTokenWrapper;

/// @dev The next token ID of the NFT to mint.
// uint256 public nextTokenIdToMint;
uint256 public nextTokenIdToMint;
//mychange

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

//mychange
mapping(uint256 => uint256) private tokenToBundle;

/*///////////////////////////////////////////////////////////////
Constructor + initializer logic
//////////////////////////////////////////////////////////////*/
Expand Down Expand Up @@ -171,7 +174,7 @@ contract TempMultiwrap is
/// @dev Returns the URI for a given tokenId.
function tokenURI(uint256 _tokenId) public view override returns (string memory) {
// return uri[_tokenId]; //mychange
return getUri(_tokenId);
return getUri(tokenToBundle[_tokenId]);
}

/// @dev See ERC 165
Expand Down Expand Up @@ -210,12 +213,12 @@ contract TempMultiwrap is
string calldata _uriForWrappedToken,
address _recipient
) external payable nonReentrant onlyMinter returns (uint256 tokenId) {
// tokenId = nextTokenIdToMint;
// nextTokenIdToMint += 1;
tokenId = nextTokenIdToMint;
nextTokenIdToMint += 1;
//mychange
tokenId = _getNextTokenId();
// tokenId = _getNextTokenId();

_setBundle(_wrappedContents, tokenId); //mychange
tokenToBundle[tokenId] = _setBundle(_wrappedContents); //mychange

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

_setUri(_uriForWrappedToken, tokenId);
_setUri(_uriForWrappedToken, tokenToBundle[tokenId]);

_safeMint(_recipient, tokenId);

Expand All @@ -248,17 +251,17 @@ contract TempMultiwrap is

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

uint256 count = getTokenCount(_tokenId);
uint256 count = getTokenCount(tokenToBundle[_tokenId]);
Token[] memory tokensUnwrapped = new Token[](count);

for (uint256 i = 0; i < count; i += 1) {
// tokensUnwrapped[i] = wrappedContents[_tokenId].token[i]; //mychange
tokensUnwrapped[i] = getToken(_tokenId, i);
tokensUnwrapped[i] = getToken(tokenToBundle[_tokenId], i);
transferToken(address(this), _recipient, tokensUnwrapped[i]);
}

// delete wrappedContents[_tokenId]; //mychange
_deleteBundle(_tokenId);
_deleteBundle(tokenToBundle[_tokenId]);

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

//mychange
for(uint256 i = 0; i < total; i += 1) {
contents[i] = getToken(_tokenId, i);
contents[i] = getToken(tokenToBundle[_tokenId], i);
}
}

Expand Down
172 changes: 86 additions & 86 deletions contracts/pack/ITempPack.sol
Original file line number Diff line number Diff line change
@@ -1,99 +1,99 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.11;
// // SPDX-License-Identifier: Apache-2.0
// pragma solidity ^0.8.11;

import "../feature/interface/ITokenBundle.sol";
// import "../feature/interface/ITokenBundle.sol";

/**
* The thirdweb `Pack` contract is a lootbox mechanism. An account can bundle up arbitrary ERC20, ERC721 and ERC1155 tokens into
* 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
* on opening a pack depends on the relative supply of all tokens in the packs.
*/
// /**
// * The thirdweb `Pack` contract is a lootbox mechanism. An account can bundle up arbitrary ERC20, ERC721 and ERC1155 tokens into
// * 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
// * on opening a pack depends on the relative supply of all tokens in the packs.
// */

interface ITempPack is ITokenBundle {
// interface ITempPack is ITokenBundle {

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

/**
* @notice A unit of content i.e. a token in a pack.
*
* @param assetContract The contract address of the token.
* @param tokenType The type of the token -- ERC20 / ERC721 / ERC1155
* @param tokenId The tokenId of the the token, if applicable.
* @param totalAmountPacked The total amount of this token packed in the pack.
* @param amountPerUnit The amount of this token to distribute as a unit,
* on opening a pack.
*/
// struct PackContent {
// address assetContract;
// TokenType tokenType;
// uint256 tokenId;
// uint256 totalAmountPacked;
// uint256 amountPerUnit;
// }
// /**
// * @notice A unit of content i.e. a token in a pack.
// *
// * @param assetContract The contract address of the token.
// * @param tokenType The type of the token -- ERC20 / ERC721 / ERC1155
// * @param tokenId The tokenId of the the token, if applicable.
// * @param totalAmountPacked The total amount of this token packed in the pack.
// * @param amountPerUnit The amount of this token to distribute as a unit,
// * on opening a pack.
// */
// // struct PackContent {
// // address assetContract;
// // TokenType tokenType;
// // uint256 tokenId;
// // uint256 totalAmountPacked;
// // uint256 amountPerUnit;
// // }

/**
* @notice All info relevant to packs.
*
* @param contents The reward units packed in the packs.
* @param openStartTimestamp The timestamp after which packs can be opened.
* @param amountDistributedPerOpen The number of reward units distributed per open.
* @param packUri The metadata URI for packs.
*/
// struct PackInfo {
// PackContent[] contents;
// uint128 openStartTimestamp;
// uint128 amountDistributedPerOpen;
// string uri;
// }
// /**
// * @notice All info relevant to packs.
// *
// * @param contents The reward units packed in the packs.
// * @param openStartTimestamp The timestamp after which packs can be opened.
// * @param amountDistributedPerOpen The number of reward units distributed per open.
// * @param packUri The metadata URI for packs.
// */
// // struct PackInfo {
// // PackContent[] contents;
// // uint128 openStartTimestamp;
// // uint128 amountDistributedPerOpen;
// // string uri;
// // }

//mychange
//modified structs from IPack
struct PackContent {
Token token;
uint256 amountPerUnit;
}
// //mychange
// //modified structs from IPack
// struct PackContent {
// Token token;
// uint256 amountPerUnit;
// }

struct PackInfo {
// BundleInfo bundle;
uint128 openStartTimestamp;
uint128 amountDistributedPerOpen;
}
// struct PackInfo {
// // BundleInfo bundle;
// uint128 openStartTimestamp;
// uint128 amountDistributedPerOpen;
// }

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

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

/// @dev Emitted when the owner is updated.
event OwnerUpdated(address prevOwner, address newOwner);
// /// @dev Emitted when the owner is updated.
// event OwnerUpdated(address prevOwner, address newOwner);

/**
* @notice Creates a pack with the stated contents.
*
* @param contents The reward units to pack in the packs.
* @param packUri The (metadata) URI assigned to the packs created.
* @param openStartTimestamp The timestamp after which packs can be opened.
* @param amountDistributedPerOpen The number of reward units distributed per open.
* @param recipient The recipient of the packs created.
*
* @return packId The unique identifer of the created set of packs.
* @return packTotalSupply The total number of packs created.
*/
function createPack(
PackContent[] calldata contents,
string calldata packUri,
uint128 openStartTimestamp,
uint128 amountDistributedPerOpen,
address recipient
) external returns (uint256 packId, uint256 packTotalSupply);
// /**
// * @notice Creates a pack with the stated contents.
// *
// * @param contents The reward units to pack in the packs.
// * @param packUri The (metadata) URI assigned to the packs created.
// * @param openStartTimestamp The timestamp after which packs can be opened.
// * @param amountDistributedPerOpen The number of reward units distributed per open.
// * @param recipient The recipient of the packs created.
// *
// * @return packId The unique identifer of the created set of packs.
// * @return packTotalSupply The total number of packs created.
// */
// function createPack(
// PackContent[] calldata contents,
// string calldata packUri,
// uint128 openStartTimestamp,
// uint128 amountDistributedPerOpen,
// address recipient
// ) external returns (uint256 packId, uint256 packTotalSupply);

/**
* @notice Lets a pack owner open a pack and receive the pack's reward unit.
*
* @param packId The identifier of the pack to open.
* @param amountToOpen The number of packs to open at once.
*/
function openPack(uint256 packId, uint256 amountToOpen) external;
}
// /**
// * @notice Lets a pack owner open a pack and receive the pack's reward unit.
// *
// * @param packId The identifier of the pack to open.
// * @param amountToOpen The number of packs to open at once.
// */
// function openPack(uint256 packId, uint256 amountToOpen) external;
// }
Loading

0 comments on commit 140e513

Please sign in to comment.