By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
rocoderesrocoderes
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Notification Show More
Latest News
How to set the dropdown value by clicking on a table row
Javascript – How to set the dropdown value by clicking on a table row
JavaScript
Attempting to increase the counter, when the object's tag exist
Javascript – Attempting to increase the counter, when the object’s tag exist
JavaScript
Cycle2 JS center active slide
Javascript – Cycle2 JS center active slide
JavaScript
Can import all THREE.js post processing modules as ES6 modules except OutputPass
Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass
JavaScript
How to return closest match for an array in Google Sheets Appscript
Javascript – How to return closest match for an array in Google Sheets Appscript
JavaScript
Aa
Aa
rocoderesrocoderes
Search
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Follow US
High Quality Design Resources for Free.
rocoderes > JavaScript > Javascript – Tron fee estimation bofore confirming transaction
JavaScript

Javascript – Tron fee estimation bofore confirming transaction

Admin
Last updated: 2023/12/19 at 5:32 PM
Admin
Share
3 Min Read
Tron fee estimation bofore confirming transaction

Problem:

I am trying to create an application which is actually a Tron(TRX) wallet, how can I make it calculate the fee for every transaction before being confirmed from the user?
I understand the fact that tron uses some BP(Bandwidth Points) every time for making a transaction if possible and it would be amazing if anyone could help with related calculations as well.

Contents
Problem:Solution:

By the way, I am using python to create such application but I don’t mind using javascript if needed, so help of any kind will be appreciated.

I have tried using tronpy and tronapi as google bard recommended but they can’t calculate the transaction fees as long as I know. There might be some ways around using tronweb, but since i am not a professional in javascript and crypto, I couldn’t use it much. Maye some code snippets from experts will help me more with the understanding.

Please feel free to mention some APIs and SDKs that you think will help in any ways.

Solution:

I finally have the trick.

The fee estimation used to return 0.2 TRX at first because I forgot to sign the transaction with private key. It returns 0.267 TRX now by including the private key and signature.

It does not need energy to transfer Tron(TRX) on it’s own unless you start dealing with smart contracts.

the following code should work just perfectly for my needs, hope it can help anyone else having similar issues.

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')

Related

Subscribe to Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Share this Article
Facebook Twitter Email Print
What do you think?
Love0
Sad0
Happy0
Sleepy0
Angry0
Dead0
Wink0
Previous Article Starting Function When Check/Uncheck Checkbox JS Javascript – Starting Function When Check/Uncheck Checkbox JS
Next Article I need to add social media images in the html string I have in the footer of my site done with js [closed] Javascript – I need to add social media images in the html string I have in the footer of my site done with js [closed]
Leave a comment Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

- Advertisement -

You Might Also Like

How to set the dropdown value by clicking on a table row

Javascript – How to set the dropdown value by clicking on a table row

February 11, 2024
Attempting to increase the counter, when the object's tag exist

Javascript – Attempting to increase the counter, when the object’s tag exist

February 11, 2024
Cycle2 JS center active slide

Javascript – Cycle2 JS center active slide

February 10, 2024
Can import all THREE.js post processing modules as ES6 modules except OutputPass

Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass

February 10, 2024
rocoderesrocoderes
Follow US

Copyright © 2022 All Right Reserved By Rocoderes

  • Home
  • About us
  • Contact us
  • Disclaimer
Join Us!

Subscribe to our newsletter and never miss our latest news, podcasts etc.

Zero spam, Unsubscribe at any time.
Welcome Back!

Sign in to your account

Lost your password?