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
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ COPY --from=builder /app/src/config/global-bundle.pem ./dist/config/global-bundl
COPY --from=builder /app/src/kadena-server/config/schema.graphql ./dist/kadena-server/config/schema.graphql
EXPOSE 3001

CMD ["node", "dist/index.js", "--graphqlServer"]
CMD ["node", "dist/index.js", "--graphql"]
3 changes: 2 additions & 1 deletion indexer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
"dev:streaming": "ts-node src/index.ts --streaming",
"dev:old-graphql": "ts-node src/index.ts --oldGraphql",
"dev:graphql": "nodemon src/index.ts --graphql",
"dev:guards": "nodemon src/index.ts --guards",
"prod:start": "docker-compose up --build indexer && docker-compose logs -f indexer",
"prod:streaming": "node dist/index.js --streaming",
"prod:backfill": "node dist/index.js --backfill",
Expand All @@ -75,4 +76,4 @@
"migrate:up": "dotenv -e .env npx sequelize-cli db:migrate",
"migrate:down": "dotenv -e .env npx sequelize-cli db:migrate:undo"
}
}
}
16 changes: 8 additions & 8 deletions indexer/src/cache/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const MEMORY_CACHE = new NodeCache({ stdTTL: 0 });

const CACHE_TTL = 1000 * 60 * 5; // 5 minutes

export default function initCache(context: ResolverContext) {
export default async function initCache(context: ResolverContext) {
const { blockRepository, networkRepository } = context;

async function getHashRateAndTotalDifficulty() {
Expand All @@ -30,7 +30,7 @@ export default function initCache(context: ResolverContext) {
};
MEMORY_CACHE.set(HASH_RATE_AND_TOTAL_DIFFICULTY_KEY, newValue);
} catch (err) {
console.log("Error getting hash rate and total difficulty");
console.log("Error getting hash rate and total difficulty", err);
}
}

Expand All @@ -39,7 +39,7 @@ export default function initCache(context: ResolverContext) {
const networkStatistics = await networkRepository.getNetworkStatistics();
MEMORY_CACHE.set(NETWORK_STATISTICS_KEY, networkStatistics);
} catch (err) {
console.log("Error getting network statistics");
console.log("Error getting network statistics", err);
}
}

Expand All @@ -48,18 +48,18 @@ export default function initCache(context: ResolverContext) {
const nodeInfo = await networkRepository.getNodeInfo();
MEMORY_CACHE.set(NODE_INFO_KEY, nodeInfo);
} catch (err) {
console.log("Error getting node info");
console.log("Error getting node info", err);
}
}

MEMORY_CACHE.set(HASH_RATE_AND_TOTAL_DIFFICULTY_KEY, {
totalDifficulty: -1,
});

const getAllInfo = () => {
getHashRateAndTotalDifficulty();
getNetworkStatistics();
getNodeInfo();
const getAllInfo = async () => {
await getNetworkStatistics();
await getNodeInfo();
await getHashRateAndTotalDifficulty();
};

getAllInfo();
Expand Down
5 changes: 4 additions & 1 deletion indexer/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ import { usePostgraphile } from "./server/metrics";
import { useKadenaGraphqlServer } from "./kadena-server/server";
import { closeDatabase } from "./config/database";
import { initializeDatabase } from "./config/init";
import { startGuardsBackfill } from "./services/sync/guards";

program
.option("-s, --streaming", "Start streaming blockchain data")
.option("-g, --oldGraphql", "Start GraphQL server based on Postgraphile")
.option("-t, --graphql", "Start GraphQL server based on kadena schema")
.option("-f, --guards", "Backfill the guards")
.option("-z, --database", "Init the database");

program.parse(process.argv);
Expand All @@ -32,8 +34,9 @@ async function main() {
}

if (options.streaming) {
await initializeDatabase();
await startStreaming();
} else if (options.guards) {
await startGuardsBackfill();
} else if (options.oldGraphql) {
await usePostgraphile();
} else if (options.graphql) {
Expand Down
19 changes: 1 addition & 18 deletions indexer/src/jobs/publisher-job.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import zod from "zod";
import { getRequiredEnvString } from "../utils/helpers";

const eventsToPublish: Array<DispatchInfo> = [];

const KADENA_GRAPHQL_URL = getRequiredEnvString("KADENA_GRAPHQL_API_URL");
const KADENA_GRAPHQL_PORT = getRequiredEnvString("KADENA_GRAPHQL_API_PORT");

const DISPATCH_INTERVAL = 1000; // 1 second

export const dispatchInfoSchema = zod.object({
hash: zod.string(),
chainId: zod.string(),
Expand All @@ -18,20 +14,7 @@ export const dispatchInfoSchema = zod.object({

export type DispatchInfo = zod.infer<typeof dispatchInfoSchema>;

export function startPublisher() {
setInterval(() => {
const [first] = eventsToPublish.splice(0, 1);
if (first) {
dispatch(first);
}
}, DISPATCH_INTERVAL);
}

export const addPublishEvents = (events: DispatchInfo[]) => {
// eventsToPublish.push(...events);
};

const dispatch = async (dispatchInfo: DispatchInfo) => {
export const dispatch = async (dispatchInfo: DispatchInfo) => {
try {
const url = `${KADENA_GRAPHQL_URL}:${KADENA_GRAPHQL_PORT}/new-block`;
const response = await fetch(url, {
Expand Down
25 changes: 22 additions & 3 deletions indexer/src/kadena-server/config/graphql-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ export type FungibleChainAccount = Node & {
balance: Scalars['Float']['output'];
chainId: Scalars['String']['output'];
fungibleName: Scalars['String']['output'];
guard: Guard;
id: Scalars['ID']['output'];
transactions: FungibleChainAccountTransactionsConnection;
transfers: FungibleChainAccountTransfersConnection;
Expand Down Expand Up @@ -250,6 +251,13 @@ export type GraphConfiguration = {
minimumBlockHeight?: Maybe<Scalars['BigInt']['output']>;
};

export type Guard = {
__typename?: 'Guard';
keys: Array<Scalars['String']['output']>;
predicate: Scalars['String']['output'];
raw: Scalars['String']['output'];
};

/** Information about the network. */
export type NetworkInfo = {
__typename?: 'NetworkInfo';
Expand Down Expand Up @@ -922,7 +930,7 @@ export type ResolversUnionTypes<_RefType extends Record<string, unknown>> = {

/** Mapping of interface types */
export type ResolversInterfaceTypes<_RefType extends Record<string, unknown>> = {
Node: ( Omit<Block, 'events' | 'minerAccount' | 'parent' | 'transactions'> & { events: _RefType['BlockEventsConnection'], minerAccount: _RefType['FungibleChainAccount'], parent?: Maybe<_RefType['Block']>, transactions: _RefType['BlockTransactionsConnection'] } ) | ( Omit<Event, 'block' | 'transaction'> & { block: _RefType['Block'], transaction?: Maybe<_RefType['Transaction']> } ) | ( Omit<FungibleAccount, 'chainAccounts' | 'transactions' | 'transfers'> & { chainAccounts: Array<_RefType['FungibleChainAccount']>, transactions: _RefType['FungibleAccountTransactionsConnection'], transfers: _RefType['FungibleAccountTransfersConnection'] } ) | ( Omit<FungibleChainAccount, 'transactions' | 'transfers'> & { transactions: _RefType['FungibleChainAccountTransactionsConnection'], transfers: _RefType['FungibleChainAccountTransfersConnection'] } ) | ( Omit<NonFungibleAccount, 'transactions'> & { transactions: _RefType['NonFungibleAccountTransactionsConnection'] } ) | ( Omit<NonFungibleChainAccount, 'transactions'> & { transactions: _RefType['NonFungibleChainAccountTransactionsConnection'] } ) | ( NonFungibleTokenBalance ) | ( Signer ) | ( Omit<Transaction, 'cmd' | 'orphanedTransactions' | 'result'> & { cmd: _RefType['TransactionCommand'], orphanedTransactions?: Maybe<Array<Maybe<_RefType['Transaction']>>>, result: _RefType['TransactionInfo'] } ) | ( Omit<Transfer, 'block' | 'crossChainTransfer' | 'transaction'> & { block: _RefType['Block'], crossChainTransfer?: Maybe<_RefType['Transfer']>, transaction?: Maybe<_RefType['Transaction']> } );
Node: ( Omit<Block, 'events' | 'minerAccount' | 'parent' | 'transactions'> & { events: _RefType['BlockEventsConnection'], minerAccount: _RefType['FungibleChainAccount'], parent?: Maybe<_RefType['Block']>, transactions: _RefType['BlockTransactionsConnection'] } ) | ( Omit<Event, 'block' | 'transaction'> & { block: _RefType['Block'], transaction?: Maybe<_RefType['Transaction']> } ) | ( Omit<FungibleAccount, 'chainAccounts' | 'transactions' | 'transfers'> & { chainAccounts: Array<_RefType['FungibleChainAccount']>, transactions: _RefType['FungibleAccountTransactionsConnection'], transfers: _RefType['FungibleAccountTransfersConnection'] } ) | ( Omit<FungibleChainAccount, 'guard' | 'transactions' | 'transfers'> & { guard: _RefType['Guard'], transactions: _RefType['FungibleChainAccountTransactionsConnection'], transfers: _RefType['FungibleChainAccountTransfersConnection'] } ) | ( Omit<NonFungibleAccount, 'transactions'> & { transactions: _RefType['NonFungibleAccountTransactionsConnection'] } ) | ( Omit<NonFungibleChainAccount, 'transactions'> & { transactions: _RefType['NonFungibleChainAccountTransactionsConnection'] } ) | ( NonFungibleTokenBalance ) | ( Signer ) | ( Omit<Transaction, 'cmd' | 'orphanedTransactions' | 'result'> & { cmd: _RefType['TransactionCommand'], orphanedTransactions?: Maybe<Array<Maybe<_RefType['Transaction']>>>, result: _RefType['TransactionInfo'] } ) | ( Omit<Transfer, 'block' | 'crossChainTransfer' | 'transaction'> & { block: _RefType['Block'], crossChainTransfer?: Maybe<_RefType['Transfer']>, transaction?: Maybe<_RefType['Transaction']> } );
};

/** Mapping between all available schema types and the resolvers types */
Expand All @@ -946,14 +954,15 @@ export type ResolversTypes = {
FungibleAccountTransactionsConnectionEdge: ResolverTypeWrapper<Omit<FungibleAccountTransactionsConnectionEdge, 'node'> & { node: ResolversTypes['Transaction'] }>;
FungibleAccountTransfersConnection: ResolverTypeWrapper<Omit<FungibleAccountTransfersConnection, 'edges'> & { edges: Array<ResolversTypes['FungibleAccountTransfersConnectionEdge']> }>;
FungibleAccountTransfersConnectionEdge: ResolverTypeWrapper<Omit<FungibleAccountTransfersConnectionEdge, 'node'> & { node: ResolversTypes['Transfer'] }>;
FungibleChainAccount: ResolverTypeWrapper<Omit<FungibleChainAccount, 'transactions' | 'transfers'> & { transactions: ResolversTypes['FungibleChainAccountTransactionsConnection'], transfers: ResolversTypes['FungibleChainAccountTransfersConnection'] }>;
FungibleChainAccount: ResolverTypeWrapper<Omit<FungibleChainAccount, 'guard' | 'transactions' | 'transfers'> & { guard: ResolversTypes['Guard'], transactions: ResolversTypes['FungibleChainAccountTransactionsConnection'], transfers: ResolversTypes['FungibleChainAccountTransfersConnection'] }>;
FungibleChainAccountTransactionsConnection: ResolverTypeWrapper<Omit<FungibleChainAccountTransactionsConnection, 'edges'> & { edges: Array<ResolversTypes['FungibleChainAccountTransactionsConnectionEdge']> }>;
FungibleChainAccountTransactionsConnectionEdge: ResolverTypeWrapper<Omit<FungibleChainAccountTransactionsConnectionEdge, 'node'> & { node: ResolversTypes['Transaction'] }>;
FungibleChainAccountTransfersConnection: ResolverTypeWrapper<Omit<FungibleChainAccountTransfersConnection, 'edges'> & { edges: Array<ResolversTypes['FungibleChainAccountTransfersConnectionEdge']> }>;
FungibleChainAccountTransfersConnectionEdge: ResolverTypeWrapper<Omit<FungibleChainAccountTransfersConnectionEdge, 'node'> & { node: ResolversTypes['Transfer'] }>;
GasLimitEstimation: ResolverTypeWrapper<GasLimitEstimation>;
GenesisHeight: ResolverTypeWrapper<GenesisHeight>;
GraphConfiguration: ResolverTypeWrapper<GraphConfiguration>;
Guard: ResolverTypeWrapper<Guard>;
ID: ResolverTypeWrapper<Scalars['ID']['output']>;
Int: ResolverTypeWrapper<Scalars['Int']['output']>;
NetworkInfo: ResolverTypeWrapper<NetworkInfo>;
Expand Down Expand Up @@ -1025,14 +1034,15 @@ export type ResolversParentTypes = {
FungibleAccountTransactionsConnectionEdge: Omit<FungibleAccountTransactionsConnectionEdge, 'node'> & { node: ResolversParentTypes['Transaction'] };
FungibleAccountTransfersConnection: Omit<FungibleAccountTransfersConnection, 'edges'> & { edges: Array<ResolversParentTypes['FungibleAccountTransfersConnectionEdge']> };
FungibleAccountTransfersConnectionEdge: Omit<FungibleAccountTransfersConnectionEdge, 'node'> & { node: ResolversParentTypes['Transfer'] };
FungibleChainAccount: Omit<FungibleChainAccount, 'transactions' | 'transfers'> & { transactions: ResolversParentTypes['FungibleChainAccountTransactionsConnection'], transfers: ResolversParentTypes['FungibleChainAccountTransfersConnection'] };
FungibleChainAccount: Omit<FungibleChainAccount, 'guard' | 'transactions' | 'transfers'> & { guard: ResolversParentTypes['Guard'], transactions: ResolversParentTypes['FungibleChainAccountTransactionsConnection'], transfers: ResolversParentTypes['FungibleChainAccountTransfersConnection'] };
FungibleChainAccountTransactionsConnection: Omit<FungibleChainAccountTransactionsConnection, 'edges'> & { edges: Array<ResolversParentTypes['FungibleChainAccountTransactionsConnectionEdge']> };
FungibleChainAccountTransactionsConnectionEdge: Omit<FungibleChainAccountTransactionsConnectionEdge, 'node'> & { node: ResolversParentTypes['Transaction'] };
FungibleChainAccountTransfersConnection: Omit<FungibleChainAccountTransfersConnection, 'edges'> & { edges: Array<ResolversParentTypes['FungibleChainAccountTransfersConnectionEdge']> };
FungibleChainAccountTransfersConnectionEdge: Omit<FungibleChainAccountTransfersConnectionEdge, 'node'> & { node: ResolversParentTypes['Transfer'] };
GasLimitEstimation: GasLimitEstimation;
GenesisHeight: GenesisHeight;
GraphConfiguration: GraphConfiguration;
Guard: Guard;
ID: Scalars['ID']['output'];
Int: Scalars['Int']['output'];
NetworkInfo: NetworkInfo;
Expand Down Expand Up @@ -1221,6 +1231,7 @@ export type FungibleChainAccountResolvers<ContextType = any, ParentType extends
balance?: Resolver<ResolversTypes['Float'], ParentType, ContextType>;
chainId?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
fungibleName?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
guard?: Resolver<ResolversTypes['Guard'], ParentType, ContextType>;
id?: Resolver<ResolversTypes['ID'], ParentType, ContextType>;
transactions?: Resolver<ResolversTypes['FungibleChainAccountTransactionsConnection'], ParentType, ContextType, RequireFields<FungibleChainAccountTransactionsArgs, 'first' | 'last'>>;
transfers?: Resolver<ResolversTypes['FungibleChainAccountTransfersConnection'], ParentType, ContextType, RequireFields<FungibleChainAccountTransfersArgs, 'first' | 'last'>>;
Expand Down Expand Up @@ -1273,6 +1284,13 @@ export type GraphConfigurationResolvers<ContextType = any, ParentType extends Re
__isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;
};

export type GuardResolvers<ContextType = any, ParentType extends ResolversParentTypes['Guard'] = ResolversParentTypes['Guard']> = {
keys?: Resolver<Array<ResolversTypes['String']>, ParentType, ContextType>;
predicate?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
raw?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
__isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;
};

export type NetworkInfoResolvers<ContextType = any, ParentType extends ResolversParentTypes['NetworkInfo'] = ResolversParentTypes['NetworkInfo']> = {
apiVersion?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
coinsInCirculation?: Resolver<ResolversTypes['Float'], ParentType, ContextType>;
Expand Down Expand Up @@ -1639,6 +1657,7 @@ export type Resolvers<ContextType = any> = {
GasLimitEstimation?: GasLimitEstimationResolvers<ContextType>;
GenesisHeight?: GenesisHeightResolvers<ContextType>;
GraphConfiguration?: GraphConfigurationResolvers<ContextType>;
Guard?: GuardResolvers<ContextType>;
NetworkInfo?: NetworkInfoResolvers<ContextType>;
Node?: NodeResolvers<ContextType>;
NonFungibleAccount?: NonFungibleAccountResolvers<ContextType>;
Expand Down
7 changes: 7 additions & 0 deletions indexer/src/kadena-server/config/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,7 @@ type FungibleChainAccount implements Node {
balance: Float!
chainId: String!
fungibleName: String!
guard: Guard!

transactions(
after: String
Expand Down Expand Up @@ -790,3 +791,9 @@ type NonFungibleChainAccountTransactionsConnectionEdge {
cursor: String!
node: Transaction!
}

type Guard {
keys: [String!]!
predicate: String!
raw: String!
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,27 @@ export default interface BalanceRepository {
chainId: string,
tokenId: string,
): Promise<INonFungibleTokenBalance | null>;

/** methods below use balance from node */
getAccountInfo_NODE(
accountName: string,
fungibleName?: string | null,
): Promise<FungibleAccountOutput>;

getChainsAccountInfo_NODE(
accountName: string,
fungibleName: string,
chainIds?: string[],
): Promise<FungibleChainAccountOutput[]>;

getAccountsByPublicKey_NODE(
publicKey: string,
fungibleName: string,
): Promise<FungibleAccountOutput[]>;

getChainAccountsByPublicKey_NODE(
publicKey: string,
fungibleName: string,
chainId: string,
): Promise<FungibleChainAccountOutput[]>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ export default interface BlockRepository {
pageInfo: PageInfo;
edges: ConnectionEdge<BlockOutput>[];
}>;
getMinerData(hash: string): Promise<FungibleChainAccountOutput>;
getMinerData(
hash: string,
chainId: string,
): Promise<FungibleChainAccountOutput>;

getLowestBlockHeight(): Promise<number>;

Expand Down
Loading
Loading