// SPDX-License-Identifier: MITpragmasolidity ^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.
*/abstractcontractAccessControlisContext, IAccessControl, ERC165{
structRoleData {
mapping(address=>bool) members;
bytes32 adminRole;
}
mapping(bytes32=> RoleData) private _roles;
bytes32publicconstant 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._
*/modifieronlyRole(bytes32 role) {
_checkRole(role, _msgSender());
_;
}
/**
* @dev See {IERC165-supportsInterface}.
*/functionsupportsInterface(bytes4 interfaceId) publicviewvirtualoverridereturns (bool) {
return interfaceId ==type(IAccessControl).interfaceId||super.supportsInterface(interfaceId);
}
/**
* @dev Returns `true` if `account` has been granted `role`.
*/functionhasRole(bytes32 role, address account) publicviewoverridereturns (bool) {
return _roles[role].members[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) internalview{
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}.
*/functiongetRoleAdmin(bytes32 role) publicviewoverridereturns (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.
*/functiongrantRole(bytes32 role, address account) publicvirtualoverrideonlyRole(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.
*/functionrevokeRole(bytes32 role, address account) publicvirtualoverrideonlyRole(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 granted `role`, emits a {RoleRevoked}
* event.
*
* Requirements:
*
* - the caller must be `account`.
*/functionrenounceRole(bytes32 role, address account) publicvirtualoverride{
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.
*
* [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}.
* ====
*/function_setupRole(bytes32 role, address account) internalvirtual{
_grantRole(role, account);
}
/**
* @dev Sets `adminRole` as ``role``'s admin role.
*
* Emits a {RoleAdminChanged} event.
*/function_setRoleAdmin(bytes32 role, bytes32 adminRole) internalvirtual{
bytes32 previousAdminRole = getRoleAdmin(role);
_roles[role].adminRole = adminRole;
emit RoleAdminChanged(role, previousAdminRole, adminRole);
}
function_grantRole(bytes32 role, address account) private{
if (!hasRole(role, account)) {
_roles[role].members[account] =true;
emit RoleGranted(role, account, _msgSender());
}
}
function_revokeRole(bytes32 role, address account) private{
if (hasRole(role, account)) {
_roles[role].members[account] =false;
emit RoleRevoked(role, account, _msgSender());
}
}
}
Contract Source Code
File 2 of 30: Address.sol
// SPDX-License-Identifier: MITpragmasolidity ^0.8.0;/**
* @dev Collection of functions related to the address type
*/libraryAddress{
/**
* @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
* ====
*/functionisContract(address account) internalviewreturns (bool) {
// This method relies on extcodesize, which returns 0 for contracts in// construction, since the code is only stored at the end of the// constructor execution.uint256 size;
assembly {
size :=extcodesize(account)
}
return size >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].
*/functionsendValue(addresspayable 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._
*/functionfunctionCall(address target, bytesmemory data) internalreturns (bytesmemory) {
return functionCall(target, data, "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._
*/functionfunctionCall(address target,
bytesmemory data,
stringmemory errorMessage
) internalreturns (bytesmemory) {
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._
*/functionfunctionCallWithValue(address target,
bytesmemory data,
uint256 value
) internalreturns (bytesmemory) {
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._
*/functionfunctionCallWithValue(address target,
bytesmemory data,
uint256 value,
stringmemory errorMessage
) internalreturns (bytesmemory) {
require(address(this).balance>= value, "Address: insufficient balance for call");
require(isContract(target), "Address: call to non-contract");
(bool success, bytesmemory returndata) = target.call{value: value}(data);
return verifyCallResult(success, returndata, errorMessage);
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
* but performing a static call.
*
* _Available since v3.3._
*/functionfunctionStaticCall(address target, bytesmemory data) internalviewreturns (bytesmemory) {
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._
*/functionfunctionStaticCall(address target,
bytesmemory data,
stringmemory errorMessage
) internalviewreturns (bytesmemory) {
require(isContract(target), "Address: static call to non-contract");
(bool success, bytesmemory returndata) = target.staticcall(data);
return verifyCallResult(success, returndata, errorMessage);
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
* but performing a delegate call.
*
* _Available since v3.4._
*/functionfunctionDelegateCall(address target, bytesmemory data) internalreturns (bytesmemory) {
return functionDelegateCall(target, data, "Address: low-level delegate call failed");
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],
* but performing a delegate call.
*
* _Available since v3.4._
*/functionfunctionDelegateCall(address target,
bytesmemory data,
stringmemory errorMessage
) internalreturns (bytesmemory) {
require(isContract(target), "Address: delegate call to non-contract");
(bool success, bytesmemory returndata) = target.delegatecall(data);
return verifyCallResult(success, returndata, errorMessage);
}
/**
* @dev Tool to verifies that a low level call was successful, and revert if it wasn't, either by bubbling the
* revert reason using the provided one.
*
* _Available since v4.3._
*/functionverifyCallResult(bool success,
bytesmemory returndata,
stringmemory errorMessage
) internalpurereturns (bytesmemory) {
if (success) {
return returndata;
} else {
// Look for revert reason and bubble it up if presentif (returndata.length>0) {
// The easiest way to bubble the revert reason is using memory via assemblyassembly {
let returndata_size :=mload(returndata)
revert(add(32, returndata), returndata_size)
}
} else {
revert(errorMessage);
}
}
}
}
Contract Source Code
File 3 of 30: Context.sol
// SPDX-License-Identifier: MITpragmasolidity ^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.
*/abstractcontractContext{
function_msgSender() internalviewvirtualreturns (address) {
returnmsg.sender;
}
function_msgData() internalviewvirtualreturns (bytescalldata) {
returnmsg.data;
}
}
Contract Source Code
File 4 of 30: Counters.sol
// SPDX-License-Identifier: MITpragmasolidity ^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;`
*/libraryCounters{
structCounter {
// 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/4637uint256 _value; // default: 0
}
functioncurrent(Counter storage counter) internalviewreturns (uint256) {
return counter._value;
}
functionincrement(Counter storage counter) internal{
unchecked {
counter._value +=1;
}
}
functiondecrement(Counter storage counter) internal{
uint256 value = counter._value;
require(value >0, "Counter: decrement overflow");
unchecked {
counter._value = value -1;
}
}
functionreset(Counter storage counter) internal{
counter._value =0;
}
}
Contract Source Code
File 5 of 30: ERC165.sol
// SPDX-License-Identifier: MITpragmasolidity ^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.
*/abstractcontractERC165isIERC165{
/**
* @dev See {IERC165-supportsInterface}.
*/functionsupportsInterface(bytes4 interfaceId) publicviewvirtualoverridereturns (bool) {
return interfaceId ==type(IERC165).interfaceId;
}
}
Contract Source Code
File 6 of 30: ERC721.sol
// SPDX-License-Identifier: MITpragmasolidity ^0.8.0;import"./IERC721.sol";
import"./IERC721Receiver.sol";
import"./extensions/IERC721Metadata.sol";
import"../../utils/Address.sol";
import"../../utils/Context.sol";
import"../../utils/Strings.sol";
import"../../utils/introspection/ERC165.sol";
/**
* @dev Implementation of https://eips.ethereum.org/EIPS/eip-721[ERC721] Non-Fungible Token Standard, including
* the Metadata extension, but not including the Enumerable extension, which is available separately as
* {ERC721Enumerable}.
*/contractERC721isContext, ERC165, IERC721, IERC721Metadata{
usingAddressforaddress;
usingStringsforuint256;
// Token namestringprivate _name;
// Token symbolstringprivate _symbol;
// Mapping from token ID to owner addressmapping(uint256=>address) private _owners;
// Mapping owner address to token countmapping(address=>uint256) private _balances;
// Mapping from token ID to approved addressmapping(uint256=>address) private _tokenApprovals;
// Mapping from owner to operator approvalsmapping(address=>mapping(address=>bool)) private _operatorApprovals;
/**
* @dev Initializes the contract by setting a `name` and a `symbol` to the token collection.
*/constructor(stringmemory name_, stringmemory symbol_) {
_name = name_;
_symbol = symbol_;
}
/**
* @dev See {IERC165-supportsInterface}.
*/functionsupportsInterface(bytes4 interfaceId) publicviewvirtualoverride(ERC165, IERC165) returns (bool) {
return
interfaceId ==type(IERC721).interfaceId||
interfaceId ==type(IERC721Metadata).interfaceId||super.supportsInterface(interfaceId);
}
/**
* @dev See {IERC721-balanceOf}.
*/functionbalanceOf(address owner) publicviewvirtualoverridereturns (uint256) {
require(owner !=address(0), "ERC721: balance query for the zero address");
return _balances[owner];
}
/**
* @dev See {IERC721-ownerOf}.
*/functionownerOf(uint256 tokenId) publicviewvirtualoverridereturns (address) {
address owner = _owners[tokenId];
require(owner !=address(0), "ERC721: owner query for nonexistent token");
return owner;
}
/**
* @dev See {IERC721Metadata-name}.
*/functionname() publicviewvirtualoverridereturns (stringmemory) {
return _name;
}
/**
* @dev See {IERC721Metadata-symbol}.
*/functionsymbol() publicviewvirtualoverridereturns (stringmemory) {
return _symbol;
}
/**
* @dev See {IERC721Metadata-tokenURI}.
*/functiontokenURI(uint256 tokenId) publicviewvirtualoverridereturns (stringmemory) {
require(_exists(tokenId), "ERC721Metadata: URI query for nonexistent token");
stringmemory baseURI = _baseURI();
returnbytes(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 overriden in child contracts.
*/function_baseURI() internalviewvirtualreturns (stringmemory) {
return"";
}
/**
* @dev See {IERC721-approve}.
*/functionapprove(address to, uint256 tokenId) publicvirtualoverride{
address owner = ERC721.ownerOf(tokenId);
require(to != owner, "ERC721: approval to current owner");
require(
_msgSender() == owner || isApprovedForAll(owner, _msgSender()),
"ERC721: approve caller is not owner nor approved for all"
);
_approve(to, tokenId);
}
/**
* @dev See {IERC721-getApproved}.
*/functiongetApproved(uint256 tokenId) publicviewvirtualoverridereturns (address) {
require(_exists(tokenId), "ERC721: approved query for nonexistent token");
return _tokenApprovals[tokenId];
}
/**
* @dev See {IERC721-setApprovalForAll}.
*/functionsetApprovalForAll(address operator, bool approved) publicvirtualoverride{
require(operator != _msgSender(), "ERC721: approve to caller");
_operatorApprovals[_msgSender()][operator] = approved;
emit ApprovalForAll(_msgSender(), operator, approved);
}
/**
* @dev See {IERC721-isApprovedForAll}.
*/functionisApprovedForAll(address owner, address operator) publicviewvirtualoverridereturns (bool) {
return _operatorApprovals[owner][operator];
}
/**
* @dev See {IERC721-transferFrom}.
*/functiontransferFrom(addressfrom,
address to,
uint256 tokenId
) publicvirtualoverride{
//solhint-disable-next-line max-line-lengthrequire(_isApprovedOrOwner(_msgSender(), tokenId), "ERC721: transfer caller is not owner nor approved");
_transfer(from, to, tokenId);
}
/**
* @dev See {IERC721-safeTransferFrom}.
*/functionsafeTransferFrom(addressfrom,
address to,
uint256 tokenId
) publicvirtualoverride{
safeTransferFrom(from, to, tokenId, "");
}
/**
* @dev See {IERC721-safeTransferFrom}.
*/functionsafeTransferFrom(addressfrom,
address to,
uint256 tokenId,
bytesmemory _data
) publicvirtualoverride{
require(_isApprovedOrOwner(_msgSender(), tokenId), "ERC721: transfer caller is not owner nor approved");
_safeTransfer(from, to, tokenId, _data);
}
/**
* @dev Safely transfers `tokenId` token from `from` to `to`, checking first that contract recipients
* are aware of the ERC721 protocol to prevent tokens from being forever locked.
*
* `_data` is additional data, it has no specified format and it is sent in call to `to`.
*
* This internal function is equivalent to {safeTransferFrom}, and can be used to e.g.
* implement alternative mechanisms to perform token transfer, such as signature-based.
*
* Requirements:
*
* - `from` cannot be the zero address.
* - `to` cannot be the zero address.
* - `tokenId` token must exist and be owned by `from`.
* - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon a safe transfer.
*
* Emits a {Transfer} event.
*/function_safeTransfer(addressfrom,
address to,
uint256 tokenId,
bytesmemory _data
) internalvirtual{
_transfer(from, to, tokenId);
require(_checkOnERC721Received(from, to, tokenId, _data), "ERC721: transfer to non ERC721Receiver implementer");
}
/**
* @dev Returns whether `tokenId` exists.
*
* Tokens can be managed by their owner or approved accounts via {approve} or {setApprovalForAll}.
*
* Tokens start existing when they are minted (`_mint`),
* and stop existing when they are burned (`_burn`).
*/function_exists(uint256 tokenId) internalviewvirtualreturns (bool) {
return _owners[tokenId] !=address(0);
}
/**
* @dev Returns whether `spender` is allowed to manage `tokenId`.
*
* Requirements:
*
* - `tokenId` must exist.
*/function_isApprovedOrOwner(address spender, uint256 tokenId) internalviewvirtualreturns (bool) {
require(_exists(tokenId), "ERC721: operator query for nonexistent token");
address owner = ERC721.ownerOf(tokenId);
return (spender == owner || getApproved(tokenId) == spender || isApprovedForAll(owner, spender));
}
/**
* @dev Safely mints `tokenId` and transfers it to `to`.
*
* Requirements:
*
* - `tokenId` must not exist.
* - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon a safe transfer.
*
* Emits a {Transfer} event.
*/function_safeMint(address to, uint256 tokenId) internalvirtual{
_safeMint(to, tokenId, "");
}
/**
* @dev Same as {xref-ERC721-_safeMint-address-uint256-}[`_safeMint`], with an additional `data` parameter which is
* forwarded in {IERC721Receiver-onERC721Received} to contract recipients.
*/function_safeMint(address to,
uint256 tokenId,
bytesmemory _data
) internalvirtual{
_mint(to, tokenId);
require(
_checkOnERC721Received(address(0), to, tokenId, _data),
"ERC721: transfer to non ERC721Receiver implementer"
);
}
/**
* @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 {Transfer} event.
*/function_mint(address to, uint256 tokenId) internalvirtual{
require(to !=address(0), "ERC721: mint to the zero address");
require(!_exists(tokenId), "ERC721: token already minted");
_beforeTokenTransfer(address(0), to, tokenId);
_balances[to] +=1;
_owners[tokenId] = to;
emit Transfer(address(0), to, tokenId);
}
/**
* @dev Destroys `tokenId`.
* The approval is cleared when the token is burned.
*
* Requirements:
*
* - `tokenId` must exist.
*
* Emits a {Transfer} event.
*/function_burn(uint256 tokenId) internalvirtual{
address owner = ERC721.ownerOf(tokenId);
_beforeTokenTransfer(owner, address(0), tokenId);
// Clear approvals
_approve(address(0), tokenId);
_balances[owner] -=1;
delete _owners[tokenId];
emit Transfer(owner, address(0), tokenId);
}
/**
* @dev Transfers `tokenId` from `from` to `to`.
* As opposed to {transferFrom}, this imposes no restrictions on msg.sender.
*
* Requirements:
*
* - `to` cannot be the zero address.
* - `tokenId` token must be owned by `from`.
*
* Emits a {Transfer} event.
*/function_transfer(addressfrom,
address to,
uint256 tokenId
) internalvirtual{
require(ERC721.ownerOf(tokenId) ==from, "ERC721: transfer of token that is not own");
require(to !=address(0), "ERC721: transfer to the zero address");
_beforeTokenTransfer(from, to, tokenId);
// Clear approvals from the previous owner
_approve(address(0), tokenId);
_balances[from] -=1;
_balances[to] +=1;
_owners[tokenId] = to;
emit Transfer(from, to, tokenId);
}
/**
* @dev Approve `to` to operate on `tokenId`
*
* Emits a {Approval} event.
*/function_approve(address to, uint256 tokenId) internalvirtual{
_tokenApprovals[tokenId] = to;
emit Approval(ERC721.ownerOf(tokenId), to, tokenId);
}
/**
* @dev Internal function to invoke {IERC721Receiver-onERC721Received} on a target address.
* The call is not executed if the target address is not a contract.
*
* @param from address representing the previous owner of the given token ID
* @param to target address that will receive the tokens
* @param tokenId uint256 ID of the token to be transferred
* @param _data bytes optional data to send along with the call
* @return bool whether the call correctly returned the expected magic value
*/function_checkOnERC721Received(addressfrom,
address to,
uint256 tokenId,
bytesmemory _data
) privatereturns (bool) {
if (to.isContract()) {
try IERC721Receiver(to).onERC721Received(_msgSender(), from, tokenId, _data) returns (bytes4 retval) {
return retval == IERC721Receiver.onERC721Received.selector;
} catch (bytesmemory reason) {
if (reason.length==0) {
revert("ERC721: transfer to non ERC721Receiver implementer");
} else {
assembly {
revert(add(32, reason), mload(reason))
}
}
}
} else {
returntrue;
}
}
/**
* @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` and `to` are never both zero.
*
* To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks].
*/function_beforeTokenTransfer(addressfrom,
address to,
uint256 tokenId
) internalvirtual{}
}
Contract Source Code
File 7 of 30: ERC721Enumerable.sol
// SPDX-License-Identifier: MITpragmasolidity ^0.8.0;import"../ERC721.sol";
import"./IERC721Enumerable.sol";
/**
* @dev This implements an optional extension of {ERC721} 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.
*/abstractcontractERC721EnumerableisERC721, IERC721Enumerable{
// Mapping from owner to list of owned token IDsmapping(address=>mapping(uint256=>uint256)) private _ownedTokens;
// Mapping from token ID to index of the owner tokens listmapping(uint256=>uint256) private _ownedTokensIndex;
// Array with all token ids, used for enumerationuint256[] private _allTokens;
// Mapping from token id to position in the allTokens arraymapping(uint256=>uint256) private _allTokensIndex;
/**
* @dev See {IERC165-supportsInterface}.
*/functionsupportsInterface(bytes4 interfaceId) publicviewvirtualoverride(IERC165, ERC721) returns (bool) {
return interfaceId ==type(IERC721Enumerable).interfaceId||super.supportsInterface(interfaceId);
}
/**
* @dev See {IERC721Enumerable-tokenOfOwnerByIndex}.
*/functiontokenOfOwnerByIndex(address owner, uint256 index) publicviewvirtualoverridereturns (uint256) {
require(index < ERC721.balanceOf(owner), "ERC721Enumerable: owner index out of bounds");
return _ownedTokens[owner][index];
}
/**
* @dev See {IERC721Enumerable-totalSupply}.
*/functiontotalSupply() publicviewvirtualoverridereturns (uint256) {
return _allTokens.length;
}
/**
* @dev See {IERC721Enumerable-tokenByIndex}.
*/functiontokenByIndex(uint256 index) publicviewvirtualoverridereturns (uint256) {
require(index < ERC721Enumerable.totalSupply(), "ERC721Enumerable: 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(addressfrom,
address to,
uint256 tokenId
) internalvirtualoverride{
super._beforeTokenTransfer(from, to, tokenId);
if (from==address(0)) {
_addTokenToAllTokensEnumeration(tokenId);
} elseif (from!= to) {
_removeTokenFromOwnerEnumeration(from, tokenId);
}
if (to ==address(0)) {
_removeTokenFromAllTokensEnumeration(tokenId);
} elseif (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 = ERC721.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(addressfrom, 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 = ERC721.balanceOf(from) -1;
uint256 tokenIndex = _ownedTokensIndex[tokenId];
// When the token to delete is the last token, the swap operation is unnecessaryif (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 arraydelete _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 arraydelete _allTokensIndex[tokenId];
_allTokens.pop();
}
}
Contract Source Code
File 8 of 30: IAccessControl.sol
// SPDX-License-Identifier: MITpragmasolidity ^0.8.0;/**
* @dev External interface of AccessControl declared to support ERC165 detection.
*/interfaceIAccessControl{
/**
* @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._
*/eventRoleAdminChanged(bytes32indexed role, bytes32indexed previousAdminRole, bytes32indexed 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}.
*/eventRoleGranted(bytes32indexed role, addressindexed account, addressindexed 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`)
*/eventRoleRevoked(bytes32indexed role, addressindexed account, addressindexed sender);
/**
* @dev Returns `true` if `account` has been granted `role`.
*/functionhasRole(bytes32 role, address account) externalviewreturns (bool);
/**
* @dev Returns the admin role that controls `role`. See {grantRole} and
* {revokeRole}.
*
* To change a role's admin, use {AccessControl-_setRoleAdmin}.
*/functiongetRoleAdmin(bytes32 role) externalviewreturns (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.
*/functiongrantRole(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.
*/functionrevokeRole(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`.
*/functionrenounceRole(bytes32 role, address account) external;
}
Contract Source Code
File 9 of 30: IERC165.sol
// SPDX-License-Identifier: MITpragmasolidity ^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}.
*/interfaceIERC165{
/**
* @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.
*/functionsupportsInterface(bytes4 interfaceId) externalviewreturns (bool);
}
Contract Source Code
File 10 of 30: IERC721.sol
// SPDX-License-Identifier: MITpragmasolidity ^0.8.0;import"../../utils/introspection/IERC165.sol";
/**
* @dev Required interface of an ERC721 compliant contract.
*/interfaceIERC721isIERC165{
/**
* @dev Emitted when `tokenId` token is transferred from `from` to `to`.
*/eventTransfer(addressindexedfrom, addressindexed to, uint256indexed tokenId);
/**
* @dev Emitted when `owner` enables `approved` to manage the `tokenId` token.
*/eventApproval(addressindexed owner, addressindexed approved, uint256indexed tokenId);
/**
* @dev Emitted when `owner` enables or disables (`approved`) `operator` to manage all of its assets.
*/eventApprovalForAll(addressindexed owner, addressindexed operator, bool approved);
/**
* @dev Returns the number of tokens in ``owner``'s account.
*/functionbalanceOf(address owner) externalviewreturns (uint256 balance);
/**
* @dev Returns the owner of the `tokenId` token.
*
* Requirements:
*
* - `tokenId` must exist.
*/functionownerOf(uint256 tokenId) externalviewreturns (address owner);
/**
* @dev Safely transfers `tokenId` token from `from` to `to`, checking first that contract recipients
* are aware of the ERC721 protocol to prevent tokens from being forever locked.
*
* Requirements:
*
* - `from` cannot be the zero address.
* - `to` cannot be the zero address.
* - `tokenId` token must exist and be owned by `from`.
* - If the caller is not `from`, it must be have been allowed to move this token by either {approve} or {setApprovalForAll}.
* - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon a safe transfer.
*
* Emits a {Transfer} event.
*/functionsafeTransferFrom(addressfrom,
address to,
uint256 tokenId
) external;
/**
* @dev Transfers `tokenId` token from `from` to `to`.
*
* WARNING: Usage of this method is discouraged, use {safeTransferFrom} whenever possible.
*
* Requirements:
*
* - `from` cannot be the zero address.
* - `to` cannot be the zero address.
* - `tokenId` token must be owned by `from`.
* - If the caller is not `from`, it must be approved to move this token by either {approve} or {setApprovalForAll}.
*
* Emits a {Transfer} event.
*/functiontransferFrom(addressfrom,
address to,
uint256 tokenId
) external;
/**
* @dev Gives permission to `to` to transfer `tokenId` token to another account.
* The approval is cleared when the token is transferred.
*
* Only a single account can be approved at a time, so approving the zero address clears previous approvals.
*
* Requirements:
*
* - The caller must own the token or be an approved operator.
* - `tokenId` must exist.
*
* Emits an {Approval} event.
*/functionapprove(address to, uint256 tokenId) external;
/**
* @dev Returns the account approved for `tokenId` token.
*
* Requirements:
*
* - `tokenId` must exist.
*/functiongetApproved(uint256 tokenId) externalviewreturns (address operator);
/**
* @dev Approve or remove `operator` as an operator for the caller.
* Operators can call {transferFrom} or {safeTransferFrom} for any token owned by the caller.
*
* Requirements:
*
* - The `operator` cannot be the caller.
*
* Emits an {ApprovalForAll} event.
*/functionsetApprovalForAll(address operator, bool _approved) external;
/**
* @dev Returns if the `operator` is allowed to manage all of the assets of `owner`.
*
* See {setApprovalForAll}
*/functionisApprovedForAll(address owner, address operator) externalviewreturns (bool);
/**
* @dev Safely transfers `tokenId` token from `from` to `to`.
*
* Requirements:
*
* - `from` cannot be the zero address.
* - `to` cannot be the zero address.
* - `tokenId` token must exist and be owned by `from`.
* - If the caller is not `from`, it must be approved to move this token by either {approve} or {setApprovalForAll}.
* - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon a safe transfer.
*
* Emits a {Transfer} event.
*/functionsafeTransferFrom(addressfrom,
address to,
uint256 tokenId,
bytescalldata data
) external;
}
Contract Source Code
File 11 of 30: IERC721Enumerable.sol
// SPDX-License-Identifier: MITpragmasolidity ^0.8.0;import"../IERC721.sol";
/**
* @title ERC-721 Non-Fungible Token Standard, optional enumeration extension
* @dev See https://eips.ethereum.org/EIPS/eip-721
*/interfaceIERC721EnumerableisIERC721{
/**
* @dev Returns the total amount of tokens stored by the contract.
*/functiontotalSupply() externalviewreturns (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.
*/functiontokenOfOwnerByIndex(address owner, uint256 index) externalviewreturns (uint256 tokenId);
/**
* @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.
*/functiontokenByIndex(uint256 index) externalviewreturns (uint256);
}
// SPDX-License-Identifier: MITpragmasolidity ^0.8.0;/**
* @title ERC721 token receiver interface
* @dev Interface for any contract that wants to support safeTransfers
* from ERC721 asset contracts.
*/interfaceIERC721Receiver{
/**
* @dev Whenever an {IERC721} `tokenId` token is transferred to this contract via {IERC721-safeTransferFrom}
* by `operator` from `from`, this function is called.
*
* It must return its Solidity selector to confirm the token transfer.
* If any other value is returned or the interface is not implemented by the recipient, the transfer will be reverted.
*
* The selector can be obtained in Solidity with `IERC721.onERC721Received.selector`.
*/functiononERC721Received(address operator,
addressfrom,
uint256 tokenId,
bytescalldata data
) externalreturns (bytes4);
}
// SPDX-License-Identifier: MITpragmasolidity 0.8.6;interfaceIPricingStrategy{
/**
* @notice Returns the next price for an N mint
*/functiongetNextPriceForNHoldersInWei(uint256 numberOfMints,
address account,
bytesmemory data
) externalviewreturns (uint256);
/**
* @notice Returns the next price for an open mint
*/functiongetNextPriceForOpenMintInWei(uint256 numberOfMints,
address account,
bytesmemory data
) externalviewreturns (uint256);
}
// SPDX-License-Identifier: MITpragmasolidity ^0.8.0;/**
* @dev Standard math utilities missing in the Solidity language.
*/libraryMath{
/**
* @dev Returns the largest of two numbers.
*/functionmax(uint256 a, uint256 b) internalpurereturns (uint256) {
return a >= b ? a : b;
}
/**
* @dev Returns the smallest of two numbers.
*/functionmin(uint256 a, uint256 b) internalpurereturns (uint256) {
return a < b ? a : b;
}
/**
* @dev Returns the average of two numbers. The result is rounded towards
* zero.
*/functionaverage(uint256 a, uint256 b) internalpurereturns (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.
*/functionceilDiv(uint256 a, uint256 b) internalpurereturns (uint256) {
// (a + b - 1) / b can overflow on addition, so we distribute.return a / b + (a % b ==0 ? 0 : 1);
}
}
Contract Source Code
File 22 of 30: NilPassCore.sol
// SPDX-License-Identifier: MITpragmasolidity 0.8.6;import"@openzeppelin/contracts/token/ERC721/extensions/ERC721Enumerable.sol";
import"@openzeppelin/contracts/security/ReentrancyGuard.sol";
import"@openzeppelin/contracts/access/AccessControl.sol";
import"@openzeppelin/contracts/utils/math/Math.sol";
import"../interfaces/IN.sol";
import"../interfaces/INilPass.sol";
import"../interfaces/IPricingStrategy.sol";
/**
* @title NilPassCore contract
* @author Tony Snark
* @notice This contract provides basic functionalities to allow minting using the NilPass
* @dev This contract should be used only for testing or testnet deployments
*/abstractcontractNilPassCoreisERC721Enumerable, ReentrancyGuard, AccessControl, INilPass, IPricingStrategy{
uint128publicconstant MAX_MULTI_MINT_AMOUNT =32;
uint128publicconstant MAX_N_TOKEN_ID =8888;
bytes32publicconstant ADMIN_ROLE =keccak256("ADMIN");
bytes32publicconstant DAO_ROLE =keccak256("DAO");
IN publicimmutable n;
uint16public reserveMinted;
uint256public mintedOutsideNRange;
addresspublic masterMint;
DerivativeParameters public derivativeParams;
uint128 maxTokenId;
structDerivativeParameters {
bool onlyNHolders;
bool supportsTokenId;
uint16 reservedAllowance;
uint128 maxTotalSupply;
uint128 maxMintAllowance;
}
eventMinted(address to, uint256 tokenId);
/**
* @notice Construct an NilPassCore instance
* @param name Name of the token
* @param symbol Symbol of the token
* @param n_ Address of your n instance (only for testing)
* @param derivativeParams_ Parameters describing the derivative settings
* @param masterMint_ Address of the master mint contract
* @param dao_ Address of the NIL DAO
*/constructor(stringmemory name,
stringmemory symbol,
IN n_,
DerivativeParameters memory derivativeParams_,
address masterMint_,
address dao_
) ERC721(name, symbol) {
derivativeParams = derivativeParams_;
require(derivativeParams.maxTotalSupply >0, "NilPass:INVALID_SUPPLY");
require(
!derivativeParams.onlyNHolders ||
(derivativeParams.onlyNHolders && derivativeParams.maxTotalSupply <= MAX_N_TOKEN_ID),
"NilPass:INVALID_SUPPLY"
);
require(derivativeParams.maxTotalSupply >= derivativeParams.reservedAllowance, "NilPass:INVALID_ALLOWANCE");
require(masterMint_ !=address(0), "NilPass:INVALID_MASTERMINT");
require(dao_ !=address(0), "NilPass:INVALID_DAO");
n = n_;
masterMint = masterMint_;
derivativeParams.maxMintAllowance = derivativeParams.maxMintAllowance < MAX_MULTI_MINT_AMOUNT
? derivativeParams.maxMintAllowance
: MAX_MULTI_MINT_AMOUNT;
maxTokenId = derivativeParams.maxTotalSupply > MAX_N_TOKEN_ID
? derivativeParams.maxTotalSupply
: MAX_N_TOKEN_ID;
_setupRole(ADMIN_ROLE, msg.sender);
_setupRole(DAO_ROLE, dao_);
_setRoleAdmin(ADMIN_ROLE, ADMIN_ROLE);
_setRoleAdmin(DAO_ROLE, DAO_ROLE);
}
modifieronlyAdmin() {
require(hasRole(ADMIN_ROLE, msg.sender), "Nil:ACCESS_DENIED");
_;
}
modifieronlyDAO() {
require(hasRole(DAO_ROLE, msg.sender), "Nil:ACCESS_DENIED");
_;
}
/**
* @notice Allow anyone to mint a token with the supply id if this pass is unrestricted.
* n token holders can use this function without using the n token holders allowance,
* this is useful when the allowance is fully utilized.
*/functionmint(address,
uint8,
uint256,
bytescalldata) publicvirtualoverridenonReentrant{
require(false, "MINTING DISABLED");
}
/**
* @notice Allow anyone to mint multiple tokens with the provided IDs if this pass is unrestricted.
* n token holders can use this function without using the n token holders allowance,
* this is useful when the allowance is fully utilized.
*/functionmintTokenId(address,
uint256[] calldata,
uint256,
bytescalldata) publicvirtualoverridenonReentrant{
require(false, "MINTING DISABLED");
}
/**
* @notice Allow a n token holder to bulk mint tokens with id of their n tokens' id
*/functionmintWithN(address,
uint256[] calldata,
uint256,
bytescalldata) publicvirtualoverridenonReentrant{
require(false, "MINTING DISABLED");
}
/**
* @dev Safely mints `tokenId` and transfers it to `to`
*/function_safeMint(address to, uint256 tokenId) internalvirtualoverride{
require(msg.sender== masterMint, "NilPass:INVALID_MINTER");
super._safeMint(to, tokenId);
emit Minted(to, tokenId);
}
/**
* @notice Set the exclusivity flag to only allow N holders to mint
* @param status Boolean to enable or disable N holder exclusivity
*/functionsetOnlyNHolders(bool status) publiconlyAdmin{
derivativeParams.onlyNHolders = status;
}
/**
* @notice Calculate the currently available number of reserved tokens for n token holders
* @return Reserved mint available
*/functionnHoldersMintsAvailable() publicviewreturns (uint256) {
return derivativeParams.reservedAllowance - reserveMinted;
}
/**
* @notice Calculate the currently available number of open mints
* @return Open mint available
*/functionopenMintsAvailable() publicviewreturns (uint256) {
uint256 maxOpenMints = derivativeParams.maxTotalSupply - derivativeParams.reservedAllowance;
uint256 currentOpenMints = totalSupply() - reserveMinted;
return maxOpenMints - currentOpenMints;
}
/**
* @notice Calculate the total available number of mints
* @return total mint available
*/functiontotalMintsAvailable() publicviewvirtualoverridereturns (uint256) {
return nHoldersMintsAvailable() + openMintsAvailable();
}
functionmintParameters() externalviewoverridereturns (INilPass.MintParams memory) {
return
INilPass.MintParams({
reservedAllowance: derivativeParams.reservedAllowance,
maxTotalSupply: derivativeParams.maxTotalSupply,
openMintsAvailable: openMintsAvailable(),
totalMintsAvailable: totalMintsAvailable(),
nHoldersMintsAvailable: nHoldersMintsAvailable(),
nHolderPriceInWei: getNextPriceForNHoldersInWei(1, address(0x1), ""),
openPriceInWei: getNextPriceForOpenMintInWei(1, address(0x1), ""),
totalSupply: totalSupply(),
onlyNHolders: derivativeParams.onlyNHolders,
maxMintAllowance: derivativeParams.maxMintAllowance,
supportsTokenId: derivativeParams.supportsTokenId
});
}
/**
* @notice Check if a token with an Id exists
* @param tokenId The token Id to check for
*/functiontokenExists(uint256 tokenId) externalviewoverridereturns (bool) {
return _exists(tokenId);
}
functionsupportsInterface(bytes4 interfaceId)
publicviewvirtualoverride(ERC721Enumerable, IERC165, AccessControl)
returns (bool)
{
returnsuper.supportsInterface(interfaceId);
}
functionmaxTotalSupply() externalviewoverridereturns (uint256) {
return derivativeParams.maxTotalSupply;
}
functionreservedAllowance() publicviewreturns (uint16) {
return derivativeParams.reservedAllowance;
}
functiongetNextPriceForNHoldersInWei(uint256 numberOfMints,
address account,
bytesmemory data
) publicviewvirtualoverridereturns (uint256);
functiongetNextPriceForOpenMintInWei(uint256 numberOfMints,
address account,
bytesmemory data
) publicviewvirtualoverridereturns (uint256);
functioncanMint(address account, bytescalldata data) externalviewvirtualoverridereturns (bool);
}
Contract Source Code
File 23 of 30: NilProtocolUtils.sol
// SPDX-License-Identifier: MITpragmasolidity 0.8.6;libraryNilProtocolUtils{
bytesinternalconstant TABLE ="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
/// [MIT License]/// @title Base64/// @notice Provides a function for encoding some bytes in base64/// @author Brecht Devos <brecht@loopring.org>/// @notice Encodes some bytes to the base64 representationfunctionbase64encode(bytesmemory data) externalpurereturns (stringmemory) {
uint256 len = data.length;
if (len ==0) return"";
// multiply by 4/3 rounded upuint256 encodedLen =4* ((len +2) /3);
// Add some extra buffer at the endbytesmemory result =newbytes(encodedLen +32);
bytesmemory table = TABLE;
assembly {
let tablePtr :=add(table, 1)
let resultPtr :=add(result, 32)
for {
let i :=0
} lt(i, len) {
} {
i :=add(i, 3)
let input :=and(mload(add(data, i)), 0xffffff)
let out :=mload(add(tablePtr, and(shr(18, input), 0x3F)))
out :=shl(8, out)
out :=add(out, and(mload(add(tablePtr, and(shr(12, input), 0x3F))), 0xFF))
out :=shl(8, out)
out :=add(out, and(mload(add(tablePtr, and(shr(6, input), 0x3F))), 0xFF))
out :=shl(8, out)
out :=add(out, and(mload(add(tablePtr, and(input, 0x3F))), 0xFF))
out :=shl(224, out)
mstore(resultPtr, out)
resultPtr :=add(resultPtr, 4)
}
switchmod(len, 3)
case1 {
mstore(sub(resultPtr, 2), shl(240, 0x3d3d))
}
case2 {
mstore(sub(resultPtr, 1), shl(248, 0x3d))
}
mstore(result, encodedLen)
}
returnstring(result);
}
// @notice converts number to stringfunctionstringify(uint256 value) externalpurereturns (stringmemory) {
// Inspired by OraclizeAPI's implementation - MIT license// https://github.com/oraclize/ethereum-api/blob/b42146b063c7d6ee1358846c198246239e9360e8/oraclizeAPI_0.4.25.solif (value ==0) {
return"0";
}
uint256 temp = value;
uint256 digits;
while (temp !=0) {
digits++;
temp /=10;
}
bytesmemory buffer =newbytes(digits);
while (value !=0) {
digits -=1;
buffer[digits] =bytes1(uint8(48+uint256(value %10)));
value /=10;
}
returnstring(buffer);
}
}
Contract Source Code
File 24 of 30: ReentrancyGuard.sol
// SPDX-License-Identifier: MITpragmasolidity ^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].
*/abstractcontractReentrancyGuard{
// 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.uint256privateconstant _NOT_ENTERED =1;
uint256privateconstant _ENTERED =2;
uint256private _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 make it call a
* `private` function that does the actual work.
*/modifiernonReentrant() {
// On the first call to nonReentrant, _notEntered will be truerequire(_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;
}
}
Contract Source Code
File 25 of 30: SeedPhrase.sol
//SPDX-License-Identifier: MITpragmasolidity >=0.8.4;import"@openzeppelin/contracts/security/ReentrancyGuard.sol";
import"@openzeppelin/contracts/token/ERC721/ERC721.sol";
import"@openzeppelin/contracts/utils/Counters.sol";
import"@openzeppelin/contracts/utils/Strings.sol";
import"@chainlink/contracts/src/v0.8/VRFConsumerBase.sol";
import"./SeedPhrasePricing.sol";
import"../interfaces/IN.sol";
import"../interfaces/IRarible.sol";
import"../interfaces/IKarmaScore.sol";
import"../interfaces/INOwnerResolver.sol";
import"../libraries/NilProtocolUtils.sol";
import"../libraries/SeedPhraseUtils.sol";
////////////////////////////////////////////////////////////////////////////////////////// //// //// ░██████╗███████╗███████╗██████╗░ ██████╗░██╗░░██╗██████╗░░█████╗░░██████╗███████╗ //// ██╔════╝██╔════╝██╔════╝██╔══██╗ ██╔══██╗██║░░██║██╔══██╗██╔══██╗██╔════╝██╔════╝ //// ╚█████╗░█████╗░░█████╗░░██║░░██║ ██████╔╝███████║██████╔╝███████║╚█████╗░█████╗░░ //// ░╚═══██╗██╔══╝░░██╔══╝░░██║░░██║ ██╔═══╝░██╔══██║██╔══██╗██╔══██║░╚═══██╗██╔══╝░░ //// ██████╔╝███████╗███████╗██████╔╝ ██║░░░░░██║░░██║██║░░██║██║░░██║██████╔╝███████╗ //// ╚═════╝░╚══════╝╚══════╝╚═════╝░ ╚═╝░░░░░╚═╝░░╚═╝╚═╝░░╚═╝╚═╝░░╚═╝╚═════╝░╚══════╝ //// //// //// Title: Seed Phrase //// Devs: Harry Faulkner & maximonee (twitter.com/maximonee_) //// Description: This contract provides minting for the //// Seed Phrase NFT by Sean Elliott //// (twitter.com/seanelliottoc) //// //////////////////////////////////////////////////////////////////////////////////////////contractSeedPhraseisSeedPhrasePricing, VRFConsumerBase{
usingStringsforuint256;
usingStringsforuint16;
usingStringsforuint8;
usingCountersforCounters.Counter;
usingSeedPhraseUtilsforSeedPhraseUtils.Random;
Counters.Counter private _doublePanelTokens;
Counters.Counter private _tokenIds;
addressprivate _owner;
// Tracks whether an n has been used already to mintmapping(uint256=>bool) publicoverride nUsed;
mapping(PreSaleType =>uint16) public presaleLimits;
address[] private genesisSketchAddresses;
uint16[] private bipWordIds =newuint16[](2048);
IRarible publicimmutable rarible;
INOwnerResolver publicimmutable nOwnerResolver;
IKarmaScore publicimmutable karma;
structMaps {
// Map double panel tokens to burned singlesmapping(uint256=>uint256[2]) burnedTokensPairings;
// Mapping of valid double panel pairings (BIP39 IDs)mapping(uint16=>uint16) doubleWordPairings;
// Stores the guarenteed token rarity for a double panelmapping(uint256=>uint8) doubleTokenRarity;
mapping(address=>bool) ogAddresses;
// Map token to their unique seedmapping(uint256=>bytes32) tokenSeed;
}
structConfig {
bool preSaleActive;
bool publicSaleActive;
bool isSaleHalted;
bool bipWordsShuffled;
bool vrfNumberGenerated;
bool isBurnActive;
bool isOwnerSupplyMinted;
bool isGsAirdropComplete;
uint8 ownerSupply;
uint16 maxPublicMint;
uint16 karmaRequirement;
uint32 preSaleLaunchTime;
uint32 publicSaleLaunchTime;
uint256 doubleBurnTokens;
uint256 linkFee;
uint256 raribleTokenId;
uint256 vrfRandomValue;
address vrfCoordinator;
address linkToken;
bytes32 vrfKeyHash;
}
structContractAddresses {
address n;
address masterMint;
address dao;
address nOwnersRegistry;
address vrfCoordinator;
address linkToken;
address karmaAddress;
address rarible;
}
Config private config;
Maps private maps;
eventBurnt(address to, uint256 firstBurntToken, uint256 secondBurntToken, uint256 doublePaneledToken);
DerivativeParameters params = DerivativeParameters(false, false, 0, 2048, 4);
constructor(
ContractAddresses memory contractAddresses,
bytes32 _vrfKeyHash,
uint256 _linkFee
)
SeedPhrasePricing("Seed Phrase",
"SEED",
IN(contractAddresses.n),
params,
30000000000000000,
60000000000000000,
contractAddresses.masterMint,
contractAddresses.dao
)
VRFConsumerBase(contractAddresses.vrfCoordinator, contractAddresses.linkToken)
{
// Start token IDs at 1
_tokenIds.increment();
presaleLimits[PreSaleType.N] =400;
presaleLimits[PreSaleType.Karma] =800;
presaleLimits[PreSaleType.GenesisSketch] =40;
presaleLimits[PreSaleType.OG] =300;
presaleLimits[PreSaleType.GM] =300;
nOwnerResolver = INOwnerResolver(contractAddresses.nOwnersRegistry);
rarible = IRarible(contractAddresses.rarible);
karma = IKarmaScore(contractAddresses.karmaAddress);
// Initialize Config struct
config.maxPublicMint =8;
config.ownerSupply =20;
config.preSaleLaunchTime =1639591200;
config.publicSaleLaunchTime =1639598400;
config.raribleTokenId =706480;
config.karmaRequirement =1020;
config.vrfCoordinator = contractAddresses.vrfCoordinator;
config.linkToken = contractAddresses.linkToken;
config.linkFee = _linkFee;
config.vrfKeyHash = _vrfKeyHash;
_owner =0x7F05F27CC5D83C3e879C53882de13Cc1cbCe8a8c;
}
functionowner() externalviewvirtualreturns (address) {
return _owner;
}
functionsetOwner(address owner_) externalonlyAdmin{
_owner = owner_;
}
functioncontractURI() publicpurereturns (stringmemory) {
return"https://www.seedphrase.codes/metadata/seedphrase-metadata.json";
}
functiongetVrfSeed() externalonlyAdminreturns (bytes32) {
require(!config.vrfNumberGenerated, "SP:VRF_ALREADY_CALLED");
require(LINK.balanceOf(address(this)) >= config.linkFee, "SP:NOT_ENOUGH_LINK");
return requestRandomness(config.vrfKeyHash, config.linkFee);
}
functionfulfillRandomness(bytes32, uint256 randomNumber) internaloverride{
config.vrfRandomValue = randomNumber;
config.vrfNumberGenerated =true;
}
function_getTokenSeed(uint256 tokenId) internalviewreturns (bytes32) {
return maps.tokenSeed[tokenId];
}
function_getBipWordIdFromTokenId(uint256 tokenId) internalviewreturns (uint16) {
return bipWordIds[tokenId -1];
}
functiontokenSVG(uint256 tokenId) publicviewvirtualreturns (stringmemory svg, bytesmemory) {
require(_exists(tokenId), "ERC721Metadata: URI query for nonexistent token");
SeedPhraseUtils.Random memory random = SeedPhraseUtils.Random({
seed: uint256(_getTokenSeed(tokenId)),
offsetBit: 0
});
uint16 bipWordId;
uint16 secondBipWordId =0;
uint8 rarityValue =0;
if (tokenId >=3000) {
uint256[2] memory tokens = maps.burnedTokensPairings[tokenId];
bipWordId = _getBipWordIdFromTokenId(tokens[0]);
secondBipWordId = _getBipWordIdFromTokenId(tokens[1]);
rarityValue = maps.doubleTokenRarity[tokenId];
} else {
bipWordId = _getBipWordIdFromTokenId(tokenId);
}
(bytesmemory traits, SeedPhraseUtils.Attrs memory attributes) = SeedPhraseUtils.getTraitsAndAttributes(
bipWordId,
secondBipWordId,
rarityValue,
random
);
return (SeedPhraseUtils.render(random, attributes), traits);
}
functiontokenURI(uint256 tokenId) publicviewvirtualoverridereturns (stringmemory) {
require(_exists(tokenId), "ERC721Metadata: URI query for nonexistent token");
(stringmemory output, bytesmemory traits) = tokenSVG(tokenId);
return SeedPhraseUtils.getTokenURI(output, traits, tokenId);
}
/**
Updates the presale state for n holders
*/functionsetPreSaleState(bool _preSaleActiveState) externalonlyAdmin{
config.preSaleActive = _preSaleActiveState;
}
/**
Updates the public sale state for non-n holders
*/functionsetPublicSaleState(bool _publicSaleActiveState) externalonlyAdmin{
config.publicSaleActive = _publicSaleActiveState;
}
functionsetPreSaleTime(uint32 _time) externalonlyAdmin{
config.preSaleLaunchTime = _time;
}
functionsetPublicSaleTime(uint32 _time) externalonlyAdmin{
config.publicSaleLaunchTime = _time;
}
/**
Give the ability to halt the sale if necessary due to automatic sale enablement based on time
*/functionsetSaleHaltedState(bool _saleHaltedState) externalonlyAdmin{
config.isSaleHalted = _saleHaltedState;
}
functionsetBurnActiveState(bool _burnActiveState) externalonlyAdmin{
config.isBurnActive = _burnActiveState;
}
functionsetGenesisSketchAllowList(address[] calldata addresses) externalonlyAdmin{
genesisSketchAddresses = addresses;
}
functionsetOgAllowList(address[] calldata addresses) externalonlyAdmin{
for (uint256 i =0; i < addresses.length; i++) {
maps.ogAddresses[addresses[i]] =true;
}
}
function_isPreSaleActive() internalviewreturns (bool) {
return ((block.timestamp>= config.preSaleLaunchTime || config.preSaleActive) &&!config.isSaleHalted);
}
function_isPublicSaleActive() internalviewoverridereturns (bool) {
return ((block.timestamp>= config.publicSaleLaunchTime || config.publicSaleActive) &&!config.isSaleHalted);
}
function_canMintPresale(address addr,
uint256 amount,
bytesmemory data
) internalviewoverridereturns (bool, PreSaleType) {
if (maps.ogAddresses[addr] && presaleLimits[PreSaleType.OG] - amount >=0) {
return (true, PreSaleType.OG);
}
bool isGsHolder;
for (uint256 i =0; i < genesisSketchAddresses.length; i++) {
if (genesisSketchAddresses[i] == addr) {
isGsHolder =true;
}
}
if (isGsHolder && presaleLimits[PreSaleType.GenesisSketch] - amount >=0) {
return (true, PreSaleType.GenesisSketch);
}
if (rarible.balanceOf(addr, config.raribleTokenId) >0&& presaleLimits[PreSaleType.GM] - amount >0) {
return (true, PreSaleType.GM);
}
uint256 karmaScore = SeedPhraseUtils.getKarma(karma, data, addr);
if (nOwnerResolver.balanceOf(addr) >0) {
if (karmaScore >= config.karmaRequirement && presaleLimits[PreSaleType.Karma] - amount >=0) {
return (true, PreSaleType.Karma);
}
if (presaleLimits[PreSaleType.N] - amount >=0) {
return (true, PreSaleType.N);
}
}
return (false, PreSaleType.None);
}
functioncanMint(address account, bytescalldata data) publicviewvirtualoverridereturns (bool) {
if (config.isBurnActive) {
returnfalse;
}
uint256 balance = balanceOf(account);
if (_isPublicSaleActive() && (totalMintsAvailable() >0) && balance < config.maxPublicMint) {
returntrue;
}
if (_isPreSaleActive()) {
(bool preSaleEligible, ) = _canMintPresale(account, 1, data);
return preSaleEligible;
}
returnfalse;
}
/**
* @notice Allow a n token holder to bulk mint tokens with id of their n tokens' id
* @param recipient Recipient of the mint
* @param tokenIds Ids to be minted
* @param paid Amount paid for the mint
*/functionmintWithN(address recipient,
uint256[] calldata tokenIds,
uint256 paid,
bytescalldata data
) publicvirtualoverridenonReentrant{
uint256 maxTokensToMint = tokenIds.length;
(bool preSaleEligible, PreSaleType presaleType) = _canMintPresale(recipient, maxTokensToMint, data);
require(config.bipWordsShuffled && config.vrfNumberGenerated, "SP:ENV_NOT_INIT");
require(_isPublicSaleActive() || (_isPreSaleActive() && preSaleEligible), "SP:SALE_NOT_ACTIVE");
require(
balanceOf(recipient) + maxTokensToMint <= _getMaxMintPerWallet(),
"NilPass:MINT_ABOVE_MAX_MINT_ALLOWANCE"
);
require(!config.isBurnActive, "SP:SALE_OVER");
require(totalSupply() + maxTokensToMint <= params.maxTotalSupply, "NilPass:MAX_ALLOCATION_REACHED");
uint256 price = getNextPriceForNHoldersInWei(maxTokensToMint, recipient, data);
require(paid == price, "NilPass:INVALID_PRICE");
for (uint256 i =0; i < maxTokensToMint; i++) {
require(!nUsed[tokenIds[i]], "SP:N_ALREADY_USED");
uint256 tokenId = _tokenIds.current();
require(tokenId <= params.maxTotalSupply, "SP:TOKEN_TOO_HIGH");
maps.tokenSeed[tokenId] = SeedPhraseUtils.generateSeed(tokenId, config.vrfRandomValue);
_safeMint(recipient, tokenId);
_tokenIds.increment();
nUsed[tokenIds[i]] =true;
}
if (preSaleEligible) {
presaleLimits[presaleType] -=uint16(maxTokensToMint);
}
}
/**
* @notice Allow anyone to mint a token with the supply id if this pass is unrestricted.
* n token holders can use this function without using the n token holders allowance,
* this is useful when the allowance is fully utilized.
* @param recipient Recipient of the mint
* @param amount Amount of tokens to mint
* @param paid Amount paid for the mint
*/functionmint(address recipient,
uint8 amount,
uint256 paid,
bytescalldata data
) publicvirtualoverridenonReentrant{
(bool preSaleEligible, PreSaleType presaleType) = _canMintPresale(recipient, amount, data);
require(config.bipWordsShuffled && config.vrfNumberGenerated, "SP:ENV_NOT_INIT");
require(
_isPublicSaleActive() ||
(_isPreSaleActive() &&
preSaleEligible &&
(presaleType != PreSaleType.N && presaleType != PreSaleType.Karma)),
"SP:SALE_NOT_ACTIVE"
);
require(!config.isBurnActive, "SP:SALE_OVER");
require(balanceOf(recipient) + amount <= _getMaxMintPerWallet(), "NilPass:MINT_ABOVE_MAX_MINT_ALLOWANCE");
require(totalSupply() + amount <= params.maxTotalSupply, "NilPass:MAX_ALLOCATION_REACHED");
uint256 price = getNextPriceForOpenMintInWei(amount, recipient, data);
require(paid == price, "NilPass:INVALID_PRICE");
for (uint256 i =0; i < amount; i++) {
uint256 tokenId = _tokenIds.current();
require(tokenId <= params.maxTotalSupply, "SP:TOKEN_TOO_HIGH");
maps.tokenSeed[tokenId] = SeedPhraseUtils.generateSeed(tokenId, config.vrfRandomValue);
_safeMint(recipient, tokenId);
_tokenIds.increment();
}
if (preSaleEligible) {
presaleLimits[presaleType] -= amount;
}
}
functionmintOwnerSupply(address account) publicnonReentrantonlyAdmin{
require(!config.isOwnerSupplyMinted, "SP:ALREADY_MINTED");
require(config.bipWordsShuffled && config.vrfNumberGenerated, "SP:ENV_NOT_INIT");
require(
totalSupply() + config.ownerSupply <= params.maxTotalSupply,
"NilPass:MAX_ALLOCATION_REACHED"
);
for (uint256 i =0; i < config.ownerSupply; i++) {
uint256 tokenId = _tokenIds.current();
maps.tokenSeed[tokenId] = SeedPhraseUtils.generateSeed(tokenId, config.vrfRandomValue);
_mint(account, tokenId);
_tokenIds.increment();
}
config.isOwnerSupplyMinted =true;
}
/**
* @notice Allow anyone to burn two single panels they own in order to mint
* a double paneled token.
* @param firstTokenId Token ID of the first token
* @param secondTokenId Token ID of the second token
*/functionburnForDoublePanel(uint256 firstTokenId, uint256 secondTokenId) publicnonReentrant{
require(config.isBurnActive, "SP:BURN_INACTIVE");
require(ownerOf(firstTokenId) ==msg.sender&& ownerOf(secondTokenId) ==msg.sender, "SP:INCORRECT_OWNER");
require(firstTokenId != secondTokenId, "SP:EQUAL_TOKENS");
// Ensure two owned tokens are in Burnable token pairingsrequire(
isValidPairing(_getBipWordIdFromTokenId(firstTokenId), _getBipWordIdFromTokenId(secondTokenId)),
"SP:INVALID_TOKEN_PAIRING"
);
_burn(firstTokenId);
_burn(secondTokenId);
// Any Token ID of 3000 or greater indicates it is a double panel e.g. 3000, 3001, 3002...uint256 doublePanelTokenId =3000+ _doublePanelTokens.current();
maps.tokenSeed[doublePanelTokenId] = SeedPhraseUtils.generateSeed(doublePanelTokenId, config.vrfRandomValue);
// Get the rarity rating from the burned tokens, store this against the new token// Burners are guaranteed their previous strongest trait (at least, could be rarer)uint8 rarity1 = SeedPhraseUtils.getRarityRating(_getTokenSeed(firstTokenId));
uint8 rarity2 = SeedPhraseUtils.getRarityRating(_getTokenSeed(secondTokenId));
maps.doubleTokenRarity[doublePanelTokenId] = (rarity1 > rarity2 ? rarity1 : rarity2);
_mint(msg.sender, doublePanelTokenId);
// Add burned tokens to maps.burnedTokensPairings mapping so we can use them to render the double panels later
maps.burnedTokensPairings[doublePanelTokenId] = [firstTokenId, secondTokenId];
_doublePanelTokens.increment();
emit Burnt(msg.sender, firstTokenId, secondTokenId, doublePanelTokenId);
}
functionairdropGenesisSketch() publicnonReentrantonlyAdmin{
require(!config.isGsAirdropComplete, "SP:ALREADY_AIRDROPPED");
require(config.bipWordsShuffled && config.vrfNumberGenerated, "SP:ENV_NOT_INIT");
uint256 airdropAmount = genesisSketchAddresses.length;
require(totalSupply() + airdropAmount <= params.maxTotalSupply, "NilPass:MAX_ALLOCATION_REACHED");
for (uint256 i =0; i < airdropAmount; i++) {
uint256 tokenId = _tokenIds.current();
maps.tokenSeed[tokenId] = SeedPhraseUtils.generateSeed(tokenId, config.vrfRandomValue);
_mint(genesisSketchAddresses[i], tokenId);
_tokenIds.increment();
}
config.isGsAirdropComplete =true;
}
functionmintOrphanedPieces(uint256 amount, address addr) publicnonReentrantonlyAdmin{
require(totalMintsAvailable() ==0, "SP:MINT_NOT_OVER");
// _tokenIds - 1 to get the current number of minted tokens (token IDs start at 1)uint256 currentSupply = _tokenIds.current() -1;
config.doubleBurnTokens = derivativeParams.maxTotalSupply - currentSupply;
require(config.doubleBurnTokens >= amount, "SP:NOT_ENOUGH_ORPHANS");
require(currentSupply + amount <= params.maxTotalSupply, "NilPass:MAX_ALLOCATION_REACHED");
for (uint256 i =0; i < amount; i++) {
uint256 tokenId = _tokenIds.current();
require(tokenId <= params.maxTotalSupply, "SP:TOKEN_TOO_HIGH");
maps.tokenSeed[tokenId] = SeedPhraseUtils.generateSeed(tokenId, config.vrfRandomValue);
_mint(addr, tokenId);
_tokenIds.increment();
}
config.doubleBurnTokens -= amount;
}
/**
* @notice Calculate the total available number of mints
* @return total mint available
*/functiontotalMintsAvailable() publicviewoverridereturns (uint256) {
uint256 totalAvailable = derivativeParams.maxTotalSupply - totalSupply();
if (block.timestamp> config.publicSaleLaunchTime +18hours) {
// Double candle burning starts and decreases max. mintable supply with 1 token per minute.uint256 doubleBurn = (block.timestamp- (config.publicSaleLaunchTime +18hours)) /1minutes;
totalAvailable = totalAvailable > doubleBurn ? totalAvailable - doubleBurn : 0;
}
return totalAvailable;
}
functiongetDoubleBurnedTokens() externalviewreturns (uint256) {
return config.doubleBurnTokens;
}
function_getMaxMintPerWallet() internalviewreturns (uint128) {
return _isPublicSaleActive() ? config.maxPublicMint : params.maxMintAllowance;
}
functionisValidPairing(uint16 first, uint16 second) publicviewreturns (bool) {
return maps.doubleWordPairings[first] == second;
}
functionamendPairings(uint16[][] calldata pairings) externalonlyAdmin{
for (uint16 i =0; i < pairings.length; i++) {
if (pairings[i].length!=2) {
continue;
}
maps.doubleWordPairings[pairings[i][0]] = pairings[i][1];
}
}
functionshuffleBipWords() externalonlyAdmin{
require(config.vrfNumberGenerated, "SP:VRF_NOT_CALLED");
require(!config.bipWordsShuffled, "SP:ALREADY_SHUFFLED");
bipWordIds = SeedPhraseUtils.shuffleBipWords(config.vrfRandomValue);
config.bipWordsShuffled =true;
}
}
Contract Source Code
File 26 of 30: SeedPhrasePricing.sol
// SPDX-License-Identifier: MITpragmasolidity >=0.8.4;import"../core/NilPassCore.sol";
abstractcontractSeedPhrasePricingisNilPassCore{
uint256 preSalePrice;
uint256 publicSalePrice;
constructor(stringmemory name,
stringmemory symbol,
IN n,
DerivativeParameters memory derivativeParams,
uint256 preSalePrice_,
uint256 publicSalePrice_,
address masterMint,
address dao
) NilPassCore(name, symbol, n, derivativeParams, masterMint, dao) {
preSalePrice = preSalePrice_;
publicSalePrice = publicSalePrice_;
}
enumPreSaleType {
GenesisSketch,
OG,
GM,
Karma,
N,
None
}
function_canMintPresale(address addr,
uint256 amount,
bytesmemory data
) internalviewvirtualreturns (bool, PreSaleType);
function_isPublicSaleActive() internalviewvirtualreturns (bool);
/**
* @notice Returns the next price for an N mint
*/functiongetNextPriceForNHoldersInWei(uint256 numberOfMints,
address account,
bytesmemory data
) publicviewoverridereturns (uint256) {
(bool preSaleEligible, ) = _canMintPresale(account, numberOfMints, data);
uint256 price = preSaleEligible &&!_isPublicSaleActive() ? preSalePrice : publicSalePrice;
return numberOfMints * price;
}
/**
* @notice Returns the next price for an open mint
*/functiongetNextPriceForOpenMintInWei(uint256 numberOfMints,
address account,
bytesmemory data
) publicviewoverridereturns (uint256) {
(bool preSaleEligible, ) = _canMintPresale(account, numberOfMints, data);
uint256 price = preSaleEligible &&!_isPublicSaleActive() ? preSalePrice : publicSalePrice;
return numberOfMints * price;
}
}
Contract Source Code
File 27 of 30: SeedPhraseUtils.sol
// SPDX-License-Identifier: MITpragmasolidity >=0.8.4;import"@openzeppelin/contracts/utils/Strings.sol";
import"../interfaces/IKarmaScore.sol";
import"./NilProtocolUtils.sol";
import"../libraries/NilProtocolUtils.sol";
librarySeedPhraseUtils{
usingStringsforuint256;
usingStringsforuint16;
usingStringsforuint8;
structRandom {
uint256 seed;
uint256 offsetBit;
}
structColors {
string background;
string panel;
string panel2;
string panelStroke;
string selectedCircleStroke;
string selectedCircleFill;
string selectedCircleFill2;
string negativeCircleStroke;
string negativeCircleFill;
string blackOrWhite;
string dynamicOpacity;
string backgroundCircle;
}
structAttrs {
bool showStroke;
bool border;
bool showPanel;
bool backgroundSquare;
bool bigBackgroundCircle;
bool showGrid;
bool backgroundCircles;
bool greyscale;
bool doublePanel;
uint16 bipWordId;
uint16 secondBipWordId;
}
uint8internalconstant strokeWeight =7;
uint16internalconstant segmentSize =100;
uint16internalconstant radius =50;
uint16internalconstant padding =10;
uint16internalconstant viewBox =1600;
uint16internalconstant panelWidth = segmentSize *4;
uint16internalconstant panelHeight = segmentSize *10;
uint16internalconstant singlePanelX = (segmentSize *6);
uint16internalconstant doublePanel1X = (segmentSize *3);
uint16internalconstant doublePanel2X = doublePanel1X + (segmentSize *6);
uint16internalconstant panelY = (segmentSize *3);
functiongenerateSeed(uint256 tokenId, uint256 vrfRandomValue) externalviewreturns (bytes32) {
returnkeccak256(abi.encode(tokenId, block.timestamp, block.difficulty, vrfRandomValue));
}
function_shouldAddTrait(bool isTrue,
bytesmemory trueName,
bytesmemory falseName,
uint8 prevRank,
uint8 newRank,
bytesmemory traits
) internalpurereturns (bytesmemory, uint8) {
if (isTrue) {
traits =abi.encodePacked(traits, ',{"value": "', trueName, '"}');
}
// Only add the falsy trait if it's named (e.g. there's no negative version of "greyscale")elseif (falseName.length!=0) {
traits =abi.encodePacked(traits, ',{"value": "', falseName, '"}');
}
// Return new (higher rank if trait is true)return (traits, (isTrue ? newRank : prevRank));
}
functiontokenTraits(Attrs memory attributes) internalpurereturns (bytesmemory traits, uint8 rarityRating) {
rarityRating =0;
traits =abi.encodePacked("[");
// Add both words to trait if a double panelif (attributes.doublePanel) {
traits =abi.encodePacked(
traits,
'{"trait_type": "Double Panel BIP39 IDs", "value": "',
attributes.bipWordId.toString(),
" - ",
attributes.secondBipWordId.toString(),
'"},',
'{"value": "Double Panel"}'
);
} else {
traits =abi.encodePacked(
traits,
'{"trait_type": "BIP39 ID", "display_type": "number", "max_value": 2048, "value": ',
attributes.bipWordId.toString(),
"}"
);
}
// Stroke trait - rank 1
(traits, rarityRating) = _shouldAddTrait(
!attributes.showStroke,
"No Stroke",
"OG Stroke",
rarityRating,
1,
traits
);
// Border - rank 2
(traits, rarityRating) = _shouldAddTrait(attributes.border, "Border", "", rarityRating, 2, traits);
// No Panel - rank 3
(traits, rarityRating) = _shouldAddTrait(
!attributes.showPanel,
"No Panel",
"OG Panel",
rarityRating,
3,
traits
);
// Symmetry Group Square - rank 4
(traits, rarityRating) = _shouldAddTrait(
attributes.backgroundSquare,
"Group Square",
"",
rarityRating,
4,
traits
);
// Symmetry Group Circle - rank 5
(traits, rarityRating) = _shouldAddTrait(
attributes.bigBackgroundCircle,
"Group Circle",
"",
rarityRating,
5,
traits
);
// Caged - rank 6
(traits, rarityRating) = _shouldAddTrait(attributes.showGrid, "Caged", "", rarityRating, 6, traits);
// Bubblewrap - rank 7
(traits, rarityRating) = _shouldAddTrait(
attributes.backgroundCircles,
"Bubblewrap",
"",
rarityRating,
7,
traits
);
// Monochrome - rank 8
(traits, rarityRating) = _shouldAddTrait(attributes.greyscale, "Monochrome", "", rarityRating, 8, traits);
traits =abi.encodePacked(traits, "]");
}
/**
* @notice Generates the art defining attributes
* @param bipWordId bip39 word id
* @param secondBipWordId ^ only for a double panel
* @param random RNG
* @param predefinedRarity double panels trait to carry over
* @return attributes struct
*/functiontokenAttributes(uint16 bipWordId,
uint16 secondBipWordId,
Random memory random,
uint8 predefinedRarity
) internalpurereturns (Attrs memory attributes) {
attributes = Attrs({
showStroke: (predefinedRarity ==1) ? false : _boolPercentage(random, 70), // rank 1
border: (predefinedRarity ==2) ? true : _boolPercentage(random, 30), // rank 2
showPanel: (predefinedRarity ==3) ? false : _boolPercentage(random, 80), // rank 3
backgroundSquare: (predefinedRarity ==4) ? true : _boolPercentage(random, 18), // rank 4
bigBackgroundCircle: (predefinedRarity ==5) ? true : _boolPercentage(random, 12), // rank = 5
showGrid: (predefinedRarity ==6) ? true : _boolPercentage(random, 6), // rank 6
backgroundCircles: (predefinedRarity ==7) ? true : _boolPercentage(random, 4), // rank 7
greyscale: (predefinedRarity ==8) ? true : _boolPercentage(random, 2), // rank 8
bipWordId: bipWordId,
doublePanel: (secondBipWordId >0),
secondBipWordId: secondBipWordId
});
// Rare attributes should always superseed less-rare// If greyscale OR grid is true then turn on stroke (as it is required)if (attributes.showGrid || attributes.greyscale) {
attributes.showStroke =true;
}
// backgroundCircles superseeds grid (they cannot co-exist)if (attributes.backgroundCircles) {
attributes.showGrid =false;
}
// Border cannot be on if background shapes are turned onif (attributes.bigBackgroundCircle || attributes.backgroundSquare) {
attributes.border =false;
// Big Background Shapes cannot co-existif (attributes.bigBackgroundCircle) {
attributes.backgroundSquare =false;
}
}
}
/**
* @notice Converts a tokenId (uint256) into the formats needed to generate the art
* @param tokenId tokenId (also the BIP39 word)
* @return tokenArray with prepended 0's (if tokenId is less that 4 digits) also returns in string format
*/function_transformTokenId(uint256 tokenId) internalpurereturns (uint8[4] memory tokenArray, stringmemory) {
bytesmemory tokenString;
uint8 digit;
for (int8 i =3; i >=0; i--) {
digit =uint8(tokenId %10); // This returns the final digit in the tokenif (tokenId >0) {
tokenId = tokenId /10; // this removes the last digit from the token as we've grabbed the digit already
tokenArray[uint8(i)] = digit;
}
tokenString =abi.encodePacked(digit.toString(), tokenString);
}
return (tokenArray, string(tokenString));
}
function_renderText(stringmemory text, stringmemory color) internalpurereturns (bytesmemory svg) {
svg =abi.encodePacked(
"<text x='1500' y='1500' text-anchor='end' style='font:700 36px "Courier New";fill:",
color,
";opacity:.4'>#",
text,
"</text>"
);
return svg;
}
function_backgroundShapeSizing(Random memory random, Attrs memory attributes)
internalpurereturns (uint16, uint16)
{
uint256 idx;
// If we DON'T have a 'doublePanel' or 'no panel' we can return the default sizingif (!attributes.doublePanel && attributes.showPanel) {
uint16[2][6] memory defaultSizing = [
[1275, 200],
[1150, 375],
[900, 300],
[925, 225],
[850, 150],
[775, 125]
];
idx = SeedPhraseUtils._next(random, 0, defaultSizing.length);
return (defaultSizing[idx][0], defaultSizing[idx][1]);
}
// Otherwise we need to return some slightly different dataif (attributes.bigBackgroundCircle) {
uint16[2][4] memory restrictedCircleDimensions = [[1150, 150], [1275, 200], [1300, 100], [1350, 200]];
idx = SeedPhraseUtils._next(random, 0, restrictedCircleDimensions.length);
return (restrictedCircleDimensions[idx][0], restrictedCircleDimensions[idx][1]);
}
// Else we can assume that it is backgroundSquaresuint16[2][4] memory restrictedSquareDimensions = [[1150, 50], [1100, 125], [1275, 200], [1300, 150]];
idx = SeedPhraseUtils._next(random, 0, restrictedSquareDimensions.length);
return (restrictedSquareDimensions[idx][0], restrictedSquareDimensions[idx][1]);
}
function_getStrokeStyle(bool showStroke,
stringmemory color,
stringmemory opacity,
uint8 customStrokeWeight
) internalpurereturns (bytesmemory strokeStyle) {
if (showStroke) {
strokeStyle =abi.encodePacked(
" style='stroke-opacity:",
opacity,
";stroke:",
color,
";stroke-width:",
customStrokeWeight.toString(),
"' "
);
return strokeStyle;
}
}
function_getPalette(Random memory random, Attrs memory attributes) internalpurereturns (Colors memory) {
string[6] memory selectedPallet;
uint8[6] memory lumosity;
if (attributes.greyscale) {
selectedPallet = ["#f8f9fa", "#c3c4c4", "#909091", "#606061", "#343435", "#0a0a0b"];
lumosity = [249, 196, 144, 96, 52, 10];
} else {
uint256 randPalette = SeedPhraseUtils._next(random, 0, 25);
if (randPalette ==0) {
selectedPallet = ["#ffe74c", "#ff5964", "#ffffff", "#6bf178", "#35a7ff", "#5b3758"];
lumosity = [225, 125, 255, 204, 149, 65];
} elseif (randPalette ==1) {
selectedPallet = ["#ff0000", "#ff8700", "#e4ff33", "#a9ff1f", "#0aefff", "#0a33ff"];
lumosity = [54, 151, 235, 221, 191, 57];
} elseif (randPalette ==2) {
selectedPallet = ["#f433ab", "#cb04a5", "#934683", "#65334d", "#2d1115", "#e0e2db"];
lumosity = [101, 58, 91, 64, 23, 225];
} elseif (randPalette ==3) {
selectedPallet = ["#f08700", "#f6aa28", "#f9d939", "#00a6a6", "#bbdef0", "#23556c"];
lumosity = [148, 177, 212, 131, 216, 76];
} elseif (randPalette ==4) {
selectedPallet = ["#f7e6de", "#e5b59e", "#cb7d52", "#bb8f77", "#96624a", "#462b20"];
lumosity = [233, 190, 138, 151, 107, 48];
} elseif (randPalette ==5) {
selectedPallet = ["#f61379", "#d91cbc", "#da81ee", "#5011e4", "#4393ef", "#8edef6"];
lumosity = [75, 80, 156, 46, 137, 207];
} elseif (randPalette ==6) {
selectedPallet = ["#010228", "#006aa3", "#005566", "#2ac1df", "#82dded", "#dbf5fa"];
lumosity = [5, 88, 68, 163, 203, 240];
} elseif (randPalette ==7) {
selectedPallet = ["#f46036", "#5b85aa", "#414770", "#372248", "#171123", "#f7f5fb"];
lumosity = [124, 127, 73, 41, 20, 246];
} elseif (randPalette ==8) {
selectedPallet = ["#393d3f", "#fdfdff", "#c6c5b9", "#62929e", "#546a7b", "#c52233"];
lumosity = [60, 253, 196, 137, 103, 70];
} elseif (randPalette ==9) {
selectedPallet = ["#002626", "#0e4749", "#95c623", "#e55812", "#efe7da", "#8ddbe0"];
lumosity = [30, 59, 176, 113, 232, 203];
} elseif (randPalette ==10) {
selectedPallet = ["#03071e", "#62040d", "#d00000", "#e85d04", "#faa307", "#ffcb47"];
lumosity = [8, 25, 44, 116, 170, 205];
} elseif (randPalette ==11) {
selectedPallet = ["#f56a00", "#ff931f", "#ffd085", "#20003d", "#7b2cbf", "#c698eb"];
lumosity = [128, 162, 213, 11, 71, 168];
} elseif (randPalette ==12) {
selectedPallet = ["#800016", "#ffffff", "#ff002b", "#407ba7", "#004e89", "#00043a"];
lumosity = [29, 255, 57, 114, 66, 7];
} elseif (randPalette ==13) {
selectedPallet = ["#d6d6d6", "#f9f7dc", "#ffee32", "#ffd100", "#202020", "#6c757d"];
lumosity = [214, 245, 228, 204, 32, 116];
} elseif (randPalette ==14) {
selectedPallet = ["#fff5d6", "#ccc5b9", "#403d39", "#252422", "#eb5e28", "#bb4111"];
lumosity = [245, 198, 61, 36, 120, 87];
} elseif (randPalette ==15) {
selectedPallet = ["#0c0f0a", "#ff206e", "#fbff12", "#41ead4", "#6c20fd", "#ffffff"];
lumosity = [14, 85, 237, 196, 224, 255];
} elseif (randPalette ==16) {
selectedPallet = ["#fdd8d8", "#f67979", "#e51010", "#921314", "#531315", "#151315"];
lumosity = [224, 148, 61, 46, 33, 20];
} elseif (randPalette ==17) {
selectedPallet = ["#000814", "#002752", "#0066cc", "#f5bc00", "#ffd60a", "#ffee99"];
lumosity = [7, 34, 88, 187, 208, 235];
} elseif (randPalette ==18) {
selectedPallet = ["#010b14", "#022d4f", "#fdfffc", "#2ec4b6", "#e71d36", "#ff990a"];
lumosity = [10, 38, 254, 163, 74, 164];
} elseif (randPalette ==19) {
selectedPallet = ["#fd650d", "#d90368", "#820263", "#291720", "#06efa9", "#0d5943"];
lumosity = [127, 56, 36, 27, 184, 71];
} elseif (randPalette ==20) {
selectedPallet = ["#002914", "#005200", "#34a300", "#70e000", "#aef33f", "#e0ff85"];
lumosity = [31, 59, 128, 184, 215, 240];
} elseif (randPalette ==21) {
selectedPallet = ["#001413", "#fafffe", "#6f0301", "#a92d04", "#f6b51d", "#168eb6"];
lumosity = [16, 254, 26, 68, 184, 119];
} elseif (randPalette ==22) {
selectedPallet = ["#6a1f10", "#d53e20", "#f7d1ca", "#c4f3fd", "#045362", "#fffbfa"];
lumosity = [46, 92, 217, 234, 67, 252];
} elseif (randPalette ==23) {
selectedPallet = ["#6b42ff", "#a270ff", "#dda1f7", "#ffd6eb", "#ff8fb2", "#f56674"];
lumosity = [88, 133, 180, 224, 169, 133];
} elseif (randPalette ==24) {
selectedPallet = ["#627132", "#273715", "#99a271", "#fefae1", "#e0a35c", "#bf6b21"];
lumosity = [105, 49, 157, 249, 171, 120];
}
}
// Randomize pallet order here...return _shufflePallet(random, selectedPallet, lumosity, attributes);
}
function_shufflePallet(
Random memory random,
string[6] memory hexColors,
uint8[6] memory lumaValues,
Attrs memory attributes
) internalpurereturns (Colors memory) {
// Shuffle colors and luma values with the same indexfor (uint8 i =0; i < hexColors.length; i++) {
// n = Pick random i > (array length - i)uint256 n = i + SeedPhraseUtils._next(random, 0, (hexColors.length- i));
// temp = Temporarily store value from array[n]stringmemory tempHex = hexColors[n];
uint8 tempLuma = lumaValues[n];
// Swap n value with i value
hexColors[n] = hexColors[i];
hexColors[i] = tempHex;
lumaValues[n] = lumaValues[i];
lumaValues[i] = tempLuma;
}
Colors memory pallet = Colors({
background: hexColors[0],
panel: hexColors[1],
panel2: "", // panel2 should match selected circles
panelStroke: hexColors[2],
selectedCircleStroke: hexColors[2], // Match panel stroke
negativeCircleStroke: hexColors[3],
negativeCircleFill: hexColors[4],
selectedCircleFill: hexColors[5],
selectedCircleFill2: "", // should match panel1
backgroundCircle: "",
blackOrWhite: lumaValues[0] <150 ? "#fff" : "#000",
dynamicOpacity: lumaValues[0] <150 ? "0.08" : "0.04"
});
if (attributes.doublePanel) {
pallet.panel2 = pallet.selectedCircleFill;
pallet.selectedCircleFill2 = pallet.panel;
}
if (attributes.bigBackgroundCircle) {
// Set background circle colors here
pallet.backgroundCircle = pallet.background;
pallet.background = pallet.panel;
// Luma based on 'new background', previous background is used for bgCircleColor)
pallet.blackOrWhite = lumaValues[1] <150 ? "#fff" : "#000";
pallet.dynamicOpacity = lumaValues[1] <150 ? "0.08" : "0.04";
}
return pallet;
}
/// @notice get an random number between (min and max) using seed and offseting bits/// this function assumes that max is never bigger than 0xffffff (hex color with opacity included)/// @dev this function is simply used to get random number using a seed./// if does bitshifting operations to try to reuse the same seed as much as possible./// should be enough for anyth/// @param random the randomizer/// @param min the minimum/// @param max the maximum/// @return result the resulting pseudo random numberfunction_next(
Random memory random,
uint256 min,
uint256 max
) internalpurereturns (uint256 result) {
uint256 newSeed = random.seed;
uint256 newOffset = random.offsetBit +3;
uint256 maxOffset =4;
uint256 mask =0xf;
if (max >0xfffff) {
mask =0xffffff;
maxOffset =24;
} elseif (max >0xffff) {
mask =0xfffff;
maxOffset =20;
} elseif (max >0xfff) {
mask =0xffff;
maxOffset =16;
} elseif (max >0xff) {
mask =0xfff;
maxOffset =12;
} elseif (max >0xf) {
mask =0xff;
maxOffset =8;
}
// if offsetBit is too high to get the max number// just get new seed and restart offset to 0if (newOffset > (256- maxOffset)) {
newOffset =0;
newSeed =uint256(keccak256(abi.encode(newSeed)));
}
uint256 offseted = (newSeed >> newOffset);
uint256 part = offseted & mask;
result = min + (part % (max - min));
random.seed = newSeed;
random.offsetBit = newOffset;
}
function_boolPercentage(Random memory random, uint256 percentage) internalpurereturns (bool) {
// E.G. If percentage = 30, and random = 0-29 we return true// Percentage = 1, random = 0 (TRUE)return (SeedPhraseUtils._next(random, 0, 100) < percentage);
}
/// @param random source of randomness (based on tokenSeed)/// @param attributes art attributes/// @return the jsonfunctionrender(SeedPhraseUtils.Random memory random, SeedPhraseUtils.Attrs memory attributes)
externalpurereturns (stringmemory)
{
// Get color pallet
SeedPhraseUtils.Colors memory pallet = SeedPhraseUtils._getPalette(random, attributes);
// Start SVG (viewbox & static patterns)bytesmemory svg =abi.encodePacked(
"<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 1600 1600'><path fill='",
pallet.background,
"' ",
SeedPhraseUtils._getStrokeStyle(attributes.border, pallet.blackOrWhite, "0.3", 50),
" d='M0 0h1600v1600H0z'/>",
" <pattern id='panelCircles' x='0' y='0' width='.25' height='.1' patternUnits='objectBoundingBox'>",
"<circle cx='50' cy='50' r='40' fill='",
pallet.negativeCircleFill,
"' ",
SeedPhraseUtils._getStrokeStyle(attributes.showStroke, pallet.negativeCircleStroke, "1", strokeWeight),
" /></pattern>"
);
// Render optional patterns (grid OR background circles)if (attributes.backgroundCircles) {
svg =abi.encodePacked(
svg,
"<pattern id='backgroundCircles' x='0' y='0' width='100' height='100'",
" patternUnits='userSpaceOnUse'><circle cx='50' cy='50' r='40' fill='",
pallet.blackOrWhite,
"' style='fill-opacity: ",
pallet.dynamicOpacity,
";'></circle></pattern><path fill='url(#backgroundCircles)' d='M0 0h1600v1600H0z'/>"
);
} elseif (attributes.showGrid) {
svg =abi.encodePacked(
svg,
"<pattern id='grid' x='0' y='0' width='100' height='100'",
" patternUnits='userSpaceOnUse'><rect x='0' y='0' width='100' height='100' fill='none' ",
SeedPhraseUtils._getStrokeStyle(true, pallet.blackOrWhite, pallet.dynamicOpacity, strokeWeight),
" /></pattern><path fill='url(#grid)' d='M0 0h1600v1600H0z'/>"
);
}
if (attributes.bigBackgroundCircle) {
(uint16 shapeSize, uint16 stroke) = SeedPhraseUtils._backgroundShapeSizing(random, attributes);
// uint16 centerCircle = (viewBox / 2); // Viewbox = 1600, Center = 800
svg =abi.encodePacked(
svg,
"<circle cx='800' cy='800' r='",
(shapeSize /2).toString(),
"' fill='",
pallet.backgroundCircle,
"' stroke='",
pallet.negativeCircleStroke,
"' style='stroke-width:",
stroke.toString(),
";stroke-opacity:0.3'/>"
);
} elseif (attributes.backgroundSquare) {
(uint16 shapeSize, uint16 stroke) = SeedPhraseUtils._backgroundShapeSizing(random, attributes);
uint16 centerSquare = ((viewBox - shapeSize) /2);
svg =abi.encodePacked(
svg,
"<rect x='",
centerSquare.toString(),
"' y='",
centerSquare.toString(),
"' width='",
shapeSize.toString(),
"' height='",
shapeSize.toString(),
"' fill='",
pallet.backgroundCircle,
"' stroke='",
pallet.negativeCircleStroke,
"' style='stroke-width:",
stroke.toString(),
";stroke-opacity:0.3'/>"
);
}
// Double panel (only if holder has burned two tokens from the defined pairings)if (attributes.doublePanel) {
(uint8[4] memory firstBipIndexArray, stringmemory firstBipIndexStr) = SeedPhraseUtils._transformTokenId(
attributes.bipWordId
);
(uint8[4] memory secondBipIndexArray, stringmemory secondBipIndexStr) = SeedPhraseUtils._transformTokenId(
attributes.secondBipWordId
);
svg =abi.encodePacked(
svg,
_renderSinglePanel(firstBipIndexArray, attributes, pallet, doublePanel1X, false),
_renderSinglePanel(secondBipIndexArray, attributes, pallet, doublePanel2X, true)
);
// Create textbytesmemory combinedText =abi.encodePacked(firstBipIndexStr, " - #", secondBipIndexStr);
svg =abi.encodePacked(
svg,
SeedPhraseUtils._renderText(string(combinedText), pallet.blackOrWhite),
"</svg>"
);
}
// Single Panelelse {
(uint8[4] memory bipIndexArray, stringmemory bipIndexStr) = SeedPhraseUtils._transformTokenId(
attributes.bipWordId
);
svg =abi.encodePacked(svg, _renderSinglePanel(bipIndexArray, attributes, pallet, singlePanelX, false));
// Add closing text and svg element
svg =abi.encodePacked(svg, SeedPhraseUtils._renderText(bipIndexStr, pallet.blackOrWhite), "</svg>");
}
returnstring(svg);
}
function_renderSinglePanel(uint8[4] memory bipIndexArray,
SeedPhraseUtils.Attrs memory attributes,
SeedPhraseUtils.Colors memory pallet,
uint16 panelX,
bool secondPanel
) internalpurereturns (bytesmemory panelSvg) {
// Draw panelsbool squareEdges = (attributes.doublePanel && attributes.backgroundSquare);
if (attributes.showPanel) {
panelSvg =abi.encodePacked(
"<rect x='",
(panelX - padding).toString(),
"' y='",
(panelY - padding).toString(),
"' width='",
(panelWidth + (padding *2)).toString(),
"' height='",
(panelHeight + (padding *2)).toString(),
"' rx='",
(squareEdges ? 0 : radius).toString(),
"' fill='",
(secondPanel ? pallet.panel2 : pallet.panel),
"' ",
SeedPhraseUtils._getStrokeStyle(attributes.showStroke, pallet.panelStroke, "1", strokeWeight),
"/>"
);
}
// Fill panel with negative circles, should resemble M600 300h400v1000H600z
panelSvg =abi.encodePacked(
panelSvg,
"<path fill='url(#panelCircles)' d='M",
panelX.toString(),
" ",
panelY.toString(),
"h",
panelWidth.toString(),
"v",
panelHeight.toString(),
"H",
panelX.toString(),
"z'/>"
);
// Draw selected circles
panelSvg =abi.encodePacked(
panelSvg,
_renderSelectedCircles(bipIndexArray, pallet, attributes.showStroke, panelX, secondPanel)
);
}
function_renderSelectedCircles(uint8[4] memory bipIndexArray,
SeedPhraseUtils.Colors memory pallet,
bool showStroke,
uint16 panelX,
bool secondPanel
) internalpurereturns (bytesmemory svg) {
for (uint8 i =0; i < bipIndexArray.length; i++) {
svg =abi.encodePacked(
svg,
"<circle cx='",
(panelX + (segmentSize * i) + radius).toString(),
"' cy='",
(panelY + (segmentSize * bipIndexArray[i]) + radius).toString(),
"' r='41' fill='", // Increase the size a tiny bit here (+1) to hide negative circle outline
(secondPanel ? pallet.selectedCircleFill2 : pallet.selectedCircleFill),
"' ",
SeedPhraseUtils._getStrokeStyle(showStroke, pallet.selectedCircleStroke, "1", strokeWeight),
" />"
);
}
}
functiongetRarityRating(bytes32 tokenSeed) externalpurereturns (uint8) {
SeedPhraseUtils.Random memory random = SeedPhraseUtils.Random({ seed: uint256(tokenSeed), offsetBit: 0 });
(, uint8 rarityRating) = SeedPhraseUtils.tokenTraits(SeedPhraseUtils.tokenAttributes(0, 0, random, 0));
return rarityRating;
}
functiongetTraitsAndAttributes(uint16 bipWordId,
uint16 secondBipWordId,
uint8 rarityValue,
SeedPhraseUtils.Random memory random
) externalpurereturns (bytesmemory, SeedPhraseUtils.Attrs memory) {
SeedPhraseUtils.Attrs memory attributes = SeedPhraseUtils.tokenAttributes(
bipWordId,
secondBipWordId,
random,
rarityValue
);
(bytesmemory traits, ) = SeedPhraseUtils.tokenTraits(attributes);
return (traits, attributes);
}
functiongetKarma(IKarmaScore karma, bytesmemory data, address account) externalviewreturns (uint256) {
if (data.length>0) {
(, uint256 karmaScore, ) =abi.decode(data, (address, uint256, bytes32[]));
if (karma.verify(account, karmaScore, data)) {
return account ==address(0) ? 1000 : karmaScore;
}
}
return1000;
}
functionshuffleBipWords(uint256 randomValue) externalpurereturns (uint16[] memory) {
uint16 size =2048;
uint16[] memory result =newuint16[](size);
// Initialize array.for (uint16 i =0; i < size; i++) {
result[i] = i +1;
}
// Set the initial randomness based on the provided entropy from VRF.bytes32 random =keccak256(abi.encodePacked(randomValue));
// Set the last item of the array which will be swapped.uint16 lastItem = size -1;
// We need to do `size - 1` iterations to completely shuffle the array.for (uint16 i =1; i < size -1; i++) {
// Select a number based on the randomness.uint16 selectedItem =uint16(uint256(random) % lastItem);
// Swap items `selected_item <> last_item`.
(result[lastItem], result[selectedItem]) = (result[selectedItem], result[lastItem]);
// Decrease the size of the possible shuffle// to preserve the already shuffled items.// The already shuffled items are at the end of the array.
lastItem--;
// Generate new randomness.
random =keccak256(abi.encodePacked(random));
}
return result;
}
functiongetDescriptionPt1() internalpurereturns (stringmemory) {
return"\"Seed Phrase is a 'Crypto Native' *fully* on-chain collection.\\n\\nA '*SEED*' is unique, it represents a single word from the BIP-0039 word list (the most commonly used word list to generate a seed/recovery phrase, think of it as a dictionary that only holds 2048 words).\\n\\n***Your 'SEED*' = *Your 'WORD*' in the list.** \\nClick [here](https://www.seedphrase.codes/token?id=";
}
functiongetDescriptionPt2() internalpurereturns (stringmemory) {
return") to decipher *your 'SEED*' and find out which word it translates to!\\n\\nFor Licensing, T&Cs or any other info, please visit: [www.seedphrase.codes](https://www.seedphrase.codes/).\"";
}
functiongetTokenURI(stringmemory output, bytesmemory traits, uint256 tokenId) externalpurereturns (stringmemory) {
returnstring(
abi.encodePacked(
"data:application/json;base64,",
NilProtocolUtils.base64encode(
bytes(
string(
abi.encodePacked(
'{"name": "Seed Phrase #',
NilProtocolUtils.stringify(tokenId),
'", "image": "data:image/svg+xml;base64,',
NilProtocolUtils.base64encode(bytes(output)),
'", "attributes": ',
traits,
', "description": ',
getDescriptionPt1(),
tokenId.toString(),
getDescriptionPt2(),
"}"
)
)
)
)
)
);
}
}
Contract Source Code
File 28 of 30: Strings.sol
// SPDX-License-Identifier: MITpragmasolidity ^0.8.0;/**
* @dev String operations.
*/libraryStrings{
bytes16privateconstant _HEX_SYMBOLS ="0123456789abcdef";
/**
* @dev Converts a `uint256` to its ASCII `string` decimal representation.
*/functiontoString(uint256 value) internalpurereturns (stringmemory) {
// Inspired by OraclizeAPI's implementation - MIT licence// https://github.com/oraclize/ethereum-api/blob/b42146b063c7d6ee1358846c198246239e9360e8/oraclizeAPI_0.4.25.solif (value ==0) {
return"0";
}
uint256 temp = value;
uint256 digits;
while (temp !=0) {
digits++;
temp /=10;
}
bytesmemory buffer =newbytes(digits);
while (value !=0) {
digits -=1;
buffer[digits] =bytes1(uint8(48+uint256(value %10)));
value /=10;
}
returnstring(buffer);
}
/**
* @dev Converts a `uint256` to its ASCII `string` hexadecimal representation.
*/functiontoHexString(uint256 value) internalpurereturns (stringmemory) {
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.
*/functiontoHexString(uint256 value, uint256 length) internalpurereturns (stringmemory) {
bytesmemory buffer =newbytes(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");
returnstring(buffer);
}
}
Contract Source Code
File 29 of 30: VRFConsumerBase.sol
// SPDX-License-Identifier: MITpragmasolidity ^0.8.0;import"./interfaces/LinkTokenInterface.sol";
import"./VRFRequestIDBase.sol";
/** ****************************************************************************
* @notice Interface for contracts using VRF randomness
* *****************************************************************************
* @dev PURPOSE
*
* @dev Reggie the Random Oracle (not his real job) wants to provide randomness
* @dev to Vera the verifier in such a way that Vera can be sure he's not
* @dev making his output up to suit himself. Reggie provides Vera a public key
* @dev to which he knows the secret key. Each time Vera provides a seed to
* @dev Reggie, he gives back a value which is computed completely
* @dev deterministically from the seed and the secret key.
*
* @dev Reggie provides a proof by which Vera can verify that the output was
* @dev correctly computed once Reggie tells it to her, but without that proof,
* @dev the output is indistinguishable to her from a uniform random sample
* @dev from the output space.
*
* @dev The purpose of this contract is to make it easy for unrelated contracts
* @dev to talk to Vera the verifier about the work Reggie is doing, to provide
* @dev simple access to a verifiable source of randomness.
* *****************************************************************************
* @dev USAGE
*
* @dev Calling contracts must inherit from VRFConsumerBase, and can
* @dev initialize VRFConsumerBase's attributes in their constructor as
* @dev shown:
*
* @dev contract VRFConsumer {
* @dev constuctor(<other arguments>, address _vrfCoordinator, address _link)
* @dev VRFConsumerBase(_vrfCoordinator, _link) public {
* @dev <initialization with other arguments goes here>
* @dev }
* @dev }
*
* @dev The oracle will have given you an ID for the VRF keypair they have
* @dev committed to (let's call it keyHash), and have told you the minimum LINK
* @dev price for VRF service. Make sure your contract has sufficient LINK, and
* @dev call requestRandomness(keyHash, fee, seed), where seed is the input you
* @dev want to generate randomness from.
*
* @dev Once the VRFCoordinator has received and validated the oracle's response
* @dev to your request, it will call your contract's fulfillRandomness method.
*
* @dev The randomness argument to fulfillRandomness is the actual random value
* @dev generated from your seed.
*
* @dev The requestId argument is generated from the keyHash and the seed by
* @dev makeRequestId(keyHash, seed). If your contract could have concurrent
* @dev requests open, you can use the requestId to track which seed is
* @dev associated with which randomness. See VRFRequestIDBase.sol for more
* @dev details. (See "SECURITY CONSIDERATIONS" for principles to keep in mind,
* @dev if your contract could have multiple requests in flight simultaneously.)
*
* @dev Colliding `requestId`s are cryptographically impossible as long as seeds
* @dev differ. (Which is critical to making unpredictable randomness! See the
* @dev next section.)
*
* *****************************************************************************
* @dev SECURITY CONSIDERATIONS
*
* @dev A method with the ability to call your fulfillRandomness method directly
* @dev could spoof a VRF response with any random value, so it's critical that
* @dev it cannot be directly called by anything other than this base contract
* @dev (specifically, by the VRFConsumerBase.rawFulfillRandomness method).
*
* @dev For your users to trust that your contract's random behavior is free
* @dev from malicious interference, it's best if you can write it so that all
* @dev behaviors implied by a VRF response are executed *during* your
* @dev fulfillRandomness method. If your contract must store the response (or
* @dev anything derived from it) and use it later, you must ensure that any
* @dev user-significant behavior which depends on that stored value cannot be
* @dev manipulated by a subsequent VRF request.
*
* @dev Similarly, both miners and the VRF oracle itself have some influence
* @dev over the order in which VRF responses appear on the blockchain, so if
* @dev your contract could have multiple VRF requests in flight simultaneously,
* @dev you must ensure that the order in which the VRF responses arrive cannot
* @dev be used to manipulate your contract's user-significant behavior.
*
* @dev Since the ultimate input to the VRF is mixed with the block hash of the
* @dev block in which the request is made, user-provided seeds have no impact
* @dev on its economic security properties. They are only included for API
* @dev compatability with previous versions of this contract.
*
* @dev Since the block hash of the block which contains the requestRandomness
* @dev call is mixed into the input to the VRF *last*, a sufficiently powerful
* @dev miner could, in principle, fork the blockchain to evict the block
* @dev containing the request, forcing the request to be included in a
* @dev different block with a different hash, and therefore a different input
* @dev to the VRF. However, such an attack would incur a substantial economic
* @dev cost. This cost scales with the number of blocks the VRF oracle waits
* @dev until it calls responds to a request.
*/abstractcontractVRFConsumerBaseisVRFRequestIDBase{
/**
* @notice fulfillRandomness handles the VRF response. Your contract must
* @notice implement it. See "SECURITY CONSIDERATIONS" above for important
* @notice principles to keep in mind when implementing your fulfillRandomness
* @notice method.
*
* @dev VRFConsumerBase expects its subcontracts to have a method with this
* @dev signature, and will call it once it has verified the proof
* @dev associated with the randomness. (It is triggered via a call to
* @dev rawFulfillRandomness, below.)
*
* @param requestId The Id initially returned by requestRandomness
* @param randomness the VRF output
*/functionfulfillRandomness(bytes32 requestId,
uint256 randomness
)
internalvirtual;
/**
* @dev In order to keep backwards compatibility we have kept the user
* seed field around. We remove the use of it because given that the blockhash
* enters later, it overrides whatever randomness the used seed provides.
* Given that it adds no security, and can easily lead to misunderstandings,
* we have removed it from usage and can now provide a simpler API.
*/uint256constantprivate USER_SEED_PLACEHOLDER =0;
/**
* @notice requestRandomness initiates a request for VRF output given _seed
*
* @dev The fulfillRandomness method receives the output, once it's provided
* @dev by the Oracle, and verified by the vrfCoordinator.
*
* @dev The _keyHash must already be registered with the VRFCoordinator, and
* @dev the _fee must exceed the fee specified during registration of the
* @dev _keyHash.
*
* @dev The _seed parameter is vestigial, and is kept only for API
* @dev compatibility with older versions. It can't *hurt* to mix in some of
* @dev your own randomness, here, but it's not necessary because the VRF
* @dev oracle will mix the hash of the block containing your request into the
* @dev VRF seed it ultimately uses.
*
* @param _keyHash ID of public key against which randomness is generated
* @param _fee The amount of LINK to send with the request
*
* @return requestId unique ID for this request
*
* @dev The returned requestId can be used to distinguish responses to
* @dev concurrent requests. It is passed as the first argument to
* @dev fulfillRandomness.
*/functionrequestRandomness(bytes32 _keyHash,
uint256 _fee
)
internalreturns (bytes32 requestId
)
{
LINK.transferAndCall(vrfCoordinator, _fee, abi.encode(_keyHash, USER_SEED_PLACEHOLDER));
// This is the seed passed to VRFCoordinator. The oracle will mix this with// the hash of the block containing this request to obtain the seed/input// which is finally passed to the VRF cryptographic machinery.uint256 vRFSeed = makeVRFInputSeed(_keyHash, USER_SEED_PLACEHOLDER, address(this), nonces[_keyHash]);
// nonces[_keyHash] must stay in sync with// VRFCoordinator.nonces[_keyHash][this], which was incremented by the above// successful LINK.transferAndCall (in VRFCoordinator.randomnessRequest).// This provides protection against the user repeating their input seed,// which would result in a predictable/duplicate output, if multiple such// requests appeared in the same block.
nonces[_keyHash] = nonces[_keyHash] +1;
return makeRequestId(_keyHash, vRFSeed);
}
LinkTokenInterface immutableinternal LINK;
addressimmutableprivate vrfCoordinator;
// Nonces for each VRF key from which randomness has been requested.//// Must stay in sync with VRFCoordinator[_keyHash][this]mapping(bytes32/* keyHash */=>uint256/* nonce */) private nonces;
/**
* @param _vrfCoordinator address of VRFCoordinator contract
* @param _link address of LINK token contract
*
* @dev https://docs.chain.link/docs/link-token-contracts
*/constructor(address _vrfCoordinator,
address _link
) {
vrfCoordinator = _vrfCoordinator;
LINK = LinkTokenInterface(_link);
}
// rawFulfillRandomness is called by VRFCoordinator when it receives a valid VRF// proof. rawFulfillRandomness then calls fulfillRandomness, after validating// the origin of the callfunctionrawFulfillRandomness(bytes32 requestId,
uint256 randomness
)
external{
require(msg.sender== vrfCoordinator, "Only VRFCoordinator can fulfill");
fulfillRandomness(requestId, randomness);
}
}
Contract Source Code
File 30 of 30: VRFRequestIDBase.sol
// SPDX-License-Identifier: MITpragmasolidity ^0.8.0;contractVRFRequestIDBase{
/**
* @notice returns the seed which is actually input to the VRF coordinator
*
* @dev To prevent repetition of VRF output due to repetition of the
* @dev user-supplied seed, that seed is combined in a hash with the
* @dev user-specific nonce, and the address of the consuming contract. The
* @dev risk of repetition is mostly mitigated by inclusion of a blockhash in
* @dev the final seed, but the nonce does protect against repetition in
* @dev requests which are included in a single block.
*
* @param _userSeed VRF seed input provided by user
* @param _requester Address of the requesting contract
* @param _nonce User-specific nonce at the time of the request
*/functionmakeVRFInputSeed(bytes32 _keyHash,
uint256 _userSeed,
address _requester,
uint256 _nonce
)
internalpurereturns (uint256)
{
returnuint256(keccak256(abi.encode(_keyHash, _userSeed, _requester, _nonce)));
}
/**
* @notice Returns the id for this request
* @param _keyHash The serviceAgreement ID to be used for this request
* @param _vRFInputSeed The seed to be passed directly to the VRF
* @return The id for this request
*
* @dev Note that _vRFInputSeed is not the seed passed by the consuming
* @dev contract, but the one generated by makeVRFInputSeed
*/functionmakeRequestId(bytes32 _keyHash,
uint256 _vRFInputSeed
)
internalpurereturns (bytes32)
{
returnkeccak256(abi.encodePacked(_keyHash, _vRFInputSeed));
}
}