Skip to content

Commit

Permalink
Add hosted SDK example (#4)
Browse files Browse the repository at this point in the history
* Add hosted SDK docs

* remove `enableAggregator` since it is enabled by default

* Add `/api` to endpoints
  • Loading branch information
tientran3 authored Aug 22, 2024
1 parent 40c0e7a commit e5f2f27
Show file tree
Hide file tree
Showing 15 changed files with 1,258 additions and 0 deletions.
15 changes: 15 additions & 0 deletions hosted-sdk-demo/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "hosted-sdk-demo",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"dependencies": {
"axios": "^1.7.4",
"ethers": "5",
"typescript": "^5.5.4"
},
"devDependencies": {
"@types/node": "^22.2.0",
"ts-node": "^10.9.2"
}
}
37 changes: 37 additions & 0 deletions hosted-sdk-demo/src/add-liquidity-dual.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { CHAIN_ID, MARKET_ADDRESS, RECEIVER_ADDRESS, SY_ADDRESS, wstETH } from "./constants";
import { callSDK, getSigner } from "./helper";
import { AddLiquidityDualData } from "./types";

export async function addLiquidityDualSyAndPt() {
// Use 1 SY and 1 PT to add liquidity to wstETH pool with 1% slippage
const res = await callSDK<AddLiquidityDualData>(`/v1/sdk/${CHAIN_ID}/markets/${MARKET_ADDRESS}/add-liquidity-dual`, {
receiver: RECEIVER_ADDRESS,
slippage: 0.01,
tokenIn: SY_ADDRESS,
amountTokenIn: '1000000000000000000',
amountPtIn: '1000000000000000000',
});

console.log('Amount LP Out: ', res.data.amountOut);
console.log('Price impact: ', res.data.priceImpact);

// Send tx
getSigner().sendTransaction(res.tx);
}

export async function addLiquidityDualTokenAndPt() {
// USe 1 wstETH and 1 PT to add liquidity to wstETH pool with 1% slippage
const res = await callSDK<AddLiquidityDualData>(`/v1/sdk/${CHAIN_ID}/markets/${MARKET_ADDRESS}/add-liquidity-dual`, {
receiver: RECEIVER_ADDRESS,
slippage: 0.01,
tokenIn: wstETH,
amountTokenIn: '1000000000000000000',
amountPtIn: '1000000000000000000',
});

console.log('Amount LP Out: ', res.data.amountOut);
console.log('Price impact: ', res.data.priceImpact);

// Send tx
getSigner().sendTransaction(res.tx);
}
87 changes: 87 additions & 0 deletions hosted-sdk-demo/src/add-liquidity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { CHAIN_ID, MARKET_ADDRESS, PT_ADDRESS, RECEIVER_ADDRESS, SY_ADDRESS, wstETH } from "./constants";
import { callSDK, getSigner } from "./helper";
import { AddLiquidityData } from "./types";

export async function addLiquiditySinglePt() {
// Use 1 PT to add liquidity to wstETH pool with 1% slippage
const res = await callSDK<AddLiquidityData>(`/v1/sdk/${CHAIN_ID}/markets/${MARKET_ADDRESS}/add-liquidity`, {
receiver: RECEIVER_ADDRESS,
slippage: 0.01,
tokenIn: PT_ADDRESS,
amountIn: '1000000000000000000'
});

console.log('Amount LP Out: ', res.data.amountLpOut);
console.log('Price impact: ', res.data.priceImpact);

// Send tx
getSigner().sendTransaction(res.tx);
}

export async function addLiquiditySingleSy() {
// Use 1 SY to add liquidity to wstETH pool with 1% slippage
const res = await callSDK<AddLiquidityData>(`/v1/sdk/${CHAIN_ID}/markets/${MARKET_ADDRESS}/add-liquidity`, {
receiver: RECEIVER_ADDRESS,
slippage: 0.01,
tokenIn: SY_ADDRESS,
amountIn: '1000000000000000000',
});

console.log('Amount LP Out: ', res.data.amountLpOut);
console.log('Price impact: ', res.data.priceImpact);

// Send tx
getSigner().sendTransaction(res.tx);
}

export async function addLiquiditySingleSyKeepYt() {
// Use 1 SY to add liquidity to wstETH pool (zero price impact mode) with 1% slippage
const res = await callSDK<AddLiquidityData>(`/v1/sdk/${CHAIN_ID}/markets/${MARKET_ADDRESS}/add-liquidity`, {
receiver: RECEIVER_ADDRESS,
slippage: 0.01,
tokenIn: SY_ADDRESS,
amountIn: '1000000000000000000',
zpi: true,
});

console.log('Amount LP Out: ', res.data.amountLpOut);
console.log('Amount YT Out: ', res.data.amountYtOut);
console.log('Price impact: ', res.data.priceImpact);

// Send tx
getSigner().sendTransaction(res.tx);
}

export async function addLiquiditySingleToken() {
// Use 1 wstETH to add liquidity to wstETH pool with 1% slippage
const res = await callSDK<AddLiquidityData>(`/v1/sdk/${CHAIN_ID}/markets/${MARKET_ADDRESS}/add-liquidity`, {
receiver: RECEIVER_ADDRESS,
slippage: 0.01,
tokenIn: wstETH,
amountIn: '1000000000000000000'
});

console.log('Amount LP Out: ', res.data.amountLpOut);
console.log('Price impact: ', res.data.priceImpact);

// Send tx
getSigner().sendTransaction(res.tx);
}

export async function addLiquiditySingleTokenKeepYt() {
// Use 1 wstETH to add liquidity to wstETH pool (zero price impact mode) with 1% slippage
const res = await callSDK<AddLiquidityData>(`/v1/sdk/${CHAIN_ID}/markets/${MARKET_ADDRESS}/add-liquidity`, {
receiver: RECEIVER_ADDRESS,
slippage: 0.01,
tokenIn: wstETH,
amountIn: '1000000000000000000',
zpi: true,
});

console.log('Amount LP Out: ', res.data.amountLpOut);
console.log('Amount YT Out: ', res.data.amountYtOut);
console.log('Price impact: ', res.data.priceImpact);

// Send tx
getSigner().sendTransaction(res.tx);
}
9 changes: 9 additions & 0 deletions hosted-sdk-demo/src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Tokens
export const wstETH = '0x7f39C581F595B53c5cb19bD0b3f8dA6c935E2Ca0';
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>';
27 changes: 27 additions & 0 deletions hosted-sdk-demo/src/helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import axios from 'axios';
import { ethers } from 'ethers';

const HOSTED_SDK_URL = 'https://api-v2.pendle.finance/api/external/';

type MethodReturnType<Data> = {
tx: {
data: string;
to: string;
value: string;
};
data: Data;
};

export async function callSDK<Data>(path: string, params: Record<string, any> = {}) {
const response = await axios.get<MethodReturnType<Data>>(HOSTED_SDK_URL + path, {
params
});

return response.data;
}

export function getSigner() {
const provider = new ethers.providers.JsonRpcProvider('https://rpc.ankr.com/eth');
const signer = new ethers.Wallet(process.env.PRIVATE_KEY!, provider);
return signer;
}
37 changes: 37 additions & 0 deletions hosted-sdk-demo/src/mint-py.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { CHAIN_ID, RECEIVER_ADDRESS, SY_ADDRESS, wstETH, YT_ADDRESS } from "./constants";
import { callSDK, getSigner } from "./helper";
import { MintPyData } from "./types";

export async function mintPyFromSy() {
// Use 1 SY to mint PT and YT with 1% slippage
const res = await callSDK<MintPyData>(`/v1/sdk/${CHAIN_ID}/mint`, {
receiver: RECEIVER_ADDRESS,
yt: YT_ADDRESS,
slippage: 0.01,
tokenIn: SY_ADDRESS,
amountIn: '1000000000000000000',
});

console.log('Amount PT & YT Out: ', res.data.amountOut);
console.log('Price impact: ', res.data.priceImpact);

// Send tx
getSigner().sendTransaction(res.tx);
}

export async function mintPyFromToken() {
// Use 1 wstETH to mint PT and YT with 1% slippage
const res = await callSDK<MintPyData>(`/v1/sdk/${CHAIN_ID}/mint`, {
receiver: RECEIVER_ADDRESS,
yt: YT_ADDRESS,
slippage: 0.01,
tokenIn: wstETH,
amountIn: '1000000000000000000',
});

console.log('Amount PT & YT Out: ', res.data.amountOut);
console.log('Price impact: ', res.data.priceImpact);

// Send tx
getSigner().sendTransaction(res.tx);
}
37 changes: 37 additions & 0 deletions hosted-sdk-demo/src/redeem-py.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { CHAIN_ID, RECEIVER_ADDRESS, SY_ADDRESS, wstETH, YT_ADDRESS } from "./constants";
import { callSDK, getSigner } from "./helper";
import { RedeemPyData } from "./types";

export async function redeemPyToSy() {
// Redeem 1 PT and 1 YT to SY with 1% slippage
const res = await callSDK<RedeemPyData>(`/v1/sdk/${CHAIN_ID}/redeem`, {
receiver: RECEIVER_ADDRESS,
slippage: 0.01,
yt: YT_ADDRESS,
amountIn: '1000000000000000000',
tokenOut: SY_ADDRESS,
});

console.log('Amount SY Out: ', res.data.amountOut);
console.log('Price impact: ', res.data.priceImpact);

// Send tx
getSigner().sendTransaction(res.tx);
}

export async function redeemPyToToken() {
// Redeem 1 PT and 1 YT to wstETH with 1% slippage
const res = await callSDK<RedeemPyData>(`/v1/sdk/${CHAIN_ID}/redeem`, {
receiver: RECEIVER_ADDRESS,
slippage: 0.01,
yt: YT_ADDRESS,
amountIn: '1000000000000000000',
tokenOut: wstETH,
});

console.log('Amount wstETH Out: ', res.data.amountOut);
console.log('Price impact: ', res.data.priceImpact);

// Send tx
getSigner().sendTransaction(res.tx);
}
37 changes: 37 additions & 0 deletions hosted-sdk-demo/src/remove-liquidity-dual.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { CHAIN_ID, MARKET_ADDRESS, RECEIVER_ADDRESS, SY_ADDRESS, wstETH } from "./constants";
import { callSDK, getSigner } from "./helper";
import { RemoveLiquidityDualData } from "./types";

export async function removeLiquidityDualSyAndPt() {
// Remove 1 LP from wstETH pool to SY and PT with 1% slippage
const res = await callSDK<RemoveLiquidityDualData>(`/v1/sdk/${CHAIN_ID}/markets/${MARKET_ADDRESS}/remove-liquidity-dual`, {
receiver: RECEIVER_ADDRESS,
slippage: 0.01,
tokenOut: SY_ADDRESS,
amountIn: '1000000000000000000',
});

console.log('Amount SY Out: ', res.data.amountTokenOut);
console.log('Amount PT Out: ', res.data.amountPtOut);
console.log('Price impact: ', res.data.priceImpact);

// Send tx
getSigner().sendTransaction(res.tx);
}

export async function removeLiquidityDualTokenAndPt() {
// Remove 1 LP from wstETH pool to wstETH and PT with 1% slippage
const res = await callSDK<RemoveLiquidityDualData>(`/v1/sdk/${CHAIN_ID}/markets/${MARKET_ADDRESS}/remove-liquidity-dual`, {
receiver: RECEIVER_ADDRESS,
slippage: 0.01,
tokenOut: wstETH,
amountIn: '1000000000000000000',
});

console.log('Amount wstETH Out: ', res.data.amountTokenOut);
console.log('Amount PT Out: ', res.data.amountPtOut);
console.log('Price impact: ', res.data.priceImpact);

// Send tx
getSigner().sendTransaction(res.tx);
}
51 changes: 51 additions & 0 deletions hosted-sdk-demo/src/remove-liquidity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { CHAIN_ID, MARKET_ADDRESS, PT_ADDRESS, RECEIVER_ADDRESS, SY_ADDRESS, wstETH } from "./constants";
import { callSDK, getSigner } from "./helper";
import { RemoveLiquidityData } from "./types";

export async function removeLiquiditySinglePt() {
// Remove 1 LP from wstETH pool to PT with 1% slippage
const res = await callSDK<RemoveLiquidityData>(`/v1/sdk/${CHAIN_ID}/markets/${MARKET_ADDRESS}/remove-liquidity`, {
receiver: RECEIVER_ADDRESS,
slippage: 0.01,
tokenOut: PT_ADDRESS,
amountIn: '1000000000000000000'
});

console.log('Amount PT Out: ', res.data.amountOut);
console.log('Price impact: ', res.data.priceImpact);

// Send tx
getSigner().sendTransaction(res.tx);
}

export async function removeLiquiditySingleSy() {
// Remove 1 LP from wstETH pool to SY with 1% slippage
const res = await callSDK<RemoveLiquidityData>(`/v1/sdk/${CHAIN_ID}/markets/${MARKET_ADDRESS}/remove-liquidity`, {
receiver: RECEIVER_ADDRESS,
slippage: 0.01,
tokenOut: SY_ADDRESS,
amountIn: '1000000000000000000'
});

console.log('Amount SY Out: ', res.data.amountOut);
console.log('Price impact: ', res.data.priceImpact);

// Send tx
getSigner().sendTransaction(res.tx);
}

export async function removeLiquiditySingleToken() {
// Remove 1 LP from wstETH pool to wstETH with 1% slippage
const res = await callSDK<RemoveLiquidityData>(`/v1/sdk/${CHAIN_ID}/markets/${MARKET_ADDRESS}/remove-liquidity`, {
receiver: RECEIVER_ADDRESS,
slippage: 0.01,
tokenOut: wstETH,
amountIn: '1000000000000000000'
});

console.log('Amount wstETH Out: ', res.data.amountOut);
console.log('Price impact: ', res.data.priceImpact);

// Send tx
getSigner().sendTransaction(res.tx);
}
21 changes: 21 additions & 0 deletions hosted-sdk-demo/src/roll-over-pt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { CHAIN_ID, MARKET_ADDRESS, RECEIVER_ADDRESS } from "./constants";
import { callSDK, getSigner } from "./helper";
import { RollOverPtData } from "./types";

const eETH_MARKET_ADDRESS = '0xe1f19cbda26b6418b0c8e1ee978a533184496066';

export async function rollOverPt() {
// Transfer 1 PT eETH to wstETH PT with 1% slippage
const res = await callSDK<RollOverPtData>(`/v1/sdk/${CHAIN_ID}/markets/${eETH_MARKET_ADDRESS}/roll-over-pt`, {
receiver: RECEIVER_ADDRESS,
slippage: 0.01,
dstMarket: MARKET_ADDRESS,
ptAmount: '1000000000000000000',
});

console.log('Amount PT Out: ', res.data.amountPtOut);
console.log('Price impact: ', res.data.priceImpact);

// Send tx
getSigner().sendTransaction(res.tx);
}
Loading

0 comments on commit e5f2f27

Please sign in to comment.