指令调用
指令调用
const anchor = require('@project-serum/anchor');
// 初始化Anchor客户端 const provider = anchor.Provider.local(); anchor.setProvider(provider);
// 加载程序 const programId = new anchor.web3.PublicKey("YourProgramIDHere..."); const program = new anchor.Program(idl, programId, provider);
// 调用initialize_counter async function initializeCounter(initialValue) { const counter = anchor.web3.Keypair.generate();
await program.rpc.initializeCounter(new anchor.BN(initialValue), { accounts: { counter: counter.publicKey, user: provider.wallet.publicKey, systemProgram: anchor.web3.SystemProgram.programId, }, signers: [counter], });
console.log("计数器已初始化"); }
// 调用increment_counter async function incrementCounter(counterPublicKey) { await program.rpc.incrementCounter({ accounts: { counter: counterPublicKey, }, });
console.log("计数器已递增"); }