简体   繁体   中英

How payable function in solidity receives money by msg.value method?

I have two payable functions in 2 different contracts. One is "BuyLand" and the other is "depositEthers". There are some checks in both functions but I am facing a confusion that when I call a payable function with some value in value field, then is it necessary that function will receive that Ethers either checks or conditions inside the function are true or not?

Confusion with my functions: BuyLand function receives ethers either checks are true or not. depositEthers function receives ethers only when checks are true but not when checks are false.

How is it possible that 2 payable functions behave differently?

// 1st Function
function depositEthers() public payable
{
    require(users[msg.sender].flag != 0, "You are not a registered user, 
get yourself registered first");
    require(msg.value > 0, "No Ethers was sent, Please send Ethers");
    users[msg.sender].balance += msg.value;
}


// 2nd Function
function BuyLand
(
    uint _landId
) public payable
{
    require(landOwnerMapping[_landId] != msg.sender, "You can not Buy 
Land because you are the Owner");
    require(BuyerMapping[msg.sender].isVerified == true, "Buyer is not 
verified");
    require(SellerMapping[landOwnerMapping[_landId]].isVerified == true, 
"Seller is not verified");
    require(Lands[_landId].isVerified == true, "Land is not verified");

    if (msg.value > Lands[_landId].LandPrice*1000000000000000000)
    {
        //payable(msg.sender).transfer(address(this).balance);
        emit buyingLand("Land not bought, sent more Ethers than Land 
price",
        _landId, Lands[_landId].LandPrice, landOwnerMapping[_landId], 
msg.sender);
    }

    else if (msg.value < Lands[_landId].LandPrice*1000000000000000000)
    {
        //payable(msg.sender).transfer(address(this).balance);
        emit buyingLand("Land not bought, sent less Ethers than Land 
price",
        _landId, Lands[_landId].LandPrice, landOwnerMapping[_landId], 
msg.sender);
    }

    else
    {
        payable(landOwnerMapping[_landId]).transfer(msg.value);
        landOwnerMapping[_landId] = msg.sender;
        ownerMapping[msg.sender] = _landId;
        emit buyingLand("Land bought successfully",
        _landId, Lands[_landId].LandPrice, landOwnerMapping[_landId], 
msg.sender);
    }
}

When you define a function with the payable keyword, this means that the function expects to receive a value in terms of Wei. You will notice after deploying your contract that this adds a parameter to your method. The value passed can be any amount of Ether, even zero, and this is where your require statement comes into play.

When you call the payable() function inside a method like in BuyLand , this allows the contract to send Ether to the address specified in the first parameter, or in your case landOwnerMapping[_landId] .

Basically it's the difference between using payable as a keyword, and using it as a method. You can find out more about it in the solidity documents.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM