Skip to content
This repository has been archived by the owner on Oct 11, 2024. It is now read-only.

Use strings instead of custom scalars in GraphQL schema when possible #906

Merged
merged 3 commits into from
Aug 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions common/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,21 @@ type Metadata struct {
EthRPCRequestsSentInCurrentUTCDay int
StartOfCurrentUTCDay time.Time
}

// HexToBytes converts the the given hex string (with or without the "0x" prefix)
// to a slice of bytes. If the string is "0x" it returns nil.
func HexToBytes(s string) []byte {
if s == "0x" {
return nil
}
return common.FromHex(s)
}

// BytesToHex converts the given slice of bytes to a hex string with a "0x" prefix.
// If b is nil or has length 0, it returns "0x".
func BytesToHex(b []byte) string {
if len(b) == 0 {
return "0x"
}
return common.ToHex(b)
}
11 changes: 6 additions & 5 deletions constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ const TestChainID = 1337
// GanacheEndpoint specifies the Ganache test Ethereum node JSON RPC endpoint used in tests
const GanacheEndpoint = "http://localhost:8545"

// NullAddress is an Ethereum address with all zeroes.
var NullAddress = common.HexToAddress("0x0000000000000000000000000000000000000000")

// NullBytes is an empty byte array
var NullBytes = common.FromHex("0x")
var (
// NullAddress is an Ethereum address with all zeroes.
NullAddress = common.HexToAddress("0x0000000000000000000000000000000000000000")
// NullBytes is an empty byte array
NullBytes = common.FromHex("0x")
)

var (
// GanacheAccount0 is the first account exposed on the Ganache test Ethereum node
Expand Down
7 changes: 5 additions & 2 deletions graphql/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ const (
}
}`

orderQuery = `query Order($hash: Hash!) {
orderQuery = `query Order($hash: String!) {
order(hash: $hash) {
hash
chainId
Expand Down Expand Up @@ -188,6 +188,9 @@ func (c *Client) GetOrder(ctx context.Context, hash common.Hash) (*OrderWithMeta
if err := c.Run(ctx, req, &resp); err != nil {
return nil, err
}
if resp.Order == nil {
return nil, nil
}
return orderWithMetadataFromGQLType(resp.Order), nil
}

Expand Down Expand Up @@ -242,7 +245,7 @@ func (c *Client) GetStats(ctx context.Context) (*Stats, error) {
if err := c.Run(ctx, req, &resp); err != nil {
return nil, err
}
return statsFromGQLType(resp.Stats), nil
return statsFromGQLType(resp.Stats)
}

func (c *Client) RawQuery(ctx context.Context, query string, response interface{}) error {
Expand Down
93 changes: 50 additions & 43 deletions graphql/client/conversions.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package client

import (
"time"

"github.com/0xProject/0x-mesh/graphql/gqltypes"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/math"
)

func addOrdersResultsFromGQLType(results *gqltypes.AddOrdersResults) *AddOrdersResults {
Expand Down Expand Up @@ -30,29 +33,29 @@ func acceptedOrderResultsFromGQLType(results []*gqltypes.AcceptedOrderResult) []
func rejectedOrderResultFromGQLType(result *gqltypes.RejectedOrderResult) *RejectedOrderResult {
var hash *common.Hash
if result.Hash != nil {
h := common.Hash(*result.Hash)
h := common.HexToHash(*result.Hash)
hash = &h
}
return &RejectedOrderResult{
Hash: hash,
Order: &Order{
ChainID: gqltypes.BigNumberToBigInt(result.Order.ChainID),
ExchangeAddress: common.Address(result.Order.ExchangeAddress),
MakerAddress: common.Address(result.Order.MakerAddress),
MakerAssetData: result.Order.MakerAssetData,
MakerFeeAssetData: result.Order.MakerFeeAssetData,
MakerAssetAmount: gqltypes.BigNumberToBigInt(result.Order.MakerAssetAmount),
MakerFee: gqltypes.BigNumberToBigInt(result.Order.MakerFee),
TakerAddress: common.Address(result.Order.TakerAddress),
TakerAssetData: result.Order.TakerAssetData,
TakerFeeAssetData: result.Order.TakerFeeAssetData,
TakerAssetAmount: gqltypes.BigNumberToBigInt(result.Order.TakerAssetAmount),
TakerFee: gqltypes.BigNumberToBigInt(result.Order.TakerFee),
SenderAddress: common.Address(result.Order.SenderAddress),
FeeRecipientAddress: common.Address(result.Order.FeeRecipientAddress),
ExpirationTimeSeconds: gqltypes.BigNumberToBigInt(result.Order.ExpirationTimeSeconds),
Salt: gqltypes.BigNumberToBigInt(result.Order.Salt),
Signature: result.Order.Signature,
ChainID: math.MustParseBig256(result.Order.ChainID),
ExchangeAddress: common.HexToAddress(result.Order.ExchangeAddress),
MakerAddress: common.HexToAddress(result.Order.MakerAddress),
MakerAssetData: common.FromHex(result.Order.MakerAssetData),
MakerFeeAssetData: common.FromHex(result.Order.MakerFeeAssetData),
MakerAssetAmount: math.MustParseBig256(result.Order.MakerAssetAmount),
MakerFee: math.MustParseBig256(result.Order.MakerFee),
TakerAddress: common.HexToAddress(result.Order.TakerAddress),
TakerAssetData: common.FromHex(result.Order.TakerAssetData),
TakerFeeAssetData: common.FromHex(result.Order.TakerFeeAssetData),
TakerAssetAmount: math.MustParseBig256(result.Order.TakerAssetAmount),
TakerFee: math.MustParseBig256(result.Order.TakerFee),
SenderAddress: common.HexToAddress(result.Order.SenderAddress),
FeeRecipientAddress: common.HexToAddress(result.Order.FeeRecipientAddress),
ExpirationTimeSeconds: math.MustParseBig256(result.Order.ExpirationTimeSeconds),
Salt: math.MustParseBig256(result.Order.Salt),
Signature: common.FromHex(result.Order.Signature),
},
Code: result.Code,
Message: result.Message,
Expand All @@ -69,25 +72,25 @@ func rejectedOrderResultsFromGQLType(results []*gqltypes.RejectedOrderResult) []

func orderWithMetadataFromGQLType(order *gqltypes.OrderWithMetadata) *OrderWithMetadata {
return &OrderWithMetadata{
Hash: common.Hash(order.Hash),
ChainID: gqltypes.BigNumberToBigInt(order.ChainID),
ExchangeAddress: common.Address(order.ExchangeAddress),
MakerAddress: common.Address(order.MakerAddress),
MakerAssetData: order.MakerAssetData,
MakerFeeAssetData: order.MakerFeeAssetData,
MakerAssetAmount: gqltypes.BigNumberToBigInt(order.MakerAssetAmount),
MakerFee: gqltypes.BigNumberToBigInt(order.MakerFee),
TakerAddress: common.Address(order.TakerAddress),
TakerAssetData: order.TakerAssetData,
TakerFeeAssetData: order.TakerFeeAssetData,
TakerAssetAmount: gqltypes.BigNumberToBigInt(order.TakerAssetAmount),
TakerFee: gqltypes.BigNumberToBigInt(order.TakerFee),
SenderAddress: common.Address(order.SenderAddress),
FeeRecipientAddress: common.Address(order.FeeRecipientAddress),
ExpirationTimeSeconds: gqltypes.BigNumberToBigInt(order.ExpirationTimeSeconds),
Salt: gqltypes.BigNumberToBigInt(order.Salt),
Signature: order.Signature,
FillableTakerAssetAmount: gqltypes.BigNumberToBigInt(order.FillableTakerAssetAmount),
Hash: common.HexToHash(order.Hash),
ChainID: math.MustParseBig256(order.ChainID),
ExchangeAddress: common.HexToAddress(order.ExchangeAddress),
MakerAddress: common.HexToAddress(order.MakerAddress),
MakerAssetData: common.FromHex(order.MakerAssetData),
MakerFeeAssetData: common.FromHex(order.MakerFeeAssetData),
MakerAssetAmount: math.MustParseBig256(order.MakerAssetAmount),
MakerFee: math.MustParseBig256(order.MakerFee),
TakerAddress: common.HexToAddress(order.TakerAddress),
TakerAssetData: common.FromHex(order.TakerAssetData),
TakerFeeAssetData: common.FromHex(order.TakerFeeAssetData),
TakerAssetAmount: math.MustParseBig256(order.TakerAssetAmount),
TakerFee: math.MustParseBig256(order.TakerFee),
SenderAddress: common.HexToAddress(order.SenderAddress),
FeeRecipientAddress: common.HexToAddress(order.FeeRecipientAddress),
ExpirationTimeSeconds: math.MustParseBig256(order.ExpirationTimeSeconds),
Salt: math.MustParseBig256(order.Salt),
Signature: common.FromHex(order.Signature),
FillableTakerAssetAmount: math.MustParseBig256(order.FillableTakerAssetAmount),
}
}

Expand All @@ -99,7 +102,11 @@ func ordersWithMetadataFromGQLType(orders []*gqltypes.OrderWithMetadata) []*Orde
return result
}

func statsFromGQLType(stats *gqltypes.Stats) *Stats {
func statsFromGQLType(stats *gqltypes.Stats) (*Stats, error) {
startOfCurrentUTCDay, err := time.Parse(time.RFC3339, stats.StartOfCurrentUTCDay)
if err != nil {
return nil, err
}
return &Stats{
Version: stats.Version,
PubSubTopic: stats.PubSubTopic,
Expand All @@ -110,16 +117,16 @@ func statsFromGQLType(stats *gqltypes.Stats) *Stats {
NumPeers: stats.NumPeers,
NumOrders: stats.NumOrders,
NumOrdersIncludingRemoved: stats.NumOrdersIncludingRemoved,
StartOfCurrentUTCDay: stats.StartOfCurrentUTCDay,
StartOfCurrentUTCDay: startOfCurrentUTCDay,
EthRPCRequestsSentInCurrentUTCDay: stats.EthRPCRequestsSentInCurrentUTCDay,
EthRPCRateLimitExpiredRequests: stats.EthRPCRateLimitExpiredRequests,
MaxExpirationTime: gqltypes.BigNumberToBigInt(stats.MaxExpirationTime),
}
MaxExpirationTime: math.MustParseBig256(stats.MaxExpirationTime),
}, nil
}

func latestBlockFromGQLType(latestBlock *gqltypes.LatestBlock) *LatestBlock {
return &LatestBlock{
Number: gqltypes.BigNumberToBigInt(latestBlock.Number),
Hash: common.Hash(latestBlock.Hash),
Number: math.MustParseBig256(latestBlock.Number),
Hash: common.HexToHash(latestBlock.Hash),
}
}
Loading