ledger-icp-js
A library for interfacing with the ICP ledger on the Internet Computer.

âšī¸ This library is meant to interface with the ICP ledger only. If you are looking to interact with Snses, ckBTC, or other ICRC tokens, use the ledger-icrc-js library.
Table of contents
Installation
You can use ledger-icp-js
by installing it in your project.
npm i @dfinity/ledger-icp
The bundle needs peer dependencies, be sure that following resources are available in your project as well.
npm i @dfinity/agent @dfinity/candid @dfinity/principal @dfinity/utils
Usage
The features are available through the class LedgerCanister
. It has to be instantiated with a canister ID.
e.g. fetching a token metadata.
import { createAgent } from "@dfinity/utils";
import { LedgerCanister } from "@dfinity/ledger-icp";
const agent = await createAgent({
identity,
host: HOST,
});
const { metadata } = LedgerCanister.create({
agent,
canisterId: MY_LEDGER_CANISTER_ID,
});
const data = await metadata();
Features
ledger-icp-js
implements following features:
:factory: AccountIdentifier
:link: Source
Static Methods
:gear: fromHex
fromHex | (hex: string) => AccountIdentifier |
:link: Source
:gear: fromPrincipal
fromPrincipal | ({ principal, subAccount, }: { principal: Principal; subAccount?: SubAccount or undefined; }) => AccountIdentifier |
:link: Source
Methods
:gear: toHex
:link: Source
:gear: toUint8Array
toUint8Array | () => Uint8Array |
:link: Source
:gear: toNumbers
:link: Source
:gear: toAccountIdentifierHash
toAccountIdentifierHash | () => { hash: Uint8Array; } |
:link: Source
:factory: SubAccount
:link: Source
Static Methods
:gear: fromBytes
fromBytes | (bytes: Uint8Array) => SubAccount or Error |
:link: Source
:gear: fromPrincipal
fromPrincipal | (principal: Principal) => SubAccount |
:link: Source
:gear: fromID
fromID | (id: number) => SubAccount |
:link: Source
Methods
:gear: toUint8Array
toUint8Array | () => Uint8Array |
:link: Source
:factory: LedgerCanister
:link: Source
Static Methods
:gear: create
create | (options?: LedgerCanisterOptions) => LedgerCanister |
:link: Source
Methods
:gear: accountBalance
Returns the balance of the specified account identifier.
If certified
is true, the request is fetched as an update call, otherwise
it is fetched using a query call.
accountBalance | ({ accountIdentifier: accountIdentifierParam, certified, }: AccountBalanceParams) => Promise<bigint> |
Parameters:
params
: The parameters to get the balance of an account.
params.accountIdentifier
: The account identifier provided either as hex string or as an AccountIdentifier.
params.certified
: query or update call.
:link: Source
:gear: metadata
Fetches the ledger metadata.
metadata | (params: QueryParams) => Promise<[string, Value][]> |
Parameters:
params
: - The parameters used to fetch the metadata, notably query or certified call.
:link: Source
:gear: transactionFee
Returns the transaction fee of the ICP ledger canister.
transactionFee | (params?: QueryParams) => Promise<bigint> |
Parameters:
params
: - Optional query parameters for the request, defaulting to { certified: false }
for backwards compatibility reason.
:link: Source
:gear: transfer
Transfer ICP from the caller to the destination accountIdentifier
.
Returns the index of the block containing the tx if it was successful.
transfer | (request: TransferRequest) => Promise<bigint> |
:link: Source
:gear: icrc1Transfer
Transfer ICP from the caller to the destination Account
.
Returns the index of the block containing the tx if it was successful.
icrc1Transfer | (request: Icrc1TransferRequest) => Promise<bigint> |
:link: Source
:gear: icrc2Approve
This method entitles the spender
to transfer token amount
on behalf of the caller from account { owner = caller; subaccount = from_subaccount }
.
Reference: https://github.com/dfinity/ICRC-1/blob/main/standards/ICRC-2/README.md#icrc2_approve
icrc2Approve | (params: Icrc2ApproveRequest) => Promise<bigint> |
Parameters:
params
: - The parameters to approve.
:link: Source
:gear: icrc21ConsentMessage
Fetches the consent message for a specified canister call, intended to provide a human-readable message that helps users make informed decisions.
icrc21ConsentMessage | (params: Icrc21ConsentMessageRequest) => Promise<icrc21_consent_info> |
Parameters:
params
: - The request parameters containing the method name, arguments, and consent preferences (e.g., language).
:link: Source
:factory: IndexCanister
:link: Source
Static Methods
:gear: create
create | ({ canisterId: optionsCanisterId, ...options }: CanisterOptions<_SERVICE>) => IndexCanister |
:link: Source
Methods
:gear: accountBalance
Returns the balance of the specified account identifier.
accountBalance | ({ certified, accountIdentifier, }: AccountBalanceParams) => Promise<bigint> |
Parameters:
params
: The parameters to get the balance of an account.
params.accountIdentifier
: The account identifier provided either as hex string or as an AccountIdentifier.
params.certified
: query or update call.
:link: Source
:gear: getTransactions
Returns the transactions and balance of an ICP account.
getTransactions | ({ certified, accountIdentifier, start, maxResults: max_results, }: GetTransactionsParams) => Promise<GetAccountIdentifierTransactionsResponse> |
Parameters:
params
: The parameters to get the transactions.
params.certified
: query or update call.
params.accountIdentifier
: The account identifier provided either as hex string or as an AccountIdentifier.
params.start
: If set then the results will start from the next most recent transaction id after start (start won't be included). If not provided, then the results will start from the most recent transaction id.
params.maxResults
: Maximum number of transactions to fetch.
:link: Source
Resources