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:
Marius van der Wijden
2020-09-21 16:33:35 +02:00
committed by GitHub
parent 9a39c6bcb1
commit 2c097bb7a2
5 changed files with 93 additions and 0 deletions

View File

@ -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) }