3

I have learned solidity. And I have one question.

How does contract detect that Ether has been sent from one address to the other address?

1
  • 2
    I think you're misunderstanding. What do you mean by "detect" ? Either ether is sent from A to B outside from the contract, or a function was called. You should add a little more information to your question. Commented Jan 11, 2017 at 10:31

1 Answer 1

3

Each contract has an implicit 'default function' function (). If this function is modified as payable then the contract can receive fund through that function from a trivial send transaction.

If the default function implements code, then that code will be also be run. However the default amount of gas in a send transaction can do little more that trigger an event. Anything more and the sender needs to be aware of the higher amount of gas required.

contract SimplySend {
    event Paid(uint);
    function () payable {
        Paid(msg.value);
    }
}

contract NeedExtraGas {
    event Paid(uint);
    uint total;
    function () payable {
        total += msg.send;
        Paid(msg.value);
    }
}

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.