I want to create a very simple swap between my own ERC20 and USDC.

I'm coding this all in Vyper and Python.

One way I have envisaged this working is by monitoring the USDC Transfer events on the blockchain and then having an off-chain program that mints and burns my ERC20 accordingly depending on whether someone wants to buy or sell my ERC20 token using USDC.

The mechanics I have envisioned are as follows:

Receiving USDC sending my ERC20 token (Someone buys my token)

The USDC Transfer event would have as a to: my contract address. My off chain listener, would then mint my ERC20 token and send it to the from: address that sent USDC to my contract.

Question 1:

  • If my contract receives USDC, how can it then access those funds? How can it send them to another wallet. I want to make sure that any USDC sent to the contract is easily accessible. Sample code on Vyper would be awesome.

Receiving ERC20 token sending USDC (Someone sells my token in exchange for USDC)

I would have a method which would burn my ERC20 token and emit a special Event (lets call it SELL2USDC).

Question 2: How can I send USDC from my contract to a third party address ? Sample Vyper code would be awesome.

Question 3: Would my contract need to have a USDC balance before it sends out USDC. What if there's not enough ? I'm guessing the contract would revert and transaction would fail.

2 Answers 2


I cannot help you with your Vyper/Python implementation but I think a better solution is to keep entire workflow on-chain. When a user wants to swap USDC for your ERC20 token, you can call Approve/TransferFrom functions from USDC contract to transfer the tokens to your smart contract, once that's done you can mint and send tokens to the user. No off-chain code needed (and thus more secured).

If you want the USDC in your contract to be accessible, it's always possible to implement an admin-only "Withdraw" function that would allow you to send the token to any other address. You can also programatically determine what the contract will do with the USDC (for example, lending it out via Aave) within the contract.

Regarding your third question, yes. You must always ensure your contract has enough USDC balance to back the outstanding ERC20 if you want to ensure users can swap back and forth at will. The transaction fails otherwise.

  • This is super useful, thank you - what would be the best way to 'programmatically determine what the contract will do with the USDC for example lending out on AAVE" do you have any examples (even in solidity) that I could refer to to get a better understanding of the mechanics ? Sep 29, 2021 at 21:40
  • You can programatically interact with other contracts (similarly to API calls). For this you need the interface of contract you want to use and its address. See sample application interacting with Uniswap in Solidity: solidity-by-example.org/interface
    – Bigbrotha
    Sep 29, 2021 at 22:35

Note to future self:

This looks extremely relevant to the task at hand:


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.