Skip to content

Commit

Permalink
++
Browse files Browse the repository at this point in the history
  • Loading branch information
bitdivine committed Sep 5, 2024
1 parent 40d455d commit c965dc4
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 31 deletions.
51 changes: 35 additions & 16 deletions src/example/paid_service/tests/it/icrc2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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),
},
);
}
33 changes: 19 additions & 14 deletions src/example/paid_service/tests/it/util/cycles_depositor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<serde_bytes::ByteBuf>,
pub(crate) owner: Principal,
pub(crate) subaccount: Option<serde_bytes::ByteBuf>,
}
#[derive(CandidType, Deserialize, Debug)]
pub(crate) struct DepositArg {
pub(crate) to: Account,
pub(crate) memo: Option<serde_bytes::ByteBuf>,
pub(crate) cycles: candid::Nat,
pub(crate) to: Account,
pub(crate) memo: Option<serde_bytes::ByteBuf>,
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 {
Expand Down Expand Up @@ -59,8 +61,11 @@ impl PicCanisterTrait for CyclesDepositorPic {
}

impl CyclesDepositorPic {
pub fn deposit(&self, caller: Principal, arg0: &DepositArg) -> std::result::Result<DepositResult, String> {
pub fn deposit(
&self,
caller: Principal,
arg0: &DepositArg,
) -> std::result::Result<DepositResult, String> {
self.update(self.canister_id, "deposit", (arg0,))
}
}
}

2 changes: 1 addition & 1 deletion src/example/paid_service/tests/it/util/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pub mod cycles_depositor;
pub mod cycles_ledger;
pub mod pic_canister;
pub mod cycles_depositor;

0 comments on commit c965dc4

Please sign in to comment.