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

标签 javascript web3 solana

我正在与 Solana 区 block 链合作。我正在尝试通过 Phantom 转移 Solana SOL。为此,我使用了下面在 stackoverflow 中使用的代码。
source link
我已经在我的 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);
  }

最佳答案

您需要连接到钱包。那部分不见了

  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");
    }
  };

关于javascript - 如何通过 Phantom 使用 web3js 正确传输 Solana SOL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69546971/

相关文章:

javascript - setTimeout 是 JavaScript 本身的一部分还是它只是浏览器提供的一个 API?

javascript - 使用 MEAN 堆栈进行身份验证,无需用户系统

ubuntu - 无法在 ubuntu 18.04 中安装 web3

solana - 将图像和名称添加到现有的 WL spl-token

solana - 如何使用交易哈希(签名)确认 Solana 上是否发生了交易

javascript - 单击父级填充 iframe 输入字段

Javascript 源代码层

javascript - eth.sign vs eth.accounts.sign 创建不同的符号

ubuntu - 如何在 Ubuntu 上安装 web3?

rpc - Solana 上的某些集群禁用了 connection.getProgramAccounts?