0
pragma solidity 0.4.19;

contract blackeye {
    uint public max_amount = 1000000;
    uint public total_balance_bought = 0;

    mapping(address => uint) total_balance;   

    address owneraddress;

    modifier onlyowner {
        require(owneraddress == msg.sender);
        _;
    }

    modifier can_buy_amount(uint rupee_invested) {
        require(rupee_invested + total_balance_bought <= max_amount);
        _;
    }

    function balance(address investor) external constant returns(uint) {
        return total_balance[investor];
    }

    function deposit(address creator, address investor, uint rupee_invested) external {
        if (msg.sender == address(creator)) {
            uint amount_bought = rupee_invested;
            total_balance[investor] += amount_bought;
            total_balance_bought += amount_bought;
        }
    }

    function withdraw(address creator, address investor, uint amount_sold) external {
        if (msg.sender == address(creator)) {
            total_balance[investor] -= amount_sold;
            total_balance_bought -= amount_sold;
        }
    }

    function send_money(address sender, address receiver, uint amount_send) external {
        total_balance[sender] -= amount_send;
        total_balance[receiver] += amount_send;
    }
}
0

There are a few problems.

This doesn't do anything useful.

if (msg.sender == address(creator))  {

A sender can just pass their own address into the function to make the condition true. I think what you want is to check that the sender is authorized to report rupees received.

You don't need this because you can make the mapping public. It gives you a "free" getter function.

function balance(address investor) external constant returns(uint){
    return total_balance[investor];
}

The withdraw function doesn't do anything because there is nothing to withdraw to. This is not dealing with Ethereum and not a standard token. In any case, a token like this "withdrawn" by sending from one account to another, which is covered by your send function.

Don't trust the sender. Verify. The send function should use msg.sender to check the balance of the sender's account.

Here's a cleaned up version you can play around with in Remix.

pragma solidity 0.4.19;

contract BlackEye{

    uint public initial_supply = 1000000;

    mapping(address=> uint) public balances;   

    address ownerAddress;

    modifier onlyOwner {
        require(ownerAddress == msg.sender);
        _;
    }
    function BlackEye() public {
        ownerAddress = msg.sender;
        balances[address(this)] = initial_supply;
    }
    function deposit(address investor, uint rupee_invested) external onlyOwner {
        balances[investor] += rupee_invested;
        balances[address(this)] -= rupee_invested;
    }
    function send_money(address receiver , uint amount_send) external{
        require(balances[msg.sender] >= amount_send);
        balances[msg.sender] -= amount_send;
        balances[receiver] += amount_send;
    }
    function transferReserves(address receiver, uint amount) external onlyOwner {
        require(balances[address(this)] >= amount);
        balances[address(this)] -= amount;
        balances[receiver] += amount;
    }
}
  1. Deploy the contract.
  2. Copy the contract address and paste into balances(). See the initial supply.
  3. Use the transferReserves() function to send some to 0xca3... (copy). This function gives the owner access to the contract's reserve supply created in the constructor. (Using the mapping and address(this) simplifies the accounting).
  4. Now 0xca3... can send some to someone else.

Hope it helps.

  • What if two different user which are not owner and they want to transfer amount to each other? I am trying to do this functionality in send function. – shai Mar 1 at 5:24
  • They don't have to be owner to use send_money, but they need to have some tokens. They get some tokens from transactions that happened before. Initially, the only possible source is owner sending some transferReserves. – Rob Hitchens - B9lab Mar 1 at 14:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.