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.
Hello All … found the candid on the ledger canister while trying to verify a block to/from/amount for payment verification … found @quint ’ s comments and his example canister in github and while I was learning what he did I noticed the one you found (query_blocks) and was off the races … now I am coming back and trying to add a link to the UI to the Internet Computer Dashboard … i.e.:
that’s a random transaction, but if I have the block can anyone confirm how to get the “Hash” to build this URL … is the “Hash” the “parent_hash” in the “Block” type?:
type Block = record {
parent_hash : opt blob;
transaction : Transaction;
timestamp : TimeStamp;
};
any confirmation that is correct and/or example converting the blob into hash would be wicked. … or if I am missing something let me know as well, might be an obvious thing …
My interface file was downloaded from Canlista, and the type for both start and length is set to bigint.
It seems to be broken on the candid interface for the archive canister as well so my guess is that it’s a bug in the typescript implementation on the decoding result side of things.