pragma solidity >=0.7.0 <0.9.0;
contract StructMapping {
mapping(address => uint) public balanceReceived;
function getBalance() public view returns(uint) {
return address(this).balance;
}
function sendMoney() public payable {
balanceReceived[msg.sender] += msg.value;
}
function withdrawAllMoney(address payable _to) public {
uint balanceToSend = balanceReceived[msg.sender];
balanceReceived[msg.sender] = 0;
_to.transfer(balanceToSend);
}
}
requirements: remix web ide, solidity file with above code opened in remix, two metamask wallets
steps to reproduce:
- save file and open through remix
- injected web3 (connect metamask wallet A)
- deploy contract
- send from wallet A to the deployed contract using remix interactive feature
- copy address of the contract and send from metamask wallet B
When I try to perform step 5 the transaction in my metamask from wallet b which is on a different machine fails says not enough gas. How can I send from this remote wallet and have it interact with the contract, im assuming it is just sending it like its a wallet address and timesout.
Best Answer
When you send money to a smart contract, you'll have to call a payable function. It's not possible directly via Metamask, as Metamask merely sends a transaction without any data unless specifically requested to do so via its API. To send send money, you can change your Metamask to wallet B and call
sendMoney()
function of the contract along with some ETH.If you want your smart contract to accept money without being called via specific function, you can define a
receive
function like this.This tells the contract to accept payment (those sent as normal transactions as you mentioned). This is a security feature of solidity to safeguard accidental sending of ETH to smart contracts which might not have a withdrawal mechanism.