Go语言实现以太坊交易发送代码

1 篇文章 0 订阅
1 篇文章 0 订阅

转载请注明。

talk is cheap show you the code

import (
	"math/big"
	"testing"
	"github.com/ethereum/go-ethereum/crypto"
	"github.com/ethereum/go-ethereum/core/types"
	"io/ioutil"
	"github.com/ethereum/go-ethereum/accounts/keystore"
	"github.com/stretchr/testify/require"
	"github.com/ethereum/go-ethereum/ethclient"
	"github.com/ethereum/go-ethereum/accounts/abi/bind"
	"context"
	"fmt"
	"github.com/ethereum/go-ethereum/common"
)

// 交易发起方keystore文件地址
var fromKeyStoreFile = "";

// keystore文件对应的密码
var password = "";

// 交易接收方地址
var toAddress = ""

// http服务地址, 例:http://localhost:8545
var httpUrl = "http://ip:port"

/*
	以太坊交易发送
 */
func TestSendTx(t *testing.T){
	// 交易发送方
	// 获取私钥方式一,通过keystore文件
	fromKeystore,err := ioutil.ReadFile(fromKeyStoreFile)
	require.NoError(t,err)
	fromKey,err := keystore.DecryptKey(fromKeystore,password)
	fromPrivkey := fromKey.PrivateKey
	fromPubkey := fromPrivkey.PublicKey
	fromAddr := crypto.PubkeyToAddress(fromPubkey)

	// 获取私钥方式二,通过私钥字符串
	//privateKey, err := crypto.HexToECDSA("私钥字符串")

	// 交易接收方
	toAddr := common.StringToAddress(toAddress)

	// 数量
	amount := big.NewInt(14)

	// gasLimit
	var gasLimit uint64 = 300000

	// gasPrice
	var gasPrice *big.Int = big.NewInt(200)

	// 创建客户端
	client, err:= ethclient.Dial(httpUrl)
	require.NoError(t, err)

	// nonce获取
	nonce, err := client.PendingNonceAt(context.Background(), fromAddr)

	// 认证信息组装
	auth := bind.NewKeyedTransactor(fromPrivkey)
	//auth,err := bind.NewTransactor(strings.NewReader(mykey),"111")
	auth.Nonce = big.NewInt(int64(nonce))
	auth.Value = amount     // in wei
	//auth.Value = big.NewInt(100000)     // in wei
	auth.GasLimit = gasLimit // in units
	//auth.GasLimit = uint64(0) // in units
	auth.GasPrice = gasPrice
	auth.From = fromAddr

	// 交易创建
	tx := types.NewTransaction(nonce,toAddr,amount,gasLimit,gasPrice,[]byte{})

	// 交易签名
	signedTx ,err:= auth.Signer(types.HomesteadSigner{}, auth.From, tx)
	//signedTx ,err := types.SignTx(tx,types.HomesteadSigner{},fromPrivkey)
	require.NoError(t, err)

	// 交易发送
	serr := client.SendTransaction(context.Background(),signedTx)
	if serr != nil {
		fmt.Println(serr)
	}

	// 等待挖矿完成
	bind.WaitMined(context.Background(),client,signedTx)

}

 

代码运行前需做如下准备工作:

本地启动一个以太坊节点,该节点连接到哪个网络,之后的交易就相应发送到该网络中。

通过geth启动本地私网例子,供参考:

geth --networkid 11 --dev --datadir data1 --rpcapi personal,db,eth,net,web3 --rpc --ws

 

上述go代码简单说明,知道原理的看代码注释就一清二楚了。

明确:

1.通过什么发送交易

交易发送通过的是client,该client可以支持连接http或者websocket端口

2.发送交易需要提供哪些信息

这些信息就是client.SendTransaction的参数

nonce,toAddr,amount,gasLimit,gasPrice,payload[]

3.挖矿在交易发送后进行

如果不等待挖矿执行完成,程序就会直接退出,交易将不会成功

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值