Solana主网使用自定义的RPC进行转账-sol转账代码

import { useWallet } from 'solana-wallets-vue'
import {Connection, clusterApiUrl, Keypair, SystemProgram, Transaction, PublicKey} from '@solana/web3.js';

const { publicKey, connected, sendTransaction } = useWallet();

const onClick = async () => {
        if (!publicKey) {
            console.log('error', `Send Transaction: Wallet not connected!`);
            return;
        }

        let signature = '';
        try {
            // send 1 lamport to random account
            // https://solanacookbook.com/references/basic-transactions.html#how-to-send-sol

            const account = Keypair.generate();
            const transaction = new Transaction().add(
                SystemProgram.transfer({
                    fromPubkey: publicKey.value,
                    toPubkey: new PublicKey("接收地址"),
                    lamports: 1_000_000,
                })
            );

            //const connection = new Connection(clusterApiUrl('devnet'));
            const connection = new Connection(
                "https://solana-mainnet.core.chainstack.com/xxx", //替换rpc链接
                {
                  wsEndpoint:'wss://solana-mainnet.core.chainstack.com/ws/xxx' //替换rpc链接
                }
            );
            signature = await sendTransaction(transaction, connection);
            await connection.confirmTransaction(signature, 'confirmed');
            console.log("successs")
        } catch (error) {
            console.log('error', `Transaction failed! ${error?.message}`, signature);
            return;
        }
    }
上一篇:Zookeeper+Kafka


下一篇:基于JavaScript的简单RPC原理演示