Skip to content

Commit

Permalink
Fix swap fees (#88)
Browse files Browse the repository at this point in the history
* Fix pricing server

* Update Jenkinsfile

* Update Jenkinsfile

---------

Co-authored-by: f33r0 <[email protected]>
  • Loading branch information
vovac12 and f33r0 authored Apr 15, 2024
1 parent 561827a commit fc18ec2
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 14 deletions.
3 changes: 3 additions & 0 deletions models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
class Token(Base):
__tablename__ = "token"

# Original scheme don't work locally. Keep it here to make local development easier.
# pk = Column(Integer, primary_key=True)
# id = Column(Numeric(80), nullable=False, unique=True)
id = Column(Numeric(80), primary_key=True)
symbol = Column(String(8), nullable=False)
name = Column(String(128), nullable=False)
Expand Down
35 changes: 24 additions & 11 deletions processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,19 @@
CURRENCIES = "Currencies"
DEPOSITED = "Deposited"

def get_swap_fee_amount(fee, pairs):
if isinstance(fee, list):
fee = dict(map(lambda info: (info[0]['code'], info[1]), fee))
final_fee = 0
for fee_asset in fee:
if fee_asset == XOR_ID:
price = 1
else:
price = pairs(fee_asset)
final_fee += fee[fee_asset] * price
return int(final_fee)
else:
return fee

def get_value(attribute, name="value"):
if isinstance(attribute, dict):
Expand Down Expand Up @@ -62,7 +75,7 @@ def set_max_amount(value, current_value):
return current_value


def process_swap_transaction(timestamp, extrinsicEvents, ex_dict):
def process_swap_transaction(timestamp, extrinsicEvents, ex_dict, prices):
# verify that the swap was a success
swap_success = False

Expand Down Expand Up @@ -126,7 +139,7 @@ def process_swap_transaction(timestamp, extrinsicEvents, ex_dict):
elif event["event_id"] == "Exchange":
input_amount = get_value(event["event"]["attributes"][4])
output_amount = get_value(event["event"]["attributes"][5])
swap_fee_amount = get_value(event["event"]["attributes"][6])
swap_fee_amount = get_swap_fee_amount(get_value(event["event"]["attributes"][6]), prices)
xor_fee = max(get_fees_from_event(event), xor_fee)
if not swap_success:
# TODO: add swap fail handler
Expand All @@ -149,7 +162,7 @@ def process_swap_transaction(timestamp, extrinsicEvents, ex_dict):
)


def process_withdraw_transaction(timestamp, extrinsicEvents, ex_dict):
def process_withdraw_transaction(timestamp, extrinsicEvents, ex_dict, prices):
withdraw_asset1_type = None
withdraw_asset2_type = None
withdraw_asset1_amount = None
Expand Down Expand Up @@ -182,7 +195,7 @@ def process_withdraw_transaction(timestamp, extrinsicEvents, ex_dict):
)


def process_deposit_transaction(timestamp, extrinsicEvents, ex_dict):
def process_deposit_transaction(timestamp, extrinsicEvents, ex_dict, prices):
deposit_asset1_id = None
deposit_asset2_id = None
deposit_asset1_amount = None
Expand Down Expand Up @@ -216,7 +229,7 @@ def process_deposit_transaction(timestamp, extrinsicEvents, ex_dict):
)


def process_in_bridge_tx(timestamp, extrinsicEvents, ex_dict):
def process_in_bridge_tx(timestamp, extrinsicEvents, ex_dict, prices):
bridge_success = False
asset_id = None
bridged_amt = None
Expand All @@ -239,7 +252,7 @@ def process_in_bridge_tx(timestamp, extrinsicEvents, ex_dict):
return InBridgeTx(timestamp, xor_fee_paid, asset_id, bridged_amt, ext_tx_hash)


def process_out_bridge_tx(timestamp, extrinsicEvents, ex_dict):
def process_out_bridge_tx(timestamp, extrinsicEvents, ex_dict, prices):
bridge_success = False
outgoing_asset_id = None
outgoing_asset_amt = None
Expand Down Expand Up @@ -275,7 +288,7 @@ def process_out_bridge_tx(timestamp, extrinsicEvents, ex_dict):
)


def process_claim(timestamp, extrinsicEvents, ex_dict):
def process_claim(timestamp, extrinsicEvents, ex_dict, prices):
claim_success = False
xor_fee_paid = 0

Expand All @@ -295,7 +308,7 @@ def process_claim(timestamp, extrinsicEvents, ex_dict):
return ClaimTx(timestamp, xor_fee_paid, asset_id, asset_amt)


def process_rewards(timestamp, extrinsicEvents, ex_dict):
def process_rewards(timestamp, extrinsicEvents, ex_dict, prices):
rewards = []

for event in extrinsicEvents:
Expand All @@ -307,7 +320,7 @@ def process_rewards(timestamp, extrinsicEvents, ex_dict):
return None


def process_transfers(timestamp, extrinsicEvents, ex_dict):
def process_transfers(timestamp, extrinsicEvents, ex_dict, prices):
success = False
asset_id = None
amount = None
Expand All @@ -326,7 +339,7 @@ def process_transfers(timestamp, extrinsicEvents, ex_dict):
return TransferTx(timestamp, fees, asset_id, amount)


def process_batch_all(timestamp, extrinsicEvents, ex_dict):
def process_batch_all(timestamp, extrinsicEvents, ex_dict, prices):
success = False
fees = 0

Expand Down Expand Up @@ -362,7 +375,7 @@ def get_timestamp(result) -> str:


def get_processing_functions() -> Dict[
str, Callable[[str, List, Dict], Optional[SoraOp]]
str, Callable[[str, List, Dict, Callable[[str], float]], Optional[SoraOp]]
]:
return {
"swap": process_swap_transaction,
Expand Down
49 changes: 46 additions & 3 deletions run_node_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,43 @@

DENOM = Decimal(10 ** 18)

SWAP_FEE_ASSETS = {}

def get_fee_price_func(substrate, block_hash, pairs):
xor_id_int = int(XOR_ID, 16)
for asset_id in list(SWAP_FEE_ASSETS):
asset_id_int = int(asset_id, 16)
if (asset_id_int, xor_id_int) in pairs and pairs[asset_id_int, xor_id_int].quote_price is not None:
SWAP_FEE_ASSETS[asset_id] = float(pairs[asset_id_int, xor_id_int].quote_price)

def get_fee_price(asset_id):
if asset_id in SWAP_FEE_ASSETS:
return SWAP_FEE_ASSETS[asset_id]

asset_id_int = int(asset_id, 16)
if (asset_id_int, xor_id_int) in pairs and pairs[asset_id_int, xor_id_int].quote_price is not None:
SWAP_FEE_ASSETS[asset_id] = float(pairs[asset_id_int, xor_id_int].quote_price)
return SWAP_FEE_ASSETS[asset_id]

params = [
0,
asset_id,
XOR_ID,
"1000000000000000000",
"WithDesiredInput",
[],
"Disabled",
block_hash,
]
result = substrate.rpc_request("liquidityProxy_quote", params)
price = 0
if result["result"] is not None:
price = int(result["result"]["amount"]) / DENOM

SWAP_FEE_ASSETS[asset_id] = float(price)
return price
return get_fee_price

def connect_to_substrate_node():
try:
substrate = SubstrateInterface(
Expand Down Expand Up @@ -91,7 +128,7 @@ def get_events_from_block(substrate, block_id: int):
return block_hash, events, result, grouped_events


def process_events(dataset, func_map, result, grouped_events):
def process_events(dataset, func_map, result, grouped_events, get_fee_price):
"""
Call function from func_map for every extrinsic depending on extrinsic type.
"""
Expand All @@ -106,7 +143,7 @@ def process_events(dataset, func_map, result, grouped_events):
tx_type = exdict["call"]["call_function"]
processing_func = func_map.get(tx_type)
if processing_func:
tx = processing_func(timestamp, extrinsic_events, exdict)
tx = processing_func(timestamp, extrinsic_events, exdict, get_fee_price)
if tx:
dataset.append(asdict(tx))

Expand Down Expand Up @@ -214,6 +251,11 @@ def get_event_param(event, param_idx):


async def async_main(async_session, begin=1, clean=False, silent=False):
# if clean:
# async with db.engine.begin() as conn:
# await conn.run_sync(models.Base.metadata.drop_all)
# await conn.run_sync(models.Base.metadata.create_all)

def get_end(substrate: SubstrateInterface):
block_hash = substrate.get_chain_finalised_head()
block = substrate.get_block(block_hash)
Expand Down Expand Up @@ -269,7 +311,8 @@ def get_end(substrate: SubstrateInterface):
substrate = connect_to_substrate_node()

timestamp = get_timestamp(res)
process_events(dataset, func_map, res, grouped_events)
get_fee_price = get_fee_price_func(substrate, block_hash, pairs)
process_events(dataset, func_map, res, grouped_events, get_fee_price)
# await previous INSERT to finish if any
if pending:
await pending
Expand Down

0 comments on commit fc18ec2

Please sign in to comment.