XRPLF / xrpl.js

A JavaScript/TypeScript API for interacting with the XRP Ledger in Node.js and the browser
https://xrpl.org/
1.2k stars 510 forks source link

RippledError : ledgerNnotFound [ How to get ledger from specific ledger version ] #1351

Closed prodevcn closed 3 years ago

prodevcn commented 3 years ago

I need to get ledger from specific ledger version with ripple-lib API. I did like follows :

var RippleAPI = require('ripple-lib').RippleAPI;
var api = new RippleAPI({
    server: conf.rpc_url
});

api.connect()
        .then(() => {
            var options = {ledgerVersion: 60897667};
            return api.getLedger(options)
                .then(ledger => {
                    ...
                })
                .catch(err => {
                    console.error(err);
                });
        })
        .catch(err => {
            console.error(err);
        });

But, response is like :

{
    "name": "RippledError",
    "data": {
        "error": "lgrNotFound",
        "error_code": 21,
        "error_message": "ledgerNotFound",
        "id": 57,
        "request": {
            "command": "ledger",
            "id": 57,
            "ledger_index": 60897667
        },
        "status": "error",
        "type": "response"
    }
}

How can I fix it ? Especially, where can I refer to use ripple-lib API with optional parameters I can't find them any where. The official guide provides only simple e.g

mDuo13 commented 3 years ago

Your request is formatted correctly, but the server you're asking doesn't have ledger 60897667.

Unlike many other blockchains, servers in the XRP Ledger don't need to have full history to keep up with current events, so by default a server only stores a chunk of recent ledgers (blocks), starting from when it came online. If you want to get data from an older ledger version, you need to either query another server that has that data, or configure your server to store data that far back. You can read more about ledger history for details.

Both xrpl.ws and s2.ripple.com provide full history, so you can get the data by asking one of those instead of whatever RPC URL you're using.

If you want to see what ledger versions your server has available, you can get that from the server_info's complete_ledgers field. For example:


const RippleAPI = require('ripple-lib').RippleAPI;
const api = new RippleAPI({
    server: conf.rpc_url
});

async function main() {
    r = await api.request("server_info");
    console.log("Complete ledgers:", r.info.complete_ledgers);
}

api.connect().then(main).catch( err => { console.error(err); });