Your First NFT
This tutorial describes how to create and transfer NFTs on the Jin blockchain. The Jin implementation for core NFTs can be found in the token.move Move module.
Step 1: Pick an SDK
Install your preferred SDK from the below list:
Step 2: Run the example
Each SDK provides an examples directory. This tutorial covers the simple-nft example.
Clone the aptos-core repo:
git clone git@github.com:aptos-labs/aptos-core.git ~/aptos-core
- Typescript
- Python
- Rust
Navigate to the Typescript SDK examples directory:
cd ~/aptos-core/ecosystem/typescript/sdk/examples/typescript
Install the necessary dependencies:
yarn install
Run the Typescript simple_nft example:
yarn run simple_nft
Navigate to the Python SDK directory:
cd ~/aptos-core/ecosystem/python/sdk
Install the necessary dependencies:
curl -sSL https://install.python-poetry.org | python3
poetry update
Run the Python simple-nft example:
poetry run python -m examples.simple-nft
Coming soon.
Step 3: Understand the output
The following output should appear after executing the simple-nft example, though some values will be different:
=== Addresses ===
Alice: 0x9df0f527f3a0b445e4d5c320cfa269cdefafc7cd1ed17ffce4b3fd485b17aafb
Bob: 0xfcc74af84dde26b0050dce35d6b3d11c60f5c8c58728ca3a0b11035942a0b1de
=== Initial Coin Balances ===
Alice: 20000
Bob: 20000
=== Creating Collection and Token ===
Alice's collection: {
"description": "Alice's simple collection",
"maximum": "18446744073709551615",
"mutability_config": {
"description": false,
"maximum": false,
"uri": false
},
"name": "Alice's",
"supply": "1",
"uri": "https://aptos.dev"
}
Alice's token balance: 1
Alice's token data: {
"default_properties": {
"map": {
"data": []
}
},
"description": "Alice's simple token",
"largest_property_version": "0",
"maximum": "1",
"mutability_config": {
"description": false,
"maximum": false,
"properties": false,
"royalty": false,
"uri": false
},
"name": "Alice's first token",
"royalty": {
"payee_address": "0x9df0f527f3a0b445e4d5c320cfa269cdefafc7cd1ed17ffce4b3fd485b17aafb",
"royalty_points_denominator": "1000000",
"royalty_points_numerator": "0"
},
"supply": "1",
"uri": "https://aptos.dev/img/nyan.jpeg"
}
=== Transferring the token to Bob ===
Alice's token balance: 0
Bob's token balance: 1
=== Transferring the token back to Alice using MultiAgent ===
Alice's token balance: 1
Bob's token balance: 0
This example demonstrates:
- Initializing the REST and faucet clients.
- The creation of two accounts: Alice and Bob.
- The funding and creation of Alice and Bob's accounts.
- The creation of a collection and a token using Alice's account.
- Alice offering a token and Bob claiming it.
- Bob unilaterally sending the token to Alice via a multiagent transaction.
Step 4: The SDK in depth
- Typescript
- Python
- Rust
See simple_nft for the complete code as you follow the below steps.
See simple-nft for the complete code as you follow the below steps.
Coming soon.
Step 4.1: Initializing the clients
In the first step the example initializes both the API and faucet clients.
- The API client interacts with the REST API, and
- The faucet client interacts with the devnet Faucet service for creating and funding accounts.
- Typescript
- Python
- Rust
const client = new AptosClient(NODE_URL);
const faucetClient = new FaucetClient(NODE_URL, FAUCET_URL);
Using the API client we can create a TokenClient, which we use for common token operations such as creating collections and tokens, transferring them, claiming them, and so on.
const tokenClient = new TokenClient(client);
common.ts initializes the URL values as such:
export const NODE_URL = process.env.APTOS_NODE_URL || "https://fullnode.devnet.aptoslabs.com";
export const FAUCET_URL = process.env.APTOS_FAUCET_URL || "https://faucet.devnet.aptoslabs.com";
rest_client = RestClient(NODE_URL)
faucet_client = FaucetClient(FAUCET_URL, rest_client)
common.py initializes these values as follows:
NODE_URL = os.getenv("APTOS_NODE_URL", "https://fullnode.devnet.aptoslabs.com/v1")
FAUCET_URL = os.getenv(
"APTOS_FAUCET_URL",
"https://tap.devnet.prod.gcp.aptosdev.com", # "https://faucet.testnet.aptoslabs.com"
)
Coming soon.
By default the URLs for both the services point to Jin devnet services. However, they can be configured with the following environment variables:
APTOS_NODE_URLAPTOS_FAUCET_URL
Step 4.2: Creating local accounts
The next step is to create two accounts locally. Accounts represent both on and off-chain state. Off-chain state consists of an address and the public, private key pair used to authenticate ownership. This step demonstrates how to generate that off-chain state.
- Typescript
- Python
- Rust
const alice = new AptosAccount();
const bob = new AptosAccount();
alice = Account.generate()
bob = Account.generate()
Coming soon.
Step 4.3: Creating blockchain accounts
In Aptos, each account must have an on-chain representation in order to support receive tokens and coins as well as interacting in other dApps. An account represents a medium for storing assets, hence it must be explicitly created. This example leverages the Faucet to create Alice and Bob's accounts:
- Typescript
- Python
- Rust
await faucetClient.fundAccount(alice.address(), 100_000_000);
await faucetClient.fundAccount(bob.address(), 100_000_000);
faucet_client.fund_account(alice.address(), 100_000_000)
faucet_client.fund_account(bob.address(), 100_000_000)
Coming soon.
Step 4.4: Creating a collection
Now begins the process of creating tokens. First, the creator must create a collection to store tokens. A collection can contain zero, one, or many distinct tokens within it. The collection does not restrict the attributes of the tokens, as it is only a container.
- Typescript
- Python
- Rust
Your application will call createCollection:
const txnHash1 = await tokenClient.createCollection(
alice,
collectionName,
"Alice's simple collection",
"https://alice.com",
);
The function signature of createCollection. It returns a transaction hash:
async createCollection(
account: AptosAccount,
name: string,
description: string,
uri: string,
maxAmount: AnyNumber = MAX_U64_BIG_INT,
extraArgs?: OptionalTransactionArgs,
): Promise<string> {
Your application will call create_collection:
txn_hash = rest_client.create_collection(
alice, collection_name, "Alice's simple collection", "https://aptos.dev"
)
The function signature of create_collection. It returns a transaction hash:
def create_collection(
self, account: Account, name: str, description: str, uri: str
) -> str:
Coming soon.
Step 4.5: Creating a token
To create a token, the creator must specify an associated collection. A token must be associated with a collection and that collection must have remaining tokens that can be minted. There are many attributes associated with a token, but the helper API only exposes the minimal amount required to create static content.
- Typescript
- Python
- Rust
Your application will call createToken:
const txnHash2 = await tokenClient.createToken(
alice,
collectionName,
tokenName,
"Alice's simple token",
1,
"https://aptos.dev/img/nyan.jpeg",
);
The function signature of createToken. It returns a transaction hash:
async createToken(
account: AptosAccount,
collectionName: string,
name: string,
description: string,
supply: number,
uri: string,
max: AnyNumber = MAX_U64_BIG_INT,
royalty_payee_address: MaybeHexString = account.address(),
royalty_points_denominator: number = 0,
royalty_points_numerator: number = 0,
property_keys: Array<string> = [],
property_values: Array<string> = [],
property_types: Array<string> = [],
extraArgs?: OptionalTransactionArgs,
): Promise<string> {
Your application will call create_token:
txn_hash = rest_client.create_token(
alice,
collection_name,
token_name,
"Alice's simple token",
1,
"https://aptos.dev/img/nyan.jpeg",
0,
)
The function signature of create_token. It returns a transaction hash:
def create_token(
self,
account: Account,
collection_name: str,
name: str,
description: str,
supply: int,
uri: str,
royalty_points_per_million: int,
) -> str:
Coming soon.
Step 4.6: Reading token and collection metadata
Both the collection and token metadata are stored on the creator's account within their Collections in a table. The SDKs provide convenience wrappers around querying these specific tables:
- Typescript
- Python
- Rust
To read a collection's metadata:
const collectionData = await tokenClient.getCollectionData(alice.address(), collectionName);
console.log(`Alice's collection: ${JSON.stringify(collectionData, null, 4)}`);
To read a token's metadata:
const tokenData = await tokenClient.getTokenData(alice.address(), collectionName, tokenName);
console.log(`Alice's token data: ${JSON.stringify(tokenData, null, 4)}`);
Here's how getTokenData queries the token metadata:
async getTokenData(
creator: MaybeHexString,
collectionName: string,
tokenName: string,
): Promise<TokenTypes.TokenData> {
const creatorHex = creator instanceof HexString ? creator.hex() : creator;
const collection: { type: Gen.MoveStructTag; data: any } = await this.aptosClient.getAccountResource(
creatorHex,
"0x3::token::Collections",
);
const { handle } = collection.data.token_data;
const tokenDataId = {
creator: creatorHex,
collection: collectionName,
name: tokenName,
};
const getTokenTableItemRequest: Gen.TableItemRequest = {
key_type: "0x3::token::TokenDataId",
value_type: "0x3::token::TokenData",
key: tokenDataId,
};
// We know the response will be a struct containing TokenData, hence the
// implicit cast.
return this.aptosClient.getTableItem(handle, getTokenTableItemRequest);
}
To read a collection's metadata:
collection_data = rest_client.get_collection(alice.address(), collection_name)
print(
f"Alice's collection: {json.dumps(collection_data, indent=4, sort_keys=True)}"
)
To read a token's metadata:
token_data = rest_client.get_token_data(
alice.address(), collection_name, token_name, property_version
)
print(
f"Alice's token data: {json.dumps(token_data, indent=4, sort_keys=True)}"
)
Here's how get_token_data queries the token metadata:
def get_token_data(
self,
creator: AccountAddress,
collection_name: str,
token_name: str,
property_version: int,
) -> Any:
token_data_handle = self.account_resource(creator, "0x3::token::Collections")[
"data"
]["token_data"]["handle"]
token_data_id = {
"creator": creator.hex(),
"collection": collection_name,
"name": token_name,
}
return self.get_table_item(
token_data_handle,
"0x3::token::TokenDataId",
"0x3::token::TokenData",
token_data_id,
)
Coming soon.
Step 4.7: Reading a token balance
Each token within Jin is a distinct asset, the assets owned by the user are stored within their TokenStore. To get the balance:
- Typescript
- Python
- Rust
const aliceBalance1 = await tokenClient.getToken(
alice.address(),
collectionName,
tokenName,
`${tokenPropertyVersion}`,
);
console.log(`Alice's token balance: ${aliceBalance1["amount"]}`);
balance = rest_client.get_token_balance(
alice.address(), alice.address(), collection_name, token_name, property_version
)
print(f"Alice's token balance: {balance}")
Coming soon.
Step 4.8: Offering and claiming a token
Many users have received unwanted tokens that may cause minimally embarrassment to serious ramifications. Jin gives the rights to each owner of an account to dictate whether or not to receive unilateral transfers. By default, unilateral transfers are unsupported. So Jin provides a framework for offering and claiming tokens.
To offer a token:
- Typescript
- Python
- Rust
const txnHash3 = await tokenClient.offerToken(
alice,
bob.address(),
alice.address(),
collectionName,
tokenName,
1,
tokenPropertyVersion,
);
txn_hash = rest_client.offer_token(
alice,
bob.address(),
alice.address(),
collection_name,
token_name,
property_version,
1,
)
Coming soon!
To claim a token:
- Typescript
- Python
- Rust
const txnHash4 = await tokenClient.claimToken(
bob,
alice.address(),
alice.address(),
collectionName,
tokenName,
tokenPropertyVersion,
);
txn_hash = rest_client.claim_token(
bob,
alice.address(),
alice.address(),
collection_name,
token_name,
property_version,
)
Coming soon.
Step 4.9: Safe unilateral transferring of a token
To support safe unilateral transfers of a token, the sender may first ask the recipient to acknowledge off-chain about a pending transfer. This comes in the form of a multiagent transaction request. Multiagent transactions contain multiple signatures, one for each on-chain account. Move then can leverage this to give signer level permissions to all that signed. For token transfers, this ensures that the receiving party does indeed desire to receive this token without requiring the use of the token transfer framework described above.
- Typescript
- Python
- Rust
let txnHash5 = await tokenClient.directTransferToken(
bob,
alice,
alice.address(),
collectionName,
tokenName,
1,
tokenPropertyVersion,
);
txn_hash = rest_client.direct_transfer_token(
bob, alice, alice.address(), collection_name, token_name, 0, 1
)
Coming soon.
Step 4.10: Enabling unilateral token transfers
Coming soon.
- Python
- Rust
- Typescript
Coming soon.
Coming soon.
Coming soon.