from common.make_tx import make_swap_tx
from sol.handle_simple import handle_unknown_detect_transfers


def handle_metaplex(exporter, txinfo):
    transfers_in, transfers_out, _ = txinfo.transfers_net

    if len(transfers_in) == 1 and len(transfers_out) == 1:
        sent_amount, sent_currency, _, _ = transfers_out[0]
        received_amount, received_currency, _, _ = transfers_in[0]

        row = make_swap_tx(txinfo, sent_amount, sent_currency, received_amount, received_currency)
        exporter.ingest_row(row)
    else:
        handle_unknown_detect_transfers(exporter, txinfo)


def is_nft_mint(txinfo):
    log_instructions = txinfo.log_instructions
    transfers_in, transfers_out, _ = txinfo.transfers_net

    if "MintTo" in log_instructions and len(transfers_out) == 1 and len(transfers_in) == 0:
        return True
    elif ("MintTo" in log_instructions
          and len(transfers_out) == 1
          and len(transfers_in) == 1
          and transfers_in[0][0] == 1):
        return True
    else:
        return False


def handle_nft_mint(exporter, txinfo):
    transfers_in, transfers_out, transfers_unknown = txinfo.transfers_net

    if len(transfers_in) == 1 and len(transfers_out) == 1:
        sent_amount, sent_currency, _, _ = transfers_out[0]
        received_amount, received_currency, _, _ = transfers_in[0]

        row = make_swap_tx(txinfo, sent_amount, sent_currency, received_amount, received_currency)
        exporter.ingest_row(row)
        return

    handle_unknown_detect_transfers(exporter, txinfo)

[((44, 4, 44, 53), 'sol.handle_simple.handle_unknown_detect_transfers', 'handle_unknown_detect_transfers', ({(44, 36, 44, 44): 'exporter', (44, 46, 44, 52): 'txinfo'}, {}), '(exporter, txinfo)', False, 'from sol.handle_simple import handle_unknown_detect_transfers\n'), ((12, 14, 12, 98), 'common.make_tx.make_swap_tx', 'make_swap_tx', ({(12, 27, 12, 33): 'txinfo', (12, 35, 12, 46): 'sent_amount', (12, 48, 12, 61): 'sent_currency', (12, 63, 12, 78): 'received_amount', (12, 80, 12, 97): 'received_currency'}, {}), '(txinfo, sent_amount, sent_currency, received_amount,\n    received_currency)', False, 'from common.make_tx import make_swap_tx\n'), ((15, 8, 15, 57), 'sol.handle_simple.handle_unknown_detect_transfers', 'handle_unknown_detect_transfers', ({(15, 40, 15, 48): 'exporter', (15, 50, 15, 56): 'txinfo'}, {}), '(exporter, txinfo)', False, 'from sol.handle_simple import handle_unknown_detect_transfers\n'), ((40, 14, 40, 98), 'common.make_tx.make_swap_tx', 'make_swap_tx', ({(40, 27, 40, 33): 'txinfo', (40, 35, 40, 46): 'sent_amount', (40, 48, 40, 61): 'sent_currency', (40, 63, 40, 78): 'received_amount', (40, 80, 40, 97): 'received_currency'}, {}), '(txinfo, sent_amount, sent_currency, received_amount,\n    received_currency)', False, 'from common.make_tx import make_swap_tx\n')]