From ba4d15f74dac03f9459386a7160dcd20766b5b58 Mon Sep 17 00:00:00 2001 From: Max Murphy Date: Mon, 9 Sep 2024 17:19:33 +0200 Subject: [PATCH] ++ --- src/example/paid_service/src/lib.rs | 3 +- src/guard/src/guards/icrc2_from_caller.rs | 51 ----------------------- src/guard/src/guards/mod.rs | 1 - 3 files changed, 1 insertion(+), 54 deletions(-) delete mode 100644 src/guard/src/guards/icrc2_from_caller.rs diff --git a/src/example/paid_service/src/lib.rs b/src/example/paid_service/src/lib.rs index 82ca9c3..74f3628 100644 --- a/src/example/paid_service/src/lib.rs +++ b/src/example/paid_service/src/lib.rs @@ -3,8 +3,7 @@ mod state; use example_paid_service_api::InitArgs; use ic_cdk::init; use ic_cdk_macros::{export_candid, update}; -use ic_papi_api::{Account, PaymentError}; -use ic_papi_guard::guards::icrc2_from_caller::Icrc2FromCaller; +use ic_papi_api::PaymentError; use ic_papi_guard::guards::PaymentGuard; use ic_papi_guard::guards::{ attached_cycles::AttachedCyclesPayment, icrc2_cycles::Icrc2CyclesPaymentGuard, diff --git a/src/guard/src/guards/icrc2_from_caller.rs b/src/guard/src/guards/icrc2_from_caller.rs deleted file mode 100644 index 24f4180..0000000 --- a/src/guard/src/guards/icrc2_from_caller.rs +++ /dev/null @@ -1,51 +0,0 @@ -use super::{PaymentError, PaymentGuard}; -use candid::{Nat, Principal}; -use cycles_ledger_client::WithdrawFromArgs; - -/// The information required to deduct an ICRC-2 payment from the caller. -pub struct Icrc2FromCaller { - /// The payer - pub payer: cycles_ledger_client::Account, - /// The ledger to deduct the charge from. - pub ledger_canister_id: Principal, - /// Own canister ID - pub own_canister_id: Principal, -} - -impl PaymentGuard for Icrc2FromCaller { - async fn deduct(&self, fee: u64) -> Result<(), PaymentError> { - cycles_ledger_client::Service(self.ledger_canister_id) - .withdraw_from(&WithdrawFromArgs { - to: self.own_canister_id.clone(), - amount: Nat::from(fee), - from: self.payer.clone(), - spender_subaccount: None, - created_at_time: None, - }) - .await - .map_err(|(rejection_code, string)| { - eprintln!( - "Failed to reach ledger canister at {}: {rejection_code:?}: {string}", - self.ledger_canister_id - ); - PaymentError::LedgerUnreachable { - ledger: self.ledger_canister_id, - } - })? - .0 - .map_err(|e| { - // TODO: Improve error handling - eprintln!( - "Failed to withdraw from ledger canister at {}: {e:?}", - self.ledger_canister_id - ); - match e { - error => PaymentError::LedgerError { - ledger: self.ledger_canister_id, - error, - }, - } - }) - .map(|_| ()) - } -} diff --git a/src/guard/src/guards/mod.rs b/src/guard/src/guards/mod.rs index f11f451..5dcbe21 100644 --- a/src/guard/src/guards/mod.rs +++ b/src/guard/src/guards/mod.rs @@ -3,7 +3,6 @@ use ic_papi_api::PaymentError; pub mod attached_cycles; pub mod icrc2_cycles; -pub mod icrc2_from_caller; #[allow(async_fn_in_trait)] pub trait PaymentGuard {