|
| 1 | +--- |
| 2 | +id: nft-contract-tools |
| 3 | +title: Create NFT using Contract Tools |
| 4 | +sidebar_label: Create NFT using Contract Tools |
| 5 | +description: "Learn how to create a non-fungible token (NFT) using Contract Tools package" |
| 6 | +--- |
| 7 | + |
| 8 | +import {Github} from "@site/src/components/UI/Codetabs"; |
| 9 | +import MovingForwardSupportSection from '@site/src/components/MovingForwardSupportSection'; |
| 10 | + |
| 11 | +In this tutorial, you will create a non-fungible token (NFT) using the [NEAR SDK Contract Tools](https://github.com/near/near-sdk-contract-tools) package. This package is a collection of common tools and patterns to simplify smart contract development, including: |
| 12 | + |
| 13 | +- Storage fee management |
| 14 | +- Escrow pattern and derive macro |
| 15 | +- Owner pattern and derive macro |
| 16 | +- Pause pattern and derive macro |
| 17 | +- Role-based access control |
| 18 | +- Derive macros for [NEP standards](./standard.md) |
| 19 | + - [NEP-141](https://github.com/near/NEPs/blob/master/neps/nep-0141.md) (fungible token), extension [NEP-148](https://github.com/near/NEPs/blob/master/neps/nep-0148.md) |
| 20 | + - [NEP-145](https://github.com/near/NEPs/blob/master/neps/nep-0145.md) (storage management), and integrations for the fungible token and non-fungible token standards |
| 21 | + - [NEP-171](https://github.com/near/NEPs/blob/master/neps/nep-0171.md) (non-fungible token), extensions [NEP-177](https://github.com/near/NEPs/blob/master/neps/nep-0177.md), [NEP-178](https://github.com/near/NEPs/blob/master/neps/nep-0178.md), [NEP-181](https://github.com/near/NEPs/blob/master/neps/nep-0181.md) |
| 22 | + - [NEP-297](https://github.com/near/NEPs/blob/master/neps/nep-0297.md) (events) |
| 23 | + |
| 24 | +--- |
| 25 | + |
| 26 | +## Introduction |
| 27 | + |
| 28 | +While one can create a non-fungible token (NFT) contract from scratch using only the `near-sdk` and `near_contract_standards` (e.g. [NFT contract](https://github.com/near-examples/NFT)), a simpler approach is to use `near-sdk-contract-tools`. |
| 29 | + |
| 30 | +`near-sdk-contract-tools` allows you to implement the minting/burning logic, access control, and other NFT standards by simply deriving macros on the contract's `struct`, as `OpenZeppelin` does for Ethereum contracts. |
| 31 | + |
| 32 | +--- |
| 33 | + |
| 34 | +## Basic NFT Methods |
| 35 | + |
| 36 | +To derive basic NFT methods to your smart contract, you need to derive the `NonFungibleToken` macro to the contract's `struct`: |
| 37 | + |
| 38 | +<Github fname="lib.rs" language="rust" |
| 39 | + url="https://github.com/near-examples/nft-contract-tools/blob/main/src/lib.rs" |
| 40 | + start="9" end="12" /> |
| 41 | + |
| 42 | +This will bring all the basic NFT methods to the contract: |
| 43 | +- `new` |
| 44 | +- `contract_source_metadata` |
| 45 | +- `nft_is_approved` |
| 46 | +- `nft_metadata` |
| 47 | +- `nft_supply_for_owner` |
| 48 | +- `nft_token` |
| 49 | +- `nft_tokens` |
| 50 | +- `nft_tokens_for_owner` |
| 51 | +- `nft_total_supply` |
| 52 | +- `nft_approve` |
| 53 | +- `nft_resolve_transfer` |
| 54 | +- `nft_revoke` |
| 55 | +- `nft_revoke_all` |
| 56 | +- `nft_transfer` |
| 57 | +- `nft_transfer_call` |
| 58 | +- `storage_balance_bounds` |
| 59 | +- `storage_balance_of` |
| 60 | +- `storage_deposit` |
| 61 | +- `storage_unregister` |
| 62 | +- `storage_withdraw` |
| 63 | + |
| 64 | +To bring basic owner methods to the contract, you also need to derive the `Owner` macro, which adds the following methods: |
| 65 | +- `own_get_owner` |
| 66 | +- `own_get_proposed_owner` |
| 67 | +- `own_accept_owner` |
| 68 | +- `own_propose_owner` |
| 69 | +- `own_renounce_owner` |
| 70 | + |
| 71 | +--- |
| 72 | + |
| 73 | +## Initialization |
| 74 | + |
| 75 | +To initialize the basic NFT contract with a custom owner, metadata, and storage bounds, implement the `new` method: |
| 76 | + |
| 77 | +<Github fname="lib.rs" language="rust" |
| 78 | + url="https://github.com/near-examples/nft-contract-tools/blob/main/src/lib.rs" |
| 79 | + start="14" end="45" /> |
| 80 | + |
| 81 | +--- |
| 82 | + |
| 83 | +## Transfer Hook |
| 84 | + |
| 85 | +If you want to customize how the token transfer works (i.e., modify the `nft_transfer` method), you need to implement a hook. Hooks are a way to **wrap (inject code before and after)** component functions: |
| 86 | + |
| 87 | +<Github fname="transfer_hook.rs" language="rust" |
| 88 | + url="https://github.com/near-examples/nft-contract-tools/blob/main/src/transfer_hook.rs" |
| 89 | + start="5" end="33" /> |
| 90 | + |
| 91 | +Then derive it to our contract struct: |
| 92 | + |
| 93 | +<Github fname="lib.rs" language="rust" |
| 94 | + url="https://github.com/near-examples/nft-contract-tools/blob/main/src/lib.rs" |
| 95 | + start="9" end="12" /> |
| 96 | + |
| 97 | +--- |
| 98 | + |
| 99 | +## Minting |
| 100 | + |
| 101 | +By default, the NFT standards do not include a minting method. However, you can easily mint tokens for the owner by implementing an `nft_mint` method: |
| 102 | + |
| 103 | +<Github fname="mint.rs" language="rust" |
| 104 | + url="https://github.com/near-examples/nft-contract-tools/blob/main/src/mint.rs" |
| 105 | + start="10" end="40" /> |
| 106 | + |
| 107 | +:::tip |
| 108 | + |
| 109 | +You can modify this method as you need, for example, to allow minting only when the contract is not paused (requires deriving [`Pausable`](https://github.com/near/near-sdk-contract-tools/tree/develop?tab=readme-ov-file#macro-combinations) hook), or to enable minting only to specific accounts with a certain role or from a whitelist with custom limitations. |
| 110 | + |
| 111 | +::: |
| 112 | + |
| 113 | +--- |
| 114 | + |
| 115 | +## Burning |
| 116 | + |
| 117 | +In the same way that minting is not included in the NFT standards, burning is also not included. However, you can also easily implement it. |
| 118 | + |
| 119 | +To allow users to burn their tokens, you can add a `burn` method: |
| 120 | + |
| 121 | +<Github fname="burn.rs" language="rust" |
| 122 | + url="https://github.com/near-examples/nft-contract-tools/blob/main/src/burn.rs" |
| 123 | + start="5" end="25" /> |
| 124 | + |
| 125 | +--- |
| 126 | + |
| 127 | +## Conclusion |
| 128 | + |
| 129 | +Using `near-sdk-contract-tools` is a simple and flexible way to create an NFT contract with minimal boilerplate, which allows you to focus on the business logic. |
| 130 | + |
| 131 | +You can further extend this contract with more features like pausing, role-based access control, escrow pattern, and more by deriving corresponding macros from the package. |
| 132 | + |
| 133 | +<MovingForwardSupportSection /> |
0 commit comments