-
Notifications
You must be signed in to change notification settings - Fork 282
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update dependencies, code structure, tests, README
- Loading branch information
1 parent
0c920da
commit 83956ae
Showing
24 changed files
with
1,062 additions
and
1,097 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,18 @@ | ||
[profile.release] | ||
codegen-units = 1 | ||
# Tell `rustc` to optimize for small code size. | ||
opt-level = "z" | ||
lto = true | ||
debug = false | ||
panic = "abort" | ||
overflow-checks = true | ||
[package] | ||
name = "non-fungible-token" | ||
version = "1.1.0" | ||
authors = ["Near Inc <[email protected]>"] | ||
edition = "2018" | ||
|
||
[workspace] | ||
# remember to include a member for each contract | ||
members = [ | ||
"nft", | ||
"test-approval-receiver", | ||
"test-token-receiver", | ||
] | ||
exclude = [ | ||
"integration-tests" | ||
] | ||
[lib] | ||
crate-type = ["cdylib", "rlib"] | ||
|
||
[dependencies] | ||
near-sdk = "5.5.0" | ||
near-contract-standards = "5.5.0" | ||
|
||
[dev-dependencies] | ||
near-sdk = { version = "5.5.0", features = ["unit-testing"] } | ||
near-workspaces = { version = "0.14.1", features = ["unstable"] } | ||
anyhow = "1.0" | ||
tokio = { version = "1.41.0", features = ["full"] } |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,154 +1,70 @@ | ||
Non-fungible Token (NFT) | ||
=================== | ||
Non-fungible Token (NFT) Example 👋 | ||
[![](https://img.shields.io/badge/⋈%20Examples-Basics-green)](https://docs.near.org/tutorials/welcome) | ||
[![](https://img.shields.io/badge/Contract-Rust-red)](contract-rs) | ||
![example workflow](https://github.com/near-examples/NFT/actions/workflows/tests-rs.yml/badge.svg) | ||
|
||
>**Note**: If you'd like to learn how to create an NFT contract from scratch that explores every aspect of the [NEP-171](https://github.com/near/NEPs/blob/master/neps/nep-0171.md) standard including an NFT marketplace, check out the NFT [Zero to Hero Tutorial](https://docs.near.org/tutorials/nfts/introduction). | ||
[![Open in Gitpod](https://gitpod.io/button/open-in-gitpod.svg)](https://gitpod.io/#https://github.com/near-examples/NFT) | ||
|
||
This repository includes an example implementation of a [non-fungible token] contract which uses [near-contract-standards] and workspaces-js and -rs tests. | ||
This repository contains an example implementation of a [non-fungible token] contract in Rust which uses [near-contract-standards] and workspaces-rs tests. | ||
|
||
[non-fungible token]: https://nomicon.io/Standards/NonFungibleToken/README.html | ||
[near-contract-standards]: https://github.com/near/near-sdk-rs/tree/master/near-contract-standards | ||
[simulation]: https://github.com/near/near-sdk-rs/tree/master/near-sdk-sim | ||
|
||
--- | ||
|
||
Prerequisites | ||
============= | ||
|
||
If you're using Gitpod, you can skip this step. | ||
[near-workspaces-rs]: https://github.com/near/near-workspaces-rs | ||
|
||
* Make sure Rust is installed per the prerequisites in [`near-sdk-rs`](https://github.com/near/near-sdk-rs). | ||
* Make sure [near-cli](https://github.com/near/near-cli) is installed. | ||
|
||
Explore this contract | ||
===================== | ||
>**Note**: If you'd like to learn how to create an NFT contract from scratch that explores every aspect of the [NEP-171](https://github.com/near/NEPs/blob/master/neps/nep-0171.md) standard including an NFT marketplace, check out the NFT [Zero to Hero Tutorial](https://docs.near.org/tutorials/nfts/introduction). | ||
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. | ||
<br /> | ||
|
||
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. | ||
```bash | ||
./scripts/build.sh | ||
``` | ||
## How to Build Locally? | ||
|
||
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. | ||
Install [`cargo-near`](https://github.com/near/cargo-near) and run: | ||
|
||
*Unit Tests* | ||
```bash | ||
cd nft | ||
cargo test -- --nocapture | ||
cargo near build | ||
``` | ||
|
||
*Integration Tests* | ||
*Rust* | ||
```bash | ||
cd integration-tests/rs | ||
cargo run --example integration-tests | ||
``` | ||
## How to Test Locally? | ||
|
||
*TypeScript* | ||
```bash | ||
cd integration-tests/ts | ||
yarn && yarn test | ||
cargo test | ||
``` | ||
|
||
Using this contract | ||
=================== | ||
## How to Deploy? | ||
|
||
### Quickest deploy | ||
|
||
You can build and deploy this smart contract to a development account. [Dev Accounts](https://docs.near.org/concepts/basics/account#dev-accounts) are auto-generated accounts to assist in developing and testing smart contracts. Please see the [Standard deploy](#standard-deploy) section for creating a more personalized account to deploy to. | ||
To deploy manually, install [`cargo-near`](https://github.com/near/cargo-near) and run: | ||
|
||
```bash | ||
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: | ||
# Create a new account | ||
cargo near create-dev-account | ||
|
||
>Done deploying to dev-1234567890123 | ||
# Deploy the contract on it | ||
cargo near deploy <account-id> | ||
|
||
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: | ||
|
||
```bash | ||
source neardev/dev-account.env | ||
# Initialize the contract | ||
near call <account-id> new_default_meta '{"owner_id": "<account-id>"}' --accountId <account-id> | ||
``` | ||
|
||
You can tell if the environment variable is set correctly if your command line prints the account name after this command: | ||
## Basic methods | ||
```bash | ||
echo $CONTRACT_NAME | ||
``` | ||
# View metadata | ||
near view <account-id> nft_metadata | ||
|
||
The next command will initialize the contract using the `new` method: | ||
# Mint a NFT | ||
near call <account-id> nft_mint '{"token_id": "0", "receiver_id": "<account-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 <account-id> --deposit 0.1 | ||
|
||
```bash | ||
near call $CONTRACT_NAME new_default_meta '{"owner_id": "'$CONTRACT_NAME'"}' --accountId $CONTRACT_NAME | ||
``` | ||
|
||
To view the NFT metadata: | ||
# View tokens for owner | ||
near view <account-id> nft_tokens_for_owner '{"account_id": "<owner_id>"}' | ||
|
||
```bash | ||
near view $CONTRACT_NAME nft_metadata | ||
# Transfer a NFT | ||
near call <account-id> nft_transfer '{"token_id": "0", "receiver_id": "<receiver-id>", "memo": "transfer ownership"}' --accountId <account-id> --depositYocto 1 | ||
``` | ||
|
||
### 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](https://wallet.testnet.near.org) or ([NEAR Wallet](https://wallet.near.org/) 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](https://nomicon.io/Standards/NonFungibleToken/Metadata.html). 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. | ||
## Useful Links | ||
|
||
- [cargo-near](https://github.com/near/cargo-near) - NEAR smart contract development toolkit for Rust | ||
- [near CLI](https://near.cli.rs) - Iteract with NEAR blockchain from command line | ||
- [NEAR Rust SDK Documentation](https://docs.near.org/sdk/rust/introduction) | ||
- [NEAR Documentation](https://docs.near.org) | ||
- [NFT Zero to Hero Tutorial](https://docs.near.org/tutorials/nfts/introduction) | ||
- [NEAR StackOverflow](https://stackoverflow.com/questions/tagged/nearprotocol) | ||
- [NEAR Discord](https://near.chat) | ||
- [NEAR Telegram Developers Community Group](https://t.me/neardev) | ||
- NEAR DevHub: [Telegram](https://t.me/neardevhub), [Twitter](https://twitter.com/neardevhub) |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.