Created
June 7, 2023 10:53
-
-
Save nonseodion/65050518c316cc81cb111faf24ee7ef2 to your computer and use it in GitHub Desktop.
USDT Transfer Gas Fee on Tron
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const bs58 = require('bs58'); | |
const axios = require("axios"); | |
const triggerUrl = "https://api.trongrid.io/wallet/triggerconstantcontract"; | |
const energyUnitUrl = "https://api.trongrid.io/wallet/getenergyprices"; | |
// amount should not include decimals | |
// sender and receiver should be in base58 | |
async function getFee(sender, receiver, amount) { | |
const decimals = 10**6; //USDT decimals | |
amount = Number(amount*decimals).toString(16); | |
amount = Array(64-amount.length).fill(0).join("") + amount; // amount in ABI encoded hex | |
// convert receiver to hex | |
const receiverHex = Buffer.from( | |
bs58.decode(receiver) | |
) | |
.toString('hex') | |
.slice(0,42); | |
const data = { | |
owner_address: sender, | |
contract_address: "TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t", // USDT Address | |
function_selector: "transfer(address,uint256)", | |
parameter: `0000000000000000000000${receiverHex}${amount}`, // abi encoded parameters | |
"call_value": 0, | |
"visible": true | |
} | |
// get latest energy unit price in SUN (WEI equivalent) | |
const energyUnitPriceHistory = (await axios.post(energyUnitUrl)).data.prices; | |
const energyUnitPrice = energyUnitPriceHistory.split(":").pop(); | |
// get energy used for transaction | |
const res = await axios.post(triggerUrl, data); | |
const energyUsed = res.data.energy_used; | |
// get total fee and convert to TRX :fee = energy used * energy unit price, 1000000 SUN = 1 TRX | |
const fee = parseInt(energyUsed) * parseInt(energyUnitPrice) / 1000000 | |
return fee; | |
} | |
feePromise = getFee("TCTxqwRdh1uvzHPXFQMMZ3StJCBA7A5Ycg", "TNb3CCAfd4CmBojKpJVn9iuJ4j52NbWoZ1", 30); | |
feePromise.then(console.log); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment