//Write your own contracts here. Currently compiles using solc v0.4.15+commit.bbb8e64f. pragma solidity ^0.4.18; contract SimpleStore { struct LockedBalance { uint32 expiration; uint96 totalAmount; } /// @dev Tracks an account's info. struct AccountInfo { /// @dev The amount of ETH which has been unlocked already. uint128 freedBalance; /// @dev The first applicable lockup bucket for this account. uint128 lockupStartIndex; /// @dev Stores up to 25 buckets of locked balance for a user, one per hour. mapping(uint256 => uint256) lockups; /// @dev Returns the amount which a spender is still allowed to withdraw from this account. mapping(address => uint256) allowance; } /// @dev Stores per-account details. mapping(uint256 => AccountInfo) private accountToInfo; function set(uint256 key, uint256 lockupStartIndex, uint32 expiration, uint96 totalAmount) public { setLockedBalance(accountToInfo[key], lockupStartIndex, expiration, totalAmount); } function getExpiration(uint256 key, uint256 lockupStartIndex) public constant returns (uint32) { return getLockedBalance(accountToInfo[key], lockupStartIndex).expiration; } function getTotal(uint256 key, uint256 lockupStartIndex) public constant returns (uint96) { return getLockedBalance(accountToInfo[key], lockupStartIndex).totalAmount; } function getLockedBalance(AccountInfo storage accountInfo, uint256 lockupStartIndex) private view returns (LockedBalance memory) { uint256 lockupMetadata = accountInfo.lockups[lockupStartIndex/2]; LockedBalance memory balance; if (lockupMetadata == 0){ return balance; } uint128 lockedBalanceBits; if (lockupStartIndex % 2 == 0){ // lockedBalanceBits = uint128((lockupMetadata & (bytes32(2 ** 128 -1) * 2 ** 128)) >> 128); // batu = uin128(bytes32(lockupMetadata) & bytes32(2 ** 128 - 1) >> 128); lockedBalanceBits = uint128(lockupMetadata >> 128); } else { lockedBalanceBits = uint128(lockupMetadata % 2 ** 128); } // balance.expiration = lockedBalanceBits & uint128(bytes1(2 ** 31)) * 2 ** 96; balance.expiration = uint32(lockedBalanceBits >> 96); balance.totalAmount = uint96(lockedBalanceBits % 2 ** 96); // console.log("BATU getLockedBalance: ", balance.expiration, balance.totalAmount); return balance; } function setLockedBalance( AccountInfo storage accountInfo, uint256 lockupStartIndex, uint32 expiration, uint96 totalAmount) private { uint128 lockedBalanceBits = uint128(totalAmount) | uint128(expiration) << 96; // console.logBytes16(bytes16(uint128(totalAmount))); // console.logBytes16(bytes16(uint128(expiration) << 96)); // console.logBytes16(bytes16(lockedBalanceBits)); if (lockupStartIndex % 2 == 0){ // console.logBytes32(bytes32(lockedBalanceBits << 128)); accountInfo.lockups[lockupStartIndex/2] = accountInfo.lockups[lockupStartIndex/2] | (uint256(lockedBalanceBits) << 128); } else { // console.logBytes32(bytes32(lockedBalanceBits)); accountInfo.lockups[lockupStartIndex/2] = accountInfo.lockups[lockupStartIndex/2] | uint256(lockedBalanceBits); } // console.logBytes32(bytes32(accountInfo.lockups[lockupStartIndex/2])); // console.log("BATU setLockedBalance for: ", expiration, totalAmount); // getLockedBalance(accountInfo, lockupStartIndex); } uint value; }
0.4.18