Skip to content

Commit ce144ff

Browse files
gregfromstljnsdls
andauthored
[SDK] Add test cases for sign-transaction types (#2737)
Co-authored-by: Jonas Daniels <jonas.daniels@outlook.com>
1 parent d836889 commit ce144ff

File tree

1 file changed

+158
-15
lines changed

1 file changed

+158
-15
lines changed
Lines changed: 158 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,186 @@
1-
import type { TransactionSerializableBase } from "viem";
1+
import {
2+
type TransactionSerializable,
3+
type TransactionSerializableBase,
4+
type TransactionSerializableEIP1559,
5+
type TransactionSerializableEIP2930,
6+
type TransactionSerializableLegacy,
7+
zeroAddress,
8+
} from "viem";
29
import { describe, expect, test } from "vitest";
310
import { ANVIL_PKEY_A } from "~test/test-wallets.js";
11+
import { fromGwei } from "../../utils/units.js";
412
import { signTransaction } from "./sign-transaction.js";
513

614
const BASE_TRANSACTION = {
715
gas: 21000n,
816
nonce: 785,
917
} satisfies TransactionSerializableBase;
1018

11-
describe("signTransaction", () => {
12-
test("default w/ legacy gasPrice", async () => {
19+
describe("eip1559", () => {
20+
const BASE_EIP1559_TRANSACTION = {
21+
...BASE_TRANSACTION,
22+
chainId: 1,
23+
type: "eip1559",
24+
} as const satisfies TransactionSerializableEIP1559;
25+
26+
test("default", () => {
27+
const signature = signTransaction({
28+
transaction: BASE_EIP1559_TRANSACTION,
29+
privateKey: ANVIL_PKEY_A,
30+
});
31+
32+
expect(signature).toMatchInlineSnapshot(
33+
'"0x02f850018203118080825208808080c080a04012522854168b27e5dc3d5839bab5e6b39e1a0ffd343901ce1622e3d64b48f1a04e00902ae0502c4728cbf12156290df99c3ed7de85b1dbfe20b5c36931733a33"',
34+
);
35+
});
36+
37+
test("with maxFeePerGas", () => {
38+
const signature = signTransaction({
39+
transaction: {
40+
...BASE_EIP1559_TRANSACTION,
41+
maxFeePerGas: 1n,
42+
},
43+
privateKey: ANVIL_PKEY_A,
44+
});
45+
46+
expect(signature).toMatchInlineSnapshot(
47+
'"0x02f850018203118001825208808080c080a02b89e41bbb3da8aa2edbc47596f9669c472a1f604795247ea074a5dd7f46c97aa04070caf004ae82551a3d55caceacc50ae309883298c84105505b0cc31b3ec6d6"',
48+
);
49+
});
50+
51+
test("with maxPriorityFeePerGas", () => {
52+
const signature = signTransaction({
53+
transaction: {
54+
...BASE_EIP1559_TRANSACTION,
55+
maxFeePerGas: fromGwei("20"),
56+
maxPriorityFeePerGas: fromGwei("2"),
57+
},
58+
privateKey: ANVIL_PKEY_A,
59+
});
60+
61+
expect(signature).toMatchInlineSnapshot(
62+
'"0x02f8590182031184773594008504a817c800825208808080c001a06ea33b188b30a5f5d0d1cec62b2bac7203ff428a49048766596727737689043fa0255b74c8e704e3692497a29cd246ffc4344b4107457ce1c914fe2b4e09993859"',
63+
);
64+
});
65+
66+
test("with accessList", () => {
67+
const signature = signTransaction({
68+
transaction: {
69+
...BASE_EIP1559_TRANSACTION,
70+
accessList: [
71+
{
72+
address: zeroAddress,
73+
storageKeys: [
74+
"0x0000000000000000000000000000000000000000000000000000000000000001",
75+
"0x60fdd29ff912ce880cd3edaf9f932dc61d3dae823ea77e0323f94adb9f6a72fe",
76+
],
77+
},
78+
],
79+
},
80+
privateKey: ANVIL_PKEY_A,
81+
});
82+
83+
expect(signature).toMatchInlineSnapshot(
84+
'"0x02f8ac018203118080825208808080f85bf859940000000000000000000000000000000000000000f842a00000000000000000000000000000000000000000000000000000000000000001a060fdd29ff912ce880cd3edaf9f932dc61d3dae823ea77e0323f94adb9f6a72fe01a0ab47ceec8673fd579fe961f3ee8f31e6e06985fccdde9ad61d03d304bd37d71da0670b6469de6010c8685e4c849a0eb4d5f8f782cab989296101c81c3e81fd3c0d"',
85+
);
86+
});
87+
88+
test("with data", () => {
1389
const signature = signTransaction({
1490
transaction: {
15-
...BASE_TRANSACTION,
16-
gasPrice: 1n, // default legacy transaction
91+
...BASE_EIP1559_TRANSACTION,
92+
data: "0x1234",
1793
},
1894
privateKey: ANVIL_PKEY_A,
1995
});
96+
97+
expect(signature).toMatchInlineSnapshot(
98+
'"0x02f8520182031180808252088080821234c001a054d552c58a162c9003633c20871d8e381ef7a3c35d1c8a79c7c12d5cf09a0914a03c5d6241f8c4fcf8b35262de038d3ab1940feb1a70b934ae5d40ea6bce912e2d"',
99+
);
100+
});
101+
});
102+
103+
describe("eip2930", () => {
104+
const BASE_EIP2930_TRANSACTION = {
105+
...BASE_TRANSACTION,
106+
chainId: 1,
107+
type: "eip2930",
108+
} as const satisfies TransactionSerializable;
109+
110+
test("default", () => {
111+
const signature = signTransaction({
112+
transaction: BASE_EIP2930_TRANSACTION,
113+
privateKey: ANVIL_PKEY_A,
114+
});
20115
expect(signature).toMatchInlineSnapshot(
21-
'"0xf84d820311018252088080801ca0c8624e076e109748268ff37c3c7291f34b1eb8c576ea3558bbb689bc5390d1bba01d1c58180f52deac8dce8b3edaedd1dc72323131be27800f219a7ca045a00fd6"',
116+
'"0x01f84f0182031180825208808080c080a089cebce5c7f728febd1060b55837c894ec2a79dd7854350abce252fc2de96b5da039f2782c70b92f4b1916aa8db91453c7229f33458bd091b3e10a40f9a7e443d2"',
22117
);
23118
});
24119

25-
test("minimal w/ maxFeePerGas", async () => {
120+
test("with accessList and gasPrice", () => {
121+
const signature = signTransaction({
122+
transaction: {
123+
...BASE_EIP2930_TRANSACTION,
124+
gasPrice: fromGwei("2"),
125+
accessList: [
126+
{
127+
address: zeroAddress,
128+
storageKeys: [
129+
"0x0000000000000000000000000000000000000000000000000000000000000000",
130+
],
131+
},
132+
],
133+
} as TransactionSerializableEIP2930,
134+
privateKey: ANVIL_PKEY_A,
135+
});
136+
137+
expect(signature).toMatchInlineSnapshot(
138+
'"0x01f88c018203118477359400825208808080f838f7940000000000000000000000000000000000000000e1a0000000000000000000000000000000000000000000000000000000000000000001a0da12dbd793012df6fcf099460ebcc4d5f6bccdc86218b35389b387d479c27129a045f1ccd6edad0727b47a1985469a3a101c31378395bc0b045d55357e7a19baf0"',
139+
);
140+
});
141+
142+
test("with data", () => {
143+
const signature = signTransaction({
144+
transaction: {
145+
...BASE_EIP2930_TRANSACTION,
146+
data: "0x1234",
147+
},
148+
privateKey: ANVIL_PKEY_A,
149+
});
150+
151+
expect(signature).toMatchInlineSnapshot(
152+
'"0x01f85101820311808252088080821234c080a084fdcea5fe55ce8378aa94a8d4a9c01545d59922f1edcdd89a71ebf740dc0bf5a0539a4ab61a42509a6b4c35c85099d8b7b8e815967f0c832c868327caca6307cb"',
153+
);
154+
});
155+
});
156+
157+
describe("legacy", () => {
158+
const BASE_LEGACY_TRANSACTION = {
159+
...BASE_TRANSACTION,
160+
gasPrice: fromGwei("2"),
161+
type: "legacy",
162+
} as const satisfies TransactionSerializableLegacy;
163+
164+
test("default", () => {
165+
const signature = signTransaction({
166+
transaction: BASE_LEGACY_TRANSACTION,
167+
privateKey: ANVIL_PKEY_A,
168+
});
169+
expect(signature).toMatchInlineSnapshot(
170+
'"0xf85182031184773594008252088080801ba0462e5dabe6d0e82ac9d2832d5ecc815e317669ae2eb018c2a07ae6f3a4763618a003214adcddee51ee1d46cb12a694f5520c851581fe53c543c8999d45fa18de07"',
171+
);
172+
});
173+
174+
test("with gasPrice", () => {
26175
expect(
27176
signTransaction({
28177
transaction: {
29-
chainId: 1,
30-
maxFeePerGas: 1n,
178+
gasPrice: fromGwei("2"),
31179
},
32180
privateKey: ANVIL_PKEY_A,
33181
}),
34182
).toMatchInlineSnapshot(
35-
'"0x02f84c0180800180808080c080a0c95f157628e67a435b1b85584db1b8346cbccf9890d28466f6edfed07d097793a03bdfde4f59a340a4308b5b4d2c89da83838d691c8059b228d0667309f1d2e893"',
183+
'"0xf84d808477359400808080801ba07abf45a28c3ce5a1d79d5ab5362878be5411ac51b3c2316670e1263936ef869ea001ca38d1782880bff3e2056f4949e75418858195b06fa8b6b13910a789e51989"',
36184
);
37185
});
38-
39-
// TODO: Add eip1559 test suite
40-
// TODO: Add eip2930 test suite
41-
// TODO: Add eip4844 test suite
42-
// TODO: Add legacy transaction test suite
43186
});

0 commit comments

Comments
 (0)