Skip to content

Commit

Permalink
contract standards
Browse files Browse the repository at this point in the history
  • Loading branch information
PolyProgrammist committed Feb 27, 2024
1 parent 995198c commit b06850d
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 51 deletions.
6 changes: 2 additions & 4 deletions near-contract-standards/src/fungible_token/core_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ 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 near_sdk::borsh::{BorshDeserialize, BorshSerialize};
use near_sdk::collections::LookupMap;
use near_sdk::json_types::U128;
use near_sdk::{
assert_one_yocto, env, log, require, AccountId, Gas, IntoStorageKey, PromiseOrValue,
near, assert_one_yocto, env, log, require, AccountId, Gas, IntoStorageKey, PromiseOrValue,
PromiseResult, StorageUsage,
};

Expand All @@ -26,8 +25,7 @@ pub type Balance = u128;
/// - AccountRegistrar -- interface for an account to register and unregister
///
/// For example usage, see examples/fungible-token/src/lib.rs.
#[derive(BorshDeserialize, BorshSerialize)]
#[borsh(crate = "near_sdk::borsh")]
#[near]
pub struct FungibleToken {
/// AccountID -> Account balance.
pub accounts: LookupMap<AccountId, Balance>,
Expand Down
6 changes: 3 additions & 3 deletions near-contract-standards/src/fungible_token/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ macro_rules! impl_fungible_token_core {
use $crate::fungible_token::core::FungibleTokenCore;
use $crate::fungible_token::resolver::FungibleTokenResolver;

#[near_bindgen]
#[near]
impl FungibleTokenCore for $contract {
#[payable]
fn ft_transfer(
Expand Down Expand Up @@ -41,7 +41,7 @@ macro_rules! impl_fungible_token_core {
}
}

#[near_bindgen]
#[near]
impl FungibleTokenResolver for $contract {
#[private]
fn ft_resolve_transfer(
Expand Down Expand Up @@ -76,7 +76,7 @@ macro_rules! impl_fungible_token_storage {
StorageManagement, StorageBalance, StorageBalanceBounds
};

#[near_bindgen]
#[near]
impl StorageManagement for $contract {
#[payable]
fn storage_deposit(
Expand Down
9 changes: 3 additions & 6 deletions near-contract-standards/src/fungible_token/metadata.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
use near_sdk::borsh::{BorshDeserialize, BorshSerialize};
use near_sdk::json_types::Base64VecU8;
use near_sdk::serde::{Deserialize, Serialize};
use near_sdk::{ext_contract, require, NearSchema};
use near_sdk::{ext_contract, near, require, NearSchema};

pub const FT_METADATA_SPEC: &str = "ft-1.0.0";

#[derive(Clone, BorshDeserialize, BorshSerialize, Deserialize, Serialize, NearSchema)]
#[serde(crate = "near_sdk::serde")]
#[borsh(crate = "near_sdk::borsh")]
#[derive(Clone, NearSchema)]
#[near]
pub struct FungibleTokenMetadata {
pub spec: String,
pub name: String,
Expand Down
10 changes: 4 additions & 6 deletions near-contract-standards/src/non_fungible_token/core/core_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ use crate::non_fungible_token::events::{NftMint, NftTransfer};
use crate::non_fungible_token::metadata::TokenMetadata;
use crate::non_fungible_token::token::{Token, TokenId};
use crate::non_fungible_token::utils::{refund_approved_account_ids, refund_deposit_to_account};
use near_sdk::borsh::{BorshDeserialize, BorshSerialize};
use near_sdk::collections::{LookupMap, TreeMap, UnorderedSet};
use near_sdk::json_types::Base64VecU8;
use near_sdk::{
assert_one_yocto, env, require, AccountId, BorshStorageKey, Gas, IntoStorageKey,
near, assert_one_yocto, env, require, AccountId, BorshStorageKey, Gas, IntoStorageKey,
PromiseOrValue, PromiseResult, StorageUsage,
};
use std::collections::HashMap;
Expand All @@ -28,8 +27,7 @@ const GAS_FOR_NFT_TRANSFER_CALL: Gas = Gas::from_tgas(30);
/// - NonFungibleTokenMetadata -- return metadata for the token in NEP-177, up to contract to implement.
///
/// For example usage, see examples/non-fungible-token/src/lib.rs.
#[derive(BorshDeserialize, BorshSerialize)]
#[borsh(crate = "near_sdk::borsh")]
#[near]
pub struct NonFungibleToken {
// owner of contract
pub owner_id: AccountId,
Expand All @@ -51,8 +49,8 @@ pub struct NonFungibleToken {
pub next_approval_id_by_id: Option<LookupMap<TokenId, u64>>,
}

#[derive(BorshStorageKey, BorshSerialize)]
#[borsh(crate = "near_sdk::borsh")]
#[derive(BorshStorageKey)]
#[near]
pub enum StorageKey {
TokensPerOwner { account_hash: Vec<u8> },
}
Expand Down
8 changes: 4 additions & 4 deletions near-contract-standards/src/non_fungible_token/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ macro_rules! impl_non_fungible_token_core {
use $crate::non_fungible_token::core::NonFungibleTokenCore;
use $crate::non_fungible_token::core::NonFungibleTokenResolver;

#[near_bindgen]
#[near]
impl NonFungibleTokenCore for $contract {
#[payable]
fn nft_transfer(
Expand Down Expand Up @@ -39,7 +39,7 @@ macro_rules! impl_non_fungible_token_core {
}
}

#[near_bindgen]
#[near]
impl NonFungibleTokenResolver for $contract {
#[private]
fn nft_resolve_transfer(
Expand Down Expand Up @@ -70,7 +70,7 @@ macro_rules! impl_non_fungible_token_approval {
($contract: ident, $token: ident) => {
use $crate::non_fungible_token::approval::NonFungibleTokenApproval;

#[near_bindgen]
#[near]
impl NonFungibleTokenApproval for $contract {
#[payable]
fn nft_approve(
Expand Down Expand Up @@ -114,7 +114,7 @@ macro_rules! impl_non_fungible_token_enumeration {
($contract: ident, $token: ident) => {
use $crate::non_fungible_token::enumeration::NonFungibleTokenEnumeration;

#[near_bindgen]
#[near]
impl NonFungibleTokenEnumeration for $contract {
fn nft_total_supply(&self) -> near_sdk::json_types::U128 {
self.$token.nft_total_supply()
Expand Down
18 changes: 3 additions & 15 deletions near-contract-standards/src/non_fungible_token/metadata.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use near_sdk::borsh::{BorshDeserialize, BorshSerialize};
use near_sdk::json_types::Base64VecU8;
use near_sdk::serde::{Deserialize, Serialize};
use near_sdk::{require, NearSchema};
use near_sdk::{require, near, NearSchema};

/// This spec can be treated like a version of the standard.
pub const NFT_METADATA_SPEC: &str = "nft-1.0.0";
Expand All @@ -12,14 +10,9 @@ pub const NFT_METADATA_SPEC: &str = "nft-1.0.0";
Debug,
PartialEq,
Eq,
BorshDeserialize,
BorshSerialize,
Serialize,
Deserialize,
NearSchema,
)]
#[serde(crate = "near_sdk::serde")]
#[borsh(crate = "near_sdk::borsh")]
#[near(serializers=[borsh, json])]
pub struct NFTContractMetadata {
pub spec: String, // required, essentially a version like "nft-1.0.0"
pub name: String, // required, ex. "Mosaics"
Expand All @@ -37,14 +30,9 @@ pub struct NFTContractMetadata {
Default,
PartialEq,
Eq,
Serialize,
Deserialize,
BorshDeserialize,
BorshSerialize,
NearSchema,
)]
#[serde(crate = "near_sdk::serde")]
#[borsh(crate = "near_sdk::borsh")]
#[near(serializers=[borsh, json])]
pub struct TokenMetadata {
pub title: Option<String>, // ex. "Arch Nemesis: Mail Carrier" or "Parcel #5055"
pub description: Option<String>, // free-form description
Expand Down
14 changes: 5 additions & 9 deletions near-contract-standards/src/storage_management/mod.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
use near_sdk::borsh::{BorshDeserialize, BorshSerialize};
use near_sdk::serde::{Deserialize, Serialize};
use near_sdk::{AccountId, NearSchema, NearToken};
use near_sdk::{near, AccountId, NearSchema, NearToken};

#[derive(BorshDeserialize, BorshSerialize, Serialize, Deserialize, NearSchema)]
#[serde(crate = "near_sdk::serde")]
#[borsh(crate = "near_sdk::borsh")]
#[derive(NearSchema)]
#[near(serializers=[borsh, json])]
pub struct StorageBalance {
pub total: NearToken,
pub available: NearToken,
}

#[derive(BorshDeserialize, BorshSerialize, Serialize, Deserialize, NearSchema)]
#[serde(crate = "near_sdk::serde")]
#[borsh(crate = "near_sdk::borsh")]
#[derive(NearSchema)]
#[near(serializers=[borsh, json])]
pub struct StorageBalanceBounds {
pub min: NearToken,
pub max: Option<NearToken>,
Expand Down
6 changes: 2 additions & 4 deletions near-contract-standards/src/upgrade/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use near_sdk::borsh::{BorshDeserialize, BorshSerialize};
use near_sdk::json_types::U64;
use near_sdk::{env, require, AccountId, Duration, Promise, Timestamp};
use near_sdk::{near, env, require, AccountId, Duration, Promise, Timestamp};

type WrappedDuration = U64;

Expand All @@ -25,8 +24,7 @@ pub trait Upgradable {
}
}

#[derive(BorshSerialize, BorshDeserialize)]
#[borsh(crate = "near_sdk::borsh")]
#[near]
pub struct Upgrade {
pub owner: AccountId,
pub staging_duration: Duration,
Expand Down

0 comments on commit b06850d

Please sign in to comment.