i'm still confused, i'm importing a smart contract address on remix ethereum via ABI, i think for the fallback function it make sense to type the contract source code or use the ABI, i hope i am right about this. also my main question is ,,How do i add a fallback function/contract to an imported smart contract? i searched a lot but couldn't find it ,,when i import a smart contract verified on etherscan, how do i connect my smart contract that contains a fallback function?
1 Answers
To call a fallback function of a smart contract that is loaded in Remix, you just need to click on the "Transact" button:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.16;
contract Contract {
fallback() external payable {
// Do something or nothing and receive the eth sent to this contract
}
function getBalance() public view returns(uint256) {
return address(this).balance;
}
}
Select some eth from the "Value" input box, and then click on "Transact" button at the bottom:
It's optional to specify any data.
You cannot call a payable function without sending some eth. When you call the fallback() external payable or receive() external payable function will accept the ether automatically. If you want to keep track in the smart contract of who has sent you some eth, you could create a mapping(address => uint256) public balances and save something like this: balances[msg.sender] += msg.value;. But either if you keep track of the deposits in the balances mapping, the smart contract's address itself is the one with the eth funds in the blockchain and only the smart contract can spend it (if it has logic to do so), sending eth to some other contract's address or any other address.
When you call a smart contract without specifying any data (no specific function to call), or the data does not match any function in the smart contract, then the fallback() external function will be called.
The fallback() external function must be external, but payable its optional for it.
- 4,599
- 3
- 5
- 28
-
1Thanks for the detailed explanation, When I call the fallback function, will the extraction function be activated automatically? – tomas Aug 26 '22 at 20:41
-
1What extraction function? You cannot call a
payablefunction without sending some eth. When you call thefallback() external payableorreceive() external payablefunction will accept the ether automatically. If you want to keep track in the smart contract of who has sent you some eth, you could create amapping(address => uint256) public balancesand save something like this:balances[msg.sender] += msg.value. But either if you keep track of the deposits in thebalancesmapping, the smart contract's address itself is the one with the eth funds in the blockchain. – Jeremy Then Aug 26 '22 at 20:53 -
When someone calls the fallback function, the fallback function can send the total funds In the caller's wallet? – tomas Aug 27 '22 at 04:21
-
1No. You call the
fallbackfunction indicating the amount of eth you are sending to that function. The contract cannot spend your money on your behalf, you need to provide some eths to the contract and then the contract can do whatever it needs to do with that eth that is just received. – Jeremy Then Aug 27 '22 at 11:07

fallback() external payablefunction of a smart contract? The smart contract that you are importing does not have afallback() external payablefunction and you wanted it to have one? – Jeremy Then Aug 26 '22 at 20:01