0.0.12-next-2023-07-30 • Published 2 years ago

@dfinity/ledger v0.0.12-next-2023-07-30

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
2 years ago

ledger-js

A library for interfacing with ICRC ledger on the Internet Computer.

npm version GitHub license

Table of contents

Installation

You can use ledger-js by installing it in your project.

npm i @dfinity/ledger

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 { IcrcLedgerCanister } from "@dfinity/ledger";
import { createAgent } from "@dfinity/utils";

const agent = await createAgent({
  identity,
  host: HOST,
});

const { metadata } = LedgerCanister.create({
  agent,
  canisterId: MY_LEDGER_CANISTER_ID,
});

const data = await metadata();

Features

ledger-js implements following features:

:toolbox: Functions

:gear: encodeIcrcAccount

Encodes an Icrc-1 account compatible into a string. Formatting Reference: https://github.com/dfinity/ICRC-1/blob/main/standards/ICRC-1/TextualEncoding.md

FunctionType
encodeIcrcAccount({ owner, subaccount, }: IcrcAccount) => string

Parameters:

  • account: : Principal, subaccount?: Uint8Array }

:link: Source

:gear: decodeIcrcAccount

Decodes a string into an Icrc-1 compatible account. Formatting Reference: https://github.com/dfinity/ICRC-1/blob/main/standards/ICRC-1/TextualEncoding.md

FunctionType
decodeIcrcAccount(accountString: string) => IcrcAccount

Parameters:

  • accountString: string

:link: Source

:gear: decodePayment

šŸ‘€ This feature is currently in draft. You can find more information about it at https://github.com/dfinity/ICRC/issues/22.

A naive implementation of a payment parser. Given a code, the function attempts to extract a token name, account identifier (textual representation), and an optional amount.

If the code doesn't match the expected pattern, undefined is returned for simplicity. Similarly, if an optional amount is provided but it's not a valid number, the parser will not throw an exception and returns undefined.

Please note that this function doesn't perform any validity checks on the extracted information. It does not verify if the token is known or if the identifier is a valid address.

urn            = token ":" address [ "?" params]
token         = [ ckbtc / icp / chat / bitcoin / ethereum ... ]
address       = STRING
params        = param [ "&" params ]
param         = [ amountparam ]
amountparam   = "amount=" *digit [ "." *digit ]
FunctionType
decodePayment(code: string) => { token: string; identifier: string; amount?: number; }

Parameters:

  • code: string

:link: Source

:factory: IcrcLedgerCanister

:link: Source

Methods

:gear: create
MethodType
create(options: IcrcLedgerCanisterOptions<_SERVICE>) => IcrcLedgerCanister

:link: Source

:gear: metadata

The token metadata (name, symbol, etc.).

MethodType
metadata(params: QueryParams) => Promise<IcrcTokenMetadataResponse>

:link: Source

:gear: transactionFee

The ledger transaction fees.

MethodType
transactionFee(params: QueryParams) => Promise<bigint>

:link: Source

:gear: balance

Returns the balance for a given account provided as owner and with optional subaccount.

MethodType
balance(params: BalanceParams) => Promise<bigint>

Parameters:

  • params: The parameters to get the balance of an account.

:link: Source

:gear: transfer

Transfers tokens from the sender to the given account.

MethodType
transfer(params: TransferParams) => Promise<bigint>

Parameters:

  • params: The parameters to transfer tokens.

:link: Source

:gear: totalTokensSupply

Returns the total supply of tokens.

MethodType
totalTokensSupply(params: QueryParams) => Promise<bigint>

:link: Source

:factory: IcrcIndexCanister

:link: Source

Methods

:gear: create
MethodType
create(options: IcrcLedgerCanisterOptions<_SERVICE>) => IcrcIndexCanister

:link: Source

:gear: getTransactions

Get the transactions of an account

Always certified. get_account_transactions needs to be called with an update because the index canisters makes a call to the ledger canister to get the transaction data. Index Canister only holds the transactions ids in state, not the whole transaction data.

MethodType
getTransactions(params: GetAccountTransactionsParams) => Promise<GetTransactions>

:link: Source

Resources

0.0.15

2 years ago

0.0.11

2 years ago

0.0.12

2 years ago

0.0.13

2 years ago

0.0.14

2 years ago

1.0.0

2 years ago

0.0.9

2 years ago

0.0.8

2 years ago

0.0.7

3 years ago

0.0.6

3 years ago

0.0.10

2 years ago

0.0.5

3 years ago

0.0.4

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago