You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过已连接钱包的客户端正确调用Solana程序方法?

Solana钱包适配器调用程序方法的正确方式
  • 无需手动传入signers数组
    使用solana-wallet-adapter连接钱包后,不需要自行构造signers数组(也无需获取私钥)。直接移除.signers([wallet])部分,代码可修改为:

    const tx = await program.methods.list(price, expiryTime)
      .accounts(dataToSend)
      .rpc()
    

    只要你的program是通过钱包适配器的provider初始化的,调用.rpc()时会自动触发钱包的签名弹窗,完成交易签名。

  • 手动处理签名的场景(如需)
    如果需要手动控制交易签名流程(比如多签名场景),可以借助钱包的signTransaction方法,全程无需私钥:

    // 先构建交易对象
    const transaction = await program.methods.list(price, expiryTime)
      .accounts(dataToSend)
      .transaction()
    // 调用钱包签名交易
    const signedTransaction = await wallet.signTransaction(transaction)
    // 发送已签名的交易
    const txId = await connection.sendRawTransaction(signedTransaction.serialize())
    
  • 确认Program初始化正确性
    确保你的Program是使用钱包适配器提供的anchorWallet初始化的,示例代码:

    import { Program } from "@coral-xyz/anchor"
    import { IDL } from "./your-idl-file"
    import { useAnchorWallet, useConnection } from "@solana/wallet-adapter-react"
    
    const { connection } = useConnection()
    const anchorWallet = useAnchorWallet()
    
    const program = new Program(IDL, YOUR_PROGRAM_ID, {
      connection,
      wallet: anchorWallet,
    })
    

内容的提问来源于stack exchange,提问作者Mohsen Parvar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 08:01:41