|
| 1 | +import base58 |
| 2 | +import psycopg2 |
| 3 | + |
| 4 | +from ..environment import EVM_LOADER_ID |
| 5 | +from ..indexer.sql_dict import POSTGRES_USER, POSTGRES_HOST, POSTGRES_DB, POSTGRES_PASSWORD |
| 6 | + |
| 7 | +class SQLCost(): |
| 8 | + def __init__(self): |
| 9 | + |
| 10 | + self.conn = psycopg2.connect( |
| 11 | + dbname=POSTGRES_DB, |
| 12 | + user=POSTGRES_USER, |
| 13 | + password=POSTGRES_PASSWORD, |
| 14 | + host=POSTGRES_HOST |
| 15 | + ) |
| 16 | + |
| 17 | + self.conn.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT) |
| 18 | + cur = self.conn.cursor() |
| 19 | + cur.execute(''' |
| 20 | + CREATE TABLE IF NOT EXISTS OPERATOR_COST |
| 21 | + ( |
| 22 | + hash char(64), |
| 23 | + cost bigint, |
| 24 | + used_gas bigint, |
| 25 | + sender char(40), |
| 26 | + to_address char(40) , |
| 27 | + sig char(100), |
| 28 | + status varchar(100), |
| 29 | + reason varchar(100) |
| 30 | + )''' |
| 31 | + ) |
| 32 | + |
| 33 | + def close(self): |
| 34 | + self.conn.close() |
| 35 | + |
| 36 | + def insert(self, hash, cost, used_gas, sender, to_address, sig, status, reason): |
| 37 | + cur = self.conn.cursor() |
| 38 | + cur.execute(''' |
| 39 | + INSERT INTO OPERATOR_COST (hash, cost, used_gas, sender, to_address, sig, status, reason) |
| 40 | + VALUES (%s,%s,%s,%s,%s,%s,%s,%s) |
| 41 | + ''', |
| 42 | + (hash, cost, used_gas, sender, to_address, sig, status, reason) |
| 43 | + ) |
| 44 | + |
| 45 | + |
| 46 | +class CostSingleton(object): |
| 47 | + def __new__(cls): |
| 48 | + if not hasattr(cls, 'instance'): |
| 49 | + cls.instance = super(CostSingleton, cls).__new__(cls) |
| 50 | + cls.instance.operator_cost = SQLCost() |
| 51 | + return cls.instance |
| 52 | + |
| 53 | + |
| 54 | +def update_transaction_cost(receipt, eth_trx, extra_sol_trx=False, reason=None): |
| 55 | + cost = receipt['result']['meta']['preBalances'][0] - receipt['result']['meta']['postBalances'][0] |
| 56 | + if eth_trx: |
| 57 | + hash = eth_trx.hash_signed().hex() |
| 58 | + sender = eth_trx.sender() |
| 59 | + to_address = eth_trx.toAddress.hex() if eth_trx.toAddress else "None" |
| 60 | + else: |
| 61 | + hash = None |
| 62 | + sender = None |
| 63 | + to_address = None |
| 64 | + |
| 65 | + sig = receipt['result']['transaction']['signatures'][0] |
| 66 | + used_gas=None |
| 67 | + |
| 68 | + tx_info = receipt['result'] |
| 69 | + accounts = tx_info["transaction"]["message"]["accountKeys"] |
| 70 | + evm_loader_instructions = [] |
| 71 | + |
| 72 | + for idx, instruction in enumerate(tx_info["transaction"]["message"]["instructions"]): |
| 73 | + if accounts[instruction["programIdIndex"]] == EVM_LOADER_ID: |
| 74 | + evm_loader_instructions.append(idx) |
| 75 | + |
| 76 | + for inner in (tx_info['meta']['innerInstructions']): |
| 77 | + if inner["index"] in evm_loader_instructions: |
| 78 | + for event in inner['instructions']: |
| 79 | + if accounts[event['programIdIndex']] == EVM_LOADER_ID: |
| 80 | + used_gas = base58.b58decode(event['data'])[2:10] |
| 81 | + used_gas = int().from_bytes(used_gas, "little") |
| 82 | + |
| 83 | + table = CostSingleton().operator_cost |
| 84 | + table.insert( |
| 85 | + hash, |
| 86 | + cost, |
| 87 | + used_gas if used_gas else 0, |
| 88 | + sender, |
| 89 | + to_address, |
| 90 | + sig, |
| 91 | + 'extra' if extra_sol_trx else 'ok', |
| 92 | + reason if reason else '' |
| 93 | + ) |
0 commit comments