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 3e70b5e commit 40d455d
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 465 deletions.
7 changes: 3 additions & 4 deletions dfx.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,10 @@
}
}
},
"cycles_wallet": {
"cycles_depositor": {
"type": "custom",
"candid": "https://github.com/dfinity/cycles-wallet/releases/download/20240410/wallet.did",
"wasm": "https://github.com/dfinity/cycles-wallet/releases/download/20240410/wallet.wasm",
"gzip": true
"candid": "https://github.com/dfinity/cycles-ledger/releases/download/cycles-ledger-v1.0.1/depositor.did",
"wasm": "https://github.com/dfinity/cycles-ledger/releases/download/cycles-ledger-v1.0.1/depositor.wasm.gz"
}
},
"defaults": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
didc bind --target rs --config didc.toml .dfx/local/canisters/cycles_wallet/cycles_wallet.did
didc bind --target rs --config didc.toml .dfx/local/canisters/cycles_depositor/cycles_depositor.did
23 changes: 16 additions & 7 deletions src/example/paid_service/tests/it/icrc2.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::util::cycles_depositor::{self, CyclesDepositorPic};
use crate::util::cycles_ledger::{CyclesLedgerPic, InitArgs, LedgerArgs};
use crate::util::cycles_wallet::CyclesWalletPic;
use crate::util::pic_canister::{PicCanister, PicCanisterBuilder, PicCanisterTrait};
use candid::{encode_one, Principal};
use ic_papi_api::PaymentError;
Expand All @@ -15,8 +15,10 @@ pub struct CallerPaysWithIcRc2TestSetup {
paid_service: PicCanister,
/// ICRC2 ledger
ledger: CyclesLedgerPic,
/// User
user: Principal,
/// User's wallet. We use the cycles wallet so that we can top it up easily, but any source of funds will do, with any ICRC-2 token.
wallet: CyclesWalletPic,
wallet: CyclesDepositorPic,
}
impl Default for CallerPaysWithIcRc2TestSetup {
fn default() -> Self {
Expand All @@ -25,7 +27,7 @@ impl Default for CallerPaysWithIcRc2TestSetup {
pic.clone(),
&PicCanister::cargo_wasm_path("example_paid_service"),
);
let ledger = PicCanisterBuilder::default()
let ledger = CyclesLedgerPic::from(PicCanisterBuilder::default()
.with_wasm(&PicCanister::dfx_wasm_path("cycles_ledger"))
.with_arg(
encode_one(LedgerArgs::Init(InitArgs {
Expand All @@ -34,14 +36,19 @@ impl Default for CallerPaysWithIcRc2TestSetup {
}))
.expect("Failed to encode ledger init arg"),
)
.deploy_to(pic.clone())
.into();
let wallet =
PicCanister::new(pic.clone(), &PicCanister::dfx_wasm_path("cycles_wallet")).into();
.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();
Self {
pic,
paid_service,
ledger,
user,
wallet,
}
}
Expand All @@ -57,4 +64,6 @@ fn icrc2_payment_works() {
let setup = CallerPaysWithIcRc2TestSetup::default();
// 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);
}
66 changes: 66 additions & 0 deletions src/example/paid_service/tests/it/util/cycles_depositor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#![allow(dead_code, unused_imports)]
use std::sync::Arc;

use candid::{self, CandidType, Deserialize, Principal};
use ic_cdk::api::call::CallResult as Result;
use pocket_ic::PocketIc;

use super::pic_canister::{PicCanister, PicCanisterTrait};

#[derive(CandidType, Deserialize, Debug)]
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>,
}
#[derive(CandidType, Deserialize, Debug)]
pub(crate) struct DepositArg {
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 struct Service(pub Principal);
impl Service {
pub async fn deposit(&self, arg0: &DepositArg) -> Result<(DepositResult,)> {
ic_cdk::call(self.0, "deposit", (arg0,)).await
}
}

pub struct CyclesDepositorPic {
pub pic: Arc<PocketIc>,
pub canister_id: Principal,
}

impl From<PicCanister> for CyclesDepositorPic {
fn from(pic: PicCanister) -> Self {
Self {
pic: pic.pic(),
canister_id: pic.canister_id(),
}
}
}

impl PicCanisterTrait for CyclesDepositorPic {
/// The shared PocketIc instance.
fn pic(&self) -> Arc<PocketIc> {
self.pic.clone()
}
/// The ID of this canister.
fn canister_id(&self) -> Principal {
self.canister_id.clone()
}
}

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

Loading

0 comments on commit 40d455d

Please sign in to comment.