From 969a3da2a5ed73339c435aed64197e4949917112 Mon Sep 17 00:00:00 2001 From: Cho Young-Hwi Date: Fri, 24 Apr 2026 11:57:17 +0900 Subject: [PATCH] [#154] Fix balanceOf fallback: separate try/catch for tokenOfOwnerByIndex tokenOfOwnerByIndex reverts on ERC-8004 (not ERC-721 Enumerable). The outer catch was swallowing the balanceOf success. Now returns linked: true with agentId undefined when enumeration isn't supported. Co-Authored-By: Claude Opus 4.6 (1M context) --- app/routes/settings.ts | 23 ++++++++++++++--------- package-lock.json | 4 ++-- package.json | 2 +- 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/app/routes/settings.ts b/app/routes/settings.ts index a0c23a8..14a1bed 100644 --- a/app/routes/settings.ts +++ b/app/routes/settings.ts @@ -194,16 +194,21 @@ settings.get("/link-status", async (c) => { }) as bigint; if (balance > 0n) { - const tokenId = await publicClient.readContract({ - address: ERC_8004, - abi: [{ type: "function", name: "tokenOfOwnerByIndex", stateMutability: "view", inputs: [{ name: "owner", type: "address" }, { name: "index", type: "uint256" }], outputs: [{ name: "", type: "uint256" }] }] as const, - functionName: "tokenOfOwnerByIndex", - args: [address as `0x${string}`, 0n], - }) as bigint; - - return c.json({ linked: true, agentId: Number(tokenId), owsWallet: address }); + // Try to get token ID (ERC-721 Enumerable — may not be supported) + let agentId: number | undefined; + try { + const tokenId = await publicClient.readContract({ + address: ERC_8004, + abi: [{ type: "function", name: "tokenOfOwnerByIndex", stateMutability: "view", inputs: [{ name: "owner", type: "address" }, { name: "index", type: "uint256" }], outputs: [{ name: "", type: "uint256" }] }] as const, + functionName: "tokenOfOwnerByIndex", + args: [address as `0x${string}`, 0n], + }) as bigint; + agentId = Number(tokenId); + } catch { /* ERC-721 Enumerable not supported — agent ID unknown */ } + + return c.json({ linked: true, agentId, owsWallet: address }); } - } catch { /* best effort */ } + } catch { /* balanceOf failed */ } return c.json({ linked: false, owsWallet: address }); } catch (err: unknown) { diff --git a/package-lock.json b/package-lock.json index e23aabf..2970536 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "plotlink-ows", - "version": "1.0.15", + "version": "1.0.16", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "plotlink-ows", - "version": "1.0.15", + "version": "1.0.16", "hasInstallScript": true, "workspaces": [ "packages/*" diff --git a/package.json b/package.json index 82ff4db..bf8b425 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "plotlink-ows", - "version": "1.0.15", + "version": "1.0.16", "bin": { "plotlink-ows": "./bin/plotlink-ows.js" },