Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[hosted-sdk] add demo for aggregation #12

Merged
merged 1 commit into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
Loading