Skip to content

Commit

Permalink
chore: move resolve code to macro
Browse files Browse the repository at this point in the history
  • Loading branch information
encody committed Sep 21, 2023
1 parent 449700b commit f384da2
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 64 deletions.
50 changes: 44 additions & 6 deletions macros/src/standard/nep141.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,50 @@ pub fn expand(meta: Nep141Meta) -> Result<TokenStream, darling::Error> {
receiver_id: #near_sdk::AccountId,
amount: #near_sdk::json_types::U128,
) -> #near_sdk::json_types::U128 {
#me::standard::nep141::Nep141Controller::resolve_transfer(
self,
sender_id,
receiver_id,
amount.into(),
).into()
use #near_sdk::{env, PromiseResult, serde_json, json_types::U128};
use #me::standard::nep141::*;

let amount = amount.0;

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)
} else {
amount
}
}
PromiseResult::Failed => amount,
};

let refunded_amount = if unused_amount > 0 {
let receiver_balance = Nep141Controller::balance_of(self, &receiver_id);
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,
amount: refund_amount,
memo: None,
msg: None,
revert: true,
};

Nep141Controller::transfer(self, &transfer);

refund_amount
} else {
0
}
} else {
0
};

// Used amount
U128(amount - refunded_amount)
}
}
})
Expand Down
59 changes: 1 addition & 58 deletions src/standard/nep141/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@

use near_sdk::{
borsh::{self, BorshDeserialize, BorshSerialize},
env,
json_types::U128,
AccountId, BorshStorageKey, Gas, PromiseResult,
env, AccountId, BorshStorageKey, Gas,
};
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -146,14 +144,6 @@ pub trait Nep141Controller {
///
/// See: `Nep141Controller::withdraw_unchecked`
fn burn(&mut self, account_id: AccountId, amount: u128, memo: Option<String>);

/// Resolves an NEP-141 `ft_transfer_call` promise chain.
fn resolve_transfer(
&mut self,
sender_id: AccountId,
receiver_id: AccountId,
amount: u128,
) -> u128;
}

impl<T: Nep141ControllerInternal> Nep141Controller for T {
Expand Down Expand Up @@ -269,51 +259,4 @@ impl<T: Nep141ControllerInternal> Nep141Controller for T {
}])
.emit();
}

fn resolve_transfer(
&mut self,
sender_id: AccountId,
receiver_id: AccountId,
amount: u128,
) -> u128 {
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)
} else {
amount
}
}
PromiseResult::Failed => amount,
};

let refunded_amount = if unused_amount > 0 {
let receiver_balance = self.balance_of(&receiver_id);
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,
amount: refund_amount,
memo: None,
msg: None,
revert: true,
};

self.transfer(&transfer);

refund_amount
} else {
0
}
} else {
0
};

// Used amount
amount - refunded_amount
}
}

0 comments on commit f384da2

Please sign in to comment.