Giter Site home page Giter Site logo

Comments (8)

joadr avatar joadr commented on July 23, 2024 30

Why would you need to set an address for calling a balanceOf?

from tronweb.

 avatar commented on July 23, 2024 23

Setting the private key is not a great advise to be honest. You should try to avoid keeping your private key available as a string in your application. Keep it on a secure device for example. Not in the memory of your software. Anyway, this issue can also be fixed by applying the following code:

tronWeb.setAddress('TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t');

So that would be:

const TronWeb = require('tronweb')

const tronWeb = new TronWeb({
  fullNode: 'https://api.trongrid.io',
  solidityNode: 'https://api.trongrid.io',
})

// set the owner address
tronWeb.setAddress('TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t');

// read trc20 account balance
let contract = await tronWeb.contract().at("TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t"); 
let result = await contract.balanceOf('TZ13rQk53DDvK2pR4nfBLyuYTR4EGAFBwT').call();```

from tronweb.

Jo-Chris avatar Jo-Chris commented on July 23, 2024 8

Any explanation as to why we have to set an address here?

from tronweb.

CQBinh avatar CQBinh commented on July 23, 2024 2

@XiChaoSir if you are developing at serverside, let pass the privateKey to TronWeb:

const tronWeb = new TronWeb({
  fullNode,
  solidityNode,
  eventServer,
  privateKey
})

from tronweb.

MonchiLin avatar MonchiLin commented on July 23, 2024 2

In addition to the above methods, you can also use transactionBuilder.triggerSmartContract.

const balanceOfRes = await this.tronWeb.transactionBuilder
  .triggerSmartContract(
    "Contract Address",
    "balanceOf(address)",
    options,
    [
      {
        type: 'address',
        value: "Balance Of Wallet Address"
      }
    ],
    "Wallet Address"
);

For example:

// Get USDT balance of TSu8rSjd86uhtY7EVNazZsuEA7p1DdohZ8
const balanceOfRes = await this.tronWeb.transactionBuilder
  .triggerSmartContract(
    "TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t",
    "balanceOf(address)",
    options,
    [
      {
        type: "address",
        value: "TSu8rSjd86uhtY7EVNazZsuEA7p1DdohZ8"
      }
   ],
    "TSu8rSjd86uhtY7EVNazZsuEA7p1DdohZ8"
);

Docs: https://developers.tron.network/reference/tronweb-triggersmartcontract

from tronweb.

itxtoledo avatar itxtoledo commented on July 23, 2024 1

tron is funny

from tronweb.

kricha avatar kricha commented on July 23, 2024

any news here?

from tronweb.

G33kNoob avatar G33kNoob commented on July 23, 2024

tronWeb.setAddress('TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t');

worked thanks

from tronweb.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.