-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from pendle-finance/manh/init-backend-api-demo
init backend api demo
- Loading branch information
Showing
12 changed files
with
484 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,4 +33,6 @@ coverage.json | |
__pycache__/ | ||
|
||
.npmrc | ||
.editorconfig | ||
.editorconfig | ||
|
||
*/.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/api/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). |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const EXTERNAL_DOMAIN = `https://api-v2.pendle.finance/api/external`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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], | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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], | ||
}) | ||
} |
Oops, something went wrong.