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
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
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
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.