Skip to content

Commit

Permalink
feat: better hooks for nep141
Browse files Browse the repository at this point in the history
  • Loading branch information
encody committed Sep 21, 2023
1 parent 755e813 commit 449700b
Show file tree
Hide file tree
Showing 10 changed files with 761 additions and 605 deletions.
102 changes: 48 additions & 54 deletions macros/src/standard/nep141.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::ops::Not;

use darling::{util::Flag, FromDeriveInput};
use proc_macro2::TokenStream;
use quote::quote;
Expand Down Expand Up @@ -41,20 +39,15 @@ pub fn expand(meta: Nep141Meta) -> Result<TokenStream, darling::Error> {
}
});

let before_transfer = no_hooks.is_present().not().then(|| {
quote! {
let hook_state = <Self as #me::standard::nep141::Nep141Hook::<_>>::before_transfer(self, &transfer);
}
});

let after_transfer = no_hooks.is_present().not().then(|| {
quote! {
<Self as #me::standard::nep141::Nep141Hook::<_>>::after_transfer(self, &transfer, hook_state);
}
});
let hook = if no_hooks.is_present() {
quote! { () }
} else {
quote! { Self }
};

Ok(quote! {
impl #imp #me::standard::nep141::Nep141ControllerInternal for #ident #ty #wher {
type Hook = #hook;
#root
}

Expand All @@ -67,36 +60,22 @@ pub fn expand(meta: Nep141Meta) -> Result<TokenStream, darling::Error> {
amount: #near_sdk::json_types::U128,
memo: Option<String>,
) {
use #me::{
standard::{
nep141::{Nep141Controller, event},
nep297::Event,
},
};
use #me::standard::nep141::*;

#near_sdk::assert_one_yocto();
let sender_id = #near_sdk::env::predecessor_account_id();
let amount: u128 = amount.into();

let transfer = #me::standard::nep141::Nep141Transfer {
let transfer = Nep141Transfer {
sender_id: sender_id.clone(),
receiver_id: receiver_id.clone(),
amount,
memo: memo.clone(),
memo,
msg: None,
revert: false,
};

#before_transfer

Nep141Controller::transfer(
self,
sender_id.clone(),
receiver_id.clone(),
amount,
memo,
);

#after_transfer
Nep141Controller::transfer(self, &transfer);
}

#[payable]
Expand All @@ -107,41 +86,56 @@ pub fn expand(meta: Nep141Meta) -> Result<TokenStream, darling::Error> {
memo: Option<String>,
msg: String,
) -> #near_sdk::Promise {
use #me::standard::nep141::*;

let prepaid_gas = #near_sdk::env::prepaid_gas();

#near_sdk::require!(
prepaid_gas >= GAS_FOR_FT_TRANSFER_CALL,
MORE_GAS_FAIL_MESSAGE,
);

#near_sdk::assert_one_yocto();
let sender_id = #near_sdk::env::predecessor_account_id();
let amount: u128 = amount.into();

let transfer = #me::standard::nep141::Nep141Transfer {
sender_id: sender_id.clone(),
receiver_id: receiver_id.clone(),
amount,
memo: memo.clone(),
msg: None,
};

#before_transfer

let r = #me::standard::nep141::Nep141Controller::transfer_call(
self,
sender_id.clone(),
receiver_id.clone(),
let transfer = Nep141Transfer {
sender_id,
receiver_id,
amount,
memo,
msg.clone(),
#near_sdk::env::prepaid_gas(),
);

#after_transfer
msg: Some(msg.clone()),
revert: false,
};

r
Nep141Controller::transfer(self, &transfer);

let receiver_gas = prepaid_gas
.0
.checked_sub(GAS_FOR_FT_TRANSFER_CALL.0) // TODO: Double-check this math. Should this be GAS_FOR_RESOLVE_TRANSFER? If not, this checked_sub call is superfluous given the require!() at the top of this function.
.unwrap_or_else(|| #near_sdk::env::panic_str("Prepaid gas overflow"));

// Initiating receiver's call and the callback
ext_nep141_receiver::ext(transfer.receiver_id.clone())
.with_static_gas(receiver_gas.into())
.ft_on_transfer(transfer.sender_id.clone(), 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.amount.into(),
),
)
}

fn ft_total_supply(&self) -> #near_sdk::json_types::U128 {
<Self as #me::standard::nep141::Nep141Controller>::total_supply().into()
#me::standard::nep141::Nep141Controller::total_supply(self).into()
}

fn ft_balance_of(&self, account_id: #near_sdk::AccountId) -> #near_sdk::json_types::U128 {
<Self as #me::standard::nep141::Nep141Controller>::balance_of(&account_id).into()
#me::standard::nep141::Nep141Controller::balance_of(self, &account_id).into()
}
}

Expand Down
1 change: 1 addition & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
tab_spaces = 4
max_width = 100
Loading

0 comments on commit 449700b

Please sign in to comment.