Skip to content

lazy loading tronweb in esm module is resulting in ReferenceError: proto is not defined #685

@ice-chillios

Description

@ice-chillios

Importing tronweb dynamically in esm module sometimes doesn't load proto properly and throws ReferenceError. On my quick check over codebase I could assume it's probably caused by some circular dependencies but would need further confirmation

Code

async function loadTokenInfo(address) {
	const TW = await import("tronweb");
	const tronWeb = new TronWeb({
	  fullHost: "<rpcUrl>",
	  // Set a default address for read-only calls (required by TronWeb)
	  privateKey: "0000000000000000000000000000000000000000000000000000000000000001",
	});
	
	const contract = await tronWeb.contract().at(address);
	
	const [symbolResult, decimalsResult] = await Promise.all([
	  contract
	    .symbol()
	    .call()
	    .catch((error: Error) => {
	      console.warn(`Could not fetch symbol for ${address} on Tron:`, error);
	      return undefined;
	    }),
	  contract
	    .decimals()
	    .call()
	    .catch((error: Error) => {
	      console.warn(`Could not fetch decimals for ${address} on Tron:`, error);
	      return 6;
	    }),
	]);
}

Error

22 |         return this;
23 |     }.call(null) ||
24 |     Function('return this')();
25 | 
26 | var core_contract_common_pb = require('../../core/contract/common_pb.cjs');
27 | goog.object.extend(proto, core_contract_common_pb);
                        ^
ReferenceError: proto is not defined
      at <anonymous> (/home/runner/work/sdk/sdk/node_modules/.bun/tronweb@6.1.1+4789783d1fa00420/node_modules/tronweb/lib/esm/protocol/core/contract/balance_contract_pb.cjs:27:20)

::error file=node_modules/.bun/tronweb@6.1.1+4789783d1fa00420/node_modules/tronweb/lib/esm/protocol/core/contract/balance_contract_pb.cjs,line=27,col=20,title=ReferenceError: proto is not defined::%0A     node_modules/.bun/tronweb@6.1.1+4789783d1fa00420/node_modules/tronweb/lib/esm/protocol/core/contract/balance_contract_pb.cjs:27:20)%0A      at <anonymous> (11:43) 

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions