【问题标题】:How can you transfer SOL using the web3.js sdk for Solana?如何使用 Solana 的 web3.js sdk 传输 SOL?
【发布时间】:2021-09-10 23:18:44
【问题描述】:

如何使用 Solana 的 web3.js sdk 传输 SOL?

【问题讨论】:

    标签: javascript web3 solana


    【解决方案1】:
    var web3 = require("@solana/web3.js");
    // Address: 9vpsmXhZYMpvhCKiVoX5U8b1iKpfwJaFpPEEXF7hRm9N
    const DEMO_FROM_SECRET_KEY = new Uint8Array([
        37, 21, 197, 185, 105, 201, 212, 148, 164, 108, 251, 159, 174, 252, 43, 246,
        225, 156, 38, 203, 99, 42, 244, 73, 252, 143, 34, 239, 15, 222, 217, 91, 132,
        167, 105, 60, 17, 211, 120, 243, 197, 99, 113, 34, 76, 127, 190, 18, 91, 246,
        121, 93, 189, 55, 165, 129, 196, 104, 25, 157, 209, 168, 165, 149,
    ]);
    (async () => {
        // Connect to cluster
        var connection = new web3.Connection(web3.clusterApiUrl("devnet"));
        // Construct a `Keypair` from secret key
        var from = web3.Keypair.fromSecretKey(DEMO_FROM_SECRET_KEY);
        // Generate a new random public key
        var to = web3.Keypair.generate();
        // Add transfer instruction to transaction
        var transaction = new web3.Transaction().add(
            web3.SystemProgram.transfer({
                fromPubkey: from.publicKey,
                toPubkey: to.publicKey,
                lamports: web3.LAMPORTS_PER_SOL / 100,
            })
        );
        // Sign transaction, broadcast, and confirm
        var signature = await web3.sendAndConfirmTransaction(
            connection,
            transaction,
            [from]
        );
        console.log("SIGNATURE", signature);
        console.log("SUCCESS");
    })();
    

    【讨论】:

    • 如何从现有的 Pvt 密钥和公钥创建或构造签名者?或者是否可以直接从 PVT 密钥字符串签署交易?
    • 您不能仅从公钥中创建签名者。您必须生成签名者的唯一选项是使用 privateKey 或助记词。相关功能是: import * as web3 from '@solana/web3.js'; const signer1 = web3.Keypair.fromSeed();或 const signer2 = web3.Keypair.fromSecretKey();或常量歌手 3 = web3.Keypair.generate(); //随机生成公钥和私钥
    • 在范围之外询问,但有人知道如何在智能合约中将图像与 solana 令牌链接起来吗?
    【解决方案2】:

    下面是一个示例,说明如何做到这一点。假设:

    1. 您有一个 Phantom 钱包。
    2. 您正在开发开发网
    3. 你有一个目标钱包的公钥(地址)字符串。此处将使用"9fuYBoRvgptU4fVZ8ZqvWTTc6oC68P4tjuSA2ySzn6Nv"

    代码

      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("9fuYBoRvgptU4fVZ8ZqvWTTc6oC68P4tjuSA2ySzn6Nv");
    
        // 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);
      }

    【讨论】:

      猜你喜欢
      • 2021-12-20
      • 2021-12-01
      • 1970-01-01
      • 2021-09-15
      • 1970-01-01
      • 2021-12-05
      • 2021-12-09
      • 2021-12-10
      • 2022-07-14
      相关资源
      最近更新 更多