5

I'd like to accept regular transactions to a contract and record a mapping address -> value. Is it possible to achieve it?

5
mapping (address => uint) data;
function () payable {
    data[msg.sender] = msg.value;
}

I think that would do it, but someone else should verify. The function without a name is the default function and handles any call that is not called on a named function.

  • The comma in the mapping should be replaced by an arrow =>. Also, for the OP to store it he would need to create an associative array. – Jossie Calderon Jun 14 '17 at 7:36
2

Improving on Thomas Jay Rush's code (it's now missing the payable modifier as of 09/2016):

mapping (address => uint) data
function () payable {
    data[msg.sender] = msg.value;
}

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.