Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: token additional fields #31

Merged
merged 6 commits into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@
"test:e2e": "dotenv -e .env.test -- jest --runInBand --config ./test/e2e/jest-e2e.json",
"db": "docker compose up",
"db:deploy": "docker compose up -d",
"db:remove": "docker compose down ",
"db:remove": "docker compose down -v",
"db:redeploy": "npm run db:remove; npm run db:deploy",
"db:stop": "docker compose stop ",
"db:push-schema": "npx prisma db push",
"postdb:deploy": "sleep 1.5; npm run db:push-schema",
"db:migrate": "npx prisma migrate deploy",
"postdb:deploy": "sleep 1.5; npm run db:migrate",
"pretest:e2e": "dotenv -e .env.test npm run db:push-schema",
"prisma:test-studio": "dotenv -e .env.test npx prisma studio",
"prisma:studio": "npx prisma studio"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
CREATE
OR REPLACE FUNCTION total_reserve (integer, interval) RETURNS numeric AS 'SELECT SUM(CASE
WHEN t.id = p.t0 THEN
(latest_liquidity_info."reserve0" / POW(10, t.decimals))
ELSE (latest_liquidity_info."reserve1" / POW(10, t.decimals)) END
)
FROM "Token" t
LEFT JOIN public."Pair" p on t.id = p.t0 OR t.id = p.t1
LEFT JOIN LATERAL (SELECT *
FROM "PairLiquidityInfoHistory"
WHERE p.id = "pairId"
AND "microBlockTime" <= extract(epoch from NOW() - $2) * 1000

ORDER BY "microBlockTime" DESC, "logIndex" DESC
LIMIT 1) latest_liquidity_info ON TRUE
WHERE $1 = t.id' LANGUAGE SQL IMMUTABLE RETURNS NULL ON NULL INPUT;

CREATE
OR REPLACE FUNCTION volume_usd (integer, interval) RETURNS numeric AS 'SELECT ROUND(SUM(CASE
WHEN t.id = p.t0 THEN
CASE
WHEN liquidity_history."token0AePrice" >= 0 AND
liquidity_history."eventType" = ''SwapTokens'' AND
liquidity_history."microBlockTime" >=
extract(epoch from NOW() - $2) * 1000
THEN
(ABS(liquidity_history."deltaReserve0") / POW(10, t.decimals)) *
liquidity_history."token0AePrice" *
liquidity_history."aeUsdPrice" END
ELSE CASE
WHEN liquidity_history."token1AePrice" >= 0 AND
liquidity_history."eventType" = ''SwapTokens'' AND
liquidity_history."microBlockTime" >=
extract(epoch from NOW() - $2) * 1000
THEN
(ABS(liquidity_history."deltaReserve1") / POW(10, t.decimals)) *
liquidity_history."token1AePrice" *
liquidity_history."aeUsdPrice" END END
)::numeric, 4)
FROM "Token" t
LEFT JOIN public."Pair" p on t.id = p.t0 OR t.id = p.t1
LEFT JOIN "PairLiquidityInfoHistory" liquidity_history ON p.id = liquidity_history."pairId"

WHERE $1 = t.id' LANGUAGE SQL IMMUTABLE RETURNS NULL ON NULL INPUT;

CREATE
OR REPLACE FUNCTION historic_price (integer, interval) RETURNS numeric AS 'SELECT SUM(CASE
WHEN t.id = p.t0 THEN (latest_liquidity_info."token0AePrice") *
(latest_liquidity_info."reserve0" / POW(10, t.decimals))
ELSE (latest_liquidity_info."token1AePrice") *
(latest_liquidity_info."reserve1" / POW(10, t.decimals)) END /
total_reserve(t.id, $2))
FROM "Token" t
LEFT JOIN public."Pair" p on t.id = p.t0 OR t.id = p.t1
LEFT JOIN LATERAL (SELECT *
FROM "PairLiquidityInfoHistory"
WHERE p.id = "pairId"
AND "microBlockTime" <= extract(epoch from NOW() - $2) * 1000
ORDER BY "microBlockTime" DESC, "logIndex" DESC
LIMIT 1) latest_liquidity_info ON TRUE
WHERE $1 = t.id' LANGUAGE SQL IMMUTABLE RETURNS NULL ON NULL INPUT;
114 changes: 85 additions & 29 deletions src/api/api.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ export const transactionPattern =
export const accountPattern =
'ak_([23456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz]){49,50}';
export const bigNumberPattern = '[1-9]+';
export const usdValuePattern = '[1-9]+(\.[1-9]{0,4})?';
export const usdValuePattern = '[1-9]+(.[1-9]{0,4})?';
export const aeValuePattern = '[1-9]+(.[1-9]{0,18})?';
export const percentPattern = '[1-9]+(.[1-9]+)?';
export const microBlockTimePattern = '[1-9]{13}';
export const microBlockHashPattern =
'mh_([23456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz]){49,50}';
Expand Down Expand Up @@ -140,41 +142,95 @@ export class TokenPairWithLiquidityInfo {
}

export class TokenWithUsd extends TokenWithListed {
@ApiProperty({
description: 'Price of the token in AE',
pattern: aeValuePattern,
})
priceAe: string;

@ApiProperty({
description: 'Price of the token in USD',
pattern: bigNumberPattern,
pattern: usdValuePattern,
})
priceUsd: string;

@ApiProperty({
description: 'Price change in percent',
example: {
day: '0',
week: '0',
},
description: 'Total locked value in AE',
pattern: aeValuePattern,
})
tvlAe: string;

@ApiProperty({
description: 'Total locked value in USD',
pattern: usdValuePattern,
})
tvlUsd: string;

@ApiProperty({
description: 'Total Reserve',
pattern: usdValuePattern,
})
priceChange: {
day: string;
week: string;
};
totalReserve: string;

@ApiProperty({
description: 'Fully diluted valuation in USD',
description: 'Number of pairs for this token',
pattern: bigNumberPattern,
})
fdvUsd: string;
pairs: number;

@ApiProperty({
description: 'Volume in USD',
example: {
day: '0',
week: '0',
},
})
volumeUsd: {
day: string;
week: string;
};
description: 'Volume for last day in USD',
thepiwo marked this conversation as resolved.
Show resolved Hide resolved
pattern: usdValuePattern,
})
volumeUsdDay: string;

@ApiProperty({
description: 'Volume for last week in USD',
pattern: usdValuePattern,
})
volumeUsdWeek: string;

@ApiProperty({
description: 'Volume for last month in USD',
pattern: usdValuePattern,
})
volumeUsdMonth: string;

@ApiProperty({
description: 'Volume for last year in USD',
pattern: usdValuePattern,
})
volumeUsdYear: string;

@ApiProperty({
description: 'Volume for all time in USD',
pattern: usdValuePattern,
})
volumeUsdAll: string;

@ApiProperty({
description: 'Price change for last day in percent',
pattern: percentPattern,
})
priceChangeDay: string;

@ApiProperty({
description: 'Price change for last week in percent',
pattern: percentPattern,
})
priceChangeWeek: string;

@ApiProperty({
description: 'Price change for last month in percent',
pattern: percentPattern,
})
priceChangeMonth: string;

@ApiProperty({
description: 'Price change for last year in percent',
pattern: percentPattern,
})
priceChangeYear: string;
}

export class PairWithUsd extends PairBase {
Expand All @@ -200,37 +256,37 @@ export class PairWithUsd extends PairBase {
description: 'Total Value Locked in USD',
pattern: usdValuePattern,
})
tvlUsd: number;
tvlUsd: string;

@ApiProperty({
description: 'Volume for last day in USD',
pattern: usdValuePattern,
})
volumeUsdDay: number;
volumeUsdDay: string;

@ApiProperty({
description: 'Volume for last week in USD',
pattern: usdValuePattern,
})
volumeUsdWeek: number;
volumeUsdWeek: string;

@ApiProperty({
description: 'Volume for last month in USD',
pattern: usdValuePattern,
})
volumeUsdMonth: number;
volumeUsdMonth: string;

@ApiProperty({
description: 'Volume for last year in USD',
pattern: usdValuePattern,
})
volumeUsdYear: number;
volumeUsdYear: string;

@ApiProperty({
description: 'Volume for all time in USD',
pattern: usdValuePattern,
})
volumeUsdAll: number;
volumeUsdAll: string;
}

class PairWithLiquidity extends PairBase {
Expand Down
15 changes: 1 addition & 14 deletions src/api/tokens/tokens.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,7 @@ export class TokensController {
})
@ApiResponse({ status: 200, type: [dto.TokenWithUsd] })
async getAllTokens(): Promise<dto.TokenWithUsd[]> {
return (await this.tokensService.getAllTokens()).map((token) => ({
...removeId(token),
priceUsd: '0', // TODO PIWO: fill me
priceChange: {
// in percent
day: '0', // TODO PIWO: fill me
week: '0', // TODO PIWO: fill me
},
fdvUsd: '0', // TODO PIWO: fill me
volumeUsd: {
day: '0', // TODO PIWO: fill me
week: '0', // TODO PIWO: fill me
},
}));
return this.tokensService.getAllTokensWithAggregation();
}

@Get('listed')
Expand Down
3 changes: 3 additions & 0 deletions src/api/tokens/tokens.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ export class TokensService {
async getAllTokens(): Promise<Token[]> {
return this.tokenDbService.getAll(presentInvalidTokens);
}
async getAllTokensWithAggregation() {
return this.tokenDbService.getAllWithAggregation(presentInvalidTokens);
}
async getListedTokens(): Promise<Token[]> {
return this.tokenDbService.getListed();
}
Expand Down
12 changes: 6 additions & 6 deletions src/database/pair/pair-db.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ export class PairDbService {
token1: string;
synchronized: boolean;
transactions: number;
tvlUsd: number;
volumeUsdDay: number;
volumeUsdWeek: number;
volumeUsdMonth: number;
volumeUsdYear: number;
volumeUsdAll: number;
tvlUsd: string;
volumeUsdDay: string;
volumeUsdWeek: string;
volumeUsdMonth: string;
volumeUsdYear: string;
volumeUsdAll: string;
}[]
>`
SELECT
Expand Down
Loading
Loading