This site requires Javascript to be enabled.

IBC transaction via arch3.js

In this tutorial, you will learn how to execute an Inter-Blockchain Communication (IBC) transaction using the arch3.js library. The transaction will transfer tokens from your Archway account to a destination address on the Cosmos Hub blockchain.

Prerequisites

Before moving forward, ensure that you have completed the following prerequisites:

Create a new NPM project

Create a folder to store your project, and within that folder, execute the following command to initialize an NPM project:

npm init -y

A default package.json file is generated with a structure similar to the following:

{  "name": "ibc_transaction",  "version": "1.0.0",  "description": "",  "main": "index.js",  "scripts": {    "test": "echo \"Error: no test specified\" && exit 1"  },  "keywords": [],  "author": "",  "license": "ISC"}

Add "type": "module", to the package.json file to enable the use of ES6 module import syntax in your Node.js application.

Install required packages

Execute the following command to install arch3.js:

npm install --save @archwayhq/arch3.js

Execute the following command to install the long library, which is used for working with large integers:

npm install --save long

Execute the following command to install the dotenv library, which will be used for reading your mnemonic stored in a ".env" file:

npm install --save dotenv

Create .env file

Create a ".env" file that will be used to store your mnemonic and allows for not saving this sensitive information to your source control. In the ".env" file, add the following:

MNEMONIC="enter mnemonic here"
COSMOS_ADDRESS="enter your Cosmos wallet address here"

You can now add your .env file to your .gitignore file so it’s not added to source control.

Create index.js file

In the root of your project folder create an index.js file. This file will contain the entire script that will be executed to complete the IBC transaction.

Script breakdown

  1. Import the necessary dependencies in your index.js file:
import { SigningArchwayClient } from '@archwayhq/arch3.js';import { DirectSecp256k1HdWallet } from "@cosmjs/proto-signing";import Long from "long";import dotenv from "dotenv";
  1. Configure dotenv:
dotenv.config();
  1. Create the main function which will contain the lines of code below:
async function main() {}
  1. Set up the network data:

mainnet

testnet

const network = {  chainId: 'archway-1',  endpoint: 'https://rpc.mainnet.archway.io',  prefix: 'archway',};
  1. Set up your wallet using your mnemonic:
const mnemonic = process.env.MNEMONIC;const wallet = await DirectSecp256k1HdWallet.fromMnemonic(mnemonic, { prefix: network.prefix });const accounts = await wallet.getAccounts();
  1. Set account (sender) and destination addresses:
const accountAddress = accounts[0].address;const destinationAddress = process.env.COSMOS_ADDRESS;
  1. Create the SigningArchwayClient using the connectWithSigner method.
const signingClient = await SigningArchwayClient.connectWithSigner(network.endpoint, wallet);
  1. Create the IBC transfer message with the necessary parameters. You can find the source channels for Archway's supported IBC Connections, here:

mainnet

testnet

const msgIBCTransfer = {  typeUrl: "/ibc.applications.transfer.v1.MsgTransfer",  value: {    sourcePort: 'transfer',    sourceChannel: 'channel-0', // channel of the bridge    token: {      denom: 'aarch',      amount: '1000000000000000000'    },    sender: accountAddress,    receiver: destinationAddress,    // Timeout is in nanoseconds, you can also just send Long.UZERO for default timeout    timeoutTimestamp: Long.fromNumber(Date.now() + 600_000).multiply(1_000_000),  },};
  1. Use the signAndBroadcast method of SigningArchwayClient to sign and broadcast the transaction:
const broadcastResult = await signingClient.signAndBroadcast(  accountAddress,  [msgIBCTransfer],  'auto',  'IBC Transfer', // optional memo);
  1. Verify if the transaction was successful by checking the broadcastResult. This is the final lines of code for fucntion main:
if (broadcastResult.code !== undefined && broadcastResult.code !== 0) {  console.log("Transaction failed:", broadcastResult.log || broadcastResult.rawLog);} else {  console.log("Transaction successful:", broadcastResult.transactionHash);}
  1. Call the main function:
main();

Full script

mainnet

testnet

import { SigningArchwayClient } from '@archwayhq/arch3.js';import { DirectSecp256k1HdWallet } from "@cosmjs/proto-signing";import Long from "long";import dotenv from "dotenv";dotenv.config();async function main() {  const network = {    chainId: 'archway-1',    endpoint: 'https://rpc.mainnet.archway.io',    prefix: 'archway',  };  const mnemonic = process.env.MNEMONIC;  const wallet = await DirectSecp256k1HdWallet.fromMnemonic(mnemonic, { prefix: network.prefix });  const accounts = await wallet.getAccounts();  const accountAddress = accounts[0].address;  const destinationAddress = process.env.COSMOS_ADDRESS;    const signingClient = await SigningArchwayClient.connectWithSigner(network.endpoint, wallet);  const msgIBCTransfer = {    typeUrl: "/ibc.applications.transfer.v1.MsgTransfer",    value: {      sourcePort: 'transfer',      sourceChannel: 'channel-0', // channel of the bridge      token: {        denom: 'aarch',        amount: '1000000000000000000'      },      sender: accountAddress,      receiver: destinationAddress,      // Timeout is in nanoseconds, you can also just send Long.UZERO for default timeout      timeoutTimestamp: Long.fromNumber(Date.now() + 600_000).multiply(1_000_000),    },  };  const broadcastResult = await signingClient.signAndBroadcast(    accountAddress,    [msgIBCTransfer],    'auto',    'IBC Transfer', // optional memo  );    if (broadcastResult.code !== undefined && broadcastResult.code !== 0) {    console.log("Transaction failed:", broadcastResult.log || broadcastResult.rawLog);  } else {    console.log("Transaction successful:", broadcastResult.transactionHash);  }}main();

Execute script

Navigate to the root of your project folder in the terminal and run the following command:

node index.js

Clone and try it yourself

You can find a working example of this Node.js app in the dapp examples (IBC Transaction) repository.

Drop Camp is here!

Join the queue and be one of the first to get in!.

Go Camping ↗