Contract Name:
GameRegistry
Contract Source Code:
// SPDX-License-Identifier: MIT LICENSE
pragma solidity ^0.8.13;
import '@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol';
import '@openzeppelin/contracts-upgradeable/access/AccessControlUpgradeable.sol';
import '@openzeppelin/contracts-upgradeable/security/PausableUpgradeable.sol';
import '@openzeppelin/contracts/token/ERC20/ERC20.sol';
import '@openzeppelin/contracts/utils/cryptography/ECDSA.sol';
import '@openzeppelin/contracts/utils/structs/EnumerableSet.sol';
import '@openzeppelin/contracts/utils/Strings.sol';
import '@opengsn/contracts/src/interfaces/IERC2771Recipient.sol';
import {PAUSER_ROLE, VRF_SYSTEM_ROLE, DEPOSITOR_ROLE, MANAGER_ROLE, MINTER_ROLE, GAME_CURRENCY_CONTRACT_ROLE, GAME_NFT_CONTRACT_ROLE, GAME_ITEMS_CONTRACT_ROLE, GAME_LOGIC_CONTRACT_ROLE, TRUSTED_FORWARDER_ROLE, DEPLOYER_ROLE, TRUSTED_MULTICHAIN_ORACLE_ROLE} from './Constants.sol';
import './core/IGameRegistry.sol';
import {EntityLibrary} from './core/EntityLibrary.sol';
import {IComponent} from './core/components/IComponent.sol';
import {GUIDLibrary} from './core/GUIDLibrary.sol';
import {GuidCounterComponent, ID as GUID_COUNTER_COMPONENT_ID} from './generated/components/GuidCounterComponent.sol';
import {IMultichain1155} from './tokens/IMultichain1155.sol';
import {IMultichain721} from './tokens/IMultichain721.sol';
import {ChainIdComponent, ID as CHAIN_ID_COMPONENT_ID} from './generated/components/ChainIdComponent.sol';
import {ArchivedComponent, ID as ARCHIVED_COMPONENT_ID} from './generated/components/ArchivedComponent.sol';
// NOTE: Do NOT change ID if we wish to keep multi-chain GUID's in the same namespace
uint256 constant ID = uint256(keccak256('game.piratenation.gameregistry.v1'));
uint80 constant GUID_PREFIX = uint80(ID);
struct BatchComponentData {
uint256[] entities;
uint256[] componentIds;
bytes[] data;
}
/** @title Contract to track and limit access by accounts in the same block */
contract GameRegistry is
AccessControlUpgradeable,
OwnableUpgradeable,
PausableUpgradeable,
IERC2771Recipient,
IGameRegistry
{
/// @notice Block limit on transmitting a signed operator registration message
uint256 public constant OPERATOR_MESSAGE_BLOCK_LIMIT = 30; // 30 blocks
/// @notice Operator registration cooldown time in secons
uint256 public constant REGISTER_OPERATOR_COOLDOWN_LIMIT = 60 * 2; // 2 minutes
/** LIBRARY METHODS **/
using EnumerableSet for EnumerableSet.AddressSet;
using EnumerableSet for EnumerableSet.UintSet;
/** TYPES **/
/// @notice Player Account structure
struct PlayerAccount {
address playerAddress;
uint256 expiration;
}
/** MEMBERS **/
/// @notice Last time the player registered an operator wallet
mapping(address => uint256) public lastRegisterOperatorTime;
/// @notice System registry
mapping(uint256 => address) private _systemRegistry;
/// @notice Registry of current operator address to the player account
mapping(address => PlayerAccount) private _operatorToPlayerAccount;
/// @notice Registry of player account mapped to authorized operators
mapping(address => EnumerableSet.AddressSet) private _playerToOperatorAddresses;
/// @notice Map component id to its contract address
mapping(uint256 => address) _componentIdToAddress;
/// @notice Map component address to its ID
mapping(address => uint256) _componentAddressToId;
/// @notice GUID Counter
uint96 private _guidCounter;
/// @notice Map component ID to a map of the entities it stores
mapping(uint256 => EnumerableSet.UintSet) private _entityToComponents;
/// @notice Flag to check if the GUID counter has been set
bool private _guidCounterSet;
// @notice RequestID Mapping for cross-chain transfers
mapping(uint256 => bool) public requestIdProcessed;
/** EVENTS **/
/// @notice Emitted when an Operator address is registered
event OperatorRegistered(address player, address operator, uint256 expiration);
/// @notice Emitted when a System address is registered
event SystemRegistered(uint256 indexed id, address indexed systemAddress);
/// @notice Emitted when an Operator address is removed
event OperatorDeregistered(address operator, address player);
/// @notice Emitted when a component has been registered
event ComponentRegistered(uint256 indexed componentId, address indexed componentAddress);
/// @notice Emitted when a component value has been set
event ComponentValueSet(uint256 indexed componentId, uint256 indexed entity, bytes data);
/// @notice Emitted when a component value has been removed
event ComponentValueRemoved(uint256 indexed componentId, uint256 indexed entity);
/// @notice Emitted when a batch of component values has been set
event BatchComponentValueSet(uint256 indexed componentId, uint256[] entities, bytes[] data);
/// @notice Emitted when a batch of component values has been removed
event BatchComponentValueRemoved(uint256 indexed componentId, uint256[] entities);
/// @notice Emitted when a batch of component values has been set
event BatchMultiComponentValueSet(uint256[] componentIds, uint256[] entities, bytes[] data);
/// @notice Emitted when a batch of component values has been removed
event BatchMultiComponentValueRemoved(uint256[] componentIds, uint256[] entities);
/// @notice Emitted when a ComponentValueSet should be mirrored across chains
event PublishComponentValueSet(
uint256 indexed requestId,
uint256 indexed componentId,
uint256 indexed entity,
uint256 chainId,
uint256 requestTime,
bytes data
);
/// @notice Emitted when a BatchComponentValueSet should be mirrored across chains
event PublishBatchComponentValueSet(
uint256 indexed requestId,
uint256 indexed componentId,
uint256 chainId,
uint256 requestTime,
uint256[] entities,
bytes[] data
);
/// @notice Emitted when a BatchComponentValueSet should be mirrored across chains
event PublishBatchSetComponentValue(
uint256 indexed requestId,
uint256[] componentIds,
uint256[] entities,
uint256 fromChainId,
uint256 requestTime,
bytes[] data
);
/// @notice Emitted when a ComponentValueRemoved should be mirrored across chains
// TODO: Reenable when we're ready to support cross-chain removal
// event PublishComponentValueRemoved(
// uint256 indexed requestId,
// uint256 indexed componentId,
// uint256 indexed entity,
// uint256 chainId,
// uint256 requestTime
// );
/// @notice Emitted when a BatchComponentValueRemoved should be mirrored across chains
// TODO: Reenable when we're ready to support cross-chain removal
// event PublishBatchComponentValueRemoved(
// uint256 indexed requestId,
// uint256 indexed componentId,
// uint256 chainId,
// uint256 requestTime,
// uint256[] entities
// );
// 1155 Events
event Multichain1155TransferSingleSent(
uint256 requestId,
uint256 indexed systemId,
address indexed from,
address indexed to,
uint256 toChainId,
uint256 id,
uint256 amount
);
event Multichain1155TransferSingleReceived(
uint256 requestId,
uint256 indexed systemId,
address indexed from,
address indexed to,
uint256 fromChainId,
uint256 id,
uint256 amount
);
event Multichain1155TransferBatchSent(
uint256 requestId,
uint256 indexed systemId,
address indexed from,
address indexed to,
uint256 toChainId,
uint256[] ids,
uint256[] amounts
);
event Multichain1155TransferBatchReceived(
uint256 requestId,
uint256 indexed systemId,
address indexed from,
address indexed to,
uint256 fromChainId,
uint256[] ids,
uint256[] amounts
);
// 721 events
event Multichain721TransferSent(
uint256 requestId,
uint256 indexed systemId,
address indexed from,
address indexed to,
uint256 tokenId,
uint256 toChainId
);
event Multichain721TransferReceived(
uint256 requestId,
uint256 indexed systemId,
address indexed from,
address indexed to,
uint256 tokenId,
uint256 toChainId
);
/** ERRORS **/
/// @notice Invalid data count compared to number of entity count
error InvalidBatchData(uint256 entityCount, uint256 dataCount);
/// @notice Trying to access a component that hasn't been previously registered
error ComponentNotRegistered(address component);
/// @notice Trying to access a componentId that hasn't been previously registered
error ComponentIdNotRegistered(uint256 componentId);
/// @notice Operator
error InvalidOperatorAddress();
/// @notice Operator address must send transaction
error InvalidCaller();
/// @notice Player does not match signature
error PlayerSignerMismatch(address expected, address actual);
/// @notice Operator is registered to a different address, deregister first
error OperatorAlreadyRegistered();
/// @notice Invalid expiration timestamp provided
error InvalidExpirationTimestamp();
/// @notice Invalid block number (future block)
error InvalidBlockNumber();
/// @notice Invalid block number (expired)
error InvalidExpirationBlockNumber();
/// @notice Degregister request must come from player or operator
error InvalidDeregisterCaller();
/// @notice Operator has already expired
error OperatorExpired();
/// @notice Operator was not registered
error OperatorNotRegistered();
/// @notice Register operator in cooldown
error RegisterOperatorInCooldown();
/// @notice Not authorized to perform action
error MissingRole(address account, bytes32 expectedRole);
/// @notice Guid counter already set
error GuidCounterSet();
/// @notice Invalid System ID - The system ID must be registered.
error InvalidSystem(uint256 systemId);
/// @notice Invalid Chain ID - Must be processed on the correct Chain
error InvalidChain(uint256 chainId);
/// @notice Already Processed this request
error AlreadyProcessed(uint256 requestId);
/** SETUP **/
/**
* Initializer for this upgradeable contract
*/
function initialize(address admin) public initializer {
__AccessControl_init();
__Ownable_init();
__Pausable_init();
// Move ownership to deployer
_transferOwnership(admin);
// Give admin access role to owner
_setupRole(DEFAULT_ADMIN_ROLE, admin);
_setRoleAdmin(PAUSER_ROLE, DEFAULT_ADMIN_ROLE);
_setRoleAdmin(VRF_SYSTEM_ROLE, DEFAULT_ADMIN_ROLE);
_setRoleAdmin(DEPOSITOR_ROLE, DEFAULT_ADMIN_ROLE);
_setRoleAdmin(MINTER_ROLE, DEFAULT_ADMIN_ROLE);
_setRoleAdmin(MANAGER_ROLE, DEFAULT_ADMIN_ROLE);
_setRoleAdmin(DEPLOYER_ROLE, DEFAULT_ADMIN_ROLE);
_setRoleAdmin(GAME_NFT_CONTRACT_ROLE, DEFAULT_ADMIN_ROLE);
_setRoleAdmin(GAME_CURRENCY_CONTRACT_ROLE, DEFAULT_ADMIN_ROLE);
_setRoleAdmin(GAME_ITEMS_CONTRACT_ROLE, DEFAULT_ADMIN_ROLE);
_setRoleAdmin(GAME_LOGIC_CONTRACT_ROLE, DEFAULT_ADMIN_ROLE);
_pause();
}
/** EXTERNAL **/
/**
* @dev Set the GUID counter, should only be called once
*/
function setGuidCounter(uint96 guidValue) external onlyRole(MANAGER_ROLE) {
if (_guidCounterSet) {
revert GuidCounterSet();
}
_guidCounterSet = true;
_guidCounter = guidValue;
}
/**
* @dev Get the GUID counter value
*/
function getGuidCounter() external view returns (uint96) {
return _guidCounter;
}
/**
* Pause/Unpause the game and ALL the systems that utilize this game
*
* @param _paused Whether or pause or unpause
*/
function setPaused(bool _paused) external {
if (_msgSender() == owner() || hasRole(PAUSER_ROLE, _msgSender())) {
if (_paused) {
_pause();
} else {
_unpause();
}
} else {
revert MissingRole(_msgSender(), PAUSER_ROLE);
}
}
/**
* @inheritdoc IGameRegistry
*/
function paused() public view override(IGameRegistry, PausableUpgradeable) returns (bool) {
return PausableUpgradeable.paused();
}
/**
* @inheritdoc IGameRegistry
*/
function registerSystem(uint256 systemId, address systemAddress) external onlyRole(DEPLOYER_ROLE) {
_systemRegistry[systemId] = systemAddress;
emit SystemRegistered(systemId, systemAddress);
}
/**
* @inheritdoc IGameRegistry
*/
function getSystem(uint256 systemId) external view returns (address) {
return _systemRegistry[systemId];
}
/**
* @inheritdoc IGameRegistry
*/
function registerComponent(uint256 componentId, address componentAddress) public {
if (
hasAccessRole(GAME_LOGIC_CONTRACT_ROLE, _msgSender()) == false &&
hasAccessRole(MANAGER_ROLE, _msgSender()) == false &&
hasAccessRole(DEFAULT_ADMIN_ROLE, _msgSender()) == false
) {
revert MissingRole(_msgSender(), GAME_LOGIC_CONTRACT_ROLE);
}
_componentIdToAddress[componentId] = componentAddress;
_componentAddressToId[componentAddress] = componentId;
emit ComponentRegistered(componentId, componentAddress);
}
/**
* Gets a raw entity component value
* @param entity Entity to get value for
* @param componentId Component to get value from
*
* @return Bytes value of the entity component
*/
function getComponentValue(uint256 entity, uint256 componentId) external view returns (bytes memory) {
address componentAddress = _componentIdToAddress[componentId];
if (componentAddress == address(0)) {
revert ComponentNotRegistered(componentAddress);
}
return IComponent(componentAddress).getBytes(entity);
}
/**
* @inheritdoc IGameRegistry
* @dev Only registered components can call this function, otherwise it will revert
*/
function batchGetComponentValues(
uint256[] calldata entities,
uint256[] calldata componentIds
) external view returns (bytes[] memory values) {
values = new bytes[](entities.length);
for (uint256 i = 0; i < entities.length; i++) {
address componentAddress = _componentIdToAddress[componentIds[i]];
if (componentAddress == address(0)) {
revert ComponentNotRegistered(componentAddress);
}
values[i] = IComponent(componentAddress).getBytes(entities[i]);
}
}
/**
* @inheritdoc IGameRegistry
*/
function batchSetComponentValue(
uint256[] calldata entities,
uint256[] calldata componentIds,
bytes[] calldata values
) external override {
if (
hasAccessRole(GAME_LOGIC_CONTRACT_ROLE, _msgSender()) == false &&
hasAccessRole(MANAGER_ROLE, _msgSender()) == false &&
owner() != _msgSender()
) {
revert MissingRole(_msgSender(), GAME_LOGIC_CONTRACT_ROLE);
}
_batchSetComponentValue(entities, componentIds, values);
}
/**
* @inheritdoc IGameRegistry
*/
function batchPublishSetComponentValue(
uint256[] calldata entities,
uint256[] calldata componentIds,
bytes[] calldata values
) external override returns (uint256 requestId) {
if (
hasAccessRole(GAME_LOGIC_CONTRACT_ROLE, _msgSender()) == false &&
hasAccessRole(MANAGER_ROLE, _msgSender()) == false &&
owner() != _msgSender()
) {
revert MissingRole(_msgSender(), GAME_LOGIC_CONTRACT_ROLE);
}
_batchSetComponentValue(entities, componentIds, values);
requestId = _generateGUID();
emit PublishBatchSetComponentValue(requestId, componentIds, entities, block.chainid, block.timestamp, values);
return requestId;
}
/**
* @inheritdoc IGameRegistry
* @dev Only registered components can call this function, otherwise it will revert
*/
function registerComponentValueSet(uint256 entity, bytes calldata data) external virtual {
// Only registered components can call this function, if the component isn't register the event won't be emitted
uint256 componentId = _componentAddressToId[msg.sender];
if (componentId == 0) {
revert ComponentNotRegistered(msg.sender);
}
// Store reference of entity to component
_entityToComponents[entity].add(componentId);
emit ComponentValueSet(componentId, entity, data);
}
/**
* @inheritdoc IGameRegistry
* @dev Only registered components can call this function, otherwise it will revert
*/
function publishComponentValueSet(
uint256 componentId,
uint256 entity,
bytes calldata data
) external virtual onlyRole(GAME_LOGIC_CONTRACT_ROLE) returns (uint256 requestId) {
if (_componentIdToAddress[componentId] == address(0)) {
revert ComponentIdNotRegistered(componentId);
}
requestId = _generateGUID();
emit PublishComponentValueSet(requestId, componentId, entity, block.chainid, block.timestamp, data);
}
/**
* @inheritdoc IGameRegistry
* @dev Only registered components can call this function, otherwise it will revert
*/
function batchRegisterComponentValueSet(uint256[] calldata entities, bytes[] calldata data) external virtual {
// Check to make sure the component is registered
uint256 componentId = _componentAddressToId[msg.sender];
if (componentId == 0) {
revert ComponentNotRegistered(msg.sender);
}
if (entities.length != data.length) {
revert InvalidBatchData(entities.length, data.length);
}
// Store references of entities to component
for (uint256 i = 0; i < entities.length; i++) {
_entityToComponents[entities[i]].add(componentId);
}
emit BatchComponentValueSet(componentId, entities, data);
}
/**
* @inheritdoc IGameRegistry
* @dev Only registered components can call this function, otherwise it will revert
*/
function batchPublishComponentValueSet(
uint256 componentId,
uint256[] calldata entities,
bytes[] calldata data
) external virtual onlyRole(GAME_LOGIC_CONTRACT_ROLE) returns (uint256 requestId) {
if (_componentIdToAddress[componentId] == address(0)) {
revert ComponentIdNotRegistered(componentId);
}
// Check to make sure the component is registered
if (entities.length != data.length) {
revert InvalidBatchData(entities.length, data.length);
}
requestId = _generateGUID();
emit PublishBatchComponentValueSet(requestId, componentId, block.chainid, block.timestamp, entities, data);
}
/**
* @inheritdoc IGameRegistry
* @dev Only registered components can call this function, otherwise it will revert
*/
function registerComponentValueRemoved(uint256 entity) external virtual {
// Only registered components can call this function, if the component isn't register the event won't be emitted
uint256 componentId = _componentAddressToId[msg.sender];
if (componentId == 0) {
revert ComponentNotRegistered(msg.sender);
}
// Remove reference of entity to component
_entityToComponents[entity].remove(componentId);
emit ComponentValueRemoved(componentId, entity);
}
/**
* @inheritdoc IGameRegistry
* @dev Only registered components can call this function, otherwise it will revert
*/
function batchRegisterComponentValueRemoved(uint256[] calldata entities) external virtual {
uint256 componentId = _componentAddressToId[msg.sender];
if (componentId == 0) {
revert ComponentNotRegistered(msg.sender);
}
// Store references of entities to component
for (uint256 i = 0; i < entities.length; i++) {
_entityToComponents[entities[i]].remove(componentId);
}
emit BatchComponentValueRemoved(componentId, entities);
}
/**
* @notice Emits an event which oracles pick up to mint the item on another chian.
* @dev Only registered components can call this function, otherwise it will revert
*/
function sendMultichain1155TransferSingle(
uint256 systemId,
address from,
address to,
uint256 toChainId,
uint256 id,
uint256 amount
) external onlyRole(GAME_LOGIC_CONTRACT_ROLE) {
if (to == address(0)) {
return;
}
uint256 requestId = _generateGUID();
if (address(_systemRegistry[systemId]) != msg.sender) {
revert InvalidSystem(systemId);
}
emit Multichain1155TransferSingleSent(requestId, systemId, from, to, toChainId, id, amount);
}
/**
* @notice Emits an event which oracles pick up to mint the item on another chian.
* @dev Only registered components can call this function, otherwise it will revert
*/
function sendMultichain1155TransferBatch(
uint256 systemId,
address from,
address to,
uint256 toChainId,
uint256[] calldata ids,
uint256[] calldata amounts
) external onlyRole(GAME_LOGIC_CONTRACT_ROLE) {
if (to == address(0)) {
return;
}
uint256 requestId = _generateGUID();
if (address(_systemRegistry[systemId]) != msg.sender) {
revert InvalidSystem(systemId);
}
emit Multichain1155TransferBatchSent(requestId, systemId, from, to, toChainId, ids, amounts);
}
/**
* @notice Emits an event which oracles pick up to mint the item on another chian.
* @dev Only registered components can call this function, otherwise it will revert
*/
function sendMultichain721Transfer(
uint256 systemId,
address from,
address to,
uint256 tokenId,
uint256 toChainId
) external onlyRole(GAME_LOGIC_CONTRACT_ROLE) {
if (to == address(0)) {
return;
}
uint256 requestId = _generateGUID();
if (address(_systemRegistry[systemId]) != msg.sender) {
revert InvalidSystem(systemId);
}
emit Multichain721TransferSent(requestId, systemId, from, to, tokenId, toChainId);
}
/**
* @notice Delivers an item that has been transferred from another chain in the Multichain
* @dev Must be called by a trusted multichain
*/
function deliverMultichain1155TransferSingle(
uint256 requestId,
uint256 systemId,
address from,
address to,
uint256 fromChainId,
uint256 id,
uint256 amount
) external onlyRole(TRUSTED_MULTICHAIN_ORACLE_ROLE) {
_enforceChain(to);
//replay protection
_validateRequestId(requestId);
// mint items
if (_systemRegistry[systemId] == address(0)) {
revert InvalidSystem(systemId);
}
IMultichain1155 system = IMultichain1155(_systemRegistry[systemId]);
system.receivedMultichain1155TransferSingle(to, id, amount);
emit Multichain1155TransferSingleReceived(requestId, systemId, from, to, fromChainId, id, amount);
}
/**
* @notice Delivers an item that has been transferred from another chain in the Multichain
* @dev Must be called by a trusted multichain
*/
function deliverMultichain1155TransferBatch(
uint256 requestId,
uint256 systemId,
address from,
address to,
uint256 fromChainId,
uint256[] calldata ids,
uint256[] calldata amounts
) external onlyRole(TRUSTED_MULTICHAIN_ORACLE_ROLE) {
_enforceChain(to);
//replay protection
_validateRequestId(requestId);
// mint items
if (_systemRegistry[systemId] == address(0)) {
revert InvalidSystem(systemId);
}
IMultichain1155 system = IMultichain1155(_systemRegistry[systemId]);
system.receivedMultichain1155TransferBatch(to, ids, amounts);
emit Multichain1155TransferBatchReceived(requestId, systemId, from, to, fromChainId, ids, amounts);
}
/**
* @notice Delivers an item that has been transferred from another chain in the Multichain
* @dev Must be called by a trusted multichain
*/
function deliverMultichain721Transfer(
uint256 requestId,
uint256 systemId,
address from,
address to,
uint256 tokenId,
uint256 fromChainId,
BatchComponentData calldata componentData
) external onlyRole(TRUSTED_MULTICHAIN_ORACLE_ROLE) {
_enforceChain(to);
_validateRequestId(requestId);
if (_systemRegistry[systemId] == address(0)) {
revert InvalidSystem(systemId);
}
// mint items
IMultichain721 system = IMultichain721(_systemRegistry[systemId]);
system.receivedMultichain721Transfer(to, tokenId);
_batchSetComponentData(componentData);
emit Multichain721TransferReceived(requestId, systemId, from, to, tokenId, fromChainId);
}
/**
* @inheritdoc IGameRegistry
*/
function getComponent(uint256 componentId) external view returns (address) {
return _componentIdToAddress[componentId];
}
/**
* @inheritdoc IGameRegistry
*/
function getComponentIdFromAddress(address componentAddr) external view returns (uint256) {
return _componentAddressToId[componentAddr];
}
/**
* @inheritdoc IGameRegistry
*/
function getEntityHasComponent(uint256 entity, uint256 componentId) external view returns (bool) {
return _entityToComponents[entity].contains(componentId);
}
/**
* @inheritdoc IGameRegistry
*/
function batchGetEntitiesHasComponents(
uint256[] calldata entities,
uint256[] calldata componentIds
) external view returns (bool[] memory) {
bool[] memory results = new bool[](entities.length);
for (uint256 i = 0; i < entities.length; i++) {
results[i] = _entityToComponents[entities[i]].contains(componentIds[i]);
}
return results;
}
/**
* @inheritdoc IGameRegistry
*/
function getEntityComponents(uint256 entity) external view returns (uint256[] memory) {
return _entityToComponents[entity].values();
}
/**
* @dev Will filter out archived components and return only unarchived components
*/
function getUnarchivedEntityComponents(uint256 entity) external view returns (uint256[] memory) {
uint256[] memory componentIds = _entityToComponents[entity].values();
ArchivedComponent archivedComponent = ArchivedComponent(_componentIdToAddress[ARCHIVED_COMPONENT_ID]);
uint256[] memory unarchivedComponentIds = new uint256[](componentIds.length);
uint256 counter = 0;
for (uint256 i = 0; i < componentIds.length; i++) {
if (archivedComponent.getValue(componentIds[i])) {
continue;
}
unarchivedComponentIds[counter] = componentIds[i];
counter++;
}
assembly {
mstore(unarchivedComponentIds, counter)
}
return unarchivedComponentIds;
}
/**
* @inheritdoc IGameRegistry
*/
function getEntityComponentCount(uint256 entity) external view returns (uint256) {
return _entityToComponents[entity].length();
}
/**
* @inheritdoc IGameRegistry
*/
function generateGUIDDeprecated() external onlyRole(GAME_LOGIC_CONTRACT_ROLE) returns (uint256) {
_guidCounter++;
uint256 guidEntity = EntityLibrary.tokenToEntity(address(this), _guidCounter);
return guidEntity;
}
/**
* @inheritdoc IERC165
*/
function supportsInterface(
bytes4 interfaceId
) public view virtual override(IERC165, AccessControlUpgradeable) returns (bool) {
return
interfaceId == type(IGameRegistry).interfaceId ||
interfaceId == type(IERC165).interfaceId ||
AccessControlUpgradeable.supportsInterface(interfaceId);
}
/**
* @dev Returns `true` if `account` has been granted `role`.
*/
function hasAccessRole(bytes32 role, address account) public view override returns (bool) {
return AccessControlUpgradeable.hasRole(role, account);
}
/**
* Returns the address of the account the operatorAddress is authorized to
*
* @param operatorAddress Address the sending controller
*/
function getPlayerAccount(address operatorAddress) external view returns (address) {
if (operatorAddress == address(0)) {
revert InvalidOperatorAddress();
}
PlayerAccount memory account = _operatorToPlayerAccount[operatorAddress];
address playerAddress = account.playerAddress;
if (playerAddress != address(0)) {
if (account.expiration < block.timestamp && account.expiration != 0) {
revert OperatorExpired();
}
} else {
return operatorAddress;
}
return playerAddress;
}
/**
* Called in order to retrieve message to sign to register an oeperator
*
* @param player address operator is being registered for
* @param operator address of operator being registered
* @param expiration block time for registration (or 0 for infinite)
* @param blockNumber the message was signed at
*/
function getOperatorAccountRegistrationMessageToSign(
address player,
address operator,
uint256 expiration,
uint256 blockNumber
) public pure returns (bytes memory) {
return
abi.encodePacked(
'Authorize operator account ',
Strings.toHexString(uint256(uint160(operator)), 20),
' to perform gameplay actions on behalf of player account ',
Strings.toHexString(uint256(uint160(player)), 20),
' with expiration ',
Strings.toString(expiration),
' signed at block ',
Strings.toString(blockNumber)
);
}
/**
* Called by an Operator Address with a signature from a Player Address authorizing it until a given expiration time
*
* @param signature from signer/player address authorizing operator until expiration time
* @param player address of player being registered
* @param operator address of operator being registered
* @param expiration block time for registration (or 0 for infinite)
* @param blockNumber the message was signed at
*/
function registerOperator(
bytes calldata signature,
address player,
address operator,
uint256 expiration,
uint256 blockNumber
) external whenNotPaused {
if (_msgSender() != operator) {
revert InvalidCaller();
}
if ((block.timestamp - lastRegisterOperatorTime[player]) < REGISTER_OPERATOR_COOLDOWN_LIMIT) {
revert RegisterOperatorInCooldown();
}
if (operator == player || operator == address(0)) {
revert InvalidOperatorAddress();
}
if (expiration < block.timestamp && expiration != 0) {
revert InvalidExpirationTimestamp();
}
// if (blockNumber > block.number) {
// revert InvalidBlockNumber();
// }
// if (block.number > blockNumber + OPERATOR_MESSAGE_BLOCK_LIMIT) {
// revert InvalidExpirationBlockNumber();
// }
PlayerAccount memory currentAccount = _operatorToPlayerAccount[operator];
if (currentAccount.playerAddress != address(0) && currentAccount.playerAddress != player) {
revert OperatorAlreadyRegistered();
}
bytes memory message = getOperatorAccountRegistrationMessageToSign(player, operator, expiration, blockNumber);
bytes32 digest = ECDSA.toEthSignedMessageHash(message);
address recoveredSigner = ECDSA.recover(digest, signature);
if (player != recoveredSigner) {
revert PlayerSignerMismatch(player, recoveredSigner);
}
_operatorToPlayerAccount[operator] = PlayerAccount({playerAddress: player, expiration: expiration});
_playerToOperatorAddresses[player].add(operator);
// Track cooldown timer
lastRegisterOperatorTime[player] = block.timestamp;
emit OperatorRegistered(player, operator, expiration);
}
/**
* Batch set operator accounts for players
* @param players addresses of players being registered
* @param operators addresses of operators being registered
* @param expirations block times for registration (or 0 for infinite)
*/
function registerOperatorBatch(
address[] calldata players,
address[] calldata operators,
uint256[] calldata expirations
) external whenNotPaused onlyRole(MANAGER_ROLE) {
if (players.length != operators.length || players.length != expirations.length) {
revert InvalidOperatorAddress();
}
for (uint256 i = 0; i < players.length; i++) {
if (operators[i] == players[i] || operators[i] == address(0)) {
revert InvalidOperatorAddress();
}
_operatorToPlayerAccount[operators[i]] = PlayerAccount({playerAddress: players[i], expiration: expirations[i]});
_playerToOperatorAddresses[players[i]].add(operators[i]);
// Track cooldown timer
lastRegisterOperatorTime[players[i]] = block.timestamp;
emit OperatorRegistered(players[i], operators[i], expirations[i]);
}
}
function deregisterOperatorBatch(address[] calldata operators) public whenNotPaused onlyRole(MANAGER_ROLE) {
for (uint256 i = 0; i < operators.length; i++) {
address playerAddress = _operatorToPlayerAccount[operators[i]].playerAddress;
// if (playerAddress == address(0)) {
// revert OperatorNotRegistered();
// }
delete _operatorToPlayerAccount[operators[i]];
_playerToOperatorAddresses[playerAddress].remove(operators[i]);
// if (operatorRemovedFromPlayer != true) {
// revert OperatorNotRegistered();
// }
emit OperatorDeregistered(operators[i], playerAddress);
}
}
/**
* Called by an Operator or Player to deregister an Operator account
*
* @param operatorToDeregister address of operator to deregister
*/
function deregisterOperator(address operatorToDeregister) external {
address playerAddress = _operatorToPlayerAccount[operatorToDeregister].playerAddress;
if (playerAddress == address(0)) {
revert OperatorNotRegistered();
}
if (operatorToDeregister != _msgSender() && playerAddress != _msgSender()) {
revert InvalidDeregisterCaller();
}
delete _operatorToPlayerAccount[operatorToDeregister];
bool operatorRemovedFromPlayer = _playerToOperatorAddresses[playerAddress].remove(operatorToDeregister);
if (operatorRemovedFromPlayer != true) {
revert OperatorNotRegistered();
}
emit OperatorDeregistered(operatorToDeregister, playerAddress);
}
/**
* Returns an array of registered Operators for a Player address
*
* @param player address to retrieve operators for
*/
function getRegisteredOperators(address player) external view returns (address[] memory) {
return _playerToOperatorAddresses[player].values();
}
/// @inheritdoc IERC2771Recipient
function isTrustedForwarder(address forwarder) public view virtual override returns (bool) {
return hasAccessRole(TRUSTED_FORWARDER_ROLE, forwarder);
}
/// @inheritdoc IERC2771Recipient
function _msgSender() internal view virtual override(ContextUpgradeable, IERC2771Recipient) returns (address ret) {
if (msg.data.length >= 20 && isTrustedForwarder(msg.sender)) {
assembly {
ret := shr(96, calldataload(sub(calldatasize(), 20)))
}
} else {
ret = msg.sender;
}
}
/// @inheritdoc IERC2771Recipient
function _msgData()
internal
view
virtual
override(ContextUpgradeable, IERC2771Recipient)
returns (bytes calldata ret)
{
if (msg.data.length >= 20 && isTrustedForwarder(msg.sender)) {
return msg.data[0:msg.data.length - 20];
} else {
return msg.data;
}
}
function _generateGUID() internal returns (uint256) {
GuidCounterComponent counter = GuidCounterComponent(_componentIdToAddress[GUID_COUNTER_COMPONENT_ID]);
// Increment guid counter
uint256 count = counter.getValue(GUID_PREFIX) + 1;
counter.setValue(GUID_PREFIX, count);
return GUIDLibrary.packGuid(GUID_PREFIX, count);
}
function _enforceChain(address to) internal view {
uint256 userToChainId = ChainIdComponent(_componentIdToAddress[CHAIN_ID_COMPONENT_ID]).getValue(
EntityLibrary.addressToEntity(to)
);
if (userToChainId != block.chainid) {
revert InvalidChain(userToChainId);
}
}
function _validateRequestId(uint256 requestId) internal {
if (requestIdProcessed[requestId]) {
revert AlreadyProcessed(requestId);
}
requestIdProcessed[requestId] = true;
}
function _batchSetComponentData(BatchComponentData calldata componentData) internal {
_batchSetComponentValue(componentData.entities, componentData.componentIds, componentData.data);
}
function _batchSetComponentValue(
uint256[] calldata entities,
uint256[] calldata componentIds,
bytes[] calldata values
) internal {
if (entities.length != values.length || entities.length != componentIds.length) {
revert InvalidBatchData(entities.length, values.length);
}
for (uint256 i = 0; i < entities.length; i++) {
address componentAddress = _componentIdToAddress[componentIds[i]];
if (componentAddress == address(0)) {
revert ComponentNotRegistered(componentAddress);
}
IComponent(componentAddress).setBytes(entities[i], values[i]);
}
}
}
// SPDX-License-Identifier: MIT LICENSE
pragma solidity ^0.8.9;
// Used for calculating decimal-point percentages (10000 = 100%)
uint256 constant PERCENTAGE_RANGE = 10000;
// 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');
// Game Currency Contract - Allowlisted currency ERC20 contract
bytes32 constant GAME_CURRENCY_CONTRACT_ROLE = keccak256('GAME_CURRENCY_CONTRACT_ROLE');
// Game NFT Contract - Allowlisted game NFT ERC721 contract
bytes32 constant GAME_NFT_CONTRACT_ROLE = keccak256('GAME_NFT_CONTRACT_ROLE');
// Game Items Contract - Allowlist game items ERC1155 contract
bytes32 constant GAME_ITEMS_CONTRACT_ROLE = keccak256('GAME_ITEMS_CONTRACT_ROLE');
// Depositor role - used by Polygon bridge to mint on child chain
bytes32 constant DEPOSITOR_ROLE = keccak256('DEPOSITOR_ROLE');
// Randomizer role - Used by the randomizer contract to callback
bytes32 constant VRF_SYSTEM_ROLE = keccak256('VRF_SYSTEM_ROLE');
// VRF Consumer Contract - Contract that uses VRF to demonstrate randomness
bytes32 constant VRF_CONSUMER_ROLE = keccak256('VRF_CONSUMER_ROLE');
// Trusted forwarder role - Used by meta transactions to verify trusted forwader(s)
bytes32 constant TRUSTED_FORWARDER_ROLE = keccak256('TRUSTED_FORWARDER_ROLE');
// Trusted mirror role - Used by pirate mirroring
bytes32 constant TRUSTED_MIRROR_ROLE = keccak256('TRUSTED_MIRROR_ROLE');
// Trusted multichain oracle role - Used by multichain contracts
bytes32 constant TRUSTED_MULTICHAIN_ORACLE_ROLE = keccak256('TRUSTED_MULTICHAIN_ORACLE_ROLE');
// =====
// All of the possible traits in the system
// =====
/// @dev Trait that points to another token/template id
uint256 constant TEMPLATE_ID_TRAIT_ID = uint256(keccak256('template_id'));
// Generation of a token
uint256 constant GENERATION_TRAIT_ID = uint256(keccak256('generation'));
// XP for a token
uint256 constant XP_TRAIT_ID = uint256(keccak256('xp'));
// Current level of a token
uint256 constant LEVEL_TRAIT_ID = uint256(keccak256('level'));
// Whether or not a token is a pirate
uint256 constant IS_PIRATE_TRAIT_ID = uint256(keccak256('is_pirate'));
// Whether or not a token is a ship
uint256 constant IS_SHIP_TRAIT_ID = uint256(keccak256('is_ship'));
// Whether or not an item is equippable on ships
uint256 constant EQUIPMENT_TYPE_TRAIT_ID = uint256(keccak256('equipment_type'));
// Combat modifiers for items and tokens
uint256 constant COMBAT_MODIFIERS_TRAIT_ID = uint256(keccak256('combat_modifiers'));
// Animation URL for the token
uint256 constant ANIMATION_URL_TRAIT_ID = uint256(keccak256('animation_url'));
// Item slots
uint256 constant ITEM_SLOTS_TRAIT_ID = uint256(keccak256('item_slots'));
// Rank of the ship
uint256 constant SHIP_RANK_TRAIT_ID = uint256(keccak256('ship_rank'));
// Current Health trait
uint256 constant CURRENT_HEALTH_TRAIT_ID = uint256(keccak256('current_health'));
// Health trait
uint256 constant HEALTH_TRAIT_ID = uint256(keccak256('health'));
// Damage trait
uint256 constant DAMAGE_TRAIT_ID = uint256(keccak256('damage'));
// Speed trait
uint256 constant SPEED_TRAIT_ID = uint256(keccak256('speed'));
// Accuracy trait
uint256 constant ACCURACY_TRAIT_ID = uint256(keccak256('accuracy'));
// Evasion trait
uint256 constant EVASION_TRAIT_ID = uint256(keccak256('evasion'));
// Image hash of token's image, used for verifiable / fair drops
uint256 constant IMAGE_HASH_TRAIT_ID = uint256(keccak256('image_hash'));
// Name of a token
uint256 constant NAME_TRAIT_ID = uint256(keccak256('name_trait'));
// Description of a token
uint256 constant DESCRIPTION_TRAIT_ID = uint256(keccak256('description_trait'));
// General rarity for a token (corresponds to IGameRarity)
uint256 constant RARITY_TRAIT_ID = uint256(keccak256('rarity'));
// The character's affinity for a specific element
uint256 constant ELEMENTAL_AFFINITY_TRAIT_ID = uint256(keccak256('affinity_id'));
// The character's expertise value
uint256 constant EXPERTISE_TRAIT_ID = uint256(keccak256('expertise_id'));
// Expertise damage mod ID from SoT
uint256 constant EXPERTISE_DAMAGE_ID = uint256(keccak256('expertise.levelmultiplier.damage'));
// Expertise evasion mod ID from SoT
uint256 constant EXPERTISE_EVASION_ID = uint256(keccak256('expertise.levelmultiplier.evasion'));
// Expertise speed mod ID from SoT
uint256 constant EXPERTISE_SPEED_ID = uint256(keccak256('expertise.levelmultiplier.speed'));
// Expertise accuracy mod ID from SoT
uint256 constant EXPERTISE_ACCURACY_ID = uint256(keccak256('expertise.levelmultiplier.accuracy'));
// Expertise health mod ID from SoT
uint256 constant EXPERTISE_HEALTH_ID = uint256(keccak256('expertise.levelmultiplier.health'));
// Boss start time trait
uint256 constant BOSS_START_TIME_TRAIT_ID = uint256(keccak256('boss_start_time'));
// Boss end time trait
uint256 constant BOSS_END_TIME_TRAIT_ID = uint256(keccak256('boss_end_time'));
// Boss type trait
uint256 constant BOSS_TYPE_TRAIT_ID = uint256(keccak256('boss_type'));
// The character's dice rolls
uint256 constant DICE_ROLL_1_TRAIT_ID = uint256(keccak256('dice_roll_1'));
uint256 constant DICE_ROLL_2_TRAIT_ID = uint256(keccak256('dice_roll_2'));
// The character's star sign (astrology)
uint256 constant STAR_SIGN_TRAIT_ID = uint256(keccak256('star_sign'));
// Image for the token
uint256 constant IMAGE_TRAIT_ID = uint256(keccak256('image_trait'));
// How much energy the token provides if used
uint256 constant ENERGY_PROVIDED_TRAIT_ID = uint256(keccak256('energy_provided'));
// Whether a given token is soulbound, meaning it is unable to be transferred
uint256 constant SOULBOUND_TRAIT_ID = uint256(keccak256('soulbound'));
// ------
// Avatar Profile Picture related traits
// If an avatar is a 1 of 1, this is their only trait
uint256 constant PROFILE_IS_LEGENDARY_TRAIT_ID = uint256(keccak256('profile_is_legendary'));
// Avatar's archetype -- possible values: Human (including Druid, Mage, Berserker, Crusty), Robot, Animal, Zombie, Vampire, Ghost
uint256 constant PROFILE_CHARACTER_TYPE = uint256(keccak256('profile_character_type'));
// Avatar's profile picture's background image
uint256 constant PROFILE_BACKGROUND_TRAIT_ID = uint256(keccak256('profile_background'));
// Avatar's eye style
uint256 constant PROFILE_EYES_TRAIT_ID = uint256(keccak256('profile_eyes'));
// Avatar's facial hair type
uint256 constant PROFILE_FACIAL_HAIR_TRAIT_ID = uint256(keccak256('profile_facial_hair'));
// Avatar's hair style
uint256 constant PROFILE_HAIR_TRAIT_ID = uint256(keccak256('profile_hair'));
// Avatar's skin color
uint256 constant PROFILE_SKIN_TRAIT_ID = uint256(keccak256('profile_skin'));
// Avatar's coat color
uint256 constant PROFILE_COAT_TRAIT_ID = uint256(keccak256('profile_coat'));
// Avatar's earring(s) type
uint256 constant PROFILE_EARRING_TRAIT_ID = uint256(keccak256('profile_facial_hair'));
// Avatar's eye covering
uint256 constant PROFILE_EYE_COVERING_TRAIT_ID = uint256(keccak256('profile_eye_covering'));
// Avatar's headwear
uint256 constant PROFILE_HEADWEAR_TRAIT_ID = uint256(keccak256('profile_headwear'));
// Avatar's (Mages only) gem color
uint256 constant PROFILE_MAGE_GEM_TRAIT_ID = uint256(keccak256('profile_mage_gem'));
// ------
// Dungeon traits
// Whether this token template is a dungeon trigger
uint256 constant IS_DUNGEON_TRIGGER_TRAIT_ID = uint256(keccak256('is_dungeon_trigger'));
// Dungeon start time trait
uint256 constant DUNGEON_START_TIME_TRAIT_ID = uint256(keccak256('dungeon.start_time'));
// Dungeon end time trait
uint256 constant DUNGEON_END_TIME_TRAIT_ID = uint256(keccak256('dungeon.end_time'));
// Dungeon SoT map id trait
uint256 constant DUNGEON_MAP_TRAIT_ID = uint256(keccak256('dungeon.map_id'));
// Whether this token template is a mob
uint256 constant IS_MOB_TRAIT_ID = uint256(keccak256('is_mob'));
// ------
// Island traits
// Whether a game item is placeable on an island
uint256 constant IS_PLACEABLE_TRAIT_ID = uint256(keccak256('is_placeable'));
// ------
// Extra traits for component migration
// NOTE: CURRENTLY NOT USED IN CONTRACTS CODE
uint256 constant MODEL_GLTF_URL_TRAIT_ID = uint256(keccak256('model_gltf_url'));
uint256 constant PLACEABLE_CATEGORY_TRAIT_ID = uint256(keccak256('placeable_category'));
uint256 constant PLACEABLE_IS_BOTTOM_STACKABLE_TRAIT_ID = uint256(keccak256('placeable.is_bottom_stackable'));
uint256 constant PLACEABLE_IS_TOP_STACKABLE_TRAIT_ID = uint256(keccak256('placeable.is_top_stackable'));
uint256 constant PLACEABLE_TERRAIN_TRAIT_ID = uint256(keccak256('placeable.terrain'));
uint256 constant GLTF_SCALING_FACTOR_TRAIT_ID = uint256(keccak256('gltf_scaling_factor'));
uint256 constant SIZE_TRAIT_ID = uint256(keccak256('size'));
// SPDX-License-Identifier: MIT LICENSE
pragma solidity ^0.8.13;
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) external;
/**
* @param systemId Id of the system
* @return System based on an id
*/
function getSystem(uint256 systemId) external view returns (address);
/**
* Registers a component using an id and contract address
* @param componentId Id of the component to register
* @param componentAddress Address of the component contract
*/
function registerComponent(
uint256 componentId,
address componentAddress
) external;
/**
* @param componentId Id of the component
* @return A component's contract address given its ID
*/
function getComponent(uint256 componentId) external view returns (address);
/**
* @param componentAddr Address of the component contract
* @return A component's id given its contract address
*/
function getComponentIdFromAddress(
address componentAddr
) external view returns (uint256);
/**
* @param entity Entity to check
* @param componentId Component to check
* @return Boolean indicating if entity belongs to component
*/
function getEntityHasComponent(
uint256 entity,
uint256 componentId
) external view returns (bool);
/**
* @return Boolean array indicating if entity belongs to component
* @param entities Entities to check
* @param componentIds Components to check
*/
function batchGetEntitiesHasComponents(
uint256[] calldata entities,
uint256[] calldata componentIds
) external view returns (bool[] memory);
/**
* Sets multiple component values at once
* @param entities Entities to set values for
* @param componentIds Component to set value on
* @param values Values to set
*/
function batchSetComponentValue(
uint256[] calldata entities,
uint256[] calldata componentIds,
bytes[] calldata values
) external;
/**
* Sets multiple component values at once and emits a publish event (for cross-chain)
* @param entities Entities to set values for
* @param componentIds Component to set value on
* @param values Values to set
*/
function batchPublishSetComponentValue(
uint256[] calldata entities,
uint256[] calldata componentIds,
bytes[] calldata values
) external returns (uint256 requestId);
/**
* @param componentId Id of the component
* @return Entire array of components belonging an entity
*
* WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
* to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
* this function has an unbounded cost, and using it as part of a state-changing function may render the function
* uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.
*/
function getEntityComponents(
uint256 componentId
) external view returns (uint256[] memory);
/**
* @param componentId Id of the component
* @return Number of components belonging to an entity
*/
function getEntityComponentCount(
uint256 componentId
) external view returns (uint256);
/**
* Gets multiple component values at once
* @param entities Entities to get values for
* @param componentIds Component to get value from
*/
function batchGetComponentValues(
uint256[] calldata entities,
uint256[] calldata componentIds
) external view returns (bytes[] memory values);
/**
* Register a component value update.
* Emits the `ComponentValueSet` event for clients to reconstruct the state.
* @param entity Entity to update
* @param data Data to update
*/
function registerComponentValueSet(
uint256 entity,
bytes calldata data
) external;
/**
* Emit a component value update across chains.
* Emits the `PublishComponentValueSet` event for cross-chain clients to reconstruct the state.
* @param entity Entity to update
* @param data Data to update
*/
function publishComponentValueSet(
uint256 componentId,
uint256 entity,
bytes calldata data
) external returns (uint256);
/**
* Register a component batch value update.
* Emits the `ComponentBatchValueSet` event for clients to reconstruct the state.
* @param entities Entities to update
* @param data Data to update
*/
function batchRegisterComponentValueSet(
uint256[] calldata entities,
bytes[] calldata data
) external;
/**
* Emit a component batch value update across chains.
* Emits the `PublishComponentBatchValueSet` event for cross-chain clients to reconstruct the state.
* @param entities Entities to update
* @param data Data to update
*/
function batchPublishComponentValueSet(
uint256 componentId,
uint256[] calldata entities,
bytes[] calldata data
) external returns (uint256);
/**
* Register a component value removal.
* Emits the `ComponentValueRemoved` event for clients to reconstruct the state.
*/
function registerComponentValueRemoved(uint256 entity) external;
/**
* Emit a component value removal across chains.
* Emits the `PublishComponentValueRemoved` event for cross-chain clients to reconstruct the state.
*/
// TODO: Reenable when we're ready to support cross-chain removal
// function publishComponentValueRemoved(
// uint256 componentId,
// uint256 entity
// ) external returns (uint256);
/**
* Register a component batch value removal.
* Emits the `ComponentBatchValueRemoved` event for clients to reconstruct the state.
* @param entities Entities to update
*/
function batchRegisterComponentValueRemoved(
uint256[] calldata entities
) external;
/**
* Emit a component batch value removal across chains.
* Emits the `PublishComponentBatchValueRemoved` event for cross-chain clients to reconstruct the state.
* @param entities Entities to update
*/
// TODO: Reenable when we're ready to support cross-chain removal
// function batchPublishComponentValueRemoved(
// uint256 componentId,
// uint256[] calldata entities
// ) external returns (uint256);
/**
* DEPRECATED: Generate a new general-purpose entity GUID
*/
function generateGUIDDeprecated() external returns (uint256);
/**
*
* @param operatorAddress Address of the Operator account
* @return Authorized Player account for an address
*/
function getPlayerAccount(
address operatorAddress
) external view returns (address);
/**
* @notice Sends a transfer to another chain in the multichain
* @param systemId Id of the 1155 System (Must implement IMultichain1155)
* @param from From address of the user sending the token
* @param to To address of the user receiving the token
* @param toChainId Chain ID of the receiving chain
* @param id Array of token IDs to send
* @param amount Array of token amounts to send
*/
function sendMultichain1155TransferSingle(
uint256 systemId,
address from,
address to,
uint256 toChainId,
uint256 id,
uint256 amount
) external;
/**
* @notice Sends a transfer to another chain in the multichain
* @param systemId Id of the 1155 System (Must implement IMultichain1155)
* @param from From address of the user sending the token
* @param to To address of the user receiving the token
* @param toChainId Chain ID of the receiving chain
* @param ids Array of token IDs to send
* @param amounts Array of token amounts to send
*/
function sendMultichain1155TransferBatch(
uint256 systemId,
address from,
address to,
uint256 toChainId,
uint256[] calldata ids,
uint256[] calldata amounts
) external;
/**
* @notice Sends a transfer to another chain in the multichain
* @param systemId Id of the 1155 System (Must implement Multichain721)
* @param from From address of the user sending the token
* @param to To address of the user receiving the token
* @param tokenId the tokenId being transferred
* @param toChainId Chain ID of the receiving chain
*/
function sendMultichain721Transfer(
uint256 systemId,
address from,
address to,
uint256 tokenId,
uint256 toChainId
) external;
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;
uint256 constant MAX_UINT96 = 2 ** 96 - 1;
/** @title Entity related helpers **/
library EntityLibrary {
/** ERRORS **/
error TokenIdExceedsMaxValue(uint256 tokenId);
/** INTERNAL **/
/**
* @dev Note this function will require the tokenId is < uint96.MAX
* Unpacks a token address from a single uint256 which is the entity ID
*
* @return tokenAddress Address of the unpacked token
*/
function entityToAddress(
uint256 value
) internal pure returns (address tokenAddress) {
tokenAddress = address(uint160(value));
uint256 tokenId = uint256(value >> 160);
uint256 verify = (tokenId << 160) | uint160(tokenAddress);
require(verify == value);
}
/**
* Packs an address into a single uint256 entity
*
* @param addr Address to convert to entity
* @return Converted address to entity
*/
function addressToEntity(address addr) internal pure returns (uint256) {
return uint160(addr);
}
/**
* @dev Note this function will require the tokenId is < uint96.MAX
* Unpacks a token address and token id from a single uint256
*
* @return tokenAddress Address of the unpacked token
* @return tokenId Id of the unpacked token
*/
function entityToToken(
uint256 value
) internal pure returns (address tokenAddress, uint256 tokenId) {
tokenAddress = address(uint160(value));
tokenId = uint256(value >> 160);
uint256 verify = (tokenId << 160) | uint160(tokenAddress);
require(verify == value);
}
/**
* @dev Note this function will require the tokenId is < uint96.MAX
* Packs a token address and token id into a single uint256
*
* @param tokenAddress Address of the unpacked token
* @param tokenId Id of the unpacked token
* @return Token address and token id packed into single uint256
*/
function tokenToEntity(
address tokenAddress,
uint256 tokenId
) internal pure returns (uint256) {
if (tokenId > MAX_UINT96) {
revert TokenIdExceedsMaxValue(tokenId);
}
return (tokenId << 160) | uint160(tokenAddress);
}
/**
* @dev Pack an account and entity ID together and keccak256 hash them, then return the uint value of the hash
* @param account Account address
* @param entity Entity ID
* @return Uint256 value of the keccak256 hash
*/
function accountSubEntity(
address account,
uint256 entity
) internal pure returns (uint256) {
return uint256((keccak256(abi.encodePacked(account, entity))));
}
}
// SPDX-License-Identifier: MIT LICENSE
pragma solidity ^0.8.9;
import "@openzeppelin/contracts/utils/Strings.sol";
import {IGameRegistry} from "./IGameRegistry.sol";
import {CounterComponent, Layout as CounterLayout, ID as COUNTER_COMPONENT_ID} from "../generated/components/CounterComponent.sol";
import {GuidCounterComponent, ID as GUID_COUNTER_COMPONENT_ID} from "../generated/components/GuidCounterComponent.sol";
string constant GUID_PREFIX = "game.piratenation.guid.";
/// @notice Error thrown when the counter is too large to fit in 144 bits
error CounterOverflow(uint256 counter);
/// @notice Error thrown when the chain ID is too large to fit in 32 bits
error ChainIdOverflow(uint256 chainId);
/// @notice Error thrown when the prefix is too large to fit in 80 bits
error PrefixOverflow(uint256 prefix);
/**
* Common helper functions for dealing with GUIDS
*/
library GUIDLibrary {
/**
* @dev DEPRECATED: Increments the counter for a given key and returns a new GUID
* @dev WARNING: Does NOT generated cross-chain safe guids
* @param gameRegistry Address of the Counter component
* @param key A prefix to namespace the GUID and prevent collisions
*/
function guidV1(
IGameRegistry gameRegistry,
string memory key
) internal returns (uint256) {
string memory prefix = string.concat(GUID_PREFIX, key);
uint256 entity = uint256(keccak256(abi.encodePacked(prefix)));
CounterComponent counterComponent = CounterComponent(
gameRegistry.getComponent(COUNTER_COMPONENT_ID)
);
// Increment counter
uint256 ct = counterComponent.getValue(entity) + 1;
counterComponent.setValue(entity, ct);
// Return new guid
return
uint256(
keccak256(
abi.encodePacked(
string.concat(prefix, ".", Strings.toString(ct))
)
)
);
}
/**
* Increments the counter for a given prefix and returns a new multi-chain safe GUID
* @param gameRegistry Address of the GuidCounter component
* @param prefix A prefix to namespace the GUID and prevent collisions
*/
function guid(
IGameRegistry gameRegistry,
uint80 prefix
) internal returns (uint256) {
GuidCounterComponent counter = GuidCounterComponent(
gameRegistry.getComponent(GUID_COUNTER_COMPONENT_ID)
);
// Increment guid counter
uint256 count = counter.getValue(prefix) + 1;
counter.setValue(prefix, count);
return packGuid(prefix, count);
}
/**
* Packs prefix and counter into a multi-chain safe GUID
* @param prefix A prefix to namespace the GUID and prevent collisions
* @param counter A counter to increment the GUID
*/
function packGuid(
uint80 prefix,
uint256 counter
) internal view returns (uint256) {
if (block.chainid > type(uint32).max) {
revert ChainIdOverflow(block.chainid);
}
if (prefix > type(uint80).max) {
revert PrefixOverflow(prefix);
}
if (counter > type(uint144).max) {
revert CounterOverflow(counter);
}
// Pack into a uint256:
// - Chain ID in the highest 32 bits
// - Prefix in the next 80 bits (10 characters)
// - Counter in the lowest 144 bits
return
(uint256(block.chainid) << 224) |
(uint256(prefix) << 144) |
counter;
}
}
// SPDX-License-Identifier: MIT LICENSE
pragma solidity ^0.8.13;
interface IMultichain721 {
/**
* A contract that implements this interface is capable of receiving Multichain721 transfers
* This functiog the item
* @param tokenId id of the items to mintn should mint the item appropriately
* This will be called AFTER generic checks have been made such as
* - Validating to address is on this chain
* - Ensuring replay attacks are prevented
* @param to address of user recievin
*/
function receivedMultichain721Transfer(
address to,
uint256 tokenId
) external;
}
// SPDX-License-Identifier: MIT LICENSE
pragma solidity ^0.8.13;
interface IMultichain1155 {
/**
* A contract that implements this interface is capable of receiving Multichain1155 transfers
* This function should mint the item appropriately
* This will be called AFTER generic checks have been made such as
* - Validating to address is on this chain
* - Ensuring replay attacks are prevented
* @param to address of user recieving the item
* @param id ids of the items to mint
* @param amount amount of the items to mint
*/
function receivedMultichain1155TransferSingle(
address to,
uint256 id,
uint256 amount
) external;
/**
* A contract that implements this interface is capable of receiving Multichain1155 transfers
* This function should mint the item appropriately
* This will be called AFTER generic checks have been made such as
* - Validating to address is on this chain
* - Ensuring replay attacks are prevented
* @param to address of user recieving the item
* @param ids ids of the items to mint
* @param amounts amount of the items to mint
*/
function receivedMultichain1155TransferBatch(
address to,
uint256[] calldata ids,
uint256[] calldata amounts
) external;
}
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0;
import {TypesLibrary} from "../TypesLibrary.sol";
interface IComponent {
/**
* Sets the raw bytes value for this component
*
* @param entity Entity to set value for
* @param value Bytes encoded value for this component
*/
function setBytes(uint256 entity, bytes memory value) external;
/**
* Removes an entity from this component
* @param entity Entity to remove
*/
function remove(uint256 entity) external;
/**
* Whether or not the entity exists in this component
* @param entity Entity to check for
* @return true if the entity exists
*/
function has(uint256 entity) external view returns (bool);
/**
* @param entity Entity to retrieve value for
* @return The raw bytes value for the given entity in this component
*/
function getBytes(uint256 entity) external view returns (bytes memory);
/** Return the keys and value types of the schema of this component. */
function getSchema()
external
pure
returns (
string[] memory keys,
TypesLibrary.SchemaValue[] memory values
);
/**
* Sets the raw bytes value for this component in batches
*
* @param entities Entities to set values for
* @param values Bytes encoded values for this component
*/
function batchSetBytes(
uint256[] calldata entities,
bytes[] calldata values
) external;
}
// SPDX-License-Identifier: MIT
// Auto-generated using Mage CLI codegen (v1) - DO NOT EDIT
pragma solidity ^0.8.13;
import {TypesLibrary} from "../../core/TypesLibrary.sol";
import {BaseStorageComponentV2, IBaseStorageComponentV2} from "../../core/components/BaseStorageComponentV2.sol";
import {GAME_LOGIC_CONTRACT_ROLE} from "../../Constants.sol";
uint256 constant ID = uint256(keccak256("core.mage.archivedcomponent.v1"));
struct Layout {
bool value;
}
library ArchivedComponentStorage {
bytes32 internal constant STORAGE_SLOT = bytes32(ID);
// Declare struct for mapping entity to struct
struct InternalLayout {
mapping(uint256 => Layout) entityIdToStruct;
}
function layout() internal pure returns (InternalLayout storage dataStruct) {
bytes32 position = STORAGE_SLOT;
// solhint-disable-next-line no-inline-assembly
assembly {
dataStruct.slot := position
}
}
}
/**
* @title ArchivedComponent
* @dev maps entity id → boolean if an entity is archived or not
*/
contract ArchivedComponent is BaseStorageComponentV2 {
/** SETUP **/
/** Sets the GameRegistry contract address for this contract */
constructor(
address gameRegistryAddress
) BaseStorageComponentV2(gameRegistryAddress, ID) {
// Do nothing
}
/**
* @inheritdoc IBaseStorageComponentV2
*/
function getSchema()
public
pure
override
returns (string[] memory keys, TypesLibrary.SchemaValue[] memory values)
{
keys = new string[](1);
values = new TypesLibrary.SchemaValue[](1);
// Whether the entity is archived or not
keys[0] = "value";
values[0] = TypesLibrary.SchemaValue.BOOL;
}
/**
* Sets the typed value for this component
*
* @param entity Entity to get value for
* @param value Layout to set for the given entity
*/
function setLayoutValue(
uint256 entity,
Layout calldata value
) external virtual onlyRole(GAME_LOGIC_CONTRACT_ROLE) {
_setValue(entity, value);
}
/**
* Sets the native value for this component
*
* @param entity Entity to get value for
* @param value Whether the entity is archived or not
*/
function setValue(
uint256 entity,
bool value
) external virtual onlyRole(GAME_LOGIC_CONTRACT_ROLE) {
_setValue(entity, Layout(value));
}
/**
* Batch sets the typed value for this component
*
* @param entities Entity to batch set values for
* @param values Layout to set for the given entities
*/
function batchSetValue(
uint256[] calldata entities,
Layout[] calldata values
) external virtual onlyRole(GAME_LOGIC_CONTRACT_ROLE) {
if (entities.length != values.length) {
revert InvalidBatchData(entities.length, values.length);
}
// Set the values in storage
bytes[] memory encodedValues = new bytes[](entities.length);
for (uint256 i = 0; i < entities.length; i++) {
_setValueToStorage(entities[i], values[i]);
encodedValues[i] = _getEncodedValues(values[i]);
}
// ABI Encode all native types of the struct
_emitBatchSetBytes(entities, encodedValues);
}
/**
* Returns the typed value for this component
*
* @param entity Entity to get value for
* @return value Layout value for the given entity
*/
function getLayoutValue(
uint256 entity
) external view virtual returns (Layout memory value) {
// Get the struct from storage
value = ArchivedComponentStorage.layout().entityIdToStruct[entity];
}
/**
* Returns the native values for this component
*
* @param entity Entity to get value for
* @return value Whether the entity is archived or not
*/
function getValue(
uint256 entity
)
external
view
virtual
returns (
bool value
)
{
if (has(entity)) {
Layout memory s = ArchivedComponentStorage.layout().entityIdToStruct[entity];
(value) = abi.decode(
_getEncodedValues(s),
(bool)
);
}
}
/**
* Returns an array of byte values for each field of this component.
*
* @param entity Entity to build array of byte values for.
*/
function getByteValues(
uint256 entity
) external view virtual returns (bytes[] memory values) {
// Get the struct from storage
Layout storage s = ArchivedComponentStorage
.layout()
.entityIdToStruct[entity];
// ABI Encode all fields of the struct and add to values array
values = new bytes[](1);
values[0] = abi.encode(s.value);
}
/**
* Returns the bytes value for this component
*
* @param entity Entity to get value for
*/
function getBytes(
uint256 entity
) external view returns (bytes memory value) {
Layout memory s = ArchivedComponentStorage.layout().entityIdToStruct[entity];
value = _getEncodedValues(s);
}
/**
* Sets the value of this component using a byte array
*
* @param entity Entity to set value for
*/
function setBytes(
uint256 entity,
bytes calldata value
) external onlyRole(GAME_LOGIC_CONTRACT_ROLE) {
Layout memory s = ArchivedComponentStorage.layout().entityIdToStruct[entity];
(s.value) = abi.decode(value, (bool));
_setValueToStorage(entity, s);
// ABI Encode all native types of the struct
_emitSetBytes(
entity,
value
);
}
/**
* Sets bytes data in batch format
*
* @param entities Entities to set value for
* @param values Bytes values to set for the given entities
*/
function batchSetBytes(
uint256[] calldata entities,
bytes[] calldata values
) external onlyRole(GAME_LOGIC_CONTRACT_ROLE) {
if (entities.length != values.length) {
revert InvalidBatchData(entities.length, values.length);
}
for (uint256 i = 0; i < entities.length; i++) {
Layout memory s = ArchivedComponentStorage.layout().entityIdToStruct[entities[i]];
(s.value) = abi.decode(values[i], (bool));
_setValueToStorage(entities[i], s);
}
// ABI Encode all native types of the struct
_emitBatchSetBytes(
entities,
values
);
}
/**
* Remove the given entity from this component.
*
* @param entity Entity to remove from this component.
*/
function remove(uint256 entity) public virtual onlyRole(GAME_LOGIC_CONTRACT_ROLE) {
// Remove the entity from the component
delete ArchivedComponentStorage.layout().entityIdToStruct[entity];
_emitRemoveBytes(entity);
}
/**
* Batch remove the given entities from this component.
*
* @param entities Entities to remove from this component.
*/
function batchRemove(uint256[] calldata entities) public virtual onlyRole(GAME_LOGIC_CONTRACT_ROLE) {
// Remove the entities from the component
for (uint256 i = 0; i < entities.length; i++) {
delete ArchivedComponentStorage.layout().entityIdToStruct[
entities[i]
];
}
_emitBatchRemoveBytes(entities);
}
/**
* Check whether the given entity has a value in this component.
*
* @param entity Entity to check whether it has a value in this component for.
*/
function has(uint256 entity) public view virtual returns (bool) {
return gameRegistry.getEntityHasComponent(entity, ID);
}
/** INTERNAL **/
function _setValueToStorage(
uint256 entity,
Layout memory value
) internal {
Layout storage s = ArchivedComponentStorage
.layout()
.entityIdToStruct[entity];
s.value = value.value;
}
function _setValue(
uint256 entity,
Layout memory value
) internal {
_setValueToStorage(entity, value);
// ABI Encode all native types of the struct
_emitSetBytes(
entity,
abi.encode(value.value)
);
}
function _getEncodedValues(
Layout memory value
) internal pure returns (bytes memory) {
return
abi.encode(
value.value
);
}
}
// SPDX-License-Identifier: MIT
// Auto-generated using Mage CLI codegen (v1) - DO NOT EDIT
pragma solidity ^0.8.13;
import {TypesLibrary} from "../../core/TypesLibrary.sol";
import {BaseStorageComponentV2, IBaseStorageComponentV2} from "../../core/components/BaseStorageComponentV2.sol";
import {GAME_LOGIC_CONTRACT_ROLE} from "../../Constants.sol";
uint256 constant ID = uint256(keccak256("core.mage.guidcountercomponent.v1"));
struct Layout {
uint256 value;
}
library GuidCounterComponentStorage {
bytes32 internal constant STORAGE_SLOT = bytes32(ID);
// Declare struct for mapping entity to struct
struct InternalLayout {
mapping(uint256 => Layout) entityIdToStruct;
}
function layout() internal pure returns (InternalLayout storage dataStruct) {
bytes32 position = STORAGE_SLOT;
// solhint-disable-next-line no-inline-assembly
assembly {
dataStruct.slot := position
}
}
}
/**
* @title GuidCounterComponent
* @dev An uneditable counter specifically for universal guids
*/
contract GuidCounterComponent is BaseStorageComponentV2 {
/** SETUP **/
/** Sets the GameRegistry contract address for this contract */
constructor(
address gameRegistryAddress
) BaseStorageComponentV2(gameRegistryAddress, ID) {
// Do nothing
}
/**
* @inheritdoc IBaseStorageComponentV2
*/
function getSchema()
public
pure
override
returns (string[] memory keys, TypesLibrary.SchemaValue[] memory values)
{
keys = new string[](1);
values = new TypesLibrary.SchemaValue[](1);
// Counter tracking total counts
keys[0] = "value";
values[0] = TypesLibrary.SchemaValue.UINT256;
}
/**
* Sets the typed value for this component
*
* @param entity Entity to get value for
* @param value Layout to set for the given entity
*/
function setLayoutValue(
uint256 entity,
Layout calldata value
) external virtual onlyRole(GAME_LOGIC_CONTRACT_ROLE) {
_setValue(entity, value);
}
/**
* Sets the native value for this component
*
* @param entity Entity to get value for
* @param value Counter tracking total counts
*/
function setValue(
uint256 entity,
uint256 value
) external virtual onlyRole(GAME_LOGIC_CONTRACT_ROLE) {
_setValue(entity, Layout(value));
}
/**
* Batch sets the typed value for this component
*
* @param entities Entity to batch set values for
* @param values Layout to set for the given entities
*/
function batchSetValue(
uint256[] calldata entities,
Layout[] calldata values
) external virtual onlyRole(GAME_LOGIC_CONTRACT_ROLE) {
if (entities.length != values.length) {
revert InvalidBatchData(entities.length, values.length);
}
// Set the values in storage
bytes[] memory encodedValues = new bytes[](entities.length);
for (uint256 i = 0; i < entities.length; i++) {
_setValueToStorage(entities[i], values[i]);
encodedValues[i] = _getEncodedValues(values[i]);
}
// ABI Encode all native types of the struct
_emitBatchSetBytes(entities, encodedValues);
}
/**
* Returns the typed value for this component
*
* @param entity Entity to get value for
* @return value Layout value for the given entity
*/
function getLayoutValue(
uint256 entity
) external view virtual returns (Layout memory value) {
// Get the struct from storage
value = GuidCounterComponentStorage.layout().entityIdToStruct[entity];
}
/**
* Returns the native values for this component
*
* @param entity Entity to get value for
* @return value Counter tracking total counts
*/
function getValue(
uint256 entity
)
external
view
virtual
returns (
uint256 value
)
{
if (has(entity)) {
Layout memory s = GuidCounterComponentStorage.layout().entityIdToStruct[entity];
(value) = abi.decode(
_getEncodedValues(s),
(uint256)
);
}
}
/**
* Returns an array of byte values for each field of this component.
*
* @param entity Entity to build array of byte values for.
*/
function getByteValues(
uint256 entity
) external view virtual returns (bytes[] memory values) {
// Get the struct from storage
Layout storage s = GuidCounterComponentStorage
.layout()
.entityIdToStruct[entity];
// ABI Encode all fields of the struct and add to values array
values = new bytes[](1);
values[0] = abi.encode(s.value);
}
/**
* Returns the bytes value for this component
*
* @param entity Entity to get value for
*/
function getBytes(
uint256 entity
) external view returns (bytes memory value) {
Layout memory s = GuidCounterComponentStorage.layout().entityIdToStruct[entity];
value = _getEncodedValues(s);
}
/**
* Sets the value of this component using a byte array
*
* @param entity Entity to set value for
*/
function setBytes(
uint256 entity,
bytes calldata value
) external onlyRole(GAME_LOGIC_CONTRACT_ROLE) {
Layout memory s = GuidCounterComponentStorage.layout().entityIdToStruct[entity];
(s.value) = abi.decode(value, (uint256));
_setValueToStorage(entity, s);
// ABI Encode all native types of the struct
_emitSetBytes(
entity,
value
);
}
/**
* Sets bytes data in batch format
*
* @param entities Entities to set value for
* @param values Bytes values to set for the given entities
*/
function batchSetBytes(
uint256[] calldata entities,
bytes[] calldata values
) external onlyRole(GAME_LOGIC_CONTRACT_ROLE) {
if (entities.length != values.length) {
revert InvalidBatchData(entities.length, values.length);
}
for (uint256 i = 0; i < entities.length; i++) {
Layout memory s = GuidCounterComponentStorage.layout().entityIdToStruct[entities[i]];
(s.value) = abi.decode(values[i], (uint256));
_setValueToStorage(entities[i], s);
}
// ABI Encode all native types of the struct
_emitBatchSetBytes(
entities,
values
);
}
/**
* Remove the given entity from this component.
*
* @param entity Entity to remove from this component.
*/
function remove(uint256 entity) public virtual onlyRole(GAME_LOGIC_CONTRACT_ROLE) {
// Remove the entity from the component
delete GuidCounterComponentStorage.layout().entityIdToStruct[entity];
_emitRemoveBytes(entity);
}
/**
* Batch remove the given entities from this component.
*
* @param entities Entities to remove from this component.
*/
function batchRemove(uint256[] calldata entities) public virtual onlyRole(GAME_LOGIC_CONTRACT_ROLE) {
// Remove the entities from the component
for (uint256 i = 0; i < entities.length; i++) {
delete GuidCounterComponentStorage.layout().entityIdToStruct[
entities[i]
];
}
_emitBatchRemoveBytes(entities);
}
/**
* Check whether the given entity has a value in this component.
*
* @param entity Entity to check whether it has a value in this component for.
*/
function has(uint256 entity) public view virtual returns (bool) {
return gameRegistry.getEntityHasComponent(entity, ID);
}
/** INTERNAL **/
function _setValueToStorage(
uint256 entity,
Layout memory value
) internal {
Layout storage s = GuidCounterComponentStorage
.layout()
.entityIdToStruct[entity];
s.value = value.value;
}
function _setValue(
uint256 entity,
Layout memory value
) internal {
_setValueToStorage(entity, value);
// ABI Encode all native types of the struct
_emitSetBytes(
entity,
abi.encode(value.value)
);
}
function _getEncodedValues(
Layout memory value
) internal pure returns (bytes memory) {
return
abi.encode(
value.value
);
}
}
// SPDX-License-Identifier: MIT
// Auto-generated using Mage CLI codegen (v1) - DO NOT EDIT
pragma solidity ^0.8.13;
import {TypesLibrary} from "../../core/TypesLibrary.sol";
import {BaseStorageComponentV2, IBaseStorageComponentV2} from "../../core/components/BaseStorageComponentV2.sol";
import {GAME_LOGIC_CONTRACT_ROLE} from "../../Constants.sol";
uint256 constant ID = uint256(keccak256("core.mage.chainidcomponent.v1"));
struct Layout {
uint256 value;
}
library ChainIdComponentStorage {
bytes32 internal constant STORAGE_SLOT = bytes32(ID);
// Declare struct for mapping entity to struct
struct InternalLayout {
mapping(uint256 => Layout) entityIdToStruct;
}
function layout() internal pure returns (InternalLayout storage dataStruct) {
bytes32 position = STORAGE_SLOT;
// solhint-disable-next-line no-inline-assembly
assembly {
dataStruct.slot := position
}
}
}
/**
* @title ChainIdComponent
* @dev This component stores the chain ID for an entity
*/
contract ChainIdComponent is BaseStorageComponentV2 {
/** SETUP **/
/** Sets the GameRegistry contract address for this contract */
constructor(
address gameRegistryAddress
) BaseStorageComponentV2(gameRegistryAddress, ID) {
// Do nothing
}
/**
* @inheritdoc IBaseStorageComponentV2
*/
function getSchema()
public
pure
override
returns (string[] memory keys, TypesLibrary.SchemaValue[] memory values)
{
keys = new string[](1);
values = new TypesLibrary.SchemaValue[](1);
// The home chain ID for entity
keys[0] = "value";
values[0] = TypesLibrary.SchemaValue.UINT256;
}
/**
* Sets the typed value for this component
*
* @param entity Entity to get value for
* @param value Layout to set for the given entity
*/
function setLayoutValue(
uint256 entity,
Layout calldata value
) external virtual onlyRole(GAME_LOGIC_CONTRACT_ROLE) {
_setValue(entity, value);
}
/**
* Sets the native value for this component
*
* @param entity Entity to get value for
* @param value The home chain ID for entity
*/
function setValue(
uint256 entity,
uint256 value
) external virtual onlyRole(GAME_LOGIC_CONTRACT_ROLE) {
_setValue(entity, Layout(value));
}
/**
* Batch sets the typed value for this component
*
* @param entities Entity to batch set values for
* @param values Layout to set for the given entities
*/
function batchSetValue(
uint256[] calldata entities,
Layout[] calldata values
) external virtual onlyRole(GAME_LOGIC_CONTRACT_ROLE) {
if (entities.length != values.length) {
revert InvalidBatchData(entities.length, values.length);
}
// Set the values in storage
bytes[] memory encodedValues = new bytes[](entities.length);
for (uint256 i = 0; i < entities.length; i++) {
_setValueToStorage(entities[i], values[i]);
encodedValues[i] = _getEncodedValues(values[i]);
}
// ABI Encode all native types of the struct
_emitBatchSetBytes(entities, encodedValues);
}
/**
* Returns the typed value for this component
*
* @param entity Entity to get value for
* @return value Layout value for the given entity
*/
function getLayoutValue(
uint256 entity
) external view virtual returns (Layout memory value) {
// Get the struct from storage
value = ChainIdComponentStorage.layout().entityIdToStruct[entity];
}
/**
* Returns the native values for this component
*
* @param entity Entity to get value for
* @return value The home chain ID for entity
*/
function getValue(
uint256 entity
)
external
view
virtual
returns (
uint256 value
)
{
if (has(entity)) {
Layout memory s = ChainIdComponentStorage.layout().entityIdToStruct[entity];
(value) = abi.decode(
_getEncodedValues(s),
(uint256)
);
}
}
/**
* Returns an array of byte values for each field of this component.
*
* @param entity Entity to build array of byte values for.
*/
function getByteValues(
uint256 entity
) external view virtual returns (bytes[] memory values) {
// Get the struct from storage
Layout storage s = ChainIdComponentStorage
.layout()
.entityIdToStruct[entity];
// ABI Encode all fields of the struct and add to values array
values = new bytes[](1);
values[0] = abi.encode(s.value);
}
/**
* Returns the bytes value for this component
*
* @param entity Entity to get value for
*/
function getBytes(
uint256 entity
) external view returns (bytes memory value) {
Layout memory s = ChainIdComponentStorage.layout().entityIdToStruct[entity];
value = _getEncodedValues(s);
}
/**
* Sets the value of this component using a byte array
*
* @param entity Entity to set value for
*/
function setBytes(
uint256 entity,
bytes calldata value
) external onlyRole(GAME_LOGIC_CONTRACT_ROLE) {
Layout memory s = ChainIdComponentStorage.layout().entityIdToStruct[entity];
(s.value) = abi.decode(value, (uint256));
_setValueToStorage(entity, s);
// ABI Encode all native types of the struct
_emitSetBytes(
entity,
value
);
}
/**
* Sets bytes data in batch format
*
* @param entities Entities to set value for
* @param values Bytes values to set for the given entities
*/
function batchSetBytes(
uint256[] calldata entities,
bytes[] calldata values
) external onlyRole(GAME_LOGIC_CONTRACT_ROLE) {
if (entities.length != values.length) {
revert InvalidBatchData(entities.length, values.length);
}
for (uint256 i = 0; i < entities.length; i++) {
Layout memory s = ChainIdComponentStorage.layout().entityIdToStruct[entities[i]];
(s.value) = abi.decode(values[i], (uint256));
_setValueToStorage(entities[i], s);
}
// ABI Encode all native types of the struct
_emitBatchSetBytes(
entities,
values
);
}
/**
* Remove the given entity from this component.
*
* @param entity Entity to remove from this component.
*/
function remove(uint256 entity) public virtual onlyRole(GAME_LOGIC_CONTRACT_ROLE) {
// Remove the entity from the component
delete ChainIdComponentStorage.layout().entityIdToStruct[entity];
_emitRemoveBytes(entity);
}
/**
* Batch remove the given entities from this component.
*
* @param entities Entities to remove from this component.
*/
function batchRemove(uint256[] calldata entities) public virtual onlyRole(GAME_LOGIC_CONTRACT_ROLE) {
// Remove the entities from the component
for (uint256 i = 0; i < entities.length; i++) {
delete ChainIdComponentStorage.layout().entityIdToStruct[
entities[i]
];
}
_emitBatchRemoveBytes(entities);
}
/**
* Check whether the given entity has a value in this component.
*
* @param entity Entity to check whether it has a value in this component for.
*/
function has(uint256 entity) public view virtual returns (bool) {
return gameRegistry.getEntityHasComponent(entity, ID);
}
/** INTERNAL **/
function _setValueToStorage(
uint256 entity,
Layout memory value
) internal {
Layout storage s = ChainIdComponentStorage
.layout()
.entityIdToStruct[entity];
s.value = value.value;
}
function _setValue(
uint256 entity,
Layout memory value
) internal {
_setValueToStorage(entity, value);
// ABI Encode all native types of the struct
_emitSetBytes(
entity,
abi.encode(value.value)
);
}
function _getEncodedValues(
Layout memory value
) internal pure returns (bytes memory) {
return
abi.encode(
value.value
);
}
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.7.0) (access/Ownable.sol)
pragma solidity ^0.8.0;
import "../utils/ContextUpgradeable.sol";
import "../proxy/utils/Initializable.sol";
/**
* @dev Contract module which provides a basic access control mechanism, where
* there is an account (an owner) that can be granted exclusive access to
* specific functions.
*
* By default, the owner account will be the one that deploys the contract. This
* can later be changed with {transferOwnership}.
*
* This module is used through inheritance. It will make available the modifier
* `onlyOwner`, which can be applied to your functions to restrict their use to
* the owner.
*/
abstract contract OwnableUpgradeable is Initializable, ContextUpgradeable {
address private _owner;
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
/**
* @dev Initializes the contract setting the deployer as the initial owner.
*/
function __Ownable_init() internal onlyInitializing {
__Ownable_init_unchained();
}
function __Ownable_init_unchained() internal onlyInitializing {
_transferOwnership(_msgSender());
}
/**
* @dev Throws if called by any account other than the owner.
*/
modifier onlyOwner() {
_checkOwner();
_;
}
/**
* @dev Returns the address of the current owner.
*/
function owner() public view virtual returns (address) {
return _owner;
}
/**
* @dev Throws if the sender is not the owner.
*/
function _checkOwner() internal view virtual {
require(owner() == _msgSender(), "Ownable: caller is not the owner");
}
/**
* @dev Leaves the contract without owner. It will not be possible to call
* `onlyOwner` functions anymore. Can only be called by the current owner.
*
* NOTE: Renouncing ownership will leave the contract without an owner,
* thereby removing any functionality that is only available to the owner.
*/
function renounceOwnership() public virtual onlyOwner {
_transferOwnership(address(0));
}
/**
* @dev Transfers ownership of the contract to a new account (`newOwner`).
* Can only be called by the current owner.
*/
function transferOwnership(address newOwner) public virtual onlyOwner {
require(newOwner != address(0), "Ownable: new owner is the zero address");
_transferOwnership(newOwner);
}
/**
* @dev Transfers ownership of the contract to a new account (`newOwner`).
* Internal function without access restriction.
*/
function _transferOwnership(address newOwner) internal virtual {
address oldOwner = _owner;
_owner = newOwner;
emit OwnershipTransferred(oldOwner, newOwner);
}
/**
* @dev This empty reserved space is put in place to allow future versions to add new
* variables without shifting down storage in the inheritance chain.
* See https://docs.openzeppelin.com/contracts/4.x/upgradeable#storage_gaps
*/
uint256[49] private __gap;
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.8.0) (access/AccessControl.sol)
pragma solidity ^0.8.0;
import "./IAccessControlUpgradeable.sol";
import "../utils/ContextUpgradeable.sol";
import "../utils/StringsUpgradeable.sol";
import "../utils/introspection/ERC165Upgradeable.sol";
import "../proxy/utils/Initializable.sol";
/**
* @dev Contract module that allows children to implement role-based access
* control mechanisms. This is a lightweight version that doesn't allow enumerating role
* members except through off-chain means by accessing the contract event logs. Some
* applications may benefit from on-chain enumerability, for those cases see
* {AccessControlEnumerable}.
*
* Roles are referred to by their `bytes32` identifier. These should be exposed
* in the external API and be unique. The best way to achieve this is by
* using `public constant` hash digests:
*
* ```
* bytes32 public constant MY_ROLE = keccak256("MY_ROLE");
* ```
*
* Roles can be used to represent a set of permissions. To restrict access to a
* function call, use {hasRole}:
*
* ```
* function foo() public {
* require(hasRole(MY_ROLE, msg.sender));
* ...
* }
* ```
*
* Roles can be granted and revoked dynamically via the {grantRole} and
* {revokeRole} functions. Each role has an associated admin role, and only
* accounts that have a role's admin role can call {grantRole} and {revokeRole}.
*
* By default, the admin role for all roles is `DEFAULT_ADMIN_ROLE`, which means
* that only accounts with this role will be able to grant or revoke other
* roles. More complex role relationships can be created by using
* {_setRoleAdmin}.
*
* WARNING: The `DEFAULT_ADMIN_ROLE` is also its own admin: it has permission to
* grant and revoke this role. Extra precautions should be taken to secure
* accounts that have been granted it.
*/
abstract contract AccessControlUpgradeable is Initializable, ContextUpgradeable, IAccessControlUpgradeable, ERC165Upgradeable {
function __AccessControl_init() internal onlyInitializing {
}
function __AccessControl_init_unchained() internal onlyInitializing {
}
struct RoleData {
mapping(address => bool) members;
bytes32 adminRole;
}
mapping(bytes32 => RoleData) private _roles;
bytes32 public constant DEFAULT_ADMIN_ROLE = 0x00;
/**
* @dev Modifier that checks that an account has a specific role. Reverts
* with a standardized message including the required 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})$/
*
* _Available since v4.1._
*/
modifier onlyRole(bytes32 role) {
_checkRole(role);
_;
}
/**
* @dev See {IERC165-supportsInterface}.
*/
function supportsInterface(bytes4 interfaceId) public view virtual override returns (bool) {
return interfaceId == type(IAccessControlUpgradeable).interfaceId || super.supportsInterface(interfaceId);
}
/**
* @dev Returns `true` if `account` has been granted `role`.
*/
function hasRole(bytes32 role, address account) public view virtual override returns (bool) {
return _roles[role].members[account];
}
/**
* @dev Revert with a standard message if `_msgSender()` is missing `role`.
* Overriding this function changes the behavior of the {onlyRole} modifier.
*
* Format of the revert message is described in {_checkRole}.
*
* _Available since v4.6._
*/
function _checkRole(bytes32 role) internal view virtual {
_checkRole(role, _msgSender());
}
/**
* @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 (!hasRole(role, account)) {
revert(
string(
abi.encodePacked(
"AccessControl: account ",
StringsUpgradeable.toHexString(account),
" is missing role ",
StringsUpgradeable.toHexString(uint256(role), 32)
)
)
);
}
}
/**
* @dev Returns the admin role that controls `role`. See {grantRole} and
* {revokeRole}.
*
* To change a role's admin, use {_setRoleAdmin}.
*/
function getRoleAdmin(bytes32 role) public view virtual override returns (bytes32) {
return _roles[role].adminRole;
}
/**
* @dev Grants `role` to `account`.
*
* If `account` had not been already granted `role`, emits a {RoleGranted}
* event.
*
* Requirements:
*
* - the caller must have ``role``'s admin role.
*
* May emit a {RoleGranted} event.
*/
function grantRole(bytes32 role, address account) public virtual override onlyRole(getRoleAdmin(role)) {
_grantRole(role, account);
}
/**
* @dev Revokes `role` from `account`.
*
* If `account` had been granted `role`, emits a {RoleRevoked} event.
*
* Requirements:
*
* - the caller must have ``role``'s admin role.
*
* May emit a {RoleRevoked} event.
*/
function revokeRole(bytes32 role, address account) public virtual override onlyRole(getRoleAdmin(role)) {
_revokeRole(role, account);
}
/**
* @dev Revokes `role` from the calling account.
*
* Roles are often managed via {grantRole} and {revokeRole}: this function's
* purpose is to provide a mechanism for accounts to lose their privileges
* if they are compromised (such as when a trusted device is misplaced).
*
* If the calling account had been revoked `role`, emits a {RoleRevoked}
* event.
*
* Requirements:
*
* - the caller must be `account`.
*
* May emit a {RoleRevoked} event.
*/
function renounceRole(bytes32 role, address account) public virtual override {
require(account == _msgSender(), "AccessControl: can only renounce roles for self");
_revokeRole(role, account);
}
/**
* @dev Grants `role` to `account`.
*
* If `account` had not been already granted `role`, emits a {RoleGranted}
* event. Note that unlike {grantRole}, this function doesn't perform any
* checks on the calling account.
*
* May emit a {RoleGranted} event.
*
* [WARNING]
* ====
* This function should only be called from the constructor when setting
* up the initial roles for the system.
*
* Using this function in any other way is effectively circumventing the admin
* system imposed by {AccessControl}.
* ====
*
* NOTE: This function is deprecated in favor of {_grantRole}.
*/
function _setupRole(bytes32 role, address account) internal virtual {
_grantRole(role, account);
}
/**
* @dev Sets `adminRole` as ``role``'s admin role.
*
* Emits a {RoleAdminChanged} event.
*/
function _setRoleAdmin(bytes32 role, bytes32 adminRole) internal virtual {
bytes32 previousAdminRole = getRoleAdmin(role);
_roles[role].adminRole = adminRole;
emit RoleAdminChanged(role, previousAdminRole, adminRole);
}
/**
* @dev Grants `role` to `account`.
*
* Internal function without access restriction.
*
* May emit a {RoleGranted} event.
*/
function _grantRole(bytes32 role, address account) internal virtual {
if (!hasRole(role, account)) {
_roles[role].members[account] = true;
emit RoleGranted(role, account, _msgSender());
}
}
/**
* @dev Revokes `role` from `account`.
*
* Internal function without access restriction.
*
* May emit a {RoleRevoked} event.
*/
function _revokeRole(bytes32 role, address account) internal virtual {
if (hasRole(role, account)) {
_roles[role].members[account] = false;
emit RoleRevoked(role, account, _msgSender());
}
}
/**
* @dev This empty reserved space is put in place to allow future versions to add new
* variables without shifting down storage in the inheritance chain.
* See https://docs.openzeppelin.com/contracts/4.x/upgradeable#storage_gaps
*/
uint256[49] private __gap;
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.7.0) (security/Pausable.sol)
pragma solidity ^0.8.0;
import "../utils/ContextUpgradeable.sol";
import "../proxy/utils/Initializable.sol";
/**
* @dev Contract module which allows children to implement an emergency stop
* mechanism that can be triggered by an authorized account.
*
* This module is used through inheritance. It will make available the
* modifiers `whenNotPaused` and `whenPaused`, which can be applied to
* the functions of your contract. Note that they will not be pausable by
* simply including this module, only once the modifiers are put in place.
*/
abstract contract PausableUpgradeable is Initializable, ContextUpgradeable {
/**
* @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);
bool private _paused;
/**
* @dev Initializes the contract in unpaused state.
*/
function __Pausable_init() internal onlyInitializing {
__Pausable_init_unchained();
}
function __Pausable_init_unchained() internal onlyInitializing {
_paused = false;
}
/**
* @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();
_;
}
/**
* @dev Returns true if the contract is paused, and false otherwise.
*/
function paused() public view virtual returns (bool) {
return _paused;
}
/**
* @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 whenNotPaused {
_paused = true;
emit Paused(_msgSender());
}
/**
* @dev Returns to normal state.
*
* Requirements:
*
* - The contract must be paused.
*/
function _unpause() internal virtual whenPaused {
_paused = false;
emit Unpaused(_msgSender());
}
/**
* @dev This empty reserved space is put in place to allow future versions to add new
* variables without shifting down storage in the inheritance chain.
* See https://docs.openzeppelin.com/contracts/4.x/upgradeable#storage_gaps
*/
uint256[49] private __gap;
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.8.0) (utils/Strings.sol)
pragma solidity ^0.8.0;
import "./math/Math.sol";
/**
* @dev String operations.
*/
library Strings {
bytes16 private constant _SYMBOLS = "0123456789abcdef";
uint8 private constant _ADDRESS_LENGTH = 20;
/**
* @dev Converts a `uint256` to its ASCII `string` decimal representation.
*/
function toString(uint256 value) internal pure returns (string memory) {
unchecked {
uint256 length = Math.log10(value) + 1;
string memory buffer = new string(length);
uint256 ptr;
/// @solidity memory-safe-assembly
assembly {
ptr := add(buffer, add(32, length))
}
while (true) {
ptr--;
/// @solidity memory-safe-assembly
assembly {
mstore8(ptr, byte(mod(value, 10), _SYMBOLS))
}
value /= 10;
if (value == 0) break;
}
return buffer;
}
}
/**
* @dev Converts a `uint256` to its ASCII `string` hexadecimal representation.
*/
function toHexString(uint256 value) internal pure returns (string memory) {
unchecked {
return toHexString(value, Math.log256(value) + 1);
}
}
/**
* @dev Converts a `uint256` to its ASCII `string` hexadecimal representation with fixed length.
*/
function toHexString(uint256 value, uint256 length) internal pure returns (string memory) {
bytes memory buffer = new bytes(2 * length + 2);
buffer[0] = "0";
buffer[1] = "x";
for (uint256 i = 2 * length + 1; i > 1; --i) {
buffer[i] = _SYMBOLS[value & 0xf];
value >>= 4;
}
require(value == 0, "Strings: hex length insufficient");
return string(buffer);
}
/**
* @dev Converts an `address` with fixed length of 20 bytes to its not checksummed ASCII `string` hexadecimal representation.
*/
function toHexString(address addr) internal pure returns (string memory) {
return toHexString(uint256(uint160(addr)), _ADDRESS_LENGTH);
}
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.8.0) (token/ERC20/ERC20.sol)
pragma solidity ^0.8.0;
import "./IERC20.sol";
import "./extensions/IERC20Metadata.sol";
import "../../utils/Context.sol";
/**
* @dev Implementation of the {IERC20} interface.
*
* This implementation is agnostic to the way tokens are created. This means
* that a supply mechanism has to be added in a derived contract using {_mint}.
* For a generic mechanism see {ERC20PresetMinterPauser}.
*
* TIP: For a detailed writeup see our guide
* https://forum.openzeppelin.com/t/how-to-implement-erc20-supply-mechanisms/226[How
* to implement supply mechanisms].
*
* We have followed general OpenZeppelin Contracts guidelines: functions revert
* instead returning `false` on failure. This behavior is nonetheless
* conventional and does not conflict with the expectations of ERC20
* applications.
*
* Additionally, an {Approval} event is emitted on calls to {transferFrom}.
* This allows applications to reconstruct the allowance for all accounts just
* by listening to said events. Other implementations of the EIP may not emit
* these events, as it isn't required by the specification.
*
* Finally, the non-standard {decreaseAllowance} and {increaseAllowance}
* functions have been added to mitigate the well-known issues around setting
* allowances. See {IERC20-approve}.
*/
contract ERC20 is Context, IERC20, IERC20Metadata {
mapping(address => uint256) private _balances;
mapping(address => mapping(address => uint256)) private _allowances;
uint256 private _totalSupply;
string private _name;
string private _symbol;
/**
* @dev Sets the values for {name} and {symbol}.
*
* The default value of {decimals} is 18. To select a different value for
* {decimals} you should overload it.
*
* All two of these values are immutable: they can only be set once during
* construction.
*/
constructor(string memory name_, string memory symbol_) {
_name = name_;
_symbol = symbol_;
}
/**
* @dev Returns the name of the token.
*/
function name() public view virtual override returns (string memory) {
return _name;
}
/**
* @dev Returns the symbol of the token, usually a shorter version of the
* name.
*/
function symbol() public view virtual override returns (string memory) {
return _symbol;
}
/**
* @dev Returns the number of decimals used to get its user representation.
* For example, if `decimals` equals `2`, a balance of `505` tokens should
* be displayed to a user as `5.05` (`505 / 10 ** 2`).
*
* Tokens usually opt for a value of 18, imitating the relationship between
* Ether and Wei. This is the value {ERC20} uses, unless this function is
* overridden;
*
* NOTE: This information is only used for _display_ purposes: it in
* no way affects any of the arithmetic of the contract, including
* {IERC20-balanceOf} and {IERC20-transfer}.
*/
function decimals() public view virtual override returns (uint8) {
return 18;
}
/**
* @dev See {IERC20-totalSupply}.
*/
function totalSupply() public view virtual override returns (uint256) {
return _totalSupply;
}
/**
* @dev See {IERC20-balanceOf}.
*/
function balanceOf(address account) public view virtual override returns (uint256) {
return _balances[account];
}
/**
* @dev See {IERC20-transfer}.
*
* Requirements:
*
* - `to` cannot be the zero address.
* - the caller must have a balance of at least `amount`.
*/
function transfer(address to, uint256 amount) public virtual override returns (bool) {
address owner = _msgSender();
_transfer(owner, to, amount);
return true;
}
/**
* @dev See {IERC20-allowance}.
*/
function allowance(address owner, address spender) public view virtual override returns (uint256) {
return _allowances[owner][spender];
}
/**
* @dev See {IERC20-approve}.
*
* NOTE: If `amount` is the maximum `uint256`, the allowance is not updated on
* `transferFrom`. This is semantically equivalent to an infinite approval.
*
* Requirements:
*
* - `spender` cannot be the zero address.
*/
function approve(address spender, uint256 amount) public virtual override returns (bool) {
address owner = _msgSender();
_approve(owner, spender, amount);
return true;
}
/**
* @dev See {IERC20-transferFrom}.
*
* Emits an {Approval} event indicating the updated allowance. This is not
* required by the EIP. See the note at the beginning of {ERC20}.
*
* NOTE: Does not update the allowance if the current allowance
* is the maximum `uint256`.
*
* Requirements:
*
* - `from` and `to` cannot be the zero address.
* - `from` must have a balance of at least `amount`.
* - the caller must have allowance for ``from``'s tokens of at least
* `amount`.
*/
function transferFrom(
address from,
address to,
uint256 amount
) public virtual override returns (bool) {
address spender = _msgSender();
_spendAllowance(from, spender, amount);
_transfer(from, to, amount);
return true;
}
/**
* @dev Atomically increases the allowance granted to `spender` by the caller.
*
* This is an alternative to {approve} that can be used as a mitigation for
* problems described in {IERC20-approve}.
*
* Emits an {Approval} event indicating the updated allowance.
*
* Requirements:
*
* - `spender` cannot be the zero address.
*/
function increaseAllowance(address spender, uint256 addedValue) public virtual returns (bool) {
address owner = _msgSender();
_approve(owner, spender, allowance(owner, spender) + addedValue);
return true;
}
/**
* @dev Atomically decreases the allowance granted to `spender` by the caller.
*
* This is an alternative to {approve} that can be used as a mitigation for
* problems described in {IERC20-approve}.
*
* Emits an {Approval} event indicating the updated allowance.
*
* Requirements:
*
* - `spender` cannot be the zero address.
* - `spender` must have allowance for the caller of at least
* `subtractedValue`.
*/
function decreaseAllowance(address spender, uint256 subtractedValue) public virtual returns (bool) {
address owner = _msgSender();
uint256 currentAllowance = allowance(owner, spender);
require(currentAllowance >= subtractedValue, "ERC20: decreased allowance below zero");
unchecked {
_approve(owner, spender, currentAllowance - subtractedValue);
}
return true;
}
/**
* @dev Moves `amount` of tokens from `from` to `to`.
*
* This internal function is equivalent to {transfer}, and can be used to
* e.g. implement automatic token fees, slashing mechanisms, etc.
*
* Emits a {Transfer} event.
*
* Requirements:
*
* - `from` cannot be the zero address.
* - `to` cannot be the zero address.
* - `from` must have a balance of at least `amount`.
*/
function _transfer(
address from,
address to,
uint256 amount
) internal virtual {
require(from != address(0), "ERC20: transfer from the zero address");
require(to != address(0), "ERC20: transfer to the zero address");
_beforeTokenTransfer(from, to, amount);
uint256 fromBalance = _balances[from];
require(fromBalance >= amount, "ERC20: transfer amount exceeds balance");
unchecked {
_balances[from] = fromBalance - amount;
// Overflow not possible: the sum of all balances is capped by totalSupply, and the sum is preserved by
// decrementing then incrementing.
_balances[to] += amount;
}
emit Transfer(from, to, amount);
_afterTokenTransfer(from, to, amount);
}
/** @dev Creates `amount` tokens and assigns them to `account`, increasing
* the total supply.
*
* Emits a {Transfer} event with `from` set to the zero address.
*
* Requirements:
*
* - `account` cannot be the zero address.
*/
function _mint(address account, uint256 amount) internal virtual {
require(account != address(0), "ERC20: mint to the zero address");
_beforeTokenTransfer(address(0), account, amount);
_totalSupply += amount;
unchecked {
// Overflow not possible: balance + amount is at most totalSupply + amount, which is checked above.
_balances[account] += amount;
}
emit Transfer(address(0), account, amount);
_afterTokenTransfer(address(0), account, amount);
}
/**
* @dev Destroys `amount` tokens from `account`, reducing the
* total supply.
*
* Emits a {Transfer} event with `to` set to the zero address.
*
* Requirements:
*
* - `account` cannot be the zero address.
* - `account` must have at least `amount` tokens.
*/
function _burn(address account, uint256 amount) internal virtual {
require(account != address(0), "ERC20: burn from the zero address");
_beforeTokenTransfer(account, address(0), amount);
uint256 accountBalance = _balances[account];
require(accountBalance >= amount, "ERC20: burn amount exceeds balance");
unchecked {
_balances[account] = accountBalance - amount;
// Overflow not possible: amount <= accountBalance <= totalSupply.
_totalSupply -= amount;
}
emit Transfer(account, address(0), amount);
_afterTokenTransfer(account, address(0), amount);
}
/**
* @dev Sets `amount` as the allowance of `spender` over the `owner` s tokens.
*
* This internal function is equivalent to `approve`, and can be used to
* e.g. set automatic allowances for certain subsystems, etc.
*
* Emits an {Approval} event.
*
* Requirements:
*
* - `owner` cannot be the zero address.
* - `spender` cannot be the zero address.
*/
function _approve(
address owner,
address spender,
uint256 amount
) internal virtual {
require(owner != address(0), "ERC20: approve from the zero address");
require(spender != address(0), "ERC20: approve to the zero address");
_allowances[owner][spender] = amount;
emit Approval(owner, spender, amount);
}
/**
* @dev Updates `owner` s allowance for `spender` based on spent `amount`.
*
* Does not update the allowance amount in case of infinite allowance.
* Revert if not enough allowance is available.
*
* Might emit an {Approval} event.
*/
function _spendAllowance(
address owner,
address spender,
uint256 amount
) internal virtual {
uint256 currentAllowance = allowance(owner, spender);
if (currentAllowance != type(uint256).max) {
require(currentAllowance >= amount, "ERC20: insufficient allowance");
unchecked {
_approve(owner, spender, currentAllowance - amount);
}
}
}
/**
* @dev Hook that is called before any transfer of tokens. This includes
* minting and burning.
*
* Calling conditions:
*
* - when `from` and `to` are both non-zero, `amount` of ``from``'s tokens
* will be transferred to `to`.
* - when `from` is zero, `amount` tokens will be minted for `to`.
* - when `to` is zero, `amount` of ``from``'s tokens will be burned.
* - `from` and `to` are never both zero.
*
* To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks].
*/
function _beforeTokenTransfer(
address from,
address to,
uint256 amount
) internal virtual {}
/**
* @dev Hook that is called after any transfer of tokens. This includes
* minting and burning.
*
* Calling conditions:
*
* - when `from` and `to` are both non-zero, `amount` of ``from``'s tokens
* has been transferred to `to`.
* - when `from` is zero, `amount` tokens have been minted for `to`.
* - when `to` is zero, `amount` of ``from``'s tokens have been burned.
* - `from` and `to` are never both zero.
*
* To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks].
*/
function _afterTokenTransfer(
address from,
address to,
uint256 amount
) internal virtual {}
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.8.0) (utils/cryptography/ECDSA.sol)
pragma solidity ^0.8.0;
import "../Strings.sol";
/**
* @dev Elliptic Curve Digital Signature Algorithm (ECDSA) operations.
*
* These functions can be used to verify that a message was signed by the holder
* of the private keys of a given address.
*/
library ECDSA {
enum RecoverError {
NoError,
InvalidSignature,
InvalidSignatureLength,
InvalidSignatureS,
InvalidSignatureV // Deprecated in v4.8
}
function _throwError(RecoverError error) private pure {
if (error == RecoverError.NoError) {
return; // no error: do nothing
} else if (error == RecoverError.InvalidSignature) {
revert("ECDSA: invalid signature");
} else if (error == RecoverError.InvalidSignatureLength) {
revert("ECDSA: invalid signature length");
} else if (error == RecoverError.InvalidSignatureS) {
revert("ECDSA: invalid signature 's' value");
}
}
/**
* @dev Returns the address that signed a hashed message (`hash`) with
* `signature` or error string. This address can then be used for verification purposes.
*
* The `ecrecover` EVM opcode allows for malleable (non-unique) signatures:
* this function rejects them by requiring the `s` value to be in the lower
* half order, and the `v` value to be either 27 or 28.
*
* IMPORTANT: `hash` _must_ be the result of a hash operation for the
* verification to be secure: it is possible to craft signatures that
* recover to arbitrary addresses for non-hashed data. A safe way to ensure
* this is by receiving a hash of the original message (which may otherwise
* be too long), and then calling {toEthSignedMessageHash} on it.
*
* Documentation for signature generation:
* - with https://web3js.readthedocs.io/en/v1.3.4/web3-eth-accounts.html#sign[Web3.js]
* - with https://docs.ethers.io/v5/api/signer/#Signer-signMessage[ethers]
*
* _Available since v4.3._
*/
function tryRecover(bytes32 hash, bytes memory signature) internal pure returns (address, RecoverError) {
if (signature.length == 65) {
bytes32 r;
bytes32 s;
uint8 v;
// ecrecover takes the signature parameters, and the only way to get them
// currently is to use assembly.
/// @solidity memory-safe-assembly
assembly {
r := mload(add(signature, 0x20))
s := mload(add(signature, 0x40))
v := byte(0, mload(add(signature, 0x60)))
}
return tryRecover(hash, v, r, s);
} else {
return (address(0), RecoverError.InvalidSignatureLength);
}
}
/**
* @dev Returns the address that signed a hashed message (`hash`) with
* `signature`. This address can then be used for verification purposes.
*
* The `ecrecover` EVM opcode allows for malleable (non-unique) signatures:
* this function rejects them by requiring the `s` value to be in the lower
* half order, and the `v` value to be either 27 or 28.
*
* IMPORTANT: `hash` _must_ be the result of a hash operation for the
* verification to be secure: it is possible to craft signatures that
* recover to arbitrary addresses for non-hashed data. A safe way to ensure
* this is by receiving a hash of the original message (which may otherwise
* be too long), and then calling {toEthSignedMessageHash} on it.
*/
function recover(bytes32 hash, bytes memory signature) internal pure returns (address) {
(address recovered, RecoverError error) = tryRecover(hash, signature);
_throwError(error);
return recovered;
}
/**
* @dev Overload of {ECDSA-tryRecover} that receives the `r` and `vs` short-signature fields separately.
*
* See https://eips.ethereum.org/EIPS/eip-2098[EIP-2098 short signatures]
*
* _Available since v4.3._
*/
function tryRecover(
bytes32 hash,
bytes32 r,
bytes32 vs
) internal pure returns (address, RecoverError) {
bytes32 s = vs & bytes32(0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff);
uint8 v = uint8((uint256(vs) >> 255) + 27);
return tryRecover(hash, v, r, s);
}
/**
* @dev Overload of {ECDSA-recover} that receives the `r and `vs` short-signature fields separately.
*
* _Available since v4.2._
*/
function recover(
bytes32 hash,
bytes32 r,
bytes32 vs
) internal pure returns (address) {
(address recovered, RecoverError error) = tryRecover(hash, r, vs);
_throwError(error);
return recovered;
}
/**
* @dev Overload of {ECDSA-tryRecover} that receives the `v`,
* `r` and `s` signature fields separately.
*
* _Available since v4.3._
*/
function tryRecover(
bytes32 hash,
uint8 v,
bytes32 r,
bytes32 s
) internal pure returns (address, RecoverError) {
// EIP-2 still allows signature malleability for ecrecover(). Remove this possibility and make the signature
// unique. Appendix F in the Ethereum Yellow paper (https://ethereum.github.io/yellowpaper/paper.pdf), defines
// the valid range for s in (301): 0 < s < secp256k1n ÷ 2 + 1, and for v in (302): v ∈ {27, 28}. Most
// signatures from current libraries generate a unique signature with an s-value in the lower half order.
//
// If your library generates malleable signatures, such as s-values in the upper range, calculate a new s-value
// with 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141 - s1 and flip v from 27 to 28 or
// vice versa. If your library also generates signatures with 0/1 for v instead 27/28, add 27 to v to accept
// these malleable signatures as well.
if (uint256(s) > 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0) {
return (address(0), RecoverError.InvalidSignatureS);
}
// If the signature is valid (and not malleable), return the signer address
address signer = ecrecover(hash, v, r, s);
if (signer == address(0)) {
return (address(0), RecoverError.InvalidSignature);
}
return (signer, RecoverError.NoError);
}
/**
* @dev Overload of {ECDSA-recover} that receives the `v`,
* `r` and `s` signature fields separately.
*/
function recover(
bytes32 hash,
uint8 v,
bytes32 r,
bytes32 s
) internal pure returns (address) {
(address recovered, RecoverError error) = tryRecover(hash, v, r, s);
_throwError(error);
return recovered;
}
/**
* @dev Returns an Ethereum Signed Message, created from a `hash`. This
* produces hash corresponding to the one signed with the
* https://eth.wiki/json-rpc/API#eth_sign[`eth_sign`]
* JSON-RPC method as part of EIP-191.
*
* See {recover}.
*/
function toEthSignedMessageHash(bytes32 hash) internal pure returns (bytes32) {
// 32 is the length in bytes of hash,
// enforced by the type signature above
return keccak256(abi.encodePacked("\x19Ethereum Signed Message:\n32", hash));
}
/**
* @dev Returns an Ethereum Signed Message, created from `s`. This
* produces hash corresponding to the one signed with the
* https://eth.wiki/json-rpc/API#eth_sign[`eth_sign`]
* JSON-RPC method as part of EIP-191.
*
* See {recover}.
*/
function toEthSignedMessageHash(bytes memory s) internal pure returns (bytes32) {
return keccak256(abi.encodePacked("\x19Ethereum Signed Message:\n", Strings.toString(s.length), s));
}
/**
* @dev Returns an Ethereum Signed Typed Data, created from a
* `domainSeparator` and a `structHash`. This produces hash corresponding
* to the one signed with the
* https://eips.ethereum.org/EIPS/eip-712[`eth_signTypedData`]
* JSON-RPC method as part of EIP-712.
*
* See {recover}.
*/
function toTypedDataHash(bytes32 domainSeparator, bytes32 structHash) internal pure returns (bytes32) {
return keccak256(abi.encodePacked("\x19\x01", domainSeparator, structHash));
}
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.8.0) (utils/structs/EnumerableSet.sol)
// This file was procedurally generated from scripts/generate/templates/EnumerableSet.js.
pragma solidity ^0.8.0;
/**
* @dev Library for managing
* https://en.wikipedia.org/wiki/Set_(abstract_data_type)[sets] of primitive
* types.
*
* Sets have the following properties:
*
* - Elements are added, removed, and checked for existence in constant time
* (O(1)).
* - Elements are enumerated in O(n). No guarantees are made on the ordering.
*
* ```
* contract Example {
* // Add the library methods
* using EnumerableSet for EnumerableSet.AddressSet;
*
* // Declare a set state variable
* EnumerableSet.AddressSet private mySet;
* }
* ```
*
* As of v3.3.0, sets of type `bytes32` (`Bytes32Set`), `address` (`AddressSet`)
* and `uint256` (`UintSet`) are supported.
*
* [WARNING]
* ====
* Trying to delete such a structure from storage will likely result in data corruption, rendering the structure
* unusable.
* See https://github.com/ethereum/solidity/pull/11843[ethereum/solidity#11843] for more info.
*
* In order to clean an EnumerableSet, you can either remove all elements one by one or create a fresh instance using an
* array of EnumerableSet.
* ====
*/
library EnumerableSet {
// To implement this library for multiple types with as little code
// repetition as possible, we write it in terms of a generic Set type with
// bytes32 values.
// The Set implementation uses private functions, and user-facing
// implementations (such as AddressSet) are just wrappers around the
// underlying Set.
// This means that we can only create new EnumerableSets for types that fit
// in bytes32.
struct Set {
// Storage of set values
bytes32[] _values;
// Position of the value in the `values` array, plus 1 because index 0
// means a value is not in the set.
mapping(bytes32 => uint256) _indexes;
}
/**
* @dev Add a value to a set. O(1).
*
* Returns true if the value was added to the set, that is if it was not
* already present.
*/
function _add(Set storage set, bytes32 value) private returns (bool) {
if (!_contains(set, value)) {
set._values.push(value);
// The value is stored at length-1, but we add 1 to all indexes
// and use 0 as a sentinel value
set._indexes[value] = set._values.length;
return true;
} else {
return false;
}
}
/**
* @dev Removes a value from a set. O(1).
*
* Returns true if the value was removed from the set, that is if it was
* present.
*/
function _remove(Set storage set, bytes32 value) private returns (bool) {
// We read and store the value's index to prevent multiple reads from the same storage slot
uint256 valueIndex = set._indexes[value];
if (valueIndex != 0) {
// Equivalent to contains(set, value)
// To delete an element from the _values array in O(1), we swap the element to delete with the last one in
// the array, and then remove the last element (sometimes called as 'swap and pop').
// This modifies the order of the array, as noted in {at}.
uint256 toDeleteIndex = valueIndex - 1;
uint256 lastIndex = set._values.length - 1;
if (lastIndex != toDeleteIndex) {
bytes32 lastValue = set._values[lastIndex];
// Move the last value to the index where the value to delete is
set._values[toDeleteIndex] = lastValue;
// Update the index for the moved value
set._indexes[lastValue] = valueIndex; // Replace lastValue's index to valueIndex
}
// Delete the slot where the moved value was stored
set._values.pop();
// Delete the index for the deleted slot
delete set._indexes[value];
return true;
} else {
return false;
}
}
/**
* @dev Returns true if the value is in the set. O(1).
*/
function _contains(Set storage set, bytes32 value) private view returns (bool) {
return set._indexes[value] != 0;
}
/**
* @dev Returns the number of values on the set. O(1).
*/
function _length(Set storage set) private view returns (uint256) {
return set._values.length;
}
/**
* @dev Returns the value stored at position `index` in the set. O(1).
*
* Note that there are no guarantees on the ordering of values inside the
* array, and it may change when more values are added or removed.
*
* Requirements:
*
* - `index` must be strictly less than {length}.
*/
function _at(Set storage set, uint256 index) private view returns (bytes32) {
return set._values[index];
}
/**
* @dev Return the entire set in an array
*
* WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
* to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
* this function has an unbounded cost, and using it as part of a state-changing function may render the function
* uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.
*/
function _values(Set storage set) private view returns (bytes32[] memory) {
return set._values;
}
// Bytes32Set
struct Bytes32Set {
Set _inner;
}
/**
* @dev Add a value to a set. O(1).
*
* Returns true if the value was added to the set, that is if it was not
* already present.
*/
function add(Bytes32Set storage set, bytes32 value) internal returns (bool) {
return _add(set._inner, value);
}
/**
* @dev Removes a value from a set. O(1).
*
* Returns true if the value was removed from the set, that is if it was
* present.
*/
function remove(Bytes32Set storage set, bytes32 value) internal returns (bool) {
return _remove(set._inner, value);
}
/**
* @dev Returns true if the value is in the set. O(1).
*/
function contains(Bytes32Set storage set, bytes32 value) internal view returns (bool) {
return _contains(set._inner, value);
}
/**
* @dev Returns the number of values in the set. O(1).
*/
function length(Bytes32Set storage set) internal view returns (uint256) {
return _length(set._inner);
}
/**
* @dev Returns the value stored at position `index` in the set. O(1).
*
* Note that there are no guarantees on the ordering of values inside the
* array, and it may change when more values are added or removed.
*
* Requirements:
*
* - `index` must be strictly less than {length}.
*/
function at(Bytes32Set storage set, uint256 index) internal view returns (bytes32) {
return _at(set._inner, index);
}
/**
* @dev Return the entire set in an array
*
* WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
* to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
* this function has an unbounded cost, and using it as part of a state-changing function may render the function
* uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.
*/
function values(Bytes32Set storage set) internal view returns (bytes32[] memory) {
bytes32[] memory store = _values(set._inner);
bytes32[] memory result;
/// @solidity memory-safe-assembly
assembly {
result := store
}
return result;
}
// AddressSet
struct AddressSet {
Set _inner;
}
/**
* @dev Add a value to a set. O(1).
*
* Returns true if the value was added to the set, that is if it was not
* already present.
*/
function add(AddressSet storage set, address value) internal returns (bool) {
return _add(set._inner, bytes32(uint256(uint160(value))));
}
/**
* @dev Removes a value from a set. O(1).
*
* Returns true if the value was removed from the set, that is if it was
* present.
*/
function remove(AddressSet storage set, address value) internal returns (bool) {
return _remove(set._inner, bytes32(uint256(uint160(value))));
}
/**
* @dev Returns true if the value is in the set. O(1).
*/
function contains(AddressSet storage set, address value) internal view returns (bool) {
return _contains(set._inner, bytes32(uint256(uint160(value))));
}
/**
* @dev Returns the number of values in the set. O(1).
*/
function length(AddressSet storage set) internal view returns (uint256) {
return _length(set._inner);
}
/**
* @dev Returns the value stored at position `index` in the set. O(1).
*
* Note that there are no guarantees on the ordering of values inside the
* array, and it may change when more values are added or removed.
*
* Requirements:
*
* - `index` must be strictly less than {length}.
*/
function at(AddressSet storage set, uint256 index) internal view returns (address) {
return address(uint160(uint256(_at(set._inner, index))));
}
/**
* @dev Return the entire set in an array
*
* WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
* to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
* this function has an unbounded cost, and using it as part of a state-changing function may render the function
* uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.
*/
function values(AddressSet storage set) internal view returns (address[] memory) {
bytes32[] memory store = _values(set._inner);
address[] memory result;
/// @solidity memory-safe-assembly
assembly {
result := store
}
return result;
}
// UintSet
struct UintSet {
Set _inner;
}
/**
* @dev Add a value to a set. O(1).
*
* Returns true if the value was added to the set, that is if it was not
* already present.
*/
function add(UintSet storage set, uint256 value) internal returns (bool) {
return _add(set._inner, bytes32(value));
}
/**
* @dev Removes a value from a set. O(1).
*
* Returns true if the value was removed from the set, that is if it was
* present.
*/
function remove(UintSet storage set, uint256 value) internal returns (bool) {
return _remove(set._inner, bytes32(value));
}
/**
* @dev Returns true if the value is in the set. O(1).
*/
function contains(UintSet storage set, uint256 value) internal view returns (bool) {
return _contains(set._inner, bytes32(value));
}
/**
* @dev Returns the number of values in the set. O(1).
*/
function length(UintSet storage set) internal view returns (uint256) {
return _length(set._inner);
}
/**
* @dev Returns the value stored at position `index` in the set. O(1).
*
* Note that there are no guarantees on the ordering of values inside the
* array, and it may change when more values are added or removed.
*
* Requirements:
*
* - `index` must be strictly less than {length}.
*/
function at(UintSet storage set, uint256 index) internal view returns (uint256) {
return uint256(_at(set._inner, index));
}
/**
* @dev Return the entire set in an array
*
* WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
* to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
* this function has an unbounded cost, and using it as part of a state-changing function may render the function
* uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.
*/
function values(UintSet storage set) internal view returns (uint256[] memory) {
bytes32[] memory store = _values(set._inner);
uint256[] memory result;
/// @solidity memory-safe-assembly
assembly {
result := store
}
return result;
}
}
// SPDX-License-Identifier: MIT
pragma solidity >=0.6.0;
/**
* @title The ERC-2771 Recipient Base Abstract Class - Declarations
*
* @notice A contract must implement this interface in order to support relayed transaction.
*
* @notice It is recommended that your contract inherits from the ERC2771Recipient contract.
*/
abstract contract IERC2771Recipient {
/**
* :warning: **Warning** :warning: The Forwarder can have a full control over your Recipient. Only trust verified Forwarder.
* @param forwarder The address of the Forwarder contract that is being used.
* @return isTrustedForwarder `true` if the Forwarder is trusted to forward relayed transactions by this Recipient.
*/
function isTrustedForwarder(address forwarder) public virtual view returns(bool);
/**
* @notice Use this method the contract anywhere instead of msg.sender to support relayed transactions.
* @return sender The real sender of this call.
* For a call that came through the Forwarder the real sender is extracted from the last 20 bytes of the `msg.data`.
* Otherwise simply returns `msg.sender`.
*/
function _msgSender() internal virtual view returns (address);
/**
* @notice Use this method in the contract instead of `msg.data` when difference matters (hashing, signature, etc.)
* @return data The real `msg.data` of this call.
* For a call that came through the Forwarder, the real sender address was appended as the last 20 bytes
* of the `msg.data` - so this method will strip those 20 bytes off.
* Otherwise (if the call was made directly and not through the forwarder) simply returns `msg.data`.
*/
function _msgData() internal virtual view returns (bytes calldata);
}
// SPDX-License-Identifier: MIT
// Auto-generated using Mage CLI codegen (v1) - DO NOT EDIT
pragma solidity ^0.8.13;
import {TypesLibrary} from "../../core/TypesLibrary.sol";
import {BaseStorageComponentV2, IBaseStorageComponentV2} from "../../core/components/BaseStorageComponentV2.sol";
import {GAME_LOGIC_CONTRACT_ROLE} from "../../Constants.sol";
uint256 constant ID = uint256(keccak256("core.mage.countercomponent.v1"));
struct Layout {
uint256 counts;
}
library CounterComponentStorage {
bytes32 internal constant STORAGE_SLOT = bytes32(ID);
// Declare struct for mapping entity to struct
struct InternalLayout {
mapping(uint256 => Layout) entityIdToStruct;
}
function layout() internal pure returns (InternalLayout storage dataStruct) {
bytes32 position = STORAGE_SLOT;
// solhint-disable-next-line no-inline-assembly
assembly {
dataStruct.slot := position
}
}
}
/**
* @title CounterComponent
* @dev A generic uneditable counter
*/
contract CounterComponent is BaseStorageComponentV2 {
/** SETUP **/
/** Sets the GameRegistry contract address for this contract */
constructor(
address gameRegistryAddress
) BaseStorageComponentV2(gameRegistryAddress, ID) {
// Do nothing
}
/**
* @inheritdoc IBaseStorageComponentV2
*/
function getSchema()
public
pure
override
returns (string[] memory keys, TypesLibrary.SchemaValue[] memory values)
{
keys = new string[](1);
values = new TypesLibrary.SchemaValue[](1);
// Counter tracking total counts
keys[0] = "counts";
values[0] = TypesLibrary.SchemaValue.UINT256;
}
/**
* Sets the typed value for this component
*
* @param entity Entity to get value for
* @param value Layout to set for the given entity
*/
function setLayoutValue(
uint256 entity,
Layout calldata value
) external virtual onlyRole(GAME_LOGIC_CONTRACT_ROLE) {
_setValue(entity, value);
}
/**
* Sets the native value for this component
*
* @param entity Entity to get value for
* @param counts Counter tracking total counts
*/
function setValue(
uint256 entity,
uint256 counts
) external virtual onlyRole(GAME_LOGIC_CONTRACT_ROLE) {
_setValue(entity, Layout(counts));
}
/**
* Batch sets the typed value for this component
*
* @param entities Entity to batch set values for
* @param values Layout to set for the given entities
*/
function batchSetValue(
uint256[] calldata entities,
Layout[] calldata values
) external virtual onlyRole(GAME_LOGIC_CONTRACT_ROLE) {
if (entities.length != values.length) {
revert InvalidBatchData(entities.length, values.length);
}
// Set the values in storage
bytes[] memory encodedValues = new bytes[](entities.length);
for (uint256 i = 0; i < entities.length; i++) {
_setValueToStorage(entities[i], values[i]);
encodedValues[i] = _getEncodedValues(values[i]);
}
// ABI Encode all native types of the struct
_emitBatchSetBytes(entities, encodedValues);
}
/**
* Returns the typed value for this component
*
* @param entity Entity to get value for
* @return value Layout value for the given entity
*/
function getLayoutValue(
uint256 entity
) external view virtual returns (Layout memory value) {
// Get the struct from storage
value = CounterComponentStorage.layout().entityIdToStruct[entity];
}
/**
* Returns the native values for this component
*
* @param entity Entity to get value for
* @return counts Counter tracking total counts
*/
function getValue(
uint256 entity
)
external
view
virtual
returns (
uint256 counts
)
{
if (has(entity)) {
Layout memory s = CounterComponentStorage.layout().entityIdToStruct[entity];
(counts) = abi.decode(
_getEncodedValues(s),
(uint256)
);
}
}
/**
* Returns an array of byte values for each field of this component.
*
* @param entity Entity to build array of byte values for.
*/
function getByteValues(
uint256 entity
) external view virtual returns (bytes[] memory values) {
// Get the struct from storage
Layout storage s = CounterComponentStorage
.layout()
.entityIdToStruct[entity];
// ABI Encode all fields of the struct and add to values array
values = new bytes[](1);
values[0] = abi.encode(s.counts);
}
/**
* Returns the bytes value for this component
*
* @param entity Entity to get value for
*/
function getBytes(
uint256 entity
) external view returns (bytes memory value) {
Layout memory s = CounterComponentStorage.layout().entityIdToStruct[entity];
value = _getEncodedValues(s);
}
/**
* Sets the value of this component using a byte array
*
* @param entity Entity to set value for
*/
function setBytes(
uint256 entity,
bytes calldata value
) external onlyRole(GAME_LOGIC_CONTRACT_ROLE) {
Layout memory s = CounterComponentStorage.layout().entityIdToStruct[entity];
(s.counts) = abi.decode(value, (uint256));
_setValueToStorage(entity, s);
// ABI Encode all native types of the struct
_emitSetBytes(
entity,
value
);
}
/**
* Sets bytes data in batch format
*
* @param entities Entities to set value for
* @param values Bytes values to set for the given entities
*/
function batchSetBytes(
uint256[] calldata entities,
bytes[] calldata values
) external onlyRole(GAME_LOGIC_CONTRACT_ROLE) {
if (entities.length != values.length) {
revert InvalidBatchData(entities.length, values.length);
}
for (uint256 i = 0; i < entities.length; i++) {
Layout memory s = CounterComponentStorage.layout().entityIdToStruct[entities[i]];
(s.counts) = abi.decode(values[i], (uint256));
_setValueToStorage(entities[i], s);
}
// ABI Encode all native types of the struct
_emitBatchSetBytes(
entities,
values
);
}
/**
* Remove the given entity from this component.
*
* @param entity Entity to remove from this component.
*/
function remove(uint256 entity) public virtual onlyRole(GAME_LOGIC_CONTRACT_ROLE) {
// Remove the entity from the component
delete CounterComponentStorage.layout().entityIdToStruct[entity];
_emitRemoveBytes(entity);
}
/**
* Batch remove the given entities from this component.
*
* @param entities Entities to remove from this component.
*/
function batchRemove(uint256[] calldata entities) public virtual onlyRole(GAME_LOGIC_CONTRACT_ROLE) {
// Remove the entities from the component
for (uint256 i = 0; i < entities.length; i++) {
delete CounterComponentStorage.layout().entityIdToStruct[
entities[i]
];
}
_emitBatchRemoveBytes(entities);
}
/**
* Check whether the given entity has a value in this component.
*
* @param entity Entity to check whether it has a value in this component for.
*/
function has(uint256 entity) public view virtual returns (bool) {
return gameRegistry.getEntityHasComponent(entity, ID);
}
/** INTERNAL **/
function _setValueToStorage(
uint256 entity,
Layout memory value
) internal {
Layout storage s = CounterComponentStorage
.layout()
.entityIdToStruct[entity];
s.counts = value.counts;
}
function _setValue(
uint256 entity,
Layout memory value
) internal {
_setValueToStorage(entity, value);
// ABI Encode all native types of the struct
_emitSetBytes(
entity,
abi.encode(value.counts)
);
}
function _getEncodedValues(
Layout memory value
) internal pure returns (bytes memory) {
return
abi.encode(
value.counts
);
}
}
// SPDX-License-Identifier: MIT LICENSE
pragma solidity ^0.8.9;
/**
* Enum of supported schema types
* Note: This is pulled directly from MUD (mud.dev) to maintain compatibility
*/
library TypesLibrary {
enum SchemaValue {
BOOL,
INT8,
INT16,
INT32,
INT64,
INT128,
INT256,
INT,
UINT8,
UINT16,
UINT32,
UINT64,
UINT128,
UINT256,
BYTES,
STRING,
ADDRESS,
BYTES4,
BOOL_ARRAY,
INT8_ARRAY,
INT16_ARRAY,
INT32_ARRAY,
INT64_ARRAY,
INT128_ARRAY,
INT256_ARRAY,
INT_ARRAY,
UINT8_ARRAY,
UINT16_ARRAY,
UINT32_ARRAY,
UINT64_ARRAY,
UINT128_ARRAY,
UINT256_ARRAY,
BYTES_ARRAY,
STRING_ARRAY,
ADDRESS_ARRAY
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;
import {IBaseStorageComponentV2} from "./IBaseStorageComponentV2.sol";
import "../GameRegistryConsumerV2.sol";
/**
* @title BaseStorageComponentV2
* @notice Base storage component class, version 2
*/
abstract contract BaseStorageComponentV2 is
IBaseStorageComponentV2,
GameRegistryConsumerV2
{
/// @notice Invalid data count compared to number of entity count
error InvalidBatchData(uint256 entityCount, uint256 valueCount);
/** SETUP **/
/**
* @param _gameRegistryAddress Address of the GameRegistry contract
* @param id ID of the component being created
*/
constructor(
address _gameRegistryAddress,
uint256 id
) GameRegistryConsumerV2(_gameRegistryAddress, id) {
// Do nothing
}
/** INTERNAL */
/**
* Use GameRegistry to trigger emit when setting
* @param entity Entity to set the value for.
* @param value Value to set for the given entity.
*/
function _emitSetBytes(
uint256 entity,
bytes memory value
) internal virtual {
// Emit global event
gameRegistry.registerComponentValueSet(entity, value);
}
/**
* Use GameRegistry to trigger emit when setting
* @param entities Array of entities to set values for.
* @param values Array of values to set for a given entity.
*/
function _emitBatchSetBytes(
uint256[] calldata entities,
bytes[] memory values
) internal virtual {
// Emit global event
gameRegistry.batchRegisterComponentValueSet(entities, values);
}
/**
* Use GameRegistry to trigger emit when removing
* @param entity Entity to remove from this component.
*/
function _emitRemoveBytes(uint256 entity) internal virtual {
// Emit global event
gameRegistry.registerComponentValueRemoved(entity);
}
/**
* Use GameRegistry to trigger emit when removing
* @param entities Array of entities to remove from this component.
*/
function _emitBatchRemoveBytes(
uint256[] calldata entities
) internal virtual {
// Emit global event
gameRegistry.batchRegisterComponentValueRemoved(entities);
}
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (utils/introspection/IERC165.sol)
pragma solidity ^0.8.0;
/**
* @dev Interface of the ERC165 standard, as defined in the
* https://eips.ethereum.org/EIPS/eip-165[EIP].
*
* 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[EIP 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);
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (utils/Context.sol)
pragma solidity ^0.8.0;
import "../proxy/utils/Initializable.sol";
/**
* @dev Provides information about the current execution context, including the
* sender of the transaction and its data. While these are generally available
* via msg.sender and msg.data, they should not be accessed in such a direct
* manner, since when dealing with meta-transactions the account sending and
* paying for execution may not be the actual sender (as far as an application
* is concerned).
*
* This contract is only required for intermediate, library-like contracts.
*/
abstract contract ContextUpgradeable is Initializable {
function __Context_init() internal onlyInitializing {
}
function __Context_init_unchained() internal onlyInitializing {
}
function _msgSender() internal view virtual returns (address) {
return msg.sender;
}
function _msgData() internal view virtual returns (bytes calldata) {
return msg.data;
}
/**
* @dev This empty reserved space is put in place to allow future versions to add new
* variables without shifting down storage in the inheritance chain.
* See https://docs.openzeppelin.com/contracts/4.x/upgradeable#storage_gaps
*/
uint256[50] private __gap;
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (access/IAccessControl.sol)
pragma solidity ^0.8.0;
/**
* @dev External interface of AccessControl declared to support ERC165 detection.
*/
interface IAccessControlUpgradeable {
/**
* @dev Emitted when `newAdminRole` is set as ``role``'s admin role, replacing `previousAdminRole`
*
* `DEFAULT_ADMIN_ROLE` is the starting admin for all roles, despite
* {RoleAdminChanged} not being emitted signaling this.
*
* _Available since v3.1._
*/
event RoleAdminChanged(bytes32 indexed role, bytes32 indexed previousAdminRole, bytes32 indexed newAdminRole);
/**
* @dev Emitted when `account` is granted `role`.
*
* `sender` is the account that originated the contract call, an admin role
* bearer except when using {AccessControl-_setupRole}.
*/
event RoleGranted(bytes32 indexed role, address indexed account, address indexed sender);
/**
* @dev Emitted when `account` is revoked `role`.
*
* `sender` is the account that originated the contract call:
* - if using `revokeRole`, it is the admin role bearer
* - if using `renounceRole`, it is the role bearer (i.e. `account`)
*/
event RoleRevoked(bytes32 indexed role, address indexed account, address indexed sender);
/**
* @dev Returns `true` if `account` has been granted `role`.
*/
function hasRole(bytes32 role, address account) external view returns (bool);
/**
* @dev Returns the admin role that controls `role`. See {grantRole} and
* {revokeRole}.
*
* To change a role's admin, use {AccessControl-_setRoleAdmin}.
*/
function getRoleAdmin(bytes32 role) external view returns (bytes32);
/**
* @dev Grants `role` to `account`.
*
* If `account` had not been already granted `role`, emits a {RoleGranted}
* event.
*
* Requirements:
*
* - the caller must have ``role``'s admin role.
*/
function grantRole(bytes32 role, address account) external;
/**
* @dev Revokes `role` from `account`.
*
* If `account` had been granted `role`, emits a {RoleRevoked} event.
*
* Requirements:
*
* - the caller must have ``role``'s admin role.
*/
function revokeRole(bytes32 role, address account) external;
/**
* @dev Revokes `role` from the calling account.
*
* Roles are often managed via {grantRole} and {revokeRole}: this function's
* purpose is to provide a mechanism for accounts to lose their privileges
* if they are compromised (such as when a trusted device is misplaced).
*
* If the calling account had been granted `role`, emits a {RoleRevoked}
* event.
*
* Requirements:
*
* - the caller must be `account`.
*/
function renounceRole(bytes32 role, address account) external;
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.8.0) (utils/Strings.sol)
pragma solidity ^0.8.0;
import "./math/MathUpgradeable.sol";
/**
* @dev String operations.
*/
library StringsUpgradeable {
bytes16 private constant _SYMBOLS = "0123456789abcdef";
uint8 private constant _ADDRESS_LENGTH = 20;
/**
* @dev Converts a `uint256` to its ASCII `string` decimal representation.
*/
function toString(uint256 value) internal pure returns (string memory) {
unchecked {
uint256 length = MathUpgradeable.log10(value) + 1;
string memory buffer = new string(length);
uint256 ptr;
/// @solidity memory-safe-assembly
assembly {
ptr := add(buffer, add(32, length))
}
while (true) {
ptr--;
/// @solidity memory-safe-assembly
assembly {
mstore8(ptr, byte(mod(value, 10), _SYMBOLS))
}
value /= 10;
if (value == 0) break;
}
return buffer;
}
}
/**
* @dev Converts a `uint256` to its ASCII `string` hexadecimal representation.
*/
function toHexString(uint256 value) internal pure returns (string memory) {
unchecked {
return toHexString(value, MathUpgradeable.log256(value) + 1);
}
}
/**
* @dev Converts a `uint256` to its ASCII `string` hexadecimal representation with fixed length.
*/
function toHexString(uint256 value, uint256 length) internal pure returns (string memory) {
bytes memory buffer = new bytes(2 * length + 2);
buffer[0] = "0";
buffer[1] = "x";
for (uint256 i = 2 * length + 1; i > 1; --i) {
buffer[i] = _SYMBOLS[value & 0xf];
value >>= 4;
}
require(value == 0, "Strings: hex length insufficient");
return string(buffer);
}
/**
* @dev Converts an `address` with fixed length of 20 bytes to its not checksummed ASCII `string` hexadecimal representation.
*/
function toHexString(address addr) internal pure returns (string memory) {
return toHexString(uint256(uint160(addr)), _ADDRESS_LENGTH);
}
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.8.0) (proxy/utils/Initializable.sol)
pragma solidity ^0.8.2;
import "../../utils/AddressUpgradeable.sol";
/**
* @dev This is a base contract to aid in writing upgradeable contracts, or any kind of contract that will be deployed
* behind a proxy. Since proxied contracts do not make use of a constructor, it's common to move constructor logic to an
* external initializer function, usually called `initialize`. It then becomes necessary to protect this initializer
* function so it can only be called once. The {initializer} modifier provided by this contract will have this effect.
*
* The initialization functions use a version number. Once a version number is used, it is consumed and cannot be
* reused. This mechanism prevents re-execution of each "step" but allows the creation of new initialization steps in
* case an upgrade adds a module that needs to be initialized.
*
* For example:
*
* [.hljs-theme-light.nopadding]
* ```
* contract MyToken is ERC20Upgradeable {
* function initialize() initializer public {
* __ERC20_init("MyToken", "MTK");
* }
* }
* contract MyTokenV2 is MyToken, ERC20PermitUpgradeable {
* function initializeV2() reinitializer(2) public {
* __ERC20Permit_init("MyToken");
* }
* }
* ```
*
* TIP: To avoid leaving the proxy in an uninitialized state, the initializer function should be called as early as
* possible by providing the encoded function call as the `_data` argument to {ERC1967Proxy-constructor}.
*
* CAUTION: When used with inheritance, manual care must be taken to not invoke a parent initializer twice, or to ensure
* that all initializers are idempotent. This is not verified automatically as constructors are by Solidity.
*
* [CAUTION]
* ====
* Avoid leaving a contract uninitialized.
*
* An uninitialized contract can be taken over by an attacker. This applies to both a proxy and its implementation
* contract, which may impact the proxy. To prevent the implementation contract from being used, you should invoke
* the {_disableInitializers} function in the constructor to automatically lock it when it is deployed:
*
* [.hljs-theme-light.nopadding]
* ```
* /// @custom:oz-upgrades-unsafe-allow constructor
* constructor() {
* _disableInitializers();
* }
* ```
* ====
*/
abstract contract Initializable {
/**
* @dev Indicates that the contract has been initialized.
* @custom:oz-retyped-from bool
*/
uint8 private _initialized;
/**
* @dev Indicates that the contract is in the process of being initialized.
*/
bool private _initializing;
/**
* @dev Triggered when the contract has been initialized or reinitialized.
*/
event Initialized(uint8 version);
/**
* @dev A modifier that defines a protected initializer function that can be invoked at most once. In its scope,
* `onlyInitializing` functions can be used to initialize parent contracts.
*
* Similar to `reinitializer(1)`, except that functions marked with `initializer` can be nested in the context of a
* constructor.
*
* Emits an {Initialized} event.
*/
modifier initializer() {
bool isTopLevelCall = !_initializing;
require(
(isTopLevelCall && _initialized < 1) || (!AddressUpgradeable.isContract(address(this)) && _initialized == 1),
"Initializable: contract is already initialized"
);
_initialized = 1;
if (isTopLevelCall) {
_initializing = true;
}
_;
if (isTopLevelCall) {
_initializing = false;
emit Initialized(1);
}
}
/**
* @dev A modifier that defines a protected reinitializer function that can be invoked at most once, and only if the
* contract hasn't been initialized to a greater version before. In its scope, `onlyInitializing` functions can be
* used to initialize parent contracts.
*
* A reinitializer may be used after the original initialization step. This is essential to configure modules that
* are added through upgrades and that require initialization.
*
* When `version` is 1, this modifier is similar to `initializer`, except that functions marked with `reinitializer`
* cannot be nested. If one is invoked in the context of another, execution will revert.
*
* Note that versions can jump in increments greater than 1; this implies that if multiple reinitializers coexist in
* a contract, executing them in the right order is up to the developer or operator.
*
* WARNING: setting the version to 255 will prevent any future reinitialization.
*
* Emits an {Initialized} event.
*/
modifier reinitializer(uint8 version) {
require(!_initializing && _initialized < version, "Initializable: contract is already initialized");
_initialized = version;
_initializing = true;
_;
_initializing = false;
emit Initialized(version);
}
/**
* @dev Modifier to protect an initialization function so that it can only be invoked by functions with the
* {initializer} and {reinitializer} modifiers, directly or indirectly.
*/
modifier onlyInitializing() {
require(_initializing, "Initializable: contract is not initializing");
_;
}
/**
* @dev Locks the contract, preventing any future reinitialization. This cannot be part of an initializer call.
* Calling this in the constructor of a contract will prevent that contract from being initialized or reinitialized
* to any version. It is recommended to use this to lock implementation contracts that are designed to be called
* through proxies.
*
* Emits an {Initialized} event the first time it is successfully executed.
*/
function _disableInitializers() internal virtual {
require(!_initializing, "Initializable: contract is initializing");
if (_initialized < type(uint8).max) {
_initialized = type(uint8).max;
emit Initialized(type(uint8).max);
}
}
/**
* @dev Internal function that returns the initialized version. Returns `_initialized`
*/
function _getInitializedVersion() internal view returns (uint8) {
return _initialized;
}
/**
* @dev Internal function that returns the initialized version. Returns `_initializing`
*/
function _isInitializing() internal view returns (bool) {
return _initializing;
}
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (utils/Context.sol)
pragma solidity ^0.8.0;
/**
* @dev Provides information about the current execution context, including the
* sender of the transaction and its data. While these are generally available
* via msg.sender and msg.data, they should not be accessed in such a direct
* manner, since when dealing with meta-transactions the account sending and
* paying for execution may not be the actual sender (as far as an application
* is concerned).
*
* This contract is only required for intermediate, library-like contracts.
*/
abstract contract Context {
function _msgSender() internal view virtual returns (address) {
return msg.sender;
}
function _msgData() internal view virtual returns (bytes calldata) {
return msg.data;
}
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (utils/introspection/ERC165.sol)
pragma solidity ^0.8.0;
import "./IERC165Upgradeable.sol";
import "../../proxy/utils/Initializable.sol";
/**
* @dev Implementation of the {IERC165} interface.
*
* Contracts that want to implement ERC165 should inherit from this contract and override {supportsInterface} to check
* for the additional interface id that will be supported. For example:
*
* ```solidity
* function supportsInterface(bytes4 interfaceId) public view virtual override returns (bool) {
* return interfaceId == type(MyInterface).interfaceId || super.supportsInterface(interfaceId);
* }
* ```
*
* Alternatively, {ERC165Storage} provides an easier to use but more expensive implementation.
*/
abstract contract ERC165Upgradeable is Initializable, IERC165Upgradeable {
function __ERC165_init() internal onlyInitializing {
}
function __ERC165_init_unchained() internal onlyInitializing {
}
/**
* @dev See {IERC165-supportsInterface}.
*/
function supportsInterface(bytes4 interfaceId) public view virtual override returns (bool) {
return interfaceId == type(IERC165Upgradeable).interfaceId;
}
/**
* @dev This empty reserved space is put in place to allow future versions to add new
* variables without shifting down storage in the inheritance chain.
* See https://docs.openzeppelin.com/contracts/4.x/upgradeable#storage_gaps
*/
uint256[50] private __gap;
}
// SPDX-License-Identifier: MIT LICENSE
pragma solidity ^0.8.9;
import "@openzeppelin/contracts/access/Ownable.sol";
import "@opengsn/contracts/src/interfaces/IERC2771Recipient.sol";
import {IGameRegistry} from "./IGameRegistry.sol";
import {ISystem} from "./ISystem.sol";
import {TRUSTED_FORWARDER_ROLE, MANAGER_ROLE} from "../Constants.sol";
/** @title Contract that lets a child contract access the GameRegistry contract */
contract GameRegistryConsumerV2 is ISystem, Ownable, IERC2771Recipient {
/// @notice Id for the system/component
uint256 private _id;
/// @notice Read access contract
IGameRegistry public gameRegistry;
/** ERRORS **/
/// @notice Not authorized to perform action
error MissingRole(address account, bytes32 expectedRole);
/** MODIFIERS **/
// Modifier to verify a user has the appropriate role to call a given function
modifier onlyRole(bytes32 role) {
_checkRole(role, _msgSender());
_;
}
/** ERRORS **/
/// @notice gameRegistryAddress does not implement IGameRegistry
error InvalidGameRegistry();
/** SETUP **/
/** Sets the GameRegistry contract address for this contract */
constructor(address gameRegistryAddress, uint256 id) {
gameRegistry = IGameRegistry(gameRegistryAddress);
_id = id;
if (gameRegistryAddress == address(0)) {
revert InvalidGameRegistry();
}
}
/** EXTERNAL **/
/** @return ID for this system */
function getId() public view override returns (uint256) {
return _id;
}
/**
* 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;
}
/**
* @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);
}
}
/**
* Returns the Player address for the Operator account
* @param operatorAccount address of the Operator account to retrieve the player for
*/
function _getPlayerAccount(
address operatorAccount
) internal view returns (address playerAccount) {
return gameRegistry.getPlayerAccount(operatorAccount);
}
/// @inheritdoc IERC2771Recipient
function isTrustedForwarder(
address forwarder
) public view virtual override returns (bool) {
return
address(gameRegistry) != address(0) &&
_hasAccessRole(TRUSTED_FORWARDER_ROLE, forwarder);
}
/** INTERNAL **/
/// @inheritdoc IERC2771Recipient
function _msgSender()
internal
view
virtual
override(Context, IERC2771Recipient)
returns (address ret)
{
if (msg.data.length >= 20 && isTrustedForwarder(msg.sender)) {
assembly {
ret := shr(96, calldataload(sub(calldatasize(), 20)))
}
} else {
ret = msg.sender;
}
}
/// @inheritdoc IERC2771Recipient
function _msgData()
internal
view
virtual
override(Context, IERC2771Recipient)
returns (bytes calldata ret)
{
if (msg.data.length >= 20 && isTrustedForwarder(msg.sender)) {
return msg.data[0:msg.data.length - 20];
} else {
return msg.data;
}
}
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.8.0) (utils/math/Math.sol)
pragma solidity ^0.8.0;
/**
* @dev Standard math utilities missing in the Solidity language.
*/
library Math {
enum Rounding {
Down, // Toward negative infinity
Up, // Toward infinity
Zero // Toward zero
}
/**
* @dev Returns the largest of two numbers.
*/
function max(uint256 a, uint256 b) internal pure returns (uint256) {
return a > b ? a : b;
}
/**
* @dev Returns the smallest of two numbers.
*/
function min(uint256 a, uint256 b) internal pure returns (uint256) {
return a < b ? a : b;
}
/**
* @dev Returns the average of two numbers. The result is rounded towards
* zero.
*/
function average(uint256 a, uint256 b) internal pure returns (uint256) {
// (a + b) / 2 can overflow.
return (a & b) + (a ^ b) / 2;
}
/**
* @dev Returns the ceiling of the division of two numbers.
*
* This differs from standard division with `/` in that it rounds up instead
* of rounding down.
*/
function ceilDiv(uint256 a, uint256 b) internal pure returns (uint256) {
// (a + b - 1) / b can overflow on addition, so we distribute.
return a == 0 ? 0 : (a - 1) / b + 1;
}
/**
* @notice Calculates floor(x * y / denominator) with full precision. Throws if result overflows a uint256 or denominator == 0
* @dev Original credit to Remco Bloemen under MIT license (https://xn--2-umb.com/21/muldiv)
* with further edits by Uniswap Labs also under MIT license.
*/
function mulDiv(
uint256 x,
uint256 y,
uint256 denominator
) internal pure returns (uint256 result) {
unchecked {
// 512-bit multiply [prod1 prod0] = x * y. Compute the product mod 2^256 and mod 2^256 - 1, then use
// use the Chinese Remainder Theorem to reconstruct the 512 bit result. The result is stored in two 256
// variables such that product = prod1 * 2^256 + prod0.
uint256 prod0; // Least significant 256 bits of the product
uint256 prod1; // Most significant 256 bits of the product
assembly {
let mm := mulmod(x, y, not(0))
prod0 := mul(x, y)
prod1 := sub(sub(mm, prod0), lt(mm, prod0))
}
// Handle non-overflow cases, 256 by 256 division.
if (prod1 == 0) {
return prod0 / denominator;
}
// Make sure the result is less than 2^256. Also prevents denominator == 0.
require(denominator > prod1);
///////////////////////////////////////////////
// 512 by 256 division.
///////////////////////////////////////////////
// Make division exact by subtracting the remainder from [prod1 prod0].
uint256 remainder;
assembly {
// Compute remainder using mulmod.
remainder := mulmod(x, y, denominator)
// Subtract 256 bit number from 512 bit number.
prod1 := sub(prod1, gt(remainder, prod0))
prod0 := sub(prod0, remainder)
}
// Factor powers of two out of denominator and compute largest power of two divisor of denominator. Always >= 1.
// See https://cs.stackexchange.com/q/138556/92363.
// Does not overflow because the denominator cannot be zero at this stage in the function.
uint256 twos = denominator & (~denominator + 1);
assembly {
// Divide denominator by twos.
denominator := div(denominator, twos)
// Divide [prod1 prod0] by twos.
prod0 := div(prod0, twos)
// Flip twos such that it is 2^256 / twos. If twos is zero, then it becomes one.
twos := add(div(sub(0, twos), twos), 1)
}
// Shift in bits from prod1 into prod0.
prod0 |= prod1 * twos;
// Invert denominator mod 2^256. Now that denominator is an odd number, it has an inverse modulo 2^256 such
// that denominator * inv = 1 mod 2^256. Compute the inverse by starting with a seed that is correct for
// four bits. That is, denominator * inv = 1 mod 2^4.
uint256 inverse = (3 * denominator) ^ 2;
// Use the Newton-Raphson iteration to improve the precision. Thanks to Hensel's lifting lemma, this also works
// in modular arithmetic, doubling the correct bits in each step.
inverse *= 2 - denominator * inverse; // inverse mod 2^8
inverse *= 2 - denominator * inverse; // inverse mod 2^16
inverse *= 2 - denominator * inverse; // inverse mod 2^32
inverse *= 2 - denominator * inverse; // inverse mod 2^64
inverse *= 2 - denominator * inverse; // inverse mod 2^128
inverse *= 2 - denominator * inverse; // inverse mod 2^256
// Because the division is now exact we can divide by multiplying with the modular inverse of denominator.
// This will give us the correct result modulo 2^256. Since the preconditions guarantee that the outcome is
// less than 2^256, this is the final result. We don't need to compute the high bits of the result and prod1
// is no longer required.
result = prod0 * inverse;
return result;
}
}
/**
* @notice Calculates x * y / denominator with full precision, following the selected rounding direction.
*/
function mulDiv(
uint256 x,
uint256 y,
uint256 denominator,
Rounding rounding
) internal pure returns (uint256) {
uint256 result = mulDiv(x, y, denominator);
if (rounding == Rounding.Up && mulmod(x, y, denominator) > 0) {
result += 1;
}
return result;
}
/**
* @dev Returns the square root of a number. If the number is not a perfect square, the value is rounded down.
*
* Inspired by Henry S. Warren, Jr.'s "Hacker's Delight" (Chapter 11).
*/
function sqrt(uint256 a) internal pure returns (uint256) {
if (a == 0) {
return 0;
}
// For our first guess, we get the biggest power of 2 which is smaller than the square root of the target.
//
// We know that the "msb" (most significant bit) of our target number `a` is a power of 2 such that we have
// `msb(a) <= a < 2*msb(a)`. This value can be written `msb(a)=2**k` with `k=log2(a)`.
//
// This can be rewritten `2**log2(a) <= a < 2**(log2(a) + 1)`
// → `sqrt(2**k) <= sqrt(a) < sqrt(2**(k+1))`
// → `2**(k/2) <= sqrt(a) < 2**((k+1)/2) <= 2**(k/2 + 1)`
//
// Consequently, `2**(log2(a) / 2)` is a good first approximation of `sqrt(a)` with at least 1 correct bit.
uint256 result = 1 << (log2(a) >> 1);
// At this point `result` is an estimation with one bit of precision. We know the true value is a uint128,
// since it is the square root of a uint256. Newton's method converges quadratically (precision doubles at
// every iteration). We thus need at most 7 iteration to turn our partial result with one bit of precision
// into the expected uint128 result.
unchecked {
result = (result + a / result) >> 1;
result = (result + a / result) >> 1;
result = (result + a / result) >> 1;
result = (result + a / result) >> 1;
result = (result + a / result) >> 1;
result = (result + a / result) >> 1;
result = (result + a / result) >> 1;
return min(result, a / result);
}
}
/**
* @notice Calculates sqrt(a), following the selected rounding direction.
*/
function sqrt(uint256 a, Rounding rounding) internal pure returns (uint256) {
unchecked {
uint256 result = sqrt(a);
return result + (rounding == Rounding.Up && result * result < a ? 1 : 0);
}
}
/**
* @dev Return the log in base 2, rounded down, of a positive value.
* Returns 0 if given 0.
*/
function log2(uint256 value) internal pure returns (uint256) {
uint256 result = 0;
unchecked {
if (value >> 128 > 0) {
value >>= 128;
result += 128;
}
if (value >> 64 > 0) {
value >>= 64;
result += 64;
}
if (value >> 32 > 0) {
value >>= 32;
result += 32;
}
if (value >> 16 > 0) {
value >>= 16;
result += 16;
}
if (value >> 8 > 0) {
value >>= 8;
result += 8;
}
if (value >> 4 > 0) {
value >>= 4;
result += 4;
}
if (value >> 2 > 0) {
value >>= 2;
result += 2;
}
if (value >> 1 > 0) {
result += 1;
}
}
return result;
}
/**
* @dev Return the log in base 2, following the selected rounding direction, of a positive value.
* Returns 0 if given 0.
*/
function log2(uint256 value, Rounding rounding) internal pure returns (uint256) {
unchecked {
uint256 result = log2(value);
return result + (rounding == Rounding.Up && 1 << result < value ? 1 : 0);
}
}
/**
* @dev Return the log in base 10, rounded down, of a positive value.
* Returns 0 if given 0.
*/
function log10(uint256 value) internal pure returns (uint256) {
uint256 result = 0;
unchecked {
if (value >= 10**64) {
value /= 10**64;
result += 64;
}
if (value >= 10**32) {
value /= 10**32;
result += 32;
}
if (value >= 10**16) {
value /= 10**16;
result += 16;
}
if (value >= 10**8) {
value /= 10**8;
result += 8;
}
if (value >= 10**4) {
value /= 10**4;
result += 4;
}
if (value >= 10**2) {
value /= 10**2;
result += 2;
}
if (value >= 10**1) {
result += 1;
}
}
return result;
}
/**
* @dev Return the log in base 10, following the selected rounding direction, of a positive value.
* Returns 0 if given 0.
*/
function log10(uint256 value, Rounding rounding) internal pure returns (uint256) {
unchecked {
uint256 result = log10(value);
return result + (rounding == Rounding.Up && 10**result < value ? 1 : 0);
}
}
/**
* @dev Return the log in base 256, rounded down, of a positive value.
* Returns 0 if given 0.
*
* Adding one to the result gives the number of pairs of hex symbols needed to represent `value` as a hex string.
*/
function log256(uint256 value) internal pure returns (uint256) {
uint256 result = 0;
unchecked {
if (value >> 128 > 0) {
value >>= 128;
result += 16;
}
if (value >> 64 > 0) {
value >>= 64;
result += 8;
}
if (value >> 32 > 0) {
value >>= 32;
result += 4;
}
if (value >> 16 > 0) {
value >>= 16;
result += 2;
}
if (value >> 8 > 0) {
result += 1;
}
}
return result;
}
/**
* @dev Return the log in base 10, following the selected rounding direction, of a positive value.
* Returns 0 if given 0.
*/
function log256(uint256 value, Rounding rounding) internal pure returns (uint256) {
unchecked {
uint256 result = log256(value);
return result + (rounding == Rounding.Up && 1 << (result * 8) < value ? 1 : 0);
}
}
}
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0;
import {TypesLibrary} from "../TypesLibrary.sol";
interface IBaseStorageComponentV2 {
/** Return the keys and value types of the schema of this component. */
function getSchema()
external
pure
returns (
string[] memory keys,
TypesLibrary.SchemaValue[] memory values
);
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.6.0) (token/ERC20/IERC20.sol)
pragma solidity ^0.8.0;
/**
* @dev Interface of the ERC20 standard as defined in the EIP.
*/
interface IERC20 {
/**
* @dev Emitted when `value` tokens are moved from one account (`from`) to
* another (`to`).
*
* Note that `value` may be zero.
*/
event Transfer(address indexed from, address indexed to, uint256 value);
/**
* @dev Emitted when the allowance of a `spender` for an `owner` is set by
* a call to {approve}. `value` is the new allowance.
*/
event Approval(address indexed owner, address indexed spender, uint256 value);
/**
* @dev Returns the amount of tokens in existence.
*/
function totalSupply() external view returns (uint256);
/**
* @dev Returns the amount of tokens owned by `account`.
*/
function balanceOf(address account) external view returns (uint256);
/**
* @dev Moves `amount` tokens from the caller's account to `to`.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* Emits a {Transfer} event.
*/
function transfer(address to, uint256 amount) external returns (bool);
/**
* @dev Returns the remaining number of tokens that `spender` will be
* allowed to spend on behalf of `owner` through {transferFrom}. This is
* zero by default.
*
* This value changes when {approve} or {transferFrom} are called.
*/
function allowance(address owner, address spender) external view returns (uint256);
/**
* @dev Sets `amount` as the allowance of `spender` over the caller's tokens.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* IMPORTANT: Beware that changing an allowance with this method brings the risk
* that someone may use both the old and the new allowance by unfortunate
* transaction ordering. One possible solution to mitigate this race
* condition is to first reduce the spender's allowance to 0 and set the
* desired value afterwards:
* https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
*
* Emits an {Approval} event.
*/
function approve(address spender, uint256 amount) external returns (bool);
/**
* @dev Moves `amount` tokens from `from` to `to` using the
* allowance mechanism. `amount` is then deducted from the caller's
* allowance.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* Emits a {Transfer} event.
*/
function transferFrom(
address from,
address to,
uint256 amount
) external returns (bool);
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (token/ERC20/extensions/IERC20Metadata.sol)
pragma solidity ^0.8.0;
import "../IERC20.sol";
/**
* @dev Interface for the optional metadata functions from the ERC20 standard.
*
* _Available since v4.1._
*/
interface IERC20Metadata is IERC20 {
/**
* @dev Returns the name of the token.
*/
function name() external view returns (string memory);
/**
* @dev Returns the symbol of the token.
*/
function symbol() external view returns (string memory);
/**
* @dev Returns the decimals places of the token.
*/
function decimals() external view returns (uint8);
}
// SPDX-License-Identifier: MIT LICENSE
pragma solidity ^0.8.13;
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);
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.8.0) (utils/math/Math.sol)
pragma solidity ^0.8.0;
/**
* @dev Standard math utilities missing in the Solidity language.
*/
library MathUpgradeable {
enum Rounding {
Down, // Toward negative infinity
Up, // Toward infinity
Zero // Toward zero
}
/**
* @dev Returns the largest of two numbers.
*/
function max(uint256 a, uint256 b) internal pure returns (uint256) {
return a > b ? a : b;
}
/**
* @dev Returns the smallest of two numbers.
*/
function min(uint256 a, uint256 b) internal pure returns (uint256) {
return a < b ? a : b;
}
/**
* @dev Returns the average of two numbers. The result is rounded towards
* zero.
*/
function average(uint256 a, uint256 b) internal pure returns (uint256) {
// (a + b) / 2 can overflow.
return (a & b) + (a ^ b) / 2;
}
/**
* @dev Returns the ceiling of the division of two numbers.
*
* This differs from standard division with `/` in that it rounds up instead
* of rounding down.
*/
function ceilDiv(uint256 a, uint256 b) internal pure returns (uint256) {
// (a + b - 1) / b can overflow on addition, so we distribute.
return a == 0 ? 0 : (a - 1) / b + 1;
}
/**
* @notice Calculates floor(x * y / denominator) with full precision. Throws if result overflows a uint256 or denominator == 0
* @dev Original credit to Remco Bloemen under MIT license (https://xn--2-umb.com/21/muldiv)
* with further edits by Uniswap Labs also under MIT license.
*/
function mulDiv(
uint256 x,
uint256 y,
uint256 denominator
) internal pure returns (uint256 result) {
unchecked {
// 512-bit multiply [prod1 prod0] = x * y. Compute the product mod 2^256 and mod 2^256 - 1, then use
// use the Chinese Remainder Theorem to reconstruct the 512 bit result. The result is stored in two 256
// variables such that product = prod1 * 2^256 + prod0.
uint256 prod0; // Least significant 256 bits of the product
uint256 prod1; // Most significant 256 bits of the product
assembly {
let mm := mulmod(x, y, not(0))
prod0 := mul(x, y)
prod1 := sub(sub(mm, prod0), lt(mm, prod0))
}
// Handle non-overflow cases, 256 by 256 division.
if (prod1 == 0) {
return prod0 / denominator;
}
// Make sure the result is less than 2^256. Also prevents denominator == 0.
require(denominator > prod1);
///////////////////////////////////////////////
// 512 by 256 division.
///////////////////////////////////////////////
// Make division exact by subtracting the remainder from [prod1 prod0].
uint256 remainder;
assembly {
// Compute remainder using mulmod.
remainder := mulmod(x, y, denominator)
// Subtract 256 bit number from 512 bit number.
prod1 := sub(prod1, gt(remainder, prod0))
prod0 := sub(prod0, remainder)
}
// Factor powers of two out of denominator and compute largest power of two divisor of denominator. Always >= 1.
// See https://cs.stackexchange.com/q/138556/92363.
// Does not overflow because the denominator cannot be zero at this stage in the function.
uint256 twos = denominator & (~denominator + 1);
assembly {
// Divide denominator by twos.
denominator := div(denominator, twos)
// Divide [prod1 prod0] by twos.
prod0 := div(prod0, twos)
// Flip twos such that it is 2^256 / twos. If twos is zero, then it becomes one.
twos := add(div(sub(0, twos), twos), 1)
}
// Shift in bits from prod1 into prod0.
prod0 |= prod1 * twos;
// Invert denominator mod 2^256. Now that denominator is an odd number, it has an inverse modulo 2^256 such
// that denominator * inv = 1 mod 2^256. Compute the inverse by starting with a seed that is correct for
// four bits. That is, denominator * inv = 1 mod 2^4.
uint256 inverse = (3 * denominator) ^ 2;
// Use the Newton-Raphson iteration to improve the precision. Thanks to Hensel's lifting lemma, this also works
// in modular arithmetic, doubling the correct bits in each step.
inverse *= 2 - denominator * inverse; // inverse mod 2^8
inverse *= 2 - denominator * inverse; // inverse mod 2^16
inverse *= 2 - denominator * inverse; // inverse mod 2^32
inverse *= 2 - denominator * inverse; // inverse mod 2^64
inverse *= 2 - denominator * inverse; // inverse mod 2^128
inverse *= 2 - denominator * inverse; // inverse mod 2^256
// Because the division is now exact we can divide by multiplying with the modular inverse of denominator.
// This will give us the correct result modulo 2^256. Since the preconditions guarantee that the outcome is
// less than 2^256, this is the final result. We don't need to compute the high bits of the result and prod1
// is no longer required.
result = prod0 * inverse;
return result;
}
}
/**
* @notice Calculates x * y / denominator with full precision, following the selected rounding direction.
*/
function mulDiv(
uint256 x,
uint256 y,
uint256 denominator,
Rounding rounding
) internal pure returns (uint256) {
uint256 result = mulDiv(x, y, denominator);
if (rounding == Rounding.Up && mulmod(x, y, denominator) > 0) {
result += 1;
}
return result;
}
/**
* @dev Returns the square root of a number. If the number is not a perfect square, the value is rounded down.
*
* Inspired by Henry S. Warren, Jr.'s "Hacker's Delight" (Chapter 11).
*/
function sqrt(uint256 a) internal pure returns (uint256) {
if (a == 0) {
return 0;
}
// For our first guess, we get the biggest power of 2 which is smaller than the square root of the target.
//
// We know that the "msb" (most significant bit) of our target number `a` is a power of 2 such that we have
// `msb(a) <= a < 2*msb(a)`. This value can be written `msb(a)=2**k` with `k=log2(a)`.
//
// This can be rewritten `2**log2(a) <= a < 2**(log2(a) + 1)`
// → `sqrt(2**k) <= sqrt(a) < sqrt(2**(k+1))`
// → `2**(k/2) <= sqrt(a) < 2**((k+1)/2) <= 2**(k/2 + 1)`
//
// Consequently, `2**(log2(a) / 2)` is a good first approximation of `sqrt(a)` with at least 1 correct bit.
uint256 result = 1 << (log2(a) >> 1);
// At this point `result` is an estimation with one bit of precision. We know the true value is a uint128,
// since it is the square root of a uint256. Newton's method converges quadratically (precision doubles at
// every iteration). We thus need at most 7 iteration to turn our partial result with one bit of precision
// into the expected uint128 result.
unchecked {
result = (result + a / result) >> 1;
result = (result + a / result) >> 1;
result = (result + a / result) >> 1;
result = (result + a / result) >> 1;
result = (result + a / result) >> 1;
result = (result + a / result) >> 1;
result = (result + a / result) >> 1;
return min(result, a / result);
}
}
/**
* @notice Calculates sqrt(a), following the selected rounding direction.
*/
function sqrt(uint256 a, Rounding rounding) internal pure returns (uint256) {
unchecked {
uint256 result = sqrt(a);
return result + (rounding == Rounding.Up && result * result < a ? 1 : 0);
}
}
/**
* @dev Return the log in base 2, rounded down, of a positive value.
* Returns 0 if given 0.
*/
function log2(uint256 value) internal pure returns (uint256) {
uint256 result = 0;
unchecked {
if (value >> 128 > 0) {
value >>= 128;
result += 128;
}
if (value >> 64 > 0) {
value >>= 64;
result += 64;
}
if (value >> 32 > 0) {
value >>= 32;
result += 32;
}
if (value >> 16 > 0) {
value >>= 16;
result += 16;
}
if (value >> 8 > 0) {
value >>= 8;
result += 8;
}
if (value >> 4 > 0) {
value >>= 4;
result += 4;
}
if (value >> 2 > 0) {
value >>= 2;
result += 2;
}
if (value >> 1 > 0) {
result += 1;
}
}
return result;
}
/**
* @dev Return the log in base 2, following the selected rounding direction, of a positive value.
* Returns 0 if given 0.
*/
function log2(uint256 value, Rounding rounding) internal pure returns (uint256) {
unchecked {
uint256 result = log2(value);
return result + (rounding == Rounding.Up && 1 << result < value ? 1 : 0);
}
}
/**
* @dev Return the log in base 10, rounded down, of a positive value.
* Returns 0 if given 0.
*/
function log10(uint256 value) internal pure returns (uint256) {
uint256 result = 0;
unchecked {
if (value >= 10**64) {
value /= 10**64;
result += 64;
}
if (value >= 10**32) {
value /= 10**32;
result += 32;
}
if (value >= 10**16) {
value /= 10**16;
result += 16;
}
if (value >= 10**8) {
value /= 10**8;
result += 8;
}
if (value >= 10**4) {
value /= 10**4;
result += 4;
}
if (value >= 10**2) {
value /= 10**2;
result += 2;
}
if (value >= 10**1) {
result += 1;
}
}
return result;
}
/**
* @dev Return the log in base 10, following the selected rounding direction, of a positive value.
* Returns 0 if given 0.
*/
function log10(uint256 value, Rounding rounding) internal pure returns (uint256) {
unchecked {
uint256 result = log10(value);
return result + (rounding == Rounding.Up && 10**result < value ? 1 : 0);
}
}
/**
* @dev Return the log in base 256, rounded down, of a positive value.
* Returns 0 if given 0.
*
* Adding one to the result gives the number of pairs of hex symbols needed to represent `value` as a hex string.
*/
function log256(uint256 value) internal pure returns (uint256) {
uint256 result = 0;
unchecked {
if (value >> 128 > 0) {
value >>= 128;
result += 16;
}
if (value >> 64 > 0) {
value >>= 64;
result += 8;
}
if (value >> 32 > 0) {
value >>= 32;
result += 4;
}
if (value >> 16 > 0) {
value >>= 16;
result += 2;
}
if (value >> 8 > 0) {
result += 1;
}
}
return result;
}
/**
* @dev Return the log in base 10, following the selected rounding direction, of a positive value.
* Returns 0 if given 0.
*/
function log256(uint256 value, Rounding rounding) internal pure returns (uint256) {
unchecked {
uint256 result = log256(value);
return result + (rounding == Rounding.Up && 1 << (result * 8) < value ? 1 : 0);
}
}
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.8.0) (utils/Address.sol)
pragma solidity ^0.8.1;
/**
* @dev Collection of functions related to the address type
*/
library AddressUpgradeable {
/**
* @dev Returns true if `account` is a contract.
*
* [IMPORTANT]
* ====
* It is unsafe to assume that an address for which this function returns
* false is an externally-owned account (EOA) and not a contract.
*
* Among others, `isContract` will return false for the following
* types of addresses:
*
* - an externally-owned account
* - a contract in construction
* - an address where a contract will be created
* - an address where a contract lived, but was destroyed
* ====
*
* [IMPORTANT]
* ====
* You shouldn't rely on `isContract` to protect against flash loan attacks!
*
* Preventing calls from contracts is highly discouraged. It breaks composability, breaks support for smart wallets
* like Gnosis Safe, and does not provide security since it can be circumvented by calling from a contract
* constructor.
* ====
*/
function isContract(address account) internal view returns (bool) {
// This method relies on extcodesize/address.code.length, which returns 0
// for contracts in construction, since the code is only stored at the end
// of the constructor execution.
return account.code.length > 0;
}
/**
* @dev Replacement for Solidity's `transfer`: sends `amount` wei to
* `recipient`, forwarding all available gas and reverting on errors.
*
* https://eips.ethereum.org/EIPS/eip-1884[EIP1884] increases the gas cost
* of certain opcodes, possibly making contracts go over the 2300 gas limit
* imposed by `transfer`, making them unable to receive funds via
* `transfer`. {sendValue} removes this limitation.
*
* https://diligence.consensys.net/posts/2019/09/stop-using-soliditys-transfer-now/[Learn more].
*
* IMPORTANT: because control is transferred to `recipient`, care must be
* taken to not create reentrancy vulnerabilities. Consider using
* {ReentrancyGuard} or the
* https://solidity.readthedocs.io/en/v0.5.11/security-considerations.html#use-the-checks-effects-interactions-pattern[checks-effects-interactions pattern].
*/
function sendValue(address payable recipient, uint256 amount) internal {
require(address(this).balance >= amount, "Address: insufficient balance");
(bool success, ) = recipient.call{value: amount}("");
require(success, "Address: unable to send value, recipient may have reverted");
}
/**
* @dev Performs a Solidity function call using a low level `call`. A
* plain `call` is an unsafe replacement for a function call: use this
* function instead.
*
* If `target` reverts with a revert reason, it is bubbled up by this
* function (like regular Solidity function calls).
*
* Returns the raw returned data. To convert to the expected return value,
* use https://solidity.readthedocs.io/en/latest/units-and-global-variables.html?highlight=abi.decode#abi-encoding-and-decoding-functions[`abi.decode`].
*
* Requirements:
*
* - `target` must be a contract.
* - calling `target` with `data` must not revert.
*
* _Available since v3.1._
*/
function functionCall(address target, bytes memory data) internal returns (bytes memory) {
return functionCallWithValue(target, data, 0, "Address: low-level call failed");
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`], but with
* `errorMessage` as a fallback revert reason when `target` reverts.
*
* _Available since v3.1._
*/
function functionCall(
address target,
bytes memory data,
string memory errorMessage
) internal returns (bytes memory) {
return functionCallWithValue(target, data, 0, errorMessage);
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
* but also transferring `value` wei to `target`.
*
* Requirements:
*
* - the calling contract must have an ETH balance of at least `value`.
* - the called Solidity function must be `payable`.
*
* _Available since v3.1._
*/
function functionCallWithValue(
address target,
bytes memory data,
uint256 value
) internal returns (bytes memory) {
return functionCallWithValue(target, data, value, "Address: low-level call with value failed");
}
/**
* @dev Same as {xref-Address-functionCallWithValue-address-bytes-uint256-}[`functionCallWithValue`], but
* with `errorMessage` as a fallback revert reason when `target` reverts.
*
* _Available since v3.1._
*/
function functionCallWithValue(
address target,
bytes memory data,
uint256 value,
string memory errorMessage
) internal returns (bytes memory) {
require(address(this).balance >= value, "Address: insufficient balance for call");
(bool success, bytes memory returndata) = target.call{value: value}(data);
return verifyCallResultFromTarget(target, success, returndata, errorMessage);
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
* but performing a static call.
*
* _Available since v3.3._
*/
function functionStaticCall(address target, bytes memory data) internal view returns (bytes memory) {
return functionStaticCall(target, data, "Address: low-level static call failed");
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],
* but performing a static call.
*
* _Available since v3.3._
*/
function functionStaticCall(
address target,
bytes memory data,
string memory errorMessage
) internal view returns (bytes memory) {
(bool success, bytes memory returndata) = target.staticcall(data);
return verifyCallResultFromTarget(target, success, returndata, errorMessage);
}
/**
* @dev Tool to verify that a low level call to smart-contract was successful, and revert (either by bubbling
* the revert reason or using the provided one) in case of unsuccessful call or if target was not a contract.
*
* _Available since v4.8._
*/
function verifyCallResultFromTarget(
address target,
bool success,
bytes memory returndata,
string memory errorMessage
) internal view returns (bytes memory) {
if (success) {
if (returndata.length == 0) {
// only check isContract if the call was successful and the return data is empty
// otherwise we already know that it was a contract
require(isContract(target), "Address: call to non-contract");
}
return returndata;
} else {
_revert(returndata, errorMessage);
}
}
/**
* @dev Tool to verify that a low level call was successful, and revert if it wasn't, either by bubbling the
* revert reason or using the provided one.
*
* _Available since v4.3._
*/
function verifyCallResult(
bool success,
bytes memory returndata,
string memory errorMessage
) internal pure returns (bytes memory) {
if (success) {
return returndata;
} else {
_revert(returndata, errorMessage);
}
}
function _revert(bytes memory returndata, string memory errorMessage) private pure {
// Look for revert reason and bubble it up if present
if (returndata.length > 0) {
// The easiest way to bubble the revert reason is using memory via assembly
/// @solidity memory-safe-assembly
assembly {
let returndata_size := mload(returndata)
revert(add(32, returndata), returndata_size)
}
} else {
revert(errorMessage);
}
}
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.7.0) (access/Ownable.sol)
pragma solidity ^0.8.0;
import "../utils/Context.sol";
/**
* @dev Contract module which provides a basic access control mechanism, where
* there is an account (an owner) that can be granted exclusive access to
* specific functions.
*
* By default, the owner account will be the one that deploys the contract. This
* can later be changed with {transferOwnership}.
*
* This module is used through inheritance. It will make available the modifier
* `onlyOwner`, which can be applied to your functions to restrict their use to
* the owner.
*/
abstract contract Ownable is Context {
address private _owner;
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
/**
* @dev Initializes the contract setting the deployer as the initial owner.
*/
constructor() {
_transferOwnership(_msgSender());
}
/**
* @dev Throws if called by any account other than the owner.
*/
modifier onlyOwner() {
_checkOwner();
_;
}
/**
* @dev Returns the address of the current owner.
*/
function owner() public view virtual returns (address) {
return _owner;
}
/**
* @dev Throws if the sender is not the owner.
*/
function _checkOwner() internal view virtual {
require(owner() == _msgSender(), "Ownable: caller is not the owner");
}
/**
* @dev Leaves the contract without owner. It will not be possible to call
* `onlyOwner` functions anymore. Can only be called by the current owner.
*
* NOTE: Renouncing ownership will leave the contract without an owner,
* thereby removing any functionality that is only available to the owner.
*/
function renounceOwnership() public virtual onlyOwner {
_transferOwnership(address(0));
}
/**
* @dev Transfers ownership of the contract to a new account (`newOwner`).
* Can only be called by the current owner.
*/
function transferOwnership(address newOwner) public virtual onlyOwner {
require(newOwner != address(0), "Ownable: new owner is the zero address");
_transferOwnership(newOwner);
}
/**
* @dev Transfers ownership of the contract to a new account (`newOwner`).
* Internal function without access restriction.
*/
function _transferOwnership(address newOwner) internal virtual {
address oldOwner = _owner;
_owner = newOwner;
emit OwnershipTransferred(oldOwner, newOwner);
}
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (utils/introspection/IERC165.sol)
pragma solidity ^0.8.0;
/**
* @dev Interface of the ERC165 standard, as defined in the
* https://eips.ethereum.org/EIPS/eip-165[EIP].
*
* Implementers can declare support of contract interfaces, which can then be
* queried by others ({ERC165Checker}).
*
* For an implementation, see {ERC165}.
*/
interface IERC165Upgradeable {
/**
* @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[EIP 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);
}