如何检查从地址到智能合约的eth转移

How to check transfer of eth from an address to smart contract

如果我有这样的功能:

function sendToAuthor (uint tokenId) public payable{

  //here I want to get some ether from the msg.sender, store it in a 
  mapping and send it to another address (author). 

}

我不明白的是如何检查 msg.sender 是否给了智能合约钱。如果我可以检查,我可以从映射中取出 msg.value 并将其发送给作者,但是我如何检查发件人是否确实将 eth 转移到合约?

msg.value 变量包含在 payable 函数中存入了多少 ETH。您检查该值是否大于零或与您期望的任何付款金额相匹配。

伪代码:

contract someContract {
  address author = "0x00...";
  mapping (address => uint) public sentToAuthor;
  function sendToAuthor () public payable {
    sentToAuthor[msg.sender] = msg.value;
    author.call{value: msg.value}("");
  }
}

你的问题没有道理。

What I don't understand is how to check if the msg.sender gave the smart contract the money or not.

你看看msg.value的值就知道了。如果交易没有恢复,并且功能是可支付的,并且您没有将它发送到其他任何地方,那么您的合约就会收到这些资金。时期。不需要“检查”。如果你确实想检查,你可以在客户端,但只看合同余额(例如,ethers.utils.balanceOf(contractAddress),或其他)。你也可以只看这里的映射,它是正确的,给你看。

If I can check that, I can take the msg.value from the mapping and send it to the author, but how do I check that the sender actually made the eth transfer to the contract?

“msg.value”实际上不会“在”映射中,映射实际上不能容纳 eth,它们只能容纳一个 uint。它只是告诉你发送给合约的金额。

顺便说一下,这里写的是直接发给作者,不会留在合同里。如果您删除“sendToAuthor”的最后一行(author.call 行),那么 eth 将只保留在合约本身中。

(顺便说一句,这里有一个以太坊计算器,你应该去那里问问。)