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
37 changes: 23 additions & 14 deletions app/routes/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,25 @@
import { erc8004Abi } from "../../packages/cli/src/sdk/abi";
import { listAgentWallets, getBaseAddress } from "../../lib/ows/wallet";
import { createOwsAccount } from "../lib/publish";
import { db } from "../db";

Check warning on line 7 in app/routes/settings.ts

View workflow job for this annotation

GitHub Actions / lint-and-typecheck

'db' is defined but never used
import {
signMessage as owsSignMsg,
} from "@open-wallet-standard/core";
import { CONFIG_DIR } from "../lib/paths";
import fs from "fs";
import path from "path";

const CONFIG_FILE = path.join(CONFIG_DIR, "config.json");

function readConfig(): Record<string, unknown> {
try { return JSON.parse(fs.readFileSync(CONFIG_FILE, "utf-8")); } catch { return {}; }
}

function writeConfig(updates: Record<string, unknown>) {
const config = readConfig();
Object.assign(config, updates);
fs.writeFileSync(CONFIG_FILE, JSON.stringify(config, null, 2));
}

const ERC_8004 = "0x8004A169FB4a3325136EB29fA0ceB6D2e539a432" as const;
const rpcUrl = process.env.NEXT_PUBLIC_RPC_URL || "https://mainnet.base.org";
Expand Down Expand Up @@ -133,12 +148,8 @@
return c.json({ error: "Transaction succeeded but Registered event not found" }, 500);
}

// Store agentId locally
await db.setting.upsert({
where: { key: "agent_id" },
update: { value: String(agentId) },
create: { key: "agent_id", value: String(agentId) },
});
// Cache agentId in config.json (survives npx reinstalls, no Prisma dependency)
writeConfig({ agentId });

return c.json({
agentId,
Expand All @@ -161,10 +172,10 @@
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 });
// Check config.json cache first (survives npx reinstalls + RPC rate limits)
const config = readConfig();
if (config.agentId) {
return c.json({ linked: true, agentId: Number(config.agentId), owsWallet: address });
}

// RPC: try agentIdByWallet (for bound wallets)
Expand All @@ -177,8 +188,7 @@
}) as bigint;

if (agentId > 0n) {
// Cache locally
await db.setting.upsert({ where: { key: "agent_id" }, update: { value: String(agentId) }, create: { key: "agent_id", value: String(agentId) } });
writeConfig({ agentId: Number(agentId) });
return c.json({ linked: true, agentId: Number(agentId), owsWallet: address });
}
} catch { /* agentIdByWallet may revert if not bound */ }
Expand All @@ -205,9 +215,8 @@
agentId = Number(tokenId);
} 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) } });
writeConfig({ agentId });
}
return c.json({ linked: true, agentId, owsWallet: address });
}
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.17",
"version": "1.0.18",
"bin": {
"plotlink-ows": "./bin/plotlink-ows.js"
},
Expand Down
Loading