0.1.47 • Published 3 days ago

@initia/initia.js v0.1.47

Weekly downloads
-
License
MIT
Repository
github
Last release
3 days ago

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.

  • node.js v14+
  • npm

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', // default gas prices
    gasAdjustment: '1.75',  // default gas adjustment for fee estimation
});

gasPrices and gasAdjustmentare 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', // (optional) if null, generate a new Mnemonic key
    account: 0, // (optional) BIP44 account number. default = 0
    index: 0, // (optional) BIP44 index number. defualt = 0
    coinType: 118, // (optional) BIP44 coinType. default = 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';

// serialize, serialize value to BCS and encode it to base64
const serializedU64 = bcs
    .u64() // type
    .serialize(1234); // value 
    .toBase64()

// deserialize
const deserializedU64 = bcs
    .u64() // type
    .parse(
        Uint8Array.from(Buffer.from(serializedU64, 'base64'))
    );

// vector
const serializedVector = bcs
    .vector(bcs.u64())
    .serialize([123, 456, 789])
    .toBase64();

// option
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.

  • MsgSend()

Send coins to others.

import { MsgSend } from '@initia/initia.js';

const msg = new MsgSend(
    'init1kdwzpz3wzvpdj90gtga4fw5zm9tk4cyrgnjauu',   // sender address
    'init18sj3x80fdjc6gzfvwl7lf8sxcvuvqjpvcmp6np',   // recipient address
    '1000uinit',                                     // send amount
);
  • MsgDelegate()

Delegate governance coin to validators (staking).

import { MsgDelegate } from '@initia/initia.js';

const msg = new MsgDelegate(
    'init1kdwzpz3wzvpdj90gtga4fw5zm9tk4cyrgnjauu', // delegator address
    'init18sj3x80fdjc6gzfvwl7lf8sxcvuvqjpvcmp6np', // validator's operator addres
    '100000uinit',                                 // delegate amount
)
  • MsgUndelegate()

Undelegate governance coin from validators (unstaking).

import { MsgUndelegate } from '@initia/initia.js';

const msg = new MsgUndelegate(
    'init1kdwzpz3wzvpdj90gtga4fw5zm9tk4cyrgnjauu', // delegator address
    'init18sj3x80fdjc6gzfvwl7lf8sxcvuvqjpvcmp6np', // validator's operator addres
    '100000uinit',                                 // undelegate amount
)
  • MsgExecute()

Execute move contract function.

import { MsgExecute } from '@initia/initia.js';

const msg = new MsgExecute(
    'init1kdwzpz3wzvpdj90gtga4fw5zm9tk4cyrgnjauu', // sender address
    '0x1',                                         // owner of the module
    'dex',                                         // name of the module
    'swap_script',                                 // function name
    [],                                            // type arguments
    [                                              
        bcs.address().serialize('0x2').toBase64(), // arguments, BCS-encoded
        bcs.address().serialize('0x3').toBase64(), // arguments, BCS-encoded
        bcs.u64().serialize(10000).toBase64()      // arguments, BCS-encoded
    ]
);

Tx broadcasting

  • createAndSignTx()

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', // default gas prices
    gasAdjustment: '1.75',  // default gas adjustment for fee estimation
});

const wallet = new Wallet(lcd, key);

const sendMsg = new MsgSend(
    'init14l3c2vxrdvu6y0sqykppey930s4kufsvt97aeu',   // sender address
    'init18sj3x80fdjc6gzfvwl7lf8sxcvuvqjpvcmp6np',   // recipient address
    '1000uinit',                                     // send amount
);

const signedTx = await wallet.createAndSignTx({
    msgs: [sendMsg],
    memo: 'sample memo',
});

When sending coins with MsgSend, sender address should be same with wallet address.

  • broadcast()

broadcast() is the action that sends your transaction to the blockchain code.

const broadcastResult = await lcd.tx.broadcast(signedTx);

Queries

  • balance()

Query the balance of the account.

const balances = await lcd.bank.balance('init14l3c2vxrdvu6y0sqykppey930s4kufsvt97aeu');
  • viewfunction()

Obtain the return values of a Move view function.

const res = await lcd.move
  .viewFunction(
    '0x1',                                         // owner of the module
    'dex',                                         // name of the module
    'get_swap_simulation',                         // function name
    [],                                            // type arguments
    [       
        bcs.address().serialize('0x2').toBase64(), // arguments, BCS-encoded
        bcs.address().serialize('0x3').toBase64(), // arguments, BCS-encoded
        bcs.u64().serialize(10000).toBase64()      // arguments, BCS-encoded
    ]                           
)
0.1.47

3 days ago

0.1.46

14 days ago

0.1.45

20 days ago

0.1.44

23 days ago

0.1.42

30 days ago

0.1.43

30 days ago

0.1.41

1 month ago

0.1.40

1 month ago

0.1.39

1 month ago

0.1.38

1 month ago

0.1.37

1 month ago

0.1.36

2 months ago

0.1.35

2 months ago

0.1.34

2 months ago

0.1.33

2 months ago

0.1.32

3 months ago

0.1.31

3 months ago

0.1.30

3 months ago

0.1.29

3 months ago

0.1.28

4 months ago

0.1.27

4 months ago

0.1.26

4 months ago

0.1.25

4 months ago

0.1.23

4 months ago

0.1.24

4 months ago

0.1.22

5 months ago

0.1.21

5 months ago

0.1.20

5 months ago

0.1.10

7 months ago

0.1.11

6 months ago

0.1.12

6 months ago

0.1.13

6 months ago

0.1.14

6 months ago

0.1.15

6 months ago

0.1.8

9 months ago

0.1.7

9 months ago

0.1.9

8 months ago

0.1.4

10 months ago

0.1.3

10 months ago

0.1.6

9 months ago

0.1.5

9 months ago

0.1.16

6 months ago

0.1.17

6 months ago

0.1.18

6 months ago

0.1.19

6 months ago

0.1.0

11 months ago

0.1.2

11 months ago

0.1.1

11 months ago

0.0.51

12 months ago

0.0.50

12 months ago

0.0.49

1 year ago

0.0.40

1 year ago

0.0.41

1 year ago

0.0.42

1 year ago

0.0.43

1 year ago

0.0.44

1 year ago

0.0.45

1 year ago

0.0.46

1 year ago

0.0.47

1 year ago

0.0.37

1 year ago

0.0.38

1 year ago

0.0.39

1 year ago

0.0.33

1 year ago

0.0.34

1 year ago

0.0.35

1 year ago

0.0.36

1 year ago

0.0.48

1 year ago

0.0.25

1 year ago

0.0.30

1 year ago

0.0.31

1 year ago

0.0.32

1 year ago

0.0.26

1 year ago

0.0.27

1 year ago

0.0.28

1 year ago

0.0.29

1 year ago

0.0.24

1 year ago

0.0.23

1 year ago

0.0.22

1 year ago

0.0.21

1 year ago

0.0.20

1 year ago

0.0.19

1 year ago

0.0.18

1 year ago

0.0.17

1 year ago

0.0.16

1 year ago

0.0.15

1 year ago

0.0.14

1 year ago

0.0.13

1 year ago

0.0.12

1 year ago

0.0.11

1 year ago

0.0.10

1 year ago

0.0.9

1 year ago

0.0.8

1 year ago

0.0.7

1 year ago

0.0.6

1 year ago

0.0.5

1 year ago

0.0.4

1 year ago

0.0.3

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago