Skip to content

Commit

Permalink
[hosted-sdk] add demo for aggregation
Browse files Browse the repository at this point in the history
  • Loading branch information
leduythuccs committed Nov 5, 2024
1 parent 8ae5a25 commit 0f4bf73
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
3 changes: 2 additions & 1 deletion hosted-sdk-demo/src/constants.ts
Original file line number Diff line number Diff line change
@@ -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 = '<RECEVER_ADDRESS>';
export const RECEIVER_ADDRESS = '<RECEVER_ADDRESS>';
44 changes: 43 additions & 1 deletion hosted-sdk-demo/src/swap.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand Down Expand Up @@ -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<SwapData>(`/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<SwapData>(`/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);
}

0 comments on commit 0f4bf73

Please sign in to comment.