Question: I want to create a simple smart contract for registration of users to a competition where when users can pay to contract and 2 ethers will deduct from his account.
But when I'm trying to do so on my local ganache blockchain,only gas price is being deducted.

But I'm confused on how to accept payments from ethereum network accounts to the contract.

Help me..

Here's the code

```
pragma solidity >=0.4.22;

contract dCoders {
    // variables
    // storing contract_amount collected for the event
    address public owner;
    uint public regFee;

    mapping (address => uint) public balance; 

    constructor() public {
        owner = msg.sender;
        regFee = 2 ether;
    }

    event reg(address indexed _from);

    // register a user in the event
    function register() payable public {
        // emit reg(_sender, owner, regFee);

        balance[msg.sender] -= regFee;
        balance[address(this)] += regFee;   

        emit reg(msg.sender);
    }

    // distribute the amount collected to winners

    // register a sponser in the event
}

```

Answer: 
```
pragma solidity >=0.4.22;

contract dCoders {
 //variables
 //storing contract_amount collected for the event
 address public owner;
 uint public regFee;

 mapping (address => uint) public balance; 

  constructor() public {
  owner = msg.sender;
  regFee = 2 ether;
  }

  event reg(address indexed _from);

  //register a user in the event
 function register() payable external {
 require(msg.value == 2 ether);
 balance[address(this)] += regFee;     
 emit reg(msg.sender); 
 }

 //distribute the amount collected to winners

  //register a sponser in the event

  }

```

Assuming that at the moment you need to draft only, here it is.

1) you do not need to decrement any balance for your user: it is done automatically when he sends ethers to you and you accept them. The “balance” mapping is a local-to-your contract variable only. It is written by you in order to be readed by you

2) you can call your function “register” or you can avoid to name it. 

```
function () payable external {
...
}

```

In this way your user must send 2 ether to the contract without calling nothing. This way he can use any wallet to do it.

Just to help you to start...
The user sends a transaction to the contract function. This transaction includes a `value` which is ether funds, and `data` which selects the function to run and passes in arguments. 

On the contract side, you need the function to be `payable` and you can inspect the amount of money received with the message to confirm it is exactly what you need. 

If it's okay to proceed, then register your user, do the accounting and emit an event. 

The OP didn't reveal any details about what happens after registration, so some guesswork about what to do. Importantly, all funds from all sources rest in the contract's balance *without any help from us*. It may be important to know the sources of those funds (who sent how much) and this is where the `balances` `mapping` helps. It is not funds. It's accounting (data) about the funds. 

```
pragma solidity 0.4.22;

contract dCoders {

    address public owner;
    uint public regFee;

    mapping (address => uint) public balances; 

    constructor() public {
        owner = msg.sender;
        regFee = 2 ether;
    }

    event LogRegistration(address from);

    // User sends 2 ether to this function
    function register() payable public{

        require(msg.value == regFee);
        balances[msg.sender] += msg.value; // track contributions by user    
        emit LogRegistration(msg.sender);
    }

}

```

Hope it helps.