From 4dbc59e6c16707c18e672a9332aefc3bae9d2448 Mon Sep 17 00:00:00 2001 From: Max Murphy Date: Tue, 1 Oct 2024 13:36:06 +0200 Subject: [PATCH 1/3] rename --- src/example/paid_service/src/lib.rs | 2 +- src/guard/src/guards/any.rs | 2 +- src/guard/src/guards/attached_cycles.rs | 4 ++-- src/guard/src/guards/caller_pays_icrc2_cycles.rs | 4 ++-- src/guard/src/guards/caller_pays_icrc2_tokens.rs | 4 ++-- src/guard/src/guards/mod.rs | 2 +- src/guard/src/guards/patron_pays_icrc2_cycles.rs | 4 ++-- src/guard/src/guards/patron_pays_icrc2_tokens.rs | 4 ++-- 8 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/example/paid_service/src/lib.rs b/src/example/paid_service/src/lib.rs index 1cb4a51..a443779 100644 --- a/src/example/paid_service/src/lib.rs +++ b/src/example/paid_service/src/lib.rs @@ -10,7 +10,7 @@ use ic_papi_guard::guards::{ caller_pays_icrc2_cycles::CallerPaysIcrc2CyclesPaymentGuard, caller_pays_icrc2_tokens::CallerPaysIcrc2TokensPaymentGuard, }; -use ic_papi_guard::guards::PaymentGuard; +use ic_papi_guard::guards::PaymentGuardTrait; use state::{set_init_args, PAYMENT_GUARD}; #[init] diff --git a/src/guard/src/guards/any.rs b/src/guard/src/guards/any.rs index 6160f88..1fc072b 100644 --- a/src/guard/src/guards/any.rs +++ b/src/guard/src/guards/any.rs @@ -11,7 +11,7 @@ use super::{ caller_pays_icrc2_cycles::CallerPaysIcrc2CyclesPaymentGuard, caller_pays_icrc2_tokens::CallerPaysIcrc2TokensPaymentGuard, patron_pays_icrc2_cycles::PatronPaysIcrc2CyclesPaymentGuard, - patron_pays_icrc2_tokens::PatronPaysIcrc2TokensPaymentGuard, PaymentGuard, + patron_pays_icrc2_tokens::PatronPaysIcrc2TokensPaymentGuard, PaymentGuardTrait, }; /// A guard that accepts a user-specified payment type, providing the vendor supports it. diff --git a/src/guard/src/guards/attached_cycles.rs b/src/guard/src/guards/attached_cycles.rs index 2654318..3d60847 100644 --- a/src/guard/src/guards/attached_cycles.rs +++ b/src/guard/src/guards/attached_cycles.rs @@ -1,4 +1,4 @@ -use super::{PaymentError, PaymentGuard}; +use super::{PaymentError, PaymentGuardTrait}; use ic_cdk::api::call::{msg_cycles_accept, msg_cycles_available}; use ic_papi_api::caller::TokenAmount; @@ -6,7 +6,7 @@ use ic_papi_api::caller::TokenAmount; #[derive(Default, Debug, Eq, PartialEq)] pub struct AttachedCyclesPayment {} -impl PaymentGuard for AttachedCyclesPayment { +impl PaymentGuardTrait for AttachedCyclesPayment { async fn deduct(&self, fee: TokenAmount) -> Result<(), PaymentError> { let available = msg_cycles_available(); if available < fee { diff --git a/src/guard/src/guards/caller_pays_icrc2_cycles.rs b/src/guard/src/guards/caller_pays_icrc2_cycles.rs index 10834e8..b2e54ad 100644 --- a/src/guard/src/guards/caller_pays_icrc2_cycles.rs +++ b/src/guard/src/guards/caller_pays_icrc2_cycles.rs @@ -1,5 +1,5 @@ //! Code to receive cycles as payment, credited to the canister, using ICRC-2 and a cycles-ledger specific withdrawal method. -use super::{PaymentError, PaymentGuard}; +use super::{PaymentError, PaymentGuardTrait}; use candid::Nat; use cycles_ledger_client::WithdrawFromArgs; use ic_papi_api::{caller::TokenAmount, cycles::cycles_ledger_canister_id, Account}; @@ -9,7 +9,7 @@ use ic_papi_api::{caller::TokenAmount, cycles::cycles_ledger_canister_id, Accoun #[derive(Default)] pub struct CallerPaysIcrc2CyclesPaymentGuard {} -impl PaymentGuard for CallerPaysIcrc2CyclesPaymentGuard { +impl PaymentGuardTrait for CallerPaysIcrc2CyclesPaymentGuard { async fn deduct(&self, fee: TokenAmount) -> Result<(), PaymentError> { let caller = ic_cdk::caller(); let own_canister_id = ic_cdk::api::id(); diff --git a/src/guard/src/guards/caller_pays_icrc2_tokens.rs b/src/guard/src/guards/caller_pays_icrc2_tokens.rs index c9b9ab0..1b505c3 100644 --- a/src/guard/src/guards/caller_pays_icrc2_tokens.rs +++ b/src/guard/src/guards/caller_pays_icrc2_tokens.rs @@ -3,7 +3,7 @@ // Well known ICRC-2 tokens // TODO -use super::{PaymentError, PaymentGuard}; +use super::{PaymentError, PaymentGuardTrait}; use candid::{Nat, Principal}; use cycles_ledger_client::TransferFromArgs; use ic_papi_api::{caller::TokenAmount, Account}; @@ -13,7 +13,7 @@ pub struct CallerPaysIcrc2TokensPaymentGuard { pub ledger: Principal, } -impl PaymentGuard for CallerPaysIcrc2TokensPaymentGuard { +impl PaymentGuardTrait for CallerPaysIcrc2TokensPaymentGuard { async fn deduct(&self, cost: TokenAmount) -> Result<(), PaymentError> { let caller = ic_cdk::api::caller(); cycles_ledger_client::Service(self.ledger) diff --git a/src/guard/src/guards/mod.rs b/src/guard/src/guards/mod.rs index 1014aeb..70093c4 100644 --- a/src/guard/src/guards/mod.rs +++ b/src/guard/src/guards/mod.rs @@ -9,6 +9,6 @@ pub mod patron_pays_icrc2_cycles; pub mod patron_pays_icrc2_tokens; #[allow(async_fn_in_trait)] -pub trait PaymentGuard { +pub trait PaymentGuardTrait { async fn deduct(&self, fee: TokenAmount) -> Result<(), PaymentError>; } \ No newline at end of file diff --git a/src/guard/src/guards/patron_pays_icrc2_cycles.rs b/src/guard/src/guards/patron_pays_icrc2_cycles.rs index 8aa36c2..191a1ca 100644 --- a/src/guard/src/guards/patron_pays_icrc2_cycles.rs +++ b/src/guard/src/guards/patron_pays_icrc2_cycles.rs @@ -1,5 +1,5 @@ //! Code to receive cycles as payment, credited to the canister, using ICRC-2 and a cycles-ledger specific withdrawal method. -use super::{PaymentError, PaymentGuard}; +use super::{PaymentError, PaymentGuardTrait}; use candid::Nat; use cycles_ledger_client::WithdrawFromArgs; use ic_papi_api::{ @@ -13,7 +13,7 @@ pub struct PatronPaysIcrc2CyclesPaymentGuard { pub patron: Account, } -impl PaymentGuard for PatronPaysIcrc2CyclesPaymentGuard { +impl PaymentGuardTrait for PatronPaysIcrc2CyclesPaymentGuard { async fn deduct(&self, fee: TokenAmount) -> Result<(), PaymentError> { let own_canister_id = ic_cdk::api::id(); let caller = ic_cdk::caller(); diff --git a/src/guard/src/guards/patron_pays_icrc2_tokens.rs b/src/guard/src/guards/patron_pays_icrc2_tokens.rs index d59380e..8225e6b 100644 --- a/src/guard/src/guards/patron_pays_icrc2_tokens.rs +++ b/src/guard/src/guards/patron_pays_icrc2_tokens.rs @@ -1,5 +1,5 @@ //! Code to receive cycles as payment, credited to the canister, using ICRC-2 and a cycles-ledger specific withdrawal method. -use super::{PaymentError, PaymentGuard}; +use super::{PaymentError, PaymentGuardTrait}; use candid::{Nat, Principal}; use cycles_ledger_client::TransferFromArgs; use ic_papi_api::{caller::TokenAmount, principal2account, Account}; @@ -13,7 +13,7 @@ pub struct PatronPaysIcrc2TokensPaymentGuard { pub patron: Account, } -impl PaymentGuard for PatronPaysIcrc2TokensPaymentGuard { +impl PaymentGuardTrait for PatronPaysIcrc2TokensPaymentGuard { async fn deduct(&self, cost: TokenAmount) -> Result<(), PaymentError> { let caller = ic_cdk::api::caller(); let own_canister_id = ic_cdk::api::id(); From e03ccff23ee28a25e0bb7cdd25857693e1eeafc0 Mon Sep 17 00:00:00 2001 From: Max Murphy Date: Tue, 1 Oct 2024 13:36:59 +0200 Subject: [PATCH 2/3] Rename --- src/example/paid_service/src/state.rs | 4 ++-- src/guard/src/guards/any.rs | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/example/paid_service/src/state.rs b/src/example/paid_service/src/state.rs index 6223b22..768ba12 100644 --- a/src/example/paid_service/src/state.rs +++ b/src/example/paid_service/src/state.rs @@ -1,6 +1,6 @@ use candid::Principal; use example_paid_service_api::InitArgs; -use ic_papi_guard::guards::any::{AnyPaymentGuard, VendorPaymentConfig}; +use ic_papi_guard::guards::any::{PaymentGuard, VendorPaymentConfig}; use lazy_static::lazy_static; use std::cell::RefCell; @@ -8,7 +8,7 @@ thread_local! { pub static INIT_ARGS: RefCell> = const {RefCell::new(None)}; } lazy_static! { - pub static ref PAYMENT_GUARD: AnyPaymentGuard<5> = AnyPaymentGuard { + pub static ref PAYMENT_GUARD: PaymentGuard<5> = PaymentGuard { supported: [ VendorPaymentConfig::AttachedCycles, VendorPaymentConfig::CallerPaysIcrc2Cycles, diff --git a/src/guard/src/guards/any.rs b/src/guard/src/guards/any.rs index 1fc072b..792d512 100644 --- a/src/guard/src/guards/any.rs +++ b/src/guard/src/guards/any.rs @@ -15,7 +15,7 @@ use super::{ }; /// A guard that accepts a user-specified payment type, providing the vendor supports it. -pub struct AnyPaymentGuard { +pub struct PaymentGuard { pub supported: [VendorPaymentConfig; CAP], } @@ -45,7 +45,7 @@ pub enum PaymentWithConfig { PatronPaysIcrc2Tokens(PatronPaysIcrc2Tokens), } -impl AnyPaymentGuard { +impl PaymentGuard { pub async fn deduct(&self, payment: PaymentType, fee: TokenAmount) -> Result<(), PaymentError> { let payment_config = self .config(payment) @@ -76,7 +76,7 @@ impl AnyPaymentGuard { } } } -impl AnyPaymentGuard { +impl PaymentGuard { /// Find the vendor configuration for the offered payment type. #[must_use] pub fn config(&self, payment: PaymentType) -> Option { From 5613281a96b9002a8244effffa61aad3ba469d40 Mon Sep 17 00:00:00 2001 From: Max Murphy Date: Tue, 1 Oct 2024 13:37:11 +0200 Subject: [PATCH 3/3] fmt --- src/example/paid_service/src/lib.rs | 2 +- src/guard/src/guards/mod.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/example/paid_service/src/lib.rs b/src/example/paid_service/src/lib.rs index a443779..09f7a6c 100644 --- a/src/example/paid_service/src/lib.rs +++ b/src/example/paid_service/src/lib.rs @@ -5,12 +5,12 @@ use ic_cdk::init; use ic_cdk_macros::{export_candid, update}; use ic_papi_api::cycles::cycles_ledger_canister_id; use ic_papi_api::{PaymentError, PaymentType}; +use ic_papi_guard::guards::PaymentGuardTrait; use ic_papi_guard::guards::{ attached_cycles::AttachedCyclesPayment, caller_pays_icrc2_cycles::CallerPaysIcrc2CyclesPaymentGuard, caller_pays_icrc2_tokens::CallerPaysIcrc2TokensPaymentGuard, }; -use ic_papi_guard::guards::PaymentGuardTrait; use state::{set_init_args, PAYMENT_GUARD}; #[init] diff --git a/src/guard/src/guards/mod.rs b/src/guard/src/guards/mod.rs index 70093c4..79d76b5 100644 --- a/src/guard/src/guards/mod.rs +++ b/src/guard/src/guards/mod.rs @@ -11,4 +11,4 @@ pub mod patron_pays_icrc2_tokens; #[allow(async_fn_in_trait)] pub trait PaymentGuardTrait { async fn deduct(&self, fee: TokenAmount) -> Result<(), PaymentError>; -} \ No newline at end of file +}