1

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?

7
  • 1
    I don't think I quite understand what you mean. You want to "Load contract from address" in Remix, so you can play around with it, right? If this is the case, then you will need either the contract interface (if it implements one) or the contract source code or abi, so Remix knows how to interact with it. If you put the interface or contract source code in remix, then you select it and put the contract address in the "Load contract from address" box in remix, then click on "At address" and it should load. That loaded contract will be the one already running in Rinkebe. Commented Aug 26, 2022 at 15:11
  • what you tell, this is importing a smart contract, yes that's what I'm doing, My main question is, how do I add a fallback contract to an imported contract? Because I want to call the fallback function and interact with the smart contract
    – tomas
    Commented Aug 26, 2022 at 19:42
  • 1
    I still cannot understand what you mean. Could you add more details to your question? Code snippets, screenshots, etc. Commented Aug 26, 2022 at 19:47
  • OK, I'll try to explain better, I have done the smart contract import successfully, But I want to work with smart contract with fallback function how to do it?
    – tomas
    Commented Aug 26, 2022 at 19:55
  • Need to create a new fallback contract? To interact with a smart contract?
    – tomas
    Commented Aug 26, 2022 at 19:56

1 Answer 1

1

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:

enter image description here

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
  • 1
    Thanks for the detailed explanation, When I call the fallback function, will the extraction function be activated automatically?
    – tomas
    Commented Aug 26, 2022 at 20:41
  • 1
    What extraction function? 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. Commented Aug 26, 2022 at 20:53
  • When someone calls the fallback function, the fallback function can send the total funds In the caller's wallet?
    – tomas
    Commented Aug 27, 2022 at 4:21
  • 1
    No. You call the fallback function 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. Commented Aug 27, 2022 at 11:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.