def _save_if_erc20_token(w3: Web3Client, crud: Crud, transaction: TxData) -> None: if not w3.is_transaction_contract_creation(transaction): return address = w3.get_contract_address_by_transaction_hash(transaction["hash"]) if w3.is_contract_erc20(address): crud.save_token_address(address)
def _get_first_block(w3: Web3Client, crud: Crud) -> Optional[BlockData]: start_block_hash = crud.get_start_block_hash() if not start_block_hash: block = w3.get_latest_block(full_transactions=True) crud.set_start_block(block["hash"]) return block else: current_block_hash = crud.get_current_block_hash() assert current_block_hash is not None return w3.get_block_by_hash(current_block_hash, full_transactions=True)
def query_ERC20_tokens(*, w3: Web3Client, crud: Crud) -> None: block = _get_first_block(w3, crud) crud.set_is_block_fetch(True) last_block_hash = crud.get_last_block_hash() while block and block["hash"] != last_block_hash: crud.set_current_block(block["hash"]) transactions: Sequence[TxData] = block["transactions"] # type: ignore _find_contract_creations(w3, crud, transactions) block = w3.get_parent_block(block, full_transactions=True) _finish_process(crud)
def fetch_address_token_balances(eoa_address: str, *, w3: Web3Client, crud: Crud) -> Dict: session_obj = { "address": eoa_address, "started_at": datetime.datetime.utcnow().timestamp(), "processing": True, } crud.save_address_balances(eoa_address, session_obj) token_addresses = crud.get_token_addresses() for token_addr in token_addresses: balance = w3.get_eoa_token_balance(eoa_address, token_addr) session_obj.update({token_addr: balance}) crud.save_address_balances(eoa_address, session_obj) session_obj["processing"] = False crud.save_address_balances(eoa_address, session_obj) return session_obj
from app import create_app from app.crud import Crud from flask import request, make_response if __name__ == '__main__': app = create_app() #Instanciar el método de la clase crud encargada de realizar las operaciones sobre la BD crud = Crud() @app.route('/user', methods=['GET', 'POST', 'PUT', 'DELETE']) def user(): response = None #Identificar el método de la solicitud if request.method == 'GET': response = make_response(crud.read()) response.headers['Content-Type'] = 'application/json' return response elif request.method == 'POST': response = make_response(crud.create(request.data.decode())) response.headers['Content-Type'] = 'application/json' return response elif request.method == 'PUT': response = make_response(crud.update(request.data.decode())) response.headers['Content-Type'] = 'application/json' return response elif request.method == 'DELETE': response = make_response(crud.delete(request.data.decode()))
def _finish_process(crud: Crud) -> None: crud.set_last_block(crud.get_start_block_hash()) crud.set_current_block(None) crud.set_start_block(None) crud.set_is_block_fetch(False)