Skip to content

Commit

Permalink
Bind swagger docs and typescript types
Browse files Browse the repository at this point in the history
  • Loading branch information
prevostc committed Jun 21, 2024
1 parent ca72b14 commit 4f28ce7
Show file tree
Hide file tree
Showing 9 changed files with 312 additions and 251 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ http://localhost:4000/api/v1/vault/arbitrum/0x4c32b8d26e6ab2ce401772514c999768f6
https://clm-api.beefy.finance/api/v1/vault/arbitrum/0x4c32b8d26e6ab2ce401772514c999768f63afb4e/harvests
http://localhost:4000/api/v1/vault/arbitrum/0x4c32b8d26e6ab2ce401772514c999768f63afb4e/harvests

https://clm-api.beefy.finance/api/v1/vault/arbitrum/0x4c32b8d26e6ab2ce401772514c999768f63afb4e/investors
http://localhost:4000/api/v1/vault/arbitrum/0x4c32b8d26e6ab2ce401772514c999768f63afb4e/investors

https://clm-api.beefy.finance/api/v1/vaults/arbitrum/1d
http://localhost:4000/api/v1/vaults/arbitrum/1d

Expand Down
21 changes: 13 additions & 8 deletions src/config/chains.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import { type Static, Type } from '@sinclair/typebox';

export const chainIdSchema = Type.Union([
Type.Literal('arbitrum'),
Type.Literal('base'),
Type.Literal('optimism'),
Type.Literal('moonbeam'),
Type.Literal('linea'),
Type.Literal('polygon'),
]);
export const chainIdSchema = Type.Union(
[
Type.Literal('arbitrum'),
Type.Literal('base'),
Type.Literal('optimism'),
Type.Literal('moonbeam'),
Type.Literal('linea'),
Type.Literal('polygon'),
],
{
description: 'Chain ID',
}
);
export type ChainId = Static<typeof chainIdSchema>;

export const allChainIds: Array<ChainId> = [
Expand Down
110 changes: 55 additions & 55 deletions src/routes/v1/investor.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { type Static, Type } from '@sinclair/typebox';
import type { FastifyInstance, FastifyPluginOptions, FastifySchema } from 'fastify';
import S from 'fluent-json-schema';
import { addressSchema } from '../../schema/address';
import { addressSchemaTypebox } from '../../schema/address';
import { getAsyncCache } from '../../utils/async-lock';

import type { Address } from '../../utils/scalar-types';
import type { Address, Hex } from '../../utils/scalar-types';
import { getClmTimeline } from '../../utils/timeline';
import type { TimelineClmInteraction } from '../../utils/timeline-types';

Expand All @@ -16,22 +15,16 @@ export default async function (

// timeline endpoint
{
type UrlParams = {
investor_address: Address;
};

const urlParamsSchema = S.object().prop(
'investor_address',
addressSchema.required().description('The investor address')
);

const responseSchema = S.array().items(S.object());
const urlParamsSchema = Type.Object({
investor_address: addressSchemaTypebox,
});
type UrlParams = Static<typeof urlParamsSchema>;

const schema: FastifySchema = {
tags: ['investor'],
params: urlParamsSchema,
response: {
200: responseSchema,
200: Type.Array(timelineClmInteractionOutputSchema),
},
};

Expand All @@ -44,7 +37,7 @@ export default async function (
`timeline:${investor_address.toLowerCase()}`,
2 * 60 * 1000,
async () => {
return await getTimeline(investor_address);
return await getTimeline(investor_address as Hex);
}
);
reply.send(res);
Expand All @@ -55,47 +48,54 @@ export default async function (
done();
}

type ClmInteractionLegacy = {
datetime: string;
product_key: string;
display_name: string;
chain: string;
is_eol: false;
is_dashboard_eol: false;
transaction_hash: string;
const clmInteractionLegacySchema = Type.Object({
datetime: Type.String(),
product_key: Type.String(),
display_name: Type.String(),
chain: Type.String(),
is_eol: Type.Boolean(),
is_dashboard_eol: Type.Boolean(),
transaction_hash: Type.String(),

/** called shares for legacy reasons, this is now the total between manager and reward pool */
share_balance: string;
share_diff: string;

token0_to_usd: string;
underlying0_balance: string;
underlying0_diff: string;

token1_to_usd: string;
underlying1_balance: string;
underlying1_diff: string;

usd_balance: string;
usd_diff: string;
};

type ClmInteractionRewardPool = {
reward_pool_address: string;
reward_pool_balance: string;
reward_pool_diff: string;
};

type ClmInteractionBase = ClmInteractionLegacy & {
manager_address: string;
manager_balance: string;
manager_diff: string;
actions: string[];
};

type TimelineClmInteractionOutput =
| ClmInteractionBase
| (ClmInteractionBase & ClmInteractionRewardPool);
share_balance: Type.String(),
share_diff: Type.String(),

token0_to_usd: Type.String(),
underlying0_balance: Type.String(),
underlying0_diff: Type.String(),

token1_to_usd: Type.String(),
underlying1_balance: Type.String(),
underlying1_diff: Type.String(),

usd_balance: Type.String(),
usd_diff: Type.String(),
});

const clmInteractionRewardPoolSchema = Type.Object({
reward_pool_address: Type.String(),
reward_pool_balance: Type.String(),
reward_pool_diff: Type.String(),
});
type ClmInteractionRewardPool = Static<typeof clmInteractionRewardPoolSchema>;

const clmInteractionManagerSchema = Type.Object({
manager_address: Type.String(),
manager_balance: Type.String(),
manager_diff: Type.String(),
actions: Type.Array(Type.String()),
});

const clmInteractionBaseSchema = Type.Intersect([
clmInteractionLegacySchema,
clmInteractionManagerSchema,
]);
const timelineClmInteractionOutputSchema = Type.Union([
clmInteractionBaseSchema,
Type.Intersect([clmInteractionBaseSchema, clmInteractionRewardPoolSchema]),
]);
type TimelineClmInteractionOutput = Static<typeof timelineClmInteractionOutputSchema>;

function clmInteractionToOutput(interaction: TimelineClmInteraction): TimelineClmInteractionOutput {
const { rewardPoolToken, rewardPool } = interaction;
Expand Down
Loading

0 comments on commit 4f28ce7

Please sign in to comment.