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 committed Aug 12, 2024
1 parent d470e7f commit 75520ca
Show file tree
Hide file tree
Showing 11 changed files with 438 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
- 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`
- Configure the ethers.js signer in `/src/libs/signer.ts`
- 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/external`;
83 changes: 83 additions & 0 deletions backend-api-demo/src/get-asset-prices.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
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() {
const param: GetSpotPriceParam = {
chainId: 1,
};

const targetPath = `/v1/${param.chainId}/asset/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() {
const param: GetHistoricalPricesParam = {
chainId: 1,
address: '0x270d664d2fc7d962012a787aec8661ca83df24eb'
};

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}/asset/${param.address}/historical`;
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],
})
}
35 changes: 35 additions & 0 deletions backend-api-demo/src/get-list-assets.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
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() {
const param: Param = {
chainId: 1,
}

const targetPath = `/v1/${param.chainId}/asset/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});
}
51 changes: 51 additions & 0 deletions backend-api-demo/src/get-list-markets.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
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() {
const param: Param = {
chainId: 1,
}

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() {
const param: Param = {
chainId: 1,
}

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});
}
53 changes: 53 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,53 @@
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() {
const param: Param = {
chainId: 1,
address: '0x00b321d89a8c36b3929f20b7955080baed706d1b',
}

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],
})
}
15 changes: 15 additions & 0 deletions backend-api-demo/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { getAssetList } from "./get-list-assets";
import { getActiveMarkets, getInactiveMarkets } from "./get-list-markets";
import { getAssetPrices, getHistoricalAssetPrices } from "./get-asset-prices";
import { getMarketHistoricalData } from "./get-market-historical-data";

async function main() {
// await getAssetList();
// await getActiveMarkets();
// await getInactiveMarkets();
// await getAssetPrices();
// await getHistoricalAssetPrices();
await getMarketHistoricalData();
}

main()
Loading

0 comments on commit 75520ca

Please sign in to comment.