Contract Name:
SoulboundIdentity
Contract Source Code:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
import "./libraries/Errors.sol";
import "./interfaces/ISoulboundIdentity.sol";
import "./interfaces/ISoulName.sol";
import "./tokens/MasaSBTAuthority.sol";
/// @title Soulbound Identity
/// @author Masa Finance
/// @notice Soulbound token that represents an identity.
/// @dev Soulbound identity, that inherits from the SBT contract.
contract SoulboundIdentity is
MasaSBTAuthority,
ISoulboundIdentity,
ReentrancyGuard
{
/* ========== STATE VARIABLES =========================================== */
ISoulName public soulName;
/* ========== INITIALIZE ================================================ */
/// @notice Creates a new soulbound identity
/// @dev Creates a new soulbound identity, inheriting from the SBT contract.
/// @param admin Administrator of the smart contract
/// @param baseTokenURI Base URI of the token
constructor(address admin, string memory baseTokenURI)
MasaSBTAuthority(admin, "Masa Identity", "MID", baseTokenURI)
{}
/* ========== RESTRICTED FUNCTIONS ====================================== */
/// @notice Sets the SoulName contract address linked to this identity
/// @dev The caller must have the admin role to call this function
/// @param _soulName Address of the SoulName contract
function setSoulName(ISoulName _soulName)
external
onlyRole(DEFAULT_ADMIN_ROLE)
{
if (address(_soulName) == address(0)) revert ZeroAddress();
if (soulName == _soulName) revert SameValue();
soulName = _soulName;
}
/* ========== MUTATIVE FUNCTIONS ======================================== */
/// @notice Mints a new soulbound identity
/// @dev The caller can only mint one identity per address
/// @param to Address of the admin of the new identity
function mint(address to) public override returns (uint256) {
// Soulbound identity already created!
if (balanceOf(to) > 0) revert IdentityAlreadyCreated(to);
return _mintWithCounter(to);
}
/// @notice Mints a new soulbound identity with a SoulName associated to it
/// @dev The caller can only mint one identity per address, and the name must be unique
/// @param to Address of the admin of the new identity
/// @param name Name of the new identity
/// @param yearsPeriod Years of validity of the name
/// @param _tokenURI URI of the NFT
function mintIdentityWithName(
address to,
string memory name,
uint256 yearsPeriod,
string memory _tokenURI
) external override soulNameAlreadySet nonReentrant returns (uint256) {
uint256 identityId = mint(to);
soulName.mint(to, name, yearsPeriod, _tokenURI);
return identityId;
}
/* ========== VIEWS ===================================================== */
/// @notice Returns the address of the SoulName contract linked to this identity
/// @dev This function returns the address of the SoulName contract linked to this identity
/// @return Address of the SoulName contract
function getSoulName() external view override returns (ISoulName) {
return soulName;
}
/// @notice Returns the extension of the soul name
/// @dev This function returns the extension of the soul name
/// @return Extension of the soul name
function getExtension() external view returns (string memory) {
return soulName.getExtension();
}
/// @notice Returns the owner address of an identity
/// @dev This function returns the owner address of the identity specified by the tokenId
/// @param tokenId TokenId of the identity
/// @return Address of the owner of the identity
function ownerOf(uint256 tokenId)
public
view
override(SBT, ISBT)
returns (address)
{
return super.ownerOf(tokenId);
}
/// @notice Returns the owner address of a soul name
/// @dev This function returns the owner address of the soul name identity specified by the name
/// @param name Name of the soul name
/// @return Address of the owner of the identity
function ownerOf(string memory name)
external
view
soulNameAlreadySet
returns (address)
{
(, , uint256 identityId, , , ) = soulName.getTokenData(name);
return super.ownerOf(identityId);
}
/// @notice Returns the URI of a soul name
/// @dev This function returns the token URI of the soul name identity specified by the name
/// @param name Name of the soul name
/// @return URI of the identity associated to a soul name
function tokenURI(string memory name)
external
view
soulNameAlreadySet
returns (string memory)
{
(, , uint256 identityId, , , ) = soulName.getTokenData(name);
return super.tokenURI(identityId);
}
/// @notice Returns the URI of the owner of an identity
/// @dev This function returns the token URI of the identity owned by an account
/// @param owner Address of the owner of the identity
/// @return URI of the identity owned by the account
function tokenURI(address owner) external view returns (string memory) {
uint256 tokenId = tokenOfOwner(owner);
return super.tokenURI(tokenId);
}
/// @notice Returns the identity id of an account
/// @dev This function returns the tokenId of the identity owned by an account
/// @param owner Address of the owner of the identity
/// @return TokenId of the identity owned by the account
function tokenOfOwner(address owner)
public
view
override
returns (uint256)
{
return super.tokenOfOwnerByIndex(owner, 0);
}
/// @notice Checks if a soul name is available
/// @dev This function queries if a soul name already exists and is in the available state
/// @param name Name of the soul name
/// @return available `true` if the soul name is available, `false` otherwise
function isAvailable(string memory name)
external
view
soulNameAlreadySet
returns (bool available)
{
return soulName.isAvailable(name);
}
/// @notice Returns the information of a soul name
/// @dev This function queries the information of a soul name
/// @param name Name of the soul name
/// @return sbtName Soul name, in upper/lower case and extension
/// @return linked `true` if the soul name is linked, `false` otherwise
/// @return identityId Identity id of the soul name
/// @return tokenId SoulName id of the soul name
/// @return expirationDate Expiration date of the soul name
/// @return active `true` if the soul name is active, `false` otherwise
function getTokenData(string memory name)
external
view
soulNameAlreadySet
returns (
string memory sbtName,
bool linked,
uint256 identityId,
uint256 tokenId,
uint256 expirationDate,
bool active
)
{
return soulName.getTokenData(name);
}
/// @notice Returns all the active soul names of an account
/// @dev This function queries all the identity names of the specified account
/// @param owner Address of the owner of the identities
/// @return sbtNames Array of soul names associated to the account
function getSoulNames(address owner)
external
view
soulNameAlreadySet
returns (string[] memory sbtNames)
{
return soulName.getSoulNames(owner);
}
// SoulName -> SoulboundIdentity.tokenId
// SoulName -> account -> SoulboundIdentity.tokenId
/// @notice Returns all the active soul names of an account
/// @dev This function queries all the identity names of the specified identity Id
/// @param tokenId TokenId of the identity
/// @return sbtNames Array of soul names associated to the identity Id
function getSoulNames(uint256 tokenId)
external
view
soulNameAlreadySet
returns (string[] memory sbtNames)
{
return soulName.getSoulNames(tokenId);
}
/* ========== PRIVATE FUNCTIONS ========================================= */
/* ========== MODIFIERS ================================================= */
modifier soulNameAlreadySet() {
if (address(soulName) == address(0)) revert SoulNameContractNotSet();
_;
}
/* ========== EVENTS ==================================================== */
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (security/ReentrancyGuard.sol)
pragma solidity ^0.8.0;
/**
* @dev Contract module that helps prevent reentrant calls to a function.
*
* Inheriting from `ReentrancyGuard` will make the {nonReentrant} modifier
* available, which can be applied to functions to make sure there are no nested
* (reentrant) calls to them.
*
* Note that because there is a single `nonReentrant` guard, functions marked as
* `nonReentrant` may not call one another. This can be worked around by making
* those functions `private`, and then adding `external` `nonReentrant` entry
* points to them.
*
* TIP: If you would like to learn more about reentrancy and alternative ways
* to protect against it, check out our blog post
* https://blog.openzeppelin.com/reentrancy-after-istanbul/[Reentrancy After Istanbul].
*/
abstract contract ReentrancyGuard {
// Booleans are more expensive than uint256 or any type that takes up a full
// word because each write operation emits an extra SLOAD to first read the
// slot's contents, replace the bits taken up by the boolean, and then write
// back. This is the compiler's defense against contract upgrades and
// pointer aliasing, and it cannot be disabled.
// The values being non-zero value makes deployment a bit more expensive,
// but in exchange the refund on every call to nonReentrant will be lower in
// amount. Since refunds are capped to a percentage of the total
// transaction's gas, it is best to keep them low in cases like this one, to
// increase the likelihood of the full refund coming into effect.
uint256 private constant _NOT_ENTERED = 1;
uint256 private constant _ENTERED = 2;
uint256 private _status;
constructor() {
_status = _NOT_ENTERED;
}
/**
* @dev Prevents a contract from calling itself, directly or indirectly.
* Calling a `nonReentrant` function from another `nonReentrant`
* function is not supported. It is possible to prevent this from happening
* by making the `nonReentrant` function external, and making it call a
* `private` function that does the actual work.
*/
modifier nonReentrant() {
// On the first call to nonReentrant, _notEntered will be true
require(_status != _ENTERED, "ReentrancyGuard: reentrant call");
// Any calls to nonReentrant after this point will fail
_status = _ENTERED;
_;
// By storing the original value once again, a refund is triggered (see
// https://eips.ethereum.org/EIPS/eip-2200)
_status = _NOT_ENTERED;
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
error AddressDoesNotHaveIdentity(address to);
error AlreadyAdded();
error AuthorityNotExists(address authority);
error CallerNotOwner(address caller);
error CallerNotReader(address caller);
error CreditScoreAlreadyCreated(address to);
error IdentityAlreadyCreated(address to);
error IdentityOwnerIsReader(uint256 readerIdentityId);
error InsufficientEthAmount(uint256 amount);
error IdentityOwnerNotTokenOwner(uint256 tokenId, uint256 ownerIdentityId);
error InvalidPaymentMethod(address paymentMethod);
error InvalidSignature();
error InvalidSignatureDate(uint256 signatureDate);
error InvalidToken(address token);
error InvalidTokenURI(string tokenURI);
error LinkAlreadyExists(
address token,
uint256 tokenId,
uint256 readerIdentityId,
uint256 signatureDate
);
error LinkAlreadyRevoked();
error LinkDoesNotExist();
error NameAlreadyExists(string name);
error NameNotFound(string name);
error NameRegisteredByOtherAccount(string name, uint256 tokenId);
error NotAuthorized(address signer);
error NonExistingErc20Token(address erc20token);
error RefundFailed();
error SameValue();
error SBTAlreadyLinked(address token);
error SoulNameContractNotSet();
error TokenNotFound(uint256 tokenId);
error TransferFailed();
error URIAlreadyExists(string tokenURI);
error ValidPeriodExpired(uint256 expirationDate);
error ZeroAddress();
error ZeroLengthName(string name);
error ZeroYearsPeriod(uint256 yearsPeriod);
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
import "../tokens/SBT/ISBT.sol";
import "./ISoulName.sol";
interface ISoulboundIdentity is ISBT {
function mint(address to) external returns (uint256);
function mintIdentityWithName(
address to,
string memory name,
uint256 yearsPeriod,
string memory _tokenURI
) external returns (uint256);
function getSoulName() external view returns (ISoulName);
function tokenOfOwner(address owner) external view returns (uint256);
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
interface ISoulName {
function mint(
address to,
string memory name,
uint256 yearsPeriod,
string memory _tokenURI
) external returns (uint256);
function getExtension() external view returns (string memory);
function isAvailable(string memory name)
external
view
returns (bool available);
function getTokenData(string memory name)
external
view
returns (
string memory sbtName,
bool linked,
uint256 identityId,
uint256 tokenId,
uint256 expirationDate,
bool active
);
function getTokenId(string memory name) external view returns (uint256);
function getSoulNames(address owner)
external
view
returns (string[] memory sbtNames);
function getSoulNames(uint256 identityId)
external
view
returns (string[] memory sbtNames);
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
import "@openzeppelin/contracts/utils/Counters.sol";
import "./MasaSBT.sol";
/// @title MasaSBT
/// @author Masa Finance
/// @notice Soulbound token. Non-fungible token that is not transferable.
/// @dev Implementation of https://papers.ssrn.com/sol3/papers.cfm?abstract_id=4105763 Soulbound token.
abstract contract MasaSBTAuthority is MasaSBT {
/* ========== STATE VARIABLES =========================================== */
using Counters for Counters.Counter;
bytes32 public constant MINTER_ROLE = keccak256("MINTER_ROLE");
Counters.Counter private _tokenIdCounter;
/* ========== INITIALIZE ================================================ */
/// @notice Creates a new soulbound token
/// @dev Creates a new soulbound token
/// @param admin Administrator of the smart contract
/// @param name Name of the token
/// @param symbol Symbol of the token
/// @param baseTokenURI Base URI of the token
constructor(
address admin,
string memory name,
string memory symbol,
string memory baseTokenURI
) MasaSBT(admin, name, symbol, baseTokenURI) {
_grantRole(MINTER_ROLE, admin);
}
/* ========== RESTRICTED FUNCTIONS ====================================== */
function _mintWithCounter(address to)
internal
virtual
onlyRole(MINTER_ROLE)
returns (uint256)
{
uint256 tokenId = _tokenIdCounter.current();
_tokenIdCounter.increment();
_mint(to, tokenId);
return tokenId;
}
/* ========== MUTATIVE FUNCTIONS ======================================== */
/* ========== VIEWS ===================================================== */
/* ========== PRIVATE FUNCTIONS ========================================= */
/* ========== MODIFIERS ================================================= */
/* ========== EVENTS ==================================================== */
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
import "@openzeppelin/contracts/utils/introspection/IERC165.sol";
interface ISBT is IERC165 {
/// @dev This emits when an SBT is newly minted.
/// This event emits when SBTs are created
event Mint(address indexed _owner, uint256 indexed _tokenId);
/// @dev This emits when an SBT is burned
/// This event emits when SBTs are destroyed
event Burn(address indexed _owner, uint256 indexed _tokenId);
/// @notice Count all SBTs assigned to an owner
/// @dev SBTs assigned to the zero address are considered invalid, and this
/// function throws for queries about the zero address.
/// @param _owner An address for whom to query the balance
/// @return The number of SBTs owned by `_owner`, possibly zero
function balanceOf(address _owner) external view returns (uint256);
/// @notice Find the owner of an SBT
/// @dev SBTs assigned to zero address are considered invalid, and queries
/// about them do throw.
/// @param _tokenId The identifier for an SBT
/// @return The address of the owner of the SBT
function ownerOf(uint256 _tokenId) external view returns (address);
}
// 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/Counters.sol)
pragma solidity ^0.8.0;
/**
* @title Counters
* @author Matt Condon (@shrugs)
* @dev Provides counters that can only be incremented, decremented or reset. This can be used e.g. to track the number
* of elements in a mapping, issuing ERC721 ids, or counting request ids.
*
* Include with `using Counters for Counters.Counter;`
*/
library Counters {
struct Counter {
// This variable should never be directly accessed by users of the library: interactions must be restricted to
// the library's function. As of Solidity v0.5.2, this cannot be enforced, though there is a proposal to add
// this feature: see https://github.com/ethereum/solidity/issues/4637
uint256 _value; // default: 0
}
function current(Counter storage counter) internal view returns (uint256) {
return counter._value;
}
function increment(Counter storage counter) internal {
unchecked {
counter._value += 1;
}
}
function decrement(Counter storage counter) internal {
uint256 value = counter._value;
require(value > 0, "Counter: decrement overflow");
unchecked {
counter._value = value - 1;
}
}
function reset(Counter storage counter) internal {
counter._value = 0;
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
import "@openzeppelin/contracts/access/AccessControl.sol";
import "@openzeppelin/contracts/utils/Strings.sol";
import "../libraries/Errors.sol";
import "../interfaces/ILinkableSBT.sol";
import "./SBT/SBT.sol";
import "./SBT/extensions/SBTEnumerable.sol";
import "./SBT/extensions/SBTBurnable.sol";
/// @title MasaSBT
/// @author Masa Finance
/// @notice Soulbound token. Non-fungible token that is not transferable.
/// @dev Implementation of https://papers.ssrn.com/sol3/papers.cfm?abstract_id=4105763 Soulbound token.
abstract contract MasaSBT is
SBT,
SBTEnumerable,
AccessControl,
SBTBurnable,
ILinkableSBT
{
/* ========== STATE VARIABLES =========================================== */
using Strings for uint256;
string private _baseTokenURI;
uint256 public override addLinkPrice; // price in stable coin
uint256 public override addLinkPriceMASA; // price in MASA
uint256 public override queryLinkPrice; // price in stable coin
uint256 public override queryLinkPriceMASA; // price in MASA
/* ========== INITIALIZE ================================================ */
/// @notice Creates a new soulbound token
/// @dev Creates a new soulbound token
/// @param admin Administrator of the smart contract
/// @param name Name of the token
/// @param symbol Symbol of the token
/// @param baseTokenURI Base URI of the token
constructor(
address admin,
string memory name,
string memory symbol,
string memory baseTokenURI
) SBT(name, symbol) {
_grantRole(DEFAULT_ADMIN_ROLE, admin);
_baseTokenURI = baseTokenURI;
}
/* ========== RESTRICTED FUNCTIONS ====================================== */
/// @notice Sets the price for adding the link in SoulLinker in stable coin
/// @dev The caller must have the admin role to call this function
/// @param _addLinkPrice New price for adding the link in SoulLinker in stable coin
function setAddLinkPrice(uint256 _addLinkPrice)
external
onlyRole(DEFAULT_ADMIN_ROLE)
{
if (addLinkPrice == _addLinkPrice) revert SameValue();
addLinkPrice = _addLinkPrice;
}
/// @notice Sets the price for adding the link in SoulLinker in MASA
/// @dev The caller must have the admin role to call this function
/// @param _addLinkPriceMASA New price for adding the link in SoulLinker in MASA
function setAddLinkPriceMASA(uint256 _addLinkPriceMASA)
external
onlyRole(DEFAULT_ADMIN_ROLE)
{
if (addLinkPriceMASA == _addLinkPriceMASA) revert SameValue();
addLinkPriceMASA = _addLinkPriceMASA;
}
/// @notice Sets the price for reading data in SoulLinker in stable coin
/// @dev The caller must have the admin role to call this function
/// @param _queryLinkPrice New price for reading data in SoulLinker in stable coin
function setQueryLinkPrice(uint256 _queryLinkPrice)
external
onlyRole(DEFAULT_ADMIN_ROLE)
{
if (queryLinkPrice == _queryLinkPrice) revert SameValue();
queryLinkPrice = _queryLinkPrice;
}
/// @notice Sets the price for reading data in SoulLinker in MASA
/// @dev The caller must have the admin role to call this function
/// @param _queryLinkPriceMASA New price for reading data in SoulLinker in MASA
function setQueryLinkPriceMASA(uint256 _queryLinkPriceMASA)
external
onlyRole(DEFAULT_ADMIN_ROLE)
{
if (queryLinkPriceMASA == _queryLinkPriceMASA) revert SameValue();
queryLinkPriceMASA = _queryLinkPriceMASA;
}
/* ========== MUTATIVE FUNCTIONS ======================================== */
/* ========== VIEWS ===================================================== */
/// @notice Returns true if the token exists
/// @dev Returns true if the token has been minted
/// @param tokenId Token to check
/// @return True if the token exists
function exists(uint256 tokenId) external view returns (bool) {
return _exists(tokenId);
}
/// @notice A distinct Uniform Resource Identifier (URI) for a given asset.
/// @dev Throws if `_tokenId` is not a valid SBT. URIs are defined in RFC
/// 3986. The URI may point to a JSON file that conforms to the "ERC721
/// Metadata JSON Schema".
/// @param tokenId SBT to get the URI of
/// @return URI of the SBT
function tokenURI(uint256 tokenId)
public
view
virtual
override
returns (string memory)
{
_requireMinted(tokenId);
string memory baseURI = _baseURI();
return
bytes(baseURI).length > 0
? string(abi.encodePacked(baseURI, tokenId.toString(), ".json"))
: "";
}
/// @notice Query if a contract implements an interface
/// @dev Interface identification is specified in ERC-165.
/// @param interfaceId The interface identifier, as specified in ERC-165
/// @return `true` if the contract implements `interfaceId` and
/// `interfaceId` is not 0xffffffff, `false` otherwise
function supportsInterface(bytes4 interfaceId)
public
view
virtual
override(SBT, SBTEnumerable, AccessControl, IERC165)
returns (bool)
{
return super.supportsInterface(interfaceId);
}
/* ========== PRIVATE FUNCTIONS ========================================= */
function _baseURI() internal view virtual override returns (string memory) {
return _baseTokenURI;
}
function _beforeTokenTransfer(
address from,
address to,
uint256 tokenId
) internal virtual override(SBT, SBTEnumerable) {
super._beforeTokenTransfer(from, to, tokenId);
}
/* ========== MODIFIERS ================================================= */
/* ========== EVENTS ==================================================== */
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.7.0) (access/AccessControl.sol)
pragma solidity ^0.8.0;
import "./IAccessControl.sol";
import "../utils/Context.sol";
import "../utils/Strings.sol";
import "../utils/introspection/ERC165.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 AccessControl is Context, IAccessControl, ERC165 {
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(IAccessControl).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 ",
Strings.toHexString(uint160(account), 20),
" is missing role ",
Strings.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());
}
}
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.7.0) (utils/Strings.sol)
pragma solidity ^0.8.0;
/**
* @dev String operations.
*/
library Strings {
bytes16 private constant _HEX_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) {
// Inspired by OraclizeAPI's implementation - MIT licence
// https://github.com/oraclize/ethereum-api/blob/b42146b063c7d6ee1358846c198246239e9360e8/oraclizeAPI_0.4.25.sol
if (value == 0) {
return "0";
}
uint256 temp = value;
uint256 digits;
while (temp != 0) {
digits++;
temp /= 10;
}
bytes memory buffer = new bytes(digits);
while (value != 0) {
digits -= 1;
buffer[digits] = bytes1(uint8(48 + uint256(value % 10)));
value /= 10;
}
return string(buffer);
}
/**
* @dev Converts a `uint256` to its ASCII `string` hexadecimal representation.
*/
function toHexString(uint256 value) internal pure returns (string memory) {
if (value == 0) {
return "0x00";
}
uint256 temp = value;
uint256 length = 0;
while (temp != 0) {
length++;
temp >>= 8;
}
return toHexString(value, length);
}
/**
* @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] = _HEX_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
pragma solidity ^0.8.7;
import "../tokens/SBT/ISBT.sol";
interface ILinkableSBT is ISBT {
function addLinkPrice() external view returns (uint256);
function addLinkPriceMASA() external view returns (uint256);
function queryLinkPrice() external view returns (uint256);
function queryLinkPriceMASA() external view returns (uint256);
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
import "@openzeppelin/contracts/utils/introspection/ERC165.sol";
import "@openzeppelin/contracts/utils/Context.sol";
import "@openzeppelin/contracts/utils/Strings.sol";
import "./ISBT.sol";
import "./extensions/ISBTMetadata.sol";
/// @title SBT
/// @author Masa Finance
/// @notice Soulbound token is an NFT token that is not transferable.
contract SBT is Context, ERC165, ISBT, ISBTMetadata {
using Strings for uint256;
// Token name
string private _name;
// Token symbol
string private _symbol;
// Mapping from token ID to owner address
mapping(uint256 => address) private _owners;
// Mapping owner address to token count
mapping(address => uint256) private _balances;
/**
* @dev Initializes the contract by setting a `name` and a `symbol` to the token collection.
*/
constructor(string memory name_, string memory symbol_) {
_name = name_;
_symbol = symbol_;
}
/**
* @dev See {IERC165-supportsInterface}.
*/
function supportsInterface(bytes4 interfaceId)
public
view
virtual
override(ERC165, IERC165)
returns (bool)
{
return
interfaceId == type(ISBT).interfaceId ||
interfaceId == type(ISBTMetadata).interfaceId ||
super.supportsInterface(interfaceId);
}
/**
* @dev See {ISBT-balanceOf}.
*/
function balanceOf(address owner)
public
view
virtual
override
returns (uint256)
{
require(owner != address(0), "SBT: address zero is not a valid owner");
return _balances[owner];
}
/**
* @dev See {ISBT-ownerOf}.
*/
function ownerOf(uint256 tokenId)
public
view
virtual
override
returns (address)
{
address owner = _owners[tokenId];
require(owner != address(0), "SBT: invalid token ID");
return owner;
}
/**
* @dev See {ISBTMetadata-name}.
*/
function name() public view virtual override returns (string memory) {
return _name;
}
/**
* @dev See {ISBTMetadata-symbol}.
*/
function symbol() public view virtual override returns (string memory) {
return _symbol;
}
/**
* @dev See {ISBTMetadata-tokenURI}.
*/
function tokenURI(uint256 tokenId)
public
view
virtual
override
returns (string memory)
{
_requireMinted(tokenId);
string memory baseURI = _baseURI();
return
bytes(baseURI).length > 0
? string(abi.encodePacked(baseURI, tokenId.toString()))
: "";
}
/**
* @dev Base URI for computing {tokenURI}. If set, the resulting URI for each
* token will be the concatenation of the `baseURI` and the `tokenId`. Empty
* by default, can be overridden in child contracts.
*/
function _baseURI() internal view virtual returns (string memory) {
return "";
}
/**
* @dev Returns whether `spender` is allowed to manage `tokenId`.
*
* Requirements:
*
* - `tokenId` must exist.
*/
function _isOwner(address spender, uint256 tokenId)
internal
view
virtual
returns (bool)
{
address owner = SBT.ownerOf(tokenId);
return (spender == owner);
}
/**
* @dev Returns whether `tokenId` exists.
*
* Tokens can be managed by their owner.
*
* Tokens start existing when they are minted (`_mint`),
* and stop existing when they are burned (`_burn`).
*/
function _exists(uint256 tokenId) internal view virtual returns (bool) {
return _owners[tokenId] != address(0);
}
/**
* @dev Mints `tokenId` and transfers it to `to`.
*
* WARNING: Usage of this method is discouraged, use {_safeMint} whenever possible
*
* Requirements:
*
* - `tokenId` must not exist.
* - `to` cannot be the zero address.
*
* Emits a {Mint} event.
*/
function _mint(address to, uint256 tokenId) internal virtual {
require(to != address(0), "SBT: mint to the zero address");
require(!_exists(tokenId), "SBT: token already minted");
_beforeTokenTransfer(address(0), to, tokenId);
_balances[to] += 1;
_owners[tokenId] = to;
emit Mint(to, tokenId);
_afterTokenTransfer(address(0), to, tokenId);
}
/**
* @dev Destroys `tokenId`.
*
* Requirements:
* - `tokenId` must exist.
*
* Emits a {Burn} event.
*/
function _burn(uint256 tokenId) internal virtual {
address owner = SBT.ownerOf(tokenId);
_beforeTokenTransfer(owner, address(0), tokenId);
_balances[owner] -= 1;
delete _owners[tokenId];
emit Burn(owner, tokenId);
_afterTokenTransfer(owner, address(0), tokenId);
}
/**
* @dev Reverts if the `tokenId` has not been minted yet.
*/
function _requireMinted(uint256 tokenId) internal view virtual {
require(_exists(tokenId), "SBT: invalid token ID");
}
/**
* @dev Hook that is called before any token minting/burning
*
* Calling conditions:
*
* - When `from` and `to` are both non-zero, ``from``'s `tokenId` will be
* transferred to `to`.
* - When `from` is zero, `tokenId` will be minted for `to`.
* - When `to` is zero, ``from``'s `tokenId` 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,
address,
uint256
) internal virtual {}
/**
* @dev Hook that is called after any minting/burning of tokens
*
* Calling conditions:
* - when `from` and `to` are both non-zero.
* - `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,
address,
uint256
) internal virtual {}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
import "../SBT.sol";
import "./ISBTEnumerable.sol";
/**
* @dev This implements an optional extension of {SBT} defined in the EIP that adds
* enumerability of all the token ids in the contract as well as all token ids owned by each
* account.
*/
abstract contract SBTEnumerable is SBT, ISBTEnumerable {
// Mapping from owner to list of owned token IDs
mapping(address => mapping(uint256 => uint256)) private _ownedTokens;
// Mapping from token ID to index of the owner tokens list
mapping(uint256 => uint256) private _ownedTokensIndex;
// Array with all token ids, used for enumeration
uint256[] private _allTokens;
// Mapping from token id to position in the allTokens array
mapping(uint256 => uint256) private _allTokensIndex;
/**
* @dev See {IERC165-supportsInterface}.
*/
function supportsInterface(bytes4 interfaceId)
public
view
virtual
override(IERC165, SBT)
returns (bool)
{
return
interfaceId == type(ISBTEnumerable).interfaceId ||
super.supportsInterface(interfaceId);
}
/**
* @dev See {ISBTEnumerable-tokenOfOwnerByIndex}.
*/
function tokenOfOwnerByIndex(address owner, uint256 index)
public
view
virtual
override
returns (uint256)
{
require(
index < SBT.balanceOf(owner),
"SBTEnumerable: owner index out of bounds"
);
return _ownedTokens[owner][index];
}
/**
* @dev See {ISBTEnumerable-totalSupply}.
*/
function totalSupply() public view virtual override returns (uint256) {
return _allTokens.length;
}
/**
* @dev See {ISBTEnumerable-tokenByIndex}.
*/
function tokenByIndex(uint256 index)
public
view
virtual
override
returns (uint256)
{
require(
index < SBTEnumerable.totalSupply(),
"SBTEnumerable: global index out of bounds"
);
return _allTokens[index];
}
/**
* @dev Hook that is called before any token transfer. This includes minting
* and burning.
*
* Calling conditions:
*
* - When `from` and `to` are both non-zero, ``from``'s `tokenId` will be
* transferred to `to`.
* - When `from` is zero, `tokenId` will be minted for `to`.
* - When `to` is zero, ``from``'s `tokenId` will be burned.
* - `from` cannot be the zero address.
* - `to` cannot be the zero address.
*
* To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks].
*/
function _beforeTokenTransfer(
address from,
address to,
uint256 tokenId
) internal virtual override {
super._beforeTokenTransfer(from, to, tokenId);
if (from == address(0)) {
_addTokenToAllTokensEnumeration(tokenId);
} else if (from != to) {
_removeTokenFromOwnerEnumeration(from, tokenId);
}
if (to == address(0)) {
_removeTokenFromAllTokensEnumeration(tokenId);
} else if (to != from) {
_addTokenToOwnerEnumeration(to, tokenId);
}
}
/**
* @dev Private function to add a token to this extension's ownership-tracking data structures.
* @param to address representing the new owner of the given token ID
* @param tokenId uint256 ID of the token to be added to the tokens list of the given address
*/
function _addTokenToOwnerEnumeration(address to, uint256 tokenId) private {
uint256 length = SBT.balanceOf(to);
_ownedTokens[to][length] = tokenId;
_ownedTokensIndex[tokenId] = length;
}
/**
* @dev Private function to add a token to this extension's token tracking data structures.
* @param tokenId uint256 ID of the token to be added to the tokens list
*/
function _addTokenToAllTokensEnumeration(uint256 tokenId) private {
_allTokensIndex[tokenId] = _allTokens.length;
_allTokens.push(tokenId);
}
/**
* @dev Private function to remove a token from this extension's ownership-tracking data structures. Note that
* while the token is not assigned a new owner, the `_ownedTokensIndex` mapping is _not_ updated: this allows for
* gas optimizations e.g. when performing a transfer operation (avoiding double writes).
* This has O(1) time complexity, but alters the order of the _ownedTokens array.
* @param from address representing the previous owner of the given token ID
* @param tokenId uint256 ID of the token to be removed from the tokens list of the given address
*/
function _removeTokenFromOwnerEnumeration(address from, uint256 tokenId)
private
{
// To prevent a gap in from's tokens array, we store the last token in the index of the token to delete, and
// then delete the last slot (swap and pop).
uint256 lastTokenIndex = SBT.balanceOf(from) - 1;
uint256 tokenIndex = _ownedTokensIndex[tokenId];
// When the token to delete is the last token, the swap operation is unnecessary
if (tokenIndex != lastTokenIndex) {
uint256 lastTokenId = _ownedTokens[from][lastTokenIndex];
_ownedTokens[from][tokenIndex] = lastTokenId; // Move the last token to the slot of the to-delete token
_ownedTokensIndex[lastTokenId] = tokenIndex; // Update the moved token's index
}
// This also deletes the contents at the last position of the array
delete _ownedTokensIndex[tokenId];
delete _ownedTokens[from][lastTokenIndex];
}
/**
* @dev Private function to remove a token from this extension's token tracking data structures.
* This has O(1) time complexity, but alters the order of the _allTokens array.
* @param tokenId uint256 ID of the token to be removed from the tokens list
*/
function _removeTokenFromAllTokensEnumeration(uint256 tokenId) private {
// To prevent a gap in the tokens array, we store the last token in the index of the token to delete, and
// then delete the last slot (swap and pop).
uint256 lastTokenIndex = _allTokens.length - 1;
uint256 tokenIndex = _allTokensIndex[tokenId];
// When the token to delete is the last token, the swap operation is unnecessary. However, since this occurs so
// rarely (when the last minted token is burnt) that we still do the swap here to avoid the gas cost of adding
// an 'if' statement (like in _removeTokenFromOwnerEnumeration)
uint256 lastTokenId = _allTokens[lastTokenIndex];
_allTokens[tokenIndex] = lastTokenId; // Move the last token to the slot of the to-delete token
_allTokensIndex[lastTokenId] = tokenIndex; // Update the moved token's index
// This also deletes the contents at the last position of the array
delete _allTokensIndex[tokenId];
_allTokens.pop();
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
import "@openzeppelin/contracts/utils/Context.sol";
import "../SBT.sol";
/**
* @title SBT Burnable Token
* @dev SBT Token that can be burned (destroyed).
*/
abstract contract SBTBurnable is Context, SBT {
/**
* @dev Burns `tokenId`. See {SBT-_burn}.
*
* Requirements:
*
* - The caller must own `tokenId` or be an approved operator.
*/
function burn(uint256 tokenId) public virtual {
//solhint-disable-next-line max-line-length
require(
_isOwner(_msgSender(), tokenId),
"SBT: caller is not token owner"
);
_burn(tokenId);
}
}
// 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 IAccessControl {
/**
* @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 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 "./IERC165.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 ERC165 is IERC165 {
/**
* @dev See {IERC165-supportsInterface}.
*/
function supportsInterface(bytes4 interfaceId) public view virtual override returns (bool) {
return interfaceId == type(IERC165).interfaceId;
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
import "../ISBT.sol";
/**
* @title SBT Soulbound Token Standard, optional metadata extension
*/
interface ISBTMetadata is ISBT {
/**
* @dev Returns the token collection name.
*/
function name() external view returns (string memory);
/**
* @dev Returns the token collection symbol.
*/
function symbol() external view returns (string memory);
/**
* @dev Returns the Uniform Resource Identifier (URI) for `tokenId` token.
*/
function tokenURI(uint256 tokenId) external view returns (string memory);
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
import "../ISBT.sol";
/**
* @title SBT Soulbound Token Standard, optional enumeration extension
*/
interface ISBTEnumerable is ISBT {
/**
* @dev Returns the total amount of tokens stored by the contract.
*/
function totalSupply() external view returns (uint256);
/**
* @dev Returns a token ID owned by `owner` at a given `index` of its token list.
* Use along with {balanceOf} to enumerate all of ``owner``'s tokens.
*/
function tokenOfOwnerByIndex(address owner, uint256 index)
external
view
returns (uint256);
/**
* @dev Returns a token ID at a given `index` of all the tokens stored by the contract.
* Use along with {totalSupply} to enumerate all tokens.
*/
function tokenByIndex(uint256 index) external view returns (uint256);
}