From 561d9b5fb2a6e997404ed2cd51e34f9139486561 Mon Sep 17 00:00:00 2001 From: Cho Young-Hwi Date: Fri, 24 Apr 2026 11:51:41 +0900 Subject: [PATCH] [#154] Fix link-status: detect agent NFT ownership via balanceOf fallback agentIdByWallet only works for bound wallets. register() creates the NFT but doesn't call setAgentWallet, so the binding lookup fails. Added balanceOf + tokenOfOwnerByIndex fallback to detect owned agent NFTs. Co-Authored-By: Claude Opus 4.6 (1M context) --- app/routes/settings.ts | 23 ++++++++++++++++++++++- package-lock.json | 4 ++-- package.json | 2 +- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/app/routes/settings.ts b/app/routes/settings.ts index 7fa10ac..a0c23a8 100644 --- a/app/routes/settings.ts +++ b/app/routes/settings.ts @@ -182,7 +182,28 @@ settings.get("/link-status", async (c) => { } catch { /* best effort */ } return c.json({ linked: true, agentId: Number(agentId), owsWallet: address, owner }); } - } catch { /* contract call may fail */ } + } catch { /* agentIdByWallet may revert if not bound */ } + + // Fallback: check if wallet owns an agent NFT (register() creates NFT but doesn't bind via setAgentWallet) + try { + const balance = await publicClient.readContract({ + address: ERC_8004, + abi: [{ type: "function", name: "balanceOf", stateMutability: "view", inputs: [{ name: "owner", type: "address" }], outputs: [{ name: "", type: "uint256" }] }] as const, + functionName: "balanceOf", + args: [address as `0x${string}`], + }) 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 }); + } + } catch { /* best effort */ } return c.json({ linked: false, owsWallet: address }); } catch (err: unknown) { diff --git a/package-lock.json b/package-lock.json index f5716b6..e23aabf 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "plotlink-ows", - "version": "1.0.14", + "version": "1.0.15", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "plotlink-ows", - "version": "1.0.14", + "version": "1.0.15", "hasInstallScript": true, "workspaces": [ "packages/*" diff --git a/package.json b/package.json index dc70817..82ff4db 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "plotlink-ows", - "version": "1.0.14", + "version": "1.0.15", "bin": { "plotlink-ows": "./bin/plotlink-ows.js" },