Skip to content
Open
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
*venv
*__pycache__
*.pyc

# removing doc builds
docs/build/*
65 changes: 64 additions & 1 deletion api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,50 +15,86 @@
#TO GET ALL BLOCKS (LIMIT SET TO 10)
@app.route('/v1.0/all_blocks/', methods=['GET'])
def get_all_blocks():
"""
Returns all the blocks data in the database. Limit set to 10 for test phase.
"""
results = ApiBlocks.get_all_blocks()
return json.dumps(results, indent=2, default=json_serial)


# #TO GET LATEST BLOCK'S BLOCKNUMBER
#TO GET LATEST BLOCK'S BLOCKNUMBER
@app.route('/v1.0/current_blockNumber/', methods=['GET'])
def eth_blockNumber():
"""
Returns the block number of the latest block in the database.
"""
results=ApiBlocks.get_current_block_number()
return json.dumps(results, indent=2, default=json_serial)


#TO GET A BLOCK BY ITS BLOCK NUMBER
@app.route('/v1.0/getBlockByNumber/<int:blockno>/', methods=['GET'])
def eth_getBlockByNumber(blockno):
"""
Returns the data of the block of the given block number

:param int blockno: Block Number that we want to retrieve data of
"""
results=ApiBlocks.get_block_by_number(blockno)
return json.dumps(results, indent=2, default=json_serial)

#TO GET A BLOCK BY ITS BLOCK HASH
@app.route('/v1.0/getBlockByHash/<string:block_hash>/', methods=['GET'])
def eth_getBlockByHash(block_hash):
"""
Returns the data of the block of the given block hash

:param string block_hash: Block Hash that we want to retrieve data of
"""
results=ApiBlocks.get_block_by_hash(block_hash)
return json.dumps(results, indent=2, default=json_serial)

#TO GET A BLOCK TRANSACTION COUNT BY ITS BLOCK HASH
@app.route('/v1.0/getTransactionCountByHash/<string:block_hash>/', methods=['GET'])
def eth_getBlockTransactionCountByHash(block_hash):
"""
Returns the transaction count of the block of the given block hash

:param string block_hash: Block Hash that we want to retrieve data of
"""
results=ApiBlocks.get_block_transaction_count_by_hash(block_hash)
return json.dumps(results, indent=2, default=json_serial)

#TO GET A BLOCK TRANSACTION COUNT BY ITS BLOCK NUMBER
@app.route('/v1.0/getTransactionCountByNumber/<int:blockno>/', methods=['GET'])
def eth_getBlockTransactionCountByNumber(blockno):
"""
Returns the transaction count of the block of the given block number

:param int blockno: Block Number that we want to retrieve data of
"""
results=ApiBlocks.get_block_transaction_count_by_number(blockno)
return json.dumps(results, indent=2, default=json_serial)

#TO GET A BLOCK UNCLE COUNT BY ITS BLOCK HASH
@app.route('/v1.0/getUncleCountByBlockHash/<string:block_hash>/', methods=['GET'])
def eth_getUncleCountByBlockHash(block_hash):
"""
Returns the uncle count of the block of the given block hash

:param string block_hash: Block Hash that we want to retrieve data of
"""
results=ApiBlocks.get_uncle_count_by_block_hash(block_hash)
return json.dumps(results, indent=2, default=json_serial)

#TO GET A BLOCK UNCLE COUNT BY ITS BLOCK NUMBER
@app.route('/v1.0/getUncleCountByBlockNumber/<int:blockno>/', methods=['GET'])
def eth_getUncleCountByBlockNumber(blockno):
"""
Returns the uncle count of the block of the given block number

:param int blockno: Block Number that we want to retrieve data of
"""
results=ApiBlocks.get_uncle_count_by_block_number(blockno)
return json.dumps(results, indent=2, default=json_serial)

Expand All @@ -69,18 +105,35 @@ def eth_getUncleCountByBlockNumber(blockno):
#TO GET A TRANSACTION BY ITS HASH
@app.route('/v1.0/getTransactionByHash/<string:transaction_hash>/', methods=['GET'])
def eth_getTransactionByHash(transaction_hash):
"""
Returns the data of the transaction of the given transaction hash

:param string transaction_hash: Transaction Hash that we want to retrieve data of
"""
results=ApiTransactions.get_transaction_by_hash(transaction_hash)
return json.dumps(results, indent=2, default=json_serial)

#TO GET A TRANSACTION BY BLOCK HASH AND INDEX
@app.route('/v1.0/getTransactionByBlockHashAndIndex/<string:block_hash>/<int:index>/', methods=['GET'])
def eth_getTransactionByBlockHashAndIndex(block_hash, index):
"""
Returns the data of the transaction of the given transaction index and block hash

:param int transaction_index: Transaction Index that we want to retrieve data of
:param string block_hash: Block Hash that we want to retrieve data of
"""
results=ApiTransactions.get_transaction_by_block_hash_and_index(index, block_hash)
return json.dumps(results, indent=2, default=json_serial)

#TO GET A TRANSACTION BY BLOCK NUMBER AND INDEX
@app.route('/v1.0/getTransactionByBlockNumberAndIndex/<int:blockno>/<int:index>/', methods=['GET'])
def eth_getTransactionByBlockNumberAndIndex(blockno, index):
"""
Returns the data of the transaction of the given transaction index and block number

:param int transaction_index: Transaction Index that we want to retrieve data of
:param int blockno: Block Number that we want to retrieve data of
"""
results=ApiTransactions.get_transaction_by_block_number_and_index(index, blockno)
return json.dumps(results, indent=2, default=json_serial)

Expand All @@ -91,6 +144,11 @@ def eth_getTransactionByBlockNumberAndIndex(blockno, index):
#TO GET A TRANSACTION RECEIPTS BY ITS HASH
@app.route('/v1.0/getReceipt/<string:transaction_hash>/', methods=['GET'])
def eth_getTransactionReceipt(transaction_hash):
"""
Returns the data of the receipt of the given transaction hash

:param string transaction_hash: Transaction Hash that we want to retrieve data of
"""
results = ApiReceipts.get_receipt(transaction_hash)
return json.dumps(results, indent=2, default=json_serial)

Expand All @@ -101,6 +159,11 @@ def eth_getTransactionReceipt(transaction_hash):
#TO GET AN UNCLE BY ITS HASH
@app.route('/v1.0/getUncleByHash/<string:uncle_hash>/', methods=['GET'])
def eth_getUncleByUncleHash(uncle_hash):
"""
Returns the data of the uncle of the given uncle hash

:param string uncle_hash: Uncle Hash that we want to retrieve data of
"""
results = ApiUncles.get_uncle_by_uncle_hash(uncle_hash)
return json.dumps(results, indent=2, default=json_serial)

Expand Down
3 changes: 3 additions & 0 deletions api/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import sys

sys.path.append("/api/models/")
from .api_blocks import ApiBlocks
from .api_transactions import ApiTransactions
from .api_receipts import ApiReceipts
Expand Down
49 changes: 47 additions & 2 deletions api/models/api_blocks.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
from ether_sql.models import Blocks
import sys
from sqlalchemy.sql.expression import func
sys.path.append('../')
sys.path.append("../")
from sessions import Session

ss=Session()
session=ss.connect_to_psql()
Block=ss.get_table_object('blocks')
class ApiBlocks(Blocks):
"""
Extends the Blocks class from ether_sql.models.
The functions defined here access the psql Blocks table and retrieve the results based on requesting parameters.
Blocks Class maps a block table in the psql database to a block in ethereum node.

:param int block_number: Quantity equal to number of blocks behind the current block
:param str block_hash: The Keccak 256-bit hash of this block
:param int uncle_count: Number of uncles in this block
:param int transaction_count: Number of transactions in this block

"""
@staticmethod
def get_all_blocks():
"""
Returns all the blocks data in the database. Limit set to 10 for test phase.
"""
results = []
blocks = session.query(Block).limit(10).all()
columns = Block.columns.keys()
Expand All @@ -20,6 +32,9 @@ def get_all_blocks():

@staticmethod
def get_current_block_number():
"""
Return the block number of the latest block in the database.
"""
results = []
blocks = session.query(func.max(Block.columns.block_number).label('block_number'))
column_names = [c["name"] for c in blocks.column_descriptions]
Expand All @@ -28,6 +43,11 @@ def get_current_block_number():

@staticmethod
def get_block_by_number(blockno):
"""
Returns the data of the block of the given block number

:param int blockno: Block Number that we want to retrieve data of
"""
results = []
blocks = session.query(Block).filter_by(block_number=blockno).all()
columns = Block.columns.keys()
Expand All @@ -37,6 +57,11 @@ def get_block_by_number(blockno):

@staticmethod
def get_block_by_hash(block_hash):
"""
Returns the data of the block of the given block hash

:param string block_hash: Block Hash that we want to retrieve data of
"""
results = []
blocks = session.query(Block).filter_by(block_hash=block_hash).all()
columns = Block.columns.keys()
Expand All @@ -46,6 +71,11 @@ def get_block_by_hash(block_hash):

@staticmethod
def get_block_transaction_count_by_hash(block_hash):
"""
Returns the transaction count of the block of the given block hash

:param string block_hash: Block Hash that we want to retrieve data of
"""
try:
blocks = session.query(Block).filter_by(block_hash=block_hash).first()
return [{'transaction_count':blocks.transaction_count}]
Expand All @@ -54,6 +84,11 @@ def get_block_transaction_count_by_hash(block_hash):

@staticmethod
def get_block_transaction_count_by_number(blockno):
"""
Returns the transaction count of the block of the given block number

:param int blockno: Block Number that we want to retrieve data of
"""
try:
blocks = session.query(Block).filter_by(block_number=blockno).first()
return [{'transaction_count':blocks.transaction_count}]
Expand All @@ -62,6 +97,11 @@ def get_block_transaction_count_by_number(blockno):

@staticmethod
def get_uncle_count_by_block_hash(block_hash):
"""
Returns the uncle count of the block of the given block hash

:param string block_hash: Block Hash that we want to retrieve data of
"""
try:
blocks = session.query(Block).filter_by(block_hash=block_hash).first()
return [{'uncle_count':blocks.uncle_count}]
Expand All @@ -70,6 +110,11 @@ def get_uncle_count_by_block_hash(block_hash):

@staticmethod
def get_uncle_count_by_block_number(blockno):
"""
Returns the uncle count of the block of the given block number

:param int blockno: Block Number that we want to retrieve data of
"""
try:
blocks = session.query(Block).filter_by(block_number=blockno).first()
return [{'uncle_count':blocks.uncle_count}]
Expand Down
14 changes: 13 additions & 1 deletion api/models/api_receipts.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
from ether_sql.models import Receipts
import sys
from sqlalchemy.sql.expression import func
sys.path.append('../')
sys.path.append("../")
from sessions import Session

ss=Session()
session=ss.connect_to_psql()
Receipt=ss.get_table_object('receipts')

class ApiReceipts(Receipts):
"""
Extends the Receipts class from ether_sql.models.
The functions defined here access the psql Receipts table and retrieve the results based on requesting parameters.
Receipts Class maps a block table in the psql database to a block in ethereum node.

:param str transaction_hash: The Keccak 256-bit hash of this transaction

"""
@staticmethod
def get_receipt(transaction_hash):
"""
Returns the data of the receipt of the given transaction hash

:param string transaction_hash: Transaction Hash that we want to retrieve data of
"""
results = []
receipts = session.query(Receipt).filter_by(transaction_hash=transaction_hash).all()
columns = Receipt.columns.keys()
Expand Down
32 changes: 31 additions & 1 deletion api/models/api_transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,30 @@
import sys
from sqlalchemy.sql.expression import func
from .api_blocks import ApiBlocks
sys.path.append('../')
sys.path.append("../")
from sessions import Session

ss=Session()
session=ss.connect_to_psql()
Transaction=ss.get_table_object('transactions')

class ApiTransactions(Transactions):
"""
Extends the Transactions class from ether_sql.models.
The functions defined here access the psql Transactions table and retrieve the results based on requesting parameters.
Transactions Class maps a block table in the psql database to a block in ethereum node.

:param str transaction_hash: The Keccak 256-bit hash of this transaction
:param int block_number: Number of the block containing this transaction
:param datetime transaction_index: Position of this transaction in the transaction list of this block

"""

@staticmethod
def get_all_transactions():
"""
Returns all the transactions data in the database. Limit set to 10 for test phase.
"""
results = []
blocks = session.query(Transaction).limit(10).all()
columns = Transaction.columns.keys()
Expand All @@ -22,6 +35,11 @@ def get_all_transactions():

@staticmethod
def get_transaction_by_hash(transaction_hash):
"""
Returns the data of the transaction of the given transaction hash

:param string transaction_hash: Transaction Hash that we want to retrieve data of
"""
results = []
transactions = session.query(Transaction).filter_by(transaction_hash=transaction_hash).all()
columns = Transaction.columns.keys()
Expand All @@ -31,6 +49,12 @@ def get_transaction_by_hash(transaction_hash):

@staticmethod
def get_transaction_by_block_hash_and_index(transaction_index, block_hash):
"""
Returns the data of the transaction of the given transaction index and block hash

:param int transaction_index: Transaction Index that we want to retrieve data of
:param string block_hash: Block Hash that we want to retrieve data of
"""
results = []
blocks = ApiBlocks.get_block_by_hash(block_hash)
if blocks==[]:
Expand All @@ -43,6 +67,12 @@ def get_transaction_by_block_hash_and_index(transaction_index, block_hash):

@staticmethod
def get_transaction_by_block_number_and_index(transaction_index, blockno):
"""
Returns the data of the transaction of the given transaction index and block number

:param int transaction_index: Transaction Index that we want to retrieve data of
:param int blockno: Block Number that we want to retrieve data of
"""
results = []
transactions =session.query(Transaction).filter_by(transaction_index=transaction_index, block_number=blockno).all()
columns = Transaction.columns.keys()
Expand Down
Loading