Automatically move received ether from one address to another - ethereum

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.

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.

How to consume msg.value?

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

Burn ETH from balance of contract

I have a contract that is funded with ETH. I want to simulate a burn of ETH when a given condition is met. Is the following an acceptable way of burning ETH?
//Burn ether simulating payment of monthly rent
address burn = address(0x00);
burn.transfer(rentalAmount);
Burning means losing it forever in a provable way.
A right way to burn ether is sending it to an address which you don't have the private key.
Others can't be sure if you have the private key or not, unless you send it to a special address, like 0x0. So when you send ether to 0x0, we call it burning.

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.

Can third party send an ERC20 token transaction to ethereum blockchain?

I want to create a smart contract which people can transfer tokens without ether in their wallet.
Suppose A want to transfer ERC20 tokens to B, but he does not have ether in his wallet.
Does third party C can send the transaction for A and therefore pay the gas? Is it possible to create a function in the contract for this usgae?
I have searched online for a soloution and could not find one.
This is a key issue of Ethereum dApp development, but also of tokens. Here is a very old thread on Ethereum Stack Exchange, and also this one.
There are 2 options with their pros and cons:
Use signatures
Every function in your smart contract must have signature parameter.
People who want to interact with the smart contract must sign the function parameters with their account's private key and send it to the smart contract owner (via any communication channel).
The owner then submits the parameters along with the signature to the blockchain, paying for gas. The signature guarantees that the message was approved by the user.
Refund used gas at the end of the transaction. A modifier refundGasCost can be used for this (see below).
But (1) is really hard to apply to token transfers where you just don't know who uses the token and (2) does not really address the issue.
There is a lot happening recently, there is this blog post about How to save your Ethereum Dapp users from paying gas for transactions, and since you ask about tokens, there is an ERC that suggests to Pay transfers in tokens instead of gas, in one transaction which would be nice if you have tokens but no ETH.
I hope this helps.
Exactly this case is already defined in the ERC20 standard. Its this function:
function transferFrom(address from, address to, uint tokens) public returns (bool success);
But before party C could use it and send tokens from A to B, A would have to approve C to do this via the following function, which is also defined in the ERC20 standard:
function approve(address spender, uint tokens) public returns (bool success);
No, in a standard ERC20 token contract, the token holder must initiate at least one transaction (to call transfer() or approve()), and a transaction must always be paid for by its initiator.