// 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 2 of 8: Clones.sol
// SPDX-License-Identifier: MITpragmasolidity ^0.8.0;/**
* @dev https://eips.ethereum.org/EIPS/eip-1167[EIP 1167] is a standard for
* deploying minimal proxy contracts, also known as "clones".
*
* > To simply and cheaply clone contract functionality in an immutable way, this standard specifies
* > a minimal bytecode implementation that delegates all calls to a known, fixed address.
*
* The library includes functions to deploy a proxy using either `create` (traditional deployment) or `create2`
* (salted deterministic deployment). It also includes functions to predict the addresses of clones deployed using the
* deterministic method.
*
* _Available since v3.4._
*/libraryClones{
/**
* @dev Deploys and returns the address of a clone that mimics the behaviour of `implementation`.
*
* This function uses the create opcode, which should never revert.
*/functionclone(address implementation) internalreturns (address instance) {
assembly {
let ptr :=mload(0x40)
mstore(ptr, 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000000000000000000000)
mstore(add(ptr, 0x14), shl(0x60, implementation))
mstore(add(ptr, 0x28), 0x5af43d82803e903d91602b57fd5bf30000000000000000000000000000000000)
instance :=create(0, ptr, 0x37)
}
require(instance !=address(0), "ERC1167: create failed");
}
/**
* @dev Deploys and returns the address of a clone that mimics the behaviour of `implementation`.
*
* This function uses the create2 opcode and a `salt` to deterministically deploy
* the clone. Using the same `implementation` and `salt` multiple time will revert, since
* the clones cannot be deployed twice at the same address.
*/functioncloneDeterministic(address implementation, bytes32 salt) internalreturns (address instance) {
assembly {
let ptr :=mload(0x40)
mstore(ptr, 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000000000000000000000)
mstore(add(ptr, 0x14), shl(0x60, implementation))
mstore(add(ptr, 0x28), 0x5af43d82803e903d91602b57fd5bf30000000000000000000000000000000000)
instance :=create2(0, ptr, 0x37, salt)
}
require(instance !=address(0), "ERC1167: create2 failed");
}
/**
* @dev Computes the address of a clone deployed using {Clones-cloneDeterministic}.
*/functionpredictDeterministicAddress(address implementation,
bytes32 salt,
address deployer
) internalpurereturns (address predicted) {
assembly {
let ptr :=mload(0x40)
mstore(ptr, 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000000000000000000000)
mstore(add(ptr, 0x14), shl(0x60, implementation))
mstore(add(ptr, 0x28), 0x5af43d82803e903d91602b57fd5bf3ff00000000000000000000000000000000)
mstore(add(ptr, 0x38), shl(0x60, deployer))
mstore(add(ptr, 0x4c), salt)
mstore(add(ptr, 0x6c), keccak256(ptr, 0x37))
predicted :=keccak256(add(ptr, 0x37), 0x55)
}
}
/**
* @dev Computes the address of a clone deployed using {Clones-cloneDeterministic}.
*/functionpredictDeterministicAddress(address implementation, bytes32 salt)
internalviewreturns (address predicted)
{
return predictDeterministicAddress(implementation, salt, address(this));
}
}
Contract Source Code
File 3 of 8: FNDCollectionFactory.sol
/*
・
* ★
・ 。
・ ゚☆ 。
* ★ ゚・。 * 。
* ☆ 。・゚*.。
゚ *.。☆。★ ・
` .-:::::-.` `-::---...```
`-:` .:+ssssoooo++//:.` .-/+shhhhhhhhhhhhhyyyssooo:
.--::. .+ossso+/////++/:://-` .////+shhhhhhhhhhhhhhhhhhhhhy
`-----::. `/+////+++///+++/:--:/+/- -////+shhhhhhhhhhhhhhhhhhhhhy
`------:::-` `//-.``.-/+ooosso+:-.-/oso- -////+shhhhhhhhhhhhhhhhhhhhhy
.--------:::-` :+:.` .-/osyyyyyyso++syhyo.-////+shhhhhhhhhhhhhhhhhhhhhy
`-----------:::-. +o+:-.-:/oyhhhhhhdhhhhhdddy:-////+shhhhhhhhhhhhhhhhhhhhhy
.------------::::-- `oys+/::/+shhhhhhhdddddddddy/-////+shhhhhhhhhhhhhhhhhhhhhy
.--------------:::::-` +ys+////+yhhhhhhhddddddddhy:-////+yhhhhhhhhhhhhhhhhhhhhhy
`----------------::::::-`.ss+/:::+oyhhhhhhhhhhhhhhho`-////+shhhhhhhhhhhhhhhhhhhhhy
.------------------:::::::.-so//::/+osyyyhhhhhhhhhys` -////+shhhhhhhhhhhhhhhhhhhhhy
`.-------------------::/:::::..+o+////+oosssyyyyyyys+` .////+shhhhhhhhhhhhhhhhhhhhhy
.--------------------::/:::.` -+o++++++oooosssss/. `-//+shhhhhhhhhhhhhhhhhhhhyo
.------- ``````.......--` `-/+ooooosso+/-` `./++++///:::--...``hhhhyo
`````
*
・ 。
・ ゚☆ 。
* ★ ゚・。 * 。
* ☆ 。・゚*.。
゚ *.。☆。★ ・
* ゚。·*・。 ゚*
☆゚・。°*. ゚
・ ゚*。・゚★。
・ *゚。 *
・゚*。★・
☆∴。 *
・ 。
*/// SPDX-License-Identifier: MIT OR Apache-2.0pragmasolidity ^0.8.0;import"@openzeppelin/contracts-solc-8/proxy/Clones.sol";
import"@openzeppelin/contracts-solc-8/utils/Address.sol";
import"@openzeppelin/contracts-solc-8/utils/Strings.sol";
import"./interfaces/solc8/ICollectionContractInitializer.sol";
import"./interfaces/solc8/IRoles.sol";
import"./interfaces/solc8/ICollectionFactory.sol";
import"./interfaces/solc8/IProxyCall.sol";
/**
* @title A factory to create NFT collections.
* @notice Call this factory to create and initialize a minimal proxy pointing to the NFT collection contract.
*/contractFNDCollectionFactoryisICollectionFactory{
usingAddressforaddress;
usingAddressforaddresspayable;
usingClonesforaddress;
usingStringsforuint256;
/**
* @notice The contract address which manages common roles.
* @dev Used by the collections for a shared operator definition.
*/
IRoles public rolesContract;
/**
* @notice The address of the template all new collections will leverage.
*/addresspublic implementation;
/**
* @notice The address of the proxy call contract implementation.
* @dev Used by the collections to safely call another contract with arbitrary call data.
*/
IProxyCall public proxyCallContract;
/**
* @notice The implementation version new collections will use.
* @dev This is auto-incremented each time the implementation is changed.
*/uint256public version;
eventRolesContractUpdated(addressindexed rolesContract);
eventCollectionCreated(addressindexed collectionContract,
addressindexed creator,
uint256indexed version,
string name,
string symbol,
uint256 nonce
);
eventImplementationUpdated(addressindexed implementation, uint256indexed version);
eventProxyCallContractUpdated(addressindexed proxyCallContract);
modifieronlyAdmin() {
require(rolesContract.isAdmin(msg.sender), "FNDCollectionFactory: Caller does not have the Admin role");
_;
}
constructor(address _proxyCallContract, address _rolesContract) {
_updateRolesContract(_rolesContract);
_updateProxyCallContract(_proxyCallContract);
}
/**
* @notice Create a new collection contract.
* @param nonce An arbitrary value used to allow a creator to mint multiple collections.
* @dev The nonce is required and must be unique for the msg.sender + implementation version,
* otherwise this call will revert.
*/functioncreateCollection(stringcalldata name,
stringcalldata symbol,
uint256 nonce
) externalreturns (address) {
require(bytes(symbol).length>0, "FNDCollectionFactory: Symbol is required");
// This reverts if the NFT was previously created using this implementation version + msg.sender + nonceaddress proxy = implementation.cloneDeterministic(_getSalt(msg.sender, nonce));
ICollectionContractInitializer(proxy).initialize(payable(msg.sender), name, symbol);
emit CollectionCreated(proxy, msg.sender, version, name, symbol, nonce);
// Returning the address created allows other contracts to integrate with this callreturnaddress(proxy);
}
/**
* @notice Allows Foundation to change the admin role contract address.
*/functionadminUpdateRolesContract(address _rolesContract) externalonlyAdmin{
_updateRolesContract(_rolesContract);
}
/**
* @notice Allows Foundation to change the collection implementation used for future collections.
* This call will auto-increment the version.
* Existing collections are not impacted.
*/functionadminUpdateImplementation(address _implementation) externalonlyAdmin{
_updateImplementation(_implementation);
}
/**
* @notice Allows Foundation to change the proxy call contract address.
*/functionadminUpdateProxyCallContract(address _proxyCallContract) externalonlyAdmin{
_updateProxyCallContract(_proxyCallContract);
}
/**
* @notice Returns the address of a collection given the current implementation version, creator, and nonce.
* This will return the same address whether the collection has already been created or not.
* @param nonce An arbitrary value used to allow a creator to mint multiple collections.
*/functionpredictCollectionAddress(address creator, uint256 nonce) externalviewreturns (address) {
return implementation.predictDeterministicAddress(_getSalt(creator, nonce));
}
function_updateRolesContract(address _rolesContract) private{
require(_rolesContract.isContract(), "FNDCollectionFactory: RolesContract is not a contract");
rolesContract = IRoles(_rolesContract);
emit RolesContractUpdated(_rolesContract);
}
/**
* @dev Updates the implementation address, increments the version, and initializes the template.
* Since the template is initialized when set, implementations cannot be re-used.
* To downgrade the implementation, deploy the same bytecode again and then update to that.
*/function_updateImplementation(address _implementation) private{
require(_implementation.isContract(), "FNDCollectionFactory: Implementation is not a contract");
implementation = _implementation;
version++;
// The implementation is initialized when assigned so that others may not claim it as their own.
ICollectionContractInitializer(_implementation).initialize(
payable(address(rolesContract)),
string(abi.encodePacked("Foundation Collection Template v", version.toString())),
string(abi.encodePacked("FCTv", version.toString()))
);
emit ImplementationUpdated(_implementation, version);
}
function_updateProxyCallContract(address _proxyCallContract) private{
require(_proxyCallContract.isContract(), "FNDCollectionFactory: Proxy call address is not a contract");
proxyCallContract = IProxyCall(_proxyCallContract);
emit ProxyCallContractUpdated(_proxyCallContract);
}
function_getSalt(address creator, uint256 nonce) privatepurereturns (bytes32) {
returnkeccak256(abi.encodePacked(creator, nonce));
}
}
Contract Source Code
File 4 of 8: ICollectionContractInitializer.sol
// SPDX-License-Identifier: MIT OR Apache-2.0pragmasolidity ^0.8.0;interfaceICollectionContractInitializer{
functioninitialize(addresspayable _creator,
stringmemory _name,
stringmemory _symbol
) external;
}
Contract Source Code
File 5 of 8: ICollectionFactory.sol
// SPDX-License-Identifier: MIT OR Apache-2.0pragmasolidity ^0.8.0;import"./IRoles.sol";
import"./IProxyCall.sol";
interfaceICollectionFactory{
functionrolesContract() externalreturns (IRoles);
functionproxyCallContract() externalreturns (IProxyCall);
}
Contract Source Code
File 6 of 8: IProxyCall.sol
// SPDX-License-Identifier: MIT OR Apache-2.0pragmasolidity ^0.8.0;interfaceIProxyCall{
functionproxyCallAndReturnAddress(address externalContract, bytescalldata callData)
externalreturns (addresspayable result);
}
Contract Source Code
File 7 of 8: IRoles.sol
// SPDX-License-Identifier: MIT OR Apache-2.0pragmasolidity ^0.8.0;/**
* @notice Interface for a contract which implements admin roles.
*/interfaceIRoles{
functionisAdmin(address account) externalviewreturns (bool);
functionisOperator(address account) externalviewreturns (bool);
}
Contract Source Code
File 8 of 8: 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);
}
}