Safe Token Swap – Creating a Contract to Swap One Token for Another

erc-20tokens

If I mint a token and the contract doesn't have all the features I needed from the offset, is there a way I could develop a new contract for a new token and create a way in which people can send the Old token and be returned the new one?

I had a client who was eager to mint coins to distribute to early private investors but the actual way the token will be used or interacted with will change over time, so I'm not too sure how to go about this.

Or perhaps I'm missing something and we can extend the contract.

Best Answer

Yes it is entirely possible. Create contract A that is standard ERC20. When it is time to upgrade, create contract B with your new features.

contract B has a function upgradeFrom(address old, uint amount)

Investors then use call approve(B, uint amount) on A. This allows contract B to retrieve the funds from A.

Investors then call upgradeFrom(B, uint amount), and the upgradeFrom function withdraws the funds from A and does any other logic to set up the investor in contract B.

Another way:

If you don't care about contract A after contract B is in production, you can just have a single function in contract B called upgrade() that checks the balance of the sender in contract A and updates it in contract B. You will also want to make sure you record who has already upgraded as people could call the function multiple times.

Related Topic