Documentation
¶
Index ¶
- Constants
- Variables
- func BuildContractInputData(contract abi.ABI, name string, args ...interface{}) ([]byte, error)
- func BuildPrivateKeyFromHex(privateKeyHex string) (*ecdsa.PrivateKey, error)
- func BuildPrivateKeyFromMnemonic(mnemonic string) (*ecdsa.PrivateKey, error)
- func BuildPrivateKeyFromMnemonicAndAccountId(mnemonic string, accountId uint32) (*ecdsa.PrivateKey, error)
- func DecodeRawTxHex(rawTx string) (*types.Transaction, error)
- func GeneratePrivateKey() (*ecdsa.PrivateKey, error)
- func GetABI(abiStr string) (abi.ABI, error)
- func GetContractMethodId(method string) string
- func GetEventTopic(event string) string
- func GetHexPrivateKey(privateKey *ecdsa.PrivateKey) string
- func GetHexPublicKey(privateKey *ecdsa.PrivateKey) string
- func GetMaxUint256() *big.Int
- func IsValidAddress(iAddress interface{}) bool
- func NewTx(to common.Address, nonce, gasLimit uint64, gasPrice, value *big.Int, ...) (*types.Transaction, error)
- func NewTxWithHexData(to common.Address, nonce, gasLimit uint64, gasPrice, value *big.Int, ...) (*types.Transaction, error)
- func PrivateKeyToAddress(privateKey *ecdsa.PrivateKey) common.Address
- func PublicKeyBytesToAddress(publicKey []byte) common.Address
- func ToDecimal(iValue interface{}, decimals int) decimal.Decimal
- func ToWei(iAmount interface{}, decimals int) *big.Int
- func VerifySignature(address string, data, signature []byte) bool
- type EtherProvider
- type EtherSigner
- type EtherWallet
- type NetworkConfig
- type Provider
- func (p *Provider) Close()
- func (p *Provider) EstimateGas(from, to common.Address, nonce uint64, gasPrice, value *big.Int, data []byte) (uint64, error)
- func (p *Provider) GetBlockByHash(blkHash common.Hash) (*types.Block, error)
- func (p *Provider) GetBlockByNumber(number *big.Int) (*types.Block, error)
- func (p *Provider) GetBlockNumber() (uint64, error)
- func (p *Provider) GetChainID() (*big.Int, error)
- func (p *Provider) GetContractBytecode(address common.Address) (string, error)
- func (p *Provider) GetEthClient() *ethclient.Client
- func (p *Provider) GetFromAddress(tx *types.Transaction) (common.Address, error)
- func (p *Provider) GetNetworkID() (*big.Int, error)
- func (p *Provider) GetRpcClient() *rpc.Client
- func (p *Provider) GetSuggestGasPrice() (*big.Int, error)
- func (p *Provider) GetTransactionByHash(txHash common.Hash) (tx *types.Transaction, isPending bool, err error)
- func (p *Provider) GetTransactionReceipt(txHash common.Hash) (*types.Receipt, error)
- func (p *Provider) IsContractAddress(address common.Address) (bool, error)
- type Signer
- func NewSigner() (*Signer, error)
- func NewSignerFromHexPrivateKey(hexPk string) (*Signer, error)
- func NewSignerFromMnemonic(mnemonic string) (*Signer, error)
- func NewSignerFromMnemonicAndAccountId(mnemonic string, accountId uint32) (*Signer, error)
- func NewSignerFromPrivateKey(pk *ecdsa.PrivateKey) (*Signer, error)
- func NewSignerFromRawPrivateKey(rawPk []byte) (*Signer, error)
- type Wallet
- func (w *Wallet) BuildTxOpts(value, nonce, gasPrice *big.Int) (*bind.TransactOpts, error)
- func (w *Wallet) CallContract(contractAddress common.Address, contractAbi abi.ABI, functionName string, ...) ([]interface{}, error)
- func (w *Wallet) CloseWallet()
- func (w *Wallet) GetAddress() common.Address
- func (w *Wallet) GetBalance() (*big.Int, error)
- func (w *Wallet) GetClient() *ethclient.Client
- func (w *Wallet) GetEthProvider() EtherProvider
- func (w *Wallet) GetEthSigner() EtherSigner
- func (w *Wallet) GetNonce() (uint64, error)
- func (w *Wallet) NewTx(to common.Address, nonce, gasLimit uint64, gasPrice, value *big.Int, ...) (*types.Transaction, error)
- func (w *Wallet) NewTxWithHexInput(to common.Address, nonce, gasLimit uint64, gasPrice, value *big.Int, ...) (*types.Transaction, error)
- func (w *Wallet) SendSignedTx(signedTx *types.Transaction) (common.Hash, error)
- func (w *Wallet) SendTx(to common.Address, nonce, gasLimit uint64, gasPrice, value *big.Int, ...) (common.Hash, error)
- func (w *Wallet) SendTxWithHexInput(to common.Address, nonce, gasLimit uint64, gasPrice, value *big.Int, ...) (common.Hash, error)
- func (w *Wallet) SignTx(tx *types.Transaction) (*types.Transaction, error)
- func (w *Wallet) Signature(data []byte) ([]byte, error)
Constants ¶
const ( MainnetChainID = 1 GoerliChainID = 5 SepoliaChainID = 11155111 PolygonChainID = 137 BSCChainID = 56 ArbitrumChainID = 42161 OptimismChainID = 10 AvalancheChainID = 43114 FantomChainID = 250 )
网络链ID常量
const ( DefaultGasLimit = 21000 // ETH 转账默认 gas limit ContractGasLimit = 200000 // 合约调用默认 gas limit DefaultGasPrice = 20000000000 // 20 Gwei MaxGasPrice = 1000000000000 // 1000 Gwei MinGasPrice = 1000000000 // 1 Gwei )
Gas 相关常量
const ( ERC20TransferGasLimit = 65000 ERC20ApproveGasLimit = 50000 UniswapSwapGasLimit = 300000 )
常用 Gas Limit
const ( DefaultTimeout = 30 BlockConfirmationTime = 12 // 以太坊平均出块时间 FastConfirmationTime = 15 // 快速确认超时 SafeConfirmationTime = 180 // 安全确认超时 )
时间相关常量 (秒)
const ( Wei = 1 GWei = 1000000000 Ether = 1000000000000000000 )
以太坊单位常量
const ( EthDecimals = 18 USDCDecimals = 6 USDTDecimals = 6 DefaultDecimals = 18 )
默认小数位数
const ( // 零地址 ZeroAddress = "0x0000000000000000000000000000000000000000" // 原生代币地址 (用于表示 ETH/BNB/MATIC 等) NativeTokenAddress = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE" )
常用地址
const ( ERC20TransferMethodID = "0xa9059cbb" ERC20TransferFromMethodID = "0x23b872dd" ERC20ApproveMethodID = "0x095ea7b3" ERC20BalanceOfMethodID = "0x70a08231" ERC20TotalSupplyMethodID = "0x18160ddd" )
ERC20 常用方法选择器
const ( ERC20TransferEventTopic = "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef" ERC20ApprovalEventTopic = "0x8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925" )
ERC20 常用事件主题
const (
ZeroHash = "0x0000000000000000000000000000000000000000000000000000000000000000"
)
常用哈希
Variables ¶
var ( BigInt0 = big.NewInt(0) BigInt1 = big.NewInt(1) BigInt2 = big.NewInt(2) BigInt10 = big.NewInt(10) BigInt100 = big.NewInt(100) // Gas 价格常量 DefaultGasPriceBig = big.NewInt(DefaultGasPrice) MinGasPriceBig = big.NewInt(MinGasPrice) MaxGasPriceBig = big.NewInt(MaxGasPrice) // Gas limit 常量 DefaultGasLimitBig = big.NewInt(DefaultGasLimit) ContractGasLimitBig = big.NewInt(ContractGasLimit) ERC20TransferGasLimitBig = big.NewInt(ERC20TransferGasLimit) ERC20ApproveGasLimitBig = big.NewInt(ERC20ApproveGasLimit) // 单位转换常量 WeiPerEther = big.NewInt(Ether) WeiPerGWei = big.NewInt(GWei) )
常用 big.Int 常量
var ( // 网络相关错误 ErrNetworkConnection = errors.New("failed to connect to ethereum network") ErrInvalidRPCURL = errors.New("invalid RPC URL") ErrNetworkTimeout = errors.New("network request timeout") // 地址相关错误 ErrInvalidAddress = errors.New("invalid ethereum address") ErrZeroAddress = errors.New("address cannot be zero address") // 私钥相关错误 ErrInvalidPrivateKey = errors.New("invalid private key") ErrInvalidMnemonic = errors.New("invalid mnemonic phrase") ErrInvalidKeyFormat = errors.New("invalid key format") // 交易相关错误 ErrInsufficientFunds = errors.New("insufficient funds for transaction") ErrInvalidGasPrice = errors.New("invalid gas price") ErrInvalidGasLimit = errors.New("invalid gas limit") ErrInvalidNonce = errors.New("invalid nonce") ErrTransactionFailed = errors.New("transaction execution failed") // 合约相关错误 ErrContractCall = errors.New("contract call failed") ErrInvalidABI = errors.New("invalid contract ABI") ErrInvalidContractAddress = errors.New("invalid contract address") // 签名相关错误 ErrSignatureFailed = errors.New("signature generation failed") ErrInvalidSignature = errors.New("invalid signature") ErrSignatureVerificationFailed = errors.New("signature verification failed") // 钱包相关错误 ErrWalletClosed = errors.New("wallet connection is closed") ErrInvalidWalletConfig = errors.New("invalid wallet configuration") )
标准错误定义
var NetworkConfigs = map[int64]NetworkConfig{ MainnetChainID: { ChainID: MainnetChainID, Name: "Ethereum Mainnet", Symbol: "ETH", BlockTime: 12, Confirmations: 12, }, GoerliChainID: { ChainID: GoerliChainID, Name: "Goerli Testnet", Symbol: "ETH", BlockTime: 12, Confirmations: 3, }, SepoliaChainID: { ChainID: SepoliaChainID, Name: "Sepolia Testnet", Symbol: "ETH", BlockTime: 12, Confirmations: 3, }, PolygonChainID: { ChainID: PolygonChainID, Name: "Polygon", Symbol: "MATIC", BlockTime: 2, Confirmations: 20, }, BSCChainID: { ChainID: BSCChainID, Name: "Binance Smart Chain", Symbol: "BNB", BlockTime: 3, Confirmations: 15, }, }
预定义网络配置
Functions ¶
func BuildContractInputData ¶
BuildContractInputData 构建合约的input data
func BuildPrivateKeyFromHex ¶
func BuildPrivateKeyFromHex(privateKeyHex string) (*ecdsa.PrivateKey, error)
BuildPrivateKeyFromHex 从字符串私钥构建私钥对象
func BuildPrivateKeyFromMnemonic ¶
func BuildPrivateKeyFromMnemonic(mnemonic string) (*ecdsa.PrivateKey, error)
func BuildPrivateKeyFromMnemonicAndAccountId ¶
func BuildPrivateKeyFromMnemonicAndAccountId(mnemonic string, accountId uint32) (*ecdsa.PrivateKey, error)
BuildPrivateKeyFromMnemonicAndAccountId 从助记词获得私钥
func DecodeRawTxHex ¶
func DecodeRawTxHex(rawTx string) (*types.Transaction, error)
DecodeRawTxHex 解析rawTx
func GeneratePrivateKey ¶
func GeneratePrivateKey() (*ecdsa.PrivateKey, error)
GeneratePrivateKey 创建私钥
func GetContractMethodId ¶
GetContractMethodId 获得合约的methodId 参数method,如:transfer(address,uint256)
func GetEventTopic ¶
GetEventTopic 获得事件的topic。event字符串如:transfer(address,uint256)
func GetHexPrivateKey ¶
func GetHexPrivateKey(privateKey *ecdsa.PrivateKey) string
GetHexPrivateKey 创建十六进制的私钥(不以0x开头)
func GetHexPublicKey ¶
func GetHexPublicKey(privateKey *ecdsa.PrivateKey) string
GetHexPublicKey 从私钥中获得十六进制的公钥(不以0x和04开头)
func NewTx ¶
func NewTx(to common.Address, nonce, gasLimit uint64, gasPrice, value *big.Int, data []byte) (*types.Transaction, error)
NewTx 新建一个tx
func NewTxWithHexData ¶
func NewTxWithHexData(to common.Address, nonce, gasLimit uint64, gasPrice, value *big.Int, hexData string) (*types.Transaction, error)
NewTxWithHexData 基于hexData构建一个Tx
func PrivateKeyToAddress ¶
func PrivateKeyToAddress(privateKey *ecdsa.PrivateKey) common.Address
PrivateKeyToAddress 从私钥中获得地址
func PublicKeyBytesToAddress ¶
PublicKeyBytesToAddress 公钥到地址
func VerifySignature ¶
VerifySignature 验证签名 address: 用于签名的地址 digestHash: 用于验证的原始数据 signature: 需要进行验证的签名数据
Types ¶
type EtherProvider ¶
type EtherProvider interface {
GetEthClient() *ethclient.Client
GetRpcClient() *rpc.Client
Close()
GetNetworkID() (*big.Int, error)
GetChainID() (*big.Int, error)
GetBlockByHash(hash common.Hash) (*types.Block, error)
GetBlockByNumber(number *big.Int) (*types.Block, error)
GetBlockNumber() (uint64, error)
GetSuggestGasPrice() (*big.Int, error)
GetTransactionByHash(hash common.Hash) (tx *types.Transaction, isPending bool, err error)
GetTransactionReceipt(txHash common.Hash) (*types.Receipt, error)
GetContractBytecode(address common.Address) (string, error)
IsContractAddress(address common.Address) (bool, error)
EstimateGas(from, to common.Address, nonce uint64, gasPrice, value *big.Int, data []byte) (uint64, error)
GetFromAddress(tx *types.Transaction) (common.Address, error)
}
EtherProvider 需要通过链上查询的,但是不需要账户的
type EtherSigner ¶
type EtherSigner interface {
GetAddress() common.Address
GetPrivateKey() *ecdsa.PrivateKey
}
EtherSigner ETH账户
type EtherWallet ¶
type EtherWallet interface {
GetEthSigner() EtherSigner
GetEthProvider() EtherProvider
GetClient() *ethclient.Client
GetAddress() common.Address
CloseWallet()
GetNonce() (uint64, error)
GetBalance() (*big.Int, error)
NewTx(to common.Address, nonce, gasLimit uint64, gasPrice, value *big.Int, data []byte) (*types.Transaction, error)
SendTx(to common.Address, nonce, gasLimit uint64, gasPrice, value *big.Int, data []byte) (common.Hash, error)
NewTxWithHexInput(to common.Address, nonce, gasLimit uint64, gasPrice, value *big.Int, input string) (*types.Transaction, error)
SendTxWithHexInput(to common.Address, nonce, gasLimit uint64, gasPrice, value *big.Int, input string) (common.Hash, error)
BuildTxOpts(value, nonce, gasPrice *big.Int) (*bind.TransactOpts, error)
SignTx(tx *types.Transaction) (*types.Transaction, error)
SendSignedTx(signedTx *types.Transaction) (common.Hash, error)
Signature(data []byte) ([]byte, error)
CallContract(contractAddress common.Address, contractAbi abi.ABI, functionName string, params ...interface{}) ([]interface{}, error)
}
EtherWallet 钱包信息
type NetworkConfig ¶
type NetworkConfig struct {
ChainID int64
Name string
Symbol string
BlockTime int // 秒
Confirmations int
}
网络配置
type Provider ¶
type Provider struct {
// contains filtered or unexported fields
}
func NewProvider ¶
func NewProviderWithChainId ¶
func (*Provider) EstimateGas ¶
func (p *Provider) EstimateGas(from, to common.Address, nonce uint64, gasPrice, value *big.Int, data []byte) (uint64, error)
EstimateGas 预估手续费
func (*Provider) GetBlockByHash ¶
GetBlockByHash 根据区块Hash获得区块信息
func (*Provider) GetBlockByNumber ¶
GetBlockByNumber 根据区块号获得区块信息
func (*Provider) GetBlockNumber ¶
GetBlockNumber 获得最新区块
func (*Provider) GetChainID ¶
GetChainID 获得ChainId
func (*Provider) GetContractBytecode ¶
GetContractBytecode 根据合约地址获得bytecode
func (*Provider) GetEthClient ¶
GetEthClient 获得ethClient客户端
func (*Provider) GetFromAddress ¶
GetFromAddress 获得交易的fromAddress
func (*Provider) GetNetworkID ¶
GetNetworkID 获得NetworkId
func (*Provider) GetRpcClient ¶
GetRpcClient 获得rpcClient客户端
func (*Provider) GetSuggestGasPrice ¶
GetSuggestGasPrice 获得建议的Gas
func (*Provider) GetTransactionByHash ¶
func (p *Provider) GetTransactionByHash(txHash common.Hash) (tx *types.Transaction, isPending bool, err error)
GetTransactionByHash 根据txHash获得交易信息
func (*Provider) GetTransactionReceipt ¶
GetTransactionReceipt 根据txHash获得交易Receipt
type Signer ¶
type Signer struct {
// contains filtered or unexported fields
}
func NewSignerFromHexPrivateKey ¶
NewSignerFromHexPrivateKey 使用私钥创建一个账号信息
func NewSignerFromMnemonic ¶
NewSignerFromMnemonic 使用助记词创建一个账号信息
func NewSignerFromMnemonicAndAccountId ¶
NewSignerFromMnemonicAndAccountId 使用助记词创建一个账号信息
func NewSignerFromPrivateKey ¶
func NewSignerFromPrivateKey(pk *ecdsa.PrivateKey) (*Signer, error)
NewSignerFromPrivateKey 使用私钥创建一个账号信息
func NewSignerFromRawPrivateKey ¶
NewSignerFromRawPrivateKey 使用私钥创建一个账号信息
func (*Signer) GetPrivateKey ¶
func (s *Signer) GetPrivateKey() *ecdsa.PrivateKey
GetPrivateKey 获得私钥
type Wallet ¶
type Wallet struct {
// contains filtered or unexported fields
}
func NewWalletWithComponents ¶
func NewWalletWithComponents(es EtherSigner, ep EtherProvider) (*Wallet, error)
NewWalletWithComponents creates a new Wallet with given signer and provider components
func (*Wallet) BuildTxOpts ¶
BuildTxOpts 构建交易的选项
func (*Wallet) CallContract ¶
func (w *Wallet) CallContract(contractAddress common.Address, contractAbi abi.ABI, functionName string, params ...interface{}) ([]interface{}, error)
CallContract 调用合约的方法,无需创建交易
func (*Wallet) GetEthProvider ¶
func (w *Wallet) GetEthProvider() EtherProvider
GetEthProvider 获得EthProvider
func (*Wallet) NewTx ¶
func (w *Wallet) NewTx(to common.Address, nonce, gasLimit uint64, gasPrice, value *big.Int, data []byte) (*types.Transaction, error)
NewTx 构建一笔交易。nonce传0表示字段计算;gasLimit传0表示字段计算;gasPrice穿nil或者big.NewInt(0)表示gasPrice自动计算。
func (*Wallet) NewTxWithHexInput ¶
func (w *Wallet) NewTxWithHexInput(to common.Address, nonce, gasLimit uint64, gasPrice, value *big.Int, input string) (*types.Transaction, error)
NewTxWithHexInput 构建一笔交易,使用0x开头的input。nonce传0表示字段计算;gasLimit传0表示字段计算;gasPrice穿nil或者big.NewInt(0)表示gasPrice自动计算。
func (*Wallet) SendSignedTx ¶
SendSignedTx 发送签名后的Tx
func (*Wallet) SendTx ¶
func (w *Wallet) SendTx(to common.Address, nonce, gasLimit uint64, gasPrice, value *big.Int, data []byte) (common.Hash, error)
SendTx 发送交易。nonce传0表示字段计算;gasLimit传0表示字段计算;gasPrice穿nil或者big.NewInt(0)表示gasPrice自动计算。
func (*Wallet) SendTxWithHexInput ¶
func (w *Wallet) SendTxWithHexInput(to common.Address, nonce, gasLimit uint64, gasPrice, value *big.Int, input string) (common.Hash, error)
SendTxWithHexInput 发送一笔交易,使用0x开头的input。nonce传0表示字段计算;gasLimit传0表示字段计算;gasPrice穿nil或者big.NewInt(0)表示gasPrice自动计算。
func (*Wallet) SignTx ¶
func (w *Wallet) SignTx(tx *types.Transaction) (*types.Transaction, error)
SignTx 对交易进行签名