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.

I am thinking of just sending this contract of mine USDC, so the payout will come directly from the contract, instead of a separate wallet, and will enable me to just call transfer() with msg.sender being my buyer contract. Will this work? Is this a good approach? Can a smart contract hold/own USDC(or other tokens) ?

1 Answer 1

2

Contract can hold an ERC20 token, since "holding" is just a record in the ERC20 contract that states which address owns what. It doesn't care if the address is a contract or a wallet.

But.. a contract can't initiate a transaction on its own. The buyback will have to be fired from someone with the private key of that address, can be a backend or MetaMask.

2
  • Thanks for that, does that mean I can just transfer USDC to the contract. And I can just trigger the transfer() call inside the function that buys the token?
    – momoja
    Commented May 10, 2022 at 6:44
  • Yes, but you better learn the whole process of working with contracts before writing something yourself.
    – Kof
    Commented May 10, 2022 at 10:48

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.