ICRC1/2 Ledger Archives: How to get the length of the archives?

archives: () → (record {archives:vec record {canister_id:principal}}) query

It does not have start and end, like other standards do. The archive itself also does not store chain_length and first_index…

Is it even possible or do I have to query random numbers until I find the boundaries?
Perhaps, every ICRC1 archive has the same length?

ICP Ledger Canister as example

I know there is an index canister, but not all ledgers have them

The ICRC 1/2 ledgers are different. I suggest you looking at an ICRC1/2 ledger. For example, let’s take a look at ELNA:

You will see the archives method. By calling it, you will see this response:

(
  vec {
    record {
      block_range_end = 38_999 : nat;
      canister_id = principal "dyxt2-fqaaa-aaaaq-aacsa-cai";
      block_range_start = 0 : nat;
    };
  },
)

This indicates that blocks 0 to 38,999 are archived in canister dyxt2-fqaaa-aaaaq-aacsa-cai.

You can see the public methods on the archive canister dyxt2-fqaaa-aaaaq-aacsa-cai.

Hope this helps! Please let us know if you need more clarification.

Thanks for the answer! But why ICP ledger canister does not have start and end?
archives: () → (record {archives:vec record {canister_id:principal}}) query

I just gonna use its index canister)

1 Like

I believe that it has to do with the implementation of ICRC3 by other tokens which makes it easier and required to expose the start and last archive blocks.

I’ll see if the team has a better way but here’s an easy workaround for now:

  1. Call archives method on the NNS Ledger Canister to view the canister IDs of the archives:
(
  record {
    archives = vec {
      record { canister_id = principal "qjdve-lqaaa-aaaaa-aaaeq-cai" };
      record { canister_id = principal "qsgjb-riaaa-aaaaa-aaaga-cai" };
      record { canister_id = principal "q4eej-kyaaa-aaaaa-aaaha-cai" };
    };
  },
)
  1. Call query_blocks on the NNS Ledger Canister with start 0 and length 14_361_000:
(
  record {
    certificate = opt blob "\d9\d9\f7\a2\64\74\72\65\65\83\01\83\01\83\01\82\04\58\20\a6\5a\fa\38\e2\24\22\88\63\a8\9e\bb\73\cf\e5\5c\f9\c0\b5\e6\c8\f8\75\6c\39\88\cd\78\39\76\5e\8b\83\02\48\63\61\6e\69\73\74\65\72\83\01\83\01\83\01\82\04\58\20\d3\49\aa\33\7f\1d\e1\a6\30\c1\e3\77\e3\51\cf\9b\45\f4\58\50\76\e9\33\f6\4b\5e\3e\71\8f\9b\ea\ad\83\01\83\02\4a\00\00\00\00\00\00\00\02\01\01\83\01\83\01\83\02\4e\63\65\72\74\69\66\69\65\64\5f\64\61\74\61\82\03\58\20\24\9f\82\ca\76\48\cb\8d\07\8b\d5\de\88\92\5b\ef\6c\d5\4b\4d\a9\bc\64\c3\12\02\c0\79\f9\3f\b0\5a\82\04\58\20\d8\f6\4f\7a\fc\a6\a5\5d\4e\e6\de\d9\b0\20\0b\ac\66\51\ca\f4\c7\a1\92\02\12\b5\a0\3c\9b\f1\df\36\82\04\58\20\7a\98\5b\46\88\ba\5c\69\25\31\61\3e\e3\ad\7e\95\a5\e1\20\ea\99\38\90\d0\d0\ad\28\eb\cf\53\b6\20\82\04\58\20\6a\8a\28\cd\b0\f9\91\9c\b2\38\b1\4a\d2\ae\59\8e\ab\cd\7a\61\7e\08\04\4a\5a\ad\e6\d7\05\c4\9b\ae\82\04\58\20\6e\76\12\7f\bb\ea\29\ba\eb\0f\7a\81\61\ee\12\6b\19\3a\a7\a3\f6\95\e3\c3\41\96\43\71\dc\d2\b6\d3\82\04\58\20\22\89\ef\49\dc\cc\e1\87\cb\6f\0e\7b\2a\a3\2d\24\1e\a0\ff\8c\36\8e\39\ff\d9\97\e8\47\14\98\aa\b7\82\04\58\20\a6\50\ba\1e\09\80\b9\9c\eb\c3\6d\af\49\1b\72\8c\c6\97\de\92\59\fe\48\3a\4f\26\09\26\f1\5a\ef\34\83\01\82\04\58\20\94\a8\dd\4e\19\d2\0b\42\81\d6\8b\60\98\b8\39\0e\00\47\8e\30\fe\61\ba\03\7f\8a\7a\f6\06\44\a0\3e\83\02\44\74\69\6d\65\82\03\49\b0\f3\e2\a9\c9\f2\c5\fb\17\69\73\69\67\6e\61\74\75\72\65\58\30\83\ab\11\6a\bf\70\9a\b2\0c\5c\48\09\40\45\5e\f2\b7\82\9e\b0\d2\9b\21\52\ea\e4\22\ba\86\0e\36\bf\43\44\78\87\ad\64\28\c9\b2\fe\4e\b4\e5\97\e0\89";
    blocks = vec {};
    chain_length = 14_362_229 : nat64;
    first_block_index = 14_361_000 : nat64;
    archived_blocks = vec {
      record {
        callback = func "qjdve-lqaaa-aaaaa-aaaeq-cai".get_blocks;
        start = 0 : nat64;
        length = 7_051_992 : nat64;
      };
      record {
        callback = func "qsgjb-riaaa-aaaaa-aaaga-cai".get_blocks;
        start = 7_051_992 : nat64;
        length = 6_856_439 : nat64;
      };
      record {
        callback = func "q4eej-kyaaa-aaaaa-aaaha-cai".get_blocks;
        start = 13_908_431 : nat64;
        length = 452_569 : nat64;
      };
    };
  },
)

You will see that qjdve-lqaaa-aaaaa-aaaeq-cai holds blocks 0 to 7_051_991, qsgjb-riaaa-aaaaa-aaaga-cai blocks 7_051_992 to 13_908_430 and q4eej-kyaaa-aaaaa-aaaha-cai 13_908_431 to 14_360_999.