Skip to content

Commit

Permalink
Merge branch 'master' into rcnet
Browse files Browse the repository at this point in the history
  • Loading branch information
duelingbenjos committed Feb 4, 2025
2 parents c865475 + 0e4d575 commit 8233b9f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 29 deletions.
4 changes: 2 additions & 2 deletions src/xian/services/bds/bds.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from contracting.stdlib.bridge.time import Datetime, Timedelta
from xian.services.bds.database import DB, result_to_json
from xian_py.decompiler import ContractDecompiler
from xian_py.wallet import key_is_valid
from xian_py.wallet import Wallet
from timeit import default_timer as timer
from decimal import Decimal

Expand Down Expand Up @@ -234,7 +234,7 @@ async def _insert_addresses(self, tx: dict, block_time: datetime):
for state_change in tx['tx_result']['state']:
if state_change['key'].startswith('currency.balances:'):
address = state_change['key'].replace('currency.balances:', '')
if key_is_valid(address):
if Wallet.is_valid_key(address):
try:
self.db.add_query_to_batch(sql.insert_addresses(), [
tx['tx_result']['hash'],
Expand Down
58 changes: 31 additions & 27 deletions src/xian/services/bds/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,42 @@ def create_transactions():
"""


def create_state():
return """
CREATE TABLE IF NOT EXISTS state (
key TEXT PRIMARY KEY,
value JSONB,
value_numeric NUMERIC GENERATED ALWAYS AS (
CASE
WHEN value::text ~ '^"*[0-9]+(\.[0-9]+)?"*$'
THEN (trim(both '"' from value::text))::NUMERIC
ELSE NULL
END
) STORED,
updated TIMESTAMP NOT NULL
);
CREATE INDEX IF NOT EXISTS idx_state_value_numeric ON state(value_numeric);
"""


def create_state_changes():
return """
CREATE TABLE IF NOT EXISTS state_changes (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
tx_hash TEXT REFERENCES transactions(hash),
key TEXT NOT NULL,
value JSONB,
value_numeric NUMERIC GENERATED ALWAYS AS (
CASE
WHEN value::text ~ '^"*[0-9]+(\.[0-9]+)?"*$'
THEN (trim(both '"' from value::text))::NUMERIC
ELSE NULL
END
) STORED,
created TIMESTAMP NOT NULL
)
"""

# event = [
# {
# "caller": "e9e8aad29ce8e94fd77d9c55582e5e0c57cf81c552ba61c0d4e34b0dc11fd931",
# "contract": "con_event_token",
# "data": {
# "amount": 10
# },
# "data_indexed": {
# "from": "e9e8aad29ce8e94fd77d9c55582e5e0c57cf81c552ba61c0d4e34b0dc11fd931",
# "to": "burn"
# },
# "event": "Transfer",
# "signer": "e9e8aad29ce8e94fd77d9c55582e5e0c57cf81c552ba61c0d4e34b0dc11fd931"
# }
# ]

def create_events():
return """
Expand Down Expand Up @@ -77,17 +87,6 @@ def create_events():
WHERE data_indexed ->> key = value;
END;
$$ LANGUAGE plpgsql STABLE;
"""



def create_state():
return """
CREATE TABLE IF NOT EXISTS state (
key TEXT PRIMARY KEY,
value JSONB,
updated TIMESTAMP NOT NULL
)
"""


Expand Down Expand Up @@ -125,6 +124,7 @@ def create_contracts():
)
"""


def create_readonly_role():
return """
DO $$
Expand All @@ -149,6 +149,7 @@ def create_readonly_role():
$$;
"""


def enforce_table_limits():
return """
DO $$
Expand Down Expand Up @@ -183,7 +184,8 @@ def insert_transaction():
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13)
ON CONFLICT (hash) DO NOTHING;
"""



def insert_events():
return """
INSERT INTO events(
Expand All @@ -193,6 +195,7 @@ def insert_events():
ON CONFLICT (id) DO NOTHING;
"""


def insert_or_update_state():
return """
INSERT INTO state(
Expand All @@ -204,6 +207,7 @@ def insert_or_update_state():
updated = EXCLUDED.updated;
"""


def insert_state_changes():
return """
INSERT INTO state_changes(
Expand Down

0 comments on commit 8233b9f

Please sign in to comment.