Add types and .

This commit is contained in:
Nick Johnson 2018-10-15 13:24:58 +01:00
parent ed02332148
commit cd53e6083a

View file

@ -62,6 +62,40 @@ func (h *HexBytes) UnmarshalGraphQL(input interface{}) error {
return err return err
} }
*h = HexBytes{data} *h = HexBytes{data}
default:
err = fmt.Errorf("Unexpected type for HexBytes: %v", input)
}
return err
}
type Bytes32 struct {
common.Hash
}
func (_ Bytes32) ImplementsGraphQLType(name string) bool { return name == "Bytes32" }
func (b *Bytes32) UnmarshalGraphQL(input interface{}) error {
var err error
switch input := input.(type) {
case string:
*b = Bytes32{common.HexToHash(input)}
default:
err = fmt.Errorf("Unexpected type for Hash: %v", input)
}
return err
}
type Address struct {
common.Address
}
func (h Address) ImplementsGraphQLType(name string) bool { return name == "Address" }
func (h *Address) UnmarshalGraphQL(input interface{}) error {
var err error
switch input := input.(type) {
case string:
*h = Address{common.HexToAddress(input)}
default: default:
err = fmt.Errorf("Unexpected type for Hash: %v", input) err = fmt.Errorf("Unexpected type for Hash: %v", input)
} }
@ -109,8 +143,8 @@ func (a *Account) getState(ctx context.Context) (*state.StateDB, error) {
return state, err return state, err
} }
func (a *Account) Address(ctx context.Context) (HexBytes, error) { func (a *Account) Address(ctx context.Context) (Address, error) {
return HexBytes{a.address.Bytes()}, nil return Address{a.address}, nil
} }
func (a *Account) Balance(ctx context.Context) (BigNum, error) { func (a *Account) Balance(ctx context.Context) (BigNum, error) {
@ -141,16 +175,16 @@ func (a *Account) Code(ctx context.Context) (HexBytes, error) {
} }
type StorageSlotArgs struct { type StorageSlotArgs struct {
Slot HexBytes Slot Bytes32
} }
func (a *Account) Storage(ctx context.Context, args StorageSlotArgs) (HexBytes, error) { func (a *Account) Storage(ctx context.Context, args StorageSlotArgs) (Bytes32, error) {
state, err := a.getState(ctx) state, err := a.getState(ctx)
if err != nil { if err != nil {
return HexBytes{}, err return Bytes32{}, err
} }
return HexBytes{state.GetState(a.address, common.BytesToHash(args.Slot.Bytes)).Bytes()}, nil return Bytes32{state.GetState(a.address, args.Slot.Hash)}, nil
} }
type Log struct { type Log struct {
@ -171,10 +205,10 @@ func (l *Log) Account(ctx context.Context, args BlockNumberArgs) *Account {
} }
} }
func (l *Log) Topics(ctx context.Context) []*HexBytes { func (l *Log) Topics(ctx context.Context) []*Bytes32 {
ret := make([]*HexBytes, 0, len(l.log.Topics)) ret := make([]*Bytes32, 0, len(l.log.Topics))
for _, topic := range l.log.Topics { for _, topic := range l.log.Topics {
ret = append(ret, &HexBytes{topic.Bytes()}) ret = append(ret, &Bytes32{topic})
} }
return ret return ret
} }
@ -255,8 +289,8 @@ func (t *Transaction) resolve(ctx context.Context) (*types.Transaction, error) {
return t.tx, nil return t.tx, nil
} }
func (tx *Transaction) Hash(ctx context.Context) HexBytes { func (tx *Transaction) Hash(ctx context.Context) Bytes32 {
return HexBytes{tx.hash.Bytes()} return Bytes32{tx.hash}
} }
func (t *Transaction) Data(ctx context.Context) (HexBytes, error) { func (t *Transaction) Data(ctx context.Context) (HexBytes, error) {
@ -438,15 +472,15 @@ func (b *Block) Number(ctx context.Context) (int32, error) {
return int32(*b.num), nil return int32(*b.num), nil
} }
func (b *Block) Hash(ctx context.Context) (HexBytes, error) { func (b *Block) Hash(ctx context.Context) (Bytes32, error) {
if b.hash == (common.Hash{}) { if b.hash == (common.Hash{}) {
block, err := b.resolve(ctx) block, err := b.resolve(ctx)
if err != nil { if err != nil {
return HexBytes{}, err return Bytes32{}, err
} }
b.hash = block.Hash() b.hash = block.Hash()
} }
return HexBytes{b.hash.Bytes()}, nil return Bytes32{b.hash}, nil
} }
func (b *Block) GasLimit(ctx context.Context) (int32, error) { func (b *Block) GasLimit(ctx context.Context) (int32, error) {
@ -516,44 +550,44 @@ func (b *Block) Nonce(ctx context.Context) (BigNum, error) {
return BigNum{i}, nil return BigNum{i}, nil
} }
func (b *Block) MixDigest(ctx context.Context) (HexBytes, error) { func (b *Block) MixDigest(ctx context.Context) (Bytes32, error) {
block, err := b.resolve(ctx) block, err := b.resolve(ctx)
if err != nil { if err != nil {
return HexBytes{}, err return Bytes32{}, err
} }
return HexBytes{block.MixDigest().Bytes()}, nil return Bytes32{block.MixDigest()}, nil
} }
func (b *Block) Root(ctx context.Context) (HexBytes, error) { func (b *Block) Root(ctx context.Context) (Bytes32, error) {
block, err := b.resolve(ctx) block, err := b.resolve(ctx)
if err != nil { if err != nil {
return HexBytes{}, err return Bytes32{}, err
} }
return HexBytes{block.Root().Bytes()}, nil return Bytes32{block.Root()}, nil
} }
func (b *Block) TxHash(ctx context.Context) (HexBytes, error) { func (b *Block) TxHash(ctx context.Context) (Bytes32, error) {
block, err := b.resolve(ctx) block, err := b.resolve(ctx)
if err != nil { if err != nil {
return HexBytes{}, err return Bytes32{}, err
} }
return HexBytes{block.TxHash().Bytes()}, nil return Bytes32{block.TxHash()}, nil
} }
func (b *Block) ReceiptHash(ctx context.Context) (HexBytes, error) { func (b *Block) ReceiptHash(ctx context.Context) (Bytes32, error) {
block, err := b.resolve(ctx) block, err := b.resolve(ctx)
if err != nil { if err != nil {
return HexBytes{}, err return Bytes32{}, err
} }
return HexBytes{block.ReceiptHash().Bytes()}, nil return Bytes32{block.ReceiptHash()}, nil
} }
func (b *Block) UncleHash(ctx context.Context) (HexBytes, error) { func (b *Block) UncleHash(ctx context.Context) (Bytes32, error) {
block, err := b.resolve(ctx) block, err := b.resolve(ctx)
if err != nil { if err != nil {
return HexBytes{}, err return Bytes32{}, err
} }
return HexBytes{block.UncleHash().Bytes()}, nil return Bytes32{block.UncleHash()}, nil
} }
func (b *Block) Extra(ctx context.Context) (HexBytes, error) { func (b *Block) Extra(ctx context.Context) (HexBytes, error) {
@ -639,7 +673,7 @@ type Query struct {
type BlockArgs struct { type BlockArgs struct {
Number *int32 Number *int32
Hash *HexBytes Hash *Bytes32
} }
func (q *Query) Block(ctx context.Context, args BlockArgs) (*Block, error) { func (q *Query) Block(ctx context.Context, args BlockArgs) (*Block, error) {
@ -653,7 +687,7 @@ func (q *Query) Block(ctx context.Context, args BlockArgs) (*Block, error) {
} else if args.Hash != nil { } else if args.Hash != nil {
block = &Block{ block = &Block{
node: q.node, node: q.node,
hash: common.BytesToHash(args.Hash.Bytes), hash: args.Hash.Hash,
} }
} else { } else {
num := rpc.LatestBlockNumber num := rpc.LatestBlockNumber
@ -674,7 +708,7 @@ func (q *Query) Block(ctx context.Context, args BlockArgs) (*Block, error) {
} }
type AccountArgs struct { type AccountArgs struct {
Address HexBytes Address Address
BlockNumber *int32 BlockNumber *int32
} }
@ -686,19 +720,19 @@ func (q *Query) Account(ctx context.Context, args AccountArgs) *Account {
return &Account{ return &Account{
node: q.node, node: q.node,
address: common.BytesToAddress(args.Address.Bytes), address: args.Address.Address,
blockNumber: blockNumber, blockNumber: blockNumber,
} }
} }
type TransactionArgs struct { type TransactionArgs struct {
Hash HexBytes Hash Bytes32
} }
func (q *Query) Transaction(ctx context.Context, args TransactionArgs) (*Transaction, error) { func (q *Query) Transaction(ctx context.Context, args TransactionArgs) (*Transaction, error) {
tx := &Transaction{ tx := &Transaction{
node: q.node, node: q.node,
hash: common.BytesToHash(args.Hash.Bytes), hash: args.Hash.Hash,
} }
// Resolve the transaction; if it doesn't exist, return nil. // Resolve the transaction; if it doesn't exist, return nil.
@ -715,6 +749,8 @@ func NewHandler(n *node.Node) (http.Handler, error) {
q := Query{n} q := Query{n}
s := ` s := `
scalar Bytes32
scalar Address
scalar HexBytes scalar HexBytes
scalar BigNum scalar BigNum
@ -723,17 +759,17 @@ func NewHandler(n *node.Node) (http.Handler, error) {
} }
type Account { type Account {
address: HexBytes! address: Address!
balance: BigNum! balance: BigNum!
nonce: Int! nonce: Int!
code: HexBytes! code: HexBytes!
storage(slot: HexBytes!): HexBytes! storage(slot: Bytes32!): Bytes32!
} }
type Log { type Log {
transaction: Transaction! transaction: Transaction!
account(block: Int): Account! account(block: Int): Account!
topics: [HexBytes]! topics: [Bytes32]!
data: HexBytes! data: HexBytes!
} }
@ -746,7 +782,7 @@ func NewHandler(n *node.Node) (http.Handler, error) {
} }
type Transaction { type Transaction {
hash: HexBytes! hash: Bytes32!
data: HexBytes! data: HexBytes!
gas: Int! gas: Int!
gasPrice: BigNum! gasPrice: BigNum!
@ -761,18 +797,18 @@ func NewHandler(n *node.Node) (http.Handler, error) {
type Block { type Block {
number: Int! number: Int!
hash: HexBytes! hash: Bytes32!
gasLimit: Int! gasLimit: Int!
gasUsed: Int! gasUsed: Int!
parent: Block parent: Block
difficulty: BigNum! difficulty: BigNum!
time: BigNum! time: BigNum!
nonce: BigNum! nonce: BigNum!
mixDigest: HexBytes! mixDigest: Bytes32!
root: HexBytes! root: Bytes32!
txHash: HexBytes! txHash: Bytes32!
receiptHash: HexBytes! receiptHash: Bytes32!
uncleHash: HexBytes! uncleHash: Bytes32!
extra: HexBytes! extra: HexBytes!
totalDifficulty: BigNum! totalDifficulty: BigNum!
coinbase(block: Int): Account! coinbase(block: Int): Account!
@ -781,9 +817,9 @@ func NewHandler(n *node.Node) (http.Handler, error) {
} }
type Query { type Query {
block(number: Int, hash: HexBytes): Block block(number: Int, hash: Bytes32): Block
account(address: HexBytes!, blockNumber: Int): Account! account(address: Address!, blockNumber: Int): Account!
transaction(hash: HexBytes!): Transaction transaction(hash: Bytes32!): Transaction
} }
` `
schema, err := graphql.ParseSchema(s, &q) schema, err := graphql.ParseSchema(s, &q)