@acala-network/wallet-v2
TypeScript icon, indicating that this package has built-in type declarations

0.0.4-36 • Public • Published

Wallet

Install

pnpm install @polkadot/api @acala-network/wallet-v2

Types

Token

const token: Token = {
  id: "0x0000",
  symbol: "ACA",
  decimals: 12,
  isNative: true,
  isEvm: false,
  evm: "0x0000000000000000000100000000000000000000",
};
  • id: Token ID used in substrate, can be used to query storage in substrate
  • symbol: Token symbol
  • decimals: Token decimals
  • isNative: Whether the token is native token
  • isEvm: Whether the token is created by EVM
  • evm: The EVM address of the token

Account

const account: Account = {
  address: "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY",
  evm: "0x0000000000000000000100000000000000000000",
  isBound: true,
  isDefaultEvm: true,
  isDefaultSubstrate: false,
};
  • address: Substrate address
  • evm: EVM address
  • isBound: Whether the account has bound EVM address to substrate address
  • isDefaultEvm: Whether the account's EVM address is computed from substrate address
  • isDefaultSubstrate: Whether the account's substrate address is computed from EVM address

Balance

Balance is based on substrate balance data structure.
For EVM token, set locked and reserved to 0, and available equals to free.

const balance: Balance = {
  free: 1000000000000000000000000n,
  locked: 0n,
  reserved: 0n,
  available: 1000000000000000000000000n,
};
  • free: Free balance
  • locked: Locked balance
  • reserved: Reserved balance
  • available: Available balance

Usage

  1. Create a wallet instance
import { Wallet } from "@acala-network/wallet-v2";
import { ApiPromise, WsProvider } from "@polkadot/api";

const api = await ApiPromise.create({
  provider: new WsProvider("wss://acala-rpc.polkadot.io"),
});

await api.isReady;

const wallet = new Wallet({
  api,
  // optional accept a public client, if not passed, the wallet will also create one
  publicClient: createPublicClient({
    chain: acala,
    transport: http(),
  }),
});
  1. Get account
const account = await wallet.getAccount(
  // accepts EVM address too
  "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY",
);

console.log(account.address);
console.log(account.evm);
  1. Get registered tokens
const tokens = await wallet.getRegisteredTokenList();

console.log(JSON.stringify(tokens, null, 2));
  1. Get token by ID or symbol
// accepts token ID, EVM address, or symbol
const token = await wallet.getToken("AUSD");

console.log(token.id);
console.log(token.symbol);
console.log(token.decimals);
  1. Get balance
const balance = await wallet.getBalance(
  // accepts token ID, EVM address, or symbol
  "ACA",
  // accepts substrate address or EVM address
  "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY",
);

console.log(balance);
  1. Watch balance
const unsubscribe = await wallet.watchBalance(
  // accepts token ID, EVM address, or symbol
  "ACA",
  // accepts substrate address or EVM address
  "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY",
  (balance) => {
    console.log(balance);
    unsubscribe();
  },
);

Dependents (1)

Package Sidebar

Install

npm i @acala-network/wallet-v2

Weekly Downloads

6

Version

0.0.4-36

License

Apache-2.0

Unpacked Size

134 kB

Total Files

84

Last publish

Collaborators

  • xlc
  • aniiantt
  • qwer951123
  • shunjizhan