Skip to content

Commit

Permalink
Non-Fungible Token Standards (#69)
Browse files Browse the repository at this point in the history
* feat: nep171 wip

* feat: nep171 events are owned; some experiments for allowing approval extension

* feat: some ideas for implementing approval extension (wip)

* fix: crate name change

* feat: nep171 macro progress

* full nep171 implementation

* chore: simplify nft_resolve_transfer

* chore: finished nep171 macro

* chore: cleanup, tests, renaming

* chore: documentation comments

* chore: workspaces tests & multiple token ids

* feat: nep171 multiple token id transfer

* chore: more tests + small fixes

* fix: warnings

* feat: predicate api improvements

* feat: remove predicate, introduce check function

* feat: nft events version 1.2.0 update

* Nep177: NFT Metadata (#124)

* feat: nep177

* chore: more function implementation

* feat: nep177 macro

* feat: non_fungible_token macro

* feat: documentation comments

* feat: switch to using #[serde(flatten)] for token metadata

* feat: nep178

* chore: upgrade tests & organize libs

* feat: nep178 macro integration

* feat: nep178 testing

* chore: docs

* chore: more tests and examples in docs

* chore: lots of docs

* feat: switch hook state to associated type

* feat: nep181 internals done, improved hooks

* feat: nep181 macro, sanity test

* chore: qol nft module

* feat: docs

* feat: further enumeration tests

* feat: final (?) tests for enumeration

* chore: update readme to mention nep-171 and related
  • Loading branch information
encody authored Sep 13, 2023
1 parent a84cb44 commit a11f56a
Show file tree
Hide file tree
Showing 38 changed files with 4,590 additions and 31 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@master
with:
toolchain: 1.66
toolchain: 1.69
components: rustfmt
- name: Check formatting
run: >
Expand All @@ -30,7 +30,7 @@ jobs:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@master
with:
toolchain: 1.66
toolchain: 1.69
components: clippy
- name: Run linter
run: cargo clippy -- -D warnings
Expand All @@ -41,7 +41,7 @@ jobs:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@master
with:
toolchain: 1.66
toolchain: 1.69
- name: Run unit and integration tests
run: cargo test --workspace --exclude workspaces-tests
workspaces-test:
Expand All @@ -51,7 +51,7 @@ jobs:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@master
with:
toolchain: 1.66
toolchain: 1.69
targets: wasm32-unknown-unknown
- name: Run workspaces tests
run: >
Expand Down
28 changes: 19 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,32 @@ name = "near-sdk-contract-tools"
repository = "https://github.com/NEARFoundation/near-sdk-contract-tools"
version = "1.0.1"

[dependencies]
near-sdk = {version = "4.1.0", default-features = false}
near-sdk-contract-tools-macros = {version = "=1.0.1", path = "./macros"}
[workspace.dependencies]
near-sdk = { version = "4.1.1", default-features = false }
near-sdk-contract-tools-macros = { version = "=1.0.1", path = "./macros" }
serde = "1.0.144"
serde_json = "1.0.85"
thiserror = "1.0.35"

[dependencies]
near-sdk.workspace = true
near-sdk-contract-tools-macros.workspace = true
serde.workspace = true
serde_json.workspace = true
thiserror.workspace = true

[dev-dependencies]
near-sdk = {version = "4.1.0", default-features = false, features = ["unit-testing", "legacy"]}
near-sdk = { workspace = true, default-features = false, features = [
"unit-testing",
"legacy",
] }

[features]
unstable = ["near-sdk/unstable"]

[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]

[workspace]
members = [
".",
"macros",
"workspaces-tests",
]
members = [".", "macros", "workspaces-tests", "workspaces-tests-utils"]
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ This package is a collection of common tools and patterns in NEAR smart contract
- Pause (derive macro available)
- Derive macro for [NEP-297 events](https://nomicon.io/Standards/EventsFormat)
- Derive macro for [NEP-141](https://nomicon.io/Standards/Tokens/FungibleToken/Core) (and [NEP-148](https://nomicon.io/Standards/Tokens/FungibleToken/Metadata)) fungible tokens
- Derive macro for [NEP-171](https://nomicon.io/Standards/NonFungibleToken/NonFungibleToken) non-fungible tokens, and extensions [NEP-177](https://nomicon.io/Standards/Tokens/NonFungibleToken/Metadata), [NEP-178](https://nomicon.io/Standards/Tokens/NonFungibleToken/ApprovalManagement), and [NEP-181](https://nomicon.io/Standards/Tokens/NonFungibleToken/Enumeration).

Not to be confused with [`near-contract-standards`](https://crates.io/crates/near-contract-standards), which contains official implementations of standardized NEPs. This crate is intended to be a complement to `near-contract-standards`.

Expand Down
52 changes: 51 additions & 1 deletion macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ pub fn derive_rbac(input: TokenStream) -> TokenStream {

/// Adds NEP-141 fungible token core functionality to a contract. Exposes
/// `ft_*` functions to the public blockchain, implements internal controller
/// and receiver functionality (see: [`near_sdk_contract_tools::standard::nep141`]).
/// and receiver functionality.
///
/// The storage key prefix for the fields can be optionally specified (default:
/// `"~$141"`) using `#[nep141(storage_key = "<expression>")]`.
Expand Down Expand Up @@ -147,6 +147,56 @@ pub fn derive_fungible_token(input: TokenStream) -> TokenStream {
make_derive(input, standard::fungible_token::expand)
}

/// Adds NEP-171 non-fungible token core functionality to a contract. Exposes
/// `nft_*` functions to the public blockchain, implements internal controller
/// and receiver functionality.
///
/// The storage key prefix for the fields can be optionally specified (default:
/// `"~$171"`) using `#[nep171(storage_key = "<expression>")]`.
///
/// Fields:
/// - `no_hooks`: Flag. Removes the requirement for the contract to implement
/// transfer hooks.
/// - `token_data`: specify the token metadata loading extensions invoked by
/// `nft_token`.
#[proc_macro_derive(Nep171, attributes(nep171))]
pub fn derive_nep171(input: TokenStream) -> TokenStream {
make_derive(input, standard::nep171::expand)
}

/// Adds NEP-177 non-fungible token metadata functionality to a contract.
///
/// The storage key prefix for the fields can be optionally specified (default:
/// `"~$177"`) using `#[nep177(storage_key = "<expression>")]`.
#[proc_macro_derive(Nep177, attributes(nep177))]
pub fn derive_nep177(input: TokenStream) -> TokenStream {
make_derive(input, standard::nep177::expand)
}

/// Adds NEP-178 non-fungible token approvals functionality to a contract.
///
/// The storage key prefix for the fields can be optionally specified (default:
/// `"~$178"`) using `#[nep178(storage_key = "<expression>")]`.
#[proc_macro_derive(Nep178, attributes(nep178))]
pub fn derive_nep178(input: TokenStream) -> TokenStream {
make_derive(input, standard::nep178::expand)
}

/// Adds NEP-181 non-fungible token enumeration functionality to a contract.
///
/// The storage key prefix for the fields can be optionally specified (default:
/// `"~$181"`) using `#[nep181(storage_key = "<expression>")]`.
#[proc_macro_derive(Nep181, attributes(nep181))]
pub fn derive_nep181(input: TokenStream) -> TokenStream {
make_derive(input, standard::nep181::expand)
}

/// Implements all NFT functionality at once, like `#[derive(Nep171, Nep177, Nep178, Nep181)]`.
#[proc_macro_derive(NonFungibleToken, attributes(non_fungible_token))]
pub fn derive_non_fungible_token(input: TokenStream) -> TokenStream {
make_derive(input, standard::non_fungible_token::expand)
}

/// Migrate a contract's default struct from one schema to another.
///
/// Fields may be specified in the `#[migrate(...)]` attribute.
Expand Down
5 changes: 5 additions & 0 deletions macros/src/standard/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
pub mod event;
pub mod fungible_token;
pub mod non_fungible_token;

pub mod nep141;
pub mod nep148;
pub mod nep171;
pub mod nep177;
pub mod nep178;
pub mod nep181;
pub mod nep297;
Loading

0 comments on commit a11f56a

Please sign in to comment.