简体   繁体   English

solidity中payable function如何通过msg.value方式收款?

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

I have two payable functions in 2 different contracts.我在 2 个不同的合同中有两个应付功能。 One is "BuyLand" and the other is "depositEthers".一个是“BuyLand”,另一个是“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?这两个函数都有一些检查,但我面临一个困惑,当我调用一个 value 字段中有一些值的 payable function 时,function 是否有必要收到 Ethers 支票或 function 中的条件是否为真?

Confusion with my functions: BuyLand function receives ethers either checks are true or not.与我的功能混淆:BuyLand function 收到以太币,检查是否正确。 depositEthers function receives ethers only when checks are true but not when checks are false. depositEthers function 只有在支票为真时才会收到以太币,而在支票为假时则不会。

How is it possible that 2 payable functions behave differently? 2个应付函数的行为怎么可能不同?

// 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.当您使用payable关键字定义 function 时,这意味着 function 期望收到以 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.传递的值可以是任意数量的以太币,甚至可以是零,这就是你的require语句发挥作用的地方。

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] .当您在 BuyLand 之类的方法中调用payable() BuyLand ,这允许合约将 Ether 发送到第一个参数中指定的地址,或者在您的情况下landOwnerMapping[_landId]

Basically it's the difference between using payable as a keyword, and using it as a method.基本上,这是将payable用作关键字和将其用作方法之间的区别。 You can find out more about it in the solidity documents.您可以在 solidity 文档中找到更多相关信息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 HardHat 调用可支付的 Solidity 函数? - How to invoke a payable solidity function using HardHat? 用一些硬编码值替换 msg.value - Replace msg.value with some hardcoded value 如何测试 Solidity 应付合同 - How to test a Solidity payable contract 可靠性:调用另一个合约的函数时出错。 错误:发送值时应向构造函数付款 - Solidity: Error when calling a function of another contract. Error: The constructor should be payable when you send value msg.value 引发错误并且不会将 eth 发送到合同 - msg.value raises error and doesn't send eth to contract 如何检查功能是否应付? - How to check function is payable or not? solidity 交易错误,如果您发送 value 应该支付调用的 function 并且您发送的 value 应该小于您当前的余额 - solidity transaction error, The called function should be payable if you send value and the value you send should be less than your current balance Solidity:“如果您发送价值,则应支付称为 function 并且您发送的价值应小于当前余额” - Solidity: "The called function should be payable if you send value and the value you send should be less than your current balance" 如何在solidity 0.5.2或更高版本中分配或重置address[]payable变量? - How to assign or reset the address[] payable variable in solidity 0.5.2 or above? 如何从 Solidity 中的多个 function 返回中仅获取必要的值? - How to get only necessary value from multiple function return in Solidity?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM