Solana 中原生代币的交互

  • nilliol
  • 更新于 2024-08-09 13:48
  • 阅读 270

Solana中原生代币的交互即为对Solana的代币Sol代币的代币进行交易,实际上就是对于Sol代币进行查看,转账这两个操作。JS交互以下为通过JS代码,查看某个特定账户的余额和从某个账户(持有该账户的私钥)向另一个账户(只需要直到公钥地址)进行转账。//导入相关模块constw

Solana中原生代币的交互即为对Solana的代币Sol代币的代币进行交易,实际上就是对于Sol代币进行查看,转账这两个操作。

JS 交互

以下为通过 JS 代码,查看某个特定账户的余额和从某个账户(持有该账户的私钥)向另一个账户(只需要直到公钥地址)进行转账。

// 导入相关模块
const web3 = require("@solana/web3.js");

// 添加一个与 Solana 的连接
// 这里采用的是库中内置的开发者网的节点地址 web3.clusterApiUrl('devnet')
const connection = new web3.Connection(web3.clusterApiUrl("devnet"), "confirmed");

function localwalletkey(keypairFile) {
    // 使用 fs(file system)模块,访问本地文件系统
    const fs = require("fs");
    // 生成一个私钥钱包对象
    const loaded = web3.Keypair.fromSecretKey(
        new Uint8Array(JSON.parse(fs.readFileSync(keypairFile).toString()))
    );
    // 返回这个私钥钱包对象
    return loaded;
}

// 采用异步的方法查看账户余额
async function catBalance() {
    // 当已知为一个公钥地址时
    // mypublickey = new web3.PublicKey("$public_key_addr")
    // 当已知为自己的私钥地址(存在一个文件保存私钥)时
    const pairkey = localwalletkey("$key_path");
    // 获得私钥对应的公钥地址
    const mypublickey = pairkey.publicKey;
    const balance = await connection.getBalance(mypublickey);
    console.log("账户余额为:", balance);
}

// 采用异步的方法执行转账
async function transfersol() {
    // 转账者,发送 sol 代币
    // const pairkey = localwalletkey("$key_path");
    const sender = localwalletkey("wallet-keypair.json");
    // 接收者,接收 sol 代币
    const recipient = new web3.PublicKey("$another_publikey");

    // 生成一个调用系统转账程序的指令(可以看作一笔交易)
    let tx = web3.SystemProgram.transfer({
        // 转出 sol 的公钥地址
        fromPubkey: sender.publicKey,
        // 接收 sol 的公钥地址
        toPubkey: recipient,
        // 转账的 sol 额度(以 lamports 为单位,这个是 sol 最小单位)
        lamports: web3.LAMPORTS_PER_SOL / 100,
    });

    // 将指令放入交易(相当于指令集,可以包含多个指令)中
    let transcation = new web3.Transaction().add(tx);

    // 对交易签名并发送,交易费用由 [sender] 支付
    let signature = await web3.sendAndConfirmTransaction(connection, transcation, [sender]);
    console.log("交易签名:", signature);
}

async function main() {
    await catBalance();

    await transfersol();

    await catBalance();
}

main();

Python 交互

通过pythonseahorse框架进行转账(假设该程序需要对用户收取sol

# 导入 seahorse 的库
# 使用 seahorse 需要使用标准的命名法
from seahorse.prelude import *

# 在编译后自然会产生,然后替换掉里面的字符串
declare_id ('11111111111111111111111111111111')

# 类的命名需要驼峰命名法(单词首字母大写,如:FizzBuzz)
# 程序拥有的 PDA 账户,用于存储从用户那里收取到的 sol 代币
class Pda(Account):
    # 存储将来取出 PDA 存储的 sol 的用户地址
    owner: Pubkey

# 方法使用蛇形命名法(单词全部小写,使用_连接各个单词)
# 每个方法都要使用 @instruction 修饰
# 初始化生成一个程序控制的 PDA 账户
@instruction
def init_program_pda(owner:Signer, pad:Empty[Pda]):
    # 通过自定义的 seed 和程序 id 生成一个只能由该程序控制的 PDA 账户
    # 该账户中的数据结构由前面的类决定
    # 生成账户的费用由 payer(即 owner)出
    pda = pda.init(payer=owner,seed=['pda'])
    pda.owner = owner.key()

@instruction
def transfer_sol(Sender:Signer,pda:Pda,amount:u64):{
    # Sender 扣除指定数量的 sol (以 lamport 为单位,1 sol = 10^9 lamport)给到 pda 中
    Sender.transfer_lamports(pda,amount)
}

@instruction
def withdrawals(recipter:Signer,pda:Pda,amount:u64):
    # 要求指定用户才能取出
    assert recipter.key() == pda.owner,"Caller is not the current admin"
    pda.transfer_lamports(recipter,amount)
  • 原创
  • 学分: 8
  • 分类: Solana
  • 标签:
点赞 0
收藏 0
分享
本文参与登链社区写作激励计划 ,好文好收益,欢迎正在阅读的你也加入。

0 条评论

请先 登录 后评论
nilliol
nilliol
0xbe3e...29A9
web3 的学习者,寻找实习机会中。 博客地址:https://llwh2333.github.io