I am tring to transfer my TOKEN from metamask to another address, but I am stuck at the following error and am unable to find a solution.
Error: Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'transfer')
I have tried changing the ABI but it doesn't work...
Here is the code:
<!DOCTYPE html>
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://unpkg.com/@metamask/legacy-web3@latest/dist/metamask.web3.min.js"></script>
<script src="contractAbi.js"></script>
</head>
<body>
<div>
<button class="pay-button">Pay</button>
<div id="status"></div>
</div>
<script type="text/javascript">
window.addEventListener('load', async () => {
if (typeof window.ethereum !== 'undefined') {
ethereum.request({ method: 'eth_requestAccounts' });
}
else {
alert('Please install metamask');
}
const web3 = new Web3('https://bsc-dataseed.binance.org/');
const contractAddress = '0xbdc6b4f65eae62574551951dddab27bfcd976c5c';
const reciever = '';
const amount = 10;
const sendEthButton = document.querySelector('.pay-button');
sendEthButton.addEventListener('click', () => {
(async () => {
const contract = new web3.eth.contract(ABI, contractAddress);
if (window.ethereum.chainId == '0x38') {
await contract.methods.transfer(reciever, amount).send('from',ethereum.selectedAddress).on('receipt', (receipt) => {
console.log(receipt);
})
}
else {
ethereum.request({ method: 'wallet_switchEthereumChain', params: [{ chainId: '0x38' }] });
}
})();
});
})
</script>
</body>
</html>