Skip to content

Commit

Permalink
convert backend-api-demo to core service
Browse files Browse the repository at this point in the history
  • Loading branch information
Manh Cao authored and Manh Cao committed Sep 5, 2024
1 parent e5f2f27 commit 2181e8b
Show file tree
Hide file tree
Showing 9 changed files with 139 additions and 110 deletions.
12 changes: 11 additions & 1 deletion backend-api-demo/package-lock.json

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

3 changes: 2 additions & 1 deletion backend-api-demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"author": "Manh Cao Duy",
"license": "ISC",
"dependencies": {
"axios": "^1.7.3"
"axios": "^1.7.3",
"csv-string": "^4.1.1"
}
}
2 changes: 1 addition & 1 deletion backend-api-demo/src/const.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const EXTERNAL_DOMAIN = `https://api-v2.pendle.finance/api/external`;
export const CORE_DOMAIN = `https://api-v2.pendle.finance/api/core`;
49 changes: 29 additions & 20 deletions backend-api-demo/src/get-asset-prices.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { EXTERNAL_DOMAIN } from "./const";
import { CORE_DOMAIN } from "./const";
import axios from "axios";
import { parse } from "csv-string";

interface GetSpotPriceParam {
chainId: number;
Expand All @@ -10,7 +11,9 @@ interface GetSpotPriceQuery {
}

interface GetSpotPriceResponse {
prices: Record<string, number>;
total: number;
addresses: string[];
pricesUsd: (number | null)[];
}

interface GetHistoricalPricesParam {
Expand All @@ -34,11 +37,11 @@ interface HistoricalPriceData {

interface GetHistoricalPricesResponse {
total: number;
limit: number;
currency: string;
timestampStart: string;
timestampEnd: string;
data: HistoricalPriceData;
timeFrame: string;
timestamp_start: number;
timestamp_end: number;
results: string;
}

export async function getAssetPrices() {
Expand All @@ -48,14 +51,17 @@ export async function getAssetPrices() {
chainId: 1, // Ethereum
};

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

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

const {total, pricesUsd, addresses} = data;

const { data } = await axios.get<GetSpotPriceResponse>(EXTERNAL_DOMAIN + targetPath);
console.log('result info', {total});

const { prices } = data;
const address = addresses[0];

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

export async function getHistoricalAssetPrices() {
Expand All @@ -72,16 +78,19 @@ export async function getHistoricalAssetPrices() {
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 targetPath = `/v4/${param.chainId}/prices/${param.address}/ohlcv`;
const { data: response } = await axios.get<GetHistoricalPricesResponse>(CORE_DOMAIN + targetPath, { params: query });
console.log('response data', {total: response.total, currency: response.currency, timestamp_start: response.timestamp_start, timestamp_end: response.timestamp_end });

const {results} = response;

const data = parse(results, {output: 'objects'})

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],
timestamp: data[0].time,
high: data[0].open,
low: data[0].low,
open: data[0].open,
close: data[0].close,
})
}
41 changes: 32 additions & 9 deletions backend-api-demo/src/get-list-assets.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,60 @@
import axios from "axios";
import { EXTERNAL_DOMAIN } from "./const";
import { CORE_DOMAIN } from "./const";

interface Param {
chainId: number;
}

interface Query {
order_by?: string;
skip?: number;
limit?: number;
is_expired?: boolean;
zappable?: boolean;
type?: string;
address?: string;
q?: string;
}

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

interface Response {
assets: AssetInfo[];
results: AssetInfo[];
total: number;
limit: number;
skip: number;
}

export async function getAssetList() {
export async function getAssets() {
// 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 query: Query = {
order_by: 'name:1',
skip: 0,
limit: 10,
is_expired: false,
}

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

const { data } = await axios.get<Response>(CORE_DOMAIN + targetPath, {params: query});

const { data } = await axios.get<Response>(EXTERNAL_DOMAIN + targetPath);
const {total, limit, skip, results: assets} = data;

const { assets } = data;
console.log('result info', {limit, total, skip});

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

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

interface Param {
chainId: number;
}

interface Query {
order_by?: string;
skip?: number;
limit?: number;
is_expired?: boolean;
select?: string;
pt?: string;
yt?: string;
sy?: string;
q?: string;
is_active?: boolean;
categoryId?: string;
}

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

interface Response {
markets: MarketInfo[];
total: number;
limit: number;
skip: number;
results: MarketInfo[];
}

export async function getActiveMarkets() {
export async function getMarkets() {
// 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 query: Query = {
order_by: 'name:1',
skip: 0,
limit: 10,
is_expired: false,
select: 'pro',
}

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

const { data } = await axios.get<Response>(CORE_DOMAIN + targetPath, {params: query});

const { data } = await axios.get<Response>(EXTERNAL_DOMAIN + targetPath);
const { results: markets, skip, limit, total } = data;

const { markets } = data;
console.log('result info', {limit, total, skip});

const {name, address, expiry, pt, sy, yt} = markets[0];
const {id: ptId} = pt;
const {id: syId} = sy;
const {id: ytId} = yt;

console.log('first inactive market', {name, address, expiry, pt, sy, yt});
console.log('first active market', {name, address, expiry, ptId, syId, ytId});
}
29 changes: 16 additions & 13 deletions backend-api-demo/src/get-market-historical-data.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { EXTERNAL_DOMAIN } from "./const";
import { CORE_DOMAIN } from "./const";
import axios from "axios";
import { parse } from "csv-string";

interface Param {
chainId: number;
Expand All @@ -20,11 +21,9 @@ interface MarketHistoricalData {

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

export async function getMarketHistoricalData() {
Expand All @@ -41,15 +40,19 @@ export async function getMarketHistoricalData() {
timestampEnd: new Date("2024-08-11T00:00:00.000+00:00"),
}

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

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 } = await axios.get<Response>(CORE_DOMAIN + targetPath, {params: query});

console.log('response data', {total: response.total, timestamp_start: response.timestamp_start, timestamp_end: response.timestamp_end });

const {results} = response;

const data = parse(results, {output: 'objects'})

const {data} = response;
console.log('first data point info', {
timestamp: data.timestamps[0],
underlyingApy: data.underlyingApys[0],
impliedApy: data.impliedApys[0],
timestamp: data[0].timestamp,
underlyingApy: data[0].underlyingApy,
impliedApy: data[0].impliedApy,
})
}
32 changes: 0 additions & 32 deletions backend-api-demo/src/get-swapping-prices.ts

This file was deleted.

Loading

0 comments on commit 2181e8b

Please sign in to comment.