Ether Transactions – Comparing msg.value to Variable Ether in Remix

ethermyetherwalletremix

pragma solidity ^0.4.24;

contract TRY{

    uint public temp=0;
    uint public b=0;

    function transfer_ether() public payable{
       uint c=60;
       if(msg.value>=c ether){
            temp=msg.value;
             b=temp-(60 ether);
            temp=60 ether;
           msg.sender.transfer(b );
       }
    }

    function to_contract() public payable {
        uint a=temp;
        temp=0;
        msg.sender.transfer(a);
    }
}

This code gives an error:

if(msg.value>=c ether){  <<<< HERE

Is it that we cannot compare with variable ether?

EDITED:-

contract TRY{

    uint public temp=0;
    uint public b=0;
    uint public c ether;

    function set_c(uint _c ){
        c=_c ;
    }

    function transfer_ether() public payable{

       if(msg.value>=c ){
            temp=msg.value;
             b=temp-(60 ether);
            temp=60 ether;
           msg.sender.transfer(b );
       }
    }

    function to_contract() public payable {
        uint a=temp;
        temp=0;
        msg.sender.transfer(a);
    }
}

I tried compiling this but it doesn't work… is it that we have to have a fixed value for storing as ether?

Best Answer

You can not do variable ether instead do:

function transfer_ether() public payable{
   uint c = 60 ether;
   if(msg.value >= c){
       // here your code
   }
}

Hope this helps

EDIT: The second part of the question added as an edit by OP

You can not do uint public c ether the compiler should be telling this.

You need to establish what the subject is inputting. If the subject is requested to input amount in ether, then you should transform it to wei (this is what using the word ether does when you do 60 ether)

Therefore your function should be:

uint public c;
function set_c(uint _c){
    c=_c*10**18 ;
}

This is because 1 ether = 10**18 wie. The smart contract makes all the calculations related to ether in wei. So if the subject inputs as value the number 1, c will save 10**18 = 1000000000000000000, which is the equivalent in wei for 1 teher.

Hope this helps

Related Topic