0Pricing
Web3 & DApp Development Fundamentals · レッスン

トランザクションの送信

チェーンに書き込みます

「トランザクションの送信」はCoddyKit上の無料Web3 & DApp Development Fundamentalsレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはWeb3 & DApp Development Fundamentals学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Web3 & DApp Development Fundamentalsコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

Writing to the Chain

Changing contract state — transferring tokens, minting, voting — requires a transaction. Transactions must be signed and cost gas.

For this you need a signer, not just a provider.

A Contract with a Signer

To send transactions, create the contract with a signer or connect an existing instance to one:

const contract = new ethers.Contract( address, abi, signer ); // or const writable = readOnly.connect(signer);

Now state-changing functions become available.

const contract = new ethers.Contract(
  address,
  abi,
  signer
);
// or
const writable = readOnly.connect(signer);

Sending a Transaction

Calling a write function sends a transaction and returns a transaction response immediately — before it is mined:

const tx = await contract.transfer( recipient, ethers.parseUnits("10", 18) ); console.log("Sent:", tx.hash);

The hash identifies the pending transaction.

const tx = await contract.transfer(
  recipient,
  ethers.parseUnits("10", 18)
);
console.log("Sent:", tx.hash);

Waiting for Confirmation

The transaction is not final until it is mined. Wait for the receipt:

const receipt = await tx.wait(); console.log("Mined in block:", receipt.blockNumber); console.log("Status:", receipt.status);

A status of 1 means success; 0 means it reverted.

const receipt = await tx.wait();
console.log("Mined in block:", receipt.blockNumber);
console.log("Status:", receipt.status);

Waiting for More Blocks

For higher confidence against reorgs, wait for several confirmations:

// Wait for 3 confirmations const receipt = await tx.wait(3);

More confirmations mean it is increasingly unlikely the transaction will be reversed.

// Wait for 3 confirmations
const receipt = await tx.wait(3);

Sending ETH with a Call

Payable functions accept ETH. Pass a value override as the last argument:

const tx = await contract.deposit({ value: ethers.parseEther("0.5"), }); await tx.wait();

The value is in wei; parseEther converts from a human amount.

const tx = await contract.deposit({
  value: ethers.parseEther("0.5"),
});
await tx.wait();

Estimating Gas

Before sending, you can estimate how much gas a call will use:

const gas = await contract.transfer .estimateGas(recipient, amount); console.log("Estimated gas:", gas);

If the function would revert, estimation throws — a useful early warning.

const gas = await contract.transfer
  .estimateGas(recipient, amount);
console.log("Estimated gas:", gas);

Setting Gas Overrides

You can override gas parameters when needed, for example on EIP-1559 chains:

const tx = await contract.transfer(recipient, amount, { gasLimit: 100000, maxFeePerGas: ethers.parseUnits("30", "gwei"), maxPriorityFeePerGas: ethers.parseUnits("2", "gwei"), });

Usually the wallet picks sensible defaults, so override only when you must.

const tx = await contract.transfer(recipient, amount, {
  gasLimit: 100000,
  maxFeePerGas: ethers.parseUnits("30", "gwei"),
  maxPriorityFeePerGas: ethers.parseUnits("2", "gwei"),
});

Handling Failures

Transactions can fail by reverting, being underpriced, or by user rejection in a wallet. Wrap sends in try/catch:

try { const tx = await contract.transfer(to, amount); await tx.wait(); } catch (err) { console.error("Transaction failed:", err.shortMessage); }
try {
  const tx = await contract.transfer(to, amount);
  await tx.wait();
} catch (err) {
  console.error("Transaction failed:", err.shortMessage);
}

Nonces and Ordering

Each account has a nonce — a counter ensuring transactions execute in order. ethers manages it automatically, but if you send many transactions quickly you may set it manually to avoid collisions:

const tx = await contract.mint({ nonce: 42 });

Mismatched nonces cause transactions to get stuck or replaced.

const tx = await contract.mint({ nonce: 42 });

Reading Events from a Receipt

After mining, you can inspect the events emitted by your transaction from the receipt logs:

const receipt = await tx.wait(); for (const log of receipt.logs) { const parsed = contract.interface.parseLog(log); if (parsed) console.log(parsed.name, parsed.args); }

This confirms the contract did what you expected.

const receipt = await tx.wait();
for (const log of receipt.logs) {
  const parsed = contract.interface.parseLog(log);
  if (parsed) console.log(parsed.name, parsed.args);
}

Quick Check

Test your understanding of sending transactions.

Recap

You learned to send state-changing transactions.

  • Writes require a signer; attach one with connect or at construction.
  • A write call returns a pending tx; tx.wait() gives the mined receipt.
  • Check receipt.status; wait for more confirmations against reorgs.
  • Send ETH via a value override; estimate cost with estimateGas.
  • Handle failures with try/catch and mind the account nonce.

よくある質問

「トランザクションの送信」レッスンは無料ですか?

はい。「トランザクションの送信」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Web3 & DApp Development Fundamentalsコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Web3 & DApp Development Fundamentalsコースには全4レッスンが含まれています。

「トランザクションの送信」で何を学びますか?

チェーンに書き込みます ブラウザで直接実行するハンズオンコードでWeb3 & DApp Development Fundamentalsを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Web3 & DApp Development Fundamentalsを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのWeb3 & DApp Development Fundamentalsは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。

「トランザクションの送信」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このWeb3 & DApp Development Fundamentalsレッスンでコードを書いて実行できますか?

はい。すべてのWeb3 & DApp Development Fundamentalsレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. Providerへの接続
  2. コントラクトデータの読み取り
  3. トランザクションの送信
  4. イベントのリッスン
← Web3 & DApp Development Fundamentalsに戻る