eth

package
v0.1.9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 18, 2022 License: MIT Imports: 22 Imported by: 5

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ERC20  交易method
	ERC20_METHOD_TRANSFER = "transfer"
	ERC20_METHOD_APPROVE  = "approve"
)
View Source
var (
	DEFAULT_CONTRACT_GAS_LIMIT = "63000"
	DEFAULT_ETH_GAS_LIMIT      = "21000"
	// 当前网络 standard gas price
	DEFAULT_ETH_GAS_PRICE = "20000000000"
)

默认gas limit估算失败后,21000 * 3 = 63000

View Source
var (
	// 合约 ABI json文件,查询ERC20 相关代币信息需要使用 ABI 文件
	Erc20AbiStr = `` /* 4592-byte string literal not displayed */
)

Functions

func GetChainId

func GetChainId(e *EthChain) (string, error)

获取链ID

func IsValidAddress added in v0.1.2

func IsValidAddress(address string) bool

func MapListConcurrent added in v0.1.5

func MapListConcurrent(list []interface{}, maper func(interface{}) (interface{}, error)) ([]interface{}, error)

这个方法会并发遍历数组, 并映射数组中的每一个对象 @param list : [TYPE1], a list that all item is TYPE1 @param maper : func(TYPE1) (TYPE2, error), a function that input TYPE1, return TYPE2

you can throw an error to finish task.

@return : [TYPE2], a list that all item is TYPE2 @example : ```

nums := []interface{}{1, 2, 3, 4, 5, 6}
res, _ := MapListConcurrent(nums, func(i interface{}) (interface{}, error) {
    return strconv.Itoa(i.(int) * 100), nil
})
println(res) // ["100" "200" "300" "400" "500" "600"]

```

func MapListConcurrentStringToString added in v0.1.5

func MapListConcurrentStringToString(strList []string, maper func(string) (string, error)) ([]string, error)

对 MapListConcurrent 的封装, 映射对象不是该方法的重点,并发才是

func MapToBasicError added in v0.1.9

func MapToBasicError(e error) error

将任何其他自定义类型的 error, 转为系统基础的 error 对象

func PrivateKeyToAddress

func PrivateKeyToAddress(privateKey string) (string, error)

私钥转地址

Types

type BuildTxResult

type BuildTxResult struct {
	SignedTx *types.Transaction
	TxHex    string
}

type CallMethodOpts

type CallMethodOpts struct {
	Nonce                int64
	Value                string
	GasPrice             string // MaxFeePerGas
	GasLimit             string
	IsPredictError       bool
	MaxPriorityFeePerGas string
}

type CallMethodOptsBigInt

type CallMethodOptsBigInt struct {
	Nonce                uint64
	Value                *big.Int
	GasPrice             *big.Int // MaxFeePerGas
	GasLimit             uint64
	IsPredictError       bool
	MaxPriorityFeePerGas *big.Int
}

func OptsTobigInt

func OptsTobigInt(opts *CallMethodOpts) *CallMethodOptsBigInt

将MethodOpts 进行转化,由于端的限制,只能传入string字符

type CoinUtil added in v0.1.2

type CoinUtil struct {
	// 链的 RPC 地址
	RpcUrl string
	// 币种的合约地址,如果为 nil,表示是主网的币
	ContractAddress string
	// 用户的钱包地址
	WalletAddress string
}

func NewCoinUtilWithRpc added in v0.1.2

func NewCoinUtilWithRpc(rpcUrl, contractAddress, walletAddress string) *CoinUtil

创建 CoinUtil 对象 @param contractAddress 币种的合约地址,如果是主网的币,可传 nil @param walletAddress 用户的钱包地址

func (*CoinUtil) BuildTransferTx added in v0.1.2

func (u *CoinUtil) BuildTransferTx(privateKey, receiverAddress, nonce, gasPrice, gasLimit, amount string) (string, error)

创建 ETH 转账交易

func (*CoinUtil) CoinInfo added in v0.1.2

func (u *CoinUtil) CoinInfo() (*Erc20Token, error)

查询币种信息 主币只能获取到余额 合约币能获取到所有信息

func (*CoinUtil) EstimateGasLimit added in v0.1.2

func (u *CoinUtil) EstimateGasLimit(receiverAddress, gasPrice, amount string) (string, error)

获取转账的 预估 gasLimit

func (*CoinUtil) FetchTransactionDetail added in v0.1.2

func (u *CoinUtil) FetchTransactionDetail(hashString string) (*TransactionDetail, error)

获取交易的详情 @param hashString 交易的 hash @return 详情对象,该对象无法提供 CID 信息

func (*CoinUtil) FetchTransactionStatus added in v0.1.2

func (u *CoinUtil) FetchTransactionStatus(hashString string) TransactionStatus

获取交易的状态 @param hashString 交易的 hash

func (*CoinUtil) IsMainCoin added in v0.1.2

func (u *CoinUtil) IsMainCoin() bool

是否是主币

func (*CoinUtil) Nonce added in v0.1.2

func (u *CoinUtil) Nonce() (string, error)

获取交易的 nonce

func (*CoinUtil) QueryBalance added in v0.1.2

func (u *CoinUtil) QueryBalance() (string, error)

查询用户的钱包余额

func (*CoinUtil) SdkBatchTransactionStatus added in v0.1.5

func (u *CoinUtil) SdkBatchTransactionStatus(hashListString string) (string, error)

SDK 批量获取交易的转账状态,hash 列表和返回值,都只能用字符串,逗号隔开传递 @param hashListString 要批量查询的交易的 hash,用逗号拼接的字符串:"hash1,hash2,hash3" @return 批量的交易状态,它的顺序和 hashListString 是保持一致的: "status1,status2,status3"

func (*CoinUtil) SendRawTransaction added in v0.1.2

func (u *CoinUtil) SendRawTransaction(txHex string) (string, error)

对交易进行广播

func (*CoinUtil) SuggestGasPrice added in v0.1.2

func (u *CoinUtil) SuggestGasPrice() (string, error)

获取 gasPrice

type Erc20Balance added in v0.1.2

type Erc20Balance struct {
	ContractAddress string
	Balance         string
}

type Erc20Token

type Erc20Token struct {
	ContractAddress string // 代币合约地址
	Name            string // 代币名称
	Symbol          string // 代币符号
	Decimal         string // 代币精度
	ChainId         string // 链ID
	Balance         string // 代币余额
	TokenIcon       string // 代币图标
}

Erc20Token erc20 代币对象,定义了代币的基础信息:合约地址、代币名、代币符号、代币精度、代币余额

type Erc20TxParams

type Erc20TxParams struct {
	ToAddress string `json:"toAddress"`
	Amount    string `json:"amount"`
	Method    string `json:"method"`
}

type EthChain

type EthChain struct {
	RemoteRpcClient *ethclient.Client
	RpcClient       *rpc.Client
	// contains filtered or unexported fields
}

func GetConnection added in v0.1.2

func GetConnection(rpcUrl string) (*EthChain, error)

通过 rpcUrl, 获取 eth 的连接对象

func NewEthChain

func NewEthChain() *EthChain

func (*EthChain) Balance

func (e *EthChain) Balance(address string) (string, error)

@title 主网代币余额查询 @description 返回主网代币余额,decimal为代币精度 @auth 清欢 @param (walletAddress) (string) 合约名称,钱包地址 @return (string,error) 代币余额,错误信息

func (*EthChain) BatchTokenBalance added in v0.1.2

func (e *EthChain) BatchTokenBalance(contractList []string, address string) ([]string, error)

批量请求代币余额 @param contractList 批量查询的代币的合约地址数组 @param address 用户的钱包地址 @return 余额数组,顺序与传入的 contractList 是保持一致的 @throw 如果任意一个代币请求余额出错时,会抛出错误

func (*EthChain) BatchTransactionStatus added in v0.1.2

func (e *EthChain) BatchTransactionStatus(hashList []string) []string

批量获取交易的转账状态 @param hashList 要批量查询的交易的 hash 数组 @return 交易状态数组,它的顺序和 hashList 是保持一致的

func (*EthChain) BuildCallMethodTx

func (e *EthChain) BuildCallMethodTx(
	privateKey, contractAddress, abiStr, methodName string,
	opts *CallMethodOpts,
	erc20JsonParams string) (*BuildTxResult, error)

对合约进行调用

func (*EthChain) BuildCallMethodTxWithPayload

func (e *EthChain) BuildCallMethodTxWithPayload(
	privateKey, contractAddress, payload string, opts *CallMethodOpts) (*BuildTxResult, error)

func (*EthChain) BuildTransferTx

func (e *EthChain) BuildTransferTx(privateKey, toAddress string, opts *CallMethodOpts) (*BuildTxResult, error)

创建ETH转账交易

func (*EthChain) CallContractConstant

func (e *EthChain) CallContractConstant(out interface{}, contractAddress, abiStr, methodName string, opts *bind.CallOpts, params ...interface{}) error

func (*EthChain) CallContractConstantWithPayload

func (e *EthChain) CallContractConstantWithPayload(out interface{}, contractAddress, payload string, outputTypes abi.Arguments, opts *bind.CallOpts) error

func (*EthChain) Close

func (e *EthChain) Close()

func (*EthChain) ConnectRemote added in v0.1.2

func (e *EthChain) ConnectRemote(rpcUrl string) error

func (*EthChain) CreateRemote

func (e *EthChain) CreateRemote(rpcUrl string) (*EthChain, error)

func (*EthChain) Erc20TokenInfo

func (e *EthChain) Erc20TokenInfo(contractAddress string, walletAddress string) (*Erc20Token, error)

@title Erc20代币基础信息 @description 返回代币基础信息 @auth 清欢 @param (contractAddress, walletAddress) (string,string) 合约名称,钱包地址 @return (*Erc20Token,error) Erc20Token,错误信息

func (*EthChain) EstimateContractGasLimit

func (e *EthChain) EstimateContractGasLimit(

	fromAddress,
	contractAddress,
	abiStr,
	methodName string,
	erc20JsonParams string) (string, error)

erc20 代币 Transfer,Approve GasLimit 估计 var erc20TxParams Erc20TxParams

func (*EthChain) EstimateGasLimit

func (e *EthChain) EstimateGasLimit(fromAddress string, receiverAddress string, gasPrice string, amount string) (string, error)

Estimated gasLimit @param fromAddress The address where the transfer originated @param receiverAddress The address where the transfer will received @param gasPrice Previously acquired or entered by the user @param amount The amount transferred @return Estimate gasLimit, is a `String` converted from `Uint64`

func (*EthChain) FetchTransactionDetail added in v0.1.2

func (e *EthChain) FetchTransactionDetail(hashString string) (*TransactionDetail, error)

获取交易的详情 @param hashString 交易的 hash @return 详情对象,该对象无法提供 CID 信息

func (*EthChain) FetchTransactionStatus added in v0.1.2

func (e *EthChain) FetchTransactionStatus(hashString string) TransactionStatus

获取交易的状态 @param hashString 交易的 hash

func (*EthChain) LatestBlockNumber

func (e *EthChain) LatestBlockNumber() (int64, error)

获取最新区块高度

func (*EthChain) MethodFromPayload

func (e *EthChain) MethodFromPayload(abiStr string, payloadStr string) (*abi.Method, error)

func (*EthChain) MethodIdFromMethodStr

func (e *EthChain) MethodIdFromMethodStr(methodStr string) string

func (*EthChain) Nonce

func (e *EthChain) Nonce(spenderAddressHex string) (string, error)

获取账户nonce

func (*EthChain) PackParams

func (e *EthChain) PackParams(inputs abi.Arguments, args ...interface{}) (string, error)

不带 0x 前缀

func (*EthChain) RecoverSignerAddress

func (e *EthChain) RecoverSignerAddress(msg, sig string) (*common.Address, error)

func (*EthChain) RecoverSignerAddressFromMsgHash

func (e *EthChain) RecoverSignerAddressFromMsgHash(msgHash, sig string) (*common.Address, error)

func (*EthChain) SdkBatchTokenBalance added in v0.1.2

func (e *EthChain) SdkBatchTokenBalance(contractListString, address string) (string, error)

SDK 批量请求代币余额,因为 golang 导出的方法不支持数组,因此传参和返回都只能用字符串 @param contractListString 批量查询的代币的合约地址字符串,用逗号隔开,例如: "add1,add2,add3" @param address 用户的钱包地址 @return 余额列表,顺序与传入合约顺序是保持一致的,例如: "b1,b2,b3" @throw 如果任意一个代币请求余额出错时,会抛出错误

func (*EthChain) SdkBatchTransactionStatus added in v0.1.2

func (e *EthChain) SdkBatchTransactionStatus(hashListString string) string

SDK 批量获取交易的转账状态,hash 列表和返回值,都只能用字符串,逗号隔开传递 @param hashListString 要批量查询的交易的 hash,用逗号拼接的字符串:"hash1,hash2,hash3" @return 批量的交易状态,它的顺序和 hashListString 是保持一致的: "status1,status2,status3"

func (*EthChain) SendRawTransaction

func (e *EthChain) SendRawTransaction(txHex string) (string, error)

对交易进行广播

func (*EthChain) SignHashForMsg

func (e *EthChain) SignHashForMsg(data string) (string, error)

以太坊的 hash 专门在数据前面加上了一段话

func (*EthChain) SignMsg

func (e *EthChain) SignMsg(privateKey string, data string) (string, error)

使用私钥对信息进行签名

func (*EthChain) SignTransaction

func (e *EthChain) SignTransaction(tx *types.Transaction, privateKey string) (*BuildTxResult, error)

@title SignTransaction @description 对交易进行签名 @auth 清欢 @param (tx, privateKey) (*types.Transaction,string) 合约名称,钱包地址 @return (*BuildTxResult,error) 签名结果,失败

func (*EthChain) SuggestGasPrice

func (e *EthChain) SuggestGasPrice() (string, error)

获取标准gas价格

func (*EthChain) TokenBalance

func (e *EthChain) TokenBalance(contractAddress, address string) (string, error)

@title Erc20代币余额 @description 返回erc20代币余额 @auth 清欢 @param (contractAddress,walletAddress) 合约地址,钱包地址 @return (string,error) 余额,错误信息

func (*EthChain) TokenDecimal

func (e *EthChain) TokenDecimal(contractAddress string) (string, error)

@title Erc20代币精度 @description 返回代币精度 @auth 清欢 @param (contractAddress) 合约地址 @return (string,error) 代币精度,错误信息

func (*EthChain) TokenName

func (e *EthChain) TokenName(contractAddress string) (string, error)

@title Erc20代币名称 @description 返回代币名称 @auth 清欢 @param (contractAddress) 合约地址 @return (string,error) 名称,错误信息

func (*EthChain) TokenSymbol

func (e *EthChain) TokenSymbol(contractAddress string) (string, error)

@title Erc20代币符号 @description 返回代币符号 @auth 清欢 @param (contractAddress) 合约地址 @return (string,error) 符号,错误信息

func (*EthChain) TransactionByHash

func (e *EthChain) TransactionByHash(txHash string) (*TransactionByHashResult, error)

根据交易hash查询交易状态 TO-DO 返回更详细的信息,解析交易余额,交易动作

func (*EthChain) TransactionReceiptByHash

func (e *EthChain) TransactionReceiptByHash(txHash string) (*types.Receipt, error)

TransactionReceipt 是指交易的收据,每笔交易执行完 会产生一个收据,收据中包含交易的状态,交易的gas使用情况,交易执行是否成功的状态码等信息 交易收据属性列表: gasUsed: 交易执行时使用的gas数量 bloomFilter:交易信息日志检索 logInfoList: 交易日志集合 postTxState: 交易执行后的状态,1 表示成功,0表示失败

func (*EthChain) UnpackParams

func (e *EthChain) UnpackParams(out interface{}, inputs abi.Arguments, paramsStr string) error

func (*EthChain) WaitConfirm

func (e *EthChain) WaitConfirm(txHash string, interval time.Duration) *types.Receipt

type Jsonable

type Jsonable interface {
	// 将对象转为 json 字符串
	JsonString() string
}

支持 对象 和 json 字符串 相互转换

type SDKEnumInt added in v0.1.2

type SDKEnumInt = int

type SDKEnumString added in v0.1.2

type SDKEnumString = string

type TransactionByHashResult

type TransactionByHashResult struct {
	SignedTx    *types.Transaction
	From        common.Address
	IsPending   bool   // 交易是否处于Pending状态
	Status      string // 0: 交易失败, 1: 交易成功
	GasUsed     string // 实际花费gas
	BlockNumber string // 区块高度
}

type TransactionDetail added in v0.1.2

type TransactionDetail struct {
	// 交易在链上的哈希
	HashString string
	// 交易额
	Amount string
	// 交易手续费, Pending 时为预估手续费,交易结束时为真实手续费
	EstimateFees string
	// 转账人的地址
	FromAddress string
	// 收款人的地址
	ToAddress string
	// 交易状态 枚举常量
	// 0: TransactionStatusNone;
	// 1: TransactionStatusPending;
	// 2: TransactionStatusSuccess;
	// 3: TransactionStatusFailure;
	Status TransactionStatus
	// 交易完成时间, 如果在 Pending 中,为 0
	FinishTimestamp int64
	// 失败描述
	FailureMessage string
}

可以从链上获取的转账详情信息 客户端的详情展示还需要 FromCID, ToCID, CreateTimestamp, Transfer(转出/收入), CoinType, Decimal 这些信息需要客户端自己维护

func NewTransactionDetailWithJsonString added in v0.1.2

func NewTransactionDetailWithJsonString(s string) *TransactionDetail

func (*TransactionDetail) JsonString added in v0.1.2

func (i *TransactionDetail) JsonString() string

type TransactionStatus added in v0.1.2

type TransactionStatus = SDKEnumInt
const (
	TransactionStatusNone    TransactionStatus = 0
	TransactionStatusPending TransactionStatus = 1
	TransactionStatusSuccess TransactionStatus = 2
	TransactionStatusFailure TransactionStatus = 3
)

type UrlParam

type UrlParam struct {
	RpcUrl string
	WsUrl  string
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL