Created
September 28, 2018 15:38
-
-
Save Arachnid/2f95b7d8566366b34fb4751ad55c5dc4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import asyncio | |
import logging | |
from flask import Blueprint, Flask, request, render_template, g | |
from flask_graphql import GraphQLView | |
from google.cloud import logging as glogging | |
#from graphql.execution.executors.asyncio import AsyncioExecutor | |
from web3 import Web3, HTTPProvider | |
from werkzeug.serving import run_simple | |
from graphql import ( | |
graphql, | |
GraphQLArgument, | |
GraphQLSchema, | |
GraphQLObjectType, | |
GraphQLField, | |
GraphQLList, | |
GraphQLString, | |
GraphQLFloat, | |
GraphQLInt, | |
GraphQLBoolean | |
) | |
from graphql.type.scalars import ( | |
GraphQLScalarType, | |
coerce_string, | |
parse_string_literal | |
) | |
def get_blocks(hashes): | |
return [web3.eth.getBlock(hash) for hash in hashes] | |
def get_transactions(hashes): | |
return [web3.eth.getTransaction(hash) for hash in hashes] | |
def coerce_hash(hash): | |
return hash.hex() | |
GraphQLHexBytes = GraphQLScalarType(name="HexBytes", serialize=coerce_hash, parse_value=coerce_string, parse_literal=parse_string_literal) | |
Log = GraphQLObjectType( | |
name='Log', | |
fields=lambda:{ | |
"address": GraphQLField(GraphQLString), | |
"topics": GraphQLField(GraphQLList(GraphQLHexBytes)), | |
"data": GraphQLField(GraphQLString), | |
"transactionHash": GraphQLField(GraphQLHexBytes), | |
"transactionIndex": GraphQLField(GraphQLInt), | |
"transaction": GraphQLField(Transaction, resolver=lambda root, info: web3.eth.getTransaction(root.transactionHash)), | |
"blockNumber": GraphQLField(GraphQLInt), | |
"blockHash": GraphQLField(GraphQLHexBytes), | |
"block": GraphQLField(Block, resolver=lambda root, info: web3.eth.getBlock(root.blockHash)), | |
"logIndex": GraphQLField(GraphQLInt), | |
"removed": GraphQLField(GraphQLBoolean), | |
} | |
) | |
TransactionReceipt = GraphQLObjectType( | |
name='TransactionReceipt', | |
fields=lambda:{ | |
"block": GraphQLField(Block, resolver=lambda root, info: web3.eth.getBlock(root.blockHash)), | |
"blockHash": GraphQLField(GraphQLHexBytes), | |
"blockNumber": GraphQLField(GraphQLInt), | |
"contractAddress": GraphQLField(GraphQLString), | |
"cumulativeGasUsed": GraphQLField(GraphQLInt), | |
"from": GraphQLField(GraphQLString), | |
"gasUsed": GraphQLField(GraphQLInt), | |
"logs": GraphQLField(GraphQLList(Log)), | |
"logsBloom": GraphQLField(GraphQLHexBytes), | |
"status": GraphQLField(GraphQLInt), | |
"to": GraphQLField(GraphQLString), | |
"transactionHash": GraphQLField(GraphQLString), | |
"transactionIndex": GraphQLField(GraphQLInt), | |
"transaction": GraphQLField(Transaction, resolver=lambda root, info: web3.eth.getTransaction(root.transactionHash)), | |
} | |
) | |
Transaction = GraphQLObjectType( | |
name='Transaction', | |
fields=lambda:{ | |
"block": GraphQLField(Block, resolver=lambda root, info: web3.eth.getBlock(root.blockHash)), | |
"blockHash": GraphQLField(GraphQLHexBytes), | |
"blockNumber": GraphQLField(GraphQLInt), | |
"from": GraphQLField(GraphQLString), | |
"to": GraphQLField(GraphQLString), | |
"gas": GraphQLField(GraphQLInt), | |
"gasPrice": GraphQLField(GraphQLInt), | |
"hash": GraphQLField(GraphQLHexBytes), | |
"input": GraphQLField(GraphQLString), | |
"nonce": GraphQLField(GraphQLInt), | |
"transactionIndex": GraphQLField(GraphQLInt), | |
"value": GraphQLField(GraphQLString), | |
"v": GraphQLField(GraphQLInt), | |
"r": GraphQLField(GraphQLHexBytes), | |
"s": GraphQLField(GraphQLHexBytes), | |
"receipt": GraphQLField(TransactionReceipt, resolver=lambda root, info: web3.eth.getTransactionReceipt(root.hash)), | |
} | |
) | |
Block = GraphQLObjectType( | |
name='Block', | |
fields=lambda:{ | |
"difficulty": GraphQLField(GraphQLFloat), | |
"extraData": GraphQLField(GraphQLHexBytes), | |
"gasLimit": GraphQLField(GraphQLInt), | |
"gasUsed": GraphQLField(GraphQLInt), | |
"hash": GraphQLField(GraphQLHexBytes), | |
"logsBloom": GraphQLField(GraphQLHexBytes), | |
"miner": GraphQLField(GraphQLHexBytes), | |
"mixHash": GraphQLField(GraphQLHexBytes), | |
"nonce": GraphQLField(GraphQLHexBytes), | |
"number": GraphQLField(GraphQLInt), | |
"parentHash": GraphQLField(GraphQLHexBytes), | |
"parent": GraphQLField(Block, resolver=lambda root, info: web3.eth.getBlock(root.parentHash)), | |
"receiptsRoot": GraphQLField(GraphQLHexBytes), | |
"sha3Uncles": GraphQLField(GraphQLHexBytes), | |
"size": GraphQLField(GraphQLInt), | |
"stateRoot": GraphQLField(GraphQLHexBytes), | |
"timestamp": GraphQLField(GraphQLInt), | |
"totalDifficulty": GraphQLField(GraphQLFloat), | |
"transactions": GraphQLField(GraphQLList(Transaction), resolver=lambda root, info: get_transactions(root.transactions)), | |
"transactionsRoot": GraphQLField(GraphQLHexBytes), | |
"uncles": GraphQLField(GraphQLList(Block), resolver=lambda root, info: get_blocks(root.uncles)), | |
} | |
) | |
schema = GraphQLSchema( | |
query= GraphQLObjectType( | |
name='Query', | |
fields={ | |
'block': GraphQLField( | |
Block, | |
args={ | |
"number": GraphQLArgument(type=GraphQLInt), | |
"hash": GraphQLArgument(type=GraphQLString) | |
}, | |
resolver=lambda root, info, number=None, hash=None: web3.eth.getBlock(number or hash) | |
), | |
'transaction': GraphQLField( | |
Transaction, | |
args={ | |
"hash": GraphQLArgument(type=GraphQLString) | |
}, | |
resolver=lambda root, info, hash: web3.eth.getTransaction(hash) | |
) | |
} | |
) | |
) | |
web3 = Web3(HTTPProvider()) | |
app = Flask(__name__) | |
app.add_url_rule('/graphql', view_func=GraphQLView.as_view('graphql', schema=schema, graphiql=True, context=web3)) | |
if __name__ == '__main__': | |
logging.basicConfig(level=logging.INFO) | |
run_simple('localhost', 8080, app, use_reloader=True) | |
else: | |
logger = glogging.Client() | |
logger.setup_logging(log_level=logging.INFO) |
fubuloubu
commented
Sep 28, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment