Skip to content

Commit

Permalink
BREAKING: Upgrade near-sdk to ^5.1 (#141)
Browse files Browse the repository at this point in the history
* feat: near-sdk v5 compatibility

* fix: BREAKING use collections::* since store:: versions are deprecated

* chore: upgrade package versions

* chore: fmt

* fix: patch parity-secp256k1

* fix: update rust version

* chore: remove near sdk 4 support

* fix: tests pass for removal of near sdk 4 items

* chore: cleanup clippy & fmt

* chore: remove remaining compat macros

* fix: div instead of mul

* fix: storage accounting during withdrawal

* chore: replace forgotten U128 in test contract with NearToken

* chore: replace #[near_bindgen] with new #[near] macro

* chore: replace all instances of #[near_bindgen] with #[near] in entire project

* chore: fmt

* chore: workspaces tests inherits near-sdk directly
  • Loading branch information
encody authored May 3, 2024
1 parent 9158096 commit 3df50f2
Show file tree
Hide file tree
Showing 94 changed files with 1,137 additions and 1,196 deletions.
5 changes: 0 additions & 5 deletions .cargo/config.toml

This file was deleted.

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@v4
- uses: dtolnay/rust-toolchain@master
with:
toolchain: 1.72
toolchain: 1.74
components: rustfmt
- name: Check formatting
run: >
Expand All @@ -30,7 +30,7 @@ jobs:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@master
with:
toolchain: 1.72
toolchain: 1.74
components: clippy
- name: Run linter
run: cargo clippy -- -D warnings
Expand All @@ -43,7 +43,7 @@ jobs:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@master
with:
toolchain: 1.72
toolchain: 1.74
- uses: taiki-e/install-action@v2
with:
tool: nextest
Expand All @@ -61,7 +61,7 @@ jobs:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@master
with:
toolchain: 1.72
toolchain: 1.74
targets: wasm32-unknown-unknown
- uses: taiki-e/install-action@v2
with:
Expand Down
25 changes: 17 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,33 @@ version = "2.1.0"

[workspace.dependencies]
# normal dependencies
near-sdk = { version = "4.1.1", default-features = false }
near-sdk = { version = "5.1.0", default-features = false }
near-sdk-contract-tools-macros = { version = "=2.1.0", path = "./macros" }
thiserror = "1"

# macro dependencies
darling = "0.20"
heck = "0.4"
heck = "0.5"
proc-macro2 = "1"
quote = "1.0"
strum = "0.25"
strum_macros = "0.25"
strum = "0.26"
strum_macros = "0.26"
syn = "2.0"

# test/dev-dependencies
near-crypto = "0.15"
near-workspaces = "0.8"
near-crypto = "0.21"
near-workspaces = "0.10"
pretty_assertions = "1"
tokio = "1"

[workspace.lints.clippy]
# pedantic = "warn"

[workspace.lints.rust]
missing-docs = "warn"

[dependencies]
near-sdk.workspace = true
near-sdk = { workspace = true, default-features = false, features = ["legacy"] }
near-sdk-contract-tools-macros.workspace = true
thiserror.workspace = true

Expand All @@ -58,7 +64,7 @@ near-sdk = { workspace = true, default-features = false, features = [
unstable = ["near-sdk/unstable"]

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

[profile.release]
Expand All @@ -68,3 +74,6 @@ lto = true
opt-level = "z"
overflow-checks = true
panic = "abort"

[lints]
workspace = true
58 changes: 22 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,15 @@
## NFT

```diff
use near_sdk::{
borsh::{self, BorshDeserialize, BorshSerialize},
near_bindgen, PanicOnDefault,
};
use near_sdk::{near, PanicOnDefault};
+ use near_sdk_contract_tools::nft::*;

#[derive(BorshSerialize, BorshDeserialize, PanicOnDefault)]
#[derive(PanicOnDefault)]
+ #[derive(NonFungibleToken)]
#[near_bindgen]
#[near(contract_state)]
pub struct MyNftContract {}

#[near_bindgen]
#[near]
impl MyNftContract {
#[init]
pub fn new() -> Self {
Expand All @@ -34,18 +31,15 @@ impl MyNftContract {
## FT

```diff
use near_sdk::{
borsh::{self, BorshDeserialize, BorshSerialize},
near_bindgen, PanicOnDefault,
};
use near_sdk::{near, PanicOnDefault};
+ use near_sdk_contract_tools::ft::*;

#[derive(BorshSerialize, BorshDeserialize, PanicOnDefault)]
#[derive(PanicOnDefault)]
+ #[derive(FungibleToken)]
#[near_bindgen]
#[near(contract_state)]
pub struct MyFtContract {}

#[near_bindgen]
#[near]
impl MyFtContract {
#[init]
pub fn new() -> Self {
Expand Down Expand Up @@ -96,16 +90,16 @@ See also: [the full integration tests](tests/macros/mod.rs).
### Owner

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

#[derive(Owner)]
#[near_bindgen]
#[derive(Owner, PanicOnDefault)]
#[near(contract_state)]
struct Contract {
// ...
}

#[near_bindgen]
#[near]
impl Contract {
#[init]
pub fn new(owner_id: AccountId) -> Self {
Expand Down Expand Up @@ -164,13 +158,13 @@ To create a contract that is compatible with the [NEP-141][nep141], [NEP-145][ne

```rust
use near_sdk_contract_tools::ft::*;
use near_sdk::near_bindgen;
use near_sdk::{near, PanicOnDefault};

#[derive(FungibleToken)]
#[near_bindgen]
#[derive(FungibleToken, PanicOnDefault)]
#[near(contract_state)]
struct MyFt {}

#[near_bindgen]
#[near]
impl MyFt {
#[init]
pub fn new() -> Self {
Expand All @@ -194,15 +188,11 @@ Standalone macros for each individual standard also exist.
Use the `NonFungibleToken` derive macro to implement [NEP-145][nep145], [NEP-171][nep171], [NEP-177][nep177], [NEP-178][nep178], and [NEP-181][nep181], with [NEP-297][nep297] events.

```rust
use near_sdk::{
borsh::{self, BorshSerialize, BorshDeserialize},
PanicOnDefault,
near_bindgen,
};
use near_sdk::{near, PanicOnDefault};
use near_sdk_contract_tools::nft::*;

#[derive(BorshSerialize, BorshDeserialize, PanicOnDefault, NonFungibleToken)]
#[near_bindgen]
#[derive(NonFungibleToken, PanicOnDefault)]
#[near(contract_state)]
pub struct MyNft {}
```

Expand All @@ -216,15 +206,11 @@ use near_sdk_contract_tools::{
pause::{*, hooks::PausableHook},
Pause,
};
use near_sdk::{
borsh::{self, BorshSerialize, BorshDeserialize},
PanicOnDefault,
near_bindgen,
};
use near_sdk::{near, PanicOnDefault};

#[derive(BorshSerialize, BorshDeserialize, PanicOnDefault, FungibleToken, Pause)]
#[derive(FungibleToken, Pause, PanicOnDefault)]
#[fungible_token(all_hooks = "PausableHook")]
#[near_bindgen]
#[near(contract_state)]
struct Contract {}
```

Expand Down
3 changes: 3 additions & 0 deletions macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,6 @@ syn.workspace = true

[lib]
proc-macro = true

[lints]
workspace = true
4 changes: 2 additions & 2 deletions macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ pub fn derive_nep297(input: TokenStream) -> TokenStream {
}

/// Creates a managed, lazily-loaded `Owner` implementation for the targeted
/// `#[near_bindgen]` struct.
/// `#[near(contract_state)]` struct.
///
/// The storage key prefix for the fields can be optionally specified (default:
/// `"~o"`) using `#[owner(storage_key = "<expression>")]`.
Expand Down Expand Up @@ -282,7 +282,7 @@ pub fn derive_upgrade(input: TokenStream) -> TokenStream {
}

/// Creates a managed, lazily-loaded `Escrow` implementation for the targeted
/// `#[near_bindgen]` struct.
/// `#[near(contract_state)]` struct.
///
/// Fields include:
/// - `id` - the type required for id, must be `borsh::BorshSerialize` & `serde::Serialize`, for events
Expand Down
6 changes: 3 additions & 3 deletions macros/src/migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ pub fn expand(meta: MigrateMeta) -> Result<TokenStream, darling::Error> {
type NewSchema = #to;
}

#[#near_sdk::near_bindgen]
impl #imp #me::migrate::MigrateExternal for #ident #ty #wh {
#[#near_sdk::near]
impl #imp #ident #ty #wh {
#[init(ignore_state)]
fn migrate() -> Self {
pub fn migrate() -> Self {
let old_state = <#ident as #me::migrate::MigrateController>::deserialize_old_schema();
<#ident as #me::migrate::MigrateHook>::on_migrate(
old_state,
Expand Down
2 changes: 1 addition & 1 deletion macros/src/owner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub fn expand(meta: OwnerMeta) -> Result<TokenStream, darling::Error> {
#root
}

#[#near_sdk::near_bindgen]
#[#near_sdk::near]
impl #imp #me::owner::OwnerExternal for #ident #ty #wher {
fn own_get_owner(&self) -> Option<#near_sdk::AccountId> {
<Self as #me::owner::OwnerInternal>::slot_owner().read()
Expand Down
2 changes: 1 addition & 1 deletion macros/src/pause.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub fn expand(meta: PauseMeta) -> Result<TokenStream, darling::Error> {
#root
}

#[#near_sdk::near_bindgen]
#[#near_sdk::near]
impl #imp #me::pause::PauseExternal for #ident #ty #wher {
fn paus_is_paused(&self) -> bool {
<Self as #me::pause::Pause>::is_paused()
Expand Down
11 changes: 5 additions & 6 deletions macros/src/standard/nep141.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ pub fn expand(meta: Nep141Meta) -> Result<TokenStream, darling::Error> {
#root
}

#[#near_sdk::near_bindgen]
#[#near_sdk::near]
impl #imp #me::standard::nep141::Nep141 for #ident #ty #wher {
#[payable]
fn ft_transfer(
Expand Down Expand Up @@ -130,13 +130,12 @@ pub fn expand(meta: Nep141Meta) -> Result<TokenStream, darling::Error> {
.unwrap_or_else(|e| #near_sdk::env::panic_str(&e.to_string()));

let receiver_gas = prepaid_gas
.0
.checked_sub(GAS_FOR_FT_TRANSFER_CALL.0)
.checked_sub(GAS_FOR_FT_TRANSFER_CALL)
.unwrap_or_else(|| #near_sdk::env::panic_str("Prepaid gas underflow."));

// Initiating receiver's call and the callback
ext_nep141_receiver::ext(transfer.receiver_id.clone())
.with_static_gas(receiver_gas.into())
.with_static_gas(receiver_gas)
.ft_on_transfer(transfer.sender_id.clone(), transfer.amount.into(), msg.clone())
.then(
ext_nep141_resolver::ext(#near_sdk::env::current_account_id())
Expand All @@ -158,7 +157,7 @@ pub fn expand(meta: Nep141Meta) -> Result<TokenStream, darling::Error> {
}
}

#[#near_sdk::near_bindgen]
#[#near_sdk::near]
impl #imp #me::standard::nep141::Nep141Resolver for #ident #ty #wher {
#[private]
fn ft_resolve_transfer(
Expand All @@ -175,7 +174,6 @@ pub fn expand(meta: Nep141Meta) -> Result<TokenStream, darling::Error> {
let ft_on_transfer_promise_result = env::promise_result(0);

let unused_amount = match ft_on_transfer_promise_result {
PromiseResult::NotReady => env::abort(),
PromiseResult::Successful(value) => {
if let Ok(U128(unused_amount)) = serde_json::from_slice::<U128>(&value) {
std::cmp::min(amount, unused_amount)
Expand All @@ -184,6 +182,7 @@ pub fn expand(meta: Nep141Meta) -> Result<TokenStream, darling::Error> {
}
}
PromiseResult::Failed => amount,
_ => env::abort(),
};

let refunded_amount = if unused_amount > 0 {
Expand Down
Loading

0 comments on commit 3df50f2

Please sign in to comment.