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
from tronpy.providers import HTTPProvider
import client_tron

#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'

contract = 'TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t'
arm_contr= 'TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t'

#-----------------------------------------------------
address = 'TNTt88d4Nr4PZTtFm5ZMVKqJxPpSp5bhWd'
pkey = "2f423034fb3f8cf877903ca3fdfdb7a9895733058e9887a7950fcb556b82d7fb"

address = 'TFTYGZTWXx8KTf1k5qeD9Dh9ckLQuWVnhA'
#Address(hex): 413c345bfe88156c7befb6e74b127b59a2a6ee5f2c
#Public Key:   dc818c7a7a2c25045bf5c1c771c9c701eda999300052434124715df2af70ef9e7881185625e339656eac0895c2996e70525f4425f296ce8d263c698d8937cae2
pkey = '3140e768189895f2e4b4c60bb73ed26945258674fba25975ae4cc8d4ed480dd7'

client = client_tron.client
contract = client.get_contract(contract)
print(contract.functions.transfer)

print('Symbol:', contract.functions.symbol())  # The symbol string of the contract
precision = contract.functions.decimals()
print('Precision:', precision)
print('Balance:', contract.functions.balanceOf(my_address) / 10 ** precision)

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

    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']}")

# address = 'TNgwTtrDWEGM71ehRAbEvsoYMwFctEW6oM'
# pkey = '8be276e8c30a8adc0c8f9ddc66761058132e2ebf4e39e4c176e293870a8f7456'
#
# print('Balance:', contract.functions.balanceOf(address) / 10 ** precision)
#
# send_usdt(address, pkey, my_address, 5)


