[英]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.