Skip to content

Instantly share code, notes, and snippets.

@nonseodion
Created June 7, 2023 10:53
Show Gist options
  • Save nonseodion/65050518c316cc81cb111faf24ee7ef2 to your computer and use it in GitHub Desktop.
Save nonseodion/65050518c316cc81cb111faf24ee7ef2 to your computer and use it in GitHub Desktop.
USDT Transfer Gas Fee on Tron
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