簡體   English   中英

如何使用 Web3.js 通過 Solana Pay 連接虛擬錢包?

[英]How to connect phantom wallet by Solana Pay using Web3.js?

我正在嘗試使用 Web3.js 從 Solana Pay 連接 Phantom 錢包但是我不斷收到錯誤“緩沖區未定義”。 如何解決這個問題?

注意:我沒有使用 Nodejs。 我正在嘗試創建與 Web3.js 庫的集成。

這是代碼:

<script type="text/javascript" src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
       
<script src="https://unpkg.com/@solana/web3.js@latest/lib/index.iife.min.js"></script>
<script>
// The below is the code where I am getting the error
if(window.solana && window.solana.isPhantom){
                    const resp= await window.solana.connect();
                    var connection= new solanaWeb3.Connection(
                        solanaWeb3.clusterApiUrl('devnet'),
                        'confirmed',
                    );

const receiverWallet= new solanaWeb3.PublicKey("xxxxxxxxxxxxxxxxxxxxxx");
                    var transaction= new solanaWeb3.Transaction().add(
                        solanaWeb3.SystemProgram.transfer({
                            fromPubkey: resp.PublicKey,
                            toPubkey: receiverWallet,
                            lamports: 10000000*50,
                        }),
                    );
}
</script>

使用 solana/web3.js v1.30.2

或者為最新版本安裝一些額外的依賴項

我正在分享一個博客。 它基於 ReactJS 但希望你能理解。 沒問題,只需將您的版本更改為 1.30.2 即可。

Blog_for_extra_dependencies

您也可以參考此代碼。 代碼沙盒

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM