diff --git a/hosted-sdk-demo/src/constants.ts b/hosted-sdk-demo/src/constants.ts index fdbcab1..f70a541 100644 --- a/hosted-sdk-demo/src/constants.ts +++ b/hosted-sdk-demo/src/constants.ts @@ -1,9 +1,10 @@ // Tokens export const wstETH = '0x7f39C581F595B53c5cb19bD0b3f8dA6c935E2Ca0'; +export const USDC = '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48'; export const MARKET_ADDRESS = '0x34280882267ffa6383b363e278b027be083bbe3b'; export const SY_ADDRESS = '0xcbc72d92b2dc8187414f6734718563898740c0bc'; export const PT_ADDRESS = '0xb253eff1104802b97ac7e3ac9fdd73aece295a2c'; export const YT_ADDRESS = '0x04b7fa1e727d7290d6e24fa9b426d0c940283a95'; export const CHAIN_ID = 1; -export const RECEIVER_ADDRESS = ''; \ No newline at end of file +export const RECEIVER_ADDRESS = ''; diff --git a/hosted-sdk-demo/src/swap.ts b/hosted-sdk-demo/src/swap.ts index 869211d..74d5723 100644 --- a/hosted-sdk-demo/src/swap.ts +++ b/hosted-sdk-demo/src/swap.ts @@ -1,4 +1,4 @@ -import { CHAIN_ID, MARKET_ADDRESS, PT_ADDRESS, RECEIVER_ADDRESS, SY_ADDRESS, wstETH, YT_ADDRESS } from "./constants"; +import { CHAIN_ID, MARKET_ADDRESS, PT_ADDRESS, RECEIVER_ADDRESS, SY_ADDRESS, USDC, wstETH, YT_ADDRESS } from "./constants"; import { callSDK, getSigner } from "./helper"; import { SwapData } from "./types"; @@ -137,3 +137,45 @@ export async function swapYtToToken() { // Send tx getSigner().sendTransaction(res.tx); } + +// enable aggregator + +export async function swapTokenToPtUsingAggregation() { + // Swap 1000 USDC to PT in wstETH market with 1% slippage + const res = await callSDK(`/v1/sdk/${CHAIN_ID}/markets/${MARKET_ADDRESS}/swap`, { + receiver: RECEIVER_ADDRESS, + slippage: 0.01, + tokenIn: USDC, + tokenOut: PT_ADDRESS, + // USDC has 6 decimals + amountIn: (1000n * 10n ** 6n).toString(), + // enable aggregator, else it will throw an error because USDC could not be directly swapped to PT + enableAggregator: true, + }); + + console.log('Amount PT Out: ', res.data.amountOut); + console.log('Price impact: ', res.data.priceImpact); + + // Send tx + getSigner().sendTransaction(res.tx); +} + + +export async function swapPtToTokenUsingAggregation() { + // Swap 1 PT to USDC in wstETH market with 1% slippage + const res = await callSDK(`/v1/sdk/${CHAIN_ID}/markets/${MARKET_ADDRESS}/swap`, { + receiver: RECEIVER_ADDRESS, + slippage: 0.01, + tokenIn: PT_ADDRESS, + tokenOut: USDC, + amountIn: '1000000000000000000', + // enable aggregator + enableAggregator: true, + }); + + console.log('Amount USDC Out: ', res.data.amountOut); + console.log('Price impact: ', res.data.priceImpact); + + // Send tx + getSigner().sendTransaction(res.tx); +}