Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import csv
- from datetime import datetime
- from io import StringIO
- from django.contrib.auth.models import User
- from rp.core.currencies import CURRENCY_BTC
- from rp.balance.models import AccountBalance
- from rp.transactions.models import Transaction
- ASSETS = {"ada": "ada","bch": "bch","btc": "btc","btm": "btm","btt": "trx",
- "cvc": "eth","dash": "dash","dta": "dta","doge": "doge","ela": "ela",
- "etc": "etc","eth": "eth","gusd": "eth","iost": "iost","lsk": "lsk",
- "ltc": "ltc","nas": "nas","ont": "ont","pai": "pai","qtum": "qtum",
- "steem": "steem","usdt": "usdt, eth","taud": "eth, bnb","tcad": "eth, bnb",
- "tgbp": "eth, bnb","thkd": "eth, bnb","trx": "trx","tusd": "eth","win": "trx",
- "xlm": "xlm","xmr": "xmr","xrp": "xrp","zec": "zec","zrx": "eth"
- }
- def get_customer_id(txn):
- balance = AccountBalance.get_for_account(txn.account, txn.crypto_currency)
- return balance.wallet_id
- def normalize_hash(currency, txhash, address):
- btchash = txhash
- if currency == CURRENCY_BTC:
- btchash = btchash + ':' + address
- return btchash
- def complete(row):
- for col in row.values():
- if not col:
- return False
- return True
- def generate_txns_rows():
- rows = []
- # ver como filtrar transacciones que seon de crypto
- txns = Transaction.objects.filter(
- crypto_currency__iregex=r'(' + '|'.join(ASSETS) + ')',
- txn_type__in=['send', 'receive'],
- status='COM')
- for tx in txns:
- txrow = {
- "customerId": get_customer_id(tx),
- "counterpartyAddress": tx.bitcoin_address_to,
- "type": 'DEPOSIT' if tx.txn_type == 'send' else 'WITHDRAWAL',
- "asset": tx.crypto_currency,
- "amount": tx.total,
- "timestamp": str(tx.date_created),
- "onchainReference": normalize_hash(tx.crypto_currency, tx.txn_hash, tx.bitcoin_address_to),
- }
- if complete(txrow):
- rows.append(txrow)
- return rows
- def create_trm_transactions_csv():
- # API INPUT
- # "customerId": "AGENT007",
- # "counterpartyAddress": "149w62rY42aZBox8fGcmqNsXUzSStKeq8C",
- # "type": "WITHDRAWAL",
- # "asset": "BTC",
- # "amount": "0.01",
- # "timestamp": "2019-07-02T07:20:00.000Z",
- # "onchainReference": "b558270a8094a6bffa17915ec0a69ade86f4fb34de5bfef5728221f14dce82b3:149w62rY42aZBox8fGcmqNsXUzSStKeq8C",
- # "metadata": {
- # "accountNumber": "01BASS",
- # "bankName": "BOA"
- # }
- # balance
- # timestamp
- # txn_hash
- # amount
- # txn_type
- # block
- # currency
- # confirmations
- # vout
- date_created = datetime.today().strftime('%Y%m%d')
- rootpath = "/tmp/"
- file_name = "trm_txns" + date_created
- entries = generate_txns_rows()
- with open(rootpath + file_name + ".csv", "w") as ff:
- ff.write(",".join([
- "CUSTOMER-ID",
- "COUNTERPARTYADDRESS",
- "TYPE",
- "ASSET",
- "AMOUNT",
- "TIMESTAMP",
- "ONCHAINREFERENCE"]) + "\n")
- for txn in entries:
- line = "{},{},{},{},{},{},{}"
- row = line.format(
- txn.get('customerId'),
- txn.get('counterpartyAddress'),
- txn.get('type'),
- txn.get('asset'),
- txn.get('amount'),
- txn.get('timestamp'),
- txn.get('onchainReference')
- )
- # ff.write(row + "\n")
- print(row)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement