|
| 1 | +# Currency Swaps |
| 2 | + |
| 3 | +Swapping between different ERC20/native token currencies on a given Chain is made easy by Sequence's Smart Swap API. |
| 4 | + |
| 5 | +## GetSwapPrice |
| 6 | + |
| 7 | +This method can be used to get the current `SwapPrice` for a given buyCurrency and buyAmount using a given sellCurrency. |
| 8 | + |
| 9 | +``` |
| 10 | +ISwap swapper = new CurrencySwap(_chain); |
| 11 | +SwapPrice swapPrice = await swapper.GetSwapPrice(buyCurrencyTokenAddress, usdcAddress, "1000"); // where USDC is an example sell currency |
| 12 | +``` |
| 13 | +You can optionally subscribe to the `OnSwapPriceReturn` and `OnSwapPriceError` events in order to handle the response elsewhere. |
| 14 | + |
| 15 | +You can optionally provide a maximum allowable slippagePercentage. |
| 16 | + |
| 17 | +## GetSwapPrices |
| 18 | + |
| 19 | +This method is similar to `GetSwapPrice`, it can be used to get the current `SwapPrice` for a given buyCurrency and buyAmount. Except, instead of providing a sellCurrency, you instead provide the user's wallet address. |
| 20 | + |
| 21 | +This method will detect the eligible currencies (ERC20s or native tokens) that can be swapped for buyAmount of the buyCurrency and will return a `SwapPrice[]`. |
| 22 | + |
| 23 | +``` |
| 24 | +ISwap swapper = new CurrencySwap(_chain); |
| 25 | +SwapPrice[] swapPrices = await swapper.GetSwapPrices(userWalletAddress, buyCurrencyTokenAddress, "1000"); |
| 26 | +``` |
| 27 | +You can optionally subscribe to the `OnSwapPricesReturn` and `OnSwapPricesError` events in order to handle the response elsewhere. |
| 28 | + |
| 29 | +You can optionally provide a maximum allowable slippagePercentage. |
| 30 | + |
| 31 | +## GetSwapQuote |
| 32 | + |
| 33 | +This method is used to get an executable `SwapQuote` for a given userWallet address to buy buyAmount of buyCurrency using sellCurrency. |
| 34 | + |
| 35 | +``` |
| 36 | +ISwap swapper = new CurrencySwap(_chain); |
| 37 | +SwapQuote quote = await swapper.GetSwapQuote(userWalletAddress, buyCurrencyTokenAddess, usdcAddress, "1000", true); // where USDC is an example sell currency |
| 38 | +``` |
| 39 | + |
| 40 | +Once you've obtained a `SwapQuote`, you can transform it into a `Transaction[]` that can be submitted via your EmbeddedWallet to execute the swap. |
| 41 | + |
| 42 | +``` |
| 43 | +Transaction[] swapTransactions = quote.AsTransactionArray(); |
| 44 | +
|
| 45 | +_wallet.SendTransaction(_chain, swapTransactions); |
| 46 | +``` |
| 47 | + |
| 48 | +If `includeApprove = true` the SwapQuote response will also include the information required to create the required spend approval transaction(s). These will also be added to the `Transaction[]` created above via `SwapQuote.AsTransactionArray()`. As usual, with our embedded smart contract wallets, all of these transactions can be submitted at once in a bundle; significantly improving the UX for your players who would usually need to make separate transactions to approve and swap. |
0 commit comments