Swap tokens 1:1 - Crowdsale perhaps?


I need to swap to ERC20 tokens 1:1, and I have been looking for a Crowdsale contract which allows this but I cannot find one which takes ERC20 as "payment".

I have also been looking for af Buy/Sell contract which basically does the same, but I cannot find this either.

I can actually make do with a contract which send 1 tokenA whenever it recieves 1 tokenB


A contract cannot know if it is receiving erc20 tokens unless the erc20 contract does explicitly tell this concract about it.

well you can create a contract where someone deposits token A and receives token B. If you want to know more you can contact me on telegram: @solidityX

Yes this is quite straight forward.

You create an interface within the contract to hold the ERC20 so that the contract can contact the ERC20 and access its approval and transfer functions and other things you might like to use

interface IERC20Token {

    function balanceOf(address account) external view returns (uint256);
    function transfer(address recipient, uint256 amount) external returns (bool);
    function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
    function approve(address spender, uint256 amount) external returns (bool);

Then you declare the IERC20Token Interface as your token

IERC20Token internal myerc20Token;

    constructor () {
        myerc20Token= IERC20Token(YourTokenContractAddressHereNoQuotes);  

From which you can then interface with the ERC to allow for approval and transfer.

When you are transferring ERC20 tokens into the contract, it must happen in two parts.
A. The sender calls the ERC20 and approves the contract to send to with an amount to send.
B. The sender calls the deposit function in the approved contract to send the approved amount.

When you are retrieving the ERC20 tokens from the contract, the contract itself can call the ERC20 and use the approval function to approve the amount and the receiver prior to transferring the amount to the receiver. Example below moves all balance from contract to user.

       myerc20Token.transfer(msg.sender, myerc20Token.balanceOf(address(this)));

What many fail to get, is that it is not the coin in the wallet but the wallet in the coin.

If your using a token that is pooled you will want to look at what you can use in that pooling contract

Hope that helps