Skip to content

Commit

Permalink
feat: add data param in _before and _after token transfer hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
CJ42 committed Oct 13, 2023
1 parent 2b475b2 commit cc3f5a7
Show file tree
Hide file tree
Showing 17 changed files with 172 additions and 108 deletions.
20 changes: 12 additions & 8 deletions contracts/LSP7DigitalAsset/LSP7DigitalAssetCore.sol
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ abstract contract LSP7DigitalAssetCore is ILSP7DigitalAsset {
revert LSP7CannotSendWithAddressZero();
}

_beforeTokenTransfer(address(0), to, amount);
_beforeTokenTransfer(address(0), to, amount, data);

// tokens being minted
_existingTokens += amount;
Expand All @@ -400,7 +400,7 @@ abstract contract LSP7DigitalAssetCore is ILSP7DigitalAsset {

emit Transfer(msg.sender, address(0), to, amount, force, data);

_afterTokenTransfer(address(0), to, amount);
_afterTokenTransfer(address(0), to, amount, data);

bytes memory lsp1Data = abi.encode(address(0), to, amount, data);
_notifyTokenReceiver(to, force, lsp1Data);
Expand Down Expand Up @@ -445,7 +445,7 @@ abstract contract LSP7DigitalAssetCore is ILSP7DigitalAsset {
revert LSP7AmountExceedsBalance(balance, from, amount);
}

_beforeTokenTransfer(from, address(0), amount);
_beforeTokenTransfer(from, address(0), amount, data);

// tokens being burnt
_existingTokens -= amount;
Expand All @@ -462,7 +462,7 @@ abstract contract LSP7DigitalAssetCore is ILSP7DigitalAsset {
data: data
});

_afterTokenTransfer(from, address(0), amount);
_afterTokenTransfer(from, address(0), amount, data);

bytes memory lsp1Data = abi.encode(from, address(0), amount, data);
_notifyTokenSender(from, lsp1Data);
Expand Down Expand Up @@ -551,14 +551,14 @@ abstract contract LSP7DigitalAssetCore is ILSP7DigitalAsset {
revert LSP7AmountExceedsBalance(balance, from, amount);
}

_beforeTokenTransfer(from, to, amount);
_beforeTokenTransfer(from, to, amount, data);

_tokenOwnerBalances[from] -= amount;
_tokenOwnerBalances[to] += amount;

emit Transfer(msg.sender, from, to, amount, force, data);

_afterTokenTransfer(from, to, amount);
_afterTokenTransfer(from, to, amount, data);

bytes memory lsp1Data = abi.encode(from, to, amount, data);

Expand All @@ -573,11 +573,13 @@ abstract contract LSP7DigitalAssetCore is ILSP7DigitalAsset {
* @param from The sender address
* @param to The recipient address
* @param amount The amount of token to transfer
* @param data The data sent alongside the transfer
*/
function _beforeTokenTransfer(
address from,
address to,
uint256 amount // solhint-disable-next-line no-empty-blocks
uint256 amount,
bytes memory data // solhint-disable-next-line no-empty-blocks
) internal virtual {}

/**
Expand All @@ -587,11 +589,13 @@ abstract contract LSP7DigitalAssetCore is ILSP7DigitalAsset {
* @param from The sender address
* @param to The recipient address
* @param amount The amount of token to transfer
* @param data The data sent alongside the transfer
*/
function _afterTokenTransfer(
address from,
address to,
uint256 amount // solhint-disable-next-line no-empty-blocks
uint256 amount,
bytes memory data // solhint-disable-next-line no-empty-blocks
) internal virtual {}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ abstract contract LSP8IdentifiableDigitalAssetCore is
revert LSP8CannotSendToAddressZero();
}

_beforeTokenTransfer(address(0), to, tokenId);
_beforeTokenTransfer(address(0), to, tokenId, data);

// Check that `tokenId` was not minted inside the `_beforeTokenTransfer` hook
if (_exists(tokenId)) {
Expand All @@ -388,7 +388,7 @@ abstract contract LSP8IdentifiableDigitalAssetCore is

emit Transfer(msg.sender, address(0), to, tokenId, force, data);

_afterTokenTransfer(address(0), to, tokenId);
_afterTokenTransfer(address(0), to, tokenId, data);

bytes memory lsp1Data = abi.encode(address(0), to, tokenId, data);
_notifyTokenReceiver(to, force, lsp1Data);
Expand Down Expand Up @@ -419,7 +419,7 @@ abstract contract LSP8IdentifiableDigitalAssetCore is
function _burn(bytes32 tokenId, bytes memory data) internal virtual {
address tokenOwner = tokenOwnerOf(tokenId);

_beforeTokenTransfer(tokenOwner, address(0), tokenId);
_beforeTokenTransfer(tokenOwner, address(0), tokenId, data);

// Re-fetch and update `tokenOwner` in case `tokenId`
// was transferred inside the `_beforeTokenTransfer` hook
Expand All @@ -435,7 +435,7 @@ abstract contract LSP8IdentifiableDigitalAssetCore is

emit Transfer(msg.sender, tokenOwner, address(0), tokenId, false, data);

_afterTokenTransfer(tokenOwner, address(0), tokenId);
_afterTokenTransfer(tokenOwner, address(0), tokenId, data);

bytes memory lsp1Data = abi.encode(
tokenOwner,
Expand Down Expand Up @@ -491,7 +491,7 @@ abstract contract LSP8IdentifiableDigitalAssetCore is
revert LSP8CannotSendToAddressZero();
}

_beforeTokenTransfer(from, to, tokenId);
_beforeTokenTransfer(from, to, tokenId, data);

// Re-fetch and update `tokenOwner` in case `tokenId`
// was transferred inside the `_beforeTokenTransfer` hook
Expand All @@ -505,7 +505,7 @@ abstract contract LSP8IdentifiableDigitalAssetCore is

emit Transfer(msg.sender, from, to, tokenId, force, data);

_afterTokenTransfer(from, to, tokenId);
_afterTokenTransfer(from, to, tokenId, data);

bytes memory lsp1Data = abi.encode(from, to, tokenId, data);

Expand All @@ -520,11 +520,13 @@ abstract contract LSP8IdentifiableDigitalAssetCore is
* @param from The sender address
* @param to The recipient address
* @param tokenId The tokenId to transfer
* @param data The data sent alongside the transfer
*/
function _beforeTokenTransfer(
address from,
address to,
bytes32 tokenId // solhint-disable-next-line no-empty-blocks
bytes32 tokenId,
bytes memory data // solhint-disable-next-line no-empty-blocks
) internal virtual {}

/**
Expand All @@ -534,11 +536,13 @@ abstract contract LSP8IdentifiableDigitalAssetCore is
* @param from The sender address
* @param to The recipient address
* @param tokenId The tokenId to transfer
* @param data The data sent alongside the transfer
*/
function _afterTokenTransfer(
address from,
address to,
bytes32 tokenId // solhint-disable-next-line no-empty-blocks
bytes32 tokenId,
bytes memory data // solhint-disable-next-line no-empty-blocks
) internal virtual {}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,14 @@ abstract contract LSP8Enumerable is LSP8IdentifiableDigitalAsset {
* @param from The address sending the `tokenId` (`address(0)` when `tokenId` is being minted).
* @param to The address receiving the `tokenId` (`address(0)` when `tokenId` is being burnt).
* @param tokenId The bytes32 identifier of the token being transferred.
* @param data The data sent alongside the the token transfer.
*/
function _beforeTokenTransfer(
address from,
address to,
bytes32 tokenId
) internal virtual override(LSP8IdentifiableDigitalAssetCore) {
bytes32 tokenId,
bytes memory data
) internal virtual override {
// `tokenId` being minted
if (from == address(0)) {
uint256 index = totalSupply();
Expand All @@ -65,6 +67,6 @@ abstract contract LSP8Enumerable is LSP8IdentifiableDigitalAsset {
delete _tokenIndex[tokenId];
}

super._beforeTokenTransfer(from, to, tokenId);
super._beforeTokenTransfer(from, to, tokenId, data);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,14 @@ abstract contract LSP8EnumerableInitAbstract is
* @param from The address sending the `tokenId` (`address(0)` when `tokenId` is being minted).
* @param to The address receiving the `tokenId` (`address(0)` when `tokenId` is being burnt).
* @param tokenId The bytes32 identifier of the token being transferred.
* @param data The data sent alongside the the token transfer.
*/
function _beforeTokenTransfer(
address from,
address to,
bytes32 tokenId
) internal virtual override(LSP8IdentifiableDigitalAssetCore) {
bytes32 tokenId,
bytes memory data
) internal virtual override {
if (from == address(0)) {
uint256 index = totalSupply();
_indexToken[index] = tokenId;
Expand All @@ -60,6 +62,6 @@ abstract contract LSP8EnumerableInitAbstract is
delete _indexToken[lastIndex];
delete _tokenIndex[tokenId];
}
super._beforeTokenTransfer(from, to, tokenId);
super._beforeTokenTransfer(from, to, tokenId, data);
}
}
28 changes: 16 additions & 12 deletions docs/contracts/LSP7DigitalAsset/LSP7DigitalAsset.md
Original file line number Diff line number Diff line change
Expand Up @@ -994,7 +994,8 @@ all the parameters in the calldata packed encoded.
function _beforeTokenTransfer(
address from,
address to,
uint256 amount
uint256 amount,
bytes data
) internal nonpayable;
```

Expand All @@ -1003,11 +1004,12 @@ Allows to run custom logic before updating balances and notifiying sender/recipi

#### Parameters

| Name | Type | Description |
| -------- | :-------: | ------------------------------- |
| `from` | `address` | The sender address |
| `to` | `address` | The recipient address |
| `amount` | `uint256` | The amount of token to transfer |
| Name | Type | Description |
| -------- | :-------: | ------------------------------------ |
| `from` | `address` | The sender address |
| `to` | `address` | The recipient address |
| `amount` | `uint256` | The amount of token to transfer |
| `data` | `bytes` | The data sent alongside the transfer |

<br/>

Expand All @@ -1017,7 +1019,8 @@ Allows to run custom logic before updating balances and notifiying sender/recipi
function _afterTokenTransfer(
address from,
address to,
uint256 amount
uint256 amount,
bytes data
) internal nonpayable;
```

Expand All @@ -1026,11 +1029,12 @@ Allows to run custom logic after updating balances, but **before notifiying send

#### Parameters

| Name | Type | Description |
| -------- | :-------: | ------------------------------- |
| `from` | `address` | The sender address |
| `to` | `address` | The recipient address |
| `amount` | `uint256` | The amount of token to transfer |
| Name | Type | Description |
| -------- | :-------: | ------------------------------------ |
| `from` | `address` | The sender address |
| `to` | `address` | The recipient address |
| `amount` | `uint256` | The amount of token to transfer |
| `data` | `bytes` | The data sent alongside the transfer |

<br/>

Expand Down
28 changes: 16 additions & 12 deletions docs/contracts/LSP7DigitalAsset/extensions/LSP7Burnable.md
Original file line number Diff line number Diff line change
Expand Up @@ -1019,7 +1019,8 @@ all the parameters in the calldata packed encoded.
function _beforeTokenTransfer(
address from,
address to,
uint256 amount
uint256 amount,
bytes data
) internal nonpayable;
```

Expand All @@ -1028,11 +1029,12 @@ Allows to run custom logic before updating balances and notifiying sender/recipi

#### Parameters

| Name | Type | Description |
| -------- | :-------: | ------------------------------- |
| `from` | `address` | The sender address |
| `to` | `address` | The recipient address |
| `amount` | `uint256` | The amount of token to transfer |
| Name | Type | Description |
| -------- | :-------: | ------------------------------------ |
| `from` | `address` | The sender address |
| `to` | `address` | The recipient address |
| `amount` | `uint256` | The amount of token to transfer |
| `data` | `bytes` | The data sent alongside the transfer |

<br/>

Expand All @@ -1042,7 +1044,8 @@ Allows to run custom logic before updating balances and notifiying sender/recipi
function _afterTokenTransfer(
address from,
address to,
uint256 amount
uint256 amount,
bytes data
) internal nonpayable;
```

Expand All @@ -1051,11 +1054,12 @@ Allows to run custom logic after updating balances, but **before notifiying send

#### Parameters

| Name | Type | Description |
| -------- | :-------: | ------------------------------- |
| `from` | `address` | The sender address |
| `to` | `address` | The recipient address |
| `amount` | `uint256` | The amount of token to transfer |
| Name | Type | Description |
| -------- | :-------: | ------------------------------------ |
| `from` | `address` | The sender address |
| `to` | `address` | The recipient address |
| `amount` | `uint256` | The amount of token to transfer |
| `data` | `bytes` | The data sent alongside the transfer |

<br/>

Expand Down
28 changes: 16 additions & 12 deletions docs/contracts/LSP7DigitalAsset/extensions/LSP7CappedSupply.md
Original file line number Diff line number Diff line change
Expand Up @@ -993,7 +993,8 @@ all the parameters in the calldata packed encoded.
function _beforeTokenTransfer(
address from,
address to,
uint256 amount
uint256 amount,
bytes data
) internal nonpayable;
```

Expand All @@ -1002,11 +1003,12 @@ Allows to run custom logic before updating balances and notifiying sender/recipi

#### Parameters

| Name | Type | Description |
| -------- | :-------: | ------------------------------- |
| `from` | `address` | The sender address |
| `to` | `address` | The recipient address |
| `amount` | `uint256` | The amount of token to transfer |
| Name | Type | Description |
| -------- | :-------: | ------------------------------------ |
| `from` | `address` | The sender address |
| `to` | `address` | The recipient address |
| `amount` | `uint256` | The amount of token to transfer |
| `data` | `bytes` | The data sent alongside the transfer |

<br/>

Expand All @@ -1016,7 +1018,8 @@ Allows to run custom logic before updating balances and notifiying sender/recipi
function _afterTokenTransfer(
address from,
address to,
uint256 amount
uint256 amount,
bytes data
) internal nonpayable;
```

Expand All @@ -1025,11 +1028,12 @@ Allows to run custom logic after updating balances, but **before notifiying send

#### Parameters

| Name | Type | Description |
| -------- | :-------: | ------------------------------- |
| `from` | `address` | The sender address |
| `to` | `address` | The recipient address |
| `amount` | `uint256` | The amount of token to transfer |
| Name | Type | Description |
| -------- | :-------: | ------------------------------------ |
| `from` | `address` | The sender address |
| `to` | `address` | The recipient address |
| `amount` | `uint256` | The amount of token to transfer |
| `data` | `bytes` | The data sent alongside the transfer |

<br/>

Expand Down
Loading

0 comments on commit cc3f5a7

Please sign in to comment.