Skip to content

Commit

Permalink
fix: add receive() function in LSP7 & LSP8 to silent compiler warni…
Browse files Browse the repository at this point in the history
…ng (#711)

* refactor: add `receive()` in LSP7 & LSP8

* docs: update token docs

* test: update for the new behaivior
  • Loading branch information
b00ste authored Sep 27, 2023
1 parent ec58800 commit e6fb55d
Show file tree
Hide file tree
Showing 24 changed files with 751 additions and 131 deletions.
14 changes: 14 additions & 0 deletions contracts/LSP7DigitalAsset/LSP7DigitalAsset.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {LSP2Utils} from "../LSP2ERC725YJSONSchema/LSP2Utils.sol";

// constants
import {_INTERFACEID_LSP7} from "./LSP7Constants.sol";
import {LSP7TokenContractCannotHoldValue} from "./LSP7Errors.sol";

import {
_LSP17_EXTENSION_PREFIX
Expand Down Expand Up @@ -91,6 +92,19 @@ abstract contract LSP7DigitalAsset is
return _fallbackLSP17Extendable(callData);
}

/**
* @dev Reverts whenever someone tries to send native tokens to a LSP7 contract.
* @notice LSP7 contract cannot receive native tokens.
*/
receive() external payable virtual {
// revert on empty calls with no value
if (msg.value == 0) {
revert InvalidFunctionSelector(hex"00000000");
}

revert LSP7TokenContractCannotHoldValue();
}

/**
* @dev Forwards the call with the received value to an extension mapped to a function selector.
*
Expand Down
11 changes: 7 additions & 4 deletions contracts/LSP7DigitalAsset/LSP7DigitalAssetCore.sol
Original file line number Diff line number Diff line change
Expand Up @@ -555,13 +555,16 @@ abstract contract LSP7DigitalAssetCore is ILSP7DigitalAsset {
_INTERFACEID_LSP1
)
) {
operator.call(
abi.encodeWithSelector(
ILSP1UniversalReceiver.universalReceiver.selector,
try
ILSP1UniversalReceiver(operator).universalReceiver(
_TYPEID_LSP7_TOKENOPERATOR,
lsp1Data
)
);
{
return;
} catch {
return;
}
}
}

Expand Down
14 changes: 14 additions & 0 deletions contracts/LSP7DigitalAsset/LSP7DigitalAssetInitAbstract.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {LSP2Utils} from "../LSP2ERC725YJSONSchema/LSP2Utils.sol";

// constants
import {_INTERFACEID_LSP7} from "./LSP7Constants.sol";
import {LSP7TokenContractCannotHoldValue} from "./LSP7Errors.sol";

import {
_LSP17_EXTENSION_PREFIX
Expand Down Expand Up @@ -86,6 +87,19 @@ abstract contract LSP7DigitalAssetInitAbstract is
return _fallbackLSP17Extendable(callData);
}

/**
* @dev Reverts whenever someone tries to send native tokens to a LSP7 contract.
* @notice LSP7 contract cannot receive native tokens.
*/
receive() external payable virtual {
// revert on empty calls with no value
if (msg.value == 0) {
revert InvalidFunctionSelector(hex"00000000");
}

revert LSP7TokenContractCannotHoldValue();
}

/**
* @dev Forwards the call with the received value to an extension mapped to a function selector.
*
Expand Down
9 changes: 9 additions & 0 deletions contracts/LSP7DigitalAsset/LSP7Errors.sol
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,12 @@ error LSP7TokenOwnerCannotBeOperator();
* @dev Reverts when trying to decrease an operator's allowance to more than its current allowance.
*/
error LSP7DecreasedAllowanceBelowZero();

/**
* @dev Error occurs when sending native tokens to the LSP7 contract without sending any data.
*
* E.g. Sending value without passing a bytes4 function selector to call a LSP17 Extension.
*
* @notice LSP7 contract cannot receive native tokens.
*/
error LSP7TokenContractCannotHoldValue();
35 changes: 22 additions & 13 deletions contracts/LSP8IdentifiableDigitalAsset/LSP8Errors.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,70 +4,79 @@ pragma solidity ^0.8.4;
// --- Errors

/**
* @dev reverts when `tokenId` has not been minted.
* @dev Reverts when `tokenId` has not been minted.
*/
error LSP8NonExistentTokenId(bytes32 tokenId);

/**
* @dev reverts when `caller` is not the `tokenOwner` of the `tokenId`.
* @dev Reverts when `caller` is not the `tokenOwner` of the `tokenId`.
*/
error LSP8NotTokenOwner(address tokenOwner, bytes32 tokenId, address caller);

/**
* @dev reverts when `caller` is not an allowed operator for `tokenId`.
* @dev Reverts when `caller` is not an allowed operator for `tokenId`.
*/
error LSP8NotTokenOperator(bytes32 tokenId, address caller);

/**
* @dev reverts when `operator` is already authorized for the `tokenId`.
* @dev Reverts when `operator` is already authorized for the `tokenId`.
*/
error LSP8OperatorAlreadyAuthorized(address operator, bytes32 tokenId);

/**
* @dev reverts when trying to set the zero address as an operator.
* @dev Reverts when trying to set the zero address as an operator.
*/
error LSP8CannotUseAddressZeroAsOperator();

/**
* @dev reverts when trying to send token to the zero address.
* @dev Reverts when trying to send token to the zero address.
*/
error LSP8CannotSendToAddressZero();

/**
* @dev reverts when specifying the same address for `from` and `to` in a token transfer.
* @dev Reverts when specifying the same address for `from` and `to` in a token transfer.
*/
error LSP8CannotSendToSelf();

/**
* @dev reverts when `operator` is not an operator for the `tokenId`.
* @dev Reverts when `operator` is not an operator for the `tokenId`.
*/
error LSP8NonExistingOperator(address operator, bytes32 tokenId);

/**
* @dev reverts when `tokenId` has already been minted.
* @dev Reverts when `tokenId` has already been minted.
*/
error LSP8TokenIdAlreadyMinted(bytes32 tokenId);

/**
* @dev reverts when the parameters used for `transferBatch` have different lengths.
* @dev Reverts when the parameters used for `transferBatch` have different lengths.
*/
error LSP8InvalidTransferBatch();

/**
* @dev reverts if the `tokenReceiver` does not implement LSP1
* @dev Reverts if the `tokenReceiver` does not implement LSP1
* when minting or transferring tokens with `bool force` set as `false`.
*/
error LSP8NotifyTokenReceiverContractMissingLSP1Interface(
address tokenReceiver
);

/**
* @dev reverts if the `tokenReceiver` is an EOA
* @dev Reverts if the `tokenReceiver` is an EOA
* when minting or transferring tokens with `bool force` set as `false`.
*/
error LSP8NotifyTokenReceiverIsEOA(address tokenReceiver);

/**
* @dev reverts when trying to authorize or revoke the token's owner as an operator.
* @dev Reverts when trying to authorize or revoke the token's owner as an operator.
*/
error LSP8TokenOwnerCannotBeOperator();

/**
* @dev Error occurs when sending native tokens to the LSP8 contract without sending any data.
*
* E.g. Sending value without passing a bytes4 function selector to call a LSP17 Extension.
*
* @notice LSP8 contract cannot receive native tokens.
*/
error LSP8TokenContractCannotHoldValue();
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {LSP2Utils} from "../LSP2ERC725YJSONSchema/LSP2Utils.sol";

// constants
import {_INTERFACEID_LSP8} from "./LSP8Constants.sol";
import {LSP8TokenContractCannotHoldValue} from "./LSP8Errors.sol";

import {
_LSP17_EXTENSION_PREFIX
Expand Down Expand Up @@ -92,6 +93,19 @@ abstract contract LSP8IdentifiableDigitalAsset is
return _fallbackLSP17Extendable(callData);
}

/**
* @dev Reverts whenever someone tries to send native tokens to a LSP8 contract.
* @notice LSP8 contract cannot receive native tokens.
*/
receive() external payable virtual {
// revert on empty calls with no value
if (msg.value == 0) {
revert InvalidFunctionSelector(hex"00000000");
}

revert LSP8TokenContractCannotHoldValue();
}

/**
* @dev Forwards the call with the received value to an extension mapped to a function selector.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -534,13 +534,16 @@ abstract contract LSP8IdentifiableDigitalAssetCore is
_INTERFACEID_LSP1
)
) {
operator.call(
abi.encodeWithSelector(
ILSP1UniversalReceiver.universalReceiver.selector,
try
ILSP1UniversalReceiver(operator).universalReceiver(
_TYPEID_LSP8_TOKENOPERATOR,
lsp1Data
)
);
{
return;
} catch {
return;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {LSP2Utils} from "../LSP2ERC725YJSONSchema/LSP2Utils.sol";

// constants
import {_INTERFACEID_LSP8} from "./LSP8Constants.sol";
import {LSP8TokenContractCannotHoldValue} from "./LSP8Errors.sol";

import {
_LSP17_EXTENSION_PREFIX
Expand Down Expand Up @@ -92,6 +93,19 @@ abstract contract LSP8IdentifiableDigitalAssetInitAbstract is
return _fallbackLSP17Extendable(callData);
}

/**
* @dev Reverts whenever someone tries to send native tokens to a LSP8 contract.
* @notice LSP8 contract cannot receive native tokens.
*/
receive() external payable virtual {
// revert on empty calls with no value
if (msg.value == 0) {
revert InvalidFunctionSelector(hex"00000000");
}

revert LSP8TokenContractCannotHoldValue();
}

/**
* @dev Forwards the call with the received value to an extension mapped to a function selector.
*
Expand Down
40 changes: 40 additions & 0 deletions docs/contracts/LSP7DigitalAsset/LSP7DigitalAsset.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,25 @@ This function is executed when:

<br/>

### receive

:::note References

- Specification details: [**LSP-7-DigitalAsset**](https://github.com/lukso-network/lips/tree/main/LSPs/LSP-7-DigitalAsset.md#receive)
- Solidity implementation: [`LSP7DigitalAsset.sol`](https://github.com/lukso-network/lsp-smart-contracts/blob/develop/contracts/LSP7DigitalAsset/LSP7DigitalAsset.sol)

:::

```solidity
receive() external payable;
```

_LSP7 contract cannot receive native tokens._

Reverts whenever someone tries to send native tokens to a LSP7 contract.

<br/>

### authorizeOperator

:::note References
Expand Down Expand Up @@ -1560,6 +1579,27 @@ reverts if the `tokenReceiver` is an EOA when minting or transferring tokens wit

<br/>

### LSP7TokenContractCannotHoldValue

:::note References

- Specification details: [**LSP-7-DigitalAsset**](https://github.com/lukso-network/lips/tree/main/LSPs/LSP-7-DigitalAsset.md#lsp7tokencontractcannotholdvalue)
- Solidity implementation: [`LSP7DigitalAsset.sol`](https://github.com/lukso-network/lsp-smart-contracts/blob/develop/contracts/LSP7DigitalAsset/LSP7DigitalAsset.sol)
- Error signature: `LSP7TokenContractCannotHoldValue()`
- Error hash: `0x388f5adc`

:::

```solidity
error LSP7TokenContractCannotHoldValue();
```

_LSP7 contract cannot receive native tokens._

Error occurs when sending native tokens to the LSP7 contract without sending any data. E.g. Sending value without passing a bytes4 function selector to call a LSP17 Extension.

<br/>

### LSP7TokenOwnerCannotBeOperator

:::note References
Expand Down
40 changes: 40 additions & 0 deletions docs/contracts/LSP7DigitalAsset/extensions/LSP7Burnable.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,25 @@ This function is executed when:

<br/>

### receive

:::note References

- Specification details: [**LSP-7-DigitalAsset**](https://github.com/lukso-network/lips/tree/main/LSPs/LSP-7-DigitalAsset.md#receive)
- Solidity implementation: [`LSP7Burnable.sol`](https://github.com/lukso-network/lsp-smart-contracts/blob/develop/contracts/LSP7DigitalAsset/extensions/LSP7Burnable.sol)

:::

```solidity
receive() external payable;
```

_LSP7 contract cannot receive native tokens._

Reverts whenever someone tries to send native tokens to a LSP7 contract.

<br/>

### authorizeOperator

:::note References
Expand Down Expand Up @@ -1585,6 +1604,27 @@ reverts if the `tokenReceiver` is an EOA when minting or transferring tokens wit

<br/>

### LSP7TokenContractCannotHoldValue

:::note References

- Specification details: [**LSP-7-DigitalAsset**](https://github.com/lukso-network/lips/tree/main/LSPs/LSP-7-DigitalAsset.md#lsp7tokencontractcannotholdvalue)
- Solidity implementation: [`LSP7Burnable.sol`](https://github.com/lukso-network/lsp-smart-contracts/blob/develop/contracts/LSP7DigitalAsset/extensions/LSP7Burnable.sol)
- Error signature: `LSP7TokenContractCannotHoldValue()`
- Error hash: `0x388f5adc`

:::

```solidity
error LSP7TokenContractCannotHoldValue();
```

_LSP7 contract cannot receive native tokens._

Error occurs when sending native tokens to the LSP7 contract without sending any data. E.g. Sending value without passing a bytes4 function selector to call a LSP17 Extension.

<br/>

### LSP7TokenOwnerCannotBeOperator

:::note References
Expand Down
Loading

0 comments on commit e6fb55d

Please sign in to comment.