Skip to content

Commit b36b52a

Browse files
committed
feat: created a query to get the token price based on its latest swap
1 parent ade6289 commit b36b52a

File tree

5 files changed

+66
-0
lines changed

5 files changed

+66
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'use strict';
2+
3+
/** @type {import('sequelize-cli').Migration} */
4+
module.exports = {
5+
async up(queryInterface) {
6+
await queryInterface.sequelize.query(
7+
'CREATE INDEX transactions_result_gin_idx ON public."Transactions" USING gin (result);',
8+
);
9+
},
10+
11+
async down(queryInterface) {
12+
await queryInterface.sequelize.query('DROP INDEX IF EXISTS transactions_result_gin_idx;');
13+
},
14+
};

indexer/src/kadena-server/config/graphql-types.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -770,6 +770,8 @@ export type Query = {
770770
graphConfiguration: GraphConfiguration;
771771
/** Get the height of the block with the highest height. */
772772
lastBlockHeight?: Maybe<Scalars['BigInt']['output']>;
773+
/** Get last price for a specific token in KDA */
774+
lastTokenPriceInKda?: Maybe<Scalars['Decimal']['output']>;
773775
/** Get user's liquidity positions */
774776
liquidityPositions: LiquidityPositionsConnection;
775777
/** Get information about the network. */
@@ -904,6 +906,10 @@ export type QueryGasLimitEstimateArgs = {
904906
input: Array<Scalars['String']['input']>;
905907
};
906908

909+
export type QueryLastTokenPriceInKdaArgs = {
910+
moduleName: Scalars['String']['input'];
911+
};
912+
907913
export type QueryLiquidityPositionsArgs = {
908914
after?: InputMaybe<Scalars['String']['input']>;
909915
before?: InputMaybe<Scalars['String']['input']>;
@@ -3054,6 +3060,12 @@ export type QueryResolvers<
30543060
>;
30553061
graphConfiguration?: Resolver<ResolversTypes['GraphConfiguration'], ParentType, ContextType>;
30563062
lastBlockHeight?: Resolver<Maybe<ResolversTypes['BigInt']>, ParentType, ContextType>;
3063+
lastTokenPriceInKda?: Resolver<
3064+
Maybe<ResolversTypes['Decimal']>,
3065+
ParentType,
3066+
ContextType,
3067+
RequireFields<QueryLastTokenPriceInKdaArgs, 'moduleName'>
3068+
>;
30573069
liquidityPositions?: Resolver<
30583070
ResolversTypes['LiquidityPositionsConnection'],
30593071
ParentType,

indexer/src/kadena-server/config/schema.graphql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,11 @@ type Query {
431431
protocolAddress: String
432432
): DexMetrics! @complexity(value: 1)
433433

434+
"""
435+
Get last price for a specific token in KDA
436+
"""
437+
lastTokenPriceInKda(moduleName: String!): Decimal
438+
434439
"""
435440
Get price for a specific token
436441
"""

indexer/src/kadena-server/resolvers/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ import { transfersNonFungibleChainAccountResolver } from '@/kadena-server/resolv
104104
import { totalCountNonFungibleChainAccountTransfersConnectionResolver } from '@/kadena-server/resolvers/fields/non-fungible-chain-account/transfers-connection/total-count-non-fungible-chain-account-transfers-connection-resolver';
105105
import { totalCountQueryBlocksFromHeightConnectionResolver } from '@/kadena-server/resolvers/fields/query-blocks-from-height-connection/total-count-query-blocks-from-height-connection-resolver';
106106
import { transactionsByPactCodeQueryResolver } from '@/kadena-server/resolvers/query/transactions-by-pact-code-query-resolver';
107+
import { lastTokenPriceInKdaQueryResolver } from '@/kadena-server/resolvers/query/last-token-price-in-kda-resolver';
107108
/**
108109
* Complete resolver map for the GraphQL API
109110
*
@@ -157,6 +158,7 @@ export const resolvers: Resolvers<ResolverContext> = {
157158
transactionsByPublicKey: transactionsByPublicKeyQueryResolver,
158159
transfers: transfersQueryResolver,
159160
tokens: tokensQueryResolver,
161+
lastTokenPriceInKda: lastTokenPriceInKdaQueryResolver,
160162
pool: poolQueryResolver,
161163
poolTransactions: poolTransactionsQueryResolver,
162164
liquidityPositions: liquidityPositionsQueryResolver,
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { rootPgPool } from '@/config/database';
2+
import { ResolverContext } from '@/kadena-server/config/apollo-server-config';
3+
import { QueryLastTokenPriceInKdaArgs, QueryResolvers } from '@/kadena-server/config/graphql-types';
4+
import Decimal from 'decimal.js';
5+
6+
export const lastTokenPriceInKdaQueryResolver: QueryResolvers<ResolverContext>['lastTokenPriceInKda'] =
7+
async (_parent: unknown, args: QueryLastTokenPriceInKdaArgs) => {
8+
const query = `
9+
SELECT t.result
10+
FROM public."Transactions" t
11+
WHERE t.result::text LIKE '%' || $1 || '%'
12+
ORDER BY t.creationtime DESC, t.id DESC
13+
LIMIT 1
14+
`;
15+
16+
const res: any = await rootPgPool.query(query, [args.moduleName]);
17+
18+
const data = res.rows?.[0]?.result?.data;
19+
if (!data?.[0]?.token || !data?.[1]?.token) {
20+
return null;
21+
}
22+
23+
const isFirstElementTheKdaToken = data?.[0]?.token === 'coin';
24+
const firstValue =
25+
data?.[0]?.amount?.decimal ?? data?.[0]?.amount?.integer ?? data?.[0]?.amount;
26+
const secondValue =
27+
data?.[1]?.amount?.decimal ?? data?.[1]?.amount?.integer ?? data?.[1]?.amount;
28+
29+
const kadenaValue = isFirstElementTheKdaToken ? firstValue : secondValue;
30+
const tokenValue = isFirstElementTheKdaToken ? secondValue : firstValue;
31+
const amount = new Decimal(kadenaValue).div(new Decimal(tokenValue)).toPrecision(12);
32+
return amount;
33+
};

0 commit comments

Comments
 (0)