contract exD is  Owned {

    ERC20Interface public token;

    uint public totalDividend = 0;
    uint public totalSupply = 0;
    uint public divMultiplier =0;
    uint public totalClaimed=0;

    mapping(address => uint) claimed;
    event Dividend(uint _value);
    event Payed(address account,uint _value);
    event Withdraw(uint _value);
    
    // ------------------------------------------------------------------------
    // Constructor
    // ------------------------------------------------------------------------
    constructor(ERC20Interface _token) public {
        token=_token;
    }


    function balanceOf(address _account) public view returns (uint) {
        return (token.balanceOf(_account)*divMultiplier)-claimed[_account];
    }
    
    // ------------------------------------------------------------------------
    // As a holder of token, withdraw your dvidend
    // ------------------------------------------------------------------------
    function withdrawDividend() payable public {
        uint due=(token.balanceOf(msg.sender)*divMultiplier)-claimed[msg.sender];
        if(due+claimed[msg.sender]<claimed[msg.sender]) revert();        
        claimed[msg.sender]+=due;
        totalClaimed+=due;
        msg.sender.transfer(due);
        emit Payed(msg.sender,due);
    }
    
    function withdrawBonds(uint value) onlyOwner public {
        totalDividend-=value;
        owner.transfer(value);
        emit Withdraw(value);
    }
    
    // ------------------------------------------------------------------------
    // All ethers send via standard will become part of our dividend payments
    // ------------------------------------------------------------------------
    function () public payable  {
      if(msg.value<1) revert();
      if(totalDividend+msg.value<totalDividend) revert();
      if(token.totalSupply()+totalSupply<totalSupply) revert();
      totalDividend+=msg.value;
      totalSupply+=token.totalSupply();
      divMultiplier=totalDividend/totalSupply;
      emit Dividend(msg.value);
    }


    // ------------------------------------------------------------------------
    // Owner can transfer out any accidentally sent ERC20 tokens
    // ------------------------------------------------------------------------
    function transferAnyERC20Token(address tokenAddress, uint tokens) public onlyOwner returns (bool success) {
        return ERC20Interface(tokenAddress).transfer(owner, tokens);
    }
}
function () public payable  {
  if(msg.value<1) revert();
  if(totalDividend+msg.value<totalDividend) revert();
  if(token.totalSupply()+totalSupply<totalSupply) revert();
  totalDividend+=msg.value;
  totalSupply+=token.totalSupply();
  divMultiplier=totalDividend/totalSupply;
  emit Dividend(msg.value);
}

// ------------------------------------------------------------------------
// All ethers send via standard will become part of our dividend payments
// ------------------------------------------------------------------------