run402 1.3.0 → 1.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cli.mjs +12 -0
- package/lib/agent.mjs +68 -0
- package/lib/apps.mjs +53 -0
- package/lib/config.mjs +7 -4
- package/lib/message.mjs +56 -0
- package/lib/sites.mjs +27 -7
- package/lib/wallet.mjs +102 -18
- package/package.json +1 -1
package/cli.mjs
CHANGED
|
@@ -23,6 +23,8 @@ Commands:
|
|
|
23
23
|
subdomains Manage custom subdomains (claim, list, delete)
|
|
24
24
|
apps Browse and manage the app marketplace
|
|
25
25
|
image Generate AI images via x402 micropayments
|
|
26
|
+
message Send messages to Run402 developers
|
|
27
|
+
agent Manage agent identity (contact info)
|
|
26
28
|
|
|
27
29
|
Run 'run402 <command> --help' for detailed usage of each command.
|
|
28
30
|
|
|
@@ -98,6 +100,16 @@ switch (cmd) {
|
|
|
98
100
|
await run(sub, rest);
|
|
99
101
|
break;
|
|
100
102
|
}
|
|
103
|
+
case "message": {
|
|
104
|
+
const { run } = await import("./lib/message.mjs");
|
|
105
|
+
await run(sub, rest);
|
|
106
|
+
break;
|
|
107
|
+
}
|
|
108
|
+
case "agent": {
|
|
109
|
+
const { run } = await import("./lib/agent.mjs");
|
|
110
|
+
await run(sub, rest);
|
|
111
|
+
break;
|
|
112
|
+
}
|
|
101
113
|
default:
|
|
102
114
|
console.error(`Unknown command: ${cmd}\n`);
|
|
103
115
|
console.log(HELP);
|
package/lib/agent.mjs
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { readWallet, API, WALLET_FILE } from "./config.mjs";
|
|
2
|
+
import { existsSync } from "fs";
|
|
3
|
+
|
|
4
|
+
const HELP = `run402 agent — Manage agent identity
|
|
5
|
+
|
|
6
|
+
Usage:
|
|
7
|
+
run402 agent contact --name <name> [--email <email>] [--webhook <url>]
|
|
8
|
+
|
|
9
|
+
Notes:
|
|
10
|
+
- Costs $0.001 USDC via x402
|
|
11
|
+
- Registers contact info so Run402 can reach your agent
|
|
12
|
+
- Only name is required; email and webhook are optional
|
|
13
|
+
|
|
14
|
+
Examples:
|
|
15
|
+
run402 agent contact --name my-agent
|
|
16
|
+
run402 agent contact --name my-agent --email ops@example.com --webhook https://example.com/hook
|
|
17
|
+
`;
|
|
18
|
+
|
|
19
|
+
async function contact(args) {
|
|
20
|
+
let name = null, email = null, webhook = null;
|
|
21
|
+
for (let i = 0; i < args.length; i++) {
|
|
22
|
+
if (args[i] === "--name" && args[i + 1]) name = args[++i];
|
|
23
|
+
if (args[i] === "--email" && args[i + 1]) email = args[++i];
|
|
24
|
+
if (args[i] === "--webhook" && args[i + 1]) webhook = args[++i];
|
|
25
|
+
}
|
|
26
|
+
if (!name) { console.error(JSON.stringify({ status: "error", message: "Missing --name <name>" })); process.exit(1); }
|
|
27
|
+
if (!existsSync(WALLET_FILE)) {
|
|
28
|
+
console.error(JSON.stringify({ status: "error", message: "No wallet found. Run: run402 wallet create && run402 wallet fund" }));
|
|
29
|
+
process.exit(1);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const wallet = readWallet();
|
|
33
|
+
const { privateKeyToAccount } = await import("viem/accounts");
|
|
34
|
+
const { createPublicClient, http } = await import("viem");
|
|
35
|
+
const { baseSepolia } = await import("viem/chains");
|
|
36
|
+
const { x402Client, wrapFetchWithPayment } = await import("@x402/fetch");
|
|
37
|
+
const { ExactEvmScheme } = await import("@x402/evm/exact/client");
|
|
38
|
+
const { toClientEvmSigner } = await import("@x402/evm");
|
|
39
|
+
const account = privateKeyToAccount(wallet.privateKey);
|
|
40
|
+
const publicClient = createPublicClient({ chain: baseSepolia, transport: http() });
|
|
41
|
+
const signer = toClientEvmSigner(account, publicClient);
|
|
42
|
+
const client = new x402Client();
|
|
43
|
+
client.register("eip155:84532", new ExactEvmScheme(signer));
|
|
44
|
+
const fetchPaid = wrapFetchWithPayment(fetch, client);
|
|
45
|
+
|
|
46
|
+
const body = { name };
|
|
47
|
+
if (email) body.email = email;
|
|
48
|
+
if (webhook) body.webhook = webhook;
|
|
49
|
+
|
|
50
|
+
const res = await fetchPaid(`${API}/v1/agent/contact`, {
|
|
51
|
+
method: "PUT",
|
|
52
|
+
headers: { "Content-Type": "application/json" },
|
|
53
|
+
body: JSON.stringify(body),
|
|
54
|
+
});
|
|
55
|
+
const data = await res.json();
|
|
56
|
+
if (!res.ok) { console.error(JSON.stringify({ status: "error", http: res.status, ...data })); process.exit(1); }
|
|
57
|
+
console.log(JSON.stringify(data, null, 2));
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export async function run(sub, args) {
|
|
61
|
+
if (!sub || sub === '--help' || sub === '-h') { console.log(HELP); process.exit(0); }
|
|
62
|
+
if (sub !== "contact") {
|
|
63
|
+
console.error(`Unknown subcommand: ${sub}\n`);
|
|
64
|
+
console.log(HELP);
|
|
65
|
+
process.exit(1);
|
|
66
|
+
}
|
|
67
|
+
await contact(args);
|
|
68
|
+
}
|
package/lib/apps.mjs
CHANGED
|
@@ -14,6 +14,10 @@ Subcommands:
|
|
|
14
14
|
publish <id> [--description <desc>] [--tags <t1,t2>] [--visibility <v>] [--fork-allowed]
|
|
15
15
|
Publish a project as an app
|
|
16
16
|
versions <id> List published versions of a project
|
|
17
|
+
inspect <version_id> Inspect a published app version
|
|
18
|
+
update <project_id> <version_id> [--description <desc>] [--tags <t1,t2>] [--visibility <v>] [--fork-allowed] [--no-fork]
|
|
19
|
+
Update a published version
|
|
20
|
+
delete <project_id> <version_id> Delete a published version
|
|
17
21
|
|
|
18
22
|
Examples:
|
|
19
23
|
run402 apps browse
|
|
@@ -21,6 +25,9 @@ Examples:
|
|
|
21
25
|
run402 apps fork ver_abc123 my-todo --tier prototype
|
|
22
26
|
run402 apps publish proj123 --description "Todo app" --tags todo,auth --visibility public --fork-allowed
|
|
23
27
|
run402 apps versions proj123
|
|
28
|
+
run402 apps inspect ver_abc123
|
|
29
|
+
run402 apps update proj123 ver_abc123 --description "Updated" --tags todo
|
|
30
|
+
run402 apps delete proj123 ver_abc123
|
|
24
31
|
`;
|
|
25
32
|
|
|
26
33
|
async function browse(args) {
|
|
@@ -122,6 +129,49 @@ async function versions(projectId) {
|
|
|
122
129
|
console.log(JSON.stringify(data, null, 2));
|
|
123
130
|
}
|
|
124
131
|
|
|
132
|
+
async function inspect(versionId) {
|
|
133
|
+
if (!versionId) { console.error(JSON.stringify({ status: "error", message: "Missing version ID" })); process.exit(1); }
|
|
134
|
+
const res = await fetch(`${API}/v1/apps/${versionId}`);
|
|
135
|
+
const data = await res.json();
|
|
136
|
+
if (!res.ok) { console.error(JSON.stringify({ status: "error", http: res.status, ...data })); process.exit(1); }
|
|
137
|
+
console.log(JSON.stringify(data, null, 2));
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
async function update(projectId, versionId, args) {
|
|
141
|
+
const p = findProject(projectId);
|
|
142
|
+
const body = {};
|
|
143
|
+
for (let i = 0; i < args.length; i++) {
|
|
144
|
+
if (args[i] === "--description" && args[i + 1]) body.description = args[++i];
|
|
145
|
+
if (args[i] === "--tags" && args[i + 1]) body.tags = args[++i].split(",");
|
|
146
|
+
if (args[i] === "--visibility" && args[i + 1]) body.visibility = args[++i];
|
|
147
|
+
if (args[i] === "--fork-allowed") body.fork_allowed = true;
|
|
148
|
+
if (args[i] === "--no-fork") body.fork_allowed = false;
|
|
149
|
+
}
|
|
150
|
+
const res = await fetch(`${API}/admin/v1/projects/${projectId}/versions/${versionId}`, {
|
|
151
|
+
method: "PATCH",
|
|
152
|
+
headers: { "Authorization": `Bearer ${p.service_key}`, "Content-Type": "application/json" },
|
|
153
|
+
body: JSON.stringify(body),
|
|
154
|
+
});
|
|
155
|
+
const data = await res.json();
|
|
156
|
+
if (!res.ok) { console.error(JSON.stringify({ status: "error", http: res.status, ...data })); process.exit(1); }
|
|
157
|
+
console.log(JSON.stringify(data, null, 2));
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
async function deleteVersion(projectId, versionId) {
|
|
161
|
+
const p = findProject(projectId);
|
|
162
|
+
const res = await fetch(`${API}/admin/v1/projects/${projectId}/versions/${versionId}`, {
|
|
163
|
+
method: "DELETE",
|
|
164
|
+
headers: { "Authorization": `Bearer ${p.service_key}` },
|
|
165
|
+
});
|
|
166
|
+
if (res.status === 204 || res.ok) {
|
|
167
|
+
console.log(JSON.stringify({ status: "ok", message: `Version ${versionId} deleted.` }));
|
|
168
|
+
} else {
|
|
169
|
+
const data = await res.json();
|
|
170
|
+
console.error(JSON.stringify({ status: "error", http: res.status, ...data }));
|
|
171
|
+
process.exit(1);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
125
175
|
export async function run(sub, args) {
|
|
126
176
|
if (!sub || sub === '--help' || sub === '-h') { console.log(HELP); process.exit(0); }
|
|
127
177
|
switch (sub) {
|
|
@@ -129,6 +179,9 @@ export async function run(sub, args) {
|
|
|
129
179
|
case "fork": await fork(args[0], args[1], args.slice(2)); break;
|
|
130
180
|
case "publish": await publish(args[0], args.slice(1)); break;
|
|
131
181
|
case "versions": await versions(args[0]); break;
|
|
182
|
+
case "inspect": await inspect(args[0]); break;
|
|
183
|
+
case "update": await update(args[0], args[1], args.slice(2)); break;
|
|
184
|
+
case "delete": await deleteVersion(args[0], args[1]); break;
|
|
132
185
|
default:
|
|
133
186
|
console.error(`Unknown subcommand: ${sub}\n`);
|
|
134
187
|
console.log(HELP);
|
package/lib/config.mjs
CHANGED
|
@@ -3,9 +3,10 @@
|
|
|
3
3
|
* Kept in a separate module so credential reads stay isolated.
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
-
import { readFileSync, writeFileSync, existsSync, mkdirSync, chmodSync } from "fs";
|
|
7
|
-
import { join } from "path";
|
|
6
|
+
import { readFileSync, writeFileSync, existsSync, mkdirSync, chmodSync, renameSync } from "fs";
|
|
7
|
+
import { join, dirname } from "path";
|
|
8
8
|
import { homedir } from "os";
|
|
9
|
+
import { randomBytes } from "crypto";
|
|
9
10
|
|
|
10
11
|
export const CONFIG_DIR = join(homedir(), ".config", "run402");
|
|
11
12
|
export const WALLET_FILE = join(CONFIG_DIR, "wallet.json");
|
|
@@ -19,8 +20,10 @@ export function readWallet() {
|
|
|
19
20
|
|
|
20
21
|
export function saveWallet(data) {
|
|
21
22
|
mkdirSync(CONFIG_DIR, { recursive: true });
|
|
22
|
-
|
|
23
|
-
|
|
23
|
+
const tmp = join(CONFIG_DIR, `.wallet.${randomBytes(4).toString("hex")}.tmp`);
|
|
24
|
+
writeFileSync(tmp, JSON.stringify(data, null, 2), { mode: 0o600 });
|
|
25
|
+
renameSync(tmp, WALLET_FILE);
|
|
26
|
+
chmodSync(WALLET_FILE, 0o600);
|
|
24
27
|
}
|
|
25
28
|
|
|
26
29
|
export function loadProjects() {
|
package/lib/message.mjs
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { readWallet, API, WALLET_FILE } from "./config.mjs";
|
|
2
|
+
import { existsSync } from "fs";
|
|
3
|
+
|
|
4
|
+
const HELP = `run402 message — Send messages to Run402 developers
|
|
5
|
+
|
|
6
|
+
Usage:
|
|
7
|
+
run402 message send <text>
|
|
8
|
+
|
|
9
|
+
Notes:
|
|
10
|
+
- Costs $0.01 USDC via x402
|
|
11
|
+
- Requires a funded wallet
|
|
12
|
+
|
|
13
|
+
Examples:
|
|
14
|
+
run402 message send "Hello from my agent!"
|
|
15
|
+
`;
|
|
16
|
+
|
|
17
|
+
async function send(text) {
|
|
18
|
+
if (!text) { console.error(JSON.stringify({ status: "error", message: "Missing message text" })); process.exit(1); }
|
|
19
|
+
if (!existsSync(WALLET_FILE)) {
|
|
20
|
+
console.error(JSON.stringify({ status: "error", message: "No wallet found. Run: run402 wallet create && run402 wallet fund" }));
|
|
21
|
+
process.exit(1);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const wallet = readWallet();
|
|
25
|
+
const { privateKeyToAccount } = await import("viem/accounts");
|
|
26
|
+
const { createPublicClient, http } = await import("viem");
|
|
27
|
+
const { baseSepolia } = await import("viem/chains");
|
|
28
|
+
const { x402Client, wrapFetchWithPayment } = await import("@x402/fetch");
|
|
29
|
+
const { ExactEvmScheme } = await import("@x402/evm/exact/client");
|
|
30
|
+
const { toClientEvmSigner } = await import("@x402/evm");
|
|
31
|
+
const account = privateKeyToAccount(wallet.privateKey);
|
|
32
|
+
const publicClient = createPublicClient({ chain: baseSepolia, transport: http() });
|
|
33
|
+
const signer = toClientEvmSigner(account, publicClient);
|
|
34
|
+
const client = new x402Client();
|
|
35
|
+
client.register("eip155:84532", new ExactEvmScheme(signer));
|
|
36
|
+
const fetchPaid = wrapFetchWithPayment(fetch, client);
|
|
37
|
+
|
|
38
|
+
const res = await fetchPaid(`${API}/v1/message`, {
|
|
39
|
+
method: "POST",
|
|
40
|
+
headers: { "Content-Type": "application/json" },
|
|
41
|
+
body: JSON.stringify({ message: text }),
|
|
42
|
+
});
|
|
43
|
+
const data = await res.json();
|
|
44
|
+
if (!res.ok) { console.error(JSON.stringify({ status: "error", http: res.status, ...data })); process.exit(1); }
|
|
45
|
+
console.log(JSON.stringify(data, null, 2));
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export async function run(sub, args) {
|
|
49
|
+
if (!sub || sub === '--help' || sub === '-h') { console.log(HELP); process.exit(0); }
|
|
50
|
+
if (sub !== "send") {
|
|
51
|
+
console.error(`Unknown subcommand: ${sub}\n`);
|
|
52
|
+
console.log(HELP);
|
|
53
|
+
process.exit(1);
|
|
54
|
+
}
|
|
55
|
+
await send(args.join(" "));
|
|
56
|
+
}
|
package/lib/sites.mjs
CHANGED
|
@@ -1,13 +1,18 @@
|
|
|
1
1
|
import { readFileSync, existsSync } from "fs";
|
|
2
2
|
import { readWallet, API, WALLET_FILE } from "./config.mjs";
|
|
3
3
|
|
|
4
|
-
const HELP = `run402 sites — Deploy static sites
|
|
4
|
+
const HELP = `run402 sites — Deploy and manage static sites
|
|
5
5
|
|
|
6
6
|
Usage:
|
|
7
7
|
run402 sites deploy --name <name> --manifest <file> [--project <id>] [--target <target>]
|
|
8
|
+
run402 sites status <deployment_id>
|
|
8
9
|
cat manifest.json | run402 sites deploy --name <name>
|
|
9
10
|
|
|
10
|
-
|
|
11
|
+
Subcommands:
|
|
12
|
+
deploy Deploy a static site
|
|
13
|
+
status Check the status of a deployment
|
|
14
|
+
|
|
15
|
+
Options (deploy):
|
|
11
16
|
--name <name> Site name (e.g. 'portfolio', 'family-todo')
|
|
12
17
|
--manifest <file> Path to manifest JSON file (or read from stdin)
|
|
13
18
|
--project <id> Optional project ID to link this deployment to
|
|
@@ -24,6 +29,7 @@ Manifest format (JSON):
|
|
|
24
29
|
|
|
25
30
|
Examples:
|
|
26
31
|
run402 sites deploy --name my-site --manifest site.json
|
|
32
|
+
run402 sites status dep_abc123
|
|
27
33
|
cat site.json | run402 sites deploy --name my-site
|
|
28
34
|
|
|
29
35
|
Notes:
|
|
@@ -81,12 +87,26 @@ async function deploy(args) {
|
|
|
81
87
|
console.log(JSON.stringify(data, null, 2));
|
|
82
88
|
}
|
|
83
89
|
|
|
90
|
+
async function status(args) {
|
|
91
|
+
let deploymentId = null;
|
|
92
|
+
for (let i = 0; i < args.length; i++) {
|
|
93
|
+
if (!args[i].startsWith("-")) { deploymentId = args[i]; break; }
|
|
94
|
+
}
|
|
95
|
+
if (!deploymentId) { console.error(JSON.stringify({ status: "error", message: "Missing deployment ID" })); process.exit(1); }
|
|
96
|
+
const res = await fetch(`${API}/v1/deployments/${deploymentId}`);
|
|
97
|
+
const data = await res.json();
|
|
98
|
+
if (!res.ok) { console.error(JSON.stringify({ status: "error", http: res.status, ...data })); process.exit(1); }
|
|
99
|
+
console.log(JSON.stringify(data, null, 2));
|
|
100
|
+
}
|
|
101
|
+
|
|
84
102
|
export async function run(sub, args) {
|
|
85
103
|
if (!sub || sub === '--help' || sub === '-h') { console.log(HELP); process.exit(0); }
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
104
|
+
switch (sub) {
|
|
105
|
+
case "deploy": await deploy(args); break;
|
|
106
|
+
case "status": await status(args); break;
|
|
107
|
+
default:
|
|
108
|
+
console.error(`Unknown subcommand: ${sub}\n`);
|
|
109
|
+
console.log(HELP);
|
|
110
|
+
process.exit(1);
|
|
90
111
|
}
|
|
91
|
-
await deploy(args);
|
|
92
112
|
}
|
package/lib/wallet.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { readWallet, saveWallet, API } from "./config.mjs";
|
|
1
|
+
import { readWallet, saveWallet, WALLET_FILE, API } from "./config.mjs";
|
|
2
2
|
|
|
3
3
|
const HELP = `run402 wallet — Manage your x402 wallet
|
|
4
4
|
|
|
@@ -9,26 +9,34 @@ Subcommands:
|
|
|
9
9
|
status Show wallet address, network, and funding status
|
|
10
10
|
create Generate a new wallet and save it locally
|
|
11
11
|
fund Request test USDC from the Run402 faucet (Base Sepolia)
|
|
12
|
-
balance
|
|
12
|
+
balance Show on-chain USDC (mainnet + testnet) and Run402 billing balance
|
|
13
13
|
export Print the wallet address (useful for scripting)
|
|
14
|
+
checkout Create a billing checkout session (--amount <usd_micros>)
|
|
15
|
+
history View billing transaction history (--limit <n>)
|
|
14
16
|
|
|
15
17
|
Notes:
|
|
16
18
|
- Wallet is stored locally at ~/.run402/wallet.json
|
|
17
|
-
-
|
|
18
|
-
- You need to create and fund a wallet before
|
|
19
|
+
- The wallet works on any EVM chain (currently Run402 uses Base Mainnet and Sepolia for testnet)
|
|
20
|
+
- You need to create and fund a wallet before any x402 transaction with Run402
|
|
19
21
|
|
|
20
22
|
Examples:
|
|
21
23
|
run402 wallet create
|
|
22
24
|
run402 wallet status
|
|
23
25
|
run402 wallet fund
|
|
24
26
|
run402 wallet export
|
|
27
|
+
run402 wallet checkout --amount 5000000
|
|
28
|
+
run402 wallet history --limit 10
|
|
25
29
|
`;
|
|
26
30
|
|
|
31
|
+
const USDC_ABI = [{ name: "balanceOf", type: "function", stateMutability: "view", inputs: [{ name: "account", type: "address" }], outputs: [{ name: "", type: "uint256" }] }];
|
|
32
|
+
const USDC_MAINNET = "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913";
|
|
33
|
+
const USDC_SEPOLIA = "0x036CbD53842c5426634e7929541eC2318f3dCF7e";
|
|
34
|
+
|
|
27
35
|
async function loadDeps() {
|
|
28
36
|
const { generatePrivateKey, privateKeyToAccount } = await import("viem/accounts");
|
|
29
37
|
const { createPublicClient, http } = await import("viem");
|
|
30
|
-
const { baseSepolia } = await import("viem/chains");
|
|
31
|
-
return { generatePrivateKey, privateKeyToAccount, createPublicClient, http, baseSepolia };
|
|
38
|
+
const { base, baseSepolia } = await import("viem/chains");
|
|
39
|
+
return { generatePrivateKey, privateKeyToAccount, createPublicClient, http, base, baseSepolia };
|
|
32
40
|
}
|
|
33
41
|
|
|
34
42
|
async function status() {
|
|
@@ -37,7 +45,7 @@ async function status() {
|
|
|
37
45
|
console.log(JSON.stringify({ status: "no_wallet", message: "No wallet found. Run: run402 wallet create" }));
|
|
38
46
|
return;
|
|
39
47
|
}
|
|
40
|
-
console.log(JSON.stringify({ status: "ok", address: w.address,
|
|
48
|
+
console.log(JSON.stringify({ status: "ok", address: w.address, created: w.created, funded: w.funded || false, path: WALLET_FILE }));
|
|
41
49
|
}
|
|
42
50
|
|
|
43
51
|
async function create() {
|
|
@@ -48,31 +56,74 @@ async function create() {
|
|
|
48
56
|
const { generatePrivateKey, privateKeyToAccount } = await loadDeps();
|
|
49
57
|
const privateKey = generatePrivateKey();
|
|
50
58
|
const account = privateKeyToAccount(privateKey);
|
|
51
|
-
saveWallet({ address: account.address, privateKey,
|
|
52
|
-
console.log(JSON.stringify({ status: "ok", address: account.address, message:
|
|
59
|
+
saveWallet({ address: account.address, privateKey, created: new Date().toISOString(), funded: false });
|
|
60
|
+
console.log(JSON.stringify({ status: "ok", address: account.address, message: `Wallet created. Stored locally at ${WALLET_FILE}` }));
|
|
53
61
|
}
|
|
54
62
|
|
|
55
63
|
async function fund() {
|
|
56
64
|
const w = readWallet();
|
|
57
65
|
if (!w) { console.log(JSON.stringify({ status: "error", message: "No wallet. Run: run402 wallet create" })); process.exit(1); }
|
|
66
|
+
|
|
67
|
+
const { createPublicClient, http, baseSepolia } = await loadDeps();
|
|
68
|
+
const client = createPublicClient({ chain: baseSepolia, transport: http() });
|
|
69
|
+
const before = await readUsdcBalance(client, USDC_SEPOLIA, w.address).catch(() => 0);
|
|
70
|
+
|
|
58
71
|
const res = await fetch(`${API}/v1/faucet`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ address: w.address }) });
|
|
59
72
|
const data = await res.json();
|
|
60
|
-
if (res.ok) {
|
|
61
|
-
saveWallet({ ...w, funded: true, lastFaucet: new Date().toISOString() });
|
|
62
|
-
console.log(JSON.stringify({ status: "ok", ...data }));
|
|
63
|
-
} else {
|
|
73
|
+
if (!res.ok) {
|
|
64
74
|
console.log(JSON.stringify({ status: "error", ...data }));
|
|
65
75
|
process.exit(1);
|
|
66
76
|
}
|
|
77
|
+
|
|
78
|
+
const MAX_WAIT = 30;
|
|
79
|
+
for (let i = 0; i < MAX_WAIT; i++) {
|
|
80
|
+
await new Promise(r => setTimeout(r, 1000));
|
|
81
|
+
const now = await readUsdcBalance(client, USDC_SEPOLIA, w.address).catch(() => before);
|
|
82
|
+
if (now > before) {
|
|
83
|
+
saveWallet({ ...w, funded: true, lastFaucet: new Date().toISOString() });
|
|
84
|
+
console.log(JSON.stringify({
|
|
85
|
+
address: w.address,
|
|
86
|
+
onchain: {
|
|
87
|
+
"base-sepolia_usd_micros": now,
|
|
88
|
+
},
|
|
89
|
+
}, null, 2));
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
saveWallet({ ...w, funded: true, lastFaucet: new Date().toISOString() });
|
|
95
|
+
console.log(JSON.stringify({ status: "ok", message: "Faucet request sent but balance not yet confirmed", ...data }));
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
async function readUsdcBalance(client, usdc, address) {
|
|
99
|
+
const raw = await client.readContract({ address: usdc, abi: USDC_ABI, functionName: "balanceOf", args: [address] });
|
|
100
|
+
return Number(raw);
|
|
67
101
|
}
|
|
68
102
|
|
|
69
103
|
async function balance() {
|
|
70
104
|
const w = readWallet();
|
|
71
105
|
if (!w) { console.log(JSON.stringify({ status: "error", message: "No wallet. Run: run402 wallet create" })); process.exit(1); }
|
|
72
|
-
|
|
73
|
-
const
|
|
74
|
-
|
|
75
|
-
|
|
106
|
+
|
|
107
|
+
const { createPublicClient, http, base, baseSepolia } = await loadDeps();
|
|
108
|
+
const mainnetClient = createPublicClient({ chain: base, transport: http() });
|
|
109
|
+
const sepoliaClient = createPublicClient({ chain: baseSepolia, transport: http() });
|
|
110
|
+
|
|
111
|
+
const [mainnetUsdc, sepoliaUsdc, billingRes] = await Promise.all([
|
|
112
|
+
readUsdcBalance(mainnetClient, USDC_MAINNET, w.address).catch(() => null),
|
|
113
|
+
readUsdcBalance(sepoliaClient, USDC_SEPOLIA, w.address).catch(() => null),
|
|
114
|
+
fetch(`${API}/v1/billing/accounts/${w.address.toLowerCase()}`),
|
|
115
|
+
]);
|
|
116
|
+
|
|
117
|
+
const billing = billingRes.ok ? await billingRes.json() : null;
|
|
118
|
+
|
|
119
|
+
console.log(JSON.stringify({
|
|
120
|
+
address: w.address,
|
|
121
|
+
onchain: {
|
|
122
|
+
"base-mainnet_usd_micros": mainnetUsdc,
|
|
123
|
+
"base-sepolia_usd_micros": sepoliaUsdc,
|
|
124
|
+
},
|
|
125
|
+
run402: billing ? { balance_usd_micros: billing.available_usd_micros } : "no billing account",
|
|
126
|
+
}, null, 2));
|
|
76
127
|
}
|
|
77
128
|
|
|
78
129
|
async function exportAddr() {
|
|
@@ -81,6 +132,37 @@ async function exportAddr() {
|
|
|
81
132
|
console.log(w.address);
|
|
82
133
|
}
|
|
83
134
|
|
|
135
|
+
async function checkout(args) {
|
|
136
|
+
const w = readWallet();
|
|
137
|
+
if (!w) { console.log(JSON.stringify({ status: "error", message: "No wallet. Run: run402 wallet create" })); process.exit(1); }
|
|
138
|
+
let amount = null;
|
|
139
|
+
for (let i = 0; i < args.length; i++) {
|
|
140
|
+
if (args[i] === "--amount" && args[i + 1]) amount = parseInt(args[++i], 10);
|
|
141
|
+
}
|
|
142
|
+
if (!amount) { console.error(JSON.stringify({ status: "error", message: "Missing --amount <usd_micros> (e.g. --amount 5000000 for $5)" })); process.exit(1); }
|
|
143
|
+
const res = await fetch(`${API}/v1/billing/checkouts`, {
|
|
144
|
+
method: "POST",
|
|
145
|
+
headers: { "Content-Type": "application/json" },
|
|
146
|
+
body: JSON.stringify({ wallet: w.address.toLowerCase(), amount_usd_micros: amount }),
|
|
147
|
+
});
|
|
148
|
+
const data = await res.json();
|
|
149
|
+
if (!res.ok) { console.error(JSON.stringify({ status: "error", http: res.status, ...data })); process.exit(1); }
|
|
150
|
+
console.log(JSON.stringify(data, null, 2));
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
async function history(args) {
|
|
154
|
+
const w = readWallet();
|
|
155
|
+
if (!w) { console.log(JSON.stringify({ status: "error", message: "No wallet. Run: run402 wallet create" })); process.exit(1); }
|
|
156
|
+
let limit = 20;
|
|
157
|
+
for (let i = 0; i < args.length; i++) {
|
|
158
|
+
if (args[i] === "--limit" && args[i + 1]) limit = parseInt(args[++i], 10);
|
|
159
|
+
}
|
|
160
|
+
const res = await fetch(`${API}/v1/billing/accounts/${w.address.toLowerCase()}/history?limit=${limit}`);
|
|
161
|
+
const data = await res.json();
|
|
162
|
+
if (!res.ok) { console.error(JSON.stringify({ status: "error", http: res.status, ...data })); process.exit(1); }
|
|
163
|
+
console.log(JSON.stringify(data, null, 2));
|
|
164
|
+
}
|
|
165
|
+
|
|
84
166
|
export async function run(sub, args) {
|
|
85
167
|
if (!sub || sub === '--help' || sub === '-h') {
|
|
86
168
|
console.log(HELP);
|
|
@@ -91,7 +173,9 @@ export async function run(sub, args) {
|
|
|
91
173
|
case "create": await create(); break;
|
|
92
174
|
case "fund": await fund(); break;
|
|
93
175
|
case "balance": await balance(); break;
|
|
94
|
-
case "export":
|
|
176
|
+
case "export": await exportAddr(); break;
|
|
177
|
+
case "checkout": await checkout(args); break;
|
|
178
|
+
case "history": await history(args); break;
|
|
95
179
|
default:
|
|
96
180
|
console.error(`Unknown subcommand: ${sub}\n`);
|
|
97
181
|
console.log(HELP);
|
package/package.json
CHANGED