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
8 changes: 8 additions & 0 deletions .changeset/dull-suits-act.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
'@pubkey-cache/resolver': patch
'@pubkey-cache/core': patch
'@pubkey-cache/react': patch
'@pubkey-cache/server': patch
---

change resolvers to use handler for storing data
47 changes: 0 additions & 47 deletions examples/node-redis/src/commands/command-collection-holders.ts

This file was deleted.

25 changes: 20 additions & 5 deletions examples/node-redis/src/commands/command-resolver-sync-all.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,45 @@
import { resolve, ResolverType } from '@pubkey-cache/resolver'
import { ResolverType } from '@pubkey-cache/resolver'
import { createResolverContextHelius } from '@pubkey-cache/resolver/src'
import { createResolverContextHeliusInstance } from '@pubkey-cache/resolver/src/resolvers/helius/create-resolver-context-helius-instance'

import { getConfig } from '../lib/get-config'
import { getContext } from '../lib/get-context'
import { Command } from './command'
import { resolve } from './resolve'

export const commandResolverSyncAll: Command = {
action: async () => {
const { resolvers, verbose } = getConfig()
const { resolvers, verbose, heliusApiKey } = getConfig()
const context = getContext()
const startTime = new Date().getTime()
const results: string[] = []
const config = createResolverContextHelius({ heliusApiKey })
const instance = createResolverContextHeliusInstance(config)

try {
for (const resolver of resolvers) {
const startTimeResolver = new Date().getTime()

const items: unknown[] = []
if (
resolver.type === ResolverType['helius-collection-holders'] ||
resolver.type === ResolverType['helius-token-holders']
) {
await resolve({ context, resolver, verbose })
await resolve({
handler: (data) => {
console.log(`Handling data:`, data)
items.push(data)
return true
},
instance,
resolver,
})

const endTimeResolver = new Date().getTime()
const durationResolver = endTimeResolver - startTimeResolver

results.push(`Synced resolver ${resolver.id} took (${durationResolver / 1000} seconds)`)
results.push(
`Synced resolver ${resolver.id} took (${durationResolver / 1000} seconds) ${items.length} items resolved`,
)
await context.discordLog({
level: 'info',
message: `Synced resolver ${resolver.id} took (${durationResolver / 1000} seconds)`,
Expand Down
24 changes: 18 additions & 6 deletions examples/node-redis/src/commands/command-resolver-sync.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { resolve } from '@pubkey-cache/resolver'
import { createResolverContextHelius } from '@pubkey-cache/resolver/src'
import { createResolverContextHeliusInstance } from '@pubkey-cache/resolver/src/resolvers/helius/create-resolver-context-helius-instance'
import prompts from 'prompts'

import { getConfig } from '../lib/get-config'
import { getContext } from '../lib/get-context'
import { Command } from './command'
import { resolve } from './resolve'

export const commandResolverSync: Command = {
action: async () => {
const { resolvers, verbose } = getConfig()
const context = getContext()
const { resolvers, heliusApiKey } = getConfig()

try {
const { selected } = await prompts({
Expand All @@ -25,9 +25,21 @@ export const commandResolverSync: Command = {
return [new Error(`Resolver not found: ${selected}`), null]
}

await resolve({ context, resolver, verbose })
const config = createResolverContextHelius({ heliusApiKey })
const instance = createResolverContextHeliusInstance(config)

return [null, `Synced resolver ${resolver.id}`]
const items: unknown[] = []
const result = await resolve({
handler: (data) => {
console.log(`Handling data:`, data)
items.push(data)
return true
},
instance,
resolver,
})

return [null, JSON.stringify({ items, result }, null, 2)]
} catch (error) {
return [new Error(error as string), null]
}
Expand Down
44 changes: 0 additions & 44 deletions examples/node-redis/src/commands/command-token-holders.ts

This file was deleted.

4 changes: 0 additions & 4 deletions examples/node-redis/src/commands/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Command } from './command'
import { commandBalance } from './command-balance'
import { commandCollectionHolders } from './command-collection-holders'
import { commandDiscordLog } from './command-discord-log'
import { commandGenesisHash } from './command-genesis-hash'
import { commandHello } from './command-hello'
Expand All @@ -12,14 +11,11 @@ import { commandStorageGet } from './command-storage-get'
import { commandStorageKeys } from './command-storage-keys'
import { commandStorageRemove } from './command-storage-remove'
import { commandStorageSet } from './command-storage-set'
import { commandTokenHolders } from './command-token-holders'

export const commands: Record<string, Command> = {
'a-resolver-sync-all': commandResolverSyncAll,
'a-resolver-sync-one': commandResolverSync,
'a-resolvers': commandResolvers,
'b-collection-holders': commandCollectionHolders,
'b-token-holders': commandTokenHolders,
'storage-get': commandStorageGet,
'storage-keys': commandStorageKeys,
'storage-remove': commandStorageRemove,
Expand Down
29 changes: 29 additions & 0 deletions examples/node-redis/src/commands/resolve.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { resolveHeliusCollectionAssets, resolveHeliusTokenAccounts, Resolver } from '@pubkey-cache/resolver'
import { ResolverContextHeliusInstance } from '@pubkey-cache/resolver/src/resolvers/helius/types/resolver-context-helius-instance'

export async function resolve({
handler,
instance,
resolver,
}: {
handler: (data: unknown) => boolean
instance: ResolverContextHeliusInstance
resolver: Resolver
}) {
switch (resolver.type) {
case 'helius-collection-assets':
return await resolveHeliusCollectionAssets({
handler,
instance,
params: { collection: resolver.address },
})
case 'helius-token-accounts':
return await resolveHeliusTokenAccounts({
handler,
instance,
params: { mint: resolver.address },
})
default:
throw new Error(`Unknown resolver type: ${resolver.type}`)
}
}
4 changes: 3 additions & 1 deletion packages/resolver/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@
"maintained node versions"
],
"dependencies": {
"@solana/kit": "^2.1.0"
"@solana/kit": "^2.1.0",
"gill": "^0.9.0",
"zod": "^3.24.3"
},
"devDependencies": {
"helius-sdk": "^1.4.2",
Expand Down
13 changes: 13 additions & 0 deletions packages/resolver/src/__setup__/create-mock-helius-instance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { createSolanaClient } from 'gill';
import { Helius } from 'helius-sdk';

import { createResolverContextHelius } from '../resolvers/helius/create-resolver-context-helius';
import { ResolverContextHeliusInstance } from '../resolvers/helius/types/resolver-context-helius-instance';

export function createMockHeliusInstance(helius: unknown): ResolverContextHeliusInstance {
return {
context: createResolverContextHelius({ heliusApiKey: '00000000-0000-0000-0000-000000000000' }),
helius: helius as Helius,
solanaClient: createSolanaClient({ urlOrMoniker: 'localnet' }),
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Helius } from 'helius-sdk';

import { createResolverContextHelius } from '../resolvers/helius/create-resolver-context-helius';
import { createResolverContextHeliusInstance } from '../resolvers/helius/create-resolver-context-helius-instance';
import { type ResolverContextHelius } from '../resolvers/helius/types/resolver-context-helius';

describe('create-resolver-context-helius-instance', () => {
const heliusApiKey = '00000000-0000-0000-0000-000000000000';

describe('expected usage', () => {
it('should create an instance with minimal config', () => {
expect.assertions(3);
// ARRANGE
const context: ResolverContextHelius = createResolverContextHelius({
heliusApiKey,
});
// ACT
const instance = createResolverContextHeliusInstance(context);
// ASSERT
expect(instance.context).toEqual(context);
expect(instance.helius).toBeInstanceOf(Helius);
expect(Object.keys(instance)).toEqual(['context', 'helius', 'solanaClient']);
});
});
});
Loading
Loading