From 40d455d7e26341a178d9351211fc1b6b41134510 Mon Sep 17 00:00:00 2001 From: Max Murphy Date: Thu, 5 Sep 2024 17:08:26 +0200 Subject: [PATCH] ++ --- dfx.json | 7 +- ...les_wallet.sh => bind.cycles_depositor.sh} | 2 +- src/example/paid_service/tests/it/icrc2.rs | 23 +- .../tests/it/util/cycles_depositor.rs | 66 +++ .../tests/it/util/cycles_wallet.rs | 452 ------------------ src/example/paid_service/tests/it/util/mod.rs | 2 +- 6 files changed, 87 insertions(+), 465 deletions(-) rename scripts/{bind.cycles_wallet.sh => bind.cycles_depositor.sh} (64%) create mode 100644 src/example/paid_service/tests/it/util/cycles_depositor.rs delete mode 100644 src/example/paid_service/tests/it/util/cycles_wallet.rs diff --git a/dfx.json b/dfx.json index cc20750..75d9bfb 100644 --- a/dfx.json +++ b/dfx.json @@ -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": { diff --git a/scripts/bind.cycles_wallet.sh b/scripts/bind.cycles_depositor.sh similarity index 64% rename from scripts/bind.cycles_wallet.sh rename to scripts/bind.cycles_depositor.sh index 06818e8..1ece5c8 100755 --- a/scripts/bind.cycles_wallet.sh +++ b/scripts/bind.cycles_depositor.sh @@ -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 diff --git a/src/example/paid_service/tests/it/icrc2.rs b/src/example/paid_service/tests/it/icrc2.rs index 683278e..769fb7a 100644 --- a/src/example/paid_service/tests/it/icrc2.rs +++ b/src/example/paid_service/tests/it/icrc2.rs @@ -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; @@ -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 { @@ -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 { @@ -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, } } @@ -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); } diff --git a/src/example/paid_service/tests/it/util/cycles_depositor.rs b/src/example/paid_service/tests/it/util/cycles_depositor.rs new file mode 100644 index 0000000..ccdeb6e --- /dev/null +++ b/src/example/paid_service/tests/it/util/cycles_depositor.rs @@ -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, +} +#[derive(CandidType, Deserialize, Debug)] +pub(crate) struct DepositArg { + 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 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, + pub canister_id: Principal, +} + +impl From 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 { + 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 { + self.update(self.canister_id, "deposit", (arg0,)) + } +} + diff --git a/src/example/paid_service/tests/it/util/cycles_wallet.rs b/src/example/paid_service/tests/it/util/cycles_wallet.rs deleted file mode 100644 index 6d60cbc..0000000 --- a/src/example/paid_service/tests/it/util/cycles_wallet.rs +++ /dev/null @@ -1,452 +0,0 @@ -// This is an experimental feature to generate Rust binding from Candid. -// You may want to manually adjust some of the types. -#![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) enum Kind { - User, - Canister, - Unknown, -} -#[derive(CandidType, Deserialize, Debug)] -pub(crate) enum Role { - Custodian, - Contact, - Controller, -} -#[derive(CandidType, Deserialize, Debug)] -pub(crate) struct AddressEntry { - pub(crate) id: Principal, - pub(crate) kind: Kind, - pub(crate) name: Option, - pub(crate) role: Role, -} -pub(crate) type WalletResult = std::result::Result<(), String>; -#[derive(CandidType, Deserialize, Debug)] -pub(crate) struct GetChartArgInner { - pub(crate) count: Option, - pub(crate) precision: Option, -} -#[derive(CandidType, Deserialize, Debug)] -pub(crate) struct GetEventsArgInner { - pub(crate) to: Option, - pub(crate) from: Option, -} -#[derive(CandidType, Deserialize, Debug)] -pub(crate) enum EventKind { - CyclesReceived { - from: Principal, - memo: Option, - amount: u64, - }, - CanisterCreated { - cycles: u64, - canister: Principal, - }, - CanisterCalled { - cycles: u64, - method_name: String, - canister: Principal, - }, - CyclesSent { - to: Principal, - amount: u64, - refund: u64, - }, - AddressRemoved { - id: Principal, - }, - WalletDeployed { - canister: Principal, - }, - AddressAdded { - id: Principal, - name: Option, - role: Role, - }, -} -#[derive(CandidType, Deserialize, Debug)] -pub(crate) struct Event { - pub(crate) id: u32, - pub(crate) kind: EventKind, - pub(crate) timestamp: u64, -} -#[derive(CandidType, Deserialize, Debug)] -pub(crate) struct GetEvents128ArgInner { - pub(crate) to: Option, - pub(crate) from: Option, -} -#[derive(CandidType, Deserialize, Debug)] -pub(crate) enum EventKind128 { - CyclesReceived { - from: Principal, - memo: Option, - amount: candid::Nat, - }, - CanisterCreated { - cycles: candid::Nat, - canister: Principal, - }, - CanisterCalled { - cycles: candid::Nat, - method_name: String, - canister: Principal, - }, - CyclesSent { - to: Principal, - amount: candid::Nat, - refund: candid::Nat, - }, - AddressRemoved { - id: Principal, - }, - WalletDeployed { - canister: Principal, - }, - AddressAdded { - id: Principal, - name: Option, - role: Role, - }, -} -#[derive(CandidType, Deserialize, Debug)] -pub(crate) struct Event128 { - pub(crate) id: u32, - pub(crate) kind: EventKind128, - pub(crate) timestamp: u64, -} -#[derive(CandidType, Deserialize, Debug)] -pub(crate) struct GetManagedCanisterEventsArg { - pub(crate) to: Option, - pub(crate) from: Option, - pub(crate) canister: Principal, -} -#[derive(CandidType, Deserialize, Debug)] -pub(crate) enum ManagedCanisterEventKind { - CyclesSent { amount: u64, refund: u64 }, - Created { cycles: u64 }, - Called { cycles: u64, method_name: String }, -} -#[derive(CandidType, Deserialize, Debug)] -pub(crate) struct ManagedCanisterEvent { - pub(crate) id: u32, - pub(crate) kind: ManagedCanisterEventKind, - pub(crate) timestamp: u64, -} -#[derive(CandidType, Deserialize, Debug)] -pub(crate) struct GetManagedCanisterEvents128Arg { - pub(crate) to: Option, - pub(crate) from: Option, - pub(crate) canister: Principal, -} -#[derive(CandidType, Deserialize, Debug)] -pub(crate) enum ManagedCanisterEventKind128 { - CyclesSent { - amount: candid::Nat, - refund: candid::Nat, - }, - Created { - cycles: candid::Nat, - }, - Called { - cycles: candid::Nat, - method_name: String, - }, -} -#[derive(CandidType, Deserialize, Debug)] -pub(crate) struct ManagedCanisterEvent128 { - pub(crate) id: u32, - pub(crate) kind: ManagedCanisterEventKind128, - pub(crate) timestamp: u64, -} -#[derive(CandidType, Deserialize, Debug)] -pub(crate) struct HeaderField(pub(crate) String, pub(crate) String); -#[derive(CandidType, Deserialize, Debug)] -pub(crate) struct HttpRequest { - pub(crate) url: String, - pub(crate) method: String, - pub(crate) body: serde_bytes::ByteBuf, - pub(crate) headers: Vec, -} -#[derive(CandidType, Deserialize, Debug)] -pub(crate) struct Token {} -#[derive(CandidType, Deserialize, Debug)] -pub(crate) struct StreamingCallbackHttpResponse { - pub(crate) token: Option, - pub(crate) body: serde_bytes::ByteBuf, -} -candid::define_function!(pub(crate) StreamingStrategyCallbackCallback : ( - Token, - ) -> (StreamingCallbackHttpResponse) query); -#[derive(CandidType, Deserialize, Debug)] -pub(crate) enum StreamingStrategy { - Callback { - token: Token, - callback: StreamingStrategyCallbackCallback, - }, -} -#[derive(CandidType, Deserialize, Debug)] -pub(crate) struct HttpResponse { - pub(crate) body: serde_bytes::ByteBuf, - pub(crate) headers: Vec, - pub(crate) streaming_strategy: Option, - pub(crate) status_code: u16, -} -#[derive(CandidType, Deserialize, Debug)] -pub(crate) struct ListManagedCanistersArg { - pub(crate) to: Option, - pub(crate) from: Option, -} -#[derive(CandidType, Deserialize, Debug)] -pub(crate) struct ManagedCanisterInfo { - pub(crate) id: Principal, - pub(crate) name: Option, - pub(crate) created_at: u64, -} -#[derive(CandidType, Deserialize, Debug)] -pub(crate) struct WalletBalanceRet { - pub(crate) amount: u64, -} -#[derive(CandidType, Deserialize, Debug)] -pub(crate) struct WalletBalance128Ret { - pub(crate) amount: candid::Nat, -} -#[derive(CandidType, Deserialize, Debug)] -pub(crate) struct WalletCallArg { - pub(crate) args: serde_bytes::ByteBuf, - pub(crate) cycles: u64, - pub(crate) method_name: String, - pub(crate) canister: Principal, -} -#[derive(CandidType, Deserialize, Debug)] -pub(crate) struct WalletResultCallOk { - pub(crate) r#return: serde_bytes::ByteBuf, -} -pub(crate) type WalletResultCall = std::result::Result; -#[derive(CandidType, Deserialize, Debug)] -pub(crate) struct WalletCall128Arg { - pub(crate) args: serde_bytes::ByteBuf, - pub(crate) cycles: candid::Nat, - pub(crate) method_name: String, - pub(crate) canister: Principal, -} -#[derive(CandidType, Deserialize, Debug)] -pub(crate) struct WalletCallWithMaxCyclesArg { - pub(crate) args: serde_bytes::ByteBuf, - pub(crate) method_name: String, - pub(crate) canister: Principal, -} -#[derive(CandidType, Deserialize, Debug)] -pub(crate) struct WalletResultCallWithMaxCyclesOk { - pub(crate) r#return: serde_bytes::ByteBuf, - pub(crate) attached_cycles: candid::Nat, -} -pub(crate) type WalletResultCallWithMaxCycles = - std::result::Result; -#[derive(CandidType, Deserialize, Debug)] -pub(crate) struct CanisterSettings { - pub(crate) controller: Option, - pub(crate) freezing_threshold: Option, - pub(crate) controllers: Option>, - pub(crate) memory_allocation: Option, - pub(crate) compute_allocation: Option, -} -#[derive(CandidType, Deserialize, Debug)] -pub(crate) struct CreateCanisterArgs { - pub(crate) cycles: u64, - pub(crate) settings: CanisterSettings, -} -#[derive(CandidType, Deserialize, Debug)] -pub(crate) struct WalletResultCreateOk { - pub(crate) canister_id: Principal, -} -pub(crate) type WalletResultCreate = std::result::Result; -#[derive(CandidType, Deserialize, Debug)] -pub(crate) struct CreateCanisterArgs128 { - pub(crate) cycles: candid::Nat, - pub(crate) settings: CanisterSettings, -} -#[derive(CandidType, Deserialize, Debug)] -pub(crate) struct ReceiveOptions { - pub(crate) memo: Option, -} -#[derive(CandidType, Deserialize, Debug)] -pub(crate) struct WalletSendArg { - pub(crate) canister: Principal, - pub(crate) amount: u64, -} -#[derive(CandidType, Deserialize, Debug)] -pub(crate) struct WalletSend128Arg { - pub(crate) canister: Principal, - pub(crate) amount: candid::Nat, -} -#[derive(CandidType, Deserialize, Debug)] -pub(crate) struct WalletStoreWalletWasmArg { - pub(crate) wasm_module: serde_bytes::ByteBuf, -} - -pub struct Service(pub Principal); -impl Service { - pub async fn add_address(&self, arg0: &AddressEntry) -> Result<()> { - ic_cdk::call(self.0, "add_address", (arg0,)).await - } - pub async fn add_controller(&self, arg0: &Principal) -> Result<()> { - ic_cdk::call(self.0, "add_controller", (arg0,)).await - } - pub async fn authorize(&self, arg0: &Principal) -> Result<()> { - ic_cdk::call(self.0, "authorize", (arg0,)).await - } - pub async fn deauthorize(&self, arg0: &Principal) -> Result<(WalletResult,)> { - ic_cdk::call(self.0, "deauthorize", (arg0,)).await - } - pub async fn get_chart(&self, arg0: &Option) -> Result<(Vec<(u64, u64)>,)> { - ic_cdk::call(self.0, "get_chart", (arg0,)).await - } - pub async fn get_controllers(&self) -> Result<(Vec,)> { - ic_cdk::call(self.0, "get_controllers", ()).await - } - pub async fn get_custodians(&self) -> Result<(Vec,)> { - ic_cdk::call(self.0, "get_custodians", ()).await - } - pub async fn get_events(&self, arg0: &Option) -> Result<(Vec,)> { - ic_cdk::call(self.0, "get_events", (arg0,)).await - } - pub async fn get_events_128( - &self, - arg0: &Option, - ) -> Result<(Vec,)> { - ic_cdk::call(self.0, "get_events128", (arg0,)).await - } - pub async fn get_managed_canister_events( - &self, - arg0: &GetManagedCanisterEventsArg, - ) -> Result<(Option>,)> { - ic_cdk::call(self.0, "get_managed_canister_events", (arg0,)).await - } - pub async fn get_managed_canister_events_128( - &self, - arg0: &GetManagedCanisterEvents128Arg, - ) -> Result<(Option>,)> { - ic_cdk::call(self.0, "get_managed_canister_events128", (arg0,)).await - } - pub async fn http_request(&self, arg0: &HttpRequest) -> Result<(HttpResponse,)> { - ic_cdk::call(self.0, "http_request", (arg0,)).await - } - pub async fn list_addresses(&self) -> Result<(Vec,)> { - ic_cdk::call(self.0, "list_addresses", ()).await - } - pub async fn list_managed_canisters( - &self, - arg0: &ListManagedCanistersArg, - ) -> Result<(Vec, u32)> { - ic_cdk::call(self.0, "list_managed_canisters", (arg0,)).await - } - pub async fn name(&self) -> Result<(Option,)> { - ic_cdk::call(self.0, "name", ()).await - } - pub async fn remove_address(&self, arg0: &Principal) -> Result<(WalletResult,)> { - ic_cdk::call(self.0, "remove_address", (arg0,)).await - } - pub async fn remove_controller(&self, arg0: &Principal) -> Result<(WalletResult,)> { - ic_cdk::call(self.0, "remove_controller", (arg0,)).await - } - pub async fn set_name(&self, arg0: &String) -> Result<()> { - ic_cdk::call(self.0, "set_name", (arg0,)).await - } - pub async fn set_short_name( - &self, - arg0: &Principal, - arg1: &Option, - ) -> Result<(Option,)> { - ic_cdk::call(self.0, "set_short_name", (arg0, arg1)).await - } - pub async fn wallet_api_version(&self) -> Result<(String,)> { - ic_cdk::call(self.0, "wallet_api_version", ()).await - } - pub async fn wallet_balance(&self) -> Result<(WalletBalanceRet,)> { - ic_cdk::call(self.0, "wallet_balance", ()).await - } - pub async fn wallet_balance_128(&self) -> Result<(WalletBalance128Ret,)> { - ic_cdk::call(self.0, "wallet_balance128", ()).await - } - pub async fn wallet_call(&self, arg0: &WalletCallArg) -> Result<(WalletResultCall,)> { - ic_cdk::call(self.0, "wallet_call", (arg0,)).await - } - pub async fn wallet_call_128(&self, arg0: &WalletCall128Arg) -> Result<(WalletResultCall,)> { - ic_cdk::call(self.0, "wallet_call128", (arg0,)).await - } - pub async fn wallet_call_with_max_cycles( - &self, - arg0: &WalletCallWithMaxCyclesArg, - ) -> Result<(WalletResultCallWithMaxCycles,)> { - ic_cdk::call(self.0, "wallet_call_with_max_cycles", (arg0,)).await - } - pub async fn wallet_create_canister( - &self, - arg0: &CreateCanisterArgs, - ) -> Result<(WalletResultCreate,)> { - ic_cdk::call(self.0, "wallet_create_canister", (arg0,)).await - } - pub async fn wallet_create_canister_128( - &self, - arg0: &CreateCanisterArgs128, - ) -> Result<(WalletResultCreate,)> { - ic_cdk::call(self.0, "wallet_create_canister128", (arg0,)).await - } - pub async fn wallet_create_wallet( - &self, - arg0: &CreateCanisterArgs, - ) -> Result<(WalletResultCreate,)> { - ic_cdk::call(self.0, "wallet_create_wallet", (arg0,)).await - } - pub async fn wallet_create_wallet_128( - &self, - arg0: &CreateCanisterArgs128, - ) -> Result<(WalletResultCreate,)> { - ic_cdk::call(self.0, "wallet_create_wallet128", (arg0,)).await - } - pub async fn wallet_receive(&self, arg0: &Option) -> Result<()> { - ic_cdk::call(self.0, "wallet_receive", (arg0,)).await - } - pub async fn wallet_send(&self, arg0: &WalletSendArg) -> Result<(WalletResult,)> { - ic_cdk::call(self.0, "wallet_send", (arg0,)).await - } - pub async fn wallet_send_128(&self, arg0: &WalletSend128Arg) -> Result<(WalletResult,)> { - ic_cdk::call(self.0, "wallet_send128", (arg0,)).await - } - pub async fn wallet_store_wallet_wasm(&self, arg0: &WalletStoreWalletWasmArg) -> Result<()> { - ic_cdk::call(self.0, "wallet_store_wallet_wasm", (arg0,)).await - } -} - -pub struct CyclesWalletPic { - pub pic: Arc, - pub canister_id: Principal, -} - -impl From for CyclesWalletPic { - fn from(pic: PicCanister) -> Self { - Self { - pic: pic.pic(), - canister_id: pic.canister_id(), - } - } -} - -impl PicCanisterTrait for CyclesWalletPic { - /// The shared PocketIc instance. - fn pic(&self) -> Arc { - self.pic.clone() - } - /// The ID of this canister. - fn canister_id(&self) -> Principal { - self.canister_id.clone() - } -} diff --git a/src/example/paid_service/tests/it/util/mod.rs b/src/example/paid_service/tests/it/util/mod.rs index 1c5adb0..f50053d 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_ledger; -pub mod cycles_wallet; pub mod pic_canister; +pub mod cycles_depositor;