ネットワークへの書き込み

前のセクションでは、Solanaネットワークからデータを読み取る方法を学びました。今度はネットワークにデータを書き込む方法を探ります。Solanaネットワークへの書き込みには、1つ以上のinstructionsを含むトランザクションの送信が含まれます。

プログラム(スマートコントラクト)は、それぞれのinstructionに対するビジネスロジックに従ってこれらのinstructionsを処理します。トランザクションを送信すると、Solanaランタイムは各instructionを順番に実行し、アトミックに処理します(つまり、すべてのinstructionsが成功するか、トランザクション全体が失敗するかのどちらかです)。

このセクションでは、2つの基本的な例を見ていきます:

  1. アカウント間でのSOLの転送
  2. 新しいトークンの作成

これらの例は、Solanaプログラムを呼び出すためのトランザクションの構築と送信方法を示しています。詳細については、トランザクションとInstructionsおよび Solanaの手数料のページを参照してください。

SOLの転送

この例では、2つのアカウント間でSOLを転送する方法を学びます。

Solanaでは、各アカウントには特定のプログラムが所有者として設定されています。プログラム所有者のみがアカウントのSOL(lamport)残高を減らすことができます。

System Programは、すべての「ウォレット」アカウントの 所有者 です。SOLを転送するには、System Programの transfer instructionを呼び出す必要があります。

Transfer SOL
import {
LAMPORTS_PER_SOL,
SystemProgram,
Transaction,
sendAndConfirmTransaction,
Keypair,
Connection
} from "@solana/web3.js";
const connection = new Connection("http://localhost:8899", "confirmed");
const sender = new Keypair();
const receiver = new Keypair();
const signature = await connection.requestAirdrop(
sender.publicKey,
LAMPORTS_PER_SOL
);
await connection.confirmTransaction(signature, "confirmed");
const transferInstruction = SystemProgram.transfer({
fromPubkey: sender.publicKey,
toPubkey: receiver.publicKey,
lamports: 0.01 * LAMPORTS_PER_SOL
});
const transaction = new Transaction().add(transferInstruction);
const transactionSignature = await sendAndConfirmTransaction(
connection,
transaction,
[sender]
);
console.log("Transaction Signature:", `${transactionSignature}`);
Console
Click to execute the code.

これらはSolana上の任意のプログラムと対話するためのトランザクションを構築するステップです。

呼び出したい命令を作成します。

Instruction
const transferInstruction = SystemProgram.transfer({
fromPubkey: sender.publicKey,
toPubkey: receiver.publicKey,
lamports: 0.01 * LAMPORTS_PER_SOL
});

命令をトランザクションに追加します:

Transaction
const transaction = new Transaction().add(transferInstruction);

トランザクションに署名して送信します:

Send Transaction
const transactionSignature = await sendAndConfirmTransaction(
connection,
transaction,
[sender] // signer keypair
);

トークンを作成する

この例では、Token Extensions Programを使用してSolana上に新しいトークンを作成する方法を学びます。これには2つの命令が必要です:

  1. System Programを呼び出して新しいアカウントを作成します。
  2. Token Extensions Programを呼び出して、そのアカウントをMintとして初期化します。
Create Mint Account
import {
Connection,
Keypair,
SystemProgram,
Transaction,
sendAndConfirmTransaction,
LAMPORTS_PER_SOL
} from "@solana/web3.js";
import {
MINT_SIZE,
TOKEN_2022_PROGRAM_ID,
createInitializeMint2Instruction,
getMinimumBalanceForRentExemptMint
} from "@solana/spl-token";
const connection = new Connection("http://localhost:8899", "confirmed");
const wallet = new Keypair();
// Fund the wallet with SOL
const signature = await connection.requestAirdrop(
wallet.publicKey,
LAMPORTS_PER_SOL
);
await connection.confirmTransaction(signature, "confirmed");
// Generate keypair to use as address of mint account
const mint = new Keypair();
// Calculate lamports required for rent exemption
const rentExemptionLamports =
await getMinimumBalanceForRentExemptMint(connection);
// Instruction to create new account with space for new mint account
const createAccountInstruction = SystemProgram.createAccount({
fromPubkey: wallet.publicKey,
newAccountPubkey: mint.publicKey,
space: MINT_SIZE,
lamports: rentExemptionLamports,
programId: TOKEN_2022_PROGRAM_ID
});
// Instruction to initialize mint account
const initializeMintInstruction = createInitializeMint2Instruction(
mint.publicKey,
2, // decimals
wallet.publicKey, // mint authority
wallet.publicKey, // freeze authority
TOKEN_2022_PROGRAM_ID
);
// Build transaction with instructions to create new account and initialize mint account
const transaction = new Transaction().add(
createAccountInstruction,
initializeMintInstruction
);
const transactionSignature = await sendAndConfirmTransaction(
connection,
transaction,
[
wallet, // payer
mint // mint address keypair
]
);
console.log("Mint Account:", `${mint.publicKey}`);
console.log("Transaction Signature:", `${transactionSignature}`);
Console
Click to execute the code.

以下は、この例が行っていることの段階的な説明です:

コネクションを作成し、ウォレットに資金を提供する

Connection and Wallet
const connection = new Connection("http://localhost:8899", "confirmed");
const wallet = new Keypair();
const signature = await connection.requestAirdrop(
wallet.publicKey,
LAMPORTS_PER_SOL
);
await connection.confirmTransaction(signature, "confirmed");

Mintアカウント用のkeypairを生成する

Mint Keypair
const mint = new Keypair();

rent免除に必要な最小lamportsを計算する

Rent Exemption
const rentExemptionLamports =
await getMinimumBalanceForRentExemptMint(connection);

新しいアカウントを作成するためのinstructionを作成する

  1. mint データを保存するために必要なスペースを確保する
  2. 新しいアカウントに資金を提供するために、ウォレットからlamportsを転送する
  3. アカウントの所有権をToken Extensionsプログラム(TOKEN_2022_PROGRAM_ID)に割り当てる
Create Account Instruction
const createAccountInstruction = SystemProgram.createAccount({
fromPubkey: wallet.publicKey,
newAccountPubkey: mint.publicKey,
space: MINT_SIZE,
lamports: rentExemptionLamports,
programId: TOKEN_2022_PROGRAM_ID
});

Mintアカウントを初期化するためのinstructionを作成する

Initialize Mint Instruction
const initializeMintInstruction = createInitializeMint2Instruction(
mint.publicKey,
2, // decimals
wallet.publicKey, // mint authority
wallet.publicKey, // freeze authority
TOKEN_2022_PROGRAM_ID
);

両方のinstructionsをトランザクションに追加する

Build Transaction
const transaction = new Transaction().add(
createAccountInstruction,
initializeMintInstruction
);

必要な署名者を含むトランザクションを送信して確認する

Send Transaction
const transactionSignature = await sendAndConfirmTransaction(
connection,
transaction,
[wallet, mint]
);

Mintアカウントとトランザクション署名を表示する

Output
console.log("Mint Account:", `${mint.publicKey}`);
console.log("Transaction Signature:", `${transactionSignature}`);

両方のinstructionsを1つのトランザクションに組み合わせることで、アカウントの作成と初期化が原子的に行われることを保証します。両方のinstructionsが成功するか、どちらも実行されないかのどちらかです。このアプローチは、より複雑なSolanaトランザクションを構築する際に一般的であり、すべてのinstructionsが一緒に実行されることを保証します。

Is this page helpful?

目次

ページを編集