メインコンテンツへスキップ

Documentation Index

Fetch the complete documentation index at: https://injectivelabs-mintlify-jp-native-developers-first-half.mintlify.app/llms.txt

Use this file to discover all available pages before exploring further.

メッセージ

MsgTransfer

このメッセージは、IBC(Cosmos’s Inter-Blockchain Communication Protocol)を介して、送信者のInjective上のBankモジュールから別のCosmosチェーン上の受信者のBankモジュールへコインを送信するために使用されます。 なお、Injectiveはほとんどのネットワークについてメインネット間のIBC転送のみをサポートしていることに注意してください。 IBCにおけるアプリケーション間通信はchannel上で行われます。channelはあるチェーン上のアプリケーションモジュールと別のチェーン上の対応するアプリケーションモジュールの間をルーティングします。IBC channelの詳細は https://tutorials.cosmos.network/academy/3-ibc/3-channels.html を参照してください。 Injectiveとの間のメインネット転送用のcanonicalなchannel IDの一覧はInjective Listsリポジトリにあります。 また、各チェーン上のアプリケーションモジュールは、各端のモジュールタイプを示すportIdを持ちます。例えば、transferはbankモジュール間のICS-20トークンの転送を示すportIdです。 この例では、InjectiveからCosmosHubへATOMを転送します。
トークンメタデータ(シンボル、decimals、channelマッピング)については、最新のトークン情報をJSON形式で提供している Injective Listsを使用してください。
import { toChainFormat, toBigNumber } from "@injectivelabs/utils";
import { ChainId, CosmosChainId } from "@injectivelabs/ts-types";
import { MsgTransfer } from "@injectivelabs/sdk-ts/core/modules";
import { getNetworkEndpoints, Network } from "@injectivelabs/networks";
import { ChainGrpcBankApi } from "@injectivelabs/sdk-ts/client/chain";
import { MsgBroadcasterWithPk } from "@injectivelabs/sdk-ts/core/tx";
import { makeTimeoutTimestampInNs } from "@injectivelabs/sdk-ts/utils";
import { ChainRestTendermintApi } from "@injectivelabs/sdk-ts/client/chain";

const injectiveChainId = CosmosChainId["Injective"];
const destinationChainId = CosmosChainId["Cosmoshub"];

const endpointsForNetwork = getNetworkEndpoints(Network.Mainnet);

/**
 * For IBC transfers, you need:
 * 1. The IBC denom hash for the token on Injective
 * 2. The channel ID for the destination chain
 *
 * You can find this information in the Injective Lists repository:
 * https://github.com/InjectiveLabs/injective-lists
 */
const injectiveToCosmosHubChannelId = "channel-1";

/**
 * The IBC denom for ATOM on Injective
 * Format: ibc/{hash} where hash is derived from the channel and base denom
 */
const atomIbcDenom =
  "ibc/C4CFF46FD6DE35CA4CF4CE031E643C8FDC9BA4B99AE598E9B0ED98FE3A2319F9";

/* format amount for transfer (0.001 ATOM with 6 decimals) */
const amount = {
  denom: atomIbcDenom,
  amount: toChainFormat(0.001, 6).toFixed(),
};

const injectiveAddress = "inj...";
const destinationAddress = "cosmos...";
const port = "transfer";
const timeoutTimestamp = makeTimeoutTimestampInNs();

/* get the latestBlock from the origin chain */
const tendermintRestApi = new ChainRestTendermintApi(endpointsForNetwork.rest);

/* Block details from the origin chain */
const latestBlock = await tendermintRestApi.fetchLatestBlock();
const latestHeight = latestBlock.header.height;
const timeoutHeight = toBigNumber(latestHeight).plus(
  30 // default block timeout height
);

/* create message in proto format */
const msg = MsgTransfer.fromJSON({
  port,
  memo: `IBC transfer from ${injectiveChainId} to ${destinationChainId}`,
  sender: injectiveAddress,
  receiver: destinationAddress,
  channelId: injectiveToCosmosHubChannelId,
  timeout: timeoutTimestamp,
  height: {
    revisionHeight: timeoutHeight.toNumber(),
    revisionNumber: parseInt(latestBlock.header.version.block, 10),
  },
  amount,
});

const privateKey = "0x...";

/* broadcast transaction */
const txHash = await new MsgBroadcasterWithPk({
  privateKey,
  network: Network.Mainnet,
}).broadcast({
  msgs: msg,
});

console.log(txHash);
Last modified on May 14, 2026