Metamask: Eth ERC-20 Token Estimation Issue
I’m running a test suite with Hardhat that uses MetaMask for web3 interactions. The issue is relatively rare but has been reported by some users. Here’s a step-by-step guide to resolve the problem and what caused it.
Step 1: Understand the Issue
When you select your contract’s token from the Metamask settings in your Hardhat test, then click on “send” to transfer Ether (ETH), you will notice strange behavior in the console output. Specifically, you might see an error message like this:
Error: Invalid call metadata
This can be quite frustrating if it affects your test’s functionality.
Step 2: Investigate and Identify the Issue
To identify the issue, follow these steps:
- Check MetaMask settings: Ensure that you have selected your contract’s token in the Metamask settings. The “send” button should be labeled with your contract’s address.
- Verify contract functions: Make sure your contract has at least one function that can be called using the
eth_sendRawTransaction
method (e.g.,transfer()
). You can check this by running your contract on the local network usingnpx hardhat run scripts/contract.js
.
- Check transaction details: Inspect the transaction details in the console output to confirm that it matches what you expected.
Step 3: Test and Debug
To test and debug, follow these additional steps:
- Test with a simple call
: Call your contract’s function using
eth_sendRawTransaction
with just the token address as an argument (e.g.,eth_sendRawTransaction('0x...')
). This should trigger any necessary transactions.
- Add console logging: Add some console logging to verify that the transaction is being created and sent correctly:
const tx = await yourContract.methods.transfer(0x...).send({ from: '0x...' });
console.log(tx);
- Inspect console output
: Take a closer look at the console output for any errors or warnings that may be triggered.
Step 4: Resolve and Refactor
If you find an error, try to fix it by:
- Checking for typos in contract functions or transaction details.
- Ensuring that your contract is correctly implemented and deployed on the network.
- Verifying that your test suite is not causing any conflicts with other tests.
Example Use Case
To reproduce this issue, you can create a simple contract like so:
const Web3 = require('web3');
const web3 = new Web3(new Web3.providers.HttpProvider('
const MyContract = artifacts.require('MyERC20Contract');
module.exports function main() {
return new Promise((resolve, reject) => {
const contractAddress = '0x...';
web3.eth.sendTransaction({
from: '0x...', // sender's address
to: contractAddress,
value: web3.toWei(1, 'ether'),
gas: 20000,
}, (error, transactionHash) => {
if (!error && transactionHash !== null) {
console.log('Transaction successful');
resolve();
} else {
console.error('Error:', error);
reject(error);
}
});
});
}
In this example, replace the YOUR_PROJECT_ID
placeholder with your actual Infura project ID.
Conclusion
Metamask’s token estimation issue can be challenging to diagnose and resolve. This guide should help you identify the problem by following these steps. However, if you’re still facing issues or need further assistance, please reach out to our community forum or support team for help.