1

this is a simple contract. where I want to develop a function, optimizer, upon calling it will transfer an amount of eth to an address. but when I deploy it on the testnet, connect my Metamask and try to call the optimizer function with 0.5 eth in Remix, its gives me this error:

transact to AB.Optimizer errored: Error encoding arguments: Error: invalid BigNumber string (argument="value", value="0.5", code=INVALID_ARGUMENT)

where is fault in my code?

    pragma solidity ^0.8.7;
    
    // SPDX-License-Identifier: GPL-3.0
    
    
    contract ABc {
    
       address payable  b= payable(xxx);
    
        uint amount= 0.4 ether;
      
    
    
         function Optimizer( uint256 _amount )public  payable{
    
            
         _amount=msg.value;
            
           require(amount >=0.4 ether,"transfer amount to low");
    
          
    
          b.transfer(msg.value);
    
          
           
        }
        
    }

1 Answer 1

0

You can't pass floating point numbers as input to a function in solidity, if you want to pass 0.5 ether, you would need to do so in terms of WEI, which would be 500000000000000000.

You have a few things in your contract that I revised here:

contract AB {

  error CallFailed();
  error TransferAmountTooLow();

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


     function Optimizer( )public  payable{ 
      if(msg.value < amount) {
        revert TransferAmountTooLow();
      }
      (bool sent,) = b.call{value: msg.value}("");
      if(!sent) {
        revert CallFailed();
      }    
    }
    
}
  1. Instead of using _amount to try and pass ETH, I removed this variable entirely. When passing value to a function you don't do so using an input parameter.

  2. I changed your require statement that uses a hard-coded string to a more gas efficient custom error.

  3. I changed your transfer to a call for sending ETH in a more efficient way, as well as validating that the call went through with an additional custom error.

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.