junobuild_shared/ledger/icrc.rs
1use crate::ledger::types::icrc::{IcrcTransferFromResult, IcrcTransferResult};
2use candid::Principal;
3use ic_cdk::call::{Call, CallResult};
4use icrc_ledger_types::icrc1::transfer::TransferArg;
5use icrc_ledger_types::icrc2::transfer_from::TransferFromArgs;
6
7/// Initiates a transfer of tokens on a specified ledger using the provided ICRC-1 arguments.
8///
9/// This function performs a transfer using the `icrc1_transfer` method on the specified ledger
10/// and returns the result of the transfer operation.
11///
12/// # Arguments
13/// * `ledger_id` - A `Principal` representing the ID of the ledger where the transfer will be executed.
14/// * `args` - A `TransferArg` struct containing the details of the ICRC-1 transfer.
15///
16/// # Returns
17/// A `CallResult<IcrcTransferResult>` indicating either the success or failure of the ICRC-1 token transfer.
18pub async fn icrc_transfer_token(
19 ledger_id: Principal,
20 args: TransferArg,
21) -> CallResult<IcrcTransferResult> {
22 Ok(Call::bounded_wait(ledger_id, "icrc1_transfer")
23 .with_arg(args)
24 .await?
25 .candid::<IcrcTransferResult>()?)
26}
27
28/// Initiates a transfer from a specified account on a ledger using the provided ICRC-2 arguments.
29///
30/// This function performs a transfer using the `icrc2_transfer_from` method on the specified ledger
31/// and returns the result of the transfer operation.
32///
33/// # Arguments
34/// * `ledger_id` - A `Principal` representing the ID of the ledger where the transfer will be executed.
35/// * `args` - A `TransferFromArgs` struct containing the details of the ICRC-2 transfer, including the source account.
36///
37/// # Returns
38/// A `CallResult<IcrcTransferFromResult>` indicating either the success or failure of the ICRC-2 token transfer.
39pub async fn icrc_transfer_token_from(
40 ledger_id: Principal,
41 args: TransferFromArgs,
42) -> CallResult<IcrcTransferFromResult> {
43 Ok(Call::bounded_wait(ledger_id, "icrc2_transfer_from")
44 .with_arg(args)
45 .await?
46 .candid::<IcrcTransferFromResult>()?)
47}