Skip to content

Instantly share code, notes, and snippets.

@itoonx
Created November 26, 2024 02:35
Show Gist options
  • Save itoonx/c231f009162d648b363951c3ca2e97d4 to your computer and use it in GitHub Desktop.
Save itoonx/c231f009162d648b363951c3ca2e97d4 to your computer and use it in GitHub Desktop.
import requests
from tronpy import Tron
import trontxsize
from tronpy.keys import PrivateKey
def get_bandwidth_price():
url = "https://api.trongrid.io/wallet/getchainparameters"
response = requests.get(url)
data = response.json()
bandwidth_price = data['chainParameter'][3]['value'] / 1000_000
return bandwidth_price
def create_transaction(sender_address, recipient_address, amount):
priv_key = PrivateKey(bytes.fromhex("Your_Private_Key"))
tron = Tron()
txn = (
tron.trx.transfer(sender_address, recipient_address, amount)
.build()
.inspect()
.sign(priv_key)
)
return txn
def calculate_transaction_fee(transaction):
bandwidth_price = get_bandwidth_price()
txsize = trontxsize.get_tx_size({"raw_data": transaction._raw_data, "signature": transaction._signature})
bandwidth_points = txsize * bandwidth_price
total_fee = bandwidth_points
return total_fee
if __name__ == "__main__":
sender_address = 'Wallet_Address'
recipient_address = "recipient_address"
amount = 1000000 # Replace with your amount
transaction = create_transaction(sender_address, recipient_address, amount)
fee = calculate_transaction_fee(transaction)
print(f'Transaction Fee: {fee} TRX')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment