Skip to content

Commit

Permalink
Rethinks revealor by making the reveal process update its state.
Browse files Browse the repository at this point in the history
  • Loading branch information
steven2308 committed Oct 9, 2023
1 parent 9ef708a commit d577b6c
Show file tree
Hide file tree
Showing 11 changed files with 232 additions and 64 deletions.
17 changes: 13 additions & 4 deletions contracts/RMRK/extension/revealable/IRMRKRevealable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,22 @@
pragma solidity ^0.8.21;

interface IRMRKRevealable {
/**
* @notice Gets the `IRMRKRevealer` associated with the contract.
* @return revealer The `IRMRKRevealer` associated with the contract
*/
function getRevealer() external view returns (address);

/**
* @notice Sets the `IRMRKRevealer` associated with the contract.
* @param revealer The `IRMRKRevealer` to associate with the contract
*/
function setRevealer(address revealer) external;

// Should ask revealor which assetId should be added to the token and optionally which asset to replace.
// It should send the active asset Ids for the token.
// The Revealer might need to be added as contributor on the main contract to add assets if needed
// This method should be called by the owner or approved for assets, and should add the asset to the token and accept it
/** @notice Reveals the asset for the given tokenIds by adding and accepting and new one.
* @dev SHOULD ask revealer which assetId should be added to the token and which asset to replace through `IRMRKRevealer.getAssetsToReveal`
* @dev SHOULD be called by the owner or approved for assets
* @dev SHOULD add the new asset to each token and accept it
*/
function reveal(uint256[] memory tokenIds) external;
}
36 changes: 26 additions & 10 deletions contracts/RMRK/extension/revealable/IRMRKRevealer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,37 @@
pragma solidity ^0.8.21;

interface IRMRKRevealer {
event Revealed(
uint256[] tokenIds,
uint64[] revealedAssetsIds,
uint64[] assetsToReplaceIds
);

/**
* @notice For each `tokenId` in `tokenIds` returns whether it can be revealed or not
* @param tokenIds The `tokenIds` to check
* @return revealable The array of booleans indicating whether each `tokenId` can be revealed or not
*/
function getRevealableTokens(
uint256[] memory tokenIds
) external view returns (bool[] memory revealable);

/**
* @notice Returns the assetIds to reveal for the given tokenIds
* @param tokenIds The tokenIds to reveal
* @dev This method MUST only return existing assetIds
* @dev This method MUST return the same amount of assetIds as tokenIds
* @return revealedAssetIds The assetIds to reveal
* @return assetToReplaceIds The assetIds to replace
* @notice Returns the revealed `assetIds` for the given `tokenIds` and marks them as revealed.
* @param tokenIds The `tokenIds` to reveal
* @dev This CAN add new assets to the original contract if necessary, in which case it SHOULD have the necessary permissions
* @dev This method MUST only return existing `assetIds`
* @dev This method MUST be called only by the contract implementing the `IRMRKRevealable` interface, during the `reveal` method
* @dev This method MUST return the same amount of `revealedAssetsIds` and `assetsToReplaceIds` as `tokenIds`
* @return revealedAssetsIds The revealed `assetIds`
* @return assetsToReplaceIds The `assetIds` to replace
*/
function getRevealedAssets(
function reveal(
uint256[] memory tokenIds
)
external
view
returns (
uint64[] memory revealedAssetIds,
uint64[] memory assetToReplaceIds
uint64[] memory revealedAssetsIds,
uint64[] memory assetsToReplaceIds
);
}
10 changes: 5 additions & 5 deletions contracts/RMRK/extension/revealable/RMRKRevealable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ abstract contract RMRKRevealable is IRMRKRevealable {
_checkRevealPermissions(tokenIds);
uint256 length = tokenIds.length;
(
uint64[] memory revealedAssetIds,
uint64[] memory assetToReplaceIds
) = IRMRKRevealer(_revealer).getRevealedAssets(tokenIds);
uint64[] memory revealedAssetsIds,
uint64[] memory assetsToReplaceIds
) = IRMRKRevealer(_revealer).reveal(tokenIds);
for (uint256 i; i < length; ) {
_addAndAcceptAssetToToken(
tokenIds[i],
revealedAssetIds[i],
assetToReplaceIds[i]
revealedAssetsIds[i],
assetsToReplaceIds[i]
);
unchecked {
++i;
Expand Down
53 changes: 41 additions & 12 deletions contracts/mocks/extensions/revealable/RMRKRevealerMock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@ pragma solidity ^0.8.21;
import "../../../RMRK/extension/revealable/IRMRKRevealer.sol";
import "../../RMRKMultiAssetMock.sol";

error AlreadyRevealed(uint256 tokenId);
error CallerIsNotRevealable();

contract RMRKRevealerMock is IRMRKRevealer {
uint64 public revealedAssetId;
uint64 private _revealedAssetId;
address private _revealableContract;
mapping(uint256 tokenId => bool revealed) private _revealed;

constructor(uint64 revealedAssetId_) {
revealedAssetId = revealedAssetId_;
constructor(uint64 revealedAssetId, address revealableContract) {
_revealedAssetId = revealedAssetId;
_revealableContract = revealableContract;
}

function supportsInterface(
Expand All @@ -18,32 +24,55 @@ contract RMRKRevealerMock is IRMRKRevealer {
return interfaceId == type(IRMRKRevealer).interfaceId;
}

/**
/**-
* @inheritdoc IRMRKRevealer
*/
function getRevealableTokens(
uint256[] memory tokenIds
) external view returns (bool[] memory revealable) {
uint256 length = tokenIds.length;
revealable = new bool[](length);
for (uint256 i; i < length; ) {
revealable[i] = !_revealed[tokenIds[i]];
unchecked {
++i;
}
}
}

/**-
* @inheritdoc IRMRKRevealer
*/
function getRevealedAssets(
function reveal(
uint256[] memory tokenIds
)
external
view
returns (
uint64[] memory revealedAssetIds,
uint64[] memory assetToReplaceIds
uint64[] memory revealedAssetsIds,
uint64[] memory assetsToReplaceIds
)
{
if (msg.sender != _revealableContract) {
revert CallerIsNotRevealable();
}
uint256 length = tokenIds.length;
revealedAssetIds = new uint64[](length);
assetToReplaceIds = new uint64[](length);
revealedAssetsIds = new uint64[](length);
assetsToReplaceIds = new uint64[](length);
for (uint256 i; i < length; ) {
uint256 tokenId = tokenIds[i];
if (_revealed[tokenId]) {
revert AlreadyRevealed(tokenId);
}
_revealed[tokenId] = true;
uint64[] memory activeAssets = RMRKMultiAssetMock(msg.sender)
.getActiveAssets(tokenId);
// Asumes that the token has at least one asset
revealedAssetIds[i] = revealedAssetId;
assetToReplaceIds[i] = activeAssets[0];
revealedAssetsIds[i] = _revealedAssetId;
assetsToReplaceIds[i] = activeAssets[0];
unchecked {
++i;
}
}
emit Revealed(tokenIds, revealedAssetsIds, assetsToReplaceIds);
}
}
12 changes: 6 additions & 6 deletions docs/RMRK/extension/revealable/IRMRKRevealable.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
function getRevealer() external view returns (address)
```


Gets the `IRMRKRevealer` associated with the contract.



Expand All @@ -25,17 +25,17 @@ function getRevealer() external view returns (address)

| Name | Type | Description |
|---|---|---|
| _0 | address | undefined |
| _0 | address | revealer The `IRMRKRevealer` associated with the contract |

### reveal

```solidity
function reveal(uint256[] tokenIds) external nonpayable
```

Reveals the asset for the given tokenIds by adding and accepting and new one.



*SHOULD ask revealer which assetId should be added to the token and which asset to replace through `IRMRKRevealer.getAssetsToReveal`SHOULD be called by the owner or approved for assetsSHOULD add the new asset to each token and accept it*

#### Parameters

Expand All @@ -49,15 +49,15 @@ function reveal(uint256[] tokenIds) external nonpayable
function setRevealer(address revealer) external nonpayable
```


Sets the `IRMRKRevealer` associated with the contract.



#### Parameters

| Name | Type | Description |
|---|---|---|
| revealer | address | undefined |
| revealer | address | The `IRMRKRevealer` to associate with the contract |



Expand Down
57 changes: 50 additions & 7 deletions docs/RMRK/extension/revealable/IRMRKRevealer.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,72 @@

## Methods

### getRevealedAssets
### getRevealableTokens

```solidity
function getRevealedAssets(uint256[] tokenIds) external view returns (uint64[] revealedAssetIds, uint64[] assetToReplaceIds)
function getRevealableTokens(uint256[] tokenIds) external view returns (bool[] revealable)
```

Returns the assetIds to reveal for the given tokenIds
For each `tokenId` in `tokenIds` returns whether it can be revealed or not

*This method MUST only return existing assetIdsThis method MUST return the same amount of assetIds as tokenIds*


#### Parameters

| Name | Type | Description |
|---|---|---|
| tokenIds | uint256[] | The `tokenIds` to check |

#### Returns

| Name | Type | Description |
|---|---|---|
| revealable | bool[] | The array of booleans indicating whether each `tokenId` can be revealed or not |

### reveal

```solidity
function reveal(uint256[] tokenIds) external nonpayable returns (uint64[] revealedAssetsIds, uint64[] assetsToReplaceIds)
```

Returns the revealed `assetIds` for the given `tokenIds` and marks them as revealed.

*This CAN add new assets to the original contract if necessary, in which case it SHOULD have the necessary permissionsThis method MUST only return existing `assetIds`This method MUST be called only by the contract implementing the `IRMRKRevealable` interface, during the `reveal` methodThis method MUST return the same amount of `revealedAssetsIds` and `assetsToReplaceIds` as `tokenIds`*

#### Parameters

| Name | Type | Description |
|---|---|---|
| tokenIds | uint256[] | The tokenIds to reveal |
| tokenIds | uint256[] | The `tokenIds` to reveal |

#### Returns

| Name | Type | Description |
|---|---|---|
| revealedAssetIds | uint64[] | The assetIds to reveal |
| assetToReplaceIds | uint64[] | The assetIds to replace |
| revealedAssetsIds | uint64[] | The revealed `assetIds` |
| assetsToReplaceIds | uint64[] | The `assetIds` to replace |



## Events

### Revealed

```solidity
event Revealed(uint256[] tokenIds, uint64[] revealedAssetsIds, uint64[] assetsToReplaceIds)
```





#### Parameters

| Name | Type | Description |
|---|---|---|
| tokenIds | uint256[] | undefined |
| revealedAssetsIds | uint64[] | undefined |
| assetsToReplaceIds | uint64[] | undefined |



4 changes: 2 additions & 2 deletions docs/RMRK/extension/revealable/RMRKRevealable.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ Reveals the assets for the given tokenIds
function setRevealer(address revealer) external nonpayable
```


Sets the `IRMRKRevealer` associated with the contract.



#### Parameters

| Name | Type | Description |
|---|---|---|
| revealer | address | undefined |
| revealer | address | The `IRMRKRevealer` to associate with the contract |

### supportsInterface

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -625,15 +625,15 @@ Sets a new priority array for a given token.
function setRevealer(address revealer) external nonpayable
```


Sets the `IRMRKRevealer` associated with the contract.



#### Parameters

| Name | Type | Description |
|---|---|---|
| revealer | address | undefined |
| revealer | address | The `IRMRKRevealer` to associate with the contract |

### supportsInterface

Expand Down
Loading

0 comments on commit d577b6c

Please sign in to comment.