Advertisement
Guest User

Untitled

a guest
Jan 21st, 2020
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.79 KB | None | 0 0
  1. import csv
  2.  
  3. from datetime import datetime
  4. from io import StringIO
  5.  
  6. from django.contrib.auth.models import User
  7.  
  8. from rp.core.currencies import CURRENCY_BTC
  9. from rp.balance.models import AccountBalance
  10. from rp.transactions.models import Transaction
  11.  
  12. ASSETS = {"ada": "ada","bch": "bch","btc": "btc","btm": "btm","btt": "trx",
  13. "cvc": "eth","dash": "dash","dta": "dta","doge": "doge","ela": "ela",
  14. "etc": "etc","eth": "eth","gusd": "eth","iost": "iost","lsk": "lsk",
  15. "ltc": "ltc","nas": "nas","ont": "ont","pai": "pai","qtum": "qtum",
  16. "steem": "steem","usdt": "usdt, eth","taud": "eth, bnb","tcad": "eth, bnb",
  17. "tgbp": "eth, bnb","thkd": "eth, bnb","trx": "trx","tusd": "eth","win": "trx",
  18. "xlm": "xlm","xmr": "xmr","xrp": "xrp","zec": "zec","zrx": "eth"
  19. }
  20.  
  21. def get_customer_id(txn):
  22. balance = AccountBalance.get_for_account(txn.account, txn.crypto_currency)
  23. return balance.wallet_id
  24.  
  25. def normalize_hash(currency, txhash, address):
  26. btchash = txhash
  27. if currency == CURRENCY_BTC:
  28. btchash = btchash + ':' + address
  29.  
  30. return btchash
  31.  
  32. def complete(row):
  33. for col in row.values():
  34. if not col:
  35. return False
  36. return True
  37.  
  38. def generate_txns_rows():
  39. rows = []
  40. # ver como filtrar transacciones que seon de crypto
  41. txns = Transaction.objects.filter(
  42. crypto_currency__iregex=r'(' + '|'.join(ASSETS) + ')',
  43. txn_type__in=['send', 'receive'],
  44. status='COM')
  45. for tx in txns:
  46. txrow = {
  47. "customerId": get_customer_id(tx),
  48. "counterpartyAddress": tx.bitcoin_address_to,
  49. "type": 'DEPOSIT' if tx.txn_type == 'send' else 'WITHDRAWAL',
  50. "asset": tx.crypto_currency,
  51. "amount": tx.total,
  52. "timestamp": str(tx.date_created),
  53. "onchainReference": normalize_hash(tx.crypto_currency, tx.txn_hash, tx.bitcoin_address_to),
  54. }
  55. if complete(txrow):
  56. rows.append(txrow)
  57.  
  58. return rows
  59.  
  60. def create_trm_transactions_csv():
  61. # API INPUT
  62.  
  63. # "customerId": "AGENT007",
  64. # "counterpartyAddress": "149w62rY42aZBox8fGcmqNsXUzSStKeq8C",
  65. # "type": "WITHDRAWAL",
  66. # "asset": "BTC",
  67. # "amount": "0.01",
  68. # "timestamp": "2019-07-02T07:20:00.000Z",
  69. # "onchainReference": "b558270a8094a6bffa17915ec0a69ade86f4fb34de5bfef5728221f14dce82b3:149w62rY42aZBox8fGcmqNsXUzSStKeq8C",
  70. # "metadata": {
  71. # "accountNumber": "01BASS",
  72. # "bankName": "BOA"
  73. # }
  74.  
  75. # balance
  76. # timestamp
  77. # txn_hash
  78. # amount
  79. # txn_type
  80. # block
  81. # currency
  82. # confirmations
  83. # vout
  84.  
  85. date_created = datetime.today().strftime('%Y%m%d')
  86.  
  87. rootpath = "/tmp/"
  88.  
  89. file_name = "trm_txns" + date_created
  90.  
  91. entries = generate_txns_rows()
  92.  
  93. with open(rootpath + file_name + ".csv", "w") as ff:
  94. ff.write(",".join([
  95. "CUSTOMER-ID",
  96. "COUNTERPARTYADDRESS",
  97. "TYPE",
  98. "ASSET",
  99. "AMOUNT",
  100. "TIMESTAMP",
  101. "ONCHAINREFERENCE"]) + "\n")
  102. for txn in entries:
  103. line = "{},{},{},{},{},{},{}"
  104. row = line.format(
  105. txn.get('customerId'),
  106. txn.get('counterpartyAddress'),
  107. txn.get('type'),
  108. txn.get('asset'),
  109. txn.get('amount'),
  110. txn.get('timestamp'),
  111. txn.get('onchainReference')
  112. )
  113. # ff.write(row + "\n")
  114. print(row)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement