Metamask: Eth ERC-20 Token Valuation Problem
I’m running a test suite with Hardhat that uses MetaMask for web3 interactions. The problem is relatively rare, but some users have reported it. Here is a step-by-step guide to solving the problem and its causes.
Step 1: understanding the problem
When you select your contract token from the Metamask settings in the Hardhat test and then press “send” to transfer Ether (ETH), you will notice strange behavior in the console output. In particular, you may see an error message similar to this:
Error: Invalid call metadata
This can be quite annoying if it affects the functionality of your test.
Step 2: Investigation and identification of the problem
To identify the problem, follow these steps:
: Make sure your contract has at least one function that can be called using the eth_sendRawTransaction
method (eg transfer()
). You can test this by running your contract on the local network using npx hardhat run scripts/contract.js
.
Step 3: Testing and debugging
For testing and debugging, follow these additional steps:
eth_sendRawTransaction
, passing only the token address as an argument (eg eth_sendRawTransaction('0x...')
). This should start all necessary transactions.const tx = await yourContract.methods.transfer(0x...).send({ from: '0x...' });
console.log(tx);
: Carefully examine the console output for possible errors or warnings.
Step 4: Elimination and refactoring
If you find an error, try to fix it as follows:
Usage example
To reproduce this problem, you can create a simple contract like this:
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 completed successfully');
resolve();
} else {
console.error('Error:', error);
reject(error);
}
});
});
}
In this example, replace the placeholder `YOUR_PROJECT_ID’ with your actual Infura project ID.
Conclusion
The Metamask token valuation problem can be difficult to diagnose and solve. This guide should help you identify the problem by following these steps. However, if you’re still having trouble or need more help, please reach out to our community forum or support team for help.