cleaned up txinfo endpoint

pull/139/head
yhtiyar 2021-08-23 16:47:33 +03:00
rodzic beb6cb90b4
commit 6abc6c67b9
3 zmienionych plików z 34 dodań i 31 usunięć

Wyświetl plik

@ -17,7 +17,7 @@ from sqlalchemy.orm import Session
from . import data
from .settings import DEFAULT_STREAM_TIMEINTERVAL
from .settings import DEFAULT_STREAM_TIMEINTERVAL, ETHERSCAN_SMARTCONTRACTS_BUCKET
import boto3
import json
@ -279,20 +279,22 @@ def get_source_code(
for label in labels:
if label.label == "etherscan_smartcontract":
name = label.label_data["name"]
uri = label.label_data["object_uri"]
key = uri.split("s3://etherscan-smart-contracts/")[1]
object_uri = label.label_data["object_uri"]
key = object_uri.split("s3://etherscan-smart-contracts/")[1]
s3 = boto3.client("s3")
bucket = "etherscan-smart-contracts"
raw_obj = s3.get_object(Bucket=bucket, Key=key)
obj_data = json.loads(raw_obj["Body"].read().decode("utf-8"))["data"]
contract_source_info = data.EthereumSmartContractSourceInfo(
name=obj_data["ContractName"],
source_code=obj_data["SourceCode"],
compiler_version=obj_data["CompilerVersion"],
abi=obj_data["ABI"],
)
return contract_source_info
bucket = ETHERSCAN_SMARTCONTRACTS_BUCKET
try:
raw_obj = s3.get_object(Bucket=bucket, Key=key)
obj_data = json.loads(raw_obj["Body"].read().decode("utf-8"))["data"]
contract_source_info = data.EthereumSmartContractSourceInfo(
name=obj_data["ContractName"],
source_code=obj_data["SourceCode"],
compiler_version=obj_data["CompilerVersion"],
abi=obj_data["ABI"],
)
return contract_source_info
except:
logger.error(f"Failed to load smart contract {contract_address}")
return None

Wyświetl plik

@ -8,6 +8,8 @@ end users.
import logging
from typing import Dict, Optional
from sqlalchemy.sql.expression import true
from fastapi import FastAPI, Depends, HTTPException, Query
from fastapi.middleware.cors import CORSMiddleware
from moonstreamdb.db import yield_db_session
@ -71,26 +73,22 @@ async def txinfo_ethereum_blockchain_handler(
logger.error(err)
response.errors.append("Could not decode ABI from the given input")
# Checking if it is contract deployment:
smart_contract = (
db_session.query(EthereumAddress)
.filter(EthereumAddress.transaction_hash == txinfo_request.tx.hash)
.one_or_none()
)
is_contract_deployment = smart_contract is not None
if txinfo_request.tx.to_address:
# transaction is contract deployment:
if txinfo_request.tx.to_address is None:
response.is_smart_contract_deployment = True
smart_contract = (
db_session.query(EthereumAddress)
.filter(EthereumAddress.transaction_hash == txinfo_request.tx.hash)
.one_or_none()
)
if smart_contract is not None:
response.is_smart_contract_deployment = True
else:
response.smart_contract_info = actions.get_source_code(
db_session, txinfo_request.tx.to_address
)
if smart_contract is not None:
response.smart_contract_address = smart_contract.address
if txinfo_request.tx.to_address is None:
response.is_smart_contract_deployment = True
elif txinfo_request.tx.to_address == smart_contract.address:
response.is_smart_contract_call = True
response.smart_contract_address = txinfo_request.tx.to_address
response.is_smart_contract_call = True
return response

Wyświetl plik

@ -41,3 +41,6 @@ for path in MOONSTREAM_OPENAPI_LIST:
DOCS_PATHS[f"/{path}/{DOCS_TARGET_PATH}/openapi.json"] = "GET"
DEFAULT_STREAM_TIMEINTERVAL = 5 * 60
# S3 Bucket
ETHERSCAN_SMARTCONTRACTS_BUCKET = "etherscan-smart-contracts"