How to consume msg.value? - ethereum

I understand msg.value represents how much wei the sender sent. But how does a contract use it?
In the example of a vending machine, the contract checks to ensure enough msg.value is present to cover the cupcakes ordered, but there's no code that actually deducts the wei.
Example:
https://ethereum.org/en/developers/docs/smart-contracts/#a-digital-vending-machine
I do see some info about buyer and seller.transfer() here:
https://docs.soliditylang.org/en/v0.8.15/solidity-by-example.html#safe-remote-purchase

A contract has no need for deducting to receive ETH or msg.value. The sender will determine ETH payment amount.
ETH receiving contract only need payable modifier to indicate that it can receive ETH. We can say it is like a vending machine in the way that the contract cannot determine how much it will receive, but you can refund by transferring the exceeding ETH back to the sender.
Unlike ERC20 which works differently, it does needs to be approved and deducted for the general contract purchase function.
Read more:
https://stackoverflow.com/a/71883390/2017851
https://medium.com/coinmonks/solidity-transfer-vs-send-vs-call-function-64c92cfc878a
https://cryptomarketpool.com/reentrancy-attack-in-a-solidity-smart-contract/

Typically, this code is paired with a dapp front-end for the user to operate, so if you are a developer who wants to send ETH when calling a contract, I recommend doing this

Related

Send ether from one wallet to another with solidity smart contract?

I want to send ether from one walet to another…what technique to use and what is the safest method to do so?
wallet A(msg.sender)=10ether
wallet B=10ether
with the help of smart contract send 'x' ether from A to B.
I tried
where 'x' is the varaible ether at different times.
=>payable().transfer(msg.value);
here i am able to send ether in remix ide where i can provide the msg.value...i want to implement that it is able to change msg.value according to the value of x.
It is not possible to remove ether from someone's wallet on their behalf.
You can do something like this with ERC20 tokens, which have an approve functionality, but there is no API for this with native Ether.
To send ether out of a wallet, the owner of the wallet must sign a transaction specifying they want to send a certain amount of ether to a certain address.
Now, you can sign a transaction, and have someone else broadcast it. This is called relaying. But once you sign a transaction that sends ether from your wallet, you can basically consider the ether gone.
If you need to conditionally take ether from a user, having the user stake the ether in an escrow contract may make more sense.

Transfer ERC20 token without ETH

I would like to transfer ERC20 tokens from a wallet who don't own ETH to another wallet who own ETH and who can pay gas fee.
Do you know if it is possible to made a transfer of ERC20 tokens and to let the receiver wallet pay fees ?
TLDR: Not possible, unless the token contract explicitly allows it. Or unless the token holder is also the block producer.
Transaction fees are paid in ETH (or generally, native currency of the network - for example BNB on Binance Smart Chain, MATIC on Polygon, ...). So in most cases, you need to pay ETH to execute either the transfer() function if you want to send the tokens from your address, or the approve() function if you want someone else to transfer tokens from your address.
Very few token contracts implement delegated transfer mechanism on top of the ERC20 standard. There's currently no standardized way to perform a delegated transfer, so each contract might have a different implementation. The token holder uses their private key to sign a predetermined message saying how many tokens they want to transfer to which address. The message also usually contains a nonce or a timestamp to prevent signature replay attack. Token holder passes the message offchain to the transaction sender, and then the transaction sender executes a function of the token contract built specifically for delegated transfers (note that the transaction sender pays the fee to execute this function). The contract validates the signature, and performs the transfer. Again, most token contracts do not implement this mechanism.
One more exception from the rule is a block producer. When you create a new block, you can fill it with transactions not only from the mempool but also with your own transactions. You can build a transaction with 0 gas price, and then include it in the block that you're producing. This way you're also able to send tokens for free.

Why are these Ethereum Contracts not sweeping USDT?

I have an Eth contract, called the controller, which is used to:
Generate new eth deposit addresses, one for each user, which is
an eth contract as well.
Sweep these deposit contracts when a user deposits eth or erc20 token to them. The funds end up in another central eth account.
A number of erc20 tokens seem to work perfectly, but USDT deposit transactions get reverted: https://etherscan.io/address/0x3cd5a0dc36a8f22011193f2a03910aa8260e64db
Without the original source code for these contracts (although I have the JSON ABI), it is hard to say:
Why it fails for USDT.
If these actually can support USDT, or what it would involve to do so.
If anyone can point in the right direction, it will be appreciated!
Controller contract:
0xEb818C6a48cCd60A8078aaa20997cC3CB2538C9E
Another contract involved linked to controller, called defaultSweeper:
0x8e7ABAF1316A0edB985e494F572Fdf148e8a7E93
EDIT:
It seems that USDT contract is missing some erc20 methods. Like transfer See: https://erc20-verifier.openzeppelin.com/ Why is this?
Tether contract was deployed before the ERC20 standard was finalized. Some functions don't follow the specs, for example transfer should return a boolean and it doesn't.
Recent versions of solc will check the returned data size is correct (since 0.5 I think).
My guess is that your contract was compiled with a recent version of solidity and it uses the standard ERC20 interface. And it fails because Tether returns nothing and it is expecting a bool value.
There isn't much you can do. Either Tether should upgrade its contract or the controller contract has to be modified to do not check the contract size. See SafeERC20 from OpenZepplin, they implemented a wrapper around ERC20 that allows interfacing with tokens that are not compliant with the standard.

How ERC20 exchange websites works?

Users can deposit on Binance, for example, various ERC20 tokens, but later then those tokens should be transferred to the Binance cold wallet(wallet used for withdrawing) how those tokens are transferred when the newly generated address doesn't contain any ether(for transferring contracts), it doesn't make any sense for me that Binance sends some ether for every newly generated address.
it doesn't make any sense for me that Binance sends some ether for every newly generated address.
That's exactly what they do. If you do happen to deposit ether, they will leave a little bit ~$5 or so in the address when moving the ether to their cold wallet to pay for future token transactions. If you transfer only tokens, they will transfer some ETH before moving it.

Automatically move received ether from one address to another

Right now I am working on a smart contract with which, people are able to gamble. I want to introduce a small fee, a user has to pay every time they send ether to the smart contracts address.
The fee should be automatically sent from the smart contract to my private ether wallet whenever someone starts a new game (sends ether to the contract)
since I am new to solidity and blockchain coding, any help is much appreciated
Just transfer the amount you want (your fee) to your address.
i.e.
uint256 yourFee = msg.value * percentFee;
yourAdress.transfer(yourFee);
Don't forget to make the function payable.