For the complete documentation index, see llms.txt. This page is also available as Markdown.

Ethereum Broadcast Tx

介紹BlockRazor Ethereum Transaction Sending 模式的 Broadcast Tx 接口以及集成方法

端點

地區
Relay IP:Port

法蘭克福

64.130.47.75:50061

東京

63.254.162.18:50061

弗吉尼亞

208.91.105.204:50061

價格 & 限流

支付方式
限流
價格
操作

免費

  • TPS:10 Txs / 5s

  • 每日交易上限:10

Free

-

Personalized

  • TPS:100 Txs / 5s

  • 每日交易上限:100000

$50 / 日 $500 / 月

SendTx

請求參數

參數
必選
格式
示例
描述

Transaction

String

"0xf8……8219"

經過簽名的rawtx

請求示例

package main

import (
	"context"
	"fmt"
	"math/big"
	"time"

	// directory of the generated code using the provided relay.proto file
	pb "github.com/BlockRazorinc/eth_relay_example/protobuf"
	"github.com/ethereum/go-ethereum/common"
	"github.com/ethereum/go-ethereum/core/types"
	"github.com/ethereum/go-ethereum/crypto"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
)

// auth will be used to verify the credential
type Authentication struct {
	apiKey string
}

func (a *Authentication) GetRequestMetadata(context.Context, ...string) (map[string]string, error) {
	return map[string]string{"apikey": a.apiKey}, nil
}

func (a *Authentication) RequireTransportSecurity() bool {
	return false
}

func main() {

	// BlockRazor relay endpoint address
	blzrelayEndPoint := "ip:port"

	// auth will be used to verify the credential
	auth := Authentication{
		"your auth token",
	}

	// open gRPC connection to BlockRazor relay
	var err error
	conn, err := grpc.NewClient(blzrelayEndPoint, grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithPerRPCCredentials(&auth), grpc.WithWriteBufferSize(0), grpc.WithInitialConnWindowSize(128*1024))
	if err != nil {
		fmt.Println("error: ", err)
		return
	}
	defer conn.Close()

	// use the Relay client connection interface.
	client := pb.NewRelayClient(conn)

	// create context
	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
	defer cancel()

	// replace with your address
	fromPrivateAddress := "42b565......44d05c"
	toPublicAddress := "0x4321......3f1c66"

	// replace with your transaction data
	nonce := uint64(1)
	toAddress := common.HexToAddress(toPublicAddress)
	var data []byte
	gasLimit := uint64(21000)
	value := big.NewInt(0)
	chainID := big.NewInt(1)
	gasTipCap := big.NewInt(2_000_000_000)
	gasFeeCap := big.NewInt(30_000_000_000)

	// create new EIP-1559 transaction
	tx := types.NewTx(&types.DynamicFeeTx{
		ChainID:   chainID,
		Nonce:     nonce,
		GasTipCap: gasTipCap,
		GasFeeCap: gasFeeCap,
		Gas:       gasLimit,
		To:        &toAddress,
		Value:     value,
		Data:      data,
	})

	privateKey, err := crypto.HexToECDSA(fromPrivateAddress)
	if err != nil {
		fmt.Println("fail to casting private key to ECDSA")
		return
	}

	// sign transaction by private key
	signedTx, err := types.SignTx(tx, types.LatestSignerForChainID(chainID), privateKey)
	if err != nil {
		fmt.Println("fail to sign transaction")
		return
	}

	// marshal signed transaction to raw binary bytes
	rawTx, err := signedTx.MarshalBinary()
	if err != nil {
		fmt.Println("fail to marshal signed transaction")
		return
	}

	// send raw tx by BlockRazor
	res, err := client.SendTx(ctx, &pb.SendTxRequest{RawTx: rawTx})
	if err != nil {
		fmt.Println("failed to send raw tx: ", err)
		return
	}
	fmt.Println("raw tx sent by BlockRazor, tx hash is ", res.TxHash)
}

返回示例

正常

异常

Proto

relay.proto文件代碼如下:

代码示例

https://github.com/BlockRazorinc/eth_relay_example

最後更新於

這有幫助嗎?