Transfer ERC20 token without ETH - ethereum

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.

Related

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

How to get gas fee to send erc20 token at mass volume?

I am learning node JS developing a crypto currency exchanger as a homework project where user can deposit ERC-20 token and Ethereum. I am generating unique ethereum deposit address for every user, where they can deposit ERC-20 token and ethereum. but after sometime I want to move the received ethereum and ERC-20 token to another address (cold wallet).
If someone only send ERC-20 token to the address and no etherum, then how I'll get ethereum to pay as gas fee to transfer the received ERC-20 token to cold wallet?
If suppose, I am first paying etherum (for gas fee) from cold address to the user address, and then using that etherum to transfer received ERC-20 token, will it work at mass level ? means if 10000 users do the same thing at the same time, will not it create a problem ? or is there any better solution for this?
If there's no eth in that account, you can't take the ERC-20 token out.
Transfer some eth to the address and then get ERC-20 token out is the only way. The problem is how to save the gas as far as you can.
You can write a batch transfer contract to send eth to several hundred addresses in one go (the ceiling is determined by block gas limit), batch transfer saves gas. Then you can collect the ERC-20 tokens.
Of course, you can charge your user for compensation.

How to make automatic LP providing after time lock

I am learning solidity and want to create a contract that will lock token for Uniswap LP. What I've created is:
List item
on token creation I am creating uniswap pair with that token
I am creating a Timelock for token amount assigned to creator address
now on release() method I would like to automatically add liquidity with eth value that has been sent and token that was locked
Problem here is that to add LP to uniswap token needs to be approved and from what I see the only way to approve token is to first send this token to creator address.
Is it possible to make it automatic so that we don't need to trust contract creator to add LP after it has been released to him?
You can have non-standard ERC-20 token that allows perform approve for Uniswap contract addresses on special conditions (e.g. before a certain time or similar).
You would directly update allowances table in your token. You do not need to use approve() from ERC-20 because it is your token.

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.

How does a User account own an ERC20 Token

This question is a little conceptual, so hopefully this picture will help clear up my misunderstanding.
Image there is a crowdsale smart contract deployed on address 0x2. A User at address 0x01 buys a token.
Here is my understanding of what happens:
The crowdsale contract (# address: 0x2) accepts ether from the user account (# address: 0x1)
The crowdsale contract stores 0x1 as having purchased a token (important: this information is stored in the smart contract #address 0x2)
Now my Question: If 0x1 is a user account (and not a smart contract) there is no code at address 0x1. I thought a user account just consisted of an address + ether associated with the address, how can it also store the fact that 0x1 owns an ERC20 token? For example, I can login to MetaMask and (before clicking the "add token" option) MetaMask can see that I have a token... how is this possible?
Every ERC20 contract has the following function:
function balanceOf(address _owner) public view returns (uint256 balance) {
return balances[_owner];
}
Your wallet just calls this function from the known token contracts with your address. Since it's a view function it doesn't cost any gas.
I recon most ERC20 token get added rather quickly to a wallet like Metamask or MEW. But if your balance doesn't automatically show, you can add the contract address manually (in MEW at least, not sure about Metamask) and it will show up afterwards.
In solidity there are two ways to get the address of the person who sent the transaction
tx.origin
msg.sender
In your example, in the method inside ERC20 Token.sol, the value tx.origin will be 0x1 and msg.sender will be 0x2
So to answer your question, how does the ERC20 token know about 0x2 is: it depends on how the token contract is written and whether it uses tx.origin or msg.sender. I would imagine it uses msg.sender, because that is the more prevalent one.
If it does use msg.sender you can still make the crowdsale contract work by first buying the tokens and then immediatelly transfering the tokens from the crowdsale contract to the caller.
For more information, refer to What's the difference between 'msg.sender' and 'tx.origin'?
how can it also store the fact that 0x1 owns an ERC20 token?
Token transfers, or transfers in accounting in general, are kept in a ledger. In this case, the ledger is ERC-20 smart contract that internally keeps balances who owns and what in its balances mapping. Or, the smart contract manage the storage (EVM SSTORE instructions) where the records of ownership are kept.
Note that some other blockchains, like Telos and EOS (and mayne Solana) might be opposite and there the storage is maintained on the user account (user account has associated RAM and tables for any token user owns).