5

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

2 Answers 2

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.

1
  • 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. Commented Jun 14, 2017 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;
}
0

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.