Error fetching balance: TypeError: Cannot read properties of undefined (reading 'tronWeb')
Opened this issue · 1 comments
`// Load dependencies
import TronWeb from 'tronweb';
import xToken from './build/contracts/xToken.json' with { type: 'json' }; //importing the JSON
// private key
const privateKey = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
// Initialize TronWeb
const tronWeb = new TronWeb({
fullNode: 'https://api.shasta.trongrid.io',
solidityNode: 'https://api.shasta.trongrid.io',
eventServer: 'https://api.shasta.trongrid.io',
privateKey: privateKey,
});
// default address based on the private key
const defaultAddress = tronWeb.address.fromPrivateKey(privateKey);
tronWeb.defaultAddress.base58 = defaultAddress;
console.log('Default Address:', tronWeb.defaultAddress.base58); // Printing the address
console.log('Derived Address from Private Key:', defaultAddress); // Confirm derived address
// contract address
const contractAddress = 'Txxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxd';
// Create contract instance using the ABI
const contract = tronWeb.contract(xToken.abi, contractAddress);
// Function to mint tokens
async function mintTokens() {
const amountToMint = 10000 * (10 ** 6); // Amount to mint, adjusted for decimals
try {
const tx = await contract.mint(tronWeb.defaultAddress.base58, amountToMint).send();
console.log('Mint Transaction:', tx);
} catch (error) {
console.error('Error minting tokens:', error);
}
}
// Function to check the balance
async function checkBalance() {
try {
const balance = await contract.balanceOf(tronWeb.defaultAddress.base58).call();
console.log('Token Balance:', balance.toString());
} catch (error) {
console.error('Error fetching balance:', error);
}
}
// Call the functions
(async () => {
await mintTokens(); // Call mintTokens first
await checkBalance(); // Then check balance
})();
`
Result Error:
Default Address: T-------------------------------------------------2
Derived Address from Private Key: T------------------------------------------2
Error minting tokens: Private key does not match address in transaction
Error fetching balance: TypeError: Cannot read properties of undefined (reading 'tronWeb')
at Ye (C:\Users\x\node_modules\tronweb\dist\TronWeb.node.js:1:19537)
at C:\Users\x\node_modules\tronweb\dist\TronWeb.node.js:1:32013
at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
Can i get any solution or a hint on how to fix this issue.
It seem like there's no wrong with your code. Did you put all you code up here?