Skip to content

Commit

Permalink
refactor(guards): Rename (#35)
Browse files Browse the repository at this point in the history
# Motivation
The guard names are a bit confusing.  Try a small change.

# Changes
- Rename `PaymentGuard` -> `PaymentGuardTrait`
- Rename `AnyPaymentGuard` -> `PaymentGuard`

# Tests
Existing CI
  • Loading branch information
bitdivine authored Oct 1, 2024
1 parent 3472d95 commit 04075ba
Showing 9 changed files with 19 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/example/paid_service/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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]
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,
8 changes: 4 additions & 4 deletions src/guard/src/guards/any.rs
Original file line number Diff line number Diff line change
@@ -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],
}

@@ -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)
@@ -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> {
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 {
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};
@@ -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();
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
@@ -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)
4 changes: 2 additions & 2 deletions src/guard/src/guards/mod.rs
Original file line number Diff line number Diff line change
@@ -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::{
@@ -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();
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};
@@ -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();

0 comments on commit 04075ba

Please sign in to comment.