Search code examples
blockchainethereumsoliditysmartcontracts

why can't I send 1 ether to another account via smartcontract?


I am building a contract with Solidity and deployed it to Ethereum private network Ropsten. There is a payRequest method which transfers 1 ether to another account. I have deployed the contact and run the method successfully. (I set the recipient address and 1 as amount)

I am able to view the transaction hash in Etherscan and its status is success. But the success in the response is false.

The value in the transaction is 0. The recipient address doesn't receive this transfer as well. What did I do wrong?

// SPDX-License-Identifier: MIT
pragma solidity 0.7.4;
pragma experimental ABIEncoderV2;

contract Leger {
    struct TransferRequest {
        string title;
        uint256 amount;
        string bsb;
        string accountName;
        string accountNumber;
    }

    event PaymentReceive(address from, uint256 amount);
    event TransactionBytes(bytes transactionBytes);
    event RequestPaid(address receiver, uint256 amount);

    function payRequest(address payable _recipient, uint256 _amount) public {
        
        (bool success, bytes memory transactionBytes) = _recipient.call{value:_amount}('');
        
        emit TransactionBytes(transactionBytes);
        
        emit RequestPaid(msg.sender, _amount);
    }

    receive() external payable {
        emit PaymentReceive(msg.sender, msg.value);
    }

}


Solution

  • Your function is not payable, please use:

    function payRequest(address payable _recipient, uint256 _amount)payable public {
            
            (bool success, bytes memory transactionBytes) = _recipient.call{value:_amount}('');
            
            emit TransactionBytes(transactionBytes);
            
            emit RequestPaid(msg.sender, _amount);
        }
    

    I don't see from which account the ether are from. You don't declare your variables in the contract (for example before the event as done in the best practices).

    To be fully transparent, your contract seems strange or incomplete to me.