Skip to content

Commit

Permalink
chore: use AccountIdRef
Browse files Browse the repository at this point in the history
  • Loading branch information
encody committed May 8, 2024
1 parent ab14199 commit 831b9af
Show file tree
Hide file tree
Showing 32 changed files with 477 additions and 460 deletions.
2 changes: 1 addition & 1 deletion macros/src/standard/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub fn event_attribute(
let me_str = quote! { #me }.to_string();

Ok(quote::quote! {
#[derive(#macros::Nep297, #serde::Serialize)]
#[derive(#macros::Nep297, #serde::Serialize, #serde::Deserialize)]
#[nep297(
crate = #me_str,
standard = #standard,
Expand Down
9 changes: 4 additions & 5 deletions macros/src/standard/fungible_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use proc_macro2::TokenStream;
use quote::quote;
use syn::{Expr, Type};

use crate::unitify;

use super::{nep141, nep145, nep148};

#[derive(Debug, FromDeriveInput)]
Expand Down Expand Up @@ -53,11 +55,8 @@ pub fn expand(meta: FungibleTokenMeta) -> Result<TokenStream, darling::Error> {
near_sdk,
} = meta;

let all_hooks_or_unit = all_hooks
.clone()
.unwrap_or_else(|| syn::parse_quote! { () });
let force_unregister_hook_or_unit =
force_unregister_hook.unwrap_or_else(|| syn::parse_quote! { () });
let all_hooks_or_unit = unitify(all_hooks.clone());
let force_unregister_hook_or_unit = unitify(force_unregister_hook);

let expand_nep141 = nep141::expand(nep141::Nep141Meta {
storage_key: core_storage_key,
Expand Down
26 changes: 13 additions & 13 deletions macros/src/standard/nep141.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ pub fn expand(meta: Nep141Meta) -> Result<TokenStream, darling::Error> {
let amount: u128 = amount.into();

let transfer = Nep141Transfer {
sender_id: &sender_id,
receiver_id: &receiver_id,
sender_id: sender_id.into(),
receiver_id: receiver_id.into(),
amount,
memo: memo.as_deref(),
memo: memo.map(Into::into),
msg: None,
revert: false,
};
Expand Down Expand Up @@ -118,11 +118,11 @@ pub fn expand(meta: Nep141Meta) -> Result<TokenStream, darling::Error> {
let amount: u128 = amount.into();

let transfer = Nep141Transfer {
sender_id: &sender_id,
receiver_id: &receiver_id,
sender_id: sender_id.into(),
receiver_id: receiver_id.into(),
amount,
memo: memo.as_deref(),
msg: Some(&msg),
memo: memo.map(Into::into),
msg: Some(msg.clone().into()),
revert: false,
};

Expand All @@ -134,15 +134,15 @@ pub fn expand(meta: Nep141Meta) -> Result<TokenStream, darling::Error> {
.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())
ext_nep141_receiver::ext(transfer.receiver_id.clone().into())
.with_static_gas(receiver_gas)
.ft_on_transfer(transfer.sender_id.clone(), transfer.amount.into(), msg.clone())
.ft_on_transfer(transfer.sender_id.clone().into(), transfer.amount.into(), msg)
.then(
ext_nep141_resolver::ext(#near_sdk::env::current_account_id())
.with_static_gas(GAS_FOR_RESOLVE_TRANSFER)
.ft_resolve_transfer(
transfer.sender_id.clone(),
transfer.receiver_id.clone(),
transfer.sender_id.clone().into(),
transfer.receiver_id.clone().into(),
transfer.amount.into(),
),
)
Expand Down Expand Up @@ -190,8 +190,8 @@ pub fn expand(meta: Nep141Meta) -> Result<TokenStream, darling::Error> {
if receiver_balance > 0 {
let refund_amount = std::cmp::min(receiver_balance, unused_amount);
let transfer = Nep141Transfer {
sender_id: &receiver_id,
receiver_id: &sender_id,
sender_id: receiver_id.into(),
receiver_id: sender_id.into(),
amount: refund_amount,
memo: None,
msg: None,
Expand Down
45 changes: 21 additions & 24 deletions macros/src/standard/nep171.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,11 @@ pub fn expand(meta: Nep171Meta) -> Result<TokenStream, darling::Error> {
};

if should_revert {
let token_ids = [token_id];

let transfer = action::Nep171Transfer {
token_id: &token_ids[0],
token_id,
authorization: Nep171TransferAuthorization::Owner,
sender_id: &receiver_id,
receiver_id: &previous_owner_id,
sender_id: receiver_id.into(),
receiver_id: previous_owner_id.into(),
memo: None,
msg: None,
revert: true,
Expand Down Expand Up @@ -140,14 +138,12 @@ pub fn expand(meta: Nep171Meta) -> Result<TokenStream, darling::Error> {

let sender_id = #near_sdk::env::predecessor_account_id();

let token_ids = [token_id];

let transfer = action::Nep171Transfer {
token_id: &token_ids[0],
token_id,
authorization: approval_id.map(Nep171TransferAuthorization::ApprovalId).unwrap_or(Nep171TransferAuthorization::Owner),
sender_id: &sender_id,
receiver_id: &receiver_id,
memo: memo.as_deref(),
sender_id: sender_id.into(),
receiver_id: receiver_id.into(),
memo: memo.map(Into::into),
msg: None,
revert: false,
};
Expand Down Expand Up @@ -176,35 +172,36 @@ pub fn expand(meta: Nep171Meta) -> Result<TokenStream, darling::Error> {

let sender_id = #near_sdk::env::predecessor_account_id();

let token_ids = [token_id];

let transfer = action::Nep171Transfer {
token_id: &token_ids[0],
token_id: token_id.clone(),
authorization: approval_id.map(Nep171TransferAuthorization::ApprovalId).unwrap_or(Nep171TransferAuthorization::Owner),
sender_id: &sender_id,
receiver_id: &receiver_id,
memo: memo.as_deref(),
msg: Some(&msg),
sender_id: sender_id.clone().into(),
receiver_id: receiver_id.clone().into(),
memo: memo.map(Into::into),
msg: Some(msg.clone().into()),
revert: false,
};

<Self as Nep171Controller>::external_transfer(self, &transfer)
.unwrap_or_else(|e| #near_sdk::env::panic_str(&e.to_string()));

let [token_id] = token_ids;

ext_nep171_receiver::ext(receiver_id.clone())
ext_nep171_receiver::ext(receiver_id.clone().into())
.with_static_gas(#near_sdk::env::prepaid_gas().saturating_sub(GAS_FOR_NFT_TRANSFER_CALL))
.nft_on_transfer(
sender_id.clone(),
sender_id.clone(),
sender_id.clone().into(),
sender_id.clone().into(),
token_id.clone(),
msg.clone(),
)
.then(
ext_nep171_resolver::ext(#near_sdk::env::current_account_id())
.with_static_gas(GAS_FOR_RESOLVE_TRANSFER)
.nft_resolve_transfer(sender_id.clone(), receiver_id.clone(), token_id.clone(), None),
.nft_resolve_transfer(
sender_id.clone().into(),
receiver_id.clone().into(),
token_id.clone(),
None,
),
)
.into()
}
Expand Down
16 changes: 8 additions & 8 deletions macros/src/standard/nep178.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ pub fn expand(meta: Nep178Meta) -> Result<TokenStream, darling::Error> {
let predecessor = #near_sdk::env::predecessor_account_id();

let action = action::Nep178Approve {
token_id: &token_id,
current_owner_id: &predecessor,
account_id: &account_id,
token_id: token_id.clone(),
current_owner_id: predecessor.clone().into(),
account_id: account_id.clone().into(),
};

let approval_id = Nep178Controller::approve(self, &action)
Expand All @@ -107,9 +107,9 @@ pub fn expand(meta: Nep178Meta) -> Result<TokenStream, darling::Error> {
let predecessor = #near_sdk::env::predecessor_account_id();

let action = action::Nep178Revoke {
token_id: &token_id,
current_owner_id: &predecessor,
account_id: &account_id,
token_id,
current_owner_id: predecessor.into(),
account_id: account_id.into(),
};

Nep178Controller::revoke(self, &action)
Expand All @@ -125,8 +125,8 @@ pub fn expand(meta: Nep178Meta) -> Result<TokenStream, darling::Error> {
let predecessor = #near_sdk::env::predecessor_account_id();

let action = action::Nep178RevokeAll {
token_id: &token_id,
current_owner_id: &predecessor,
token_id,
current_owner_id: predecessor.into(),
};

Nep178Controller::revoke_all(self, &action)
Expand Down
15 changes: 12 additions & 3 deletions src/slot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//!
//! Makes it easy to create and manage storage keys and avoid unnecessary
//! writes to contract storage. This reduces transaction IO and saves on gas.
use std::marker::PhantomData;
use std::{marker::PhantomData, ops::Deref};

use near_sdk::{
borsh::{self, BorshDeserialize, BorshSerialize},
Expand Down Expand Up @@ -95,9 +95,18 @@ impl<T> Slot<T> {
}

impl<T: BorshSerialize> Slot<T> {
/// Writes a value to the managed storage slot
/// Writes a value to the managed storage slot.
pub fn write(&mut self, value: &T) -> bool {
self.write_raw(&{ borsh::to_vec(&value) }.unwrap())
self.write_raw(&borsh::to_vec(value).unwrap())
}

/// Writes a value to the managed storage slot which is dereferenced from
/// the target type.
pub fn write_deref<U: BorshSerialize + ?Sized>(&mut self, value: &U) -> bool
where
T: Deref<Target = U>,
{
self.write_raw(&borsh::to_vec(value).unwrap())
}

/// If the given value is `Some(T)`, writes `T` to storage. Otherwise,
Expand Down
59 changes: 33 additions & 26 deletions src/standard/nep141/event.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
//! NEP-141 standard events for minting, burning, and transferring tokens.

use std::borrow::Cow;

use near_sdk::{
json_types::U128,
serde::{Deserialize, Serialize},
AccountIdRef,
};

use near_sdk_contract_tools_macros::event;

/// NEP-141 standard events for minting, burning, and transferring tokens.
Expand All @@ -10,60 +18,59 @@ use near_sdk_contract_tools_macros::event;
version = "1.0.0"
)]
#[derive(Debug, Clone)]
pub enum Nep141Event {
pub enum Nep141Event<'a> {
/// Token mint event. Emitted when tokens are created and total_supply is
/// increased.
FtMint(Vec<FtMintData>),
FtMint(Vec<FtMintData<'a>>),

/// Token transfer event. Emitted when tokens are transferred between two
/// accounts. No change to total_supply.
FtTransfer(Vec<FtTransferData>),
FtTransfer(Vec<FtTransferData<'a>>),

/// Token burn event. Emitted when tokens are burned (removed from supply).
/// Decrease in total_supply.
FtBurn(Vec<FtBurnData>),
FtBurn(Vec<FtBurnData<'a>>),
}
use near_sdk::{json_types::U128, serde::Serialize, AccountId};

/// Individual mint metadata
#[derive(Serialize, Debug, Clone)]
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(crate = "near_sdk::serde")]
pub struct FtMintData {
pub struct FtMintData<'a> {
/// Address to which new tokens were minted
pub owner_id: AccountId,
pub owner_id: Cow<'a, AccountIdRef>,
/// Amount of minted tokens
pub amount: U128,
/// Optional note
#[serde(skip_serializing_if = "Option::is_none")]
pub memo: Option<String>,
pub memo: Option<Cow<'a, str>>,
}

/// Individual transfer metadata
#[derive(Serialize, Debug, Clone)]
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(crate = "near_sdk::serde")]
pub struct FtTransferData {
pub struct FtTransferData<'a> {
/// Account ID of the sender
pub old_owner_id: AccountId,
pub old_owner_id: Cow<'a, AccountIdRef>,
/// Account ID of the receiver
pub new_owner_id: AccountId,
pub new_owner_id: Cow<'a, AccountIdRef>,
/// Amount of transferred tokens
pub amount: U128,
/// Optional note
#[serde(skip_serializing_if = "Option::is_none")]
pub memo: Option<String>,
pub memo: Option<Cow<'a, str>>,
}

/// Individual burn metadata
#[derive(Serialize, Debug, Clone)]
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(crate = "near_sdk::serde")]
pub struct FtBurnData {
pub struct FtBurnData<'a> {
/// Account ID from which tokens were burned
pub owner_id: AccountId,
pub owner_id: Cow<'a, AccountIdRef>,
/// Amount of burned tokens
pub amount: U128,
/// Optional note
#[serde(skip_serializing_if = "Option::is_none")]
pub memo: Option<String>,
pub memo: Option<Cow<'a, str>>,
}

#[cfg(test)]
Expand All @@ -75,7 +82,7 @@ mod tests {
fn mint() {
assert_eq!(
Nep141Event::FtMint(vec![FtMintData {
owner_id: "foundation.near".parse().unwrap(),
owner_id: AccountIdRef::new_or_panic("foundation.near").into(),
amount: 500u128.into(),
memo: None,
}])
Expand All @@ -89,16 +96,16 @@ mod tests {
assert_eq!(
Nep141Event::FtTransfer(vec![
FtTransferData {
old_owner_id: "from.near".parse().unwrap(),
new_owner_id: "to.near".parse().unwrap(),
old_owner_id: AccountIdRef::new_or_panic("from.near").into(),
new_owner_id: AccountIdRef::new_or_panic("to.near").into(),
amount: 42u128.into(),
memo: Some("hi hello bonjour".to_string()),
memo: Some("hi hello bonjour".into()),
},
FtTransferData {
old_owner_id: "user1.near".parse().unwrap(),
new_owner_id: "user2.near".parse().unwrap(),
old_owner_id: AccountIdRef::new_or_panic("user1.near").into(),
new_owner_id: AccountIdRef::new_or_panic("user2.near").into(),
amount: 7500u128.into(),
memo: None
memo: None,
},
])
.to_event_string(),
Expand All @@ -110,7 +117,7 @@ mod tests {
fn burn() {
assert_eq!(
Nep141Event::FtBurn(vec![FtBurnData {
owner_id: "foundation.near".parse().unwrap(),
owner_id: AccountIdRef::new_or_panic("foundation.near").into(),
amount: 100u128.into(),
memo: None,
}])
Expand Down
Loading

0 comments on commit 831b9af

Please sign in to comment.