anchor hello

cli
anchor init hello
anchor build
anchor deploy
rs
use anchor_lang::prelude::*;
declare_id!("CmZmxwSoQ6ZcLdwfRZURq2DLoYoHUADYJXAMiQuJNjFv");
#[program]
pub mod hello {
use super::*;
pub fn initialize(ctx: Context<Initialize>) -> Result<()> {
msg!("Greetings from: {:?}", ctx.program_id);
Ok(())
}
}
#[derive(Accounts)]
pub struct Initialize {}
js
const anchor = require('@coral-xyz/anchor');
const { PublicKey } = require('@solana/web3.js');
// Set up the provider
const provider = anchor.AnchorProvider.local();
anchor.setProvider(provider);
// Load the IDL
const idl = require('../target/idl/hello.json');
// Program ID (replace with your program ID)
const programId = new PublicKey('CmZmxwSoQ6ZcLdwfRZURq2DLoYoHUADYJXAMiQuJNjFv');
// Create the program interface
const program = new anchor.Program(idl, provider );
console.log('program',program)
// Function to call the set_data method
async function setData() {
try {
// Call the set_data method
const tx = await program.methods.initialize().rpc();
console.log("Your transaction signature", tx);
} catch (err) {
console.error('Error calling set_data:', err);
}
}
// Execute the function
setData();
本地命令