initia.js
JavaScript SDK for Initia, written in TypeScript
Main Features
- Improve user-friendly Typescript definitions with Initia core data structures integration
- Core Layer: Key management, BCS serialization, Support Initia.proto etc
- Client Layer: API request generation, LCD provider etc
Prerequisites
Initia.js library requires the installation of the following packages in order to function properly.
Installation
Before installation, check the latest version of npm:
npm install @initia/initia.js
Usage;
The usage section of this document provides detailed explanations and code examples of the most commonly used classes of the Initia.js library, which can be utilized both in a Node.js environment and within a browser.
LCD client
LCD(Light Client Daemon) class facilitates interaction with the Initia blockchain.
import { LCDClient } from '@initia/initia.js'
const lcd = new LCDClient('https://lcd.mahalo-1.initia.xyz', {
chainId: 'mahalo-1',
gasPrices: '0.15uinit',
gasAdjustment: '1.75',
});
gasPrices
and **gasAdjustment
**are optional, but essential for the fee estimation
Key
An abstract key interface that enables transaction signing and provides Bech32 address and public key derivation from a public key.
import { MnemonicKey } from '@initia/initia.js';
const key = new MnemonicKey({
mnemonic: 'bird upset ... evil cigar',
account: 0,
index: 0,
coinType: 118,
});
BCS
BCS(Binary Canonical Serialization) is the binary encoding for Move resources and other non-module values published on-chain.
import { bcs } from '@initia/initia.js';
const serializedU64 = bcs
.u64()
.serialize(1234);
.toBase64()
const deserializedU64 = bcs
.u64()
.parse(
Uint8Array.from(Buffer.from(serializedU64, 'base64'))
);
const serializedVector = bcs
.vector(bcs.u64())
.serialize([123, 456, 789])
.toBase64();
const serializedSome = bcs.option(bcs.u64()).serialize(123);
const serializedNone = bcs.option(bcs.u64()).serialize(null);
Support types for BCS
`u8`, `u16`, `u32`, `u64`, `u128`, `u256`, `bool`, `vector`, `address`, `string`, `option`, `fixed_point32`, `fixed_point64`, `decimal128`, `decimal256`
Msg
Msgs are object whose end-goal is to trigger state-transitions. They are wrapped in transactions, which may contain one or more of them.
Send coins to others.
import { MsgSend } from '@initia/initia.js';
const msg = new MsgSend(
'init1kdwzpz3wzvpdj90gtga4fw5zm9tk4cyrgnjauu',
'init18sj3x80fdjc6gzfvwl7lf8sxcvuvqjpvcmp6np',
'1000uinit',
);
Delegate governance coin to validators (staking).
import { MsgDelegate } from '@initia/initia.js';
const msg = new MsgDelegate(
'init1kdwzpz3wzvpdj90gtga4fw5zm9tk4cyrgnjauu',
'init18sj3x80fdjc6gzfvwl7lf8sxcvuvqjpvcmp6np',
'100000uinit',
)
Undelegate governance coin from validators (unstaking).
import { MsgUndelegate } from '@initia/initia.js';
const msg = new MsgUndelegate(
'init1kdwzpz3wzvpdj90gtga4fw5zm9tk4cyrgnjauu',
'init18sj3x80fdjc6gzfvwl7lf8sxcvuvqjpvcmp6np',
'100000uinit',
)
Execute move contract function.
import { MsgExecute } from '@initia/initia.js';
const msg = new MsgExecute(
'init1kdwzpz3wzvpdj90gtga4fw5zm9tk4cyrgnjauu',
'0x1',
'dex',
'swap_script',
[],
[
bcs.address().serialize('0x2').toBase64(),
bcs.address().serialize('0x3').toBase64(),
bcs.u64().serialize(10000).toBase64()
]
);
Tx broadcasting
Create a wallet and sign transaction.
import { Wallet, LCDClient, MnemonicKey } from '@initia/initia.js';
const key = new MnemonicKey({
mnemonic:
'moral wise tape glance grit gentle movie doll omit you pet soon enter year funny gauge digital supply cereal city ring egg repair coyote',
});
const lcd = new LCDClient('https://lcd.mahalo-1.initia.xyz', {
chainId: 'mahalo-1',
gasPrices: '0.15uinit',
gasAdjustment: '1.75',
});
const wallet = new Wallet(lcd, key);
const sendMsg = new MsgSend(
'init14l3c2vxrdvu6y0sqykppey930s4kufsvt97aeu',
'init18sj3x80fdjc6gzfvwl7lf8sxcvuvqjpvcmp6np',
'1000uinit',
);
const signedTx = await wallet.createAndSignTx({
msgs: [sendMsg],
memo: 'sample memo',
});
When sending coins with MsgSend
, sender address should be same with wallet address.
broadcast()
is the action that sends your transaction to the blockchain code.
const broadcastResult = await lcd.tx.broadcast(signedTx);
Queries
Query the balance of the account.
const balances = await lcd.bank.balance('init14l3c2vxrdvu6y0sqykppey930s4kufsvt97aeu');
Obtain the return values of a Move view function.
const res = await lcd.move
.viewFunction(
'0x1',
'dex',
'get_swap_simulation',
[],
[
bcs.address().serialize('0x2').toBase64(),
bcs.address().serialize('0x3').toBase64(),
bcs.u64().serialize(10000).toBase64()
]
)