Skip to content

Commit

Permalink
Wrap can send to different receiver
Browse files Browse the repository at this point in the history
  • Loading branch information
ScreamingHawk committed May 22, 2024
1 parent 5b152e8 commit 9a6bde9
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
6 changes: 3 additions & 3 deletions src/tokens/wrappers/clawback/Clawback.sol
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ contract Clawback is ERC1155MintBurn, ERC1155Metadata, IClawback {
}

/// @inheritdoc IClawbackFunctions
function wrap(uint24 templateId, TokenType tokenType, address tokenAddr, uint256 tokenId, uint256 amount)
function wrap(uint24 templateId, TokenType tokenType, address tokenAddr, uint256 tokenId, uint256 amount, address receiver)
public
returns (uint256 wrappedTokenId)
{
Expand All @@ -56,9 +56,9 @@ contract Clawback is ERC1155MintBurn, ERC1155Metadata, IClawback {

// solhint-disable-next-line not-rely-on-time
_tokenDetails[wrappedTokenId] = TokenDetails(templateId, uint96(block.timestamp), tokenType, tokenAddr, tokenId);
_mint(sender, wrappedTokenId, amount, "");
_mint(receiver, wrappedTokenId, amount, "");

emit Wrapped(wrappedTokenId, templateId, tokenAddr, tokenId, amount, sender);
emit Wrapped(wrappedTokenId, templateId, tokenAddr, tokenId, amount, sender, receiver);
}

/// @inheritdoc IClawbackFunctions
Expand Down
6 changes: 4 additions & 2 deletions src/tokens/wrappers/clawback/IClawback.sol
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ interface IClawbackFunctions {
* @param tokenAddr The token address.
* @param tokenId The token ID.
* @param amount The amount to wrap.
* @param receiver The receiver of the wrapped token.
* @return wrappedTokenId The wrapped token ID.
*/
function wrap(uint24 templateId, TokenType tokenType, address tokenAddr, uint256 tokenId, uint256 amount)
function wrap(uint24 templateId, TokenType tokenType, address tokenAddr, uint256 tokenId, uint256 amount, address receiver)
external
returns (uint256 wrappedTokenId);

Expand Down Expand Up @@ -156,7 +157,8 @@ interface IClawbackSignals {
address tokenAddr,
uint256 tokenId,
uint256 amount,
address sender
address sender,
address receiver
);

/// @notice Emits when a token is unwrapped
Expand Down
20 changes: 11 additions & 9 deletions test/tokens/wrappers/clawback/Clawback.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ contract ClawbackTest is Test, IClawbackSignals, IERC1155TokenReceiver, IERC721T
//
// Wrap
//
function testWrap(address templateAdmin, uint8 tokenTypeNum, uint256 tokenId, uint256 amount) public {
function testWrap(address templateAdmin, uint8 tokenTypeNum, uint256 tokenId, uint256 amount, address receiver) public safeAddress(receiver) {
IClawbackFunctions.TokenType tokenType = _toTokenType(tokenTypeNum);
address tokenAddr;
(tokenAddr, tokenId, amount) = _validParams(tokenType, tokenId, amount);
Expand All @@ -287,13 +287,15 @@ contract ClawbackTest is Test, IClawbackSignals, IERC1155TokenReceiver, IERC721T
uint24 templateId = testAddTemplate(templateAdmin, 1, false, false);

vm.expectEmit(true, true, true, true, address(clawback));
emit Wrapped(0, templateId, tokenAddr, tokenId, amount, address(this));
clawback.wrap(templateId, tokenType, tokenAddr, tokenId, amount);
emit Wrapped(0, templateId, tokenAddr, tokenId, amount, address(this), receiver);
uint256 wrappedTokenId = clawback.wrap(templateId, tokenType, tokenAddr, tokenId, amount, receiver);

assertEq(IGenericToken(tokenAddr).balanceOf(receiver, tokenId), 0);
assertEq(IGenericToken(tokenAddr).balanceOf(address(this), tokenId), 0);
assertEq(IGenericToken(tokenAddr).balanceOf(address(clawback), tokenId), amount);
IClawbackFunctions.TokenDetails memory details = clawback.getTokenDetails(0);
assertEq(clawback.balanceOf(address(this), 0), amount);
assertEq(clawback.balanceOf(receiver, wrappedTokenId), amount);
assertEq(clawback.balanceOf(address(this), wrappedTokenId), 0);
assertEq(details.templateId, templateId);
assertEq(details.lockedAt, uint96(block.timestamp));
assertEq(uint8(details.tokenType), uint8(tokenType));
Expand Down Expand Up @@ -326,7 +328,7 @@ contract ClawbackTest is Test, IClawbackSignals, IERC1155TokenReceiver, IERC721T
assertFalse(success);
}

function testWrapInvalidAmount(address templateAdmin, uint8 tokenTypeNum, uint256 tokenId, uint256 amount) public {
function testWrapInvalidAmount(address templateAdmin, uint8 tokenTypeNum, uint256 tokenId, uint256 amount, address receiver) public {
IClawbackFunctions.TokenType tokenType = _toTokenType(tokenTypeNum);
address tokenAddr;
(tokenAddr, tokenId, amount) = _validParams(tokenType, tokenId, amount);
Expand All @@ -343,10 +345,10 @@ contract ClawbackTest is Test, IClawbackSignals, IERC1155TokenReceiver, IERC721T
amount = 0;

vm.expectRevert(InvalidTokenTransfer.selector);
clawback.wrap(templateId, tokenType, tokenAddr, tokenId, amount);
clawback.wrap(templateId, tokenType, tokenAddr, tokenId, amount, receiver);
}

function testWrapInvalidTemplate(uint24 templateId, uint8 tokenTypeNum, uint256 tokenId, uint256 amount) public {
function testWrapInvalidTemplate(uint24 templateId, uint8 tokenTypeNum, uint256 tokenId, uint256 amount, address receiver) public {
IClawbackFunctions.TokenType tokenType = _toTokenType(tokenTypeNum);
address tokenAddr;
(tokenAddr, tokenId, amount) = _validParams(tokenType, tokenId, amount);
Expand All @@ -355,7 +357,7 @@ contract ClawbackTest is Test, IClawbackSignals, IERC1155TokenReceiver, IERC721T
IGenericToken(tokenAddr).approve(address(this), address(clawback), tokenId, amount);

vm.expectRevert(InvalidTemplate.selector);
clawback.wrap(templateId, tokenType, tokenAddr, tokenId, amount);
clawback.wrap(templateId, tokenType, tokenAddr, tokenId, amount, receiver);
}

//
Expand Down Expand Up @@ -385,7 +387,7 @@ contract ClawbackTest is Test, IClawbackSignals, IERC1155TokenReceiver, IERC721T
IGenericToken(result.tokenAddr).approve(address(this), address(clawback), result.tokenId, result.amount);

result.wrappedTokenId =
clawback.wrap(result.templateId, tokenType, result.tokenAddr, result.tokenId, result.amount);
clawback.wrap(result.templateId, tokenType, result.tokenAddr, result.tokenId, result.amount, address(this));

// struct here prevents stack too deep during coverage reporting
return result;
Expand Down

0 comments on commit 9a6bde9

Please sign in to comment.