Skip to content

Commit

Permalink
algo: Added support for Deflex limit order create and cancel (hodgerp…
Browse files Browse the repository at this point in the history
  • Loading branch information
jumperavocado authored Feb 15, 2023
1 parent 9ceca82 commit acb4ffa
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 21 deletions.
9 changes: 4 additions & 5 deletions src/staketaxcsv/algo/export_tx.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
make_lp_withdraw_tx,
make_repay_tx,
make_reward_tx,
make_spend_tx,
make_spend_fee_tx,
make_stake_tx,
make_swap_tx,
make_transfer_in_tx,
Expand Down Expand Up @@ -103,10 +103,9 @@ def export_reward_tx(exporter, txinfo, reward_asset, fee_amount=0, comment=None,
_ingest_row(exporter, row, fee_amount, comment)


@exclude_tx
def export_spend_tx(exporter, txinfo, send_asset, fee_amount=0, comment=None, z_index=0):
row = make_spend_tx(txinfo, send_asset.amount, send_asset.ticker, z_index=z_index)
_ingest_row(exporter, row, fee_amount, comment)
def export_spend_fee_tx(exporter, txinfo, fee_asset, comment=None, z_index=0):
row = make_spend_fee_tx(txinfo, fee_asset.amount, fee_asset.ticker, z_index=z_index)
_ingest_row(exporter, row, comment=comment)


@exclude_tx
Expand Down
10 changes: 5 additions & 5 deletions src/staketaxcsv/algo/handle_amm.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
is_transfer,
is_transfer_receiver,
is_transfer_receiver_non_zero_asset,
is_transfer_sender
is_transaction_sender
)


Expand All @@ -36,7 +36,7 @@ def is_swap_group(wallet_address, group):
if not is_transfer(transaction):
return False

if not is_transfer_sender(wallet_address, transaction):
if not is_transaction_sender(wallet_address, transaction):
return False

if is_asset_optin(transaction):
Expand Down Expand Up @@ -73,7 +73,7 @@ def is_lp_add_group(wallet_address, group):
if not is_transfer(transaction):
return False

if not is_transfer_sender(wallet_address, transaction):
if not is_transaction_sender(wallet_address, transaction):
return False

if is_asset_optin(transaction):
Expand All @@ -84,7 +84,7 @@ def is_lp_add_group(wallet_address, group):
if not is_transfer(transaction):
return False

if not is_transfer_sender(wallet_address, transaction):
if not is_transaction_sender(wallet_address, transaction):
return False

if is_asset_optin(transaction):
Expand Down Expand Up @@ -119,7 +119,7 @@ def is_lp_remove_group(wallet_address, group):
if not is_transfer(transaction):
return False

if not is_transfer_sender(wallet_address, transaction):
if not is_transaction_sender(wallet_address, transaction):
return False

asset = get_transfer_asset(transaction)
Expand Down
84 changes: 77 additions & 7 deletions src/staketaxcsv/algo/handle_deflex.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@
from functools import partial
import hashlib
from staketaxcsv.algo.api_algoindexer import AlgoIndexerAPI
from staketaxcsv.algo.asset import Algo
from staketaxcsv.algo.config_algo import localconfig
from staketaxcsv.algo.export_tx import export_swap_tx, export_unknown
from staketaxcsv.algo.export_tx import export_deposit_collateral_tx, export_spend_fee_tx, export_swap_tx, export_unknown, export_withdraw_collateral_tx
from staketaxcsv.algo.transaction import (
get_fee_amount,
get_inner_transfer_asset,
get_transfer_asset,
is_algo_transfer,
is_app_call,
is_transaction_sender,
is_transfer,
is_transfer_receiver_non_zero_asset
)
Expand All @@ -32,6 +35,9 @@
DEFLEX_TRANSACTION_OPT_IN = "TMSORQ==" # "User_opt_into_assets" ABI selector
DEFLEX_TRANSACTION_SWAP = "P2FHIA==" # "User_swap" ABI selector
DEFLEX_TRANSACTION_SWAP_FINALIZE = "tTD7Hw==" # "User_swap_finalize" ABI selector

DEFLEX_TRANSACTION_CREATE_ORDER = "Ai+ORg==" # "User_create_order" ABI selector
DEFLEX_TRANSACTION_CANCEL_ORDER = "dXwdeg==" # "User_cancel_order" ABI selector
DEFLEX_TRANSACTION_FILL_ORDER_INIT = "Ynj8hA==" # "Backend_fill_order_initialize" ABI selector
DEFLEX_TRANSACTION_FILL_ORDER_FINALIZE = "QZXMuQ==" # "Backend_fill_order_finalize" ABI selector
DEFLEX_TRANSACTION_CLOSE_ESCROW = "jVTQ1A==" # "Backend_close_escrow" ABI selector
Expand Down Expand Up @@ -64,7 +70,7 @@ def _is_deflex_routed_swap(wallet_address, group):
return is_app_call(group[1], APPLICATION_ID_DEFLEX_ORDER_ROUTER, DEFLEX_TRANSACTION_SWAP_FINALIZE)


def _is_deflex_limit_swap(group):
def _is_deflex_limit_order_fill(group):
if len(group) != 2:
return False

Expand All @@ -74,17 +80,56 @@ def _is_deflex_limit_swap(group):
return is_app_call(group[1], APPLICATION_ID_DEFLEX_REGISTRY, DEFLEX_TRANSACTION_CLOSE_ESCROW)


def _is_deflex_limit_order_create(wallet_address, group):
if len(group) < 5:
return False

if not is_app_call(group[-1], localconfig.deflex_limit_order_apps, DEFLEX_TRANSACTION_CREATE_ORDER):
return False

transaction = group[-2]
if not is_transfer(transaction):
return False

if not is_transaction_sender(wallet_address, transaction):
return False

transaction = group[-3]
if not is_algo_transfer(transaction):
return False

return is_transaction_sender(wallet_address, transaction)


def _is_deflex_limit_order_cancel(group):
if len(group) != 2:
return False

if not is_app_call(group[0], localconfig.deflex_limit_order_apps, DEFLEX_TRANSACTION_CANCEL_ORDER):
return False

return is_app_call(group[1], APPLICATION_ID_DEFLEX_REGISTRY, DEFLEX_TRANSACTION_CLOSE_ESCROW)


def is_deflex_transaction(wallet_address, group):
return (_is_deflex_routed_swap(wallet_address, group)
or _is_deflex_limit_swap(group))
or _is_deflex_limit_order_fill(group)
or _is_deflex_limit_order_create(wallet_address, group)
or _is_deflex_limit_order_cancel(group))


def handle_deflex_transaction(wallet_address, group, exporter, txinfo):
if _is_deflex_routed_swap(wallet_address, group):
_handle_deflex_routed_swap(wallet_address, group, exporter, txinfo)

elif _is_deflex_limit_swap(group):
_handle_deflex_limit_swap(wallet_address, group, exporter, txinfo)
elif _is_deflex_limit_order_fill(group):
_handle_deflex_limit_order_fill(wallet_address, group, exporter, txinfo)

elif _is_deflex_limit_order_create(wallet_address, group):
_handle_deflex_limit_order_create(wallet_address, group, exporter, txinfo)

elif _is_deflex_limit_order_cancel(group):
_handle_deflex_limit_order_cancel(wallet_address, group, exporter, txinfo)

else:
export_unknown(exporter, txinfo)
Expand All @@ -103,7 +148,7 @@ def _handle_deflex_routed_swap(wallet_address, group, exporter, txinfo):
export_swap_tx(exporter, txinfo, send_asset, receive_asset, fee_amount, COMMENT_DEFLEX)


def _handle_deflex_limit_swap(wallet_address, group, exporter, txinfo):
def _handle_deflex_limit_order_fill(wallet_address, group, exporter, txinfo):
full_group = indexer.get_transactions_by_group(group[0]["group"])

if not full_group:
Expand All @@ -120,4 +165,29 @@ def _handle_deflex_limit_swap(wallet_address, group, exporter, txinfo):
receive_asset = get_inner_transfer_asset(transaction,
filter=partial(is_transfer_receiver_non_zero_asset, wallet_address))

export_swap_tx(exporter, txinfo, send_asset, receive_asset, comment=COMMENT_DEFLEX + " Limit Order")
comment = COMMENT_DEFLEX + " Order Fill"
export_withdraw_collateral_tx(exporter, txinfo, send_asset, 0, comment, 0)
export_swap_tx(exporter, txinfo, send_asset, receive_asset, 0, comment, 1)


def _handle_deflex_limit_order_create(wallet_address, group, exporter, txinfo):
fee_amount = get_fee_amount(wallet_address, group)

send_transaction = group[-2]
send_asset = get_transfer_asset(send_transaction)

fee_transaction = group[-3]
fee_asset = get_transfer_asset(fee_transaction)
fee_amount += fee_asset.uint_amount

comment = COMMENT_DEFLEX + " Create Order"
export_spend_fee_tx(exporter, txinfo, Algo(fee_amount), comment, 0)
export_deposit_collateral_tx(exporter, txinfo, send_asset, 0, comment, 1)


def _handle_deflex_limit_order_cancel(wallet_address, group, exporter, txinfo):
fee_amount = get_fee_amount(wallet_address, group)

receive_asset = get_inner_transfer_asset(group[0],
filter=partial(is_transfer_receiver_non_zero_asset, wallet_address))
export_withdraw_collateral_tx(exporter, txinfo, receive_asset, fee_amount, COMMENT_DEFLEX + " Order Cancel")
6 changes: 3 additions & 3 deletions src/staketaxcsv/algo/handle_tinymanv2.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
is_asset_optin,
is_transfer,
is_transfer_receiver,
is_transfer_sender
is_transaction_sender
)

APPLICATION_ID_TINYMANV2_VALIDATOR = 1002541853
Expand Down Expand Up @@ -70,7 +70,7 @@ def _is_tinymanv2_lp_add_single(wallet_address, group):
if not is_transfer(transaction):
return False

if not is_transfer_sender(wallet_address, transaction):
if not is_transaction_sender(wallet_address, transaction):
return False

i += 1
Expand Down Expand Up @@ -107,7 +107,7 @@ def _is_tinymanv2_lp_remove_single(wallet_address, group):
if not is_transfer(transaction):
return False

if not is_transfer_sender(wallet_address, transaction):
if not is_transaction_sender(wallet_address, transaction):
return False

send_asset = get_transfer_asset(transaction)
Expand Down
10 changes: 9 additions & 1 deletion src/staketaxcsv/algo/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def is_transfer_receiver(wallet_address, transaction):
return wallet_address == get_transfer_receiver(transaction)


def is_transfer_sender(wallet_address, transaction):
def is_transaction_sender(wallet_address, transaction):
return wallet_address == transaction["sender"]


Expand Down Expand Up @@ -157,6 +157,14 @@ def is_transfer(transaction):
return txtype == co.TRANSACTION_TYPE_PAYMENT or txtype == co.TRANSACTION_TYPE_ASSET_TRANSFER


def is_algo_transfer(transaction):
return transaction["tx-type"] == co.TRANSACTION_TYPE_PAYMENT


def is_asa_transfer(transaction):
return transaction["tx-type"] == co.TRANSACTION_TYPE_ASSET_TRANSFER


def is_app_call(transaction, app_id=None, app_args=None, foreign_app=None):
if transaction["tx-type"] != co.TRANSACTION_TYPE_APP_CALL:
return False
Expand Down

0 comments on commit acb4ffa

Please sign in to comment.