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:
- SOL overmaken tussen accounts
- 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.
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}`);
Dit zijn de stappen voor het bouwen van transacties om met elk programma op Solana te interacteren.
Maak de instructie die je wilt aanroepen.
const transferInstruction = SystemProgram.transfer({fromPubkey: sender.publicKey,toPubkey: receiver.publicKey,lamports: 0.01 * LAMPORTS_PER_SOL});
Voeg de instructie toe aan een transactie:
const transaction = new Transaction().add(transferInstruction);
Onderteken en verstuur de transactie:
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:
- Roep het System Program aan om een nieuw account te maken.
- Roep het Token Extensions Program aan om dat account te initialiseren als een Mint.
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 SOLconst signature = await connection.requestAirdrop(wallet.publicKey,LAMPORTS_PER_SOL);await connection.confirmTransaction(signature, "confirmed");// Generate keypair to use as address of mint accountconst mint = new Keypair();// Calculate lamports required for rent exemptionconst rentExemptionLamports =await getMinimumBalanceForRentExemptMint(connection);// Instruction to create new account with space for new mint accountconst createAccountInstruction = SystemProgram.createAccount({fromPubkey: wallet.publicKey,newAccountPubkey: mint.publicKey,space: MINT_SIZE,lamports: rentExemptionLamports,programId: TOKEN_2022_PROGRAM_ID});// Instruction to initialize mint accountconst initializeMintInstruction = createInitializeMint2Instruction(mint.publicKey,2, // decimalswallet.publicKey, // mint authoritywallet.publicKey, // freeze authorityTOKEN_2022_PROGRAM_ID);// Build transaction with instructions to create new account and initialize mint accountconst transaction = new Transaction().add(createAccountInstruction,initializeMintInstruction);const transactionSignature = await sendAndConfirmTransaction(connection,transaction,[wallet, // payermint // mint address keypair]);console.log("Mint Account:", `${mint.publicKey}`);console.log("Transaction Signature:", `${transactionSignature}`);
Hier is een stapsgewijze uitleg van wat het voorbeeld doet:
Maak een verbinding en financier de 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
const mint = new Keypair();
Bereken de minimale lamports voor rent exemption
const rentExemptionLamports =await getMinimumBalanceForRentExemptMint(connection);
Maak een instructie om een nieuw account aan te maken
- Reserveer de benodigde ruimte voor het opslaan van mint-gegevens
- Draag lamports over van de wallet om het nieuwe account te financieren
- Wijs eigendom toe van het account aan het Token Extensions
programma (
TOKEN_2022_PROGRAM_ID
)
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
const initializeMintInstruction = createInitializeMint2Instruction(mint.publicKey,2, // decimalswallet.publicKey, // mint authoritywallet.publicKey, // freeze authorityTOKEN_2022_PROGRAM_ID);
Voeg beide instructies toe aan een transactie
const transaction = new Transaction().add(createAccountInstruction,initializeMintInstruction);
Verstuur en bevestig de transactie met beide vereiste ondertekenaars
const transactionSignature = await sendAndConfirmTransaction(connection,transaction,[wallet, mint]);
Print de Mint account en transactiehandtekening
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?