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
33 changes: 18 additions & 15 deletions app/routes/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,13 @@ settings.get("/link-status", async (c) => {
const address = getBaseAddress(wallet);
if (!address) return c.json({ linked: false, error: "No EVM address" });

// Check local cache first (survives RPC rate limits)
const cached = await db.setting.findUnique({ where: { key: "agent_id" } });
if (cached) {
return c.json({ linked: true, agentId: Number(cached.value), owsWallet: address });
}

// RPC: try agentIdByWallet (for bound wallets)
try {
const agentId = await publicClient.readContract({
address: ERC_8004,
Expand All @@ -170,21 +177,13 @@ settings.get("/link-status", async (c) => {
}) as bigint;

if (agentId > 0n) {
// Fetch NFT owner (ERC-721 ownerOf)
let owner: string | undefined;
try {
owner = await publicClient.readContract({
address: ERC_8004,
abi: [{ type: "function", name: "ownerOf", stateMutability: "view", inputs: [{ name: "tokenId", type: "uint256" }], outputs: [{ name: "", type: "address" }] }] as const,
functionName: "ownerOf",
args: [agentId],
}) as string;
} catch { /* best effort */ }
return c.json({ linked: true, agentId: Number(agentId), owsWallet: address, owner });
// Cache locally
await db.setting.upsert({ where: { key: "agent_id" }, update: { value: String(agentId) }, create: { key: "agent_id", value: String(agentId) } });
return c.json({ linked: true, agentId: Number(agentId), owsWallet: address });
}
} catch { /* agentIdByWallet may revert if not bound */ }

// Fallback: check if wallet owns an agent NFT (register() creates NFT but doesn't bind via setAgentWallet)
// RPC fallback: check balanceOf (for owned but unbound NFTs)
try {
const balance = await publicClient.readContract({
address: ERC_8004,
Expand All @@ -194,7 +193,7 @@ settings.get("/link-status", async (c) => {
}) as bigint;

if (balance > 0n) {
// Try to get token ID (ERC-721 Enumerable — may not be supported)
// Try to get token ID
let agentId: number | undefined;
try {
const tokenId = await publicClient.readContract({
Expand All @@ -204,11 +203,15 @@ settings.get("/link-status", async (c) => {
args: [address as `0x${string}`, 0n],
}) as bigint;
agentId = Number(tokenId);
} catch { /* ERC-721 Enumerable not supported — agent ID unknown */ }
} catch { /* ERC-721 Enumerable not supported */ }

// Cache if we got the ID
if (agentId !== undefined) {
await db.setting.upsert({ where: { key: "agent_id" }, update: { value: String(agentId) }, create: { key: "agent_id", value: String(agentId) } });
}
return c.json({ linked: true, agentId, owsWallet: address });
}
} catch { /* balanceOf failed */ }
} catch { /* RPC failed — rate limited or unavailable */ }

return c.json({ linked: false, owsWallet: address });
} catch (err: unknown) {
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "plotlink-ows",
"version": "1.0.16",
"version": "1.0.17",
"bin": {
"plotlink-ows": "./bin/plotlink-ows.js"
},
Expand Down
Loading