Created
April 4, 2019 07:59
-
-
Save wanseob/717a4faeeb346fdce92f6e5cffa25c73 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import "../MerkluxReducer.sol"; | |
contract TransferReducer is MerkluxReducer { | |
function reduce( | |
IStateTree _tree, | |
address _from, | |
bytes memory _encodedParams // encoded params | |
) public returns ( | |
bytes memory _encodedPairs // encoded key value pairs | |
) { | |
// 1. Decode data | |
(uint _amount, address _to) = abi.decode(_encodedParams, (uint, address)); | |
// 2. Calculate | |
bytes memory _senderKey = abi.encodePacked(_from); | |
bytes memory _receiverKey = abi.encodePacked(_to); | |
uint _senderBalance = _tree.read(_senderKey).toRlpItem().toUint(); | |
uint _receiverBalance = _tree.read(_to).toRlpItem().toUint(); | |
require(_senderBalance >= _amount); | |
// 3. Return pairs to update | |
ReducerUtil.RlpData memory pairs; | |
pairs = pairs.addUint(_senderKey, _senderBalance - amount); | |
pairs = pairs.addUint(_receiverKey, _receiverBalance + _amount); | |
return pairs.encode(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment