-
Notifications
You must be signed in to change notification settings - Fork 86
/
WithMetadataRegistry.sol
80 lines (69 loc) · 3.29 KB
/
WithMetadataRegistry.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//SPDX-License-Identifier: MIT
// solhint-disable-next-line compiler-version
pragma solidity 0.8.23;
import {ILandMetadataRegistry} from "../interfaces/ILandMetadataRegistry.sol";
/// @title WithMetadataRegistry
/// @author The Sandbox
/// @notice Add support for the metadata registry
abstract contract WithMetadataRegistry {
string public constant UNKNOWN_NEIGHBORHOOD = "unknown";
event MetadataRegistrySet(address indexed metadataRegistry);
struct MetadataRegistryStorage {
ILandMetadataRegistry _metadataRegistry;
}
/// @custom:storage-location erc7201:thesandbox.storage.land.common.WithMetadataRegistry
bytes32 internal constant METADATA_REGISTRY_STORAGE_LOCATION =
0x3899f13de39885dfce849839be8330453b5866928dd0e5933e36794349628400;
function _getMetadataRegistryStorage() private pure returns (MetadataRegistryStorage storage $) {
// solhint-disable-next-line no-inline-assembly
assembly {
$.slot := METADATA_REGISTRY_STORAGE_LOCATION
}
}
/// @notice Get the address of the Metadata Registry
/// @return metadataRegistry The address of the Metadata Registry
function getMetadataRegistry() external view returns (ILandMetadataRegistry metadataRegistry) {
MetadataRegistryStorage storage $ = _getMetadataRegistryStorage();
return $._metadataRegistry;
}
/// @notice return the metadata for one land
/// @param tokenId the token id
/// @return premium true if the land is premium
/// @return neighborhoodId the number that identifies the neighborhood
/// @return neighborhoodName the neighborhood name
function getMetadata(
uint256 tokenId
) external view returns (bool premium, uint256 neighborhoodId, string memory neighborhoodName) {
ILandMetadataRegistry registry = _getMetadataRegistryStorage()._metadataRegistry;
if (registry == ILandMetadataRegistry(address(0))) {
return (false, 0, UNKNOWN_NEIGHBORHOOD);
}
return registry.getMetadata(tokenId);
}
/// @notice return true if a land is premium
/// @param tokenId the token id
function isPremium(uint256 tokenId) external view returns (bool) {
ILandMetadataRegistry registry = _getMetadataRegistryStorage()._metadataRegistry;
if (registry == ILandMetadataRegistry(address(0))) {
return false;
}
return registry.isPremium(tokenId);
}
/// @notice return the id that identifies the neighborhood
/// @param tokenId the token id
function getNeighborhoodId(uint256 tokenId) external view returns (uint256) {
ILandMetadataRegistry registry = _getMetadataRegistryStorage()._metadataRegistry;
if (registry == ILandMetadataRegistry(address(0))) {
return 0;
}
return registry.getNeighborhoodId(tokenId);
}
/// @notice set the address of the metadata registry
/// @param metadataRegistry the address of the metadata registry
function _setMetadataRegistry(address metadataRegistry) internal {
require(metadataRegistry != address(0), "invalid registry address");
MetadataRegistryStorage storage $ = _getMetadataRegistryStorage();
$._metadataRegistry = ILandMetadataRegistry(metadataRegistry);
emit MetadataRegistrySet(metadataRegistry);
}
}