开发者问题收集

如何通过 Phantom 使用 web3js 正确传输 Solana SOL

2021-10-12
8223

我正在使用 Solana 区块链。正在尝试通过 Phantom 转移 Solana SOL。为此,我使用了以下在 Stack Overflow 上找到的代码: 源链接

我已经在 Chrome 中安装了 Phantom。当我运行代码时,它显示错误:

Uncaught (in promise) TypeError: Cannot read properties of null (reading 'toString')

我认为是这行代码导致了上述错误

console.log("Public key of the emitter: ",provider.publicKey.toString());

这是代码

import * as web3 from '@solana/web3.js';
  import * as splToken from '@solana/spl-token';
  
  const getProvider = async () => {
    if ("solana" in window) {
      const provider = window.solana;
      if (provider.isPhantom) {
        console.log("Is Phantom installed?  ", provider.isPhantom);
        return provider;
      }
    } else {
      window.open("https://www.phantom.app/", "_blank");
    }
  };


  async function transferSOL() {
    // Detecing and storing the phantom wallet of the user (creator in this case)
    var provider = await getProvider();
    console.log("Public key of the emitter: ",provider.publicKey.toString());

    // Establishing connection
    var connection = new web3.Connection(
      web3.clusterApiUrl('devnet'),
    );

    // I have hardcoded my secondary wallet address here. You can take this address either from user input or your DB or wherever
    var recieverWallet = new web3.PublicKey("CkiKLEa9eSEoG6CoTSuaahsF2WqNgArnvoCSbNZjJ7BQ");

    // Airdrop some SOL to the sender's wallet, so that it can handle the txn fee
    var airdropSignature = await connection.requestAirdrop(
      provider.publicKey,
      web3.LAMPORTS_PER_SOL,
    );

    // Confirming that the airdrop went through
    await connection.confirmTransaction(airdropSignature);
    console.log("Airdropped");

    var transaction = new web3.Transaction().add(
      web3.SystemProgram.transfer({
        fromPubkey: provider.publicKey,
        toPubkey: recieverWallet,
        lamports: web3.LAMPORTS_PER_SOL //Investing 1 SOL. Remember 1 Lamport = 10^-9 SOL.
      }),
    );

    // Setting the variables for the transaction
    transaction.feePayer = await provider.publicKey;
    let blockhashObj = await connection.getRecentBlockhash();
    transaction.recentBlockhash = await blockhashObj.blockhash;

    // Transaction constructor initialized successfully
    if(transaction) {
      console.log("Txn created successfully");
    }
    
    // Request creator to sign the transaction (allow the transaction)
    let signed = await provider.signTransaction(transaction);
    // The signature is generated
    let signature = await connection.sendRawTransaction(signed.serialize());
    // Confirm whether the transaction went through or not
    await connection.confirmTransaction(signature);

    //Signature chhap diya idhar
    console.log("Signature: ", signature);
  }
3个回答

您需要连接到钱包。缺少该部分

const getProvider = async () => {
    if ("solana" in window) {

      // opens wallet to connect to
      await window.solana.connect(); 

      const provider = window.solana;
      if (provider.isPhantom) {
        console.log("Is Phantom installed?  ", provider.isPhantom);
        return provider;
      }
    } else {
      window.open("https://www.phantom.app/", "_blank");
    }
  };
Vikram
2021-10-21

我不确定这是否是最好的解决方案,但您的问题在于用户登录后幻影钱包的持久性。您必须为此寻找前端重型解决方案。其中之一是:

  1. 假设您正在使用 React,请使用上下文 API 来持久保存有关钱包的数据。以下是有关如何通过在项目根目录中的上下文文件夹下创建文件来执行此操作的粗略指南:
import React, { createContext, useState} from "react";

export const WalletDataContext=createContext();

export const WalletDataContextProvider=(props)=>{
    const [publicKey,setPublicKey]=useState(null);
    const [wallet,setWallet]=useState(null);
    
    return (
        <WalletDataContext.Provider
            value={{publicKey,setPublicKey,wallet,setWallet}}
        >
            {props.children}            
        </WalletDataContext.Provider>
    )
}
  1. 创建一个 connectWallet 函数,如下所示:
//import {WalletDataContext}
  //import other stuff:
  const {setPublicKey,setWallet}=useContext(WalletDataContext)
  const connectWallet = async() {
  const provider = await getProvider();
  if(provider) {
    await provider.connect();
    let publicKey = "";
    provider.on("connect", async () => {
      setWallet(provider);
      publicKey = provider.pubicKey.toString();
      setPublicKey(publicKey);
      /*
        // more things that you would like to do here
      */
    });
  }
}
  1. 在您的 transferSOL 函数中进行以下更改:
async function transferSOL() {
    //Changes are only here, in the beginning
    const phantomProvider = wallet;
    if(!phantomProvider){
      //Urge the user to sign in(connect) again
    }
    const pubKey = await phantomProvider.publicKey;
    console.log("Public Key: ", pubKey); 

    // Establishing connection
    var connection = new web3.Connection(
      web3.clusterApiUrl('devnet'),
    );

    // I have hardcoded my secondary wallet address here. You can take this address either from user input or your DB or wherever
    var recieverWallet = new web3.PublicKey("CkiKLEa9eSEoG6CoTSuaahsF2WqNgArnvoCSbNZjJ7BQ");

    // Airdrop some SOL to the sender's wallet, so that it can handle the txn fee
    var airdropSignature = await connection.requestAirdrop(
      provider.publicKey,
      web3.LAMPORTS_PER_SOL,
    );

    // Confirming that the airdrop went through
    await connection.confirmTransaction(airdropSignature);
    console.log("Airdropped");

    var transaction = new web3.Transaction().add(
      web3.SystemProgram.transfer({
        fromPubkey: provider.publicKey,
        toPubkey: recieverWallet,
        lamports: web3.LAMPORTS_PER_SOL //Investing 1 SOL. Remember 1 Lamport = 10^-9 SOL.
      }),
    );

    // Setting the variables for the transaction
    transaction.feePayer = await provider.publicKey;
    let blockhashObj = await connection.getRecentBlockhash();
    transaction.recentBlockhash = await blockhashObj.blockhash;

    // Transaction constructor initialized successfully
    if(transaction) {
      console.log("Txn created successfully");
    }
    
    // Request creator to sign the transaction (allow the transaction)
    let signed = await provider.signTransaction(transaction);
    // The signature is generated
    let signature = await connection.sendRawTransaction(signed.serialize());
    // Confirm whether the transaction went through or not
    await connection.confirmTransaction(signature);

    //Signature or the txn hash
    console.log("Signature: ", signature);
  }
Rahul Saxena
2021-10-17

我认为你的问题在这里:

// Request creator to sign the transaction (allow the transaction)
    let signed = await provider.signTransaction(transaction);
    // The signature is generated
    let signature = await connection.sendRawTransaction(signed.serialize());
    // Confirm whether the transaction went through or not
    await connection.confirmTransaction(signature);

你使用提供商签名,但使用连接发送,请尝试更改此设置:

const { signature } = await provider?.signAndSendTransaction(
      transaction
    );

    await connection.getSignatureStatus(signature);
    return signature;
Bruno Raphael Rocha
2022-12-23