Skip to content

Commit

Permalink
#260 write cost to DB (#277)
Browse files Browse the repository at this point in the history
* add cost calc

* test update

* update cost log

* fix merge

* gas_used added

* add trx details

* fix

* fix cost calc

* extra trx impl

* reason impl

* impl

* delete test_operator_spending.py

* apply comments

* add test

* apply comment

* log cost to db

* init

* impl

* fix import solana_utils

* comment operator cost

* add depends_on to docker-compose

* fix build

* apply comments
  • Loading branch information
sinev-valentine authored Nov 11, 2021
1 parent 98507f8 commit 51c3119
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
2 changes: 2 additions & 0 deletions proxy/docker-compose-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ services:
- "9090"
networks:
- net
depends_on:
- postgres

networks:
net:
2 changes: 1 addition & 1 deletion proxy/indexer/sql_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

POSTGRES_DB = os.environ.get("POSTGRES_DB", "neon-db")
POSTGRES_USER = os.environ.get("POSTGRES_USER", "neon-proxy")
POSTGRES_PASSWORD = os.environ.get("POSTGRES_PASSWORD", "neon-proxy")
POSTGRES_PASSWORD = os.environ.get("POSTGRES_PASSWORD", "neon-proxy-pass")
POSTGRES_HOST = os.environ.get("POSTGRES_HOST", "localhost")

try:
Expand Down
30 changes: 19 additions & 11 deletions proxy/plugin/solana_rest_api_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from web3.auto import w3
from proxy.environment import neon_cli, evm_loader_id, ETH_TOKEN_MINT_ID, COLLATERAL_POOL_BASE, read_elf_params
from .eth_proto import Trx
from ..indexer.sql_dict import SQLDict

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
Expand Down Expand Up @@ -806,6 +807,14 @@ def simulate_continue(signer, client, perm_accs, trx_accs, step_count):
return (continue_count, step_count)


class CostSingleton(object):
def __new__(cls):
if not hasattr(cls, 'instance'):
cls.instance = super(CostSingleton, cls).__new__(cls)
cls.instance.operator_cost = SQLDict(tablename="operator_cost")
return cls.instance


def update_transaction_cost(receipt, eth_trx, extra_sol_trx=False, reason=None):
cost = receipt['result']['meta']['preBalances'][0] - receipt['result']['meta']['postBalances'][0]
if eth_trx:
Expand Down Expand Up @@ -835,17 +844,16 @@ def update_transaction_cost(receipt, eth_trx, extra_sol_trx=False, reason=None):
used_gas = base58.b58decode(event['data'])[2:10]
used_gas = int().from_bytes(used_gas, "little")

logger.debug("COST %s %d %d %s %s %s %s %s",
hash,
cost,
used_gas if used_gas else 0,
sender,
to_address,
sig,
"extra" if extra_sol_trx else "ok",
reason if reason else "None",
)

table = CostSingleton()
table.operator_cost[hash] = {
'cost': cost,
'used_gas': used_gas if used_gas else 0,
'sender': sender,
'to_address': to_address,
'sig': sig,
'status': 'extra' if extra_sol_trx else 'ok',
'reason': reason if reason else ''
}

def create_account_list_by_emulate(signer, client, eth_trx):

Expand Down

0 comments on commit 51c3119

Please sign in to comment.