1

I want to write a contract that buys back an ERC20 Token using USDC. (in REMIX IDE)

I want to understand what is the best practice for paying out the USDC in return for the ERC20 that was sold to the contract.

Right now, I am calling a

usdcAddress.transferFrom(address(buyer), msg.sender, price);

It just dawned on me that I need to the buyer wallet address to call APPROVE before I can call the transferFrom which is something I cannot do in production. Only the msg.sender will be able to call approve via the web3.

What is the best practice to implement this scenario.

2
  • Some tokens interpret token.approve(type(uint256).max, operator) as the owner approving operator to make infinite withdrawals with transferFrom. Unfortunately it doesn't seems to be the case for the current USDC implementation.
    – Ismael
    Commented May 11, 2022 at 4:11
  • thanks for this. however, I do not want to call approve on the contract itself, I'd much rather have this done on web3 by the actual user interacting with my contract.
    – momoja
    Commented May 11, 2022 at 8:44

3 Answers 3

0

Well this may not be the best way but you can do this in 2 steps instead of doing this in 1 step . Example : Step1 : Seller deposits the ERC20 token to smart contract first. Step2 : Buyer take ERC20 token from smart contract and in same function buyer will be sending USDC to the seller as well.

1
  • Thanks for this, however, only the seller of the token is the live person here. I want my contract/wallet to automatically transfer the USDC as payment to the address that sold its tokens.
    – momoja
    Commented May 10, 2022 at 5:34
0

What I ended up doing is deciding to just regularly send in USDC to my contract, and just call transfer() from there. If there are any other suggestions, it's still welcome. Thank you

2
  • 1
    This is NOT how you should do it. You should have your user call approve first (this cannot be done through a smart contract in solidity, your user needs to call approve(yourContractAddress, amount) where amount would be type(uint256).max. ) this can easily be done with javascript (web3js.readthedocs.io/en/v1.2.11/web3-eth-contract.html#id26) And THEN call the function that calls transferFrom(userAddres, msg.sendr, amount)
    – Foxxxey
    Commented Sep 30, 2022 at 19:23
  • thanks for this callout! yes I agree, the user must call APPROVE for the tokens he wants to "sell" to the contract. However, my smart contract will just auto "buy" those tokens so I just transfer directly as pay out to the seller.
    – momoja
    Commented Oct 4, 2022 at 14:30
0

The best practice for implementing this scenario would be to have the buyer first approve the contract to transfer the USDC from their account before calling the buyback function. This way, the contract can call the transferFrom() function without needing the buyer's address.

Here is an example of how this could be implemented:

The buyer calls the approve() function on the USDC contract, passing in the address of the buyback contract and the amount of USDC they wish to use for the buyback. css

usdcAddress.approve(address(buybackContract), price);

The buyer then calls the buyback function on the buyback contract, passing in the address of the ERC20 token they wish to sell and the amount of tokens they wish to sell. scss

buybackContract.buyback(tokenAddress, tokenAmount);

The buyback function in the buyback contract checks that the buyer has approved the contract to transfer the necessary amount of USDC, then calls the transferFrom() function on the USDC contract to transfer the funds from the buyer's account to the contract's account. less

require(usdcAddress.allowance(msg.sender, address(buybackContract)) >= price, "Not enough approved USDC."); usdcAddress.transferFrom(msg.sender, address(buybackContract), price);

Finally, the buyback function calls the transfer() function on the ERC20 token contract to transfer the bought tokens to the contract's address.

tokenAddress.transfer(address(buybackContract), tokenAmount);

It is important to note that the buyer has to trust the contract enough to approve it to transfer their funds, and also the contract should be designed to ensure the buyer's funds are safe and the buyer can get the correct amount of tokens they paid for.

Additionally, it's important to consider the gas fees and make sure the contract has enough balance to pay the gas fees before executing the transaction.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.