Skip to content

Commit a45f63a

Browse files
grypezclaude
andcommitted
refactor(evm-wallet-experiment): extract wallet capabilities into capabilities.ts
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 7e3731a commit a45f63a

2 files changed

Lines changed: 101 additions & 72 deletions

File tree

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import { capability } from '@ocap/kernel-agents/capabilities/capability';
2+
import type { CapabilityRecord } from '@ocap/kernel-agents/types';
3+
4+
type CallAway = (
5+
kref: string,
6+
method: string,
7+
args?: unknown[],
8+
) => Promise<unknown>;
9+
10+
/**
11+
* Build the wallet capability set for an agent operating against the away coordinator.
12+
*
13+
* @param callAway - Function to invoke a method on a vat in the away container.
14+
* @param awayCoordKref - Kref of the away coordinator vat.
15+
* @returns A capability record suitable for passing to {@link makeChatAgent}.
16+
*/
17+
export function makeWalletCapabilities(
18+
callAway: CallAway,
19+
awayCoordKref: string,
20+
): CapabilityRecord {
21+
const balance = capability(
22+
async () => {
23+
const accounts = (await callAway(
24+
awayCoordKref,
25+
'getAccounts',
26+
)) as string[];
27+
const raw = (await callAway(awayCoordKref, 'request', [
28+
'eth_getBalance',
29+
[accounts[0], 'latest'],
30+
])) as string;
31+
return `Balance: ${(parseInt(raw, 16) / 1e18).toFixed(4)} ETH`;
32+
},
33+
{
34+
description: 'Get the ETH balance of the wallet',
35+
args: {},
36+
returns: { type: 'string' },
37+
},
38+
);
39+
40+
const accounts = capability(
41+
async () => {
42+
const addrs = (await callAway(awayCoordKref, 'getAccounts')) as string[];
43+
return `Accounts: ${addrs.join(', ')}`;
44+
},
45+
{
46+
description: 'Get wallet accounts/addresses',
47+
args: {},
48+
returns: { type: 'string' },
49+
},
50+
);
51+
52+
const send = capability(
53+
async ({ to, value }: { to: string; value: string }) => {
54+
const addrs = (await callAway(awayCoordKref, 'getAccounts')) as string[];
55+
const weiValue = `0x${(parseFloat(value || '0.01') * 1e18).toString(16)}`;
56+
const txHash = await callAway(awayCoordKref, 'request', [
57+
'eth_sendTransaction',
58+
[
59+
{
60+
from: addrs[0],
61+
to: to || '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
62+
value: weiValue,
63+
},
64+
],
65+
]);
66+
return `Transaction sent: ${txHash as string}`;
67+
},
68+
{
69+
description: 'Send ETH to an address',
70+
args: { to: { type: 'string' }, value: { type: 'string' } },
71+
returns: { type: 'string' },
72+
},
73+
);
74+
75+
const sign = capability(
76+
async ({ message }: { message: string }) => {
77+
const signature = await callAway(awayCoordKref, 'signMessage', [
78+
message || 'test',
79+
]);
80+
return `Signature: ${signature as string}`;
81+
},
82+
{
83+
description: 'Sign a message with the wallet',
84+
args: { message: { type: 'string' } },
85+
returns: { type: 'string' },
86+
},
87+
);
88+
89+
return {
90+
wallet_balance: balance,
91+
wallet_accounts: accounts,
92+
wallet_send: send,
93+
wallet_sign: sign,
94+
};
95+
}

packages/evm-wallet-experiment/test/e2e/docker/run-agent-loop-tests.mjs

Lines changed: 6 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@
1111
* as a bonus check rather than a hard requirement.
1212
*/
1313

14-
import { capability } from '@ocap/kernel-agents/capabilities/capability';
1514
import { makeChatAgent } from '@ocap/kernel-agents/chat';
1615

16+
import { makeWalletCapabilities } from './helpers/capabilities.ts';
17+
1718
const LLM_BASE_URL = process.env.LLM_BASE_URL ?? 'http://llm:11434';
1819
const LLM_MODEL = process.env.LLM_MODEL ?? 'qwen2.5:0.5b';
1920
const LLM_API = process.env.LLM_API ?? 'ollama';
@@ -50,71 +51,6 @@ export async function runAgentLoopTests(ctx) {
5051

5152
const awayCoordKref = awayInfo.coordinatorKref;
5253

53-
const walletBalance = capability(
54-
async () => {
55-
const accounts = await callAway(awayCoordKref, 'getAccounts');
56-
const balance = await callAway(awayCoordKref, 'request', [
57-
'eth_getBalance',
58-
[accounts[0], 'latest'],
59-
]);
60-
return `Balance: ${(parseInt(balance, 16) / 1e18).toFixed(4)} ETH`;
61-
},
62-
{
63-
description: 'Get the ETH balance of the wallet',
64-
args: {},
65-
returns: { type: 'string' },
66-
},
67-
);
68-
69-
const walletAccounts = capability(
70-
async () => {
71-
const accounts = await callAway(awayCoordKref, 'getAccounts');
72-
return `Accounts: ${accounts.join(', ')}`;
73-
},
74-
{
75-
description: 'Get wallet accounts/addresses',
76-
args: {},
77-
returns: { type: 'string' },
78-
},
79-
);
80-
81-
const walletSend = capability(
82-
async ({ to, value }) => {
83-
const accounts = await callAway(awayCoordKref, 'getAccounts');
84-
const weiValue = `0x${(parseFloat(value || '0.01') * 1e18).toString(16)}`;
85-
const txHash = await callAway(awayCoordKref, 'request', [
86-
'eth_sendTransaction',
87-
[
88-
{
89-
from: accounts[0],
90-
to: to || '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
91-
value: weiValue,
92-
},
93-
],
94-
]);
95-
return `Transaction sent: ${txHash}`;
96-
},
97-
{
98-
description: 'Send ETH to an address',
99-
args: { to: { type: 'string' }, value: { type: 'string' } },
100-
returns: { type: 'string' },
101-
},
102-
);
103-
104-
const walletSign = capability(
105-
async ({ message }) => {
106-
const signature = await callAway(awayCoordKref, 'signMessage', [
107-
message || 'test',
108-
]);
109-
return `Signature: ${signature}`;
110-
},
111-
{
112-
description: 'Sign a message with the wallet',
113-
args: { message: { type: 'string' } },
114-
returns: { type: 'string' },
115-
},
116-
);
117-
11854
const chat = async (messages) => {
11955
const response = await fetch(CHAT_URL, {
12056
method: 'POST',
@@ -126,12 +62,10 @@ export async function runAgentLoopTests(ctx) {
12662

12763
const agent = makeChatAgent({
12864
chat,
129-
capabilities: {
130-
wallet_balance: walletBalance,
131-
wallet_accounts: walletAccounts,
132-
wallet_send: walletSend,
133-
wallet_sign: walletSign,
134-
},
65+
capabilities: makeWalletCapabilities(
66+
(_kref, method, args) => callAway(method, args),
67+
awayCoordKref,
68+
),
13569
});
13670

13771
// -- Test 1: Balance query --

0 commit comments

Comments
 (0)