andelf / tronpy

TRON Python Client Library.
MIT License
204 stars 96 forks source link

Send all balance #62

Closed ridx807 closed 1 year ago

ridx807 commented 2 years ago

how to send all balance of TRX? when i want send using balance as amout, that alwalys fail. balance = client.get_account_balance(address)

danielqba commented 1 year ago

You might have to account for bandwidth points. If not bandwidth left, you should let (I think) 0.1 TRX for fees.

ridx807 commented 1 year ago

You might have to account for bandwidth points. If not bandwidth left, you should let (I think) 0.1 TRX for fees.

Not working, i also test balance-1000000 (1 trx in sun) as amount of send, my balance is 100 when i send 99000000 as amount is working. I want create python script which can read balance and send all automatically

ridx807 commented 1 year ago

I think if t

You might have to account for bandwidth points. If not bandwidth left, you should let (I think) 0.1 TRX for fees.

I think if tronpy update fiture like tronweb fromSun and toSun is can be worked

danielqba commented 1 year ago

Don't know if related, but when you send to an address that has not been activated, you might need to spend 1 extra trx.

ridx807 commented 1 year ago

Don't know if related, but when you send to an address that has not been activated, you might need to spend 1 extra trx.

Yes i know, but i dont know how to do that in tronpy when i send balance and left 1-5 trx in wallet is not working This my script

from tronpy import Tron from tronpy.keys import PrivateKey

client = Tron(network='nile') priv_key = PrivateKey(bytes.fromhex("x)) address = "TJzXt1sZautjqXnpjQT4xSCBHNSYgBkDr3" balance = client.get_account_balance(address)

txn = ( client.trx.transfer("TJzXt1sZautjqXnpjQT4xSCBHNSYgBkDr3", "TVjsyZ7fYF3qLF6BQgPmTEZy1xrNNyVAAA", balance-5_000) .memo("test memo") .build() .sign(priv_key) ) print(txn.txid) print(txn.broadcast().wait())

ridx807 commented 1 year ago

Buf if i write spesific balance let says 100 its working perfectly

from tronpy import Tron from tronpy.keys import PrivateKey

client = Tron(network='nile') priv_key = PrivateKey(bytes.fromhex("x)) address = "TJzXt1sZautjqXnpjQT4xSCBHNSYgBkDr3" balance = client.get_account_balance(address)

txn = ( client.trx.transfer("TJzXt1sZautjqXnpjQT4xSCBHNSYgBkDr3", "TVjsyZ7fYF3qLF6BQgPmTEZy1xrNNyVAAA", 10000000) .memo("test memo") .build() .sign(priv_key) ) print(txn.txid) print(txn.broadcast().wait())

danielqba commented 1 year ago

Here, check this code I use. Modify according to your needs.

This is all inside a class.


def transferBalance(self, receiver: str,
                        amount: float,
                        contract: Optional[str] = None,
                        sender: Optional[str] = TRON_HOT_WALLET_ADDRESS,
                        senderpk: Optional[str] = TRON_HOT_WALLET_PK) -> dict:
        """
        Transfers balance to another address
        params:
        sender: str -> Sender address
        senderpk: str -> Sender Private Key
        receiver: str -> Receiver address (send to this address)
        amount: float -> amount to send
        contract: Optional[str] -> Contract to send, if none assume TRX
        """
        private_key = PrivateKey(bytes.fromhex(senderpk))
        if contract:
            contract = self.client.get_contract(contract)
            decimals = contract.functions.decimals()
            txn = (
                    contract.functions.transfer(receiver, int(amount * pow(10, decimals)))
                    .with_owner(sender)  # address of the private key
                    .fee_limit(10_000_000)
                    .build()
                    .sign(private_key)
                )
            return txn.broadcast()
        else:
            try:
                txn = (
                    self.client.trx.transfer(sender, receiver, int(amount*pow(10, 6)))
                        .build()
                        .sign(private_key)
                )
            except Exception as e:
                coms.error(f"Error sending TRX {e}")
                return False
            else:
                return txn.broadcast() 

Sorry for the delay in the answer.

MrNaif2018 commented 1 year ago

In general sending all balance is a tedious task because it requires to know the fee you will spend with 100% accuracy beforehand

Ideally, you can use BitcartCC for this (only it provides the most reliable fee estimation function), i.e. set up bitcartcc daemon via https://docs.bitcartcc.com/deployment/ or manually:

git clone https://github.com/bitcartcc/bitcart
cd bitcart
pip install -r requirements/base.txt
pip install -r requirements/daemons/trx.txt

TRX_SERVER=https://rpc.ankr.com/http/tron python3 daemons/trx.py

Then using https://github.com/bitcartcc/bitcart-sdk or https://github.com/bitcartcc/bitcart/releases/tag/cli-1.1.0 or any other language, you can do stuff like this:

from bitcart import TRX
from decimal import Decimal

coin = TRX(xpub="yourprivatekey")
request_amount = Decimal(coin.balance()["confirmed"])
estimated_fee = Decimal(coin.server.get_default_fee(coin.server.payto(payout.destination, request_amount, unsigned=True))
request_amount -= estimated_fee
tx_hash = await coin.pay_to("address", request_amount)

https://github.com/bitcartcc/bitcart/blob/11e80ef0aec82a05ceaeca38649b184e46c354d8/daemons/trx.py#L269-L280