Schrijven naar het netwerk

In de vorige sectie heb je geleerd hoe je gegevens van het Solana-netwerk kunt lezen. Nu gaan we verkennen hoe je gegevens naar het netwerk kunt schrijven. Schrijven naar het Solana-netwerk houdt in dat je transacties verstuurt die één of meer instructies bevatten.

Programma's (smart contracts) verwerken deze instructies volgens hun bedrijfslogica voor elke respectievelijke instructie. Wanneer je een transactie indient, voert de Solana-runtime elke instructie opeenvolgend en atomisch uit (wat betekent dat ofwel alle instructies slagen, ofwel de hele transactie mislukt).

In deze sectie zie je twee basisvoorbeelden:

  1. SOL overmaken tussen accounts
  2. Een nieuw token aanmaken

Deze voorbeelden laten zien hoe je transacties kunt bouwen en verzenden om Solana- programma's aan te roepen. Voor meer details, raadpleeg de Transacties en instructies en Kosten op Solana pagina's.

SOL overmaken

In dit voorbeeld leer je hoe je SOL tussen twee accounts kunt overmaken.

Op Solana heeft elk account een specifiek programma als eigenaar. Alleen de programma- eigenaar kan het SOL (lamport) saldo van een account verminderen.

Het System Program is de eigenaar van alle "wallet"-accounts. Om SOL over te maken, moet je de transfer instructie van het System Program aanroepen.

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}`);
Click to execute the code.

Dit zijn de stappen voor het bouwen van transacties om met elk programma op Solana te interacteren.

Maak de instructie die je wilt aanroepen.

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

Voeg de instructie toe aan een transactie:

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

Onderteken en verstuur de transactie:

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

Een token maken

In dit voorbeeld leer je hoe je een nieuw token op Solana kunt maken met behulp van het Token Extensions Program. Dit vereist twee instructies:

  1. Roep het System Program aan om een nieuw account te maken.
  2. Roep het Token Extensions Program aan om dat account te initialiseren als een 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}`);
Click to execute the code.

Hier is een stapsgewijze uitleg van wat het voorbeeld doet:

Maak een verbinding en financier de wallet

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");

Genereer een keypair voor de Mint account

Mint Keypair
const mint = new Keypair();

Bereken de minimale lamports voor rent exemption

Rent Exemption
const rentExemptionLamports =
await getMinimumBalanceForRentExemptMint(connection);

Maak een instructie om een nieuw account aan te maken

  1. Reserveer de benodigde ruimte voor het opslaan van mint-gegevens
  2. Draag lamports over van de wallet om het nieuwe account te financieren
  3. Wijs eigendom toe van het account aan het Token Extensions programma (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
});

Maak een instructie om de Mint account te initialiseren

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

Voeg beide instructies toe aan een transactie

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

Verstuur en bevestig de transactie met beide vereiste ondertekenaars

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

Print de Mint account en transactiehandtekening

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

Door beide instructies in één transactie te combineren, zorg je ervoor dat het aanmaken en initialiseren van het account atomair gebeurt. Ofwel beide instructies slagen, ofwel geen van beide. Deze aanpak is gebruikelijk bij het bouwen van complexere Solana-transacties, omdat het garandeert dat alle instructies samen worden uitgevoerd.

Is this page helpful?

Inhoudsopgave

Pagina Bewerken