Skip to main content

playground Interact program

链接

https://beta.solpg.io/656a0ea7fb53fa325bfd0c3e

https://solana.com/developers/guides/getstarted/hello-world-in-your-browser#import-our-example-project

deploy Program

https://explorer.solana.com/tx/odNnUESdTRdmPbuVZfmq1cbxmymyT2RDf1EW1jXh9mEEC4F5m4to3qtXhLFnc2BmajU9H21xwXFuCUWe5JudnfK?cluster=devnet

alt text

alt text

Intro to Solana development (using only your browser)

https://explorer.solana.com/tx/2A2rg66sFkphkcukpV2tuWzBJjhDtDrv1BpAJrgKAowLfTgow2ZDhhkYuo97CrmLbvSkm1KLA3Zho4AwEyAVye1m?cluster=devnet

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(())
}

alt text

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

alt text

链上

https://explorer.solana.com/tx/2A2rg66sFkphkcukpV2tuWzBJjhDtDrv1BpAJrgKAowLfTgow2ZDhhkYuo97CrmLbvSkm1KLA3Zho4AwEyAVye1m?cluster=devnet

alt text