Replace BigNum with hexutil.Big

This commit is contained in:
Nick Johnson 2018-10-16 13:48:23 +01:00
parent 59e2a7f2c8
commit 8790e18ae7
2 changed files with 57 additions and 62 deletions

View file

@ -204,6 +204,27 @@ func (b *Big) String() string {
return EncodeBig(b.ToInt()) return EncodeBig(b.ToInt())
} }
func (b Big) ImplementsGraphQLType(name string) bool { return name == "BigInt" }
func (b *Big) UnmarshalGraphQL(input interface{}) error {
var err error
switch input := input.(type) {
case string:
return b.UnmarshalText([]byte(input))
case int32:
var num big.Int
num.SetInt64(int64(input))
*b = Big(num)
default:
err = fmt.Errorf("Unexpected type for BigInt: %v", input)
}
return err
}
func (b Big) MarshalJSON() ([]byte, error) {
return strconv.AppendQuote(nil, (*big.Int)(&b).Text(10)), nil
}
// Uint64 marshals/unmarshals as a JSON string with 0x prefix. // Uint64 marshals/unmarshals as a JSON string with 0x prefix.
// The zero value marshals as "0x0". // The zero value marshals as "0x0".
type Uint64 uint64 type Uint64 uint64

View file

@ -22,7 +22,6 @@ import (
"math/big" "math/big"
"net" "net"
"net/http" "net/http"
"strconv"
"time" "time"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
@ -84,31 +83,6 @@ func (h *Address) UnmarshalGraphQL(input interface{}) error {
return err return err
} }
type BigNum struct {
*big.Int
}
func (bn BigNum) ImplementsGraphQLType(name string) bool { return name == "BigNum" }
func (bn *BigNum) UnmarshalGraphQL(input interface{}) error {
var err error
switch input := input.(type) {
case string:
i := big.NewInt(0)
i.SetString(input, 10)
*bn = BigNum{i}
case int32:
*bn = BigNum{big.NewInt(int64(input))}
default:
err = fmt.Errorf("Unexpected type for Hash: %v", input)
}
return err
}
func (bn BigNum) MarshalJSON() ([]byte, error) {
return strconv.AppendQuote(nil, bn.Text(10)), nil
}
type Account struct { type Account struct {
node *node.Node node *node.Node
address common.Address address common.Address
@ -129,13 +103,13 @@ func (a *Account) Address(ctx context.Context) (Address, error) {
return Address{a.address}, nil return Address{a.address}, nil
} }
func (a *Account) Balance(ctx context.Context) (BigNum, error) { func (a *Account) Balance(ctx context.Context) (hexutil.Big, error) {
state, err := a.getState(ctx) state, err := a.getState(ctx)
if err != nil { if err != nil {
return BigNum{}, err return hexutil.Big{}, err
} }
return BigNum{state.GetBalance(a.address)}, nil return hexutil.Big(*state.GetBalance(a.address)), nil
} }
func (a *Account) TransactionCount(ctx context.Context) (int32, error) { func (a *Account) TransactionCount(ctx context.Context) (int32, error) {
@ -253,20 +227,20 @@ func (t *Transaction) Gas(ctx context.Context) (int32, error) {
return int32(tx.Gas()), nil return int32(tx.Gas()), nil
} }
func (t *Transaction) GasPrice(ctx context.Context) (BigNum, error) { func (t *Transaction) GasPrice(ctx context.Context) (hexutil.Big, error) {
tx, err := t.resolve(ctx) tx, err := t.resolve(ctx)
if err != nil || tx == nil { if err != nil || tx == nil {
return BigNum{}, err return hexutil.Big{}, err
} }
return BigNum{tx.GasPrice()}, nil return hexutil.Big(*tx.GasPrice()), nil
} }
func (t *Transaction) Value(ctx context.Context) (BigNum, error) { func (t *Transaction) Value(ctx context.Context) (hexutil.Big, error) {
tx, err := t.resolve(ctx) tx, err := t.resolve(ctx)
if err != nil || tx == nil { if err != nil || tx == nil {
return BigNum{}, err return hexutil.Big{}, err
} }
return BigNum{tx.Value()}, nil return hexutil.Big(*tx.Value()), nil
} }
func (t *Transaction) Nonce(ctx context.Context) (int32, error) { func (t *Transaction) Nonce(ctx context.Context) (int32, error) {
@ -524,30 +498,30 @@ func (b *Block) Parent(ctx context.Context) (*Block, error) {
return nil, nil return nil, nil
} }
func (b *Block) Difficulty(ctx context.Context) (BigNum, error) { func (b *Block) Difficulty(ctx context.Context) (hexutil.Big, error) {
block, err := b.resolve(ctx) block, err := b.resolve(ctx)
if err != nil { if err != nil {
return BigNum{}, err return hexutil.Big{}, err
} }
return BigNum{block.Difficulty()}, nil return hexutil.Big(*block.Difficulty()), nil
} }
func (b *Block) Timestamp(ctx context.Context) (BigNum, error) { func (b *Block) Timestamp(ctx context.Context) (hexutil.Big, error) {
block, err := b.resolve(ctx) block, err := b.resolve(ctx)
if err != nil { if err != nil {
return BigNum{}, err return hexutil.Big{}, err
} }
return BigNum{block.Time()}, nil return hexutil.Big(*block.Time()), nil
} }
func (b *Block) Nonce(ctx context.Context) (BigNum, error) { func (b *Block) Nonce(ctx context.Context) (hexutil.Big, error) {
block, err := b.resolve(ctx) block, err := b.resolve(ctx)
if err != nil { if err != nil {
return BigNum{}, err return hexutil.Big{}, err
} }
i := new(big.Int) i := new(big.Int)
i.SetUint64(block.Nonce()) i.SetUint64(block.Nonce())
return BigNum{i}, nil return hexutil.Big(*i), nil
} }
func (b *Block) MixHash(ctx context.Context) (Bytes32, error) { func (b *Block) MixHash(ctx context.Context) (Bytes32, error) {
@ -632,22 +606,22 @@ func (b *Block) LogsBloom(ctx context.Context) (hexutil.Bytes, error) {
return hexutil.Bytes(block.Bloom().Bytes()), nil return hexutil.Bytes(block.Bloom().Bytes()), nil
} }
func (b *Block) TotalDifficulty(ctx context.Context) (BigNum, error) { func (b *Block) TotalDifficulty(ctx context.Context) (hexutil.Big, error) {
h := b.hash h := b.hash
if h == (common.Hash{}) { if h == (common.Hash{}) {
block, err := b.resolve(ctx) block, err := b.resolve(ctx)
if err != nil { if err != nil {
return BigNum{}, err return hexutil.Big{}, err
} }
h = block.Hash() h = block.Hash()
} }
be, err := getBackend(b.node) be, err := getBackend(b.node)
if err != nil { if err != nil {
return BigNum{}, err return hexutil.Big{}, err
} }
return BigNum{be.GetTd(h)}, nil return hexutil.Big(*be.GetTd(h)), nil
} }
type BlockNumberArgs struct { type BlockNumberArgs struct {
@ -877,8 +851,8 @@ type CallData struct {
From *Address From *Address
To *Address To *Address
Gas *int32 Gas *int32
GasPrice *BigNum GasPrice *hexutil.Big
Value *BigNum Value *hexutil.Big
Data *hexutil.Bytes Data *hexutil.Bytes
BlockNumber *int32 BlockNumber *int32
} }
@ -914,10 +888,10 @@ func convertCallData(data CallData) (ethapi.CallArgs, rpc.BlockNumber) {
callArgs.Gas = hexutil.Uint64(*data.Gas) callArgs.Gas = hexutil.Uint64(*data.Gas)
} }
if data.GasPrice != nil { if data.GasPrice != nil {
callArgs.GasPrice = hexutil.Big(*data.GasPrice.Int) callArgs.GasPrice = *data.GasPrice
} }
if data.Value != nil { if data.Value != nil {
callArgs.Value = hexutil.Big(*data.Value.Int) callArgs.Value = *data.Value
} }
if data.Data != nil { if data.Data != nil {
callArgs.Data = *data.Data callArgs.Data = *data.Data
@ -970,7 +944,7 @@ func NewHandler(n *node.Node) (http.Handler, error) {
scalar Bytes32 scalar Bytes32
scalar Address scalar Address
scalar Bytes scalar Bytes
scalar BigNum scalar BigInt
schema { schema {
query: Query query: Query
@ -979,7 +953,7 @@ func NewHandler(n *node.Node) (http.Handler, error) {
type Account { type Account {
address: Address! address: Address!
balance: BigNum! balance: BigInt!
transactionCount: Int! transactionCount: Int!
code: Bytes! code: Bytes!
storage(slot: Bytes32!): Bytes32! storage(slot: Bytes32!): Bytes32!
@ -999,8 +973,8 @@ func NewHandler(n *node.Node) (http.Handler, error) {
index: Int index: Int
from(block: Int): Account! from(block: Int): Account!
to(block: Int): Account to(block: Int): Account
value: BigNum! value: BigInt!
gasPrice: BigNum! gasPrice: BigInt!
gas: Int! gas: Int!
inputData: Bytes! inputData: Bytes!
block: Block block: Block
@ -1016,7 +990,7 @@ func NewHandler(n *node.Node) (http.Handler, error) {
number: Int! number: Int!
hash: Bytes32! hash: Bytes32!
parent: Block parent: Block
nonce: BigNum! nonce: BigInt!
transactionsRoot: Bytes32! transactionsRoot: Bytes32!
transactionCount: Int! transactionCount: Int!
stateRoot: Bytes32! stateRoot: Bytes32!
@ -1025,11 +999,11 @@ func NewHandler(n *node.Node) (http.Handler, error) {
extraData: Bytes! extraData: Bytes!
gasLimit: Int! gasLimit: Int!
gasUsed: Int! gasUsed: Int!
timestamp: BigNum! timestamp: BigInt!
logsBloom: Bytes! logsBloom: Bytes!
mixHash: Bytes32! mixHash: Bytes32!
difficulty: BigNum! difficulty: BigInt!
totalDifficulty: BigNum! totalDifficulty: BigInt!
ommerCount: Int! ommerCount: Int!
ommers: [Block]! ommers: [Block]!
ommerAt(index: Int!): Block ommerAt(index: Int!): Block
@ -1042,8 +1016,8 @@ func NewHandler(n *node.Node) (http.Handler, error) {
from: Address from: Address
to: Address to: Address
gas: Int gas: Int
gasPrice: BigNum gasPrice: BigInt
value: BigNum value: BigInt
data: Bytes data: Bytes
blockNumber: Int blockNumber: Int
} }