Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(guards): Rename #35

Merged
merged 3 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/example/paid_service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::PaymentGuard;
use state::{set_init_args, PAYMENT_GUARD};

#[init]
Expand Down
4 changes: 2 additions & 2 deletions src/example/paid_service/src/state.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
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;

thread_local! {
pub static INIT_ARGS: RefCell<Option<InitArgs>> = 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,
Expand Down
8 changes: 4 additions & 4 deletions src/guard/src/guards/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ 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.
pub struct AnyPaymentGuard<const CAP: usize> {
pub struct PaymentGuard<const CAP: usize> {
pub supported: [VendorPaymentConfig; CAP],
}

Expand Down Expand Up @@ -45,7 +45,7 @@ pub enum PaymentWithConfig {
PatronPaysIcrc2Tokens(PatronPaysIcrc2Tokens),
}

impl<const CAP: usize> AnyPaymentGuard<CAP> {
impl<const CAP: usize> PaymentGuard<CAP> {
pub async fn deduct(&self, payment: PaymentType, fee: TokenAmount) -> Result<(), PaymentError> {
let payment_config = self
.config(payment)
Expand Down Expand Up @@ -76,7 +76,7 @@ impl<const CAP: usize> AnyPaymentGuard<CAP> {
}
}
}
impl<const CAP: usize> AnyPaymentGuard<CAP> {
impl<const CAP: usize> PaymentGuard<CAP> {
/// Find the vendor configuration for the offered payment type.
#[must_use]
pub fn config(&self, payment: PaymentType) -> Option<PaymentWithConfig> {
Expand Down
4 changes: 2 additions & 2 deletions src/guard/src/guards/attached_cycles.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
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;

/// The information required to charge attached cycles.
#[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 {
Expand Down
4 changes: 2 additions & 2 deletions src/guard/src/guards/caller_pays_icrc2_cycles.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions src/guard/src/guards/caller_pays_icrc2_tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions src/guard/src/guards/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>;
}
}
4 changes: 2 additions & 2 deletions src/guard/src/guards/patron_pays_icrc2_cycles.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand All @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions src/guard/src/guards/patron_pays_icrc2_tokens.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -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();
Expand Down