|
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"; |
2 | 9 | import { describe, expect, test } from "vitest";
|
3 | 10 | import { ANVIL_PKEY_A } from "~test/test-wallets.js";
|
| 11 | +import { fromGwei } from "../../utils/units.js"; |
4 | 12 | import { signTransaction } from "./sign-transaction.js";
|
5 | 13 |
|
6 | 14 | const BASE_TRANSACTION = {
|
7 | 15 | gas: 21000n,
|
8 | 16 | nonce: 785,
|
9 | 17 | } satisfies TransactionSerializableBase;
|
10 | 18 |
|
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", () => { |
13 | 89 | const signature = signTransaction({
|
14 | 90 | transaction: {
|
15 |
| - ...BASE_TRANSACTION, |
16 |
| - gasPrice: 1n, // default legacy transaction |
| 91 | + ...BASE_EIP1559_TRANSACTION, |
| 92 | + data: "0x1234", |
17 | 93 | },
|
18 | 94 | privateKey: ANVIL_PKEY_A,
|
19 | 95 | });
|
| 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 | + }); |
20 | 115 | expect(signature).toMatchInlineSnapshot(
|
21 |
| - '"0xf84d820311018252088080801ca0c8624e076e109748268ff37c3c7291f34b1eb8c576ea3558bbb689bc5390d1bba01d1c58180f52deac8dce8b3edaedd1dc72323131be27800f219a7ca045a00fd6"', |
| 116 | + '"0x01f84f0182031180825208808080c080a089cebce5c7f728febd1060b55837c894ec2a79dd7854350abce252fc2de96b5da039f2782c70b92f4b1916aa8db91453c7229f33458bd091b3e10a40f9a7e443d2"', |
22 | 117 | );
|
23 | 118 | });
|
24 | 119 |
|
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", () => { |
26 | 175 | expect(
|
27 | 176 | signTransaction({
|
28 | 177 | transaction: {
|
29 |
| - chainId: 1, |
30 |
| - maxFeePerGas: 1n, |
| 178 | + gasPrice: fromGwei("2"), |
31 | 179 | },
|
32 | 180 | privateKey: ANVIL_PKEY_A,
|
33 | 181 | }),
|
34 | 182 | ).toMatchInlineSnapshot(
|
35 |
| - '"0x02f84c0180800180808080c080a0c95f157628e67a435b1b85584db1b8346cbccf9890d28466f6edfed07d097793a03bdfde4f59a340a4308b5b4d2c89da83838d691c8059b228d0667309f1d2e893"', |
| 183 | + '"0xf84d808477359400808080801ba07abf45a28c3ce5a1d79d5ab5362878be5411ac51b3c2316670e1263936ef869ea001ca38d1782880bff3e2056f4949e75418858195b06fa8b6b13910a789e51989"', |
36 | 184 | );
|
37 | 185 | });
|
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 |
43 | 186 | });
|
0 commit comments