After reading the documentation for Solidity v0.6.0 docs, I still don't understand the meaning of the fallback functions. I read that it was split into 2 functions: fallback () external payable and receive () external payable. That they are anonymous and do not accept any parameters, and in the overwhelming majority of cases, receive () external payable is used to receive funds. Can you please explain with the example of my code, some use cases for these functions, in order to understand all their features, otherwise somehow everything is in a vacuum, but I understand that this is an important concept? Even the meaning of the receive () external payable function is not clear, in which I call on the buyToken () method, why is it needed if I call on the buyToken () in the Remix directly, bypassing the receive () external payable since she is not visible and anonymous.
pragma solidity ^0.7.0;
// SPDX-License-Identifier: MIT
contract BuyToken {
mapping(address => uint256) public balances;
address payable wallet;
event Purchase(
address indexed buyer,
uint256 amount
);
constructor(address payable _wallet) {
wallet = _wallet;
}
fallback() external payable {
}
receive() external payable {
buyToken();
}
function buyToken() public payable {
balances[msg.sender] += 1;
wallet.transfer(msg.value);
emit Purchase(msg.sender, 1);
}
}