contract TokenState is Owned {

    // the address of the contract that can modify balances and allowances
    // this can only be changed by the owner of this contract
    address public associatedContract;

    // ERC20 fields.
    mapping(address => uint) public balanceOf;
    mapping(address => mapping(address => uint)) public allowance;

    function TokenState(address _owner, address _associatedContract)
        Owned(_owner)
        public
    {
        associatedContract = _associatedContract;
        emit AssociatedContractUpdated(_associatedContract);
    }

    /* ========== SETTERS ========== */

    // Change the associated contract to a new address
    function setAssociatedContract(address _associatedContract)
        external
        onlyOwner
    {
        associatedContract = _associatedContract;
        emit AssociatedContractUpdated(_associatedContract);
    }

    function setAllowance(address tokenOwner, address spender, uint value)
        external
        onlyAssociatedContract
    {
        allowance[tokenOwner][spender] = value;
    }

    function setBalanceOf(address account, uint value)
        external
        onlyAssociatedContract
    {
        balanceOf[account] = value;
    }


    /* ========== MODIFIERS ========== */

    modifier onlyAssociatedContract
    {
        require(msg.sender == associatedContract);
        _;
    }

    /* ========== EVENTS ========== */

    event AssociatedContractUpdated(address _associatedContract);
}
/*
 -----------------------------------------------------------------
 CONTRACT DESCRIPTION
 -----------------------------------------------------------------
 
 A contract that holds the state of an ERC20 compliant token.
 
 This contract is used side by side with external state token
 contracts, such as Havven and EtherNomin.
 It provides an easy way to upgrade contract logic while
 maintaining all user balances and allowances. This is designed
 to to make the changeover as easy as possible, since mappings
 are not so cheap or straightforward to migrate.
 
 The first deployed contract would create this state contract,
 using it as its store of balances.
 When a new contract is deployed, it links to the existing
 state contract, whose owner would then change its associated
 contract to the new one.
 
 -----------------------------------------------------------------
 */
function setAssociatedContract(address _associatedContract)
    external
    onlyOwner
{
    associatedContract = _associatedContract;
    emit AssociatedContractUpdated(_associatedContract);
}

// Change the associated contract to a new address