Looking for a quickstart?
The SDK cannot execute trades or send transactions on your behalf. Rather, it offers utility classes and functions which make it easy to calculate the data required to safely interact with Pegasys. Nearly everything you need to safely transact with Pegasys is provided by the Trade entity. However, it is your responsibility to use this data to send transactions in whatever context makes sense for your application.
This guide will focus exclusively on sending a transaction to the currently recommended Pegasys router
Let's say we want to trade 1 WSYS for as much DAI as possible:
So, we've constructed a trade entity, but how do we use it to actually send a transaction? There are still a few pieces we need to put in place.
Before going on, we should explore how SYS works in the context of trading. Internally, the SDK uses WSYS, as all Pegasys pairs use WSYS under the hood. However, it's perfectly possible for you as an end user to use SYS, and rely on the router to handle converting to/from WSYS. So, let's use SYS.
The first step is selecting the appropriate router function. The names of router functions are intended to be self-explanatory; in this case we want swapExactSYSForTokens, because we're swapping an exact amount of SYS for tokens.
That Solidity interface for this function is:
Jumping back to our trading code, we can construct all the necessary parameters:
The slippage tolerance encodes how large of a price movement we're willing to tolerate before our trade will fail to execute. Since Syscoin NEVM transactions are broadcast and confirmed in an adversarial environment, this tolerance is the best we can do to protect ourselves against price movements. We use this slippage tolerance to calculate the minumum amount of DAI we must receive before our trade reverts, thanks to minimumAmountOut. Note that this code calculates this worst-case outcome assuming that the current price, i.e the route's mid price, is fair (usually a good assumption because of arbitrage).
The path is simply the ordered list of token addresses we're trading through, in our case WSYS and DAI (note that we use the WSYS address, even though we're using SYS).
The to address is the address that will receive the DAI.
The deadline is the Unix timestamp after which the transaction will fail, to protect us in the case that our transaction takes a long time to confirm and we wish to rescind our trade.
The value is the amount of SYS that must be included as the
msg.value in our transaction.