stoa/apps/web/lib/contracts.ts
stoa f29f2de140 fix(web): production build green — 9 pages + 8 API routes
- PHAROS_ATLANTIC via viem defineChain; explicit chain on writeContract calls
- next.config: ignore type/lint gates for the demo build (per no-typecheck pref)
- refresh docs/ARCHITECTURE.md to the current 7-package / 13-contract / 255-skill system
- verified: next build compiles + prerenders all routes
2026-06-12 10:01:48 +03:00

84 lines
2.7 KiB
TypeScript

"use client";
// Client-side contract writes through the user's injected wallet (viem custom transport).
// Addresses come from NEXT_PUBLIC_* env vars so they're available in the browser.
import { createWalletClient, custom, type Address, type Hash } from "viem";
import { PHAROS_ATLANTIC } from "./wallet";
type Eip1193 = { request: (args: { method: string; params?: unknown[] }) => Promise<unknown> };
function injected(): Eip1193 {
const eth = (globalThis as { ethereum?: Eip1193 }).ethereum;
if (!eth) throw new Error("No injected wallet found.");
return eth;
}
async function walletClient() {
const eth = injected();
const accounts = (await eth.request({ method: "eth_requestAccounts" })) as Address[];
const account = accounts[0];
if (!account) throw new Error("No account authorized.");
return createWalletClient({ account, chain: PHAROS_ATLANTIC, transport: custom(eth) });
}
const REGISTRY_ABI = [
{
type: "function",
name: "register",
stateMutability: "nonpayable",
inputs: [{ name: "metadataURI", type: "string" }],
outputs: [{ name: "agentId", type: "uint256" }],
},
] as const;
const SERVICES_ABI = [
{
type: "function",
name: "list",
stateMutability: "nonpayable",
inputs: [
{ name: "agentId", type: "uint256" },
{ name: "capability", type: "string" },
{ name: "endpoint", type: "string" },
{ name: "metadataURI", type: "string" },
{ name: "priceWei", type: "uint256" },
],
outputs: [{ name: "serviceId", type: "uint256" }],
},
] as const;
function addr(envName: string): Address {
const value = process.env[envName];
if (!value) throw new Error(`${envName} is not configured (deploy + set NEXT_PUBLIC_* env).`);
return value as Address;
}
/// Register an agent identity on StoaRegistry via the connected wallet. Returns the tx hash.
export async function registerAgentOnChain(metadataURI: string): Promise<Hash> {
const wallet = await walletClient();
return wallet.writeContract({
chain: PHAROS_ATLANTIC,
address: addr("NEXT_PUBLIC_STOA_REGISTRY_ADDRESS"),
abi: REGISTRY_ABI,
functionName: "register",
args: [metadataURI],
});
}
/// List a service on ServiceRegistry via the connected wallet. Returns the tx hash.
export async function listServiceOnChain(input: {
agentId: bigint;
capability: string;
endpoint: string;
metadataURI: string;
priceWei: bigint;
}): Promise<Hash> {
const wallet = await walletClient();
return wallet.writeContract({
chain: PHAROS_ATLANTIC,
address: addr("NEXT_PUBLIC_STOA_SERVICES_ADDRESS"),
abi: SERVICES_ABI,
functionName: "list",
args: [input.agentId, input.capability, input.endpoint, input.metadataURI, input.priceWei],
});
}