playground Interact program
链接
https://beta.solpg.io/656a0ea7fb53fa325bfd0c3e
deploy Program


Intro to Solana development (using only your browser)
program代码
use solana_program::{
account_info::AccountInfo, entrypoint, entrypoint::ProgramResult, msg, pubkey::Pubkey,
};
// declare and export the program's entrypoint
entrypoint!(process_instruction);
// program entrypoint's implementation
pub fn process_instruction(
program_id: &Pubkey,
accounts: &[AccountInfo],
instruction_data: &[u8],
) -> ProgramResult {
// log a message to the blockchain
msg!("Hello, world!");
// gracefully exit the program
Ok(())
}

Interact 代码
import {
LAMPORTS_PER_SOL,
SystemProgram,
Transaction,
sendAndConfirmTransaction,
Keypair,
} from "@solana/web3.js";
// Use Playground cluster connection
const connection = pg.connection;
// Use Playground wallet as sender, generate random keypair as receiver
const sender = pg.wallet.keypair;
// Check and log balance before transfer
const preBalance1 = await connection.getBalance(sender.publicKey);
console.log("sender prebalance:", preBalance1 / LAMPORTS_PER_SOL);
console.log("\n");
// Create a transfer instruction for transferring SOL from wallet_1 to wallet_2
const transferInstruction = new web3.TransactionInstruction({
keys: [],
programId: new web3.PublicKey(pg.PROGRAM_ID),
});
// Add the transfer instruction to a new transaction
const transaction = new Transaction().add(transferInstruction);
// Send the transaction to the network
const transactionSignature = await sendAndConfirmTransaction(
connection,
transaction,
[sender] // signer
);
// Check and log balance after transfer
const postBalance1 = await connection.getBalance(sender.publicKey);
console.log("sender postbalance:", postBalance1 / LAMPORTS_PER_SOL);
console.log("\n");
console.log(
"Transaction Signature:",
`https://explorer.solana.com/tx/${transactionSignature}?cluster=devnet`
);

链上
