Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error fetching balance: TypeError: Cannot read properties of undefined (reading 'tronWeb') #564

Open
0xcodeTest opened this issue Oct 3, 2024 · 1 comment

Comments

@0xcodeTest
Copy link

`// 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.

@Satan-web3
Copy link
Contributor

It seem like there's no wrong with your code. Did you put all you code up here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants