【问题标题】:Solana how to stake native SOL to a Rust program and withdraw SOL from a program?Solana 如何将原生 SOL 质押到 Rust 程序并从程序中撤回 SOL?
【发布时间】:2021-10-18 15:47:25
【问题描述】:

我在 Google 上搜索过,只发现如何通过命令行发送令牌:

https://spl.solana.com/token

但我需要在 Rust 程序中/从 Rust 程序中质押/撤回 Native SOL,而不是代币。

我还发现了这个关于 Rust 原生令牌的信息:

https://docs.rs/solana-program/1.7.10/solana_program/native_token/index.html

https://docs.rs/solana-program/1.7.10/solana_program/native_token/struct.Sol.html

看来我必须声明这个 Sol 结构

pub struct Sol(pub u64);

然后使用它的“发送”特性发送它......对吗? 有人可以解释如何通过 Rust 程序做到这一点吗?

【问题讨论】:

    标签: rust solana


    【解决方案1】:

    您可以通过 SPL Token 程序将 Sol 包装成一个包装好的 sol Token。从那里您可以像使用任何 SPL 令牌一样使用 Wrapped SOL。这是一个很好的例子

    https://github.com/project-serum/serum-dex-ui/blob/5b4487634e3738c57ace6da1377704e95f53c588/src/pages/pools/PoolPage/PoolAdminPanel.tsx#L250-L272

    const wrappedSolAccount = new Account();
    
    transaction.add(
      SystemProgram.createAccount({
        fromPubkey: wallet.publicKey,
        lamports: parsedQuantity + 2.04e6,
        newAccountPubkey: wrappedSolAccount.publicKey,
        programId: TokenInstructions.TOKEN_PROGRAM_ID,
        space: 165,
      }),
      TokenInstructions.initializeAccount({
        account: wrappedSolAccount.publicKey,
        mint: TokenInstructions.WRAPPED_SOL_MINT,
        owner: wallet.publicKey,
      }),
      TokenInstructions.transfer({
        source: wrappedSolAccount.publicKey,
        destination: vaultAddress,
        amount: parsedQuantity,
        owner: wallet.publicKey,
      }),
      TokenInstructions.closeAccount({
        source: wrappedSolAccount.publicKey,
        destination: walletTokenAccount.pubkey,
        owner: wallet.publicKey,
      }),
    );
    

    所以本质上它在这里所做的是

    1. 创建一个随机帐户,您可以在其中存入一些 SOL,并存入所需金额加上租金金额(因此 parsedQuantity + 2.04e6)
    2. 使用令牌程序将帐户初始化为 WRAPPED_SOL_ACCOUNT
    3. 传输打包的 SOL(或者在您的情况下,您可能只想调用程序并将打包的 SOL 帐户作为参数)
    4. 关闭 Wrapped SOL 帐户,以便用户取回租金

    【讨论】:

    • 是的,我看到了,但不确定这是否是正确的方法……相当复杂,但我想这是唯一的方法。谢谢!
    • 嗨 yangli-io,你对修改 Solana 的 SPL 代币程序的工作感兴趣吗,所以每笔交易都会从发送者那里扣除费用?
    • @Russo,我很荣幸 :D 但很抱歉现在超级忙
    • 嗨 yangli-io,所有包装好的 SOL 代币都具有相同的代币地址 TokenInstructions.WRAPPED_SOL_MINT,对吗?因此,我可以检查令牌地址是否与 TokenInstructions.WRAPPED_SOL_MINT 相同,该令牌必须是包装的 SOL,对吗?
    • 在包装新令牌后,我有 So11111111111111111111111111111111111111112 作为令牌铸币帐户。所以那一定和 TokenInstructions.WRAPPED_SOL_MINT 一样吧?
    猜你喜欢
    • 1970-01-01
    • 2021-08-07
    • 1970-01-01
    • 2022-01-06
    • 2021-09-10
    • 2022-01-25
    • 2021-12-01
    • 2022-07-14
    • 2021-09-27
    相关资源
    最近更新 更多