@unique-nft/contracts v0.1.0
Unique Network | Contracts
This repository contains smart contracts for minting collections and tokens using Solidity in the Unique Schema V2.
Installation and configuration
Install packages:
npm install @unique-nft/contracts
Find the RPC endpoint in the official documentation. You can get OPL
(Opal testnet) tokens for free at https://t.me/unique2faucet_opal_bot
!IMPORTANT Configure your project:
- Compatible Solidity versions are
>=0.8.18 <=0.8.24
- Use via-IR compilation pipeline.
In hardhat.config file set:
solidity: { version: "0.8.24", settings: { viaIR: true }, },
API
Structs
Attribute
Represents an attribute of a token.
Parameter | Type | Description |
---|---|---|
trait_type | string | Type of the attribute. |
value | string | Value of the attribute. |
CrossAddress
Represents ethereum or substrate account. Only one property can be filled out to consider the structure valid. Learn more about EVM in Unique Network and compatibility with substrate accounts in the official documentation.
The AddressUtils
library provides helper methods to work with the CrossAddress
struct.
Parameter | Type | Description |
---|---|---|
eth | address | Ethereum address or address(0) if the origin is substrate account |
sub | uin256 | Substrate public key or 0 if the caller is ethereum account |
Contracts
CollectionMinter.sol
import "@unique-nft/contracts/CollectionMinter.sol";
Provides functions to create collections in the Unique Schema V2.
constructor(bool _mutable, bool _admin, bool _tokenOwner)
Initializes the contract and sets the default permissions for token properties mutation
_createCollection(string _name, string _description, string _symbol, string _collectionCover)
Creates a collection with specified name, description, symbol, collection cover, and allowed nesting permissions.
TokenMinter.sol
import "@unique-nft/contracts/TokenMinter.sol";
Provides functions to create tokens in the Unique Schema V2.
_createToken(address _collectionAddress, string _name, string _description, string _image, Attribute[] _attributes, CrossAddress _to) internal returns (uint256)
Internal function to create a new token with specified image and attributes in a given collection.
TokenManager.sol
import "@unique-nft/contracts/TokenManager.sol";
Provides utility functions for managing token data, such as setting and retrieving images and traits for tokens within a collection.
_setImage(address _collection, uint256 _tokenId, bytes memory _newImage) internal
Sets a new image for a specific token in a collection.
_setTrait(address _collection, uint256 _tokenId, bytes memory _traitType, bytes memory _traitValue) internal
Sets a new trait (attribute) for a specific token in a collection.
_getImage(address _collection, uint256 _tokenId) internal view returns (bytes memory)
Retrieves the image data of a specific token in a collection.
_getTraitValue(address _collection, uint256 _tokenId, bytes memory _traitType) internal view returns (bytes memory)
Retrieves the value of a specific trait for a token in a collection.
AddressValidator.sol
import "@unique-nft/contracts/AddressValidator.sol";
Provides modifiers for validating addresses in token-related operations, ensuring that the caller is authorized.
onlyTokenOwner(uint256 _tokenId, address collectionAddress
Restricts access to the owner of the token with ID _tokenId
in the collection at collectionAddress
. Reverts if msg.sender
is not the token owner.
onlyMessageSender(CrossAddress memory _crossAddress)
Restricts access to the address represented by _crossAddress
. Reverts if _crossAddress
does not match msg.sender
.
Example Usage
Minter.sol
: Demonstrates how to create a gasless experience for minting collections and NFTs.POAP.sol
: Demonstrates how to create a POAP contract. Tokens minting sponsored by the POAP contract itself. Every account can mint only one NFT that cannot be transferred.BreedingGame.sol
: Demonstrates how contracts can mutate token's attributes and image.
Run tests
- Install packages and compile contracts
yarn
yarn compile
- Create
.env
file from.env.example
Set at least two private keys with
OPL
balance toTEST_ACCOUNTS_ETH
env. You can getOPL
tokens for free at https://t.me/unique2faucet_opal_botRun tests
yarn test