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: 
Accepting payments in Ethere only requires a function with a `payable` keyword.

As Rick mentioned, easiest way is to have a fallback function (without function name) which is executed whenever someone sends Ethers to the contract - that way they don't need to call any function directly and they can just use a regular Ether sending transaction without knowing anything of the contract's functions.

Otherwise your usage of `balance` is rather..wrong. If I understand you correctly, you want to store the amount of Ethers each user has sent. In that case you probably want to change the line to: `balance[msg.sender] = msg.value;`. If you store it with `address(this)` you will not have the sender's address stored anywhere.

If, on the other hand, you are simply trying to store the amount of Ethers received with the line, then you can just remove it. You can always find out how much Ethers a contract has without explicit functionality for it. So two things are automatic: 1) Reducing Ethers from sender 2) Incrementing Ethers in contract.
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.