-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathx402-agent-order.ts
More file actions
82 lines (73 loc) · 3.04 KB
/
Copy pathx402-agent-order.ts
File metadata and controls
82 lines (73 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Place and fund a ground-truth photo order on Onsight, paid via x402 —
// then (optionally) pick the winning photo with a wallet-signed owner action.
//
// An AI agent can't photograph a real place; this commissions a real person to.
// x402 handles payment inline: the first request comes back 402, `x402-fetch`
// signs a USDC payment on Base and retries automatically. The agent's wallet IS
// the account — no signup, no API key.
//
// Run (from the repo root):
// npm install
// AGENT_PRIVATE_KEY=0x... npm run example
//
// The wallet needs USDC on Base (or Base Sepolia for testing) plus a little ETH
// for the on-chain settlement.
import { wrapFetchWithPayment } from "x402-fetch";
import { privateKeyToAccount } from "viem/accounts";
import { missionOrderUrl, signOwnerActionHeaders } from "../src/onsight.js";
const BASE_URL = process.env.ONSIGHT_URL ?? "https://onsight.photo";
const REWARD_USD = 20;
const key = process.env.AGENT_PRIVATE_KEY;
if (!key)
throw new Error("Set AGENT_PRIVATE_KEY to a wallet holding USDC on Base");
const account = privateKeyToAccount(key as `0x${string}`);
// fetch that transparently answers a 402 by paying and retrying:
const fetchWithPay = wrapFetchWithPayment(fetch, account);
const mission = {
title: "Storefront at 123 Market St",
description: "Street-level photo of the entrance, taken today in daylight.",
lat: 37.7897,
lng: -122.3972,
radiusMeters: 150,
};
// 1. Post + fund the mission. First call → 402; x402-fetch pays and retries.
const res = await fetchWithPay(missionOrderUrl(REWARD_USD, BASE_URL), {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(mission),
});
if (!res.ok) throw new Error(`Order failed: ${res.status} ${await res.text()}`);
const created = (await res.json()) as {
id: string;
txHash?: string;
paymentTxHash?: string;
};
console.log("Mission funded:", created.id);
console.log("Settlement tx:", created.txHash ?? created.paymentTxHash);
// 2. Read it back (public — no payment, no signature) to poll for submitted photos.
const check = await fetch(`${BASE_URL}/agent/missions/${created.id}`);
const state = (await check.json()) as {
status?: string;
paymentStatus?: string;
photos?: unknown[];
};
console.log(
`Status: ${state.status ?? state.paymentStatus} — ${(state.photos ?? []).length} photo(s) so far`,
);
// 3. Pick + pay a winner. This is an OWNER action — no more x402, just prove
// control of the funding wallet by signing the request. Guarded behind an env
// var so running the example doesn't accidentally pay out.
const photoId = process.env.SELECTED_PHOTO_ID;
if (photoId) {
const path = `/agent/missions/${created.id}/select`;
const body = { photoId };
const headers = await signOwnerActionHeaders(account, "POST", path, body);
const pick = await fetchWithPay(`${BASE_URL}${path}`, {
method: "POST",
headers: { "Content-Type": "application/json", ...headers },
body: JSON.stringify(body),
});
console.log(
pick.ok ? `Paid out to photo ${photoId}` : `Select failed: ${pick.status}`,
);
}