Skip to content

Commit

Permalink
Normalize & update dependencies (#131)
Browse files Browse the repository at this point in the history
* fix: remove external serde* deps; use near_sdk-provided serde* instead

* chore: use workspace dependencies for everything

* chore: upgrade strum deps

* chore: loosen version requirements

* chore: remove unused dependencies

* feat: nextest

* chore: use nextest in GH actions

* fix: gh actions syntax

* feat: use caching in ghaction
  • Loading branch information
encody authored Oct 26, 2023
1 parent d183adf commit 9e00d4d
Show file tree
Hide file tree
Showing 21 changed files with 148 additions and 75 deletions.
25 changes: 17 additions & 8 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@master
with:
toolchain: 1.72
Expand All @@ -27,7 +27,7 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@master
with:
toolchain: 1.72
Expand All @@ -36,25 +36,34 @@ jobs:
run: cargo clippy -- -D warnings
test:
runs-on: ubuntu-latest
env:
CARGO_TERM_COLOR: always

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@master
with:
toolchain: 1.72
- uses: taiki-e/install-action@v2
with:
tool: nextest
- uses: Swatinem/rust-cache@v2
- name: Run unit and integration tests
run: cargo test --workspace --exclude workspaces-tests
run: cargo nextest run --workspace --exclude workspaces-tests
workspaces-test:
runs-on: ubuntu-latest
env:
CARGO_TERM_COLOR: always

steps:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@master
with:
toolchain: 1.72
targets: wasm32-unknown-unknown
- uses: taiki-e/install-action@v2
with:
tool: cargo-make,nextest
- uses: Swatinem/rust-cache@v2
- name: Run workspaces tests
run: >
cd workspaces-tests;
cargo install cargo-make;
cargo make test;
run: cd workspaces-tests && cargo make nextest
38 changes: 26 additions & 12 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,33 +1,50 @@
[package]
authors = ["Jacob Lindahl <[email protected]>"]
authors.workspace = true
categories = ["wasm", "cryptography::cryptocurrencies"]
description = """
Helpful functions and macros for developing smart contracts on NEAR Protocol.
"""
documentation = "https://docs.rs/near-sdk-contract-tools"
edition = "2021"
edition.workspace = true
exclude = ["documents/"]
license = "GPL-3.0"
license.workspace = true
name = "near-sdk-contract-tools"
repository.workspace = true
version.workspace = true

[workspace]
members = [".", "macros", "workspaces-tests", "workspaces-tests-utils"]

[workspace.package]
authors = ["Jacob Lindahl <[email protected]>"]
edition = "2021"
license = "GPL-3.0"
repository = "https://github.com/near/near-sdk-contract-tools"
version = "1.1.0"

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

darling = "0.20"
heck = "0.4"
proc-macro2 = "1"
quote = "1.0"
strum = "0.25"
strum_macros = "0.25"
syn = "2.0"

# dev-dependencies
pretty_assertions = "1.4.0"
near-crypto = "0.15"
near-workspaces = "0.8"
pretty_assertions = "1"
tokio = "1"

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

[dev-dependencies]
Expand All @@ -43,9 +60,6 @@ unstable = ["near-sdk/unstable"]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]

[workspace]
members = [".", "macros", "workspaces-tests", "workspaces-tests-utils"]

[profile.release]
codegen-units = 1
debug = false
Expand Down
27 changes: 13 additions & 14 deletions macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
[package]
authors = ["Jacob Lindahl <[email protected]>"]
authors.workspace = true
categories = ["wasm"]
description = """
Macros for `near-sdk-contract-tools`
"""
edition = "2021"
license = "GPL-3.0"
documentation = "https://docs.rs/near-sdk-contract-tools-macros"
edition.workspace = true
license.workspace = true
name = "near-sdk-contract-tools-macros"
version = "1.1.0"
repository.workspace = true
version.workspace = true

[dependencies]
base64 = "0.13.0"
darling = "0.14.1"
heck = "0.4.0"
once_cell = "1.16.0"
proc-macro2 = "1.0.43"
quote = "1.0.21"
regex = "1.6.0"
strum = "0.24.1"
strum_macros = "0.24.3"
syn = "1.0.99"
darling.workspace = true
heck.workspace = true
proc-macro2.workspace = true
quote.workspace = true
strum.workspace = true
strum_macros.workspace = true
syn.workspace = true

[lib]
proc-macro = true
13 changes: 9 additions & 4 deletions macros/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//! Macros for near-sdk-contract-tools.

use darling::{FromDeriveInput, FromMeta};
use darling::{ast::NestedMeta, FromDeriveInput, FromMeta};
use proc_macro::TokenStream;
use syn::{parse_macro_input, AttributeArgs, DeriveInput, Item};
use syn::{parse_macro_input, DeriveInput, Item};

mod approval;
mod escrow;
Expand All @@ -27,7 +27,7 @@ fn default_near_sdk() -> syn::Path {
}

fn default_serde() -> syn::Path {
syn::parse_str("::serde").unwrap()
syn::parse_str("::near_sdk::serde").unwrap()
}

fn unitify(ty: Option<syn::Type>) -> syn::Type {
Expand Down Expand Up @@ -248,7 +248,12 @@ pub fn derive_simple_multisig(input: TokenStream) -> TokenStream {
/// 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);
let attr = match NestedMeta::parse_meta_list(attr.into()) {
Ok(v) => v,
Err(e) => {
return TokenStream::from(darling::Error::from(e).write_errors());
}
};
let item = parse_macro_input!(item as Item);

standard::event::EventAttributeMeta::from_list(&attr)
Expand Down
24 changes: 10 additions & 14 deletions macros/src/upgrade.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use darling::{FromDeriveInput, FromMeta};
use once_cell::sync::OnceCell;
use proc_macro2::TokenStream;
use quote::quote;
use regex::Regex;
use syn::Expr;

#[derive(Debug, Clone)]
Expand All @@ -19,23 +17,21 @@ impl FromMeta for HookBody {
}

fn from_string(value: &str) -> darling::Result<Self> {
static REGEX: OnceCell<Regex> = OnceCell::new();

if value == "empty" {
Ok(HookBody::Empty)
} else if value == "owner" {
Ok(HookBody::Owner)
} else if let Some(b) = value
.strip_prefix("role(")
.and_then(|s| s.strip_suffix(')'))
.and_then(|s| syn::parse_str::<Expr>(s).ok())
.map(|e| HookBody::Role(Box::new(e)))
{
Ok(b)
} else {
let r = REGEX.get_or_init(|| Regex::new(r"^role\((.+)\)$").unwrap());
r.captures(value)
.and_then(|c| c.get(1))
.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!(
r#"Invalid value "{value}", expected "empty", "owner", or "role(...)""#,
))
})
Err(darling::Error::custom(format!(
r#"Invalid value "{value}", expected "empty", "owner", or "role(...)""#,
)))
}
}
}
Expand Down
9 changes: 6 additions & 3 deletions src/approval/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

use near_sdk::{
borsh::{self, BorshDeserialize, BorshSerialize},
env, require, AccountId, BorshStorageKey,
env, require,
serde::{Deserialize, Serialize},
AccountId, BorshStorageKey,
};
use serde::{Deserialize, Serialize};
use thiserror::Error;

use crate::{slot::Slot, DefaultStorageKey};
Expand Down Expand Up @@ -64,6 +65,7 @@ pub trait ApprovalConfiguration<A, S> {
/// An action request is composed of an action that will be executed when the
/// associated approval state is satisfied
#[derive(Serialize, Deserialize, BorshSerialize, BorshDeserialize, Debug)]
#[serde(crate = "near_sdk::serde")]
pub struct ActionRequest<A, S> {
/// The action that will be executed when the approval state is
/// fulfilled
Expand Down Expand Up @@ -336,11 +338,11 @@ mod tests {
use near_sdk::{
borsh::{self, BorshDeserialize, BorshSerialize},
near_bindgen,
serde::Serialize,
test_utils::VMContextBuilder,
testing_env, AccountId, BorshStorageKey,
};
use near_sdk_contract_tools_macros::Rbac;
use serde::Serialize;

use crate::{rbac::Rbac, slot::Slot};

Expand Down Expand Up @@ -405,6 +407,7 @@ mod tests {
}

#[derive(BorshSerialize, BorshDeserialize, Serialize, Default, Debug)]
#[serde(crate = "near_sdk::serde")]
struct MultisigApprovalState {
pub approved_by: Vec<AccountId>,
}
Expand Down
4 changes: 3 additions & 1 deletion src/approval/native_transaction_action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
use near_sdk::{
borsh::{self, BorshDeserialize, BorshSerialize},
json_types::{Base64VecU8, U128, U64},
serde::{Deserialize, Serialize},
AccountId, Gas, Promise,
};
use serde::{Deserialize, Serialize};

/// Every native NEAR action can be mapped to a Promise action.
/// NOTE: The native ADD_KEY action is split into two: one for adding a
/// full-access key, one for a function call access key.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize, BorshSerialize, BorshDeserialize)]
#[serde(crate = "near_sdk::serde")]
pub enum PromiseAction {
/// Native CREATE_ACCOUNT action
CreateAccount,
Expand Down Expand Up @@ -79,6 +80,7 @@ pub enum PromiseAction {
/// A native protocol-level transaction that (de)serializes into many different
/// formats
#[derive(BorshSerialize, BorshDeserialize, Serialize, Deserialize, Eq, PartialEq, Clone, Debug)]
#[serde(crate = "near_sdk::serde")]
pub struct NativeTransactionAction {
/// Receiver of the transaction
pub receiver_id: AccountId,
Expand Down
7 changes: 5 additions & 2 deletions src/approval/simple_multisig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ use std::marker::PhantomData;

use near_sdk::{
borsh::{self, BorshDeserialize, BorshSerialize},
env, AccountId,
env,
serde::{Deserialize, Serialize},
AccountId,
};
use serde::{Deserialize, Serialize};
use thiserror::Error;

use super::{ActionRequest, ApprovalConfiguration};
Expand All @@ -24,6 +25,7 @@ pub trait AccountAuthorizer {

/// M (threshold) of N approval scheme
#[derive(BorshSerialize, BorshDeserialize, Serialize, Deserialize, Clone, Debug)]
#[serde(crate = "near_sdk::serde")]
pub struct Configuration<Au: AccountAuthorizer> {
/// How many approvals are required?
pub threshold: u8,
Expand Down Expand Up @@ -61,6 +63,7 @@ impl<Au: AccountAuthorizer> Configuration<Au> {

/// Approval state for simple multisig
#[derive(BorshSerialize, BorshDeserialize, Serialize, Deserialize, Debug)]
#[serde(crate = "near_sdk::serde")]
pub struct ApprovalState {
/// List of accounts that have approved an action thus far
pub approved_by: Vec<AccountId>,
Expand Down
7 changes: 4 additions & 3 deletions src/standard/nep141/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use near_sdk_contract_tools_macros::event;
#[event(
crate = "crate",
macros = "crate",
serde = "serde",
standard = "nep141",
version = "1.0.0"
)]
Expand All @@ -24,11 +23,11 @@ pub enum Nep141Event {
/// Decrease in total_supply.
FtBurn(Vec<FtBurnData>),
}
use near_sdk::{json_types::U128, AccountId};
use serde::Serialize;
use near_sdk::{json_types::U128, serde::Serialize, AccountId};

/// Individual mint metadata
#[derive(Serialize, Debug, Clone)]
#[serde(crate = "near_sdk::serde")]
pub struct FtMintData {
/// Address to which new tokens were minted
pub owner_id: AccountId,
Expand All @@ -41,6 +40,7 @@ pub struct FtMintData {

/// Individual transfer metadata
#[derive(Serialize, Debug, Clone)]
#[serde(crate = "near_sdk::serde")]
pub struct FtTransferData {
/// Account ID of the sender
pub old_owner_id: AccountId,
Expand All @@ -55,6 +55,7 @@ pub struct FtTransferData {

/// Individual burn metadata
#[derive(Serialize, Debug, Clone)]
#[serde(crate = "near_sdk::serde")]
pub struct FtBurnData {
/// Account ID from which tokens were burned
pub owner_id: AccountId,
Expand Down
Loading

0 comments on commit 9e00d4d

Please sign in to comment.