mobile: better api for java users (#21580)
* (mobile): Adds string representations for types * mobile: better interfaces add stringer to types Co-authored-by: sarath <sarath@melvault.com>
This commit is contained in:
committed by
GitHub
parent
9a39c6bcb1
commit
2c097bb7a2
@ -87,6 +87,11 @@ func (h *Hash) GetHex() string {
|
||||
return h.hash.Hex()
|
||||
}
|
||||
|
||||
// String implements Stringer interface for printable representation of the hash.
|
||||
func (h *Hash) String() string {
|
||||
return h.GetHex()
|
||||
}
|
||||
|
||||
// Hashes represents a slice of hashes.
|
||||
type Hashes struct{ hashes []common.Hash }
|
||||
|
||||
@ -188,6 +193,11 @@ func (a *Address) GetHex() string {
|
||||
return a.address.Hex()
|
||||
}
|
||||
|
||||
// String returns a printable representation of the address.
|
||||
func (a *Address) String() string {
|
||||
return a.GetHex()
|
||||
}
|
||||
|
||||
// Addresses represents a slice of addresses.
|
||||
type Addresses struct{ addresses []common.Address }
|
||||
|
||||
|
@ -90,6 +90,22 @@ func NewNodeConfig() *NodeConfig {
|
||||
return &config
|
||||
}
|
||||
|
||||
// AddBootstrapNode adds an additional bootstrap node to the node config.
|
||||
func (conf *NodeConfig) AddBootstrapNode(node *Enode) {
|
||||
conf.BootstrapNodes.Append(node)
|
||||
}
|
||||
|
||||
// EncodeJSON encodes a NodeConfig into a JSON data dump.
|
||||
func (conf *NodeConfig) EncodeJSON() (string, error) {
|
||||
data, err := json.Marshal(conf)
|
||||
return string(data), err
|
||||
}
|
||||
|
||||
// String returns a printable representation of the node config.
|
||||
func (conf *NodeConfig) String() string {
|
||||
return encodeOrError(conf)
|
||||
}
|
||||
|
||||
// Node represents a Geth Ethereum node instance.
|
||||
type Node struct {
|
||||
node *node.Node
|
||||
|
@ -28,6 +28,20 @@ import (
|
||||
"github.com/ethereum/go-ethereum/rlp"
|
||||
)
|
||||
|
||||
type jsonEncoder interface {
|
||||
EncodeJSON() (string, error)
|
||||
}
|
||||
|
||||
// encodeOrError tries to encode the object into json.
|
||||
// If the encoding fails the resulting error is returned.
|
||||
func encodeOrError(encoder jsonEncoder) string {
|
||||
enc, err := encoder.EncodeJSON()
|
||||
if err != nil {
|
||||
return err.Error()
|
||||
}
|
||||
return enc
|
||||
}
|
||||
|
||||
// A Nonce is a 64-bit hash which proves (combined with the mix-hash) that
|
||||
// a sufficient amount of computation has been carried out on a block.
|
||||
type Nonce struct {
|
||||
@ -44,6 +58,11 @@ func (n *Nonce) GetHex() string {
|
||||
return fmt.Sprintf("0x%x", n.nonce[:])
|
||||
}
|
||||
|
||||
// String returns a printable representation of the nonce.
|
||||
func (n *Nonce) String() string {
|
||||
return n.GetHex()
|
||||
}
|
||||
|
||||
// Bloom represents a 256 bit bloom filter.
|
||||
type Bloom struct {
|
||||
bloom types.Bloom
|
||||
@ -59,6 +78,11 @@ func (b *Bloom) GetHex() string {
|
||||
return fmt.Sprintf("0x%x", b.bloom[:])
|
||||
}
|
||||
|
||||
// String returns a printable representation of the bloom filter.
|
||||
func (b *Bloom) String() string {
|
||||
return b.GetHex()
|
||||
}
|
||||
|
||||
// Header represents a block header in the Ethereum blockchain.
|
||||
type Header struct {
|
||||
header *types.Header
|
||||
@ -97,6 +121,11 @@ func (h *Header) EncodeJSON() (string, error) {
|
||||
return string(data), err
|
||||
}
|
||||
|
||||
// String returns a printable representation of the header.
|
||||
func (h *Header) String() string {
|
||||
return encodeOrError(h)
|
||||
}
|
||||
|
||||
func (h *Header) GetParentHash() *Hash { return &Hash{h.header.ParentHash} }
|
||||
func (h *Header) GetUncleHash() *Hash { return &Hash{h.header.UncleHash} }
|
||||
func (h *Header) GetCoinbase() *Address { return &Address{h.header.Coinbase} }
|
||||
@ -168,6 +197,11 @@ func (b *Block) EncodeJSON() (string, error) {
|
||||
return string(data), err
|
||||
}
|
||||
|
||||
// String returns a printable representation of the block.
|
||||
func (b *Block) String() string {
|
||||
return encodeOrError(b)
|
||||
}
|
||||
|
||||
func (b *Block) GetParentHash() *Hash { return &Hash{b.block.ParentHash()} }
|
||||
func (b *Block) GetUncleHash() *Hash { return &Hash{b.block.UncleHash()} }
|
||||
func (b *Block) GetCoinbase() *Address { return &Address{b.block.Coinbase()} }
|
||||
@ -244,6 +278,11 @@ func (tx *Transaction) EncodeJSON() (string, error) {
|
||||
return string(data), err
|
||||
}
|
||||
|
||||
// String returns a printable representation of the transaction.
|
||||
func (tx *Transaction) String() string {
|
||||
return encodeOrError(tx)
|
||||
}
|
||||
|
||||
func (tx *Transaction) GetData() []byte { return tx.tx.Data() }
|
||||
func (tx *Transaction) GetGas() int64 { return int64(tx.tx.Gas()) }
|
||||
func (tx *Transaction) GetGasPrice() *BigInt { return &BigInt{tx.tx.GasPrice()} }
|
||||
@ -336,6 +375,11 @@ func (r *Receipt) EncodeJSON() (string, error) {
|
||||
return string(data), err
|
||||
}
|
||||
|
||||
// String returns a printable representation of the receipt.
|
||||
func (r *Receipt) String() string {
|
||||
return encodeOrError(r)
|
||||
}
|
||||
|
||||
func (r *Receipt) GetStatus() int { return int(r.receipt.Status) }
|
||||
func (r *Receipt) GetPostState() []byte { return r.receipt.PostState }
|
||||
func (r *Receipt) GetCumulativeGasUsed() int64 { return int64(r.receipt.CumulativeGasUsed) }
|
||||
|
Reference in New Issue
Block a user