ic_icrc_tx/builder/
icrc2.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
use base64::engine::general_purpose;
use base64::Engine;
use candid::Encode;

use crate::types::CanisterCall;

pub fn build_icrc2_transfer(
    canister_id: String,
    transfer_arg: icrc_ledger_types::icrc2::approve::ApproveArgs,
) -> CanisterCall {
    CanisterCall {
        canister_id,
        method: "icrc2_approve".to_string(),
        arg: general_purpose::STANDARD.encode(Encode!(&transfer_arg).unwrap()),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use candid::Nat;
    use icrc_ledger_types::{icrc1::account::Account, icrc2::approve::ApproveArgs};

    #[test]
    fn test_build_icrc2_transfer() {
        let canister_id = "rh2pm-ryaaa-aaaan-qeniq-cai".to_string();
        let transfer_arg = ApproveArgs {
            from_subaccount: None,
            spender: Account {
                owner: "6pfju-rc52z-aihtt-ahhg6-z2bzc-ofp5r-igp5i-qy5ep-j6vob-gs3ae-nae"
                    .parse()
                    .unwrap(),
                subaccount: None,
            },
            amount: Nat::from(10000000u64),
            expected_allowance: None,
            expires_at: None,
            fee: None,
            memo: None,
            created_at_time: None,
        };

        let result = build_icrc2_transfer(canister_id.clone(), transfer_arg.clone());

        println!("Encoded Arg: {}", result.arg);

        assert_eq!(result.canister_id, canister_id);
        assert_eq!(result.method, "icrc2_approve".to_string());
        assert_eq!(
            result.arg,
            general_purpose::STANDARD.encode(Encode!(&transfer_arg).unwrap())
        );
    }
}