Skip to content

Commit

Permalink
init backend api demo
Browse files Browse the repository at this point in the history
  • Loading branch information
Manh Cao authored and Manh Cao committed Aug 16, 2024
1 parent d470e7f commit 9678b2e
Show file tree
Hide file tree
Showing 12 changed files with 484 additions and 1 deletion.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,6 @@ coverage.json
__pycache__/

.npmrc
.editorconfig
.editorconfig

*/.idea
22 changes: 22 additions & 0 deletions backend-api-demo/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Pendle Limit Order API Demo

This repository is designed for developers seeking to interact with Pendle Backend using TypeScript.

The demo project demonstrates how to
- Get list of pendle markets and assets
- Get asset's price (spot prices, historical prices)
- Get Swapping prices
- Get market historical info

## Getting Started
To run examples:
- Change directory to the demo project folder: `cd limit-order-api-demo`
- Install required dependencies with `npm install`
- Execute the index.ts file using `npm run start`

## Api specifications
* [Documentation](https://api-v2.pendle.finance/external/docs#/)

## Additional Notes

Please be aware that the code examples provided in this repository are for educational purposes and are not intended for production use. They serve as a starting point for integrating Pendle Backend functionality into your decentralized application (dApp).
97 changes: 97 additions & 0 deletions backend-api-demo/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions backend-api-demo/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "backend-api-demo",
"version": "1.0.0",
"description": "Pendle Backend API Demo",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "npx ts-node ./src/index.ts"
},
"author": "Manh Cao Duy",
"license": "ISC",
"dependencies": {
"axios": "^1.7.3"
}
}
1 change: 1 addition & 0 deletions backend-api-demo/src/const.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const EXTERNAL_DOMAIN = `https://api-v2.pendle.finance/api/external`;
87 changes: 87 additions & 0 deletions backend-api-demo/src/get-asset-prices.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { EXTERNAL_DOMAIN } from "./const";
import axios from "axios";

interface GetSpotPriceParam {
chainId: number;
}

interface GetSpotPriceQuery {
address?: string[];
}

interface GetSpotPriceResponse {
prices: Record<string, number>;
}

interface GetHistoricalPricesParam {
chainId: number;
address: string;
}

interface GetHistoricalPricesQuery {
timeFrame?: "hour" | "day" | "week";
timestampStart?: Date;
timestampEnd?: Date;
}

interface HistoricalPriceData {
timestamps: number[];
highs: number[];
lows: number[];
opens: number[];
closes: number[];
}

interface GetHistoricalPricesResponse {
total: number;
limit: number;
currency: string;
timestampStart: string;
timestampEnd: string;
data: HistoricalPriceData;
}

export async function getAssetPrices() {
// This is an example of how to get all spot prices of assets on Ethereum

const param: GetSpotPriceParam = {
chainId: 1, // Ethereum
};

const targetPath = `/v1/${param.chainId}/assets/prices`;

const { data } = await axios.get<GetSpotPriceResponse>(EXTERNAL_DOMAIN + targetPath);

const { prices } = data;

const key = Object.keys(prices)[0];
console.log(`prices of ${key} is ${prices[key]} USD`);
}

export async function getHistoricalAssetPrices() {
// This is an example of how to get daily historical prices from 2024 Aug 5th to Aug 11st of PT-USDO++ on Ethereum

const param: GetHistoricalPricesParam = {
chainId: 1, // Ethereum
address: '0x270d664d2fc7d962012a787aec8661ca83df24eb' // PT-USD0++ 31OCT 2024
};

const query: GetHistoricalPricesQuery = {
timeFrame: "day",
timestampStart: new Date("2024-08-05T00:00:00.000+00:00"),
timestampEnd: new Date("2024-08-11T00:00:00.000+00:00"),
};

const targetPath = `/v1/${param.chainId}/assets/${param.address}/historical-prices`;
const { data: response } = await axios.get<GetHistoricalPricesResponse>(EXTERNAL_DOMAIN + targetPath, { params: query });
console.log('response data', {total: response.total, limit: response.limit, currency: response.currency, timestampStart: response.timestampStart, timestampEnd: response.timestampEnd});

const {data} = response;
console.log('first data point info', {
timestamp: data.timestamps[0],
high: data.highs[0],
low: data.lows[0],
open: data.opens[0],
close: data.closes[0],
})
}
37 changes: 37 additions & 0 deletions backend-api-demo/src/get-list-assets.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import axios from "axios";
import { EXTERNAL_DOMAIN } from "./const";

interface Param {
chainId: number;
}

interface AssetInfo {
name: string;
decimals: number;
address: string;
symbol: string;
tags: string[];
expiry: string;
}

interface Response {
assets: AssetInfo[];
}

export async function getAssetList() {
// This is an example of how to get list of Pendle assets on Ethereum

const param: Param = {
chainId: 1, // Ethereum
}

const targetPath = `/v1/${param.chainId}/assets/all`;

const { data } = await axios.get<Response>(EXTERNAL_DOMAIN + targetPath);

const { assets } = data;

const {name, address, decimals, expiry, symbol, tags} = assets[0];

console.log('first asset', {name, address, decimals, expiry, symbol, tags});
}
55 changes: 55 additions & 0 deletions backend-api-demo/src/get-list-markets.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import axios from "axios";
import { EXTERNAL_DOMAIN } from "./const";

interface Param {
chainId: number;
}

interface MarketInfo {
name: string;
address: string;
expiry: string;
pt: string;
yt: string;
sy: string;
}

interface Response {
markets: MarketInfo[];
}

export async function getActiveMarkets() {
// This is an example of how to get list of active Pendle markets on Ethereum

const param: Param = {
chainId: 1, // Ethereum
}

const targetPath = `/v1/${param.chainId}/markets/active`;

const { data } = await axios.get<Response>(EXTERNAL_DOMAIN + targetPath);

const { markets } = data;

const {name, address, expiry, pt, sy, yt} = markets[0];

console.log('first active market', {name, address, expiry, pt, sy, yt});
}

export async function getInactiveMarkets() {
// This is an example of how to get list of inactive Pendle markets on Ethereum

const param: Param = {
chainId: 1, // Ethereum
}

const targetPath = `/v1/${param.chainId}/markets/inactive`;

const { data } = await axios.get<Response>(EXTERNAL_DOMAIN + targetPath);

const { markets } = data;

const {name, address, expiry, pt, sy, yt} = markets[0];

console.log('first inactive market', {name, address, expiry, pt, sy, yt});
}
55 changes: 55 additions & 0 deletions backend-api-demo/src/get-market-historical-data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { EXTERNAL_DOMAIN } from "./const";
import axios from "axios";

interface Param {
chainId: number;
address: string;
}

interface Query {
timeFrame?: "hour" | "day" | "week";
timestampStart?: Date;
timestampEnd?: Date;
}

interface MarketHistoricalData {
timestamps: number[];
underlyingApys: number[];
impliedApys: number[];
}

interface Response {
total: number;
limit: number;
currency: string;
timestampStart: string;
timestampEnd: string;
data: MarketHistoricalData;
}

export async function getMarketHistoricalData() {
// This is an example of how to get daily historical data from 2024 Aug 5th to Aug 11st of USDO++ market on Ethereum

const param: Param = {
chainId: 1, // Ethereum
address: '0x00b321d89a8c36b3929f20b7955080baed706d1b', // USDO++
}

const query: Query = {
timeFrame: 'day',
timestampStart: new Date("2024-08-05T00:00:00.000+00:00"),
timestampEnd: new Date("2024-08-11T00:00:00.000+00:00"),
}

const targetPath = `/v1/${param.chainId}/markets/${param.address}/historical-data`;

const { data: response } = await axios.get<Response>(EXTERNAL_DOMAIN + targetPath, {params: query});
console.log('response data', {total: response.total, limit: response.limit, timestampStart: response.timestampStart, timestampEnd: response.timestampEnd});

const {data} = response;
console.log('first data point info', {
timestamp: data.timestamps[0],
underlyingApy: data.underlyingApys[0],
impliedApy: data.impliedApys[0],
})
}
Loading

0 comments on commit 9678b2e

Please sign in to comment.