Reputation: 281
If i have a function like this:
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).
}
What I don't understand is how to check if the msg.sender gave the smart contract the money or not. 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?
Upvotes: 3
Views: 1443
Reputation: 3712
psuedocode:
contract someContract {
address author = "0x00...";
mapping (address => uint) public sentToAuthor;
function sendToAuthor () public payable {
sentToAuthor[msg.sender] = msg.value;
author.call{value: msg.value}("");
}
}
Your question doesn't really make sense.
What I don't understand is how to check if the msg.sender gave the smart contract the money or not.
You just look at the value of msg.value
. If the transaction doesn't revert, and the function is payable, and you don't send it anywhere else, then your contract receives those funds. Period. No "checking" needed. If you do want to check, you could on the client side, but just looking at the contract balance (e.g., ethers.utils.balanceOf(contractAddress)
, or something). You could also just look at the mapping here, it will be correct and show you.
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?
The "msg.value" won't actually be "in" the mapping, mappings can't actually hold eth, they can only hold a uint. It just tells you how much was sent to the contract.
Btw, as written here, it sends it straight to the author, so it won't stay in the contract. If you remove the last line of "sendToAuthor" (the author.call
line), then the eth will just stay in the contract itself, instead.
(btw, there's an ethereum stackoverflow, you should be asking there.)
Upvotes: 0
Reputation: 83576
The msg.value
variable contains how much ETH was deposited in payable
function. You check that the value is above zero or matches the any payment amount you are expecting.
Upvotes: 2