Tronweb - Update percentage of energy provided by the Smart Contract Owner

TRON provides a way to provide energy to a smart contract on behalf of the users to keep a dapp free from energy fees.

Using Tronweb this can be updated the following way:

const TronWeb = require('tronweb');
const tronWeb = new TronWeb({
  fullHost: 'https://api.trongrid.io',
  privateKey: '<fill in private key>'
})
const contractAddress = "412bcbeb2cb10e22a1bb08143310cddf5a1e49b18e";
const percentage = 50;
const updateSetting = await tronWeb.transactionBuilder.updateSetting(contractAddress, percentage);
const signedTransaction = await tronWeb.trx.sign(updateSetting);
const result = await tronWeb.trx.sendRawTransaction(signedTransaction);

To verify if the percentage was changed you can call https://api.trongrid.io/wallet/getcontract?value=contract_address which contains the field consume_user_resource_percent that shows the configured percentage

2 Likes
async function updatefee(){

    try {
      const updatesetting = await tronWeb.transactionBuilder.updateSetting("41ce94ff1b321b765052bd1c83949b3aab38c830f2", "412bcbeb2cb10e22a1bb08143310cddf5a1e49b18e", 50, 1);
      const signedTransaction = await tronWeb.trx.sign(updatesetting);
      const result = await tronWeb.trx.sendRawTransaction(signedTransaction);
      console.log('success ');
    } catch (err) {
      console.log(err);
    }
  }

I don’t know what is wrong in it. Every time it says Invalid contract address provided.
can you check it please sir?

Looks like i made a mistake in the example. The updateSetting call is as follows:

const contractAddress = "412bcbeb2cb10e22a1bb08143310cddf5a1e49b18e";
const percentage = 50;
updateSetting(contractAddress, percentage);

I’ve updated the example code

1 Like

It worked and receive success message. But still contract isn’t paying energy.
I had already set user fee while deploying the contract but doesn’t working in both case.

You can verify if it worked by calling https://api.trongrid.io/wallet/getcontract?value=412bcbeb2cb10e22a1bb08143310cddf5a1e49b18e

The response contains the field consume_user_resource_percent that shows the configured percentage.

Your contract looks to be configured at 60% now


please check my last transaction. Energy Usage from Contract Provided:0

The contract creator (https://tronscan.org/#/address/TUoWh2ywDjzZYos8LdA1Sk64rUEeTiaDMz) needs to freeze TRX for energy in order to be able to provide energy. Right now you haven’t frozen any TRX so the contract can’t provide any energy

Huge help sir. Thank you so much

2 Likes