固体的应付功能如何通过msg.value方法获得金钱?

发布于 2025-02-05 08:54:51 字数 1891 浏览 0 评论 0原文

我有2个不同的合同中有两个应付的职能。一个是“ buyland”,另一个是“储备金”。这两个功能都有一些检查,但是我面临的困惑是,当我调用具有一定值的付款功能时,值是否有必要接收函数的检查或函数内部的条件是正确的吗?

与我的功能混淆: Buyland功能接收以太算机的支票是真实的。 仅当检查是正确的,但在检查是错误的情况下,储备金功能才会接收以太词。

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);
    }
}

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);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怪我太投入 2025-02-12 08:54:51

当您使用付款关键字定义功能时,这意味着该功能希望从WEI中接收值。部署合同后,您会注意到,这将为您的方法添加一个参数。传递的值可以是任何数量的以太,甚至零,这是您的requient语句发挥作用的地方。

当您在类似buyland之类的方法中调用付款()函数时,这允许合同将以太发送到第一个参数中指定的地址,或者在您的情况下<代码> Landownermagping [_landid] 。

基本上,这是使用付款作为关键字之间的区别,并将其用作方法。您可以在“坚固性文档”中找到有关它的更多信息。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文