0

我正在尝试从 HTML 运行 web3.js。现在到目前为止,我已经能够调用window.solana.connect();window.solana.disconnect();函数了。但是,当我尝试在代码下方运行时,它不起作用。我已经测试了各种选项,例如删除“web3”。从代码,但仍然没有工作。如果有人可以指导我如何建立连接,我会很高兴。

const connection = new web3.Connection(web3.clusterApiUrl("devnet"));

我下面的大部分代码来自对 Stackoveflow 的研究。以下链接: Solana:将 Sollet / Phantom Wallet Connect 添加到我的网站 - 步骤? 我想在 solana 上铸造一个新的代币。如何使用 solana-web3.js 做到这一点? 如何使用 Solana 的 web3.js sdk 传输 SOL? 如何通过 Phantom 使用 web3js 正确传输 Solana SOL

不幸的是,幻影网站上的文档也无济于事。 https://docs.phantom.app/integrating/establishing-a-connection

我现有的代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Welcome to Decentralized Ecommerce</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/web3/3.0.0-rc.5/web3.min.js" integrity="sha512-jRzb6jM5wynT5UHyMW2+SD+yLsYPEU5uftImpzOcVTdu1J7VsynVmiuFTsitsoL5PJVQi+OtWbrpWq/I+kkF4Q==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
    <script src="{{ url_for('static', filename='app.js') }}"></script>
    <script src="https://unpkg.com/@solana/web3.js@latest/lib/index.iife.js"></script>

    <script src="/static/solana.js"></script>

    <script type="text/javascript">
        
        async function transferSOL() {
            //Changes are only here, in the beginning
            if (window.solana.isConnected === false){
                const resp = await window.solana.connect();
            }
            const pubKey = await window.solana.publicKey;
            console.log("Public Key: ", pubKey);             

            // Establishing connection
            const connection = new web3.Connection(web3.clusterApiUrl("devnet"));
            alert('hello2');

            // 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("4iSD5Q6AnyhRHu6Uz4u1KAzXh3TwNwwQshEGhZbEXUTw");
            alert('hello3');

            // 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);

            }
            
    </script>
</head>
4

1 回答 1

4

在 HTML 上导入脚本后:

<script src="https://unpkg.com/@solana/web3.js@latest/lib/index.iife.js"> </script>

您应该可以致电:

const connection = new solanaWeb3.Connection(solanaWeb3.clusterApiUrl("mainnet-beta"));

请注意,它是solanaWeb3而不是web3

于 2021-12-07T00:02:31.627 回答