tronprotocol / tronweb

Javascript API Library for interacting with the TRON Network
MIT License
388 stars 259 forks source link

Error tronWeb.trx.getBalance(address) #501

Closed kandrukKV closed 3 months ago

kandrukKV commented 3 months ago

Hi

I'm just getting started with TRON blockchain, but I noticed one problem. Starting from version 5.3.0 Tronweb gives an error: "Invalid address provided" if I try to execute this code:

tronWeb.trx.getBalance('TTSFjEG3Lu9WkHdp4JrWYhbGP6K1REqnGQ').then(result => console.log(result))

and

tronWeb.isAddress('TTSFjEG3Lu9WkHdp4JrWYhbGP6K1REqnGQ') // false

start940315 commented 3 months ago

As I've tested your code with TronWeb@5.3.0, it outputted the right result. So did you change TronWeb code?

kandrukKV commented 3 months ago

It's very strange.

My code is very simple. Please take a look at it here:

https://github.com/kandrukKV/tron-examples/blob/main/app.vue

Now there is 5.3.2 version and it isn't work.

If you change version to 5.2.0 it will work.

start940315 commented 3 months ago

Since nuxt don't polyfill crypto, you need to add alias to it in your nuxt.config.ts:

alias: {
    'crypto': 'crypto-browserify',
    'buffer': 'buffer/',
    'stream': 'readable-stream',
  },
  app: {
    head: {
      script: [{ children: "window.global ||= window;" }],
    },
  },
kandrukKV commented 3 months ago

Thank you, you helped me a lot.

I added your changes to the nuxt.config, but I still had this error.

Снимок экрана 2024-04-15 в 18 18 10

This is the only way I've been able to solve this problem.:

import { Buffer } from 'buffer'; window.Buffer = Buffer

start940315 commented 3 months ago

Yes you have to do this if you want to get trx20 balance, because TronWeb uses Buffer internally.

kandrukKV commented 3 months ago

Thank you!