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, 'sol.handle_simple.handle_unknown_detect_transfers', 'handle_unknown_detect_transfers', False, 'from sol.handle_simple import handle_unknown_detect_transfers\n'), (12, 'common.make_tx.make_swap_tx', 'make_swap_tx', False, 'from common.make_tx import make_swap_tx\n'), (15, 'sol.handle_simple.handle_unknown_detect_transfers', 'handle_unknown_detect_transfers', False, 'from sol.handle_simple import handle_unknown_detect_transfers\n'), (40, 'common.make_tx.make_swap_tx', 'make_swap_tx', False, 'from common.make_tx import make_swap_tx\n')]