Skeleton and JavaScript Architecture
In this article, you'll learn about the basic architecture behind the NFT contract that you'll develop while following this "Zero to Hero" series. You'll discover the contract's layout and you'll see how the JavaScript files are structured in order to build a feature-complete smart contract.
Introduction
This tutorial presents the code skeleton for the NFT smart contract and its file structure. You'll find how all the functions are laid out as well as the missing JS code that needs to be filled in. Once every file and function has been covered, you'll go through the process of building the mock-up contract to confirm that everything is working correctly.
File structure
Following a regular JavaScript project, the file structure for this smart contract has:
package.json
file to define the packages and scripts used in the project.src
folder where all the JavaScript source files are storedbuild
folder where the compiledwasm
will output to.
Source files
File | Description |
---|---|
approval.ts | Has the internal functions that controls the access and transfers of non-fungible tokens. |
enumeration.ts | Contains the internal methods to query for NFT tokens and their owners. |
index.ts | Holds the exposed smart contract functions. |
metadata.ts | Defines the token and metadata structures. |
mint.ts | Contains the internal token minting logic. |
nft_core.ts | Has the internal core logic that allows you to transfer NFTs between users. |
royalty.ts | Contains the internal payout-related functions. |
nft-tutorial-js
└── src
market-contract
nft-contract
├── approval.ts
├── enumeration.ts
├── index.ts
├── metadata.ts
├── mint.ts
├── nft_core.ts
└── royalty.ts
Explore the code in our GitHub repository.
approval.ts
This allows people to approve other accounts to transfer NFTs on their behalf.
This file contains the internal logic that complies with the standard's approvals management extension. Here is a breakdown of the methods and their functions:
Method | Description |
---|---|
internalNftApprove | Approves an account ID to transfer a token on your behalf. Called during nft_approve. |
internalNftIsApproved | Checks if the input account has access to approve the token ID. Called during nft_is_approved. |
internalNftRevoke | Revokes a specific account from transferring the token on your behalf. Called during nft_revoke. |
internalNftRevokeAll | Revokes all accounts from transferring the token on your behalf. Called during nft_revoke_all. |
Loading...
You'll learn more about these functions in the approvals section of the Zero to Hero series.
enumeration.ts
This file provides the internal functions needed to view information about NFTs, and follows the standard's enumeration extension.
Method | Description |
---|---|
internalNftTotalSupply | Returns the total amount of NFTs stored on the contract. Called during nft_total_supply. |
internalNftTokens | Returns a paginated list of NFTs stored on the contract regardless of their owner. Called during nft_tokens. |
internalNftSupplyForOwner | Allows you view the total number of NFTs owned by any given user. Called during nft_supply_for_owner. |
internalNftTokensForOwner | Returns a paginated list of NFTs owned by any given user. Called during nft_tokens_for_owner. |
Loading...
You'll learn more about these functions in the enumeration section of the tutorial series.
metadata.ts
This file is used to keep track of the information to be stored for tokens, and metadata. In addition, you can define a function to view the contract's metadata which is part of the standard's metadata extension.
Name | Description |
---|---|
TokenMetadata | This structure defines the metadata that can be stored for each token. (title, description, media, etc. |
Token | This structure outlines what information will be stored on the contract for each token. |
JsonToken | When querying information about NFTs through view calls, the return information is stored in this JSON token. |
internalNftMetadata | This function allows users to query for the contact's internal metadata. Called during nft_metadata. |
Loading...
You'll learn more about these functions in the minting section of the tutorial series.
mint.ts
Contains the internal token minting logic.
Method | Description |
---|---|
internalNftMint | This function mints a non-fungible token. Called during nft_mint. |
Loading...