I am trying to create a simple contract which adds the amount (passed as a parameter) to contract owner and reduces the equivalent amount from an account (account address passed as a parameter).

In the contract, I have created a second function 'getBalance' to inquire account balances. After executing the transferamount function when I inquire the account balances of contract owner and the passed account, they remain same with no increase or decrease of ethers. But when I verified the balances in the account balance mapping 'balanceOf' that I created in the code, I can see changes in amounts via the events that I set to watch balances before and after the transaction.

Pardon me for my noob status in solidity coding and kindly suggest what is an optimal way to implement requirement wherein we have to add or subtract a definite amount from user accounts and contract accounts. Also, please highlight errors in my code that can fix the issue.

Below is the logic of the code that I created.

pragma solidity ^0.4.0;

import "github.com/oraclize/ethereum-api/oraclizeAPI.sol";

 * @title Testing Oraclize
 * Test contract to understand the basics of leveraging the Oraclize service.

contract Betting is usingOraclize {

    address public owner = msg.sender;
    uint accountbalance;

    event Log(string);
    event error(string error_);
    event beforebetbalanceplayer(uint256);
    event beforebetbalanceowner(uint256);
    event afterbetbalanceplayer(uint256);
    event afterbetbalanceowner(uint256);
    event betamount(uint256);

        modifier onlyOwner {
            require(msg.sender == owner);

mapping (address => uint256) private balanceOf;

/*Put Bet*/  
        function transferamount (address _player, uint256 _amount) public payable {

                balanceOf[_player] = _player.balance;
                balanceOf[owner] = owner.balance;


                Log("Reducing amount from player's account");
                balanceOf[_player] -= _amount;

                Log("Adding amount to owner's account");
                balanceOf[owner] += _amount;



/*Get Balance*/

        function getBalance (address _account) constant returns(uint256){
                return _account.balance;


/*Payable Fallback function*/

        function()  payable{    


1 Answer 1


getBalance returns the amount of ether in the specified account, and transferamount doesn't change the amount of ether for either account.

Based on your comment below, you want to move ether in transferamount. This needs to happen in two phases:

  1. Someone needs to call transferamount and send ether along with their transaction. This gives the contract the ether.
  2. The contract needs to transfer that ether to the desired recipient.

It would look something like this:

// No _player field, because you can't forcibly transfer ether from someone else.
function transferamount (uint256 _amount) public payable {

msg.value tells you how much ether was sent in this transaction. address.transfer(amount) sends amount ether from the smart contract to the given address.

Note that this function is now a little silly; it's just a layer of indirection, when the sender of the transaction could have just sent the ether directly to the owner account.

These blog posts might help:

In particular, I think you might really be trying to do what the second blog post does: maintain per-account balances based on the ether they send to the contract.

  • My requirement is to move/transfer ether from the address passed in my 'transferamount' function to the contract's address/owner. Once the transfer is succesful I wish to view.
    – tanmay
    Jan 28, 2018 at 9:14
  • See if my edit helps.
    – user19510
    Jan 28, 2018 at 9:20
  • thanks for the inputs, the links and your suggestions worked.
    – tanmay
    Feb 3, 2018 at 13:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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