Implement a Swap
When trading from a smart contract, the most important thing to keep in mind is that access to an external price source is required. Without this, trades can be frontrun for considerable loss.
Using the Router
The easiest way to safely swap tokens is to use the router, which provides a variety of methods to safely swap to and from different assets. You'll notice that there is a function for each permutation of swapping to/from an exact amount of BTCC/tokens.
First you must use an external price source to calculate the safety parameters for the function you'd like to call. This is either a minimum amount received when selling an exact input or the maximum amount you are willing to pay when a buying an exact output amount
It is also important to ensure that your contract controls enough BTCC/tokens to make the swap, and has granted approval to the router to withdraw this many tokens.
transferFrom
Before swapping, our smart contracts needs to be in control of 50 USDT. The easiest way to accomplish this is by calling transferFrom
on USDT with the owner set to msg.sender
:
approve
Now that our contract owns 50 USDT, we need to approve to the router to withdraw this USDT:
Safety Considerations
Because BTC20 Smart Chain transactions occur in an adversarial environment, smart contracts that do not perform safety checks can be exploited for profit. If a smart contract assumes that the current price on Catena swap is a "fair" price without performing safety checks, it is vulnerable to manipulation. A bad actor could e.g. easily insert transactions before and after the swap (a "sandwich" attack) causing the smart contract to trade at a much worse price, profit from this at the trader's expense, and then return the contracts to their original state. (One important caveat is that these types of attacks are mitigated by trading in extremely liquid pools, and/or at low values.)
The best way to protect against these attacks is to use an external price feed or "price oracle". The best "oracle" is simply traders' off-chain observation of the current price, which can be passed into the trade as a safety check. This strategy is best for situations where users initiate trades on their own behalf.
Last updated