Go工具

Go通过go-ethereum (geth)提供以太坊的官方实现,使其成为以太坊开发的强大选择。

请注意!
嘿!友情提示:此页面旨在为您提供使用 Go 请求的全面概述。有关实际代码示例,请查看我们的 API Methods documentation ,那里有所有支持语言的即用型示例!

# Go-Ethereum (geth)

以太坊协议的官方Go实现。

package ethereum

import (
    "context"
    "math/big"
    "github.com/ethereum/go-ethereum/ethclient"
    "github.com/ethereum/go-ethereum/common"
    "github.com/ethereum/go-ethereum/core/types"
)

type EthereumClient struct {
    client *ethclient.Client
}

func NewEthereumClient(url string) (*EthereumClient, error) {
    client, err := ethclient.Dial(url)
    if err != nil {
        return nil, err
    }
    return &EthereumClient{client: client}, nil
}

func (ec *EthereumClient) GetBalance(address string) (*big.Float, error) {
    account := common.HexToAddress(address)
    balance, err := ec.client.BalanceAt(context.Background(), account, nil)
    if err != nil {
        return nil, err
    }

    fbalance := new(big.Float)
    fbalance.SetString(balance.String())
    ethValue := new(big.Float).Quo(fbalance, big.NewFloat(1e18))

    return ethValue, nil
}

func (ec *EthereumClient) SendTransaction(from, to common.Address, value *big.Int) (*types.Transaction, error) {
    nonce, err := ec.client.PendingNonceAt(context.Background(), from)
    if err != nil {
        return nil, err
    }

    gasPrice, err := ec.client.SuggestGasPrice(context.Background())
    if err != nil {
        return nil, err
    }

    tx := types.NewTransaction(nonce, to, value, 21000, gasPrice, nil)
    return tx, nil
}
  • GitHub: go-ethereum
  • 文档: geth.ethereum.org
  • 特性:
    • 完整节点实现
    • 完整的以太坊协议
    • 高性能
    • 命令行工具
    • 移动端支持
    • 合约绑定

# 智能合约集成

在Go中使用智能合约:

package contracts

import (
    "github.com/ethereum/go-ethereum/accounts/abi/bind"
    "github.com/ethereum/go-ethereum/common"
)

type SmartContractClient struct {
    contract *bind.BoundContract
    address  common.Address
}

func NewContract(address common.Address, client *ethclient.Client) (*SmartContractClient, error) {
    parsed, err := abi.JSON(strings.NewReader(ContractABI))
    if err != nil {
        return nil, err
    }

    contract := bind.NewBoundContract(address, parsed, client, client, client)
    return &SmartContractClient{
        contract: contract,
        address:  address,
    }, nil
}

func (sc *SmartContractClient) CallMethod(method string, args ...interface{}) error {
    opts := &bind.CallOpts{
        Pending: false,
        Context: context.Background(),
    }

    return sc.contract.Call(opts, method, args...)
}

# 事件监控

订阅和监控以太坊事件:

func (ec *EthereumClient) MonitorBlocks() (<-chan *types.Header, error) {
    headers := make(chan *types.Header)
    sub, err := ec.client.SubscribeNewHead(context.Background(), headers)
    if err != nil {
        return nil, err
    }

    go func() {
        for {
            select {
            case err := <-sub.Err():
                log.Fatal(err)
            case header := <-headers:
                block, err := ec.client.BlockByHash(context.Background(), header.Hash())
                if err != nil {
                    log.Fatal(err)
                }
                fmt.Println("New block:", block.Number().Uint64())
            }
        }
    }()

    return headers, nil
}

# 工具和帮助函数

以太坊开发的实用工具:

package utils

import (
    "math/big"
    "github.com/ethereum/go-ethereum/common"
    "github.com/ethereum/go-ethereum/common/hexutil"
)

// WeiToEther converts wei to ether
func WeiToEther(wei *big.Int) *big.Float {
    return new(big.Float).Quo(
        new(big.Float).SetInt(wei),
        new(big.Float).SetInt(big.NewInt(1e18)),
    )
}

// EtherToWei converts ether to wei
func EtherToWei(ether *big.Float) *big.Int {
    truncInt, _ := new(big.Float).Mul(ether, big.NewFloat(1e18)).Int(nil)
    return truncInt
}

// IsValidAddress checks if the address is valid
func IsValidAddress(address string) bool {
    return common.IsHexAddress(address)
}

另请参阅

帮助我们变得更好!
分享此页面并帮助我们为您创建更好的产品。