Skip to content

Commit 5d83146

Browse files
authored
[Agent SDK] Sign Request Value Types (#28)
This PR preserves the old functionality by keeping the hex input as it was (so should not break anything). However, its not entirely clear at this moment if the newly accepted input will pass transaction simulation. Will iterate if necessary.
1 parent 7dac160 commit 5d83146

File tree

9 files changed

+1342
-33
lines changed

9 files changed

+1342
-33
lines changed

bun.lock

Lines changed: 1292 additions & 0 deletions
Large diffs are not rendered by default.

bun.lockb

-265 KB
Binary file not shown.

packages/agent-sdk/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@
3232
"build:cjs": "tsc -p tsconfig.cjs.json"
3333
},
3434
"dependencies": {
35-
"viem": "^2.21.53",
36-
"near-safe": "^0.9.6",
35+
"viem": "^2.22.21",
36+
"near-safe": "^0.9.10",
3737
"zerion-sdk": "^0.0.13"
3838
}
3939
}

packages/agent-sdk/src/evm/index.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
import type { MetaTransaction, SignRequestData } from "near-safe";
22
import { NearSafe } from "near-safe";
3-
import { getAddress, type Hex, zeroAddress, type Address } from "viem";
3+
import { getAddress, zeroAddress, isHex, toHex, toBytes } from "viem";
4+
import type { Address, Hex } from "viem";
45

56
export * from "./types";
67
export * from "./erc20";
78
export * from "./weth";
89
export * from "./tokens";
910
export * from "./safe";
1011

12+
export function hexifyValue(value: string): Hex {
13+
if (isHex(value)) {
14+
return value;
15+
}
16+
return toHex(toBytes(BigInt(value)));
17+
}
18+
1119
export function signRequestFor({
1220
from,
1321
chainId,
@@ -20,12 +28,14 @@ export function signRequestFor({
2028
return {
2129
method: "eth_sendTransaction",
2230
chainId,
23-
params: metaTransactions.map((mt) => ({
24-
from: from ?? zeroAddress,
25-
to: getAddress(mt.to),
26-
value: mt.value as Hex,
27-
data: mt.data as Hex,
28-
})),
31+
params: metaTransactions.map((mt) => {
32+
return {
33+
from: from ?? zeroAddress,
34+
to: getAddress(mt.to),
35+
value: hexifyValue(mt.value),
36+
data: mt.data as Hex,
37+
};
38+
}),
2939
};
3040
}
3141

packages/agent-sdk/src/evm/safe.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ export async function getSafeBalances(
5454
zerionKey?: string,
5555
): Promise<TokenBalance[]> {
5656
const baseUrl = safeTxServiceUrlFor(chainId);
57-
console.log(baseUrl);
5857
if (!baseUrl) {
5958
console.warn(
6059
`Chain ID ${chainId} not supported by Safe Transaction Service`,
@@ -66,7 +65,6 @@ export async function getSafeBalances(
6665
const url = `${baseUrl}/api/v1/safes/${checksumAddress(address)}/balances/?trusted=${trusted}&exclude_spam=${exclude_spam}`;
6766

6867
try {
69-
console.log(`Fetching Safe balances for ${address} from ${url}`);
7068
const response = await fetch(url, {
7169
headers: {
7270
accept: "application/json",
@@ -79,7 +77,6 @@ export async function getSafeBalances(
7977
throw new Error(`HTTP error! status: ${response.status}`);
8078
}
8179
const data: TokenBalance[] = await response.json();
82-
console.log(`Retrieved ${data.length} balances`);
8380
return data;
8481
} catch (error) {
8582
console.warn("Error fetching Safe balances:", error);

packages/agent-sdk/src/evm/tokens.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,8 @@ export async function getTokenDetails(
4141
return getTokenInfo(chainId, symbolOrAddress);
4242
}
4343
if (!tokenMap) {
44-
console.log(
45-
"Loading TokenMap... this should be stored in memory consider setting it with loadTokenMap() in your app and passing it here.",
46-
);
4744
tokenMap = await loadTokenMap();
4845
}
49-
console.log("Seeking TokenMap for Symbol", symbolOrAddress);
5046
// TokenMap has lower cased (sanitized) symbols
5147
return tokenMap[chainId][symbolOrAddress.toLowerCase()];
5248
}

packages/agent-sdk/src/request.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ export async function handleRequest<X, T, R>(
1313
): Promise<R> {
1414
try {
1515
const result = await logic(input);
16-
console.log("Responding with", JSON.stringify(result, null, 2));
1716
return responseTransformer(result);
1817
} catch (err: unknown) {
1918
const message = errorString(err);

packages/agent-sdk/tests/evm/index.spec.ts

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,44 @@ import {
33
fallbackResponder,
44
validateRequest,
55
} from "../../src/evm";
6-
import { zeroAddress } from "viem";
6+
import { getAddress, zeroAddress } from "viem";
77
import { NextRequest, NextResponse } from "next/server";
88
import type { BaseRequest } from "../../src/evm";
9+
import { hexifyValue } from "../../src/evm";
910

10-
// Mock external dependencies
11-
jest.mock("viem", () => ({
12-
getAddress: jest.fn().mockImplementation((address) => address),
13-
zeroAddress: "0x0000000000000000000000000000000000000000",
14-
}));
11+
const address = (i: number): `0x${string}` =>
12+
getAddress(`0x${i.toString(16).padStart(40, "0")}`);
13+
14+
const to = address(123);
15+
const from = address(456);
1516

1617
jest.mock("near-safe", () => ({
1718
NearSafe: {
1819
create: jest.fn().mockImplementation(async () => ({
19-
address: "0x123",
20+
address: to,
2021
})),
2122
},
2223
}));
2324

2425
describe("evm/index", () => {
26+
describe("hexifyValue", () => {
27+
it("does the thing", () => {
28+
expect(hexifyValue("0x00")).toEqual("0x00");
29+
30+
expect(hexifyValue("0")).toEqual("0x00");
31+
expect(hexifyValue("1")).toEqual("0x01");
32+
expect(hexifyValue("15")).toEqual("0x0f");
33+
expect(hexifyValue("16")).toEqual("0x10");
34+
expect(hexifyValue("17")).toEqual("0x11");
35+
expect(hexifyValue("255")).toEqual("0xff");
36+
expect(hexifyValue("256")).toEqual("0x0100");
37+
expect(hexifyValue("257")).toEqual("0x0101");
38+
expect(hexifyValue("256")).toEqual("0x0100");
39+
});
40+
});
2541
describe("signRequestFor", () => {
2642
it("creates a sign request with default from address", () => {
27-
const metaTransactions = [{ to: "0x123", value: "0x0", data: "0xabc" }];
43+
const metaTransactions = [{ to, value: "0x00", data: "0xabc" }];
2844

2945
const result = signRequestFor({
3046
chainId: 1,
@@ -37,19 +53,19 @@ describe("evm/index", () => {
3753
params: [
3854
{
3955
from: zeroAddress,
40-
to: "0x123",
41-
value: "0x0",
56+
to,
57+
value: "0x00",
4258
data: "0xabc",
4359
},
4460
],
4561
});
4662
});
4763

4864
it("creates a sign request with specified from address", () => {
49-
const metaTransactions = [{ to: "0x123", value: "0x0", data: "0xabc" }];
65+
const metaTransactions = [{ to, value: "0x0", data: "0xabc" }];
5066

5167
const result = signRequestFor({
52-
from: "0x456",
68+
from,
5369
chainId: 1,
5470
metaTransactions,
5571
});
@@ -59,8 +75,8 @@ describe("evm/index", () => {
5975
chainId: 1,
6076
params: [
6177
{
62-
from: "0x456",
63-
to: "0x123",
78+
from,
79+
to,
6480
value: "0x0",
6581
data: "0xabc",
6682
},
@@ -132,7 +148,7 @@ describe("evm/index", () => {
132148
headers: {
133149
get: jest.fn().mockReturnValue(
134150
JSON.stringify({
135-
evmAddress: "0x123",
151+
evmAddress: address(123),
136152
}),
137153
),
138154
},

packages/agent-sdk/tests/evm/safe.spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ describe("getSafeBalances", () => {
103103
1,
104104
"0x54F08c27e75BeA0cdDdb8aA9D69FD61551B19BbA",
105105
);
106-
console.log(result);
107106
expect(result).toEqual({
108107
balances: [
109108
{

0 commit comments

Comments
 (0)