0

in this contract will the floating point work,

pragma solidity ^0.8.7;

// SPDX-License-Identifier: GPL-3.0


contract AB {

   address payable  b= payable(xx);

    uint amount= 0.1 ether;
  


     function Optimizer( uint256 _amount )public  payable{

        
     _amount=msg.value;
        
       require(amount >=0.1 ether,"transfer amount to low");

      

      b.transfer(msg.value);

      
       
    }
    
}
1

2 Answers 2

0

yes, you used the ether keyword with a floating point correctly. In Remix your 0.1 ether line didn't cause any warnings or errors.

If you create a test function that returns 0.1 ether, you can see that it really returns 100000000000000000:

function test() public pure returns(uint){

    return 0.1 ether;
    }

enter image description here

If you put this WEI value into an Ethereum unit converter, you can see the number's equivalent in GWEI and ETH:

enter image description here

A more refined version of your contract that uses msg.value correctly as opposed to your _amount uint, custom errors as opposed to your hard-coded require, and call instead of transfer for sending the ETH:



contract AB {

  error CallFailed();
  error TransferAmountTooLow();

    uint amount= 0.1 ether;
    address payable b = payable(0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2);


     function Optimizer( )public  payable{ 
      if(msg.value < 0.1 ether) {
        revert TransferAmountTooLow();
      }
      (bool sent,) = b.call{value: msg.value}("");
      if(!sent) {
        revert CallFailed();
      }    
    }
    
}
9
  • will it be okey on the mainnet, i mean with real funds & will it transfer 0.1 eth to specific address?
    – stofu
    Oct 9, 2023 at 20:53
  • @stofu if you can compile and deploy the contract on a testnet/locally, it will compile and deploy on mainnet in the same way, so yes.
    – Rohan Nero
    Oct 9, 2023 at 21:15
  • i deploy the contract on test net but its not sending eth and its also no meeting the condition of require(amount >=0.1 ether,"transfer amount to low");
    – stofu
    Oct 10, 2023 at 13:02
  • @stofu can you provide the contract address and the name of the testnet you deployed it on? Also what input are you sending along with the function call? If you deployed your contact exactly as it is in your question, then there are a couple of reasons why it wouldn't work, for example payable(xx) and _amount = msg.value
    – Rohan Nero
    Oct 10, 2023 at 14:11
  • goril, input:0.4 as a parameter in optimizer function. its the same error if you deploy locally( in remix)
    – stofu
    Oct 10, 2023 at 14:14
2

Yes. Also, there is no floating point numbers here, amount is an uint. ether in solidity means * 10 ^ 18, so your variable is effectively 10^17

3
  • i deploy the contract on test net but its not sending eth and its also no meeting the condition of require(amount >=0.1 ether,"transfer amount to low");
    – stofu
    Oct 10, 2023 at 13:01
  • are you actually sending ether in the tx? Or just setting amount to 0.1 ether and hoping it will work?
    – Foxxxey
    Oct 10, 2023 at 17:40
  • Oh it seems you figured it out with Rohan, all good :)
    – Foxxxey
    Oct 10, 2023 at 17:41

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.