Skip to content

Commit

Permalink
Add VXOR based DEX (#100)
Browse files Browse the repository at this point in the history
* add support for vxor and vxor based dex

---------

Co-authored-by: f33r0 <95526886+f33r0@users.noreply.github.com>
  • Loading branch information
ZlayaMorda and f33r0 authored Oct 16, 2024
1 parent 61b5e81 commit 1a5f6f8
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
6 changes: 4 additions & 2 deletions processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
KXOR_ID = "0x02000e0000000000000000000000000000000000000000000000000000000000"
ETH_ID = "0x0200070000000000000000000000000000000000000000000000000000000000"
KUSD_ID = "0x02000c0000000000000000000000000000000000000000000000000000000000"
VXOR_ID = "0x006a271832f44c93bd8692584d85415f0f3dccef9748fecd129442c8edcb4361"
TECH_ACCOUNT = (
# "0x54734f90f971a02c609b2d684e61b5574e35ac9942579a2635aada58e5d836a7" # noqa
"cnTQ1kbv7PBNNQrEb1tZpmK7ftiv4yCCpUQy1J2y7Y54Taiaw" # noqa
Expand Down Expand Up @@ -122,7 +123,7 @@ def process_swap_transaction(timestamp, extrinsicEvents, ex_dict, prices):
elif param["name"] == "selected_source_types":
filter_mode = get_value(param) or ["SMART"]

if dex_id not in [0, 1, 2] :
if dex_id not in [0, 1, 2, 3]:
return None

for event in extrinsicEvents:
Expand Down Expand Up @@ -150,7 +151,8 @@ def process_swap_transaction(timestamp, extrinsicEvents, ex_dict, prices):

if ((dex_id == 0 and input_asset_type != XOR_ID and output_asset_type != XOR_ID) \
or (dex_id == 1 and input_asset_type != XSTUSD_ID and output_asset_type != XSTUSD_ID) \
or (dex_id == 2 and input_asset_type != KUSD_ID and output_asset_type != KUSD_ID)) \
or (dex_id == 2 and input_asset_type != KUSD_ID and output_asset_type != KUSD_ID) \
or (dex_id == 3 and input_asset_type != VXOR_ID and output_asset_type != VXOR_ID)) \
and (not is_eth_kxor_pair(input_asset_type, output_asset_type)) \
and (not is_xst_based_pair(input_asset_type, output_asset_type)):
assert len(intermediate_amounts) > 0 , ex_dict
Expand Down
18 changes: 16 additions & 2 deletions run_node_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from sqlalchemy import and_, func, update
from sqlalchemy.future import select
from sqlalchemy.orm import selectinload
import substrateinterface
from substrateinterface import SubstrateInterface
from tqdm import trange

Expand All @@ -25,6 +24,7 @@
XOR_ID,
XSTUSD_ID,
KUSD_ID,
VXOR_ID,
get_processing_functions,
get_timestamp,
get_value,
Expand All @@ -37,6 +37,10 @@

SWAP_FEE_ASSETS = {}

# BLOCK_IMPORT_LIMIT = 10 # In blocks, 0, None or float("inf") - to not stop

# WAIT_FOR_NEXT_IMPORT = 4 # In seconds

def get_fee_price_func(substrate, block_hash, pairs):
xor_id_int = int(XOR_ID, 16)
for asset_id in list(SWAP_FEE_ASSETS):
Expand Down Expand Up @@ -280,6 +284,8 @@ def get_end(substrate: SubstrateInterface):
val_id_int = int(VAL_ID, 16)
pswap_id_int = int(PSWAP_ID, 16)
kusd_id_int = int(KUSD_ID, 16)
vxor_id_int = int(VXOR_ID, 16)

async with async_session() as session:
# cache list of pairs in memory
# to avoid SELECTing them everytime there is need to lookup ID by hash
Expand All @@ -292,15 +298,20 @@ def get_end(substrate: SubstrateInterface):
pending = None
if not silent:
logging.info("Importing from %i to %i", begin, end)
# make sure XOR, XSTUSD, VAL and PSWAP token entries created
# make sure XOR, XSTUSD, VAL, PSWAP and VXOR token entries created
# be able to import burns and buybacks
await get_or_create_token(substrate, session, xor_id_int)
await get_or_create_token(substrate, session, xstusd_id_int)
await get_or_create_token(substrate, session, val_id_int)
await get_or_create_token(substrate, session, pswap_id_int)
await get_or_create_token(substrate, session, vxor_id_int)
for block in (range if silent or not sys.stdout.isatty() else trange)(
begin, end
):
# if BLOCK_IMPORT_LIMIT != 0 and BLOCK_IMPORT_LIMIT is not None:
# if (block - begin + 1) % BLOCK_IMPORT_LIMIT == 0:
# logging.info("Waiting %i seconds to get next block", WAIT_FOR_NEXT_IMPORT)
# await asyncio.sleep(WAIT_FOR_NEXT_IMPORT)
# get events from <block> to <dataset>
dataset = []
try:
Expand Down Expand Up @@ -530,6 +541,9 @@ def get_end(substrate: SubstrateInterface):
if swap[0] == 2:
base_id = KUSD_ID
base_id_int = kusd_id_int
if swap[0] == 3:
base_id = VXOR_ID
base_id_int = vxor_id_int
other_asset = swap[1] if swap[2] == base_id_int else swap[2]
other_asset = "{0:#0{1}x}".format(other_asset, 66)
if swap[1] == base_id_int:
Expand Down
6 changes: 4 additions & 2 deletions web.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from starlette.responses import JSONResponse

from models import Burn, BuyBack, Pair, Swap, Token
from processing import XOR_ID, XSTUSD_ID, KUSD_ID
from processing import XOR_ID, XSTUSD_ID, KUSD_ID, VXOR_ID

WHITELIST_URL = "https://raw.githubusercontent.com/sora-xor/polkaswap-token-whitelist-config/master/whitelist.json" # noqa

Expand Down Expand Up @@ -218,6 +218,7 @@ async def pairs(session=Depends(get_db)):
xor_id_int = int(XOR_ID, 16)
xstusd_id_int = int(XSTUSD_ID, 16)
kusd_id_int = int(KUSD_ID, 16)
vxor_id_int = int(VXOR_ID, 16)
# fetch all pairs info
# select last swap for each pair in subquery to obtain price
for p, last_price in await session.execute(
Expand All @@ -232,7 +233,8 @@ async def pairs(session=Depends(get_db)):
):
# there are separate pairs for selling and buying XOR
# need to sum them to calculate total volumes
if p.from_token_id == xor_id_int or p.from_token_id == xstusd_id_int or p.from_token_id == kusd_id_int:
if p.from_token_id == xor_id_int or p.from_token_id == xstusd_id_int \
or p.from_token_id == kusd_id_int or p.from_token_id == vxor_id_int:
# <p> contains XOR->XXX swaps
base = p.to_token
base_volume = p.to_volume
Expand Down

0 comments on commit 1a5f6f8

Please sign in to comment.