import requests
import base58
import base64
from pprint import pprint
from tronpy import Tron
from tronpy.keys import PrivateKey
import requests
import json
import time
from tronpy import Tron
from tronpy.keys import PrivateKey

#https://betterprogramming.pub/how-to-send-tron-cryptocurrency-with-python-5d842bc8f9cd

#Wallet address:  TNTt88d4Nr4PZTtFm5ZMVKqJxPpSp5bhWd
#Private Key:  2f423034fb3f8cf877903ca3fdfdb7a9895733058e9887a7950fcb556b82d7fb

my_address = 'TLJjdgJMGDrVn22a37MZAiH3L9YPe3hYkc' #USDT Tron
armen_address = 'TX4RsY9T7YzrXjHgH9bPXEfKHngx2P7W5E'


# arm_contr= 'TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t'
#
#
# full_node = 'https://api.trongrid.io'
# solidity_node = 'https://api.trongrid.io'
# event_server = 'https://api.trongrid.io'
#
# #address = 'TNTt88d4Nr4PZTtFm5ZMVKqJxPpSp5bhWd'
# #pkey = "2f423034fb3f8cf877903ca3fdfdb7a9895733058e9887a7950fcb556b82d7fb"
#
# address = 'TFTYGZTWXx8KTf1k5qeD9Dh9ckLQuWVnhA'
# #Address(hex): 413c345bfe88156c7befb6e74b127b59a2a6ee5f2c
# #Public Key:   dc818c7a7a2c25045bf5c1c771c9c701eda999300052434124715df2af70ef9e7881185625e339656eac0895c2996e70525f4425f296ce8d263c698d8937cae2
pkey = '3140e768189895f2e4b4c60bb73ed26945258674fba25975ae4cc8d4ed480dd7'

def send_tron(from_, to_, pkey, amount: int):
    print(f'SEND {amount} TRX')
    amount = amount * 1000000

    client = Tron()
    priv_key = PrivateKey(bytes.fromhex(pkey))

    txn = (
        client.trx.transfer(from_, to_, amount)
        .memo("Transaction Description")
        .build()
        .sign(priv_key)
    )

    print(txn.txid)
    print(txn.broadcast().wait())

def send_usdt(from_, pkey, to_, amount):
    print(f'Amount {amount} USDT')
    priv_key = PrivateKey(bytes.fromhex(pkey))
    amount = amount * 1000000 # USDT * 1 000 000

    contract = 'TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t'
    client = Tron()
    contract = client.get_contract(contract)

    txn = (
        contract.functions.transfer(to_, amount)
        .with_owner(from_)  # address of the private key
        #.fee_limit(5_000_000) #fee 5TRX
        .build()
        .sign(priv_key)
    )

    print(txn)
    result = txn.broadcast().wait()  # or txn.broadcast()
    #contract.functions.transfer.parse_output(_['contractResult'][0])
    print(result)
    print(f"Fee = {result['fee']/1000000} TRX")
    print(f"Receipt = {result['receipt']['result']}")


send_usdt(armen_address, pkey, my_address, 2)