junobuild_shared/ledger/
icp.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
use crate::env::LEDGER;
use crate::ledger::types::icp::{BlockIndexed, Blocks};
use crate::ledger::utils::account_identifier_equal;
use candid::{Func, Principal};
use futures::future::join_all;
use ic_cdk::api::call::{CallResult, RejectionCode};
use ic_cdk::call;
use ic_ledger_types::{
    query_blocks, transfer, AccountIdentifier, ArchivedBlockRange, BlockIndex, GetBlocksArgs,
    GetBlocksResult, Memo, Operation, Subaccount, Tokens, Transaction, TransferArgs,
    TransferResult, DEFAULT_SUBACCOUNT,
};

// We do not use subaccount, yet.
pub const SUB_ACCOUNT: Subaccount = DEFAULT_SUBACCOUNT;

/// Converts a principal and subaccount into an account identifier.
///
/// # Arguments
/// * `principal` - A reference to the principal to be converted.
/// * `sub_account` - A reference to the subaccount.
///
/// # Returns
/// An `AccountIdentifier` derived from the given principal and subaccount.
pub fn principal_to_account_identifier(
    principal: &Principal,
    sub_account: &Subaccount,
) -> AccountIdentifier {
    AccountIdentifier::new(principal, sub_account)
}

/// Transfers tokens to a specified account.
///
/// # Arguments
/// * `to` - The principal of the destination account.
/// * `to_sub_account` - The subaccount of the destination account.
/// * `memo` - A memo for the transaction.
/// * `amount` - The amount of tokens to transfer.
/// * `fee` - The transaction fee.
///
/// # Returns
/// A result containing the transfer result or an error message.
pub async fn transfer_payment(
    to: &Principal,
    to_sub_account: &Subaccount,
    memo: Memo,
    amount: Tokens,
    fee: Tokens,
) -> CallResult<TransferResult> {
    let account_identifier: AccountIdentifier = principal_to_account_identifier(to, to_sub_account);

    let args = TransferArgs {
        memo,
        amount,
        fee,
        from_subaccount: Some(SUB_ACCOUNT),
        to: account_identifier,
        created_at_time: None,
    };

    transfer_token(args).await
}

/// Initiates a transfer of ICP tokens using the provided arguments and "old" ICP account identifier.
///
/// # Arguments
/// * `args` - A `TransferArgs` struct containing the details of the ICP transfer.
///
/// # Returns
/// A `CallResult<TransferResult>` indicating either the success or failure of the ICP token transfer.
pub async fn transfer_token(args: TransferArgs) -> CallResult<TransferResult> {
    let ledger = Principal::from_text(LEDGER).unwrap();

    transfer(ledger, args).await
}

/// Finds a payment transaction based on specified criteria.
///
/// # Arguments
/// * `from` - The account identifier of the sender.
/// * `to` - The account identifier of the receiver.
/// * `amount` - The amount of tokens transferred.
/// * `block_index` - The starting block index to search from.
///
/// # Returns
/// An option containing the found block indexed or None if not found.
pub async fn find_payment(
    from: AccountIdentifier,
    to: AccountIdentifier,
    amount: Tokens,
    block_index: BlockIndex,
) -> Option<BlockIndexed> {
    let ledger = Principal::from_text(LEDGER).unwrap();

    // We can use a length of block of 1 to find the block we are interested in
    // https://forum.dfinity.org/t/ledger-query-blocks-how-to/16996/4
    let response = blocks_since(ledger, block_index, 1).await.unwrap();

    fn payment_check(
        transaction: &Transaction,
        expected_from: AccountIdentifier,
        expected_to: AccountIdentifier,
        expected_amount: Tokens,
    ) -> bool {
        match &transaction.operation {
            None => (),
            Some(operation) => match operation {
                Operation::Transfer {
                    from, to, amount, ..
                } => {
                    return account_identifier_equal(expected_from, *from)
                        && account_identifier_equal(expected_to, *to)
                        && expected_amount.e8s() == amount.e8s();
                }
                Operation::Mint { .. } => (),
                Operation::Burn { .. } => (),
                Operation::Approve { .. } => (),
                Operation::TransferFrom { .. } => (),
            },
        }

        false
    }

    let block = response
        .iter()
        .find(|(_, block)| payment_check(&block.transaction, from, to, amount));

    block.cloned()
}

/// Queries the ledger for the current chain length.
///
/// # Arguments
/// * `block_index` - The block index from which to start the query.
///
/// # Returns
/// A result containing the chain length or an error message.
pub async fn chain_length(block_index: BlockIndex) -> CallResult<u64> {
    let ledger = Principal::from_text(LEDGER).unwrap();
    let response = query_blocks(
        ledger,
        GetBlocksArgs {
            start: block_index,
            length: 1,
        },
    )
    .await?;
    Ok(response.chain_length)
}

/// Finds blocks containing transfers for specified account identifiers.
///
/// # Arguments
/// * `block_index` - The starting block index for the query.
/// * `length` - The number of blocks to query.
/// * `account_identifiers` - A list of account identifiers to match transactions.
///
/// # Returns
/// A collection of blocks matching the criteria.
pub async fn find_blocks_transfer(
    block_index: BlockIndex,
    length: u64,
    account_identifiers: Vec<AccountIdentifier>,
) -> Blocks {
    let ledger = Principal::from_text(LEDGER).unwrap();

    // Source: OpenChat
    // https://github.com/open-ic/transaction-notifier/blob/cf8c2deaaa2e90aac9dc1e39ecc3e67e94451c08/canister/impl/src/lifecycle/heartbeat.rs#L73
    let response = blocks_since(ledger, block_index, length).await.unwrap();

    fn valid_mission_control(
        transaction: &Transaction,
        account_identifiers: &[AccountIdentifier],
    ) -> bool {
        match &transaction.operation {
            None => (),
            Some(operation) => match operation {
                Operation::Transfer { from, to, .. } => {
                    return account_identifiers.iter().any(|&account_identifier| {
                        account_identifier == *to || account_identifier == *from
                    });
                }
                Operation::Mint { .. } => (),
                Operation::Burn { .. } => (),
                Operation::Approve { .. } => (),
                Operation::TransferFrom { .. } => (),
            },
        }

        false
    }

    response
        .into_iter()
        .filter(|(_, block)| valid_mission_control(&block.transaction, &account_identifiers))
        .collect()
}

/// Queries the ledger for blocks since a specified index, including handling archived blocks.
///
/// # Arguments
/// * `ledger_canister_id` - The principal of the ledger canister.
/// * `start` - The starting block index.
/// * `length` - The number of blocks to query.
///
/// # Returns
/// A result containing the queried blocks or an error message.
async fn blocks_since(
    ledger_canister_id: Principal,
    start: BlockIndex,
    length: u64,
) -> CallResult<Blocks> {
    // Source: OpenChat
    // https://github.com/open-ic/transaction-notifier/blob/cf8c2deaaa2e90aac9dc1e39ecc3e67e94451c08/canister/impl/src/lifecycle/heartbeat.rs

    let response = query_blocks(ledger_canister_id, GetBlocksArgs { start, length }).await?;

    let blocks: Blocks = response
        .blocks
        .into_iter()
        .enumerate()
        .map(|(index, block)| (start + (index as u64), block))
        .collect();

    if response.archived_blocks.is_empty() {
        Ok(blocks)
    } else {
        type FromArchiveResult = CallResult<Blocks>;

        async fn get_blocks_from_archive(range: ArchivedBlockRange) -> FromArchiveResult {
            let args = GetBlocksArgs {
                start: range.start,
                length: range.length,
            };
            let func: Func = range.callback.into();

            let response: CallResult<(GetBlocksResult,)> =
                call(func.principal, &func.method, (args,)).await;

            match response {
                Err(e) => Err(e),
                Ok((block_result,)) => match block_result {
                    Err(_) => Err((
                        RejectionCode::Unknown,
                        "Block results cannot be decoded".to_string(),
                    )),
                    Ok(blocks_range) => Ok(blocks_range
                        .blocks
                        .into_iter()
                        .enumerate()
                        .map(|(index, block)| (range.start + (index as u64), block))
                        .collect()),
                },
            }
        }

        // Adapt original code .archived_blocks.into_iter().sorted_by_key(|a| a.start)
        let mut order_archived_blocks = response.archived_blocks;
        order_archived_blocks.sort_by(|a, b| a.start.cmp(&b.start));

        // Get the transactions from the archive canisters
        let futures: Vec<_> = order_archived_blocks
            .into_iter()
            .map(get_blocks_from_archive)
            .collect();

        let archive_responses: Vec<FromArchiveResult> = join_all(futures).await;

        let results = archive_responses
            .into_iter()
            .collect::<CallResult<Vec<Blocks>>>()?;

        Ok(results.into_iter().flatten().chain(blocks).collect())
    }
}