From c965dc44beabfe7766b8cdff908f3b3b51286072 Mon Sep 17 00:00:00 2001 From: Max Murphy Date: Thu, 5 Sep 2024 17:17:48 +0200 Subject: [PATCH] ++ --- src/example/paid_service/tests/it/icrc2.rs | 51 +++++++++++++------ .../tests/it/util/cycles_depositor.rs | 33 +++++++----- src/example/paid_service/tests/it/util/mod.rs | 2 +- 3 files changed, 55 insertions(+), 31 deletions(-) diff --git a/src/example/paid_service/tests/it/icrc2.rs b/src/example/paid_service/tests/it/icrc2.rs index 769fb7a..f5ebc92 100644 --- a/src/example/paid_service/tests/it/icrc2.rs +++ b/src/example/paid_service/tests/it/icrc2.rs @@ -27,23 +27,32 @@ impl Default for CallerPaysWithIcRc2TestSetup { pic.clone(), &PicCanister::cargo_wasm_path("example_paid_service"), ); - let ledger = CyclesLedgerPic::from(PicCanisterBuilder::default() - .with_wasm(&PicCanister::dfx_wasm_path("cycles_ledger")) + let ledger = CyclesLedgerPic::from( + PicCanisterBuilder::default() + .with_wasm(&PicCanister::dfx_wasm_path("cycles_ledger")) + .with_arg( + encode_one(LedgerArgs::Init(InitArgs { + index_id: None, + max_blocks_per_request: 999, + })) + .expect("Failed to encode ledger init arg"), + ) + .deploy_to(pic.clone()), + ); + let user = + Principal::from_text("xzg7k-thc6c-idntg-knmtz-2fbhh-utt3e-snqw6-5xph3-54pbp-7axl5-tae") + .unwrap(); + let wallet = PicCanisterBuilder::default() + .with_wasm(&PicCanister::dfx_wasm_path("cycles_wallet")) + .with_controllers(vec![user]) .with_arg( - encode_one(LedgerArgs::Init(InitArgs { - index_id: None, - max_blocks_per_request: 999, - })) - .expect("Failed to encode ledger init arg"), + encode_one(cycles_depositor::InitArg { + ledger_id: ledger.canister_id, + }) + .unwrap(), ) - .deploy_to(pic.clone())); - let user = Principal::from_text("xzg7k-thc6c-idntg-knmtz-2fbhh-utt3e-snqw6-5xph3-54pbp-7axl5-tae").unwrap(); - let wallet = PicCanisterBuilder::default() - .with_wasm(&PicCanister::dfx_wasm_path("cycles_wallet")) - .with_controllers(vec![user]) - .with_arg(encode_one(cycles_depositor::InitArg{ledger_id: ledger.canister_id}).unwrap()) - .deploy_to(pic.clone()) - .into(); + .deploy_to(pic.clone()) + .into(); Self { pic, paid_service, @@ -65,5 +74,15 @@ fn icrc2_payment_works() { // Add cycles to the wallet setup.pic.add_cycles(setup.wallet.canister_id, 100_000_000); // Send cycles to the cycles ledger. -// setup.wallet.deposit(caller, arg0); + setup.wallet.deposit( + setup.user, + &cycles_depositor::DepositArg { + to: cycles_depositor::Account { + owner: setup.user, + subaccount: None, + }, + memo: None, + cycles: candid::Nat::from(100_000u32), + }, + ); } diff --git a/src/example/paid_service/tests/it/util/cycles_depositor.rs b/src/example/paid_service/tests/it/util/cycles_depositor.rs index ccdeb6e..7a9099f 100644 --- a/src/example/paid_service/tests/it/util/cycles_depositor.rs +++ b/src/example/paid_service/tests/it/util/cycles_depositor.rs @@ -8,29 +8,31 @@ use pocket_ic::PocketIc; use super::pic_canister::{PicCanister, PicCanisterTrait}; #[derive(CandidType, Deserialize, Debug)] -pub(crate) struct InitArg { pub(crate) ledger_id: Principal } +pub(crate) struct InitArg { + pub(crate) ledger_id: Principal, +} #[derive(CandidType, Deserialize, Debug)] pub(crate) struct Account { - pub(crate) owner: Principal, - pub(crate) subaccount: Option, + pub(crate) owner: Principal, + pub(crate) subaccount: Option, } #[derive(CandidType, Deserialize, Debug)] pub(crate) struct DepositArg { - pub(crate) to: Account, - pub(crate) memo: Option, - pub(crate) cycles: candid::Nat, + pub(crate) to: Account, + pub(crate) memo: Option, + pub(crate) cycles: candid::Nat, } #[derive(CandidType, Deserialize, Debug)] pub(crate) struct DepositResult { - pub(crate) balance: candid::Nat, - pub(crate) block_index: candid::Nat, + pub(crate) balance: candid::Nat, + pub(crate) block_index: candid::Nat, } pub struct Service(pub Principal); impl Service { - pub async fn deposit(&self, arg0: &DepositArg) -> Result<(DepositResult,)> { - ic_cdk::call(self.0, "deposit", (arg0,)).await - } + pub async fn deposit(&self, arg0: &DepositArg) -> Result<(DepositResult,)> { + ic_cdk::call(self.0, "deposit", (arg0,)).await + } } pub struct CyclesDepositorPic { @@ -59,8 +61,11 @@ impl PicCanisterTrait for CyclesDepositorPic { } impl CyclesDepositorPic { - pub fn deposit(&self, caller: Principal, arg0: &DepositArg) -> std::result::Result { + pub fn deposit( + &self, + caller: Principal, + arg0: &DepositArg, + ) -> std::result::Result { self.update(self.canister_id, "deposit", (arg0,)) - } + } } - diff --git a/src/example/paid_service/tests/it/util/mod.rs b/src/example/paid_service/tests/it/util/mod.rs index f50053d..f58b736 100644 --- a/src/example/paid_service/tests/it/util/mod.rs +++ b/src/example/paid_service/tests/it/util/mod.rs @@ -1,3 +1,3 @@ +pub mod cycles_depositor; pub mod cycles_ledger; pub mod pic_canister; -pub mod cycles_depositor;