query_blocks for the ICP ledger canister works. It even gives you an index for archive canisters if the record is too old.
Looks like this was added to the repo March 3rd. No idea when it went live on mainnet, but it is there now.
DFINITY folks, you’ve got to shout this kind of stuff from the rooftops. This feature drastically reduces complexity and basically enables all kinds of new functionality. THANK YOU!!!
If I missed the announcement for this please point me to it, I don’t want to miss stuff like this again.
To query the main ledger(last couple thousand blocks or so, if not in range you get the index:
I’m using the get_blocks() method and everything functions without any errors, but I’m only getting empty arrays back, no blocks are being returned. what am I missing? Below is the relevant code.
how I instantiate the ledger index canister:
private let ledgerIndex : Ledger.InterfaceIndex = actor("qjdve-lqaaa-aaaaa-aaaeq-cai");
Where I call the function:
let tipOfChainIndex = await tipOfChainDetails();
let startIndex : Nat64 = tipOfChainIndex.0 - 1000;
let queryLength : Nat64 = 999;
let queryResult = await ledgerIndex.get_blocks({
start = startIndex;
length = queryLength;
});
On the main node it is query_blocks. On the archive nodes it is get_blocks. Make sure you have your did file right, AccountIdentfiers are blobs in these structures.
Up to 2000 blocks on the main node, all the way back on the archive.
The maximum size of a response is 2MB, sonic it bigger than that you’ll have to chunk.
Scan the chain and index it so you don’t have to donut a second time. I’d love for dfinity to offer some meta data Canisters for stuff like this. I think the Rosetta server lets you do those queries, but your canister can’t get to them.
Thank you. That’s the ledger canister, it doesn’t store all the blocks but only the latest ones. The one that I was referring to is the archive which stores all ledger transactions. Did you try the candid UI and get the same error? I’m not sure what I’m doing wrong when calling the candid directly with the candid UI.
In any case, I get the same error when calling the query_blocks method on the ledger canister. It tells me the error Not an option type.