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

Split contract-standards crate into crates for each standard #1155

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ members = [
"near-sdk",
"near-sdk-macros",
"near-contract-standards",
"near-sys",
"near-sys",
"near-ft-standard",
"near-storage-standard",
]
exclude = ["examples/"]

Expand Down
3 changes: 2 additions & 1 deletion examples/fungible-token/ft/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ crate-type = ["cdylib"]

[dependencies]
near-sdk = { path = "../../../near-sdk" }
near-contract-standards = { path = "../../../near-contract-standards" }
near-storage-standard = { path = "../../../near-storage-standard" }
near-ft-standard = { path = "../../../near-ft-standard" }
10 changes: 5 additions & 5 deletions examples/fungible-token/ft/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ NOTES:
- To prevent the deployed contract from being modified or deleted, it should not have any access
keys on its account.
*/
use near_contract_standards::fungible_token::metadata::{
use near_ft_standard::metadata::{
FungibleTokenMetadata, FungibleTokenMetadataProvider, FT_METADATA_SPEC,
};
use near_contract_standards::fungible_token::{
use near_ft_standard::{
FungibleToken, FungibleTokenCore, FungibleTokenResolver,
};
use near_contract_standards::storage_management::{
use near_storage_standard::{
StorageBalance, StorageBalanceBounds, StorageManagement,
};
use near_sdk::borsh::{BorshDeserialize, BorshSerialize};
Expand Down Expand Up @@ -83,7 +83,7 @@ impl Contract {
this.token.internal_register_account(&owner_id);
this.token.internal_deposit(&owner_id, total_supply.into());

near_contract_standards::fungible_token::events::FtMint {
near_ft_standard::events::FtMint {
owner_id: &owner_id,
amount: total_supply,
memo: Some("new tokens are minted"),
Expand Down Expand Up @@ -184,7 +184,7 @@ impl FungibleTokenMetadataProvider for Contract {

#[cfg(all(test, not(target_arch = "wasm32")))]
mod tests {
use near_contract_standards::fungible_token::Balance;
use near_ft_standard::Balance;
use near_sdk::test_utils::{accounts, VMContextBuilder};
use near_sdk::testing_env;

Expand Down
2 changes: 1 addition & 1 deletion examples/fungible-token/test-contract-defi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ crate-type = ["cdylib"]

[dependencies]
near-sdk = { path = "../../../near-sdk" }
near-contract-standards = { path = "../../../near-contract-standards" }
near-ft-standard = { path = "../../../near-ft-standard" }
2 changes: 1 addition & 1 deletion examples/fungible-token/test-contract-defi/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*!
Some hypothetical DeFi contract that will do smart things with the transferred tokens
*/
use near_contract_standards::fungible_token::{receiver::FungibleTokenReceiver, Balance};
use near_ft_standard::{receiver::FungibleTokenReceiver, Balance};
use near_sdk::borsh::{BorshDeserialize, BorshSerialize};
use near_sdk::json_types::U128;
use near_sdk::{env, log, near_bindgen, require, AccountId, Gas, PanicOnDefault, PromiseOrValue};
Expand Down
1 change: 1 addition & 0 deletions near-contract-standards/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ NEAR smart contracts standard library.
[dependencies]
near-sdk = { path = "../near-sdk", version = "~5.0.0", default-features = false, features = ["legacy"] }


[dev-dependencies]
near-sdk = { path = "../near-sdk", default-features = false, features = ["unit-testing"] }

Expand Down
1 change: 0 additions & 1 deletion near-contract-standards/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use near_sdk::serde_json;
#[serde(rename_all = "snake_case")]
pub(crate) enum NearEvent<'a> {
Nep171(crate::non_fungible_token::events::Nep171Event<'a>),
Nep141(crate::fungible_token::events::Nep141Event<'a>),
}

impl<'a> NearEvent<'a> {
Expand Down
5 changes: 1 addition & 4 deletions near-contract-standards/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
// We want to enable all clippy lints, but some of them generate false positives.
#![allow(clippy::missing_const_for_fn, clippy::redundant_pub_crate)]

/// Fungible tokens as described in [by the spec](https://nomicon.io/Standards/FungibleToken/README.html).
pub mod fungible_token;
/// Non-fungible tokens as described in [by the spec](https://nomicon.io/Standards/NonFungibleToken/README.html).
pub mod non_fungible_token;
/// Storage management deals with handling [state storage](https://docs.near.org/docs/concepts/storage-staking) on NEAR. This follows the [storage management standard](https://nomicon.io/Standards/StorageManagement.html).
pub mod storage_management;

/// This upgrade standard is a use case where a staging area exists for a WASM
/// blob, allowing it to be stored for a period of time before deployed.
#[deprecated(
Expand Down
16 changes: 16 additions & 0 deletions near-ft-standard/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "near-ft-standard"
edition = "2021"
version.workspace = true

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
near-sdk = { path = "../near-sdk", version = "~5.0.0", default-features = false, features = ["legacy"] }
near-storage-standard = { path = "../near-storage-standard"}

[dev-dependencies]
near-sdk = { path = "../near-sdk", default-features = false, features = ["unit-testing"] }

[features]
default = []
abi = ["near-sdk/abi"]
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::fungible_token::core::FungibleTokenCore;
use crate::fungible_token::events::{FtBurn, FtTransfer};
use crate::fungible_token::receiver::ext_ft_receiver;
use crate::fungible_token::resolver::{ext_ft_resolver, FungibleTokenResolver};
use crate::core::FungibleTokenCore;
use crate::events::{FtBurn, FtTransfer};
use crate::receiver::ext_ft_receiver;
use crate::resolver::{ext_ft_resolver, FungibleTokenResolver};
use near_sdk::borsh::{BorshDeserialize, BorshSerialize};
use near_sdk::collections::LookupMap;
use near_sdk::json_types::U128;
Expand Down
30 changes: 30 additions & 0 deletions near-ft-standard/src/event.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use near_sdk::env;
use near_sdk::serde::Serialize;
use near_sdk::serde_json;

#[derive(Serialize, Debug)]
#[serde(crate = "near_sdk::serde")]
#[serde(tag = "standard")]
#[must_use = "don't forget to `.emit()` this event"]
#[serde(rename_all = "snake_case")]
pub(crate) enum NearEvent<'a> {
Nep141(crate::events::Nep141Event<'a>),
}

impl<'a> NearEvent<'a> {
fn to_json_string(&self) -> String {
// Events cannot fail to serialize so fine to panic on error
#[allow(clippy::redundant_closure)]
serde_json::to_string(self).ok().unwrap_or_else(|| env::abort())
}

fn to_json_event_string(&self) -> String {
format!("EVENT_JSON:{}", self.to_json_string())
}

/// Logs the event to the host. This is required to ensure that the event is triggered
/// and to consume the event.
pub(crate) fn emit(self) {
near_sdk::env::log_str(&self.to_json_event_string());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
//! # Examples
//! See [`FungibleTokenCore`] and [`FungibleTokenResolver`] for example usage and [`FungibleToken`]
//! for core standard implementation.

pub mod core;
pub mod core_impl;
pub(crate) mod event;
pub mod events;
pub mod macros;
pub mod metadata;
pub mod receiver;
pub mod resolver;
pub mod storage_impl;

pub use crate::fungible_token::core::FungibleTokenCore;
pub use crate::core::FungibleTokenCore;
pub use core_impl::{Balance, FungibleToken};
pub use resolver::FungibleTokenResolver;
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
)]
macro_rules! impl_fungible_token_core {
($contract: ident, $token: ident $(, $on_tokens_burned_fn:ident)?) => {
use $crate::fungible_token::core::FungibleTokenCore;
use $crate::fungible_token::resolver::FungibleTokenResolver;
use $crate::core::FungibleTokenCore;
use $crate::resolver::FungibleTokenResolver;

#[near_bindgen]
impl FungibleTokenCore for $contract {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::fungible_token::{Balance, FungibleToken};
use crate::storage_management::{StorageBalance, StorageBalanceBounds, StorageManagement};
use crate::{Balance, FungibleToken};
use near_sdk::{assert_one_yocto, env, log, AccountId, NearToken, Promise};
use near_storage_standard::{StorageBalance, StorageBalanceBounds, StorageManagement};

impl FungibleToken {
/// Internal method that returns the Account ID and the balance in case the account was
Expand Down
9 changes: 9 additions & 0 deletions near-storage-standard/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "near-storage-standard"
edition = "2021"
version.workspace = true

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
near-sdk = { path = "../near-sdk", version = "~5.0.0", default-features = false, features = ["legacy"] }
Loading