0

One thing I try to understand is the limitation of smart contracts. I was looking into ICOs and I always asked a question:

Is it possible to implement an 'enterICO' function without requiring users to call the function?

I will try to explain it with an example out of context:

pragma solidity ^0.4.17;

contract ICO {
    string message; /// just assume we have a function which allows us to set a message 

    function enterIco() payable public {
        if(msg.value == 0.00000001 ether)
        {
            return message; 
        }
    } 

}

For this kind of example, we can assume that on front-end you will have some web3js which will allow you to connect MetaMask on user side and call the enterIco function when a user clicks on the button.

Can we make this function run automatically when the user doesn't have access to the button? For example, the user sends Ethereum from their own wallet to the contract address without clicking on the button and function is automatically executed.

This example can be referred to ICOs. Could a user use their wallet and smart contract address to participate in the ICO?

P.S I know ICOs shouldn't be implemented this way because they require KYC however I am curious to know if they could or if solidity limits this kind of behaviour.

1

Yes, you can do this via the fallback function.

If a contract is called with empty input data, or with a function signature that doesn't match any function on the contract, the fallback function is executed. You can then call any other function you want in that function, something like:

function() public payable {
    enterICO();
}
  • Every day Solidity continues to impress me more and more. Thanks a lot, looking to do some testing with it! – Cristian Sep 24 '18 at 0:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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