Skip to content

Commit

Permalink
added transfer_with_notify
Browse files Browse the repository at this point in the history
  • Loading branch information
rckprtr committed Sep 12, 2021
1 parent edb28b4 commit 119d180
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 14 deletions.
21 changes: 21 additions & 0 deletions src/ic_loot_rs/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,27 @@ impl AddressBook {
return false;
}

pub fn undo_transfer(&mut self, user_id: Principal, token_id: u64) -> bool {
if let Some(token_owner) = self.tokens.get(&token_id) {
if &user_id == token_owner {
self.tokens.insert(token_id, ic_cdk::caller());
return true;
}
}
return false;
}

pub fn undo_transfer2(&mut self, user_id: Principal, token_id: u64) -> bool {
if let Some(token_owner) = self.tokens.get(&token_id) {
if &user_id == token_owner {
ic_cdk::println!("qwe12qw21wq {:?}", token_owner);
self.tokens.insert(token_id, ic_cdk::caller());
return true;
}
}
return false;
}

pub fn transfer_to(&mut self, user: Principal, token_id: u64) -> bool {
if let Some(token_owner) = self.tokens.get(&token_id) {
if token_owner == &ic_cdk::caller() {
Expand Down
4 changes: 2 additions & 2 deletions src/ic_loot_rs/ic_loot.did
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ type DataOfQuery = variant {
type TransferNotification = record {
to: principal;
from: principal;
token_id: nat64;
amount: nat64;
memo: opt blob;
};

service : {
Expand All @@ -80,7 +80,7 @@ service : {
user_tokens: (principal) -> (vec nat64) query;
owner_of: (nat64) -> (opt principal) query;
transfer_to: (principal, nat64) -> (bool);
transfer_with_notify: (principal, nat64, TransferNotification) -> (bool);
transfer_with_notify: (principal, nat64) -> (bool);
claim: () -> (ClaimResult);
remaining: () -> (nat64);
supply: () -> (nat64);
Expand Down
35 changes: 23 additions & 12 deletions src/ic_loot_rs/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ use crate::loot2::{Loot2, LootData};
#[derive(Clone, Debug, CandidType, Deserialize)]
pub struct TransferNotification {
pub to: Principal,
pub token: u64,
pub token_id: u64,
pub from: Principal,
pub amount: u64,
pub memo: Option<Vec<u8>>,
}

#[init]
Expand Down Expand Up @@ -536,15 +535,27 @@ fn transfer_to(user: Principal, token_id: u64) -> bool {

#[update]
async fn transfer_with_notify(
user: Principal,
token_id: u64,
notification: TransferNotification,
user_id: Principal, token_id: u64
) -> bool {
let address_book = storage::get_mut::<AddressBook>();
if address_book.transfer_to(user, token_id) {
let _result: CallResult<()> =
ic_cdk::call(notification.to, "transfer_notification", (notification,)).await;
return true;
if address_book.transfer_to(user_id, token_id) {
match
ic_cdk::call(user_id, "transfer_notification", (
TransferNotification{
to: user_id,
from: ic_cdk::caller(),
token_id: token_id,
amount: 1
},)
).await as CallResult<()> {
Ok(_) => return true,
Err(_) => {
//gets in rejected state and the next
//line is not executed completely
//address_book.undo_transfer(user_id, token_id);
return false;
}
}
} else {
return false;
}
Expand Down Expand Up @@ -816,9 +827,9 @@ type ClaimResult = variant {
type TransferNotification = record {
to: principal;
from: principal;
token_id: nat64;
amount: nat64;
memo: opt blob;
};
};
type LootData = record {
slot: text;
Expand Down Expand Up @@ -854,7 +865,7 @@ service : {
user_tokens: (principal) -> (vec nat64) query;
owner_of: (nat64) -> (opt principal) query;
transfer_to: (principal, nat64) -> (bool);
transfer_with_notify: (principal, nat64, TransferNotification) -> (bool);
transfer_with_notify: (principal, nat64) -> (bool);
claim: () -> (ClaimResult);
remaining: () -> (nat64);
Expand Down

0 comments on commit 119d180

Please sign in to comment.