Skip to content

Commit 855abc4

Browse files
authored
Jt/app registry subgraph 4 (#4561)
### Description - fetch agentUri and store in new field agentData, a json column in subgraph postgres. ### Changes <!-- List the specific changes made in this PR, for example: - Added/modified feature X - Fixed bug in component Y - Refactored module Z - Updated documentation --> ### Checklist - [ ] Tests added where required - [ ] Documentation updated where applicable - [ ] Changes adhere to the repository's contribution guidelines
1 parent 3d3d283 commit 855abc4

File tree

5 files changed

+91
-12
lines changed

5 files changed

+91
-12
lines changed

packages/subgraph/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
},
2121
"dependencies": {
2222
"@towns-protocol/web3": "workspace:^",
23+
"axios": "^1.9.0",
2324
"hono": "^4.7.11",
2425
"ponder": "^0.13.14",
2526
"viem": "^2.29.3"

packages/subgraph/ponder.schema.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { onchainTable, onchainEnum, primaryKey, relations, index } from 'ponder'
2+
import { AgentData } from './src/agentData'
23

34
export const analyticsEventType = onchainEnum('analytics_event_type', [
45
'swap',
@@ -478,6 +479,7 @@ export const agentIdentity = onchainTable(
478479
app: t.hex().notNull(), // FK to apps.address
479480
agentId: t.bigint().notNull(),
480481
agentUri: t.text(),
482+
agentData: t.json().$type<AgentData>(),
481483
registeredAt: t.bigint().notNull(),
482484
registeredAtBlock: t.bigint().notNull(),
483485
updatedAt: t.bigint(),

packages/subgraph/src/agentData.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import axios from 'axios'
2+
3+
export type AgentData = {
4+
type?: string
5+
name?: string
6+
description?: string
7+
image?: string
8+
endpoints?: Array<{
9+
name?: string
10+
endpoint?: string
11+
version?: string
12+
}>
13+
registrations?: Array<{
14+
agentId?: number
15+
agentRegistry?: string
16+
}>
17+
supportedTrust?: string[]
18+
}
19+
20+
export async function fetchAgentData(
21+
uri: string,
22+
maxRetries: number = 3,
23+
retryDelayMs: number = 1000,
24+
): Promise<AgentData | null> {
25+
if (!uri.startsWith('https://')) return null
26+
27+
for (let attempt = 1; attempt <= maxRetries; attempt++) {
28+
try {
29+
const response = await axios.get<AgentData>(uri, { timeout: 5000 })
30+
if (response.status !== 200) {
31+
console.warn(`Warning: Received status ${response.status} from ${uri}`)
32+
}
33+
return response.data
34+
} catch (error) {
35+
if (attempt < maxRetries) {
36+
console.warn(
37+
`Fetch attempt ${attempt} failed for ${uri}, retrying in ${retryDelayMs}ms...`,
38+
)
39+
await new Promise((resolve) => setTimeout(resolve, retryDelayMs))
40+
} else {
41+
console.error(`All ${maxRetries} fetch attempts failed for ${uri}`)
42+
}
43+
}
44+
}
45+
46+
return null
47+
}

packages/subgraph/src/index.ts

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
handleRedelegation,
88
decodePermissions,
99
} from './utils'
10+
import { fetchAgentData } from './agentData'
1011

1112
const ETH_ADDRESS = '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee' as const
1213

@@ -925,6 +926,22 @@ ponder.on('AppRegistry:Registered', async ({ event, context }) => {
925926
})
926927
.onConflictDoNothing()
927928

929+
// Fetch and store agent data if URI is provided
930+
if (agentUri) {
931+
const agentData = await fetchAgentData(agentUri)
932+
if (agentData) {
933+
await context.db.sql
934+
.update(schema.agentIdentity)
935+
.set({ agentData: agentData })
936+
.where(
937+
and(
938+
eq(schema.agentIdentity.app, owner),
939+
eq(schema.agentIdentity.agentId, agentId),
940+
),
941+
)
942+
}
943+
}
944+
928945
// Initialize reputation summary
929946
await context.db
930947
.insert(schema.agentReputationSummary)
@@ -964,19 +981,30 @@ ponder.on('AppRegistry:UriUpdated', async ({ event, context }) => {
964981
return
965982
}
966983

967-
// Update the URI
968-
await context.db.sql
969-
.update(schema.agentIdentity)
970-
.set({
971-
agentUri: agentUri,
972-
updatedAt: blockTimestamp,
973-
})
974-
.where(
975-
and(
976-
eq(schema.agentIdentity.app, agent.app),
977-
eq(schema.agentIdentity.agentId, agentId),
978-
),
984+
// Fetch agent data from new URI with retries
985+
const agentData = await fetchAgentData(agentUri)
986+
987+
if (agentData !== null) {
988+
// Only update if fetch succeeds - keeps URI and data in sync
989+
await context.db.sql
990+
.update(schema.agentIdentity)
991+
.set({
992+
agentUri: agentUri,
993+
agentData: agentData,
994+
updatedAt: blockTimestamp,
995+
})
996+
.where(
997+
and(
998+
eq(schema.agentIdentity.app, agent.app),
999+
eq(schema.agentIdentity.agentId, agentId),
1000+
),
1001+
)
1002+
} else {
1003+
console.warn(
1004+
`Skipping URI update for agentId ${agentId}, fetch failed after retries. ` +
1005+
`Keeping existing URI: ${agent.agentUri}`,
9791006
)
1007+
}
9801008
} catch (error) {
9811009
console.error(
9821010
`Error processing AppRegistry:UriUpdated at blockNumber ${blockNumber}:`,

yarn.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10627,6 +10627,7 @@ __metadata:
1062710627
"@types/node": ^20.14.8
1062810628
"@typescript-eslint/eslint-plugin": ^8.29.0
1062910629
"@typescript-eslint/parser": ^8.29.0
10630+
axios: ^1.9.0
1063010631
eslint: ^8.57.1
1063110632
eslint-config-ponder: ^0.9.27
1063210633
eslint-import-resolver-typescript: ^4.3.2

0 commit comments

Comments
 (0)