Create ERC20 token swap

Hi there im new to this side of things know a little but was wondering about a smart contract for swapping erc20 token for another or multipul, for example i have 3 erc20 tokens and i want to combine them into 1 token, ach of the 3 tokens has a different value so each will get a different amount of the new token, what i would like to do is the holders of the old tokens send in these tokens and the it would auto send out the new tokens to the original sending wallet, depending on what token they sent in they would recieve xamount of the new token.
and for it to be done in the same transaction to save on fees.
Is it possible , i am learning and also if i have placed this in the wrong spot, sorry.

1 Like

So you mean you want to integrate three transactions into one transaction? But when you swap these tokens , you should specify the swap path, a little complex, I do not have a better idea.

1 Like

Hi @LogicDEX_LogicDEX,

Welcome to the community :wave:

What you describe should be possible, though I suspect potentially quite challenging depending on how volatile the price of the tokens are against each other and depends on what your use case is.

You could look at how other projects have done this. If I recalI correctly, I believe mStable does something similar with a basket of stable tokens being used to mint their stable token and then allows withdrawing back to the basket tokens.

Hi and thanks for the replys, maybe i didnt explain what i was looking at doing correctly or with enough detail.
Ok in the eco-system there are 3 erc20 tokens lets call them Token A, Token B and Token C.
These tokens have different purposes and values.
What i was looking to do is have a swap to a new token, basically combining them toa single token for easier management and smoother use within the system.
Token A would have a swap value of 1 - 1
Token B would have a swap value of 1 - 100
Token C would have a swap value of 1 - 1000 for example.
What i would like is to set up a contract were holders of token A/B or C could sent them to the contract and recieve back the relevant amount of the new token, im not sure if i would have to create a contract for each token or if they could be in 1 single contract, the new token would already be created and sent to the contract for the swap.
I have seen things like send ether and auto send back erc20 tokens like with ICO and such, but this would be send old erc20 token in recieve new one back and amount you recieve back would be dependent on which on of the tokens you sent in.
And obviously the person sending the old token in would pay gas, but the new token to get sent back in the same transaction, hence saving gas fees.
Like i said im new and was wondering if there was a template contract that could do this, am learning all about this and thanks in advance for advice and help!!

1 Like

Hi @LogicDEX_LogicDEX,

There isn’t an existing contract in OpenZeppelin Contracts for this type of exchange. You could look at the concepts in a Crowdsale contract where people pay ETH and get given an ERC20 token, either from the crowdsale contracts supply or minted.

You could then also have the reverse where a token holder sent in the new token and got back their old deposit.

Please note, token holders would need to approve an allowance to your exchange contract and then call to do the exchange.