Skip to content
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';

/** @type {import('sequelize-cli').Migration} */
module.exports = {
async up(queryInterface) {
await queryInterface.sequelize.query(
'CREATE INDEX transactions_result_gin_idx ON public."Transactions" USING gin (result);',
);
},

async down(queryInterface) {
await queryInterface.sequelize.query('DROP INDEX IF EXISTS transactions_result_gin_idx;');
},
};
12 changes: 12 additions & 0 deletions indexer/src/kadena-server/config/graphql-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,8 @@ export type Query = {
graphConfiguration: GraphConfiguration;
/** Get the height of the block with the highest height. */
lastBlockHeight?: Maybe<Scalars['BigInt']['output']>;
/** Get last price for a specific token in KDA */
lastTokenPriceInKda?: Maybe<Scalars['Decimal']['output']>;
/** Get user's liquidity positions */
liquidityPositions: LiquidityPositionsConnection;
/** Get information about the network. */
Expand Down Expand Up @@ -904,6 +906,10 @@ export type QueryGasLimitEstimateArgs = {
input: Array<Scalars['String']['input']>;
};

export type QueryLastTokenPriceInKdaArgs = {
moduleName: Scalars['String']['input'];
};

export type QueryLiquidityPositionsArgs = {
after?: InputMaybe<Scalars['String']['input']>;
before?: InputMaybe<Scalars['String']['input']>;
Expand Down Expand Up @@ -3054,6 +3060,12 @@ export type QueryResolvers<
>;
graphConfiguration?: Resolver<ResolversTypes['GraphConfiguration'], ParentType, ContextType>;
lastBlockHeight?: Resolver<Maybe<ResolversTypes['BigInt']>, ParentType, ContextType>;
lastTokenPriceInKda?: Resolver<
Maybe<ResolversTypes['Decimal']>,
ParentType,
ContextType,
RequireFields<QueryLastTokenPriceInKdaArgs, 'moduleName'>
>;
liquidityPositions?: Resolver<
ResolversTypes['LiquidityPositionsConnection'],
ParentType,
Expand Down
5 changes: 5 additions & 0 deletions indexer/src/kadena-server/config/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,11 @@ type Query {
protocolAddress: String
): DexMetrics! @complexity(value: 1)

"""
Get last price for a specific token in KDA
"""
lastTokenPriceInKda(moduleName: String!): Decimal

"""
Get price for a specific token
"""
Expand Down
2 changes: 2 additions & 0 deletions indexer/src/kadena-server/resolvers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ import { transfersNonFungibleChainAccountResolver } from '@/kadena-server/resolv
import { totalCountNonFungibleChainAccountTransfersConnectionResolver } from '@/kadena-server/resolvers/fields/non-fungible-chain-account/transfers-connection/total-count-non-fungible-chain-account-transfers-connection-resolver';
import { totalCountQueryBlocksFromHeightConnectionResolver } from '@/kadena-server/resolvers/fields/query-blocks-from-height-connection/total-count-query-blocks-from-height-connection-resolver';
import { transactionsByPactCodeQueryResolver } from '@/kadena-server/resolvers/query/transactions-by-pact-code-query-resolver';
import { lastTokenPriceInKdaQueryResolver } from '@/kadena-server/resolvers/query/last-token-price-in-kda-resolver';
/**
* Complete resolver map for the GraphQL API
*
Expand Down Expand Up @@ -157,6 +158,7 @@ export const resolvers: Resolvers<ResolverContext> = {
transactionsByPublicKey: transactionsByPublicKeyQueryResolver,
transfers: transfersQueryResolver,
tokens: tokensQueryResolver,
lastTokenPriceInKda: lastTokenPriceInKdaQueryResolver,
pool: poolQueryResolver,
poolTransactions: poolTransactionsQueryResolver,
liquidityPositions: liquidityPositionsQueryResolver,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { rootPgPool } from '@/config/database';
import { ResolverContext } from '@/kadena-server/config/apollo-server-config';
import { QueryLastTokenPriceInKdaArgs, QueryResolvers } from '@/kadena-server/config/graphql-types';
import Decimal from 'decimal.js';

export const lastTokenPriceInKdaQueryResolver: QueryResolvers<ResolverContext>['lastTokenPriceInKda'] =
async (_parent: unknown, args: QueryLastTokenPriceInKdaArgs) => {
const query = `
SELECT t.result
FROM public."Transactions" t
WHERE t.result::text LIKE '%' || $1 || '%'
ORDER BY t.creationtime DESC, t.id DESC
LIMIT 1
`;

const res: any = await rootPgPool.query(query, [args.moduleName]);

const data = res.rows?.[0]?.result?.data;
if (!data?.[0]?.token || !data?.[1]?.token) {
return null;
}

const isFirstElementTheKdaToken = data?.[0]?.token === 'coin';
const firstValue =
data?.[0]?.amount?.decimal ?? data?.[0]?.amount?.integer ?? data?.[0]?.amount;
const secondValue =
data?.[1]?.amount?.decimal ?? data?.[1]?.amount?.integer ?? data?.[1]?.amount;

const kadenaValue = isFirstElementTheKdaToken ? firstValue : secondValue;
const tokenValue = isFirstElementTheKdaToken ? secondValue : firstValue;
const amount = new Decimal(kadenaValue).div(new Decimal(tokenValue)).toFixed(12);
return amount;
};