Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v1.0.1 #117

Merged
merged 84 commits into from
Jun 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
84 commits
Select commit Hold shift + click to select a range
2352177
basic generic upgrade function
Sep 9, 2022
be78ad1
adding Upgrade and UpgradeHook traits
Sep 19, 2022
9556b5a
Merge branch 'develop' into upgradability
kenobijon Sep 19, 2022
937577f
Merge remote-tracking branch 'origin/develop' into upgradability
Sep 19, 2022
35fcbe9
update to stable near-sdk-rs
Sep 19, 2022
fe75f3f
Merge remote-tracking branch 'origin/develop' into upgradability
Sep 19, 2022
0f2bbcf
broken upgrade workspaces-tests
Sep 20, 2022
8e13ddd
fix: remove migrate argument
encody Sep 20, 2022
e4fb18d
Added basic failure tests for upgrade
Sep 20, 2022
07ad8a8
added owner check for upgrade tests
Sep 20, 2022
fcc138b
not owner failure test
Sep 20, 2022
921ffd2
removing JS scripts
Sep 20, 2022
fd2703a
Merge branch 'develop' into upgradability
kenobijon Sep 20, 2022
b770598
feat: adds more derives to events
encody Sep 22, 2022
aebada0
cleaned up workspaces tests for upgrade
Sep 22, 2022
4a2cd37
Merge branch 'develop' into upgradability
kenobijon Sep 22, 2022
451f373
removed unnecessary near_bindgen
Sep 22, 2022
57d06ff
fixed macro version issue
Sep 22, 2022
9547818
Merge pull request #66 from NEARFoundation/more-event-derives
encody Sep 22, 2022
2643aa2
fix: some comments
encody Sep 23, 2022
42b1495
chore: conciseness
encody Sep 23, 2022
c5989b2
fix: repo url in Cargo.toml
encody Sep 23, 2022
9214092
fixing naming conventions
Sep 28, 2022
aad160b
Merge pull request #48 from NEARFoundation/upgradability
encody Sep 29, 2022
6a59920
chore: adds build task to workspaces tests
encody Oct 3, 2022
fcd9df8
feat: #[derive(Nep297)] allows enums
encody Oct 11, 2022
6f6036f
feat: #[event] supports enums
encody Oct 11, 2022
cb67a10
fix: Owner uses borsh storage keys
encody Oct 11, 2022
7ef8b37
fix: upgrade workspaces version to 0.6
encody Oct 12, 2022
2b8b6c1
Merge pull request #81 from NEARFoundation/80-workspaces-rs-gh-action…
encody Oct 12, 2022
3bbc890
Merge branch 'develop' into 72-dont-use-single-letter-hardcoded-stora…
encody Oct 12, 2022
b98e860
Merge branch 'develop' into 71-allow-enums-to-be-annotated-with-event…
encody Oct 12, 2022
9117f68
Merge pull request #77 from NEARFoundation/71-allow-enums-to-be-annot…
encody Oct 13, 2022
e81f221
Merge pull request #78 from NEARFoundation/72-dont-use-single-letter-…
encody Oct 13, 2022
348f8f5
fix: removes generic requirement from Event trait
encody Oct 10, 2022
ffd365b
chore: convert events back to enum shape with owned values
encody Oct 14, 2022
dcefd9e
Merge pull request #82 from NEARFoundation/64-make-events-easier-to-use
encody Oct 18, 2022
0228d2f
fix: pin Rust version to 1.64; fixes #83
encody Oct 18, 2022
9eeba5f
Use dtolnay/rust-toolchain (#87)
encody Oct 18, 2022
dc633eb
fix: rust doc warning from README.md
encody Oct 18, 2022
56b3ad1
Add safety invariants to owner module docs (#75)
twitu Oct 20, 2022
6e93a47
chore: adds .DS_Store to .gitignore
encody Oct 21, 2022
0aecd59
fix: better doc links in Slot
encody Oct 21, 2022
eee30ed
Default storage keys enum (#90)
encody Oct 21, 2022
aee997b
Link items in docs and other improvements (#91)
twitu Nov 4, 2022
00ce50f
Upgrade macro (#84)
kenobijon Nov 10, 2022
4088d3d
feat: unstable feature flag and upgrade near_sdk -> 4.1.0
encody Nov 10, 2022
a2bedca
fix: Eq derive now that Base64VecU8: Eq
encody Nov 10, 2022
bf5d23a
fix: remove near-sdk default features usage (#98)
austinabell Nov 10, 2022
c4e081d
chore: Cow -> String (#100)
encody Nov 14, 2022
b8da9b4
Makes `Rbac` iterable (#99)
encody Nov 14, 2022
377bfdd
chore: upgrade Rust to 1.65
encody Nov 14, 2022
5e6d40a
chore: upgrade GH Actions Rust to 1.65
encody Nov 14, 2022
4cd106e
feat: update version to 0.7.0
encody Nov 14, 2022
e08bf2d
chore: optimize has_role (#102)
encody Nov 18, 2022
71fcbad
chore: update version to 0.7.1
encody Nov 19, 2022
85316b0
Merge branch 'main' into develop
encody Nov 19, 2022
80454a5
Fuller Iterator implementation for Rbac iterator (#105)
encody Dec 15, 2022
b034b09
Disallows Event name collisions (#108)
encody Dec 15, 2022
7daa142
Merge branch 'main' into develop
encody Dec 16, 2022
a36f9d6
chore: add name change notification
encody Dec 16, 2022
6a9c98c
chore: versions to 0.7.2
encody Dec 16, 2022
d404a6b
Merge branch 'main' into develop
encody Dec 16, 2022
462edd1
Change package name to `near-sdk-contract-tools` (#109)
encody Dec 16, 2022
a271e29
fix: inaccurate documentation
encody Dec 20, 2022
6a19057
chore: upgrade rust, workspaces, adds approval keys test
encody Jan 9, 2023
2533a44
fix: clippy: needless borrow
encody Jan 9, 2023
0d3a0b1
feat: easier network specification in tests
encody Jan 9, 2023
d6f622c
feat: create account test
encody Jan 9, 2023
0a604bf
chore: create account test, base64 for Vec<u8> in native tx actions
encody Jan 9, 2023
b968c5a
feat: delete account native transaction action test
encody Jan 11, 2023
a1bdc2f
feat: stake action test
encody Jan 11, 2023
a759b64
feat: utils tests
encody Jan 11, 2023
32b089b
chore: unused imports
encody Jan 11, 2023
576b59e
fix: check additional fees sum
encody Jan 11, 2023
2222cd2
chore: add rename warning to docs
encody Jan 11, 2023
b2f8ef2
feat: finally, a decent prefix_key signature
encody Jan 12, 2023
2bc7afb
BREAKING: Internal Traits (#111)
encody Mar 9, 2023
d3de8d8
better test naming
encody Apr 17, 2023
367c435
fix: double underscore warning (#113)
encody Jun 12, 2023
d75efff
feat: adds kudelski audit final doc (#114)
encody Jun 12, 2023
09cd33d
chore: mention audit in README.md
encody Jun 12, 2023
0180492
chore: version bump to v1.0.0
encody Jun 27, 2023
65195e6
fix: exclude documents folder from publishing
encody Jun 27, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.65
toolchain: 1.66
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.65
toolchain: 1.66
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.65
toolchain: 1.66
- 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.65
toolchain: 1.66
targets: wasm32-unknown-unknown
- name: Run workspaces tests
run: >
Expand Down
11 changes: 6 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ categories = ["wasm", "cryptography::cryptocurrencies"]
description = """
Helpful functions and macros for developing smart contracts on NEAR Protocol.
"""
documentation = "https://docs.rs/near-contract-tools"
documentation = "https://docs.rs/near-sdk-contract-tools"
edition = "2021"
exclude = ["documents/"]
license = "GPL-3.0"
name = "near-contract-tools"
repository = "https://github.com/NEARFoundation/near-contract-tools"
version = "0.7.2"
name = "near-sdk-contract-tools"
repository = "https://github.com/NEARFoundation/near-sdk-contract-tools"
version = "1.0.0"

[dependencies]
near-contract-tools-macros = {version = "=0.7.2", path = "./macros"}
near-sdk = {version = "4.1.0", default-features = false}
near-sdk-contract-tools-macros = {version = "=1.0.0", path = "./macros"}
serde = "1.0.144"
serde_json = "1.0.85"
thiserror = "1.0.35"
Expand Down
30 changes: 17 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
# This package will be renamed to [`near-sdk-contract-tools`](https://crates.io/crates/near-sdk-contract-tools)

# near-contract-tools
# near-sdk-contract-tools

> Helpful functions and macros for developing smart contracts on NEAR Protocol.

Expand All @@ -17,15 +15,17 @@ Not to be confused with [`near-contract-standards`](https://crates.io/crates/nea

**WARNING:** This is still early software, and there may be breaking changes between versions. I'll try my best to keep the docs & changelogs up-to-date. Don't hesitate to create an issue if find anything wrong.

(Formerly known as [`near-contract-tools`](https://crates.io/crates/near-contract-tools).)

## Benefits

- requires fewer lines of code
- Without near-contract-tools, implementing fungible token events (mint, transfer, and burn) takes ~100 lines of code. Using near-contract-tools, you can [implement them in ~40 lines](https://youtu.be/kJzes_UP5j0?t=1058).
- Without near-sdk-contract-tools, implementing fungible token events (mint, transfer, and burn) takes ~100 lines of code. Using near-sdk-contract-tools, you can [implement them in ~40 lines](https://youtu.be/kJzes_UP5j0?t=1058).
- is more readable
- follows a consistent pattern
- Every time you use the events macro, it will [implement events in the same way](https://youtu.be/kJzes_UP5j0?t=1150). Without it, you’d need to ensure that `emit`, `emit_many`, etc all work (and work the same).
- is more thorough
- near-contract-standards is also not implementing traits, so that’s another improvement that near-contract-tools offers.
- near-contract-standards is also not implementing traits, so that’s another improvement that near-sdk-contract-tools offers.

You can think of this collection of common tools and patterns (mostly in the form of [derive macros](https://doc.rust-lang.org/reference/procedural-macros.html#derive-macros)) as sort of an OpenZeppelin for NEAR.

Expand All @@ -34,7 +34,7 @@ You can think of this collection of common tools and patterns (mostly in the for
```text
rustup target add wasm32-unknown-unknown
cargo init
cargo add near-contract-tools
cargo add near-sdk-contract-tools
cargo add near-sdk
# https://raen.dev/guide/intro/getting-set-up.html
cargo install raen
Expand Down Expand Up @@ -80,7 +80,7 @@ See also: [the full integration tests](tests/macros/mod.rs).

```rust
use near_sdk::{near_bindgen, AccountId};
use near_contract_tools::{owner::Owner, Owner};
use near_sdk_contract_tools::{owner::Owner, Owner};

#[derive(Owner)]
#[near_bindgen]
Expand Down Expand Up @@ -124,7 +124,7 @@ fn own_accept_owner(&mut self);
The `#[event]` macro can be applied to structs or enums.

```rust
use near_contract_tools::{event, standard::nep297::Event};
use near_sdk_contract_tools::{event, standard::nep297::Event};

#[event(standard = "nft", version = "1.0.0")]
pub struct MintEvent {
Expand All @@ -146,7 +146,7 @@ e.emit();
To create a contract that is compatible with the NEP-141 and NEP-148 standards, that emits standard-compliant (NEP-141, NEP-297) events.

```rust
use near_contract_tools::FungibleToken;
use near_sdk_contract_tools::FungibleToken;
use near_sdk::near_bindgen;

#[derive(FungibleToken)]
Expand All @@ -169,7 +169,7 @@ Standalone macros for each individual standard also exist.
One may wish to combine the features of multiple macros in one contract. All of the macros are written such that they will work in a standalone manner, so this should largely work without issue. However, sometimes it may be desirable for the macros to work in _combination_ with each other. For example, to make a fungible token pausable, use the fungible token hooks to require that a contract be unpaused before making a token transfer:

```rust
use near_contract_tools::{
use near_sdk_contract_tools::{
pause::Pause,
standard::nep141::{Nep141Hook, Nep141Transfer},
FungibleToken, Pause,
Expand All @@ -192,13 +192,13 @@ Note: Hooks can be disabled using `#[nep141(no_hooks)]` or `#[fungible_token(no_

### Custom Crates

If you are a library developer, have modified a crate that one of the `near-contract-tools` macros uses (like `serde` or `near-sdk`), or are otherwise using a crate under a different name, you can specify crate names in macros like so:
If you are a library developer, have modified a crate that one of the `near-sdk-contract-tools` macros uses (like `serde` or `near-sdk`), or are otherwise using a crate under a different name, you can specify crate names in macros like so:

```rust, ignore
#[event(
// ...
crate = "near_contract_tools",
macros = "near_contract_tools_macros",
crate = "near_sdk_contract_tools",
macros = "near_sdk_contract_tools_macros",
serde = "serde",
)]
// ...
Expand Down Expand Up @@ -255,6 +255,10 @@ See [src/slot.rs](src/slot.rs) They are very thin wrappers over a storage key. I

First, run `git config core.hooksPath hooks/` to install the hooks of this directory (without affecting how git hooks work for other projects).

## Audit

This library has been [audited](./documents/NEAR%20Contract%20Tools%20-%20Final%20-%2005.05.2023.pdf) by [Kudelski Security](https://www.kudelskisecurity.com/).

## Authors

- Jacob Lindahl [@sudo_build](https://twitter.com/sudo_build)
Binary file not shown.
6 changes: 3 additions & 3 deletions macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
authors = ["Jacob Lindahl <jacob@near.foundation>"]
categories = ["wasm"]
description = """
Macros for `near-contract-tools`
Macros for `near-sdk-contract-tools`
"""
edition = "2021"
license = "GPL-3.0"
name = "near-contract-tools-macros"
version = "0.7.2"
name = "near-sdk-contract-tools-macros"
version = "1.0.0"

[dependencies]
base64 = "0.13.0"
Expand Down
2 changes: 1 addition & 1 deletion macros/src/approval/simple_multisig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub fn expand(meta: SimpleMultisigMeta) -> Result<TokenStream, darling::Error> {
});

Ok(quote! {
impl #imp #me::approval::ApprovalManager<
impl #imp #me::approval::ApprovalManagerInternal<
#action,
#me::approval::simple_multisig::ApprovalState,
#me::approval::simple_multisig::Configuration<Self>,
Expand Down
28 changes: 21 additions & 7 deletions macros/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! Macros for near-contract-tools
//! Macros for near-sdk-contract-tools.

use darling::{FromDeriveInput, FromMeta};
use proc_macro::TokenStream;
Expand All @@ -14,11 +14,11 @@ mod standard;
mod upgrade;

fn default_crate_name() -> syn::Path {
syn::parse_str("::near_contract_tools").unwrap()
syn::parse_str("::near_sdk_contract_tools").unwrap()
}

fn default_macros() -> syn::Path {
syn::parse_str("::near_contract_tools").unwrap()
syn::parse_str("::near_sdk_contract_tools").unwrap()
}

fn default_near_sdk() -> syn::Path {
Expand Down Expand Up @@ -59,6 +59,18 @@ where
/// - `SHOUTY_SNAKE_CASE`
/// - `SHOUTY-KEBAB-CASE`
/// - `Title Case`
///
/// # Warning
///
/// Rename strategies are provided for convenience, and the actual string
/// transformation is delegated to the [`heck`](https://crates.io/crates/heck)
/// crate. It _is_ possible that unexpected name transformations or collisions
/// may occur, but it is _extremely unlikely_ if reasonable Rust naming
/// conventions are followed.
///
/// For example, `"HelloWorld"`, `"hello_world"`, and `"hello__world"`, when
/// snake-case-ified, are `"hello_world"`, so if you happened to name distinct
/// events thusly, the transformed names would collide.
#[proc_macro_derive(Nep297, attributes(nep297))]
pub fn derive_nep297(input: TokenStream) -> TokenStream {
make_derive(input, standard::nep297::expand)
Expand All @@ -85,8 +97,8 @@ pub fn derive_pause(input: TokenStream) -> TokenStream {

/// Adds role-based access control. No external methods are exposed.
///
/// The roles prefix must be specify a type using #[rbac(roles = "MyRoles")].
/// Typically "MyRoles" is an enum and it's variants are the different role
/// The roles prefix can be specified using `#[rbac(roles = "MyRoles")]`.
/// Typically `"MyRoles"` is an enum and its variants are the different role
/// names.
///
/// The storage key prefix for the fields can be optionally specified (default:
Expand All @@ -98,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_contract_tools::standard::nep141`).
/// and receiver functionality (see: [`near_sdk_contract_tools::standard::nep141`]).
///
/// The storage key prefix for the fields can be optionally specified (default:
/// `"~$141"`) using `#[nep141(storage_key = "<expression>")]`.
Expand Down Expand Up @@ -170,7 +182,9 @@ pub fn derive_simple_multisig(input: TokenStream) -> TokenStream {
make_derive(input, approval::simple_multisig::expand)
}

/// Smart `#[event]` macro
/// Smart `#[event]` macro.
///
/// See documentation on the [`derive@Nep297`] derive macro for more details.
#[proc_macro_attribute]
pub fn event(attr: TokenStream, item: TokenStream) -> TokenStream {
let attr = parse_macro_input!(attr as AttributeArgs);
Expand Down
12 changes: 6 additions & 6 deletions macros/src/owner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,36 +39,36 @@ pub fn expand(meta: OwnerMeta) -> Result<TokenStream, darling::Error> {
});

Ok(quote! {
impl #imp #me::owner::Owner for #ident #ty #wher {
impl #imp #me::owner::OwnerInternal for #ident #ty #wher {
#root
}

#[#near_sdk::near_bindgen]
impl #imp #me::owner::OwnerExternal for #ident #ty #wher {
fn own_get_owner(&self) -> Option<#near_sdk::AccountId> {
<Self as #me::owner::Owner>::slot_owner().read()
<Self as #me::owner::OwnerInternal>::slot_owner().read()
}

fn own_get_proposed_owner(&self) -> Option<#near_sdk::AccountId> {
<Self as #me::owner::Owner>::slot_proposed_owner().read()
<Self as #me::owner::OwnerInternal>::slot_proposed_owner().read()
}

#[payable]
fn own_renounce_owner(&mut self) {
#near_sdk::assert_one_yocto();
self.renounce_owner()
#me::owner::Owner::renounce_owner(self);
}

#[payable]
fn own_propose_owner(&mut self, account_id: Option<#near_sdk::AccountId>) {
#near_sdk::assert_one_yocto();
self.propose_owner(account_id);
#me::owner::Owner::propose_owner(self, account_id);
}

#[payable]
fn own_accept_owner(&mut self) {
#near_sdk::assert_one_yocto();
self.accept_owner();
#me::owner::Owner::accept_owner(self);
}
}
})
Expand Down
2 changes: 1 addition & 1 deletion macros/src/pause.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub fn expand(meta: PauseMeta) -> Result<TokenStream, darling::Error> {
});

Ok(quote! {
impl #imp #me::pause::Pause for #ident #ty #wher {
impl #imp #me::pause::PauseInternal for #ident #ty #wher {
#root
}

Expand Down
2 changes: 1 addition & 1 deletion macros/src/rbac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub fn expand(meta: RbacMeta) -> Result<TokenStream, darling::Error> {
});

Ok(quote! {
impl #imp #me::rbac::Rbac for #ident #ty #wher {
impl #imp #me::rbac::RbacInternal for #ident #ty #wher {
type Role = #roles;

#root
Expand Down
2 changes: 1 addition & 1 deletion macros/src/standard/nep141.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub fn expand(meta: Nep141Meta) -> Result<TokenStream, darling::Error> {
});

Ok(quote! {
impl #imp #me::standard::nep141::Nep141Controller for #ident #ty #wher {
impl #imp #me::standard::nep141::Nep141ControllerInternal for #ident #ty #wher {
#root
}

Expand Down
4 changes: 2 additions & 2 deletions macros/src/upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl FromMeta for HookBody {
.and_then(|s| syn::parse_str::<Expr>(s.as_str()).ok())
.map(|e| HookBody::Role(Box::new(e)))
.ok_or_else(|| {
darling::Error::custom(&format!(
darling::Error::custom(format!(
r#"Invalid value "{value}", expected "empty", "owner", or "role(...)""#,
))
})
Expand All @@ -51,7 +51,7 @@ impl FromMeta for Serializer {
match value {
"borsh" => Ok(Self::Borsh),
"jsonbase64" => Ok(Self::JsonBase64),
_ => Err(darling::Error::custom(&format!(
_ => Err(darling::Error::custom(format!(
r#"Invalid value "{value}", expected "borsh" or "jsonbase64""#
))),
}
Expand Down
2 changes: 1 addition & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[toolchain]
channel = "1.65"
channel = "1.66"
Loading