I needed to enable the users to send some Ethereum from their metamask wallet to the smart contract which they want to buy some tokens of via frontend. Based on metamask docs this is how one can call the send function of metamask in frontend:
window.ethereum.request({
method: "eth_sendTransaction",
params: [
{
from: metamaskWalletAddress, // The user's active address.
to: tokenAddress, // Address of the recipient.
value: userDesiredAmount,
gasLimit: "0x5028", // Customizable by the user during MetaMask confirmation.
maxPriorityFeePerGas: "0x3b9aca00", // Customizable by the user during MetaMask confirmation.
maxFeePerGas: "0x2540be400", // Customizable by the user during MetaMask confirmation.
}],
})
.then((txHash: any) => console.log("txHash: ", txHash))
.catch((error: any) => console.error("errorL ", error));
However, as @petr-hejda said, the token contract needs to have receive()
and fallback()
functions as well to be able to get the Ethereum.