Solana主网使用自定义的RPC进行转账

1、引言

如果用 browser 连接主网的 RPC server 会收到 error code 403 message 為 Access forbidden, contact your app developer or support@rpcpool.com. 错误,因为主网的 RPC server 会检查 HTTP Header 如果判断出來是 browser 就会报告 403 錯誤。

要解決这个问题就是换一个 RPC server ,可以注册免费的 RPC server 。

2、免费RPC Server

Solana RPC Server 是让开发者存取区块链数据的 Server , Solana 主网有提供一个(Solana Cluster RPC Endpoints (opens new window)),但是为了避免滥用,有设定一些网路存取的限制,存取太快会产生 HTTP 429 的错误。

所以要开发 Solana 应用程式最好还是有个自己的 RPC Server ,不过要建立自己的 RPC Server 代价相当高,根据 How to run a Solana RPC node (opens new window)这篇文章, CPU 要 32 core ,记忆体要 250 GB ,这样的硬体规格租下来可是相当花钱。

于是就有一些公司提供 RPC Server 服务,然后只要注册就可以使用到一些免费额度,这些免费额度对于开发 Solana 应用程式就相当够用了,以下几个是博主试过有免费额度又稳定的 Solana RPC server

Chainstack

一个月免费额度 300 万个 request

QuickNode

一个月免费额度 1000 万个 request

3、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;
        }
    }

4、转账结果查询

相关推荐
JackSparrow4141 天前
Kafka(七)集成Apache Avro+Apicurio Schema Registry以保障生产者与消费者的消息兼容性
java·中间件·rpc·kafka·apache·avro·schema-registry
爱学堂IT分享2 天前
码神之路-Netty-从零实现RPC框架+SpringBoot实战项目教程
spring boot·网络协议·rpc
可爱系程序猿3 天前
Windows 11 添加共享打印机提示 0x0000011b:从 RPC 到驱动架构排查
windows·rpc·架构
SWAGGY..5 天前
【C++ 初阶】:(12)深入理解C++ stack:容器适配器、底层容器与算法实战
网络·网络协议·rpc
Mr. zhihao6 天前
从零手写一个 RPC 框架:用一条因果链推导出 Dubbo 的骨架
网络协议·rpc·dubbo
LayZhangStrive10 天前
后端通识 - 远程服务调用RPC
网络·网络协议·rpc·sentinel·openfeign·远程服务调用
xfhuangfu13 天前
Oracle中建立到CDB和PDB的连接
数据库·oracle·rpc
一木 之林13 天前
Python.六.(一)--1.标准库、常用工具与基础操作(进阶)
python·rpc·dubbo
qianyuqiye114 天前
NFS 网络文件系统完整实战|原理 + RPC 架构 + 多 Web 集群共享存储
运维·网络·安全·rpc·架构·php
小小龙学IT15 天前
gRPC 开源高性能 RPC 框架深度解析:从 HTTP/2 到跨语言微服务实战
http·rpc·开源