> ## Documentation Index
> Fetch the complete documentation index at: https://injectivelabs-docs-ai-sdk.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# MsgBroadcasterトランザクション

`MsgBroadcaster`抽象化クラスは、Injective上でトランザクションを簡単にブロードキャストする方法です。これを使用すると、トランザクションにパックしたいMessageと署名者のアドレスを渡すだけで、トランザクションを準備、署名、ブロードキャストできます。

使用例は当方の[Helixデモリポジトリ](https://github.com/InjectiveLabs/injective-helix-demo)を参照してください。`broadcast`メソッドに渡せるメッセージについては、ドキュメントの[Core Modules](../examples/)セクションで例を確認できます。

## MsgBroadcaster + Wallet Strategy

このMsgBroadcasterは、Wallet Strategyクラスと併用して分散型アプリケーションを構築するために使用されます。

`MsgBroadcaster`クラスをインスタンス化（および使用）するには、次のコードスニペットを使用できます。

```ts theme={null}
import { toChainFormat } from "@injectivelabs/utils";
import { ChainId, EvmChainId } from "@injectivelabs/ts-types";
import { MsgSend } from "@injectivelabs/sdk-ts/core/modules";
import { MsgBroadcaster } from "@injectivelabs/wallet-core";
import { WalletStrategy } from "@injectivelabs/wallet-strategy";
import { Network, getNetworkEndpoints } from "@injectivelabs/networks";

export const evmRpcEndpoint = "";
export const walletStrategy = new WalletStrategy({
  chainId: ChainId.Mainnet,
  evmOptions: {
    rpcUrl: evmRpcEndpoint,
    evmChainId: EvmChainId.Mainnet,
  },
  strategies: {},
});

export const msgBroadcaster = new MsgBroadcaster({
  walletStrategy,
  simulateTx: true,
  network: Network.Mainnet,
  endpoints: getNetworkEndpoints(Network.Mainnet),
  gasBufferCoefficient: 1.1,
});

// Usage Example
const signer = "inj1...";

const msg = MsgSend.fromJSON({
  amount: {
    denom: "inj",
    amount: toChainFormat(0.01, 18).toFixed(),
  },
  srcInjectiveAddress: signer,
  dstInjectiveAddress: "inj1...",
});

// Prepare + Sign + Broadcast the transaction using the Wallet Strategy
await msgBroadcaster.broadcast({
  injectiveAddress: signer,
  msgs: msg,
});
```

### Constructor/Broadcastオプション

`MsgBroadcaster`のコンストラクタおよびトランザクションのブロードキャスト時に渡される一部のオプションをオーバーライドできます。各フィールドのインターフェースと意味は次のとおりです。

```typescript theme={null}
import { Msgs } from '@injectivelabs/sdk-ts/core/modules'
import { ChainId, EvmChainId } from '@injectivelabs/ts-types'
import { Network, NetworkEndpoints } from '@injectivelabs/networks'
import type { WalletStrategy } from '../strategies'

export interface MsgBroadcasterOptions {
  network: Network /** network configuration (chainId, fees, etc) - Network.MainnetSentry for mainnet or  Network.TestnetSentry for testnet */
  endpoints?: NetworkEndpoints /** optional - overriding the endpoints taken from the `network` param **/
  feePayerPubKey?: string /** optional - if you are using the fee delegation service, you can set the fee payer so you don't do an extra query to the Web3Gateway */
  simulateTx?: boolean /** simulate the transaction before broadcasting + get gas fees needed for the transaction */
  txTimeout?: number /** optional - blocks to wait for tx to be included in a block **/
  walletStrategy: WalletStrategy
  gasBufferCoefficient?: number /** optional - as gas buffer to add to the simulated/hardcoded gas to ensure the transaction is included in a block */
}

export interface MsgBroadcasterTxOptions {
  memo?: string /** MEMO added to the transaction **/
  injectiveAddress: string /** the signer of the transaction **/
  msgs: Msgs | Msgs[] /** the messages to pack into a transaction **/

  /*
  *** overriding the hardcoded gas/simulation -
  *** depending on the simulateTx parameter in
  *** the MsgBroadcaster constructor
  */
  gas?: {
    gasPrice?: string
    gas?: number /** gas limit */
    feePayer?: string
    granter?: string
  }
}

```

<Callout icon="info" color="#07C1FF" iconType="regular">
  `endpoints`をオーバーライドし、自身のインフラストラクチャを使用するには（推奨）、
  [Networks](../../developers/concepts/networks/)ページで、提供すべきエンドポイントと
  そのセットアップ方法について詳しく読んでください。
</Callout>

## MsgBroadcaster with Private Key

このMsgBroadcasterは秘密鍵と併用されます（主にCLI環境で使用されます）。Constructor/broadcastのオプションは`MsgBroadcaster`とほぼ同様です。

```ts theme={null}
import { toChainFormat } from "@injectivelabs/utils";
import { MsgSend } from "@injectivelabs/sdk-ts/core/modules";
import { MsgBroadcasterWithPk } from "@injectivelabs/sdk-ts/core/tx";

export const msgBroadcasterWithPk = new MsgBroadcasterWithPk({
  privateKey: `0x...` /** private key hash or PrivateKey class from sdk-ts */,
  network: NETWORK,
});

// Usage Example
const signer = "inj1...";

const msg = MsgSend.fromJSON({
  amount: {
    denom: "inj",
    amount: toChainFormat(0.01, 18).toFixed(),
  },
  srcInjectiveAddress: signer,
  dstInjectiveAddress: "inj1...",
});

// Prepare + Sign + Broadcast the transaction using the Private Key
await msgBroadcasterWithPk.broadcast({
  injectiveAddress: signer,
  msgs: msg,
});
```
