Skip to content

Commit 109851c

Browse files
grypezclaude
andcommitted
refactor(evm-wallet): extract shared tx helpers to lib/tx-utils
Move applyGasBuffer, validateGasEstimate, and validateTokenCallResult out of both coordinator vats into src/lib/tx-utils.ts. Both vats now import from the shared module. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 236baaa commit 109851c

3 files changed

Lines changed: 66 additions & 118 deletions

File tree

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import type { Address, Hex } from '../types.ts';
2+
3+
/**
4+
* Apply a percentage buffer to a hex gas value.
5+
*
6+
* @param gasHex - The gas value as a hex string.
7+
* @param bufferPercent - The buffer percentage to add (e.g. 10 for 10%).
8+
* @returns The buffered gas value as a hex string.
9+
*/
10+
export function applyGasBuffer(gasHex: Hex, bufferPercent: number): Hex {
11+
const gas = BigInt(gasHex);
12+
const buffered = gas + (gas * BigInt(bufferPercent)) / 100n;
13+
return `0x${buffered.toString(16)}`;
14+
}
15+
16+
/**
17+
* Validate that an `eth_estimateGas` response is a valid hex string.
18+
*
19+
* @param result - The raw RPC response.
20+
* @returns The validated hex string.
21+
* @throws If the result is not a hex string.
22+
*/
23+
export function validateGasEstimate(result: unknown): Hex {
24+
if (typeof result !== 'string' || !result.startsWith('0x')) {
25+
throw new Error(
26+
`eth_estimateGas returned unexpected value: ${String(result)}`,
27+
);
28+
}
29+
return result as Hex;
30+
}
31+
32+
/**
33+
* Validate that a token `eth_call` response is a usable hex string.
34+
*
35+
* @param result - The raw RPC response.
36+
* @param method - The ERC-20 method name (for error context).
37+
* @param token - The token address (for error context).
38+
* @returns The validated hex string.
39+
* @throws If the result is not a non-empty hex string.
40+
*/
41+
export function validateTokenCallResult(
42+
result: unknown,
43+
method: string,
44+
token: Address,
45+
): Hex {
46+
if (
47+
typeof result !== 'string' ||
48+
!result.startsWith('0x') ||
49+
result === '0x'
50+
) {
51+
throw new Error(
52+
`${method}() call to token ${token} returned unexpected value: ${String(result)}`,
53+
);
54+
}
55+
return result as Hex;
56+
}

packages/evm-wallet-experiment/src/vats/away-coordinator.ts

Lines changed: 5 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ import {
2424
setSdkLogger,
2525
computeSmartAccountAddress,
2626
} from '../lib/sdk.ts';
27+
import {
28+
applyGasBuffer,
29+
validateGasEstimate,
30+
validateTokenCallResult,
31+
} from '../lib/tx-utils.ts';
2732
import { ENTRY_POINT_V07 } from '../lib/userop.ts';
2833
import type {
2934
Address,
@@ -41,65 +46,6 @@ import type {
4146

4247
const harden = globalThis.harden ?? (<T>(value: T): T => value);
4348

44-
// ---------------------------------------------------------------------------
45-
// Helper functions
46-
// ---------------------------------------------------------------------------
47-
48-
/**
49-
* Apply a percentage buffer to a hex gas value.
50-
*
51-
* @param gasHex - The gas value as a hex string.
52-
* @param bufferPercent - The buffer percentage to add (e.g. 10 for 10%).
53-
* @returns The buffered gas value as a hex string.
54-
*/
55-
function applyGasBuffer(gasHex: Hex, bufferPercent: number): Hex {
56-
const gas = BigInt(gasHex);
57-
const buffered = gas + (gas * BigInt(bufferPercent)) / 100n;
58-
return `0x${buffered.toString(16)}`;
59-
}
60-
61-
/**
62-
* Validate that an `eth_estimateGas` response is a valid hex string.
63-
*
64-
* @param result - The raw RPC response.
65-
* @returns The validated hex string.
66-
* @throws If the result is not a hex string.
67-
*/
68-
function validateGasEstimate(result: unknown): Hex {
69-
if (typeof result !== 'string' || !result.startsWith('0x')) {
70-
throw new Error(
71-
`eth_estimateGas returned unexpected value: ${String(result)}`,
72-
);
73-
}
74-
return result as Hex;
75-
}
76-
77-
/**
78-
* Validate that a token `eth_call` response is a usable hex string.
79-
*
80-
* @param result - The raw RPC response.
81-
* @param method - The ERC-20 method name (for error context).
82-
* @param token - The token address (for error context).
83-
* @returns The validated hex string.
84-
* @throws If the result is not a non-empty hex string.
85-
*/
86-
function validateTokenCallResult(
87-
result: unknown,
88-
method: string,
89-
token: Address,
90-
): Hex {
91-
if (
92-
typeof result !== 'string' ||
93-
!result.startsWith('0x') ||
94-
result === '0x'
95-
) {
96-
throw new Error(
97-
`${method}() call to token ${token} returned unexpected value: ${String(result)}`,
98-
);
99-
}
100-
return result as Hex;
101-
}
102-
10349
/**
10450
* Convert a wei amount in hex to a human-readable ETH string.
10551
*

packages/evm-wallet-experiment/src/vats/home-coordinator.ts

Lines changed: 5 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ import {
4141
resolveEnvironment,
4242
setSdkLogger,
4343
} from '../lib/sdk.ts';
44+
import {
45+
applyGasBuffer,
46+
validateGasEstimate,
47+
validateTokenCallResult,
48+
} from '../lib/tx-utils.ts';
4449
import { ENTRY_POINT_V07 } from '../lib/userop.ts';
4550
import type {
4651
Address,
@@ -62,65 +67,6 @@ import type {
6267

6368
const harden = globalThis.harden ?? (<T>(value: T): T => value);
6469

65-
// ---------------------------------------------------------------------------
66-
// Helper functions
67-
// ---------------------------------------------------------------------------
68-
69-
/**
70-
* Apply a percentage buffer to a hex gas value.
71-
*
72-
* @param gasHex - The gas value as a hex string.
73-
* @param bufferPercent - The buffer percentage to add (e.g. 10 for 10%).
74-
* @returns The buffered gas value as a hex string.
75-
*/
76-
function applyGasBuffer(gasHex: Hex, bufferPercent: number): Hex {
77-
const gas = BigInt(gasHex);
78-
const buffered = gas + (gas * BigInt(bufferPercent)) / 100n;
79-
return `0x${buffered.toString(16)}`;
80-
}
81-
82-
/**
83-
* Validate that an `eth_estimateGas` response is a valid hex string.
84-
*
85-
* @param result - The raw RPC response.
86-
* @returns The validated hex string.
87-
* @throws If the result is not a hex string.
88-
*/
89-
function validateGasEstimate(result: unknown): Hex {
90-
if (typeof result !== 'string' || !result.startsWith('0x')) {
91-
throw new Error(
92-
`eth_estimateGas returned unexpected value: ${String(result)}`,
93-
);
94-
}
95-
return result as Hex;
96-
}
97-
98-
/**
99-
* Validate that a token `eth_call` response is a usable hex string.
100-
*
101-
* @param result - The raw RPC response.
102-
* @param method - The ERC-20 method name (for error context).
103-
* @param token - The token address (for error context).
104-
* @returns The validated hex string.
105-
* @throws If the result is not a non-empty hex string.
106-
*/
107-
function validateTokenCallResult(
108-
result: unknown,
109-
method: string,
110-
token: Address,
111-
): Hex {
112-
if (
113-
typeof result !== 'string' ||
114-
!result.startsWith('0x') ||
115-
result === '0x'
116-
) {
117-
throw new Error(
118-
`${method}() call to token ${token} returned unexpected value: ${String(result)}`,
119-
);
120-
}
121-
return result as Hex;
122-
}
123-
12470
// ---------------------------------------------------------------------------
12571
// Vat types
12672
// ---------------------------------------------------------------------------

0 commit comments

Comments
 (0)