Skip to content

Commit

Permalink
run prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
Krishang Nadgauda authored and Krishang Nadgauda committed Apr 1, 2022
1 parent b0e754e commit b5ea712
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 82 deletions.
14 changes: 7 additions & 7 deletions contracts/interfaces/IMultiwrap.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@ import "./IThirdwebOwnable.sol";
* Thirdweb's Multiwrap contract lets you wrap arbitrary ERC20, ERC721 and ERC1155
* tokens you own into a single wrapped token / NFT.
*
* A wrapped NFT can be unwrapped i.e. burned in exchange for its underlying contents.
* A wrapped NFT can be unwrapped i.e. burned in exchange for its underlying contents.
*/

interface IMultiwrap is IThirdwebContract, IThirdwebOwnable, IThirdwebRoyalty {

/// @notice The type of assets that can be wrapped.
enum TokenType { ERC20, ERC721, ERC1155 }
enum TokenType {
ERC20,
ERC721,
ERC1155
}

/**
* @notice A generic interface to describe a token to wrap.
Expand Down Expand Up @@ -81,8 +84,5 @@ interface IMultiwrap is IThirdwebContract, IThirdwebOwnable, IThirdwebRoyalty {
* @param tokenId The token Id of the wrapped NFT to unwrap.
* @param recipient The recipient of the underlying ERC1155, ERC721, ERC20 tokens of the wrapped NFT.
*/
function unwrap(
uint256 tokenId,
address recipient
) external;
function unwrap(uint256 tokenId, address recipient) external;
}
31 changes: 18 additions & 13 deletions contracts/multiwrap/Multiwrap.sol
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ contract Multiwrap is
modifier onlyMinter() {
// if transfer is restricted on the contract, we still want to allow burning and minting
if (!hasRole(MINTER_ROLE, address(0))) {
require(hasRole(MINTER_ROLE, _msgSender()) , "restricted to MINTER_ROLE holders.");
require(hasRole(MINTER_ROLE, _msgSender()), "restricted to MINTER_ROLE holders.");
}

_;
Expand Down Expand Up @@ -195,7 +195,7 @@ contract Multiwrap is
tokenId = nextTokenIdToMint;
nextTokenIdToMint += 1;

for(uint256 i = 0; i < _wrappedContents.length; i += 1) {
for (uint256 i = 0; i < _wrappedContents.length; i += 1) {
wrappedContents[tokenId].token[i] = _wrappedContents[i];
}
wrappedContents[tokenId].count = _wrappedContents.length;
Expand All @@ -210,10 +210,7 @@ contract Multiwrap is
}

/// @dev Unwrap a wrapped NFT to retrieve underlying ERC1155, ERC721, ERC20 tokens.
function unwrap(
uint256 _tokenId,
address _recipient
) external nonReentrant {
function unwrap(uint256 _tokenId, address _recipient) external nonReentrant {
require(_tokenId < nextTokenIdToMint, "invalid tokenId");
require(_isApprovedOrOwner(_msgSender(), _tokenId), "unapproved called");

Expand All @@ -222,7 +219,7 @@ contract Multiwrap is
uint256 count = wrappedContents[_tokenId].count;
Token[] memory tokensUnwrapped = new Token[](count);

for(uint256 i = 0; i < count; i += 1) {
for (uint256 i = 0; i < count; i += 1) {
tokensUnwrapped[i] = wrappedContents[_tokenId].token[i];
transferToken(address(this), _recipient, tokensUnwrapped[i]);
}
Expand All @@ -233,25 +230,33 @@ contract Multiwrap is
}

/// @dev Transfers an arbitrary ERC20 / ERC721 / ERC1155 token.
function transferToken(address _from, address _to, Token memory _token) internal {
if(_token.tokenType == TokenType.ERC20) {
function transferToken(
address _from,
address _to,
Token memory _token
) internal {
if (_token.tokenType == TokenType.ERC20) {
CurrencyTransferLib.transferCurrencyWithWrapper(
_token.assetContract,
_from,
_to,
_token.amount,
nativeTokenWrapper
);
} else if(_token.tokenType == TokenType.ERC721) {
} else if (_token.tokenType == TokenType.ERC721) {
IERC721Upgradeable(_token.assetContract).safeTransferFrom(_from, _to, _token.tokenId);
} else if(_token.tokenType == TokenType.ERC1155) {
} else if (_token.tokenType == TokenType.ERC1155) {
IERC1155Upgradeable(_token.assetContract).safeTransferFrom(_from, _to, _token.tokenId, _token.amount, "");
}
}

/// @dev Transfers multiple arbitrary ERC20 / ERC721 / ERC1155 tokens.
function transferTokenBatch(address _from, address _to, Token[] memory _tokens) internal {
for(uint256 i = 0; i < _tokens.length; i += 1) {
function transferTokenBatch(
address _from,
address _to,
Token[] memory _tokens
) internal {
for (uint256 i = 0; i < _tokens.length; i += 1) {
transferToken(_from, _to, _tokens[i]);
}
}
Expand Down
56 changes: 32 additions & 24 deletions src/test/Multiwrap.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -71,24 +71,30 @@ contract MultiwrapTest is BaseTest, IMultiwrapData {
tokenOwner.setApprovalForAllERC1155(address(erc1155), address(multiwrap), true);

// Prepare wrapped contents.
wrappedContents.push(IMultiwrap.Token({
assetContract: address(erc20),
tokenType: IMultiwrap.TokenType.ERC20,
tokenId: 0,
amount: erc20Amount
}));
wrappedContents.push(IMultiwrap.Token({
assetContract: address(erc721),
tokenType: IMultiwrap.TokenType.ERC721,
tokenId: erc721TokenId,
amount: 1
}));
wrappedContents.push(IMultiwrap.Token({
assetContract: address(erc1155),
tokenType: IMultiwrap.TokenType.ERC1155,
tokenId: erc1155TokenId,
amount: erc1155Amount
}));
wrappedContents.push(
IMultiwrap.Token({
assetContract: address(erc20),
tokenType: IMultiwrap.TokenType.ERC20,
tokenId: 0,
amount: erc20Amount
})
);
wrappedContents.push(
IMultiwrap.Token({
assetContract: address(erc721),
tokenType: IMultiwrap.TokenType.ERC721,
tokenId: erc721TokenId,
amount: 1
})
);
wrappedContents.push(
IMultiwrap.Token({
assetContract: address(erc1155),
tokenType: IMultiwrap.TokenType.ERC1155,
tokenId: erc1155TokenId,
amount: erc1155Amount
})
);
}

// ===== Initial state =====
Expand All @@ -112,7 +118,6 @@ contract MultiwrapTest is BaseTest, IMultiwrapData {

/// @dev Test `wrap`
function test_wrap() public {

assertEq(erc20.balanceOf(address(tokenOwner)), erc20Amount);
assertEq(erc721.ownerOf(erc721TokenId), address(tokenOwner));
assertEq(erc1155.balanceOf(address(tokenOwner), erc1155TokenId), erc1155Amount);
Expand Down Expand Up @@ -194,7 +199,7 @@ contract MultiwrapTest is BaseTest, IMultiwrapData {
uint256 tokenIdOfWrapped = multiwrap.nextTokenIdToMint();

IMultiwrap.Token[] memory contents = new IMultiwrap.Token[](wrappedContents.length);
for(uint256 i = 0; i < wrappedContents.length; i += 1) {
for (uint256 i = 0; i < wrappedContents.length; i += 1) {
contents[i] = wrappedContents[i];
}

Expand All @@ -207,7 +212,6 @@ contract MultiwrapTest is BaseTest, IMultiwrapData {

/// @dev Test `unwrap`


function test_unwrap() public {
uint256 tokenIdOfWrapped = multiwrap.nextTokenIdToMint();

Expand Down Expand Up @@ -246,20 +250,24 @@ contract MultiwrapTest is BaseTest, IMultiwrapData {
multiwrap.unwrap(invalidId, address(wrappedTokenRecipient));
}


function test_unwrap_emit_Unwrapped() public {
uint256 tokenIdOfWrapped = multiwrap.nextTokenIdToMint();

vm.prank(address(tokenOwner));
multiwrap.wrap(wrappedContents, uriForWrappedToken, address(wrappedTokenRecipient));

IMultiwrap.Token[] memory contents = new IMultiwrap.Token[](wrappedContents.length);
for(uint256 i = 0; i < wrappedContents.length; i += 1) {
for (uint256 i = 0; i < wrappedContents.length; i += 1) {
contents[i] = wrappedContents[i];
}

vm.expectEmit(true, true, true, true);
emit TokensUnwrapped(address(wrappedTokenRecipient), address(wrappedTokenRecipient), tokenIdOfWrapped, contents);
emit TokensUnwrapped(
address(wrappedTokenRecipient),
address(wrappedTokenRecipient),
tokenIdOfWrapped,
contents
);

vm.prank(address(wrappedTokenRecipient));
multiwrap.unwrap(tokenIdOfWrapped, address(wrappedTokenRecipient));
Expand Down
87 changes: 49 additions & 38 deletions src/test/benchmark/MultiwrapBenchmark.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -64,48 +64,59 @@ contract MultiwrapBenchmarkTest is BaseTest {

// Prepare wrapped contents.

for(uint256 i = 0; i < 5; i += 1) {
fiveERC721NFts.push(IMultiwrap.Token({
for (uint256 i = 0; i < 5; i += 1) {
fiveERC721NFts.push(
IMultiwrap.Token({
assetContract: address(erc721),
tokenType: IMultiwrap.TokenType.ERC721,
tokenId: i,
amount: 1
})
);
}

wrappedContents.push(
IMultiwrap.Token({
assetContract: address(erc20),
tokenType: IMultiwrap.TokenType.ERC20,
tokenId: 0,
amount: erc20Amount
})
);
wrappedContents.push(
IMultiwrap.Token({
assetContract: address(erc721),
tokenType: IMultiwrap.TokenType.ERC721,
tokenId: i,
tokenId: erc721TokenId,
amount: 1
}));

}
})
);
wrappedContents.push(
IMultiwrap.Token({
assetContract: address(erc1155),
tokenType: IMultiwrap.TokenType.ERC1155,
tokenId: erc1155TokenId,
amount: erc1155Amount
})
);

oneERC721NFTWithERC20Token.push(
IMultiwrap.Token({
assetContract: address(erc20),
tokenType: IMultiwrap.TokenType.ERC20,
tokenId: 0,
amount: erc20Amount
})
);
oneERC721NFTWithERC20Token.push(
IMultiwrap.Token({
assetContract: address(erc721),
tokenType: IMultiwrap.TokenType.ERC721,
tokenId: erc721TokenId,
amount: 1
})
);

wrappedContents.push(IMultiwrap.Token({
assetContract: address(erc20),
tokenType: IMultiwrap.TokenType.ERC20,
tokenId: 0,
amount: erc20Amount
}));
wrappedContents.push(IMultiwrap.Token({
assetContract: address(erc721),
tokenType: IMultiwrap.TokenType.ERC721,
tokenId: erc721TokenId,
amount: 1
}));
wrappedContents.push(IMultiwrap.Token({
assetContract: address(erc1155),
tokenType: IMultiwrap.TokenType.ERC1155,
tokenId: erc1155TokenId,
amount: erc1155Amount
}));

oneERC721NFTWithERC20Token.push(IMultiwrap.Token({
assetContract: address(erc20),
tokenType: IMultiwrap.TokenType.ERC20,
tokenId: 0,
amount: erc20Amount
}));
oneERC721NFTWithERC20Token.push(IMultiwrap.Token({
assetContract: address(erc721),
tokenType: IMultiwrap.TokenType.ERC721,
tokenId: erc721TokenId,
amount: 1
}));

vm.startPrank(address(tokenOwner));
}

Expand Down

0 comments on commit b5ea712

Please sign in to comment.