contract SnapshotToken is BurnableToken, MintableToken, PausableToken {
    using SafeMath for uint256;

    /** 
    * @dev `Checkpoint` is the structure that attaches a block number to a
    * given value, the block number attached is the one that last changed the value
    */
    struct Checkpoint {
        // `fromBlock` is the block number that the value was generated super.mint(_to, _amount); from
        uint128 fromBlock;
        // `value` is the amount of tokens at a specific block number
        uint128 value;
    }
 
    // Tracks the history of the `totalSupply` of the token
    Checkpoint[] public totalSupplyHistory;

    // `balances` is the map that tracks the balance of each address, in this
    //  contract when the balance changes the block number that the change
    //  occurred is also included in the map
    mapping (address => Checkpoint[]) public balances;

    /**
    * @notice Send `_amount` tokens to `_to` from `msg.sender`
    * @param _to The address of the recipient
    * @param _amount The amount of tokens to be transferred
    * @return Whether the transfer was successful or not
    */
    function transfer(address _to, uint256 _amount) public whenNotPaused returns (bool success) {
        doTransfer(msg.sender, _to, _amount);
        return true;
    }

    /**
    * @notice Send `_amount` tokens to `_to` from `_from` on the condition it
    *  is approved by `_from`
    * @param _from The address holding the tokens being transferred
    * @param _to The address of the recipient
    * @param _amount The amount of tokens to be transferred
    * @return True if the transfer was successful
    */
    function transferFrom(address _from, address _to, uint256 _amount) public whenNotPaused returns (bool success) {
        // The standard ERC 20 transferFrom functionality
        require(allowed[_from][msg.sender] >= _amount, "amount > allowance");
        allowed[_from][msg.sender] = allowed[_from][msg.sender].sub(_amount);

        doTransfer(_from, _to, _amount);
        return true;
    }

    /**
    * @param _owner The address that's balance is being requested
    * @return The balance of `_owner` at the current block
    */
    function balanceOf(address _owner) public view returns (uint256 balance) {
        return balanceOfAt(_owner, block.number);
    }

    /**
    * @notice `msg.sender` approves `_spender` to spend `_amount` tokens on
    *  its behalf. This is a modified version of the ERC20 approve function
    *  to be a little bit safer
    * @param _spender The address of the account able to transfer the tokens
    * @param _amount The amount of tokens to be approved for transfer
    * @return True if the approval was successful
    */
    function approve(address _spender, uint256 _amount) public whenNotPaused returns (bool success) {
        // To change the approve amount you first have to reduce the addresses`
        //  allowance to zero by calling `approve(_spender,0)` if it is not
        //  already 0 to mitigate the race condition described here:
        //  https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
        require((_amount == 0) || (allowed[msg.sender][_spender] == 0), "reset allowance required");

        allowed[msg.sender][_spender] = _amount;
        emit Approval(msg.sender, _spender, _amount);
        return true;
    }

    /**
    * @dev This function makes it easy to get the total number of tokens
    * @return The total number of tokens
    */
    function totalSupply() public view returns (uint256) {
        return totalSupplyAt(block.number);
    }

    /**
    * @dev Queries the balance of `_owner` at a specific `_blockNumber`
    * @param _owner The address from which the balance will be retrieved
    * @param _blockNumber The block number when the balance is queried
    * @return The balance at `_blockNumber`
    */
    function balanceOfAt(address _owner, uint256 _blockNumber) public view returns (uint256) {
        return balanceAt(_owner, _blockNumber, false);
    }

    /**
    * @notice Total amount of tokens at a specific `_blockNumber`.
    * @param _blockNumber The block number when the totalSupply is queried
    * @return The total amount of tokens at `_blockNumber`
    */
    function totalSupplyAt(uint256 _blockNumber) public view returns(uint256) {
        return balanceAt(address(0), _blockNumber, true);
    }

    /**
    * @dev Generates `_amount` tokens that are assigned to `_to`
    * @param _to The address that will be assigned the new tokens
    * @param _amount The quantity of tokens generated
    * @return True if the tokens are generated correctly
    */
    function mint(address _to, uint256 _amount) public hasMintPermission canMint returns (bool) {
        uint256 curTotalSupply = totalSupply();

        uint256 previousBalanceTo = balanceOf(_to);

        updateValueAtNow(totalSupplyHistory, curTotalSupply.add(_amount));
        updateValueAtNow(balances[_to], previousBalanceTo.add(_amount));

        emit Mint(_to, _amount);
        emit Transfer(0, _to, _amount);
        return true;
    }

    /**
    * @notice Burns `_amount` tokens from `msg.sender`
    * @param _amount The quantity of tokens to burn
    * @return True if the tokens are burned correctly
    */
    function burn(uint256 _amount) public {
        uint256 curTotalSupply = totalSupply();
        uint256 previousBalanceFrom = balanceOf(msg.sender);

        updateValueAtNow(totalSupplyHistory, curTotalSupply.sub(_amount));
        updateValueAtNow(balances[msg.sender], previousBalanceFrom.sub(_amount));

        emit Burn(msg.sender, _amount);
        emit Transfer(msg.sender, 0, _amount);
    }

    /*** INTERNAL/PRIVATE ***/
    /**
    * @dev This is the actual transfer function in the token contract, it can
    *      only be called by other functions in this contract.
    * @param _from The address holding the tokens being transferred
    * @param _to The address of the recipient
    * @param _amount The amount of tokens to be transferred
    * @return True if the transfer was successful
    */
    function doTransfer(address _from, address _to, uint256 _amount) internal {
        if (_amount == 0) {
            emit Transfer(_from, _to, _amount);    // Follow the spec to louch the event when transfer 0
            return;
        }

        // Do not allow transfer to 0x0 or the token contract itself
        require((_to != address(0)) && (_to != address(this)), "cannot transfer to 0x0 or self");

        // If the amount being transfered is more than the balance of the
        //  account the transfer throws
        uint256 previousBalanceFrom = balanceOfAt(_from, block.number);
        require(previousBalanceFrom >= _amount, "amount > balance");

        // First update the balance array with the new value for the address
        //  sending the tokens
        updateValueAtNow(balances[_from], previousBalanceFrom.sub(_amount));

        // Then update the balance array with the new value for the address
        //  receiving the tokens
        uint256 previousBalanceTo = balanceOfAt(_to, block.number);
        updateValueAtNow(balances[_to], previousBalanceTo.add(_amount));

        // An event to make the transfer easy to find on the blockchain
        emit Transfer(_from, _to, _amount);
    }

    /**
    * @notice this internal function retrieives the historyical balance of either totalSupply or from an address
    * @param _owner address of a wallet/contract to check
    * @param _blockNumber uint256 block number to retrieve balance at
    * @param _isTotalSupply bool marks if totalSupply or wallet/contract balance lookup
    */
    function balanceAt(address _owner, uint256 _blockNumber, bool _isTotalSupply) internal view returns (uint256) {
        Checkpoint[] memory balanceArray;

        if (_isTotalSupply) {
            balanceArray = totalSupplyHistory;
        } else {
            balanceArray = balances[_owner];
        }

        if ((balanceArray.length == 0) || (balanceArray[0].fromBlock > _blockNumber)) {
            return 0;
        // This will return the expected totalSupply during normal situations
        } else {
            return getValueAt(balanceArray, _blockNumber);
        }
    }

    /**
    * @dev `getValueAt` retrieves the number of tokens at a given block number
    * @param checkpoints The history of values being queried
    * @param _block The block number to retrieve the value at
    * @return The number of tokens being queried
    */
    function getValueAt(Checkpoint[] memory checkpoints, uint256 _block) internal pure returns (uint256) {
        // Shortcut for the actual value
        if (_block >= checkpoints[checkpoints.length.sub(1)].fromBlock) {
            return checkpoints[checkpoints.length.sub(1)].value;
        }

        // Binary search of the value in the array
        uint256 min = 0;
        uint256 max = checkpoints.length.sub(1);
        while (max > min) {
            uint256 mid = (max.add(min).add(1)).div(2);
            if (checkpoints[mid].fromBlock <= _block) {
                min = mid;
            } else {
                max = mid.sub(1);
            }
        }
        return checkpoints[min].value;
    }

    /**
    *@dev `updateValueAtNow` used to update the `balances` map and the
    *  `totalSupplyHistory`
    * @param checkpoints The history of data being updated
    * @param _value The new number of tokens
    */
    function updateValueAtNow(Checkpoint[] storage checkpoints, uint256 _value) internal {
        if ((checkpoints.length == 0) || (checkpoints[checkpoints.length.sub(1)].fromBlock < block.number)) {
            checkpoints.push(Checkpoint(uint128(block.number), uint128(_value)));
        } else {
            checkpoints[checkpoints.length.sub(1)].value = uint128(_value);
        }
    }
}
function balanceOfAt(address _owner, uint256 _blockNumber) public view returns (uint256) {
        return balanceAt(_owner, _blockNumber, false);
    }
/**
 * @dev Queries the balance of `_owner` at a specific `_blockNumber`
 * @param _owner The address from which the balance will be retrieved
 * @param _blockNumber The block number when the balance is queried
 * @return The balance at `_blockNumber`
 */