skalenetwork / docs.skale.space

https://docs.skale.space/
MIT License
0 stars 1 forks source link

IMA.js #57

Closed manuelbarbas closed 2 days ago

manuelbarbas commented 7 months ago

IMA.js

IMA.JS is a Typescirpt/Javascript library which implements client for SKALE Interchain Messaging Agent (IMA).

Implementation Example

Package Install

npm install --save @skalenetwork/ima-js

Usage

Initialization

There are 2 ways to use IMA.js library in your project:

First approach is more convenient in general because single IMA object have simplified API for some functionality that requires both Mainnet and Schain interactions, but in this case you will need two web3 objects to be available at the same time.

Second approach is more flexible but requires more developer work in some cases.

Working with a single IMA object

Import and init single IMA-JS object:

import { IMA } from '@skalenetwork/ima-js';

import mainnetAbi from './mainnetAbi.json'; // your local sources
import schainAbi from './schainAbi.json'; // your local sources

const MAINNET_ENDPOINT = '[YOUR_ETHEREUM_MAINNET_ENDPOINT]';
const SCHAIN_ENDPOINT = '[YOUR_SCHAIN_ENDPOINT]';

const mainnetWeb3 = new Web3(MAINNET_ENDPOINT);
const sChainWeb3 = new Web3(SCHAIN_ENDPOINT);

let ima = new IMA(mainnetWeb3, sChainWeb3, mainnetAbi, sChainAbi);

Accessing Mainnet and Schain parts:

Working with 2 separate objects

Import and init Mainnet object:

import { MainnetChain } from '@skalenetwork/ima-js';
import mainnetAbi from './mainnetAbi.json'; // your local sources

const MAINNET_ENDPOINT = '[YOUR_ETHEREUM_MAINNET_ENDPOINT]';
const mainnetWeb3 = new Web3(MAINNET_ENDPOINT);

let mainnet = new MainnetChain(mainnetWeb3, mainnetAbi);

Import and init Schain object:

import { SChain } from '@skalenetwork/ima-js';
import schainAbi from './schainAbi.json'; // your local sources

const SCHAIN_ENDPOINT = '[YOUR_SCHAIN_ENDPOINT]';
const sChainWeb3 = new Web3(SCHAIN_ENDPOINT);

let schain = new SChain(sChainWeb3, schainAbi);

ETH and token transfers

Detailed documentation about ETH and token transfers using IMA-JS can be found here:

Signing transactions

There are 2 ways to sign a transaction in the current version of IMA-JS:

Signing with private key

To sing and send a transaction using local private key you should specify privateKey and address in the txOpts object:

// init ima object

const txOpts = {
    address: "[ADDRESS]",
    privateKey: "[PRIVATE_KEY]"
}

this.state.sChain.withdrawETH(
    receiverAddress,
    amountWei,
    txOpts
);

Signing with Metamask

Just drop privateKey from the txOpts object to trigger external signing, keep address field:

// init ima object

const txOpts = {
    address: "[ADDRESS]"
}

this.state.sChain.withdrawETH(
    receiverAddress,
    amountWei,
    txOpts
);
manuelbarbas commented 7 months ago

Missing the section "ETH and token transfers" due to broken links

manuelbarbas commented 6 months ago

What is: Tool Language: Javascript, Typescript Target: Web