Skip to content

Commit

Permalink
++
Browse files Browse the repository at this point in the history
  • Loading branch information
bitdivine committed Sep 9, 2024
1 parent 7a009b0 commit 608ef9f
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/example/paid_service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use ic_papi_api::{Account, PaymentError};
use ic_papi_guard::guards::attached_cycles::AttachedCyclesPayment;
use ic_papi_guard::guards::icrc2_from_caller::Icrc2FromCaller;
use ic_papi_guard::guards::PaymentGuard;
use state::{payment_ledger, set_init_args};
use state::{own_canister_id, payment_ledger, set_init_args};

#[init]
fn init(init_args: Option<InitArgs>) {
Expand All @@ -32,6 +32,7 @@ async fn cost_1000_attached_cycles() -> Result<String, PaymentError> {
#[update()]
async fn cost_1000_icrc2_from_caller() -> Result<String, PaymentError> {
let guard = Icrc2FromCaller {
own_canister_id: own_canister_id(),
payer: Account {
owner: ic_cdk::caller(),
subaccount: None,
Expand Down
7 changes: 6 additions & 1 deletion src/example/paid_service/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,18 @@ where
INIT_ARGS.with(|init_args| f(init_args.borrow().as_ref().expect("No init args provided")))
}

/// Provides own canister ID
pub fn own_canister_id() -> Principal {
init_element(|init_args| init_args.own_canister_id.clone())
}

/// Provides the canister_id of the ledger used for payments.
pub fn payment_ledger() -> Principal {
init_element(|init_args| {
init_args
.ledger
.clone()
.expect("Init args specify no ledger")
.clone()
})
}

Expand Down
11 changes: 7 additions & 4 deletions src/example/paid_service/tests/it/icrc2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,13 @@ impl Default for CallerPaysWithIcRc2TestSetup {
let paid_service = PicCanisterBuilder::default()
.with_wasm(&PicCanister::cargo_wasm_path("example_paid_service"))
.with_canister(paid_service.clone())
.with_arg(encode_one(Some(InitArgs{
ledger: Some(ledger.canister_id()),
own_canister_id: paid_service,
})).unwrap())
.with_arg(
encode_one(Some(InitArgs {
ledger: Some(ledger.canister_id()),
own_canister_id: paid_service,
}))
.unwrap(),
)
.deploy_to(pic.clone());
let user =
Principal::from_text("xzg7k-thc6c-idntg-knmtz-2fbhh-utt3e-snqw6-5xph3-54pbp-7axl5-tae")
Expand Down
4 changes: 3 additions & 1 deletion src/guard/src/guards/icrc2_from_caller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ pub struct Icrc2FromCaller {
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: Principal::anonymous(), // TODO: Should presumably be the canister ID of the service
to: self.own_canister_id.clone(),
from: self.payer.clone(),
amount: Nat::from(fee),
spender_subaccount: None,
Expand Down

0 comments on commit 608ef9f

Please sign in to comment.