Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: unable to send transaction with 0 as nonce #383

Merged
merged 4 commits into from
Apr 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/two-ties-sort.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"viem": patch
---

Fixed an issue where `serializeTransaction` was incorrectly encoding zero-ish properties.
65 changes: 65 additions & 0 deletions src/utils/transaction/serializeTransaction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,28 @@ describe('eip1559', () => {
})
})

test('default (all zeros)', () => {
const baseEip1559Zero = {
to: accounts[1].address,
nonce: 0,
chainId: 1,
maxFeePerGas: 0n,
maxPriorityFeePerGas: 0n,
value: 0n,
} satisfies TransactionSerializableEIP1559

const serialized = serializeTransaction(baseEip1559Zero)

expect(serialized).toEqual(
'0x02dd01808080809470997970c51812dc3a010c7d01b50e0d17dc79c88080c0',
)
expect(parseTransaction(serialized)).toEqual({
chainId: 1,
to: accounts[1].address,
type: 'eip1559',
})
})

test('minimal (w/ maxFeePerGas)', () => {
const args = {
chainId: 1,
Expand Down Expand Up @@ -223,6 +245,29 @@ describe('eip2930', () => {
})
})

test('default (all zeros)', () => {
const baseEip2930Zero = {
to: accounts[1].address,
nonce: 0,
chainId: 1,
value: 0n,
gasPrice: 0n,
accessList: [],
} satisfies TransactionSerializableEIP2930

const serialized = serializeTransaction(baseEip2930Zero)

expect(serialized).toEqual(
'0x01dc018080809470997970c51812dc3a010c7d01b50e0d17dc79c88080c0',
)

expect(parseTransaction(serialized)).toEqual({
chainId: 1,
to: accounts[1].address,
type: 'eip2930',
})
})

test('minimal (w/ accessList & gasPrice)', () => {
const args = {
chainId: 1,
Expand Down Expand Up @@ -384,6 +429,26 @@ describe('legacy', () => {
})
})

test('default (all zeros)', () => {
const baseLegacyZero = {
to: accounts[1].address,
nonce: 0,
value: 0n,
gasPrice: 0n,
} satisfies TransactionSerializableLegacy

const serialized = serializeTransaction(baseLegacyZero)

expect(serialized).toEqual(
'0xda8080809470997970c51812dc3a010c7d01b50e0d17dc79c88080',
)

expect(parseTransaction(serialized)).toEqual({
to: accounts[1].address,
type: 'legacy',
})
})

test('minimal (w/ gasPrice)', () => {
const args = {
gasPrice: parseGwei('2'),
Expand Down
28 changes: 13 additions & 15 deletions src/utils/transaction/serializeTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,12 @@ function serializeTransactionEIP1559(

const serializedTransaction = [
toHex(chainId),
typeof nonce !== 'undefined' ? toHex(nonce) : '0x',
typeof maxPriorityFeePerGas !== 'undefined'
? toHex(maxPriorityFeePerGas)
: '0x',
typeof maxFeePerGas !== 'undefined' ? toHex(maxFeePerGas) : '0x',
typeof gas !== 'undefined' ? toHex(gas) : '0x',
nonce ? toHex(nonce) : '0x',
maxPriorityFeePerGas ? toHex(maxPriorityFeePerGas) : '0x',
maxFeePerGas ? toHex(maxFeePerGas) : '0x',
gas ? toHex(gas) : '0x',
to ?? '0x',
typeof value !== 'undefined' ? toHex(value) : '0x',
value ? toHex(value) : '0x',
data ?? '0x',
serializedAccessList,
]
Expand Down Expand Up @@ -120,11 +118,11 @@ function serializeTransactionEIP2930(

const serializedTransaction = [
toHex(chainId),
typeof nonce !== 'undefined' ? toHex(nonce) : '0x',
typeof gasPrice !== 'undefined' ? toHex(gasPrice) : '0x',
typeof gas !== 'undefined' ? toHex(gas) : '0x',
nonce ? toHex(nonce) : '0x',
gasPrice ? toHex(gasPrice) : '0x',
gas ? toHex(gas) : '0x',
to ?? '0x',
typeof value !== 'undefined' ? toHex(value) : '0x',
value ? toHex(value) : '0x',
data ?? '0x',
serializedAccessList,
]
Expand All @@ -151,11 +149,11 @@ function serializeTransactionLegacy(
assertTransactionLegacy(transaction)

let serializedTransaction = [
typeof nonce !== 'undefined' ? toHex(nonce) : '0x',
typeof gasPrice !== 'undefined' ? toHex(gasPrice) : '0x',
typeof gas !== 'undefined' ? toHex(gas) : '0x',
nonce ? toHex(nonce) : '0x',
gasPrice ? toHex(gasPrice) : '0x',
gas ? toHex(gas) : '0x',
to ?? '0x',
typeof value !== 'undefined' ? toHex(value) : '0x',
value ? toHex(value) : '0x',
data ?? '0x',
]

Expand Down