-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(yield): return liquidity pools info (#99)
- Loading branch information
Showing
18 changed files
with
356 additions
and
4 deletions.
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
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,32 @@ | ||
import { | ||
Column, | ||
Entity, | ||
PrimaryColumn | ||
} from 'typeorm'; | ||
import { bufferToString, stringToBuffer } from '@cowprotocol/shared'; | ||
|
||
@Entity({ name: 'cow_amm_competitor_info', schema: 'public' }) | ||
export class PoolInfo { | ||
@PrimaryColumn('bytea', { | ||
transformer: { from: bufferToString, to: stringToBuffer }, | ||
}) | ||
contract_address: string; | ||
|
||
@Column('int') | ||
chain_id: number; | ||
|
||
@Column('varchar') | ||
project: string; | ||
|
||
@Column('double precision') | ||
apr: number; | ||
|
||
@Column('double precision') | ||
fee: number; | ||
|
||
@Column('double precision') | ||
tvl: number; | ||
|
||
@Column('double precision') | ||
volume: number; | ||
} |
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
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,42 @@ | ||
import 'reflect-metadata'; | ||
import { FastifyInstance } from 'fastify'; | ||
import typeORMPlugin from 'typeorm-fastify-plugin'; | ||
import fp from 'fastify-plugin'; | ||
import { PoolInfo } from '../data/poolInfo'; | ||
|
||
export default fp(async function (fastify: FastifyInstance) { | ||
const dbParams = { | ||
host: fastify.config.COW_ANALYTICS_DATABASE_HOST, | ||
port: Number(fastify.config.COW_ANALYTICS_DATABASE_PORT), | ||
database: fastify.config.COW_ANALYTICS_DATABASE_NAME, | ||
username: fastify.config.COW_ANALYTICS_DATABASE_USERNAME, | ||
password: fastify.config.COW_ANALYTICS_DATABASE_PASSWORD, | ||
} | ||
|
||
const dbParamsAreInvalid = Object.values(dbParams).some((v) => Number.isNaN(v) || v === undefined); | ||
|
||
if (dbParamsAreInvalid) { | ||
console.error('Invalid CoW Analytics database parameters, please check COW_ANALYTICS_* env vars'); | ||
return | ||
} | ||
|
||
fastify.register(typeORMPlugin, { | ||
...dbParams, | ||
type: 'postgres', | ||
entities: [PoolInfo], | ||
ssl: true, | ||
extra: { | ||
ssl: { | ||
rejectUnauthorized: false | ||
} | ||
} | ||
}); | ||
|
||
fastify.ready((err) => { | ||
if (err) { | ||
throw err; | ||
} | ||
|
||
fastify.orm.runMigrations({ transaction: 'all' }); | ||
}); | ||
}); |
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,4 @@ | ||
import ms from 'ms'; | ||
|
||
export const POOLS_RESULT_LIMIT = 500 | ||
export const POOLS_QUERY_CACHE = ms('12h') |
65 changes: 65 additions & 0 deletions
65
apps/api/src/app/routes/__chainId/yield/getPoolsAverageApr.ts
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,65 @@ | ||
import { FastifyPluginAsync } from 'fastify'; | ||
import { FromSchema } from 'json-schema-to-ts'; | ||
import { PoolInfo } from '../../../data/poolInfo'; | ||
import { | ||
errorSchema, | ||
paramsSchema, | ||
poolsAverageAprBodySchema | ||
} from './schemas'; | ||
import { trimDoubleQuotes } from './utils'; | ||
import { CACHE_CONTROL_HEADER, getCacheControlHeaderValue } from '../../../../utils/cache'; | ||
|
||
type RouteSchema = FromSchema<typeof paramsSchema>; | ||
type SuccessSchema = FromSchema<typeof poolsAverageAprBodySchema>; | ||
type ErrorSchema = FromSchema<typeof errorSchema>; | ||
|
||
interface PoolInfoResult { | ||
project: string; | ||
average_apr: number; | ||
} | ||
|
||
const CACHE_SECONDS = 21600; // 6 hours | ||
|
||
const root: FastifyPluginAsync = async (fastify): Promise<void> => { | ||
fastify.get<{ | ||
Params: RouteSchema; | ||
Reply: SuccessSchema | ErrorSchema; | ||
}>( | ||
'/pools-average-apr', | ||
{ | ||
schema: { | ||
params: paramsSchema, | ||
response: { | ||
'2XX': poolsAverageAprBodySchema, | ||
'400': errorSchema, | ||
}, | ||
}, | ||
}, | ||
async function (request, reply) { | ||
const { chainId } = request.params; | ||
|
||
const poolInfoRepository = fastify.orm.getRepository(PoolInfo); | ||
|
||
const result = await poolInfoRepository.query(` | ||
SELECT project, | ||
AVG(apr) AS average_apr | ||
FROM cow_amm_competitor_info | ||
WHERE chain_id = ${chainId} | ||
GROUP BY project; | ||
`) | ||
|
||
const averageApr = result.reduce((acc: Record<string, number>, val: PoolInfoResult) => { | ||
const projectName = trimDoubleQuotes(val.project) | ||
|
||
acc[projectName] = +val.average_apr.toFixed(6) | ||
|
||
return acc | ||
}, {}) | ||
|
||
reply.header(CACHE_CONTROL_HEADER, getCacheControlHeaderValue(CACHE_SECONDS)); | ||
reply.status(200).send(averageApr); | ||
} | ||
); | ||
}; | ||
|
||
export default root; |
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,58 @@ | ||
import { FastifyPluginAsync } from 'fastify'; | ||
import { FromSchema } from 'json-schema-to-ts'; | ||
import { PoolInfo } from '../../../data/poolInfo'; | ||
import { In } from 'typeorm'; | ||
import { poolsInfoBodySchema, errorSchema, paramsSchema, poolsInfoSuccessSchema } from './schemas'; | ||
import { POOLS_QUERY_CACHE, POOLS_RESULT_LIMIT } from './const'; | ||
import { trimDoubleQuotes } from './utils'; | ||
|
||
type RouteSchema = FromSchema<typeof paramsSchema>; | ||
type SuccessSchema = FromSchema<typeof poolsInfoSuccessSchema>; | ||
type ErrorSchema = FromSchema<typeof errorSchema>; | ||
type BodySchema = FromSchema<typeof poolsInfoBodySchema>; | ||
|
||
const root: FastifyPluginAsync = async (fastify): Promise<void> => { | ||
fastify.post<{ | ||
Params: RouteSchema; | ||
Reply: SuccessSchema | ErrorSchema; | ||
Body: BodySchema; | ||
}>( | ||
'/pools', | ||
{ | ||
schema: { | ||
params: paramsSchema, | ||
response: { | ||
'2XX': poolsInfoSuccessSchema, | ||
'400': errorSchema, | ||
}, | ||
body: poolsInfoBodySchema | ||
}, | ||
}, | ||
async function (request, reply) { | ||
const { chainId } = request.params; | ||
const poolsAddresses = request.body; | ||
|
||
const poolInfoRepository = fastify.orm.getRepository(PoolInfo); | ||
|
||
const results = await poolInfoRepository.find({ | ||
take: POOLS_RESULT_LIMIT, | ||
where: { | ||
...(poolsAddresses.length > 0 ? { contract_address: In(poolsAddresses) } : null), | ||
chain_id: chainId | ||
}, | ||
cache: POOLS_QUERY_CACHE | ||
}) | ||
|
||
const mappedResults = results.map(res => { | ||
return { | ||
...res, | ||
project: trimDoubleQuotes(res.project) | ||
} | ||
}) | ||
|
||
reply.status(200).send(mappedResults); | ||
} | ||
); | ||
}; | ||
|
||
export default root; |
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,92 @@ | ||
import { AddressSchema, ChainIdSchema } from '../../../schemas'; | ||
import { JSONSchema } from 'json-schema-to-ts'; | ||
import { POOLS_RESULT_LIMIT } from './const'; | ||
|
||
export const paramsSchema = { | ||
type: 'object', | ||
required: ['chainId'], | ||
additionalProperties: false, | ||
properties: { | ||
chainId: ChainIdSchema, | ||
}, | ||
} as const satisfies JSONSchema; | ||
|
||
export const poolsInfoSuccessSchema = { | ||
type: 'array', | ||
items: { | ||
type: 'object', | ||
required: [ | ||
'contract_address', | ||
'chain_id', | ||
'project', | ||
'apr', | ||
'fee', | ||
'tvl', | ||
'volume' | ||
], | ||
additionalProperties: false, | ||
properties: { | ||
contract_address: { | ||
title: 'Pool address', | ||
type: 'string', | ||
pattern: AddressSchema.pattern | ||
}, | ||
chain_id: ChainIdSchema, | ||
project: { | ||
title: 'Liquidity provider', | ||
type: 'string', | ||
}, | ||
apr: { | ||
title: 'APR', | ||
description: 'Annual Percentage Rate', | ||
type: 'number', | ||
}, | ||
fee: { | ||
title: 'Fee tier', | ||
description: 'Pool fee percent', | ||
type: 'number', | ||
}, | ||
tvl: { | ||
title: 'TVL', | ||
description: 'Total value locked (in USD)', | ||
type: 'number', | ||
}, | ||
volume: { | ||
title: 'Volume 24h', | ||
description: 'Trading volume in the last 24 hours (in USD)', | ||
type: 'number', | ||
}, | ||
}, | ||
}, | ||
} as const satisfies JSONSchema; | ||
|
||
export const poolsInfoBodySchema = { | ||
type: 'array', | ||
items: { | ||
title: 'Pool address', | ||
description: 'Blockchain address of the pool', | ||
type: 'string', | ||
pattern: AddressSchema.pattern, | ||
}, | ||
maxItems: POOLS_RESULT_LIMIT | ||
} as const satisfies JSONSchema; | ||
|
||
export const poolsAverageAprBodySchema = { | ||
type: 'object', | ||
title: 'Liquidity provider - apr', | ||
additionalProperties: true | ||
} as const satisfies JSONSchema; | ||
|
||
|
||
export const errorSchema = { | ||
type: 'object', | ||
required: ['message'], | ||
additionalProperties: false, | ||
properties: { | ||
message: { | ||
title: 'Message', | ||
description: 'Message describing the error.', | ||
type: 'string', | ||
}, | ||
}, | ||
} as const satisfies JSONSchema; |
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,6 @@ | ||
export interface PoolInfo { | ||
apy: number | ||
tvl: number | ||
feeTier: number | ||
volume24h: number | ||
} |
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,11 @@ | ||
export function trimDoubleQuotes(value: string): string { | ||
if (value[0] === '"') { | ||
return trimDoubleQuotes(value.slice(1)) | ||
} | ||
|
||
if (value[value.length - 1] === '"') { | ||
return trimDoubleQuotes(value.slice(0, -1)) | ||
} | ||
|
||
return value | ||
} |
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,16 @@ | ||
import * as dotenv from 'dotenv'; | ||
import { DataSource } from 'typeorm'; | ||
|
||
dotenv.config(); | ||
|
||
export const cowAnalyticsDb = new DataSource({ | ||
type: 'postgres', | ||
host: process.env.COW_ANALYTICS_DATABASE_HOST, | ||
port: Number(process.env.COW_ANALYTICS_DATABASE_PORT), | ||
username: process.env.COW_ANALYTICS_DATABASE_USERNAME, | ||
password: process.env.COW_ANALYTICS_DATABASE_PASSWORD, | ||
database: process.env.COW_ANALYTICS_DATABASE_NAME, | ||
entities: ['src/app/data/*.ts'], | ||
}); | ||
|
||
cowAnalyticsDb.initialize(); |
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
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 |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
} | ||
], | ||
"compilerOptions": { | ||
"strictPropertyInitialization": false, | ||
"esModuleInterop": true | ||
} | ||
} |
Oops, something went wrong.