Skip to content

Latest commit

History

History
84 lines (62 loc) 路 3.03 KB

ibc.mdx

File metadata and controls

84 lines (62 loc) 路 3.03 KB

IBC Transfers

Terra has full IBC transfer capability through both Feather.js and Station. Although IBC functionality is not readily exposed through Station鈥檚 front end, it can be incorporated into any dApp. It is up to a dApp鈥檚 front end to initiate IBC transfers.

MsgTransfer

Feather.js exports a MsgTransfer class that can be used to construct IBC transfers.

new MsgTransfer(
  'transfer', // IBC port
  'channel-1', // Outbound channel (Osmosis)
  new Coin('uluna', '1000000'), // 1 LUNA
  'terra1cvw8sundusurqajhurpcfk7yvuzlh92cvkpy28', // Source Address on Terra
  'osmo1cl4qw7u35uf77l4scjtv0qej8ycevu4mrdpvmg', // Destination address on Osmosis
  undefined, // Timeout block height (optional)
  (Date.now() + 60 * 1000) * 1e6, // Timeout timestamp (in nanoseconds) relative to the current block timestamp.
  'field can be used to send notes, interact with ibc-hooks, packet-middleware, etc...', // Memo field
);

Supported Channels

Channels are defined when a relayer is set up between Terra and an external chain. For each new connected chain, the channel ID is incremented.

You can use the Map of Zones to find the available channels and their IDs.

Derive Cosmos chain addresses from a Terra address

Cosmos SDK-based blockchains use bech32 to encode the public key for display. Assuming the same private key is used on multiple Cosmos SDK chains, it is possible to decode a Terra address and generate the corresponding public key on another chain.

Here's a quick example using the bech32 JavaScript library:

import { bech32 } from 'bech32';

const terraAddress = 'terra1cvw8sundusurqajhurpcfk7yvuzlh92cvkpy28';
const decodedAddress = bech32.decode(terraAddress);
const osmosisAddress = bech32.encode('osmo', decodedAddress.words);

Complete example

The following example demonstrates how to send 1 LUNA from Terra to the Osmosis blockchain.

import {
  LCDClient,
  MnemonicKey,
  MsgTransfer,
  Coin,
} from '@terra-money/feather.js';

// const lcd = new LCDClient(...);

const mk = new MnemonicKey({
  mnemonic:
    'satisfy adjust timber high purchase tuition stool faith fine install that you unaware feed domain license impose boss human eager hat rent enjoy dawn',
});

const wallet = lcd.wallet(mk);

// Send 1 LUNA to the Osmosis blockchain.
const transfer = new MsgTransfer(
  'transfer',
  'channel-1',
  new Coin('uluna', '1000000'),
  'terra1cvw8sundusurqajhurpcfk7yvuzlh92cvkpy28',
  'osmo1cl4qw7u35uf77l4scjtv0qej8ycevu4mrdpvmg',
  undefined,
  (Date.now() + 60 * 1000) * 1e6,
  'hello world',
);

const tx = await wallet.createAndSignTx({
  msgs: [transfer],
  chainID: 'pisco-1',
});
const result = await lcd.tx.broadcast(tx, 'pisco-1');

console.log(result);

Instructions for initializing LCDClient can be found in the common examples section.