• Stars
    star
    346
  • Rank 122,430 (Top 3 %)
  • Language
    Rust
  • License
    MIT License
  • Created over 4 years ago
  • Updated 8 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Example implementations of tokens to represent unique assets, such as collectibles or deeds, using the NEP-171 spec (similar to ERC-721)

Non-fungible Token (NFT)

Note: If you'd like to learn how to create an NFT contract from scratch that explores every aspect of the NEP-171 standard including an NFT marketplace, check out the NFT Zero to Hero Tutorial.

Open in Gitpod

This repository includes an example implementation of a non-fungible token contract which uses near-contract-standards and workspaces-js and -rs tests.

Prerequisites

If you're using Gitpod, you can skip this step.

  • Make sure Rust is installed per the prerequisites in near-sdk-rs.
  • Make sure near-cli is installed.

Explore this contract

The source for this contract is in nft/src/lib.rs. It provides methods to manage access to tokens, transfer tokens, check access, and get token owner. Note, some further exploration inside the rust macros is needed to see how the NonFungibleToken contract is implemented.

Building this contract

Run the following, and we'll build our rust project up via cargo. This will generate our WASM binaries into our res/ directory. This is the smart contract we'll be deploying onto the NEAR blockchain later.

./scripts/build.sh

Testing this contract

We have some tests that you can run. For example, the following will run our simple tests to verify that our contract code is working.

Unit Tests

cd nft
cargo test -- --nocapture

Integration Tests Rust

cd integration-tests/rs
cargo run --example integration-tests

TypeScript

cd integration-tests/ts
yarn && yarn test 

Using this contract

Quickest deploy

You can build and deploy this smart contract to a development account. Dev Accounts are auto-generated accounts to assist in developing and testing smart contracts. Please see the Standard deploy section for creating a more personalized account to deploy to.

near dev-deploy --wasmFile res/non_fungible_token.wasm

Behind the scenes, this is creating an account and deploying a contract to it. On the console, notice a message like:

Done deploying to dev-1234567890123

In this instance, the account is dev-1234567890123. A file has been created containing a key pair to the account, located at neardev/dev-account. To make the next few steps easier, we're going to set an environment variable containing this development account id and use that when copy/pasting commands. Run this command to set the environment variable:

source neardev/dev-account.env

You can tell if the environment variable is set correctly if your command line prints the account name after this command:

echo $CONTRACT_NAME

The next command will initialize the contract using the new method:

near call $CONTRACT_NAME new_default_meta '{"owner_id": "'$CONTRACT_NAME'"}' --accountId $CONTRACT_NAME

To view the NFT metadata:

near view $CONTRACT_NAME nft_metadata

Standard deploy

This smart contract will get deployed to your NEAR account. For this example, please create a new NEAR account. Because NEAR allows the ability to upgrade contracts on the same account, initialization functions must be cleared. If you'd like to run this example on a NEAR account that has had prior contracts deployed, please use the near-cli command near delete, and then recreate it in Wallet. To create (or recreate) an account, please follow the directions in Test Wallet or (NEAR Wallet if we're using mainnet).

In the project root, log in to your newly created account with near-cli by following the instructions after this command.

near login

To make this tutorial easier to copy/paste, we're going to set an environment variable for our account id. In the below command, replace MY_ACCOUNT_NAME with the account name we just logged in with, including the .testnet (or .near for mainnet):

ID=MY_ACCOUNT_NAME

We can tell if the environment variable is set correctly if our command line prints the account name after this command:

echo $ID

Now we can deploy the compiled contract in this example to your account:

near deploy --wasmFile res/non_fungible_token.wasm --accountId $ID

NFT contract should be initialized before usage. More info about the metadata at nomicon.io. But for now, we'll initialize with the default metadata.

near call $ID new_default_meta '{"owner_id": "'$ID'"}' --accountId $ID

We'll be able to view our metadata right after:

near view $ID nft_metadata

Then, let's mint our first token. This will create a NFT based on Olympus Mons where only one copy exists:

near call $ID nft_mint '{"token_id": "0", "receiver_id": "'$ID'", "token_metadata": { "title": "Olympus Mons", "description": "Tallest mountain in charted solar system", "media": "https://upload.wikimedia.org/wikipedia/commons/thumb/0/00/Olympus_Mons_alt.jpg/1024px-Olympus_Mons_alt.jpg", "copies": 1}}' --accountId $ID --deposit 0.1

Transferring our NFT

Let's set up an account to transfer our freshly minted token to. This account will be a sub-account of the NEAR account we logged in with originally via near login.

near create-account alice.$ID --masterAccount $ID --initialBalance 10

Checking Alice's account for tokens:

near view $ID nft_tokens_for_owner '{"account_id": "'alice.$ID'"}'

Then we'll transfer over the NFT into Alice's account. Exactly 1 yoctoNEAR of deposit should be attached:

near call $ID nft_transfer '{"token_id": "0", "receiver_id": "alice.'$ID'", "memo": "transfer ownership"}' --accountId $ID --depositYocto 1

Checking Alice's account again shows us that she has the Olympus Mons token.

Notes

  • The maximum balance value is limited by U128 (2**128 - 1).
  • JSON calls should pass U128 as a base-10 string. E.g. "100".
  • This does not include escrow functionality, as ft_transfer_call provides a superior approach. An escrow system can, of course, be added as a separate contract or additional functionality within this contract.

AssemblyScript

Currently, AssemblyScript is not supported for this example. An old version can be found in the NEP4 example, but this is not recommended as it is out of date and does not follow the standards the NEAR SDK has set currently.

More Repositories

1

nft-market

NFT & Marketplace Contracts with royalties and fungible token support. Sample React app included.
Rust
144
star
2

FT

Example implementations of money-like tokens, where one token is the same as any other, using the NEP-141 spec (similar to ERC-20)
Rust
117
star
3

nft-tutorial

Rust
98
star
4

guest-book-as

Sign in with NEAR and add a message to the guest book!
JavaScript
76
star
5

counter-rust-deprecated

Simple counter in Rust. Increment, decrement, and reset. See branches for extended usage.
JavaScript
61
star
6

rust-status-message

Set and retrieve status messages per account with this simple smart contract.
JavaScript
46
star
7

wallet-example

This example demonstrates how to integrate your application with NEAR Wallet.
JavaScript
40
star
8

workshop--exploring-assemblyscript-contracts

Learn to develop contracts for NEAR Protocol using AssemblyScript
TypeScript
38
star
9

counter-as

Increment and decrement a counter with this simple smart contract via a web page.
JavaScript
35
star
10

rust-template

Rust
27
star
11

token-factory

Allows anyone to issue new fungible tokens
JavaScript
27
star
12

pow-faucet

Proof of Work Faucet for NEAR accounts to gain Ⓝ (NEAR denomination) from a React app.
JavaScript
23
star
13

token-contract-as

Implementation of token contract similar to ERC20.
JavaScript
22
star
14

nft-onboarding

A more detailed example of progressive onboarding using an NFT mint + sale shop example.
JavaScript
21
star
15

near-multichain

Examples for signing and executing transactions across multiple blockchain protocols from one NEAR account.
JavaScript
19
star
16

poker

Decentralized poker using NEAR blockchain as a backend.
Rust
18
star
17

workshop--exploring-near-apis

Learn to work with NEAR APIs
TypeScript
18
star
18

coin-flip-examples

A random coin-flip that lives in the NEAR blockchain
JavaScript
16
star
19

nft-tutorial-frontend

Rust
15
star
20

nft-tutorial-js

TypeScript
15
star
21

chainsig-script

Script to sign TX for Ethereum and Bitcoin, using Near Protocol MPC.
TypeScript
15
star
22

nearnames

Gift someone a named near account ID with a link. Rotates the main full access key and secures receiver of new account.
JavaScript
12
star
23

rust-fungible-token

Create a fungible token on a NEAR account. Additionally, use escrow functionality to send tokens on behalf of another account.
Rust
12
star
24

transaction-examples

Examples of how to construct, sign, and send transactions on the NEAR blockchain.
JavaScript
11
star
25

place

NEAR Place - draw with pixels to earn more pixels
JavaScript
11
star
26

crossword-tutorial-chapter-1

This repo covers the first chapter from the tutorial teaching Rust smart contract development on NEAR
JavaScript
11
star
27

near-account-utils

Small command line demo using near-api-js and JSON RPC API
JavaScript
10
star
28

near-pet-shop

Compile and migrate simple Solidity contracts using Truffle to the NEAR EVM
JavaScript
10
star
29

hello-near-js-deprecated

Hello NEAR in JavaScript!
JavaScript
8
star
30

staking-pool-factory

Frontend for the Staking Pool Factory on betanet
JavaScript
7
star
31

hello-near-rust-deprecated

Hello NEAR in Rust!
JavaScript
7
star
32

token-printer

Faucet using Proof of Work to distribute Ⓝ. The token printer goes BRRR…..
JavaScript
7
star
33

ft-tutorial

Rust
7
star
34

near-lake-accounts-watcher

A source code for a video tutorial on how to use the [NEAR Lake Framework](https://github.com/near/near-lake-framework)
Rust
6
star
35

guest-book-js-deprecated

About Sign in with NEAR and add a message to the guest book!
JavaScript
6
star
36

simulation-testing

Demonstration of simulation testing - a category of testing allowing (direct) cross-contract calls
Rust
5
star
37

nft-series

mint editions of an NFT series (with individual owner permissions) from a single contract on NEAR Protocol
Rust
5
star
38

bos-gateway-deprecated

The simplest gateway for BOS
TypeScript
5
star
39

collection-examples-rs

JavaScript
5
star
40

indexer-tx-watcher-example-lake

NEAR Lake Framework indexer example that watches for transaction for specified accounts/contracts
Rust
5
star
41

near-lake-raw-printer

An example of NEAR Lake Framework usage that prints the raw data from the stream
Rust
4
star
42

rust-high-level-cross-contract

Explore various cross-contract calls written in Rust using a localnet and the cli.
Rust
4
star
43

simple_counter_as

An example of creating a AssemblyScript NEAR contract by hand.
WebAssembly
4
star
44

storage-examples

Rust
4
star
45

indexer-tx-watcher-example

NEAR Indexer example that watches for transaction for specified accounts/contracts
Rust
4
star
46

crud-tutorial

JavaScript
4
star
47

near-lake-nft-indexer

Source code for the tutorial
TypeScript
4
star
48

escrow-js

A simple example of a NEAR escrow contract in JavaScript
JavaScript
4
star
49

evm-simple

Interact with the NEAR EVM in 42 lines of code.
HTML
3
star
50

cross-contract-hello-rust

An example on how to perform the simplest cross-contract call in NEAR
Rust
3
star
51

hello-near-examples

👋 Hello NEAR
JavaScript
3
star
52

near-dashboard

A toy project to produce a cool looking CLI dashboard for the NEAR platform.
Shell
3
star
53

chainlink-demo

This demonstrates how a smart contract on NEAR can access off-chain data via Chainlink's oracle solution.
JavaScript
3
star
54

update-migrate-rust

An example on how to perform self-updates and state migrations in NEAR Rust contracts
Rust
3
star
55

donation-js-deprecated

JavaScript donations contract seen in our documentation.
JavaScript
3
star
56

free-place

Unlimited NEAR Place - draw for free. We're paying for you
JavaScript
3
star
57

crossword-tutorial-chapter-2

Second chapter in the zero-to-hero guide for Rust smart contract development
JavaScript
3
star
58

collection-examples-as

Examples for using NEAR AssemblyScript collections
JavaScript
3
star
59

coingecko-oracle

Python
2
star
60

guest-book-rust-deprecated

A guest book that lives in the NEAR network
JavaScript
2
star
61

rust-linkdrop-app

JavaScript
2
star
62

guest-book-tutorial

Sign in with NEAR and add a message to the guest book!
JavaScript
2
star
63

cross-contract-calls

This repo contains examples for cross contract calls in Rust, JS, as well as an advanced cross contract call example
Rust
2
star
64

cross-contract-calls-tutorial

Zero to hero tutorial for cross-contract calls.
Rust
2
star
65

hackathon-starter-kit

This starter kit includes 3 project templates
JavaScript
2
star
66

crossword-tutorial-chapter-3

Adding cross-contract calls and an interesting use of access keys to our crossword puzzle.
JavaScript
2
star
67

jestnet

Run jest JS tests on testnet
Rust
2
star
68

donation-rust-deprecated

Donation example in rust
JavaScript
2
star
69

counter-js-deprecated

Simple counter in JavaScript. Increment, decrement, and reset.
JavaScript
2
star
70

donation-examples

JavaScript
1
star
71

rust-multisig-example

A multisig contract example app. Deploy the contract and contract the multisig via React App.
JavaScript
1
star
72

activity--TEMPLATE

A template for workshop activities using AssemblyScript contracts
JavaScript
1
star
73

rust-filehash

A map of file hashes to account ids
Rust
1
star
74

cross-contract-rust-counter

A simple extension to the rust counter with the addition of cross-contract calls
Rust
1
star
75

contract-that-deploys-contracts-rs

Rust
1
star
76

NEAR-Arweave-Tutorial

A tutorial on how to store information on Arweave and mint the ID on NEAR
Rust
1
star
77

oracle

1
star
78

BlockVote-JS-Edition

C
1
star
79

lake-indexer-start-options

The source code for the tutorial
Rust
1
star
80

indexer-tutorials

A code related to indexer tutorials from official documentation
Rust
1
star
81

NEAR-SIA-SKYNET-Tutorial

Quick tutorial on how to integrate Sia'
Rust
1
star
82

webrtc-chat

[WIP] WebRTC based video calls using NEAR to establish connection
JavaScript
1
star
83

bos-components

A template for teams to collaborate on BOS components
JavaScript
1
star