Skip to main content

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 stored
  • build folder where the compiled wasm will output to.

Source filesโ€‹

FileDescription
approval.tsHas the internal functions that controls the access and transfers of non-fungible tokens.
enumeration.tsContains the internal methods to query for NFT tokens and their owners.
index.tsHolds the exposed smart contract functions.
metadata.tsDefines the token and metadata structures.
mint.tsContains the internal token minting logic.
nft_core.tsHas the internal core logic that allows you to transfer NFTs between users.
royalty.tsContains 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
tip

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:

MethodDescription
internalNftApproveApproves an account ID to transfer a token on your behalf. Called during nft_approve.
internalNftIsApprovedChecks if the input account has access to approve the token ID. Called during nft_is_approved.
internalNftRevokeRevokes a specific account from transferring the token on your behalf. Called during nft_revoke.
internalNftRevokeAllRevokes all accounts from transferring the token on your behalf. Called during nft_revoke_all.

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.

MethodDescription
internalNftTotalSupplyReturns the total amount of NFTs stored on the contract. Called during nft_total_supply.
internalNftTokensReturns a paginated list of NFTs stored on the contract regardless of their owner. Called during nft_tokens.
internalNftSupplyForOwnerAllows you view the total number of NFTs owned by any given user. Called during nft_supply_for_owner.
internalNftTokensForOwnerReturns a paginated list of NFTs owned by any given user. Called during nft_tokens_for_owner.

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.

NameDescription
TokenMetadataThis structure defines the metadata that can be stored for each token. (title, description, media, etc.
TokenThis structure outlines what information will be stored on the contract for each token.
JsonTokenWhen querying information about NFTs through view calls, the return information is stored in this JSON token.
internalNftMetadataThis function allows users to query for the contact's internal metadata. Called during nft_metadata.

You'll learn more about these functions in the minting section of the tutorial series.


mint.tsโ€‹

Contains the internal token minting logic.

MethodDescription
internalNftMintThis function mints a non-fungible token. Called during nft_mint.

nft_core.tsโ€‹

Core logic that allows you to transfer NFTs between users.

MethodDescription
internalNftTransferTransfers an NFT to a receiver ID. Called during nft_transfer.
internalNftTransferCallTransfers an NFT to a receiver and calls a function on the receiver ID's contract. The function returns true if the token was transferred from the sender's account. Called during nft_transfer_call.
internalNftTokenAllows users to query for the information about a specific NFT. Called during nft_token.
internalNftResolveTransferWhen you start the nft_transfer_call and transfer an NFT, the standard dictates that you should also call a method on the receiver's contract. If the receiver needs you to return the NFT to the sender (as per the return value of the nft_on_transfer method), this function allows you to execute that logic. Called during nft_resolve_transfer.

You'll learn more about these functions in the minting section of the tutorial series.


royalty.tsโ€‹

Contains the internal payout-related functions.

MethodDescription
internalNftPayoutThis internal method calculates the payout for a given token. Called during nft_payout.
internalNftTransferPayoutInternal method to transfer the token to the receiver ID and return the payout object that should be paid for a given balance. Called during nft_transfer_payout.

You'll learn more about these functions in the royalty section of the tutorial series.


index.tsโ€‹

This file outlines the smart contract class and what information it stores and keeps track of. In addition, it exposes all public facing methods that are callable by the user.

MethodDescription
initConstructor function used to initialize the contract with some metadata and default state.
nft_mintCalls the internal mint function to mint an NFT.
nft_tokenCalls the internal function to query for info on a specific NFT
nft_transferCalls the internal function to transfer an NFT
nft_transfer_callCalls the internal function to transfer an NFT and call nft_on_transfer on the receiver's contract
nft_resolve_transferCalls the internal function to resolve the transfer call promise.
nft_is_approvedCalls the internal function to check whether someone is approved for an NFT
nft_approveCalls the internal function to approve someone to transfer your NFT
nft_payoutCalls the internal function to query for the payout object for an NFT
nft_transfer_payoutCalls the internal function to transfer an NFT and return the payout object.
nft_revokeCalls the internal function to revoke someone access to transfer your NFT
nft_revoke_allCalls the internal function to revoke everyone's access to transfer your NFT
nft_total_supplyCalls the internal function to query the total supply of NFTs on the contract.
nft_tokensCalls the internal function to paginate through NFTs on the contract
nft_tokens_for_ownerCalls the internal function to paginate through NFTs for a given owner
nft_supply_for_ownerCalls the internal function to query for the total number of NFTs owned by someone.
nft_metadataCalls the internal function to query for the contract's metadata

You'll learn more about these functions in the minting section of the tutorial series.


Building the skeletonโ€‹

  • If you haven't cloned the main repository yet, open a terminal and run:
git clone https://github.com/near-examples/nft-tutorial-js/
  • Next, switch to the 1.skeleton branch.
  • Install the dependencies (including the JS SDK): yarn
  • Build the contract with yarn build:
git clone https://github.com/near-examples/nft-tutorial-js/
cd nft-tutorial-js
git checkout 1.skeleton
yarn && yarn build

Once this finishes, the nft-tutorial-js/build directory should contain the nft.wasm smart contract!

Building the skeleton is useful to validate that everything works properly and that you'll be able to compile improved versions of this NFT contract in the upcoming tutorials.


Conclusionโ€‹

You've seen the layout of this NFT smart contract, and how all the functions are laid out across the different source files. Using yarn, you've been able to compile the contract, and you'll start fleshing out this skeleton in the next Minting tutorial.

Versioning for this article

At the time of this writing, this example works with the following versions:

  • near-sdk-js: 0.4.0-5
  • NFT standard: NEP171, version 1.0.0
Was this page helpful?