Skip to content

Instantly share code, notes, and snippets.

@bjoveski
Last active November 12, 2022 22:42
Show Gist options
  • Save bjoveski/1784320db7a6a4879d36801871a021c1 to your computer and use it in GitHub Desktop.
Save bjoveski/1784320db7a6a4879d36801871a021c1 to your computer and use it in GitHub Desktop.
contract Forwarder {
address public destination;
constructor(address _destination) public {
destination = _destination;
}
function flushERC20(address tokenContractAddress) public {
IERC20 tokenContract = ERC20(tokenContractAddress);
uint256 forwarderBalance = tokenContract.balanceOf(address(this));
tokenContract.transfer(destination, forwarderBalance);
}
}
@oleksandr-shubin
Copy link

Shouldn't it be IERC20 tokenContract = ERC20(tokenContractAddress);? Isn't it a typo?

@bjoveski
Copy link
Author

bjoveski commented Jun 1, 2021

Yes, you are correct! Updated the gist, thank you for the note!

@ksaitor
Copy link

ksaitor commented Aug 13, 2021

How does the forwarder contract know which token to forward?
Or is that determined via a separate call to flushERC20(address tokenContractAddress)?
It looks like Coinbase Commerce provides the same address, e.g. 0x3d0a...2c7a, for all three payment methods ETH, DAI, USDC.
Yet, TX event logs for a successful transactions don't seem to call flushERC20 at all.
How does this work?

@bjoveski
Copy link
Author

This part is a bit complicated.

All interactions Coinbase has are through the factory contract. The first time you pay to an address, that factory will create a Forwarder & flush the ERC20 token indicated in the argument. The second time you pay to the same address, the factory will call a different method that will just flush the tokens (again, the token is indicated in the argument).

This is also made a bit more difficult, since you are actually calling the original Forwarder and not the proxy copies; so the tx logs might look funky at a first glance.

ETH is handled differently from the ERC20 tokens.

@smclof
Copy link

smclof commented Sep 8, 2021

Hi
I tried to pay the customer via coinbase commerce with this forwarder contract but I accidentally sent it via BEP20 instead of ERC20, what needs to be done now in order to flush the ETH PEG tokens to the merchant final address?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment