From 0af697a76e4a25496a2facb4ca4a2bb8a9400e5c Mon Sep 17 00:00:00 2001 From: Quinn Purdy Date: Thu, 12 Dec 2024 14:25:48 -0500 Subject: [PATCH] Added smart swap docs for Unity --- docs/pages/sdk/unity/currency-swaps.mdx | 48 +++++++++++++++++++++++++ nav.ts | 1 + 2 files changed, 49 insertions(+) create mode 100644 docs/pages/sdk/unity/currency-swaps.mdx diff --git a/docs/pages/sdk/unity/currency-swaps.mdx b/docs/pages/sdk/unity/currency-swaps.mdx new file mode 100644 index 00000000000..93b4167038d --- /dev/null +++ b/docs/pages/sdk/unity/currency-swaps.mdx @@ -0,0 +1,48 @@ +# Currency Swaps + +Swapping between different ERC20/native token currencies on a given Chain is made easy by Sequence's Smart Swap API. + +## GetSwapPrice + +This method can be used to get the current `SwapPrice` for a given buyCurrency and buyAmount using a given sellCurrency. + +``` +ISwap swapper = new CurrencySwap(_chain); +SwapPrice swapPrice = await swapper.GetSwapPrice(buyCurrencyTokenAddress, usdcAddress, "1000"); // where USDC is an example sell currency +``` +You can optionally subscribe to the `OnSwapPriceReturn` and `OnSwapPriceError` events in order to handle the response elsewhere. + +You can optionally provide a maximum allowable slippagePercentage. + +## GetSwapPrices + +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. + +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[]`. + +``` +ISwap swapper = new CurrencySwap(_chain); +SwapPrice[] swapPrices = await swapper.GetSwapPrices(userWalletAddress, buyCurrencyTokenAddress, "1000"); +``` +You can optionally subscribe to the `OnSwapPricesReturn` and `OnSwapPricesError` events in order to handle the response elsewhere. + +You can optionally provide a maximum allowable slippagePercentage. + +## GetSwapQuote + +This method is used to get an executable `SwapQuote` for a given userWallet address to buy buyAmount of buyCurrency using sellCurrency. + +``` +ISwap swapper = new CurrencySwap(_chain); +SwapQuote quote = await swapper.GetSwapQuote(userWalletAddress, buyCurrencyTokenAddess, usdcAddress, "1000", true); // where USDC is an example sell currency +``` + +Once you've obtained a `SwapQuote`, you can transform it into a `Transaction[]` that can be submitted via your EmbeddedWallet to execute the swap. + +``` +Transaction[] swapTransactions = quote.AsTransactionArray(); + +_wallet.SendTransaction(_chain, swapTransactions); +``` + +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. \ No newline at end of file diff --git a/nav.ts b/nav.ts index b8ad0b990fd..b8546e16ec4 100644 --- a/nav.ts +++ b/nav.ts @@ -662,6 +662,7 @@ export const sidebar = { { text: 'Session Management', link: '/sdk/unity/session-management' }, { text: 'On-Ramp Funds via Credit Card', link: '/sdk/unity/onboard-user-funds' }, { text: 'Marketplace', link: '/sdk/unity/marketplace' }, + { text: 'Currency Swaps', link: '/sdk/unity/currency-swaps' }, { text: 'Connect with External Wallets', link: '/sdk/unity/connecting-external-wallets',