本文介绍 Solana Anchor 程序如何通过交易转移 SOL。
本文介绍 Solana Anchor 程序如何通过交易转移 SOL。不同于 Solidity 的 msg.value 将 ETH “推送”至合约,Solana 程序通过跨程序调用(CPI)从签名者账户“拉取” SOL。因此,Solana 无 payable 函数和 msg.value 类似的内容。
我们将创建 sol_splitter 项目,展示单账户转账及多账户分割的实现。
以下代码将 SOL 从签名者转移至单一接收者:
use anchor_lang::prelude::*;
use anchor_lang::system_program;
declare_id!("DoDAytTn6aFcUjS3hxLGs5CgaHyMvwGaZeQnLNA6bpiY");
#[program]
pub mod sol_splitter {
use super::*;
pub fn send_sol(ctx: Context<SendSol>, amount: u64) -> Result<()> {
let cpi_context = CpiContext::new(
ctx.accounts.system_program.to_account_info(),
system_program::Transfer {
from: ctx.accounts.signer.to_account_info(),
to: ctx.accounts.recipient.to_account_info(),
}
);
let res = system_program::transfer(cpi_context, amount);
if res.is_ok() {
return Ok(());
} else {
return err!(Errors::TransferFailed);
}
}
}
#[error_code]
pub enum Errors {
#[msg("transfer failed")]
TransferFailed,
}
#[derive(Accounts)]
pub struct SendSol<'info> {
/// CHECK: we do not read or write the data of this account
#[account(mut)]
recipient: UncheckedAccount<'info>,
system_program: Program<'info, System>,
#[account(mut)]
signer: Signer<'info>,
}
import * as anchor from "@coral-xyz/anchor";
import { Program } from "@coral-xyz/anchor";
import { SolSplitter } from "../target/types/sol_splitter";
describe("sol_splitter", () => {
// Configure the client to use the local cluster.
anchor.setProvider(anchor.AnchorProvider.env());
const program = anchor.workspace.SolSplitter as Program<SolSplitter>;
async function printAccountBalance(account) {
const balance = await anchor.getProvider().connection.getBalance(account);
console.log(`${account} has ${balance / anchor.web3.LAMPORTS_PER_SOL} SOL`);
}
it("Transmit SOL", async () => {
// generate a new wallet
const recipient = anchor.web3.Keypair.generate();
await printAccountBalance(recipient.publicKey);
// send the account 1 SOL via the program
let amount = new anchor.BN(1 * anchor.web3.LAMPORTS_PER_SOL);
await program.methods.sendSol(amount)
.accounts({recipient: recipient.publicKey})
.rpc();
await printAccountBalance(recipient.publicKey);
});
});
输出:
sol_splitter
4e9F7AWMNwyMPZCycs2NU8MLRY6xHa6QbkQ2z33sqLq7 has 0 SOL
4e9F7AWMNwyMPZCycs2NU8MLRY6xHa6QbkQ2z33sqLq7 has 1 SOL
✔ Transmit SOL (415ms)
以下代码将 SOL 均分至多个接收者,使用 remaining_accounts 处理动态账户列表:
use anchor_lang::prelude::*;
use anchor_lang::system_program;
declare_id!("DoDAytTn6aFcUjS3hxLGs5CgaHyMvwGaZeQnLNA6bpiY");
#[program]
pub mod sol_splitter {
use super::*;
// 'a, 'b, 'c are Rust lifetim...
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!