简体   繁体   English

window.solana 在 js web3 上找不到

[英]window.solana not found on js web3

I want to connect a Solana wallet (phantom or any other) to a web aplication through the js web3 library.我想通过 js web3 库将 Solana 钱包(幻像或任何其他)连接到 web 应用程序。 I've read docs for most wallets and it seems like it's just as simple as await window.solana.request({ method: "connect" });我已经阅读了大多数钱包的文档,看起来就像await window.solana.request({ method: "connect" }); but window.solana is undefined in my case.但 window.solana 在我的情况下是未定义的。 When I do console.log(window) I can see the solana value with all its corresponding keys and values.当我执行console.log(window)时,我可以看到 solana 值及其所有对应的键和值。 How can I do this?我怎样才能做到这一点?

Found some workinmg code that solved my issue.找到了一些解决我的问题的工作代码。 I am not sure what was the issue as I'm not very experience with js but the following code lets me connect to phantom.我不确定是什么问题,因为我对 js 不是很熟悉,但是下面的代码可以让我连接到幻像。 I found this on StackOverflow on a similar thread, although I belive the original answer is missing some brackets.我在 StackOverflow 上的一个类似线程上找到了这个,尽管我相信原始答案缺少一些括号。 Solana: Adding Sollet / Phantom Wallet Connect to my website - Steps? Solana:将 Sollet / Phantom Wallet Connect 添加到我的网站 - 步骤?

const getProvider = async () => {
    if ("solana" in window) {
      await window.solana.connect(); // opens wallet to connect to

      const provider = window.solana;
      if (provider.isPhantom) {
        console.log("Is Phantom installed?  ", provider.isPhantom);
        return provider;
      }
    } else {
      document.write('Install https://www.phantom.app/');
    }
};

window.onload = () => {

    getProvider().then(provider => {
        console.log('key', provider.publicKey.toString())
    })
    .catch(function(error){
        console.log(error)
    });

}

With your current implementation, everytime you refresh the app, you will get pop up to connect to the wallet.使用您当前的实现,每次刷新应用程序时,您都会弹出连接到钱包。 Instead you add {onlyIfTrusted:true} option to connect.相反,您添加{onlyIfTrusted:true}选项来连接。

const getProvider = async () => { if ("solana" in window) { await window.solana.connect({onlyIfTrusted:true}); const getProvider = async () => { if ("solana" in window) { await window.solana.connect({onlyIfTrusted:true}); // opens wallet to connect to // 打开要连接的钱包

  const provider = window.solana;
  if (provider.isPhantom) {
    console.log("Is Phantom installed?  ", provider.isPhantom);
    return provider;
  }
} else {
  document.write('Install https://www.phantom.app/');
}
};

then instead of getting pop up when you reload the app, write a connection function to handle the connection when a user clicks on the button然后在重新加载应用程序时不要弹出,而是编写一个连接 function 来处理用户单击按钮时的连接

const connectToWallet=async ()=>{
    const {solana}=window
    if(solana){
      const response=await solana.connect()
      console.log('address',response.publicKey.toString())
      
    }
  }


<button onClick={connectToWallet}  >
    Connect to Wallet
    </button>

Now once user is connected, when you reload the app, it you wont get pop up to connect to the wallet现在一旦用户连接,当你重新加载应用程序时,你不会弹出连接到钱包

Is your website https enabled?您的网站 https 是否已启用? If not then it won't work如果没有,那么它将无法正常工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM