stuntman 0.1.2 → 0.2.1
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/README.md +26 -2
- package/dist/cli.js +106 -8
- package/dist/credits.generated.js +4 -0
- package/dist/game.js +173 -0
- package/dist/kv.js +153 -0
- package/dist/launch.js +22 -0
- package/dist/mcp.js +49 -3
- package/dist/remote.js +69 -0
- package/dist/store.js +32 -3
- package/dist/version.js +1 -1
- package/dist/witness.js +550 -0
- package/package.json +15 -1
package/dist/launch.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export const LAUNCH = {
|
|
2
|
+
/** $STUNT */
|
|
3
|
+
token: '0xfadcdbb63b0326a772e18c5955028f2c23e81651',
|
|
4
|
+
/** StuntmanCredits on Robinhood Chain */
|
|
5
|
+
credits: '0xA9846E958f65D2A0f9Cc69A27650c793a5E9A529',
|
|
6
|
+
freeTakes: 25,
|
|
7
|
+
takePriceUsd: 0.01,
|
|
8
|
+
fallbackTakePriceTokens: '10',
|
|
9
|
+
burnBps: 5000,
|
|
10
|
+
};
|
|
11
|
+
const addr = (v) => (v && /^0x[0-9a-fA-F]{40}$/.test(v) ? v : null);
|
|
12
|
+
/** Launch settings with any environment overrides applied. */
|
|
13
|
+
export function launchSettings(env = process.env) {
|
|
14
|
+
return {
|
|
15
|
+
...LAUNCH,
|
|
16
|
+
// "none" turns a setting off, for local runs away from Robinhood Chain.
|
|
17
|
+
token: env.STUNTMAN_TOKEN === 'none' ? null : (addr(env.STUNTMAN_TOKEN) ?? LAUNCH.token),
|
|
18
|
+
credits: env.STUNTMAN_CREDITS === 'none' ? null : (addr(env.STUNTMAN_CREDITS) ?? LAUNCH.credits),
|
|
19
|
+
freeTakes: env.STUNTMAN_FREE_TAKES !== undefined ? Number(env.STUNTMAN_FREE_TAKES) : LAUNCH.freeTakes,
|
|
20
|
+
takePriceUsd: env.STUNTMAN_TAKE_PRICE_USD !== undefined ? Number(env.STUNTMAN_TAKE_PRICE_USD) : LAUNCH.takePriceUsd,
|
|
21
|
+
};
|
|
22
|
+
}
|
package/dist/mcp.js
CHANGED
|
@@ -12,6 +12,7 @@ import { balances, fund, runTake, TakeRejected } from './engine.js';
|
|
|
12
12
|
import { describeCapabilities } from './caps.js';
|
|
13
13
|
import { readiness, formatTake } from './report.js';
|
|
14
14
|
import { VERSION } from './version.js';
|
|
15
|
+
import { RemoteError, remoteCall, remoteTake, remoteTakes } from './remote.js';
|
|
15
16
|
function text(value) {
|
|
16
17
|
return { content: [{ type: 'text', text: value }] };
|
|
17
18
|
}
|
|
@@ -23,6 +24,9 @@ export function buildMcpServer(engine) {
|
|
|
23
24
|
instructions: 'Stuntman is a PRACTICE wallet. Every transaction you send here is simulated against the live chain with practice money and is never broadcast. Use it exactly like a real wallet: send the transactions apps give you, then check balances and takes. Tell the user results are from practice.',
|
|
24
25
|
});
|
|
25
26
|
const { cfg } = engine;
|
|
27
|
+
// Witness mode: sends, balances and takes live on the agent's witness endpoint.
|
|
28
|
+
const witness = cfg.witness;
|
|
29
|
+
const takesNow = async (limit = 200) => (witness ? (await remoteTakes(witness, limit)).reverse() : readTakes());
|
|
26
30
|
server.registerTool('wallet_address', {
|
|
27
31
|
title: 'Practice wallet address',
|
|
28
32
|
description: 'Returns the address this agent is practicing as and the chain it is on. Use it wherever an app asks for your wallet address.',
|
|
@@ -36,6 +40,16 @@ export function buildMcpServer(engine) {
|
|
|
36
40
|
annotations: { readOnlyHint: true },
|
|
37
41
|
}, async ({ tokens }) => {
|
|
38
42
|
const extra = (tokens ?? []).filter(isAddr);
|
|
43
|
+
if (witness) {
|
|
44
|
+
try {
|
|
45
|
+
const rows = await remoteCall(witness, 'stuntman_balances');
|
|
46
|
+
const lines = rows.map((r) => `${r.symbol.padEnd(10)} ${r.formatted}${r.practice ? '' : ' (real balance, untouched in practice)'}${r.token === 'ETH' ? '' : ` ${r.token}`}`);
|
|
47
|
+
return text(`Practice balances for ${checksum(cfg.account)} (witnessed):\n${lines.join('\n')}`);
|
|
48
|
+
}
|
|
49
|
+
catch (err) {
|
|
50
|
+
return errorText(err.message);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
39
53
|
const rows = await balances(engine, extra);
|
|
40
54
|
const lines = rows.map((r) => `${r.symbol.padEnd(10)} ${fmt(r.balance, r.decimals)}${r.practice ? '' : ' (real balance, untouched in practice)'}${r.token === 'ETH' ? '' : ` ${r.token}`}`);
|
|
41
55
|
return text(`Practice balances for ${checksum(cfg.account)}:\n${lines.join('\n')}`);
|
|
@@ -67,6 +81,31 @@ export function buildMcpServer(engine) {
|
|
|
67
81
|
catch {
|
|
68
82
|
return errorText('value and gas must be whole numbers (decimal or 0x hex).');
|
|
69
83
|
}
|
|
84
|
+
if (witness) {
|
|
85
|
+
try {
|
|
86
|
+
const tx = { from: cfg.account, data: callData, value: `0x${v.toString(16)}` };
|
|
87
|
+
if (!deploying)
|
|
88
|
+
tx.to = to;
|
|
89
|
+
if (g !== undefined)
|
|
90
|
+
tx.gas = `0x${g.toString(16)}`;
|
|
91
|
+
const hash = await remoteCall(witness, 'eth_sendTransaction', [tx]);
|
|
92
|
+
const take = await remoteTake(witness, hash);
|
|
93
|
+
if (!take)
|
|
94
|
+
return text(`Witnessed. Practice transaction hash: ${hash}\nNothing was broadcast.`);
|
|
95
|
+
const where = take.contractAddress ? `\nContract address: ${take.contractAddress}` : '';
|
|
96
|
+
const body = `${formatTake(take)}\n\nPractice transaction hash: ${take.txHash}${where}\nWitnessed by Stuntman. Nothing was broadcast.`;
|
|
97
|
+
return take.status === 'filled' ? text(body) : errorText(body);
|
|
98
|
+
}
|
|
99
|
+
catch (err) {
|
|
100
|
+
if (err instanceof RemoteError && err.code !== -32603) {
|
|
101
|
+
const latest = (await remoteTakes(witness, 1).catch(() => []))[0];
|
|
102
|
+
if (latest?.status === 'rejected' && Date.now() - Date.parse(latest.at) < 15_000) {
|
|
103
|
+
return errorText(`${formatTake(latest)}\n\nNothing was broadcast.`);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return errorText(`Could not simulate: ${err.message}`);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
70
109
|
try {
|
|
71
110
|
const take = await runTake(engine, { from: cfg.account, to: deploying ? null : to, data: callData, value: v, gas: g, source: 'mcp' });
|
|
72
111
|
const where = take.contractAddress ? `\nContract address: ${take.contractAddress}` : '';
|
|
@@ -84,7 +123,7 @@ export function buildMcpServer(engine) {
|
|
|
84
123
|
inputSchema: { limit: z.number().int().min(1).max(100).optional() },
|
|
85
124
|
annotations: { readOnlyHint: true },
|
|
86
125
|
}, async ({ limit }) => {
|
|
87
|
-
const takes =
|
|
126
|
+
const takes = (await takesNow(limit ?? 10)).slice(-(limit ?? 10)).reverse();
|
|
88
127
|
if (!takes.length)
|
|
89
128
|
return text('No takes yet. Send a transaction to start practicing.');
|
|
90
129
|
return text(takes.map((t) => `Take ${t.id} ${t.status.padEnd(8)} ${t.summary}`).join('\n'));
|
|
@@ -95,7 +134,7 @@ export function buildMcpServer(engine) {
|
|
|
95
134
|
inputSchema: { id: z.number().int().min(1) },
|
|
96
135
|
annotations: { readOnlyHint: true },
|
|
97
136
|
}, async ({ id }) => {
|
|
98
|
-
const take =
|
|
137
|
+
const take = (await takesNow()).find((t) => t.id === id);
|
|
99
138
|
return take ? text(formatTake(take, true)) : errorText(`No take ${id}.`);
|
|
100
139
|
});
|
|
101
140
|
server.registerTool('fund_practice', {
|
|
@@ -119,6 +158,10 @@ export function buildMcpServer(engine) {
|
|
|
119
158
|
}
|
|
120
159
|
}
|
|
121
160
|
const units = parseUnits(amount, decimals);
|
|
161
|
+
if (witness) {
|
|
162
|
+
const res = await remoteCall(witness, 'stuntman_fund', [isEth ? 'ETH' : asset, units.toString()]);
|
|
163
|
+
return text(`Added ${amount} ${res.symbol}. Practice balance is now ${fmt(BigInt(res.balance), res.decimals)} ${res.symbol}. This shows on the agent's public record.`);
|
|
164
|
+
}
|
|
122
165
|
const res = await fund(engine, isEth ? 'ETH' : asset, units);
|
|
123
166
|
return text(`Added ${amount} ${res.symbol}. Practice balance is now ${fmt(res.balance, res.decimals)} ${res.symbol}.`);
|
|
124
167
|
}
|
|
@@ -154,9 +197,10 @@ export function buildMcpServer(engine) {
|
|
|
154
197
|
inputSchema: {},
|
|
155
198
|
annotations: { readOnlyHint: true },
|
|
156
199
|
}, async () => {
|
|
157
|
-
const r = readiness(
|
|
200
|
+
const r = readiness(await takesNow().catch(() => []));
|
|
158
201
|
return text([
|
|
159
202
|
`Practicing as ${checksum(cfg.account)} (${cfg.mode === 'shadow' ? 'your wallet, watch only' : 'practice key'}) on chain ${cfg.chainId}.`,
|
|
203
|
+
...(witness ? ['Witnessed: every take is simulated and signed by Stuntman and kept on a public record.'] : []),
|
|
160
204
|
`Simulation: ${describeCapabilities(engine.caps)}.`,
|
|
161
205
|
`Takes: ${r.total} (${r.filled} filled, ${r.bad} bad).`,
|
|
162
206
|
`Ready to go live: ${r.ready ? 'yes' : 'not yet'}.`,
|
|
@@ -169,6 +213,8 @@ export function buildMcpServer(engine) {
|
|
|
169
213
|
inputSchema: { confirm: z.literal(true).describe('Must be true') },
|
|
170
214
|
annotations: { destructiveHint: true },
|
|
171
215
|
}, async () => {
|
|
216
|
+
if (witness)
|
|
217
|
+
return errorText('A witnessed record cannot be reset: every witnessed take stays on the public record. Turn witness mode off with "stuntman witness off" to practice locally.');
|
|
172
218
|
resetPractice(cfg.chainId, cfg.account);
|
|
173
219
|
return text(`Practice reset for ${shortAddr(cfg.account)}.`);
|
|
174
220
|
});
|
package/dist/remote.js
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
// Witness mode on the agent's side: the CLI and MCP tools send through the
|
|
2
|
+
// agent's hosted witness endpoint instead of simulating locally.
|
|
3
|
+
import { privateKeyToAccount } from 'viem/accounts';
|
|
4
|
+
export const DEFAULT_WITNESS_URL = 'https://stuntmanhq.xyz';
|
|
5
|
+
export function witnessBase() {
|
|
6
|
+
return (process.env.STUNTMAN_WITNESS_URL || DEFAULT_WITNESS_URL).replace(/\/$/, '');
|
|
7
|
+
}
|
|
8
|
+
export class RemoteError extends Error {
|
|
9
|
+
code;
|
|
10
|
+
constructor(code, message) {
|
|
11
|
+
super(message);
|
|
12
|
+
this.code = code;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
let nextId = 1;
|
|
16
|
+
/** One JSON-RPC call to the witness endpoint. */
|
|
17
|
+
export async function remoteCall(endpoint, method, params = []) {
|
|
18
|
+
let res;
|
|
19
|
+
try {
|
|
20
|
+
res = await fetch(endpoint, {
|
|
21
|
+
method: 'POST',
|
|
22
|
+
headers: { 'content-type': 'application/json' },
|
|
23
|
+
body: JSON.stringify({ jsonrpc: '2.0', id: nextId++, method, params }),
|
|
24
|
+
signal: AbortSignal.timeout(90_000),
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
catch (err) {
|
|
28
|
+
throw new RemoteError(-32603, `Could not reach the witness at ${new URL(endpoint).host}: ${err.message}`);
|
|
29
|
+
}
|
|
30
|
+
const body = (await res.json().catch(() => null));
|
|
31
|
+
if (!body)
|
|
32
|
+
throw new RemoteError(-32603, `The witness answered HTTP ${res.status}.`);
|
|
33
|
+
if (body.error)
|
|
34
|
+
throw new RemoteError(body.error.code, body.error.message);
|
|
35
|
+
return body.result;
|
|
36
|
+
}
|
|
37
|
+
export async function remoteTakes(endpoint, limit = 50) {
|
|
38
|
+
return remoteCall(endpoint, 'stuntman_takes', [limit]);
|
|
39
|
+
}
|
|
40
|
+
/** The take a send just made: the newest take with that hash. */
|
|
41
|
+
export async function remoteTake(endpoint, hash) {
|
|
42
|
+
return (await remoteTakes(endpoint, 5)).find((t) => t.txHash.toLowerCase() === hash.toLowerCase());
|
|
43
|
+
}
|
|
44
|
+
/** Register the practice account for witnessed practice. Needs the practice key. */
|
|
45
|
+
export async function registerWitness(cfg) {
|
|
46
|
+
if (!cfg.practiceKey) {
|
|
47
|
+
throw new Error(`Witnessed practice needs an account that can sign. Your account ${cfg.account} is your own wallet, so sign up with it at ${witnessBase()}/witness, or run "stuntman init --new" for a practice key.`);
|
|
48
|
+
}
|
|
49
|
+
const signer = privateKeyToAccount(cfg.practiceKey);
|
|
50
|
+
const issued = new Date().toISOString();
|
|
51
|
+
const message = `Stuntman witness\nAccount: ${signer.address}\nIssued: ${issued}`;
|
|
52
|
+
const signature = await signer.signMessage({ message });
|
|
53
|
+
let res;
|
|
54
|
+
try {
|
|
55
|
+
res = await fetch(`${witnessBase()}/api/witness/register`, {
|
|
56
|
+
method: 'POST',
|
|
57
|
+
headers: { 'content-type': 'application/json' },
|
|
58
|
+
body: JSON.stringify({ account: signer.address, issued, signature }),
|
|
59
|
+
signal: AbortSignal.timeout(30_000),
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
catch (err) {
|
|
63
|
+
throw new Error(`Could not reach ${witnessBase()}: ${err.message}`);
|
|
64
|
+
}
|
|
65
|
+
const body = (await res.json().catch(() => ({})));
|
|
66
|
+
if (!res.ok || !body.endpoint)
|
|
67
|
+
throw new Error(body.error ?? `The witness answered HTTP ${res.status}.`);
|
|
68
|
+
return { account: signer.address, endpoint: body.endpoint };
|
|
69
|
+
}
|
package/dist/store.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
// The RPC proxy and the MCP server can run at the same time, so every write goes
|
|
3
3
|
// through a small cross-process lock and an atomic rename.
|
|
4
4
|
import { join } from 'node:path';
|
|
5
|
+
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
5
6
|
import { existsSync, mkdirSync, readFileSync, writeFileSync, renameSync, rmSync, statSync, appendFileSync } from 'node:fs';
|
|
6
7
|
import { ensureHome } from './config.js';
|
|
7
8
|
/** Bumped when slot search learns new layouts, so earlier misses are tried again. */
|
|
@@ -34,8 +35,16 @@ function lockPath() {
|
|
|
34
35
|
return join(ensureHome(), 'state.lock');
|
|
35
36
|
}
|
|
36
37
|
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
38
|
+
const memory = new AsyncLocalStorage();
|
|
39
|
+
export function withMemoryStore(store, fn) {
|
|
40
|
+
return memory.run(store, fn);
|
|
41
|
+
}
|
|
42
|
+
const clone = (v) => JSON.parse(JSON.stringify(v));
|
|
37
43
|
/** Run fn while holding the cross-process lock. */
|
|
38
44
|
export async function withLock(fn) {
|
|
45
|
+
// The hosted witness locks each agent in its database instead.
|
|
46
|
+
if (memory.getStore())
|
|
47
|
+
return fn();
|
|
39
48
|
const path = lockPath();
|
|
40
49
|
const started = Date.now();
|
|
41
50
|
for (;;) {
|
|
@@ -68,10 +77,13 @@ export async function withLock(fn) {
|
|
|
68
77
|
}
|
|
69
78
|
}
|
|
70
79
|
export function readState(chainId, account) {
|
|
71
|
-
const
|
|
72
|
-
if (!
|
|
80
|
+
const mem = memory.getStore();
|
|
81
|
+
if (mem && !mem.state)
|
|
73
82
|
return freshState(chainId, account);
|
|
74
|
-
const
|
|
83
|
+
const path = mem ? '' : statePath();
|
|
84
|
+
if (!mem && !existsSync(path))
|
|
85
|
+
return freshState(chainId, account);
|
|
86
|
+
const parsed = mem ? clone(mem.state) : JSON.parse(readFileSync(path, 'utf8'));
|
|
75
87
|
// A state for a different account or chain is stale: start clean.
|
|
76
88
|
if (parsed.account?.toLowerCase() !== account.toLowerCase() || parsed.chainId !== chainId) {
|
|
77
89
|
return freshState(chainId, account);
|
|
@@ -94,15 +106,29 @@ export function readState(chainId, account) {
|
|
|
94
106
|
return parsed;
|
|
95
107
|
}
|
|
96
108
|
export function writeState(state) {
|
|
109
|
+
const mem = memory.getStore();
|
|
110
|
+
if (mem) {
|
|
111
|
+
mem.state = clone(state);
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
97
114
|
const path = statePath();
|
|
98
115
|
const tmp = `${path}.${process.pid}.tmp`;
|
|
99
116
|
writeFileSync(tmp, JSON.stringify(state, null, 2) + '\n', { mode: 0o600 });
|
|
100
117
|
renameSync(tmp, path);
|
|
101
118
|
}
|
|
102
119
|
export function appendTake(take) {
|
|
120
|
+
const mem = memory.getStore();
|
|
121
|
+
if (mem) {
|
|
122
|
+
mem.takes.push(clone(take));
|
|
123
|
+
mem.added.push(clone(take));
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
103
126
|
appendFileSync(takesPath(), JSON.stringify(take) + '\n', { mode: 0o600 });
|
|
104
127
|
}
|
|
105
128
|
export function readTakes() {
|
|
129
|
+
const mem = memory.getStore();
|
|
130
|
+
if (mem)
|
|
131
|
+
return clone(mem.takes);
|
|
106
132
|
const path = takesPath();
|
|
107
133
|
if (!existsSync(path))
|
|
108
134
|
return [];
|
|
@@ -122,6 +148,9 @@ export function readTakes() {
|
|
|
122
148
|
}
|
|
123
149
|
export function findTakeByHash(hash) {
|
|
124
150
|
const lower = hash.toLowerCase();
|
|
151
|
+
const mem = memory.getStore();
|
|
152
|
+
if (mem)
|
|
153
|
+
return mem.takes.find((t) => t.txHash.toLowerCase() === lower) ?? mem.byHash.get(lower);
|
|
125
154
|
return readTakes().find((t) => t.txHash.toLowerCase() === lower);
|
|
126
155
|
}
|
|
127
156
|
export function resetPractice(chainId, account) {
|
package/dist/version.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const VERSION = '0.1
|
|
1
|
+
export const VERSION = '0.2.1';
|