Contract Source Code:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "./DataTypes.sol";
import {IDataStore, ID} from "./IDataStore.sol";
import {GameRegistryConsumer} from "../core/GameRegistryConsumer.sol";
import {GAME_LOGIC_CONTRACT_ROLE} from "../constants/RoleConstants.sol";
contract DataStore is IDataStore, GameRegistryConsumer {
using DataTypes for *;
mapping(uint256 => mapping(uint256 => bytes32)) public datastore;
mapping(uint256 => mapping(uint256 => bytes32[])) public arrayStore;
mapping(uint256 => mapping(uint256 => string)) private stringStore;
mapping(uint256 => bytes32) public columnTypes;
constructor(address gameRegistryAddress) GameRegistryConsumer(gameRegistryAddress, ID) {}
function generateKey(uint256 docId, uint256 colId) public pure returns (uint256) {
return uint256(keccak256(abi.encodePacked(docId, colId)));
}
function generateArrayKey (uint256 docId, uint256 colId) public pure returns (uint256) {
return uint256(keccak256(abi.encodePacked(docId, colId, "__array")));
}
function setValue(uint256 tableId, uint256 docId, uint256 colId, bytes32 value) public onlyRole(GAME_LOGIC_CONTRACT_ROLE) {
datastore[tableId][uint256(keccak256(abi.encodePacked(docId, colId)))] = value;
emit ValueSet(tableId, docId, colId, value);
}
function setArrayValue(uint256 tableId, uint256 docId, uint256 colId, bytes32[] memory value) public onlyRole(GAME_LOGIC_CONTRACT_ROLE) {
arrayStore[tableId][generateArrayKey(docId, colId)] = value;
emit ArrayValueSet(tableId, docId, colId, value);
}
function setUint256ArrayValue(uint256 tableId, uint256 docId, uint256 colId, uint256[] memory value) public onlyRole(GAME_LOGIC_CONTRACT_ROLE) {
require (getColumnType(colId) == IDataStore.ColumnType.UINT256, "Column is not UINT256ARRAY");
bytes32[] memory packedValues = new bytes32[](value.length);
for (uint256 i = 0; i < value.length; i++) {
packedValues[i] = value[i].packUint256();
}
setArrayValue(tableId, docId, colId, packedValues);
}
function setBoolArrayValue(uint256 tableId, uint256 docId, uint256 colId, bool[] memory value) public onlyRole(GAME_LOGIC_CONTRACT_ROLE) {
require (getColumnType(colId) == IDataStore.ColumnType.BOOL, "Column is not BOOLARRAY");
bytes32[] memory packedValues = new bytes32[](value.length);
for (uint256 i = 0; i < value.length; i++) {
packedValues[i] = value[i].packBool();
}
setArrayValue(tableId, docId, colId, packedValues);
}
function setAddressArrayValue(uint256 tableId, uint256 docId, uint256 colId, address[] memory value) public onlyRole(GAME_LOGIC_CONTRACT_ROLE) {
require (getColumnType(colId) == IDataStore.ColumnType.ADDRESS, "Column is not ADDRESSARRAY");
bytes32[] memory packedValues = new bytes32[](value.length);
for (uint256 i = 0; i < value.length; i++) {
packedValues[i] = value[i].packAddress();
}
setArrayValue(tableId, docId, colId, packedValues);
}
function getUint256Array(uint256 tableId, uint256 docId, uint256 colId) public view returns (uint256[] memory) {
require (getColumnType(colId) == IDataStore.ColumnType.UINT256, "Column is not UINT256");
bytes32[] memory byteArray = arrayStore[tableId][generateArrayKey(docId, colId)];
uint256[] memory uintArray = new uint256[](byteArray.length);
for (uint256 i = 0; i < byteArray.length; i++) {
uintArray[i] = byteArray[i].unpackUint256();
}
return uintArray;
}
function getBoolArray(uint256 tableId, uint256 docId, uint256 colId) public view returns (bool[] memory) {
require (getColumnType(colId) == IDataStore.ColumnType.BOOL, "Column is not BOOL");
bytes32[] memory byteArray = arrayStore[tableId][generateArrayKey(docId, colId)];
bool[] memory boolArray = new bool[](byteArray.length);
for (uint256 i = 0; i < byteArray.length; i++) {
boolArray[i] = byteArray[i].unpackBool();
}
return boolArray;
}
function getAddressArray(uint256 tableId, uint256 docId, uint256 colId) public view returns (address[] memory) {
require (getColumnType(colId) == IDataStore.ColumnType.ADDRESS, "Column is not ADDRESS");
bytes32[] memory byteArray = arrayStore[tableId][generateArrayKey(docId, colId)];
address[] memory addressArray = new address[](byteArray.length);
for (uint256 i = 0; i < byteArray.length; i++) {
addressArray[i] = byteArray[i].unpackAddress();
}
return addressArray;
}
function getValue(uint256 tableId, uint256 docId, uint256 colId) public view returns (bytes32) {
return datastore[tableId][uint256(keccak256(abi.encodePacked(docId, colId)))];
}
function setColumnType(uint256 colId, IDataStore.ColumnType columnType) public onlyRole(GAME_LOGIC_CONTRACT_ROLE) {
require(!isColumnTypeSet(colId), "Column type already set");
columnTypes[colId] = bytes32(uint256(columnType));
emit ColumnTypeSet(colId, columnType);
}
function isColumnTypeSet(uint256 colId) public view returns (bool) {
return columnTypes[colId] != bytes32(0);
}
function getColumnType(uint256 colId) public view returns (IDataStore.ColumnType) {
bytes32 typeValue = columnTypes[colId];
require(typeValue != bytes32(0), "Column type not set");
return IDataStore.ColumnType(uint8(uint256(typeValue)));
}
// Type-specific setters
function setUint256(uint256 tableId, uint256 docId, uint256 colId, uint256 value) public onlyRole(GAME_LOGIC_CONTRACT_ROLE){
require(getColumnType(colId) == IDataStore.ColumnType.UINT256, "Column is not UINT256");
setValue(tableId, docId, colId, value.packUint256());
}
function setInt256(uint256 tableId, uint256 docId, uint256 colId, int256 value) public onlyRole(GAME_LOGIC_CONTRACT_ROLE){
require(getColumnType(colId) == IDataStore.ColumnType.INT256, "Column is not INT256");
setValue(tableId, docId, colId, value.packInt256());
}
function setBool(uint256 tableId, uint256 docId, uint256 colId, bool value) public onlyRole(GAME_LOGIC_CONTRACT_ROLE){
require(getColumnType(colId) == IDataStore.ColumnType.BOOL, "Column is not BOOL");
setValue(tableId, docId, colId, value.packBool());
}
function setAddress(uint256 tableId, uint256 docId, uint256 colId, address value) public onlyRole(GAME_LOGIC_CONTRACT_ROLE){
require(getColumnType(colId) == IDataStore.ColumnType.ADDRESS, "Column is not ADDRESS");
setValue(tableId, docId, colId, value.packAddress());
}
function setBytes32(uint256 tableId, uint256 docId, uint256 colId, bytes32 value) public onlyRole(GAME_LOGIC_CONTRACT_ROLE){
require(getColumnType(colId) == IDataStore.ColumnType.BYTES32, "Column is not BYTES32");
setValue(tableId, docId, colId, value);
}
function setString(uint256 tableId, uint256 docId, uint256 colId, string memory value) public onlyRole(GAME_LOGIC_CONTRACT_ROLE){
require(getColumnType(colId) == IDataStore.ColumnType.STRING, "Column is not STRING");
uint256 key = generateKey(docId, colId);
stringStore[tableId][key] = value;
emit StringValueSet(tableId, docId, colId, value);
}
function deleteValue(uint256 tableId, uint256 docId, uint256 colId) public onlyRole(GAME_LOGIC_CONTRACT_ROLE){
uint256 key = generateKey(docId, colId);
delete datastore[tableId][key];
}
// Type-specific getters
function getUint256(uint256 tableId, uint256 docId, uint256 colId) public view returns (uint256) {
require(getColumnType(colId) == IDataStore.ColumnType.UINT256, "Column is not UINT256");
return getValue(tableId, docId, colId).unpackUint256();
}
function getInt256(uint256 tableId, uint256 docId, uint256 colId) public view returns (int256) {
require(getColumnType(colId) == IDataStore.ColumnType.INT256, "Column is not INT256");
return getValue(tableId, docId, colId).unpackInt256();
}
function getBool(uint256 tableId, uint256 docId, uint256 colId) public view returns (bool) {
require(getColumnType(colId) == IDataStore.ColumnType.BOOL, "Column is not BOOL");
return getValue(tableId, docId, colId).unpackBool();
}
function getAddress(uint256 tableId, uint256 docId, uint256 colId) public view returns (address) {
require(getColumnType(colId) == IDataStore.ColumnType.ADDRESS, "Column is not ADDRESS");
return getValue(tableId, docId, colId).unpackAddress();
}
function getBytes32(uint256 tableId, uint256 docId, uint256 colId) public view returns (bytes32) {
require(getColumnType(colId) == IDataStore.ColumnType.BYTES32, "Column is not BYTES32");
return getValue(tableId, docId, colId);
}
function getString(uint256 tableId, uint256 docId, uint256 colId) public view returns (string memory) {
require(getColumnType(colId) == IDataStore.ColumnType.STRING, "Column is not STRING");
uint256 key = generateKey(docId, colId);
return stringStore[tableId][key];
}
function hasValue(uint256 tableId, uint256 docId, uint256 colId) public view returns (bool) {
uint256 key = generateKey(docId, colId);
return datastore[tableId][key] != bytes32(0);
}
function hasStringValue(uint256 tableId, uint256 docId, uint256 colId) public view returns (bool) {
uint256 key = generateKey(docId, colId);
return keccak256(bytes(stringStore[tableId][key])) != keccak256(bytes(""));
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;
library DataTypes {
// Pack and unpack uint256
function packUint256(uint256 value) internal pure returns (bytes32) {
return bytes32(value);
}
function unpackUint256(bytes32 packed) internal pure returns (uint256) {
return uint256(packed);
}
// Pack and unpack int256
function packInt256(int256 value) internal pure returns (bytes32) {
return bytes32(uint256(value));
}
function unpackInt256(bytes32 packed) internal pure returns (int256) {
return int256(uint256(packed));
}
// Pack and unpack address
function packAddress(address value) internal pure returns (bytes32) {
return bytes32(uint256(uint160(value)));
}
function unpackAddress(bytes32 packed) internal pure returns (address) {
return address(uint160(uint256(packed)));
}
// Pack and unpack bool
function packBool(bool value) internal pure returns (bytes32) {
return bytes32(uint256(value ? 1 : 0));
}
function unpackBool(bytes32 packed) internal pure returns (bool) {
return uint256(packed) == 1;
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
uint256 constant ID = uint256(keccak256("game.gigaverse.datastore"));
interface IDataStore {
enum ColumnType { NONE, UINT256, INT256, BOOL, ADDRESS, BYTES32, STRING, UINT256_ARRAY }
event ValueSet(uint256 indexed tableId, uint256 indexed docId, uint256 indexed colId, bytes32 value);
event StringValueSet(uint256 indexed tableId, uint256 indexed docId, uint256 indexed colId, string value);
event ArrayValueSet(uint256 indexed tableId, uint256 indexed docId, uint256 indexed colId, bytes32[] value);
event ColumnTypeSet(uint256 indexed colId, ColumnType columnType);
function setValue(uint256 tableId, uint256 docId, uint256 colId, bytes32 value) external;
function getValue(uint256 tableId, uint256 docId, uint256 colId) external view returns (bytes32);
function setColumnType(uint256 colId, ColumnType columnType) external;
function getColumnType(uint256 colId) external view returns (ColumnType);
// Type-specific setters
function setUint256(uint256 tableId, uint256 docId, uint256 colId, uint256 value) external;
function setInt256(uint256 tableId, uint256 docId, uint256 colId, int256 value) external;
function setBool(uint256 tableId, uint256 docId, uint256 colId, bool value) external;
function setAddress(uint256 tableId, uint256 docId, uint256 colId, address value) external;
function setBytes32(uint256 tableId, uint256 docId, uint256 colId, bytes32 value) external;
function setString(uint256 tableId, uint256 docId, uint256 colId, string memory value) external;
// Type-specific getters
function getUint256(uint256 tableId, uint256 docId, uint256 colId) external view returns (uint256);
function getInt256(uint256 tableId, uint256 docId, uint256 colId) external view returns (int256);
function getBool(uint256 tableId, uint256 docId, uint256 colId) external view returns (bool);
function getAddress(uint256 tableId, uint256 docId, uint256 colId) external view returns (address);
function getBytes32(uint256 tableId, uint256 docId, uint256 colId) external view returns (bytes32);
function getString(uint256 tableId, uint256 docId, uint256 colId) external view returns (string memory);
function deleteValue(uint256 tableId, uint256 docId, uint256 colId) external;
function hasValue(uint256 tableId, uint256 docId, uint256 colId) external view returns (bool);
}
// SPDX-License-Identifier: MIT LICENSE
pragma solidity ^0.8.13;
import {ReentrancyGuard} from "@openzeppelin/contracts/utils/ReentrancyGuard.sol";
import {PAUSER_ROLE, MANAGER_ROLE} from "../constants/RoleConstants.sol";
import {ISystem} from "./ISystem.sol";
import {IGameRegistry, IERC165} from "./IGameRegistry.sol";
import {IDataStore, ID as DATA_STORE_ID} from "../db/IDataStore.sol";
import {DEPLOYER_ROLE} from "../constants/RoleConstants.sol";
/** @title Contract that lets a child contract access the GameRegistry contract */
abstract contract GameRegistryConsumer is
ReentrancyGuard,
ISystem
{
/// @notice Whether or not the contract is paused
bool private _paused;
/// @notice Reference to the game registry that this contract belongs to
IGameRegistry internal _gameRegistry;
/// @notice Id for the system/component
uint256 private _id;
/** EVENTS **/
/// @dev Emitted when the pause is triggered by `account`.
event Paused(address account);
/// @dev Emitted when the pause is lifted by `account`.
event Unpaused(address account);
/** ERRORS **/
/// @notice Not authorized to perform action
error MissingRole(address account, bytes32 expectedRole);
/** MODIFIERS **/
/// @notice Modifier to verify a user has the appropriate role to call a given function
modifier onlyRole(bytes32 role) {
_checkRole(role, msg.sender);
_;
}
/**
* @dev Modifier to make a function callable only when the contract is not paused.
*
* Requirements:
*
* - The contract must not be paused.
*/
modifier whenNotPaused() {
_requireNotPaused();
_;
}
/**
* @dev Modifier to make a function callable only when the contract is paused.
*
* Requirements:
*
* - The contract must be paused.
*/
modifier whenPaused() {
_requirePaused();
_;
}
/** ERRORS **/
/// @notice Error if the game registry specified is invalid
error InvalidGameRegistry();
/** SETUP **/
/** @return ID for this system */
function getId() public view override returns (uint256) {
return _id;
}
/**
* Pause/Unpause the contract
*
* @param shouldPause Whether or pause or unpause
*/
function setPaused(bool shouldPause) external onlyRole(PAUSER_ROLE) {
if (shouldPause) {
_pause();
} else {
_unpause();
}
}
/**
* @dev Returns true if the contract OR the GameRegistry is paused, and false otherwise.
*/
function paused() public view virtual returns (bool) {
return _paused || _gameRegistry.paused();
}
/**
* Sets the GameRegistry contract address for this contract
*
* @param gameRegistryAddress Address for the GameRegistry contract
*/
function setGameRegistry(
address gameRegistryAddress
) external onlyRole(MANAGER_ROLE) {
_gameRegistry = IGameRegistry(gameRegistryAddress);
if (gameRegistryAddress == address(0)) {
revert InvalidGameRegistry();
}
}
/** @return GameRegistry contract for this contract */
function getGameRegistry() external view returns (IGameRegistry) {
return _gameRegistry;
}
/** INTERNAL **/
/**
* @dev Returns `true` if `account` has been granted `role`.
*/
function _hasAccessRole(
bytes32 role,
address account
) internal view returns (bool) {
return _gameRegistry.hasAccessRole(role, account);
}
/**
* @dev Revert with a standard message if `account` is missing `role`.
*
* The format of the revert reason is given by the following regular expression:
*
* /^AccessControl: account (0x[0-9a-f]{40}) is missing role (0x[0-9a-f]{64})$/
*/
function _checkRole(bytes32 role, address account) internal view virtual {
if (!_gameRegistry.hasAccessRole(role, account)) {
revert MissingRole(account, role);
}
}
/** @return Returns the dataStore for this contract */
function _dataStore() internal view returns (IDataStore) {
return IDataStore(_getSystem(DATA_STORE_ID));
}
/** @return Address for a given system */
function _getSystem(uint256 systemId) internal view returns (address) {
return _gameRegistry.getSystem(systemId);
}
/** PAUSABLE **/
/**
* @dev Throws if the contract is paused.
*/
function _requireNotPaused() internal view virtual {
require(!paused(), "Pausable: paused");
}
/**
* @dev Throws if the contract is not paused.
*/
function _requirePaused() internal view virtual {
require(paused(), "Pausable: not paused");
}
/**
* @dev Triggers stopped state.
*
* Requirements:
*
* - The contract must not be paused.
*/
function _pause() internal virtual {
require(_paused == false, "Pausable: not paused");
_paused = true;
emit Paused(msg.sender);
}
/**
* @dev Returns to normal state.
*
* Requirements:
*
* - The contract must be paused.
*/
function _unpause() internal virtual {
require(_paused == true, "Pausable: not paused");
_paused = false;
emit Unpaused(msg.sender);
}
function initialize() external virtual onlyRole(DEPLOYER_ROLE) { }
/**
* Constructor for this contract
*
* @param gameRegistryAddress Address of the GameRegistry contract
* @param id Id of the system/component
*/
constructor(
address gameRegistryAddress,
uint256 id
) {
_gameRegistry = IGameRegistry(gameRegistryAddress);
if (gameRegistryAddress == address(0)) {
revert InvalidGameRegistry();
}
_paused = true;
_id = id;
}
}
// SPDX-License-Identifier: MIT LICENSE
pragma solidity ^0.8.9;
// Pauser Role - Can pause the game
bytes32 constant PAUSER_ROLE = keccak256("PAUSER_ROLE");
// Minter Role - Can mint items, NFTs, and ERC20 currency
bytes32 constant MINTER_ROLE = keccak256("MINTER_ROLE");
// Manager Role - Can manage the shop, loot tables, and other game data
bytes32 constant MANAGER_ROLE = keccak256("MANAGER_ROLE");
// Depoloyer Role - Can Deploy new Systems
bytes32 constant DEPLOYER_ROLE = keccak256("DEPLOYER_ROLE");
// Game Logic Contract - Contract that executes game logic and accesses other systems
bytes32 constant GAME_LOGIC_CONTRACT_ROLE = keccak256("GAME_LOGIC_CONTRACT_ROLE");
// For functions callable from game server
bytes32 constant SERVER_JUDGE_ROLE = keccak256("SERVER_JUDGE_ROLE");
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.1.0) (utils/ReentrancyGuard.sol)
pragma solidity ^0.8.20;
/**
* @dev Contract module that helps prevent reentrant calls to a function.
*
* Inheriting from `ReentrancyGuard` will make the {nonReentrant} modifier
* available, which can be applied to functions to make sure there are no nested
* (reentrant) calls to them.
*
* Note that because there is a single `nonReentrant` guard, functions marked as
* `nonReentrant` may not call one another. This can be worked around by making
* those functions `private`, and then adding `external` `nonReentrant` entry
* points to them.
*
* TIP: If EIP-1153 (transient storage) is available on the chain you're deploying at,
* consider using {ReentrancyGuardTransient} instead.
*
* TIP: If you would like to learn more about reentrancy and alternative ways
* to protect against it, check out our blog post
* https://blog.openzeppelin.com/reentrancy-after-istanbul/[Reentrancy After Istanbul].
*/
abstract contract ReentrancyGuard {
// Booleans are more expensive than uint256 or any type that takes up a full
// word because each write operation emits an extra SLOAD to first read the
// slot's contents, replace the bits taken up by the boolean, and then write
// back. This is the compiler's defense against contract upgrades and
// pointer aliasing, and it cannot be disabled.
// The values being non-zero value makes deployment a bit more expensive,
// but in exchange the refund on every call to nonReentrant will be lower in
// amount. Since refunds are capped to a percentage of the total
// transaction's gas, it is best to keep them low in cases like this one, to
// increase the likelihood of the full refund coming into effect.
uint256 private constant NOT_ENTERED = 1;
uint256 private constant ENTERED = 2;
uint256 private _status;
/**
* @dev Unauthorized reentrant call.
*/
error ReentrancyGuardReentrantCall();
constructor() {
_status = NOT_ENTERED;
}
/**
* @dev Prevents a contract from calling itself, directly or indirectly.
* Calling a `nonReentrant` function from another `nonReentrant`
* function is not supported. It is possible to prevent this from happening
* by making the `nonReentrant` function external, and making it call a
* `private` function that does the actual work.
*/
modifier nonReentrant() {
_nonReentrantBefore();
_;
_nonReentrantAfter();
}
function _nonReentrantBefore() private {
// On the first call to nonReentrant, _status will be NOT_ENTERED
if (_status == ENTERED) {
revert ReentrancyGuardReentrantCall();
}
// Any calls to nonReentrant after this point will fail
_status = ENTERED;
}
function _nonReentrantAfter() private {
// By storing the original value once again, a refund is triggered (see
// https://eips.ethereum.org/EIPS/eip-2200)
_status = NOT_ENTERED;
}
/**
* @dev Returns true if the reentrancy guard is currently set to "entered", which indicates there is a
* `nonReentrant` function in the call stack.
*/
function _reentrancyGuardEntered() internal view returns (bool) {
return _status == ENTERED;
}
}
// SPDX-License-Identifier: MIT LICENSE
pragma solidity ^0.8.9;
import "@openzeppelin/contracts/utils/introspection/IERC165.sol";
/**
* Defines a system the game engine
*/
interface ISystem {
/** @return The ID for the system. Ex: a uint256 casted keccak256 hash */
function getId() external view returns (uint256);
function initialize() external;
}
// SPDX-License-Identifier: MIT LICENSE
pragma solidity ^0.8.9;
import "@openzeppelin/contracts/utils/introspection/IERC165.sol";
// @title Interface the game's ACL / Management Layer
interface IGameRegistry is IERC165 {
/**
* @dev Returns `true` if `account` has been granted `role`.
* @param role The role to query
* @param account The address to query
*/
function hasAccessRole(
bytes32 role,
address account
) external view returns (bool);
/**
* @return Whether or not the registry is paused
*/
function paused() external view returns (bool);
/**
* Registers a system by id
*
* @param systemId Id of the system
* @param systemAddress Address of the system contract
*/
function registerSystem(uint256 systemId, address systemAddress, bool isGameLogicContract) external;
/**
* @param systemId Id of the system
* @return System based on an id
*/
function getSystem(uint256 systemId) external view returns (address);
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.1.0) (utils/introspection/IERC165.sol)
pragma solidity ^0.8.20;
/**
* @dev Interface of the ERC-165 standard, as defined in the
* https://eips.ethereum.org/EIPS/eip-165[ERC].
*
* Implementers can declare support of contract interfaces, which can then be
* queried by others ({ERC165Checker}).
*
* For an implementation, see {ERC165}.
*/
interface IERC165 {
/**
* @dev Returns true if this contract implements the interface defined by
* `interfaceId`. See the corresponding
* https://eips.ethereum.org/EIPS/eip-165#how-interfaces-are-identified[ERC section]
* to learn more about how these ids are created.
*
* This function call must use less than 30 000 gas.
*/
function supportsInterface(bytes4 interfaceId) external view returns (bool);
}