Skip to content

Commit

Permalink
Fix: cip64 regressions (#6)
Browse files Browse the repository at this point in the history
* fix: refactor cip64 to be more robust

* fix: types

* fix: signTransaction tests

* refactor: PR feedback
  • Loading branch information
nicolasbrugneaux committed Nov 2, 2023
1 parent 6f48bd0 commit 4d0c7a2
Show file tree
Hide file tree
Showing 7 changed files with 494 additions and 220 deletions.
35 changes: 32 additions & 3 deletions src/actions/wallet/signTransaction.test.ts
Expand Up @@ -343,7 +343,7 @@ describe('legacy', () => {
})
})

describe('custom (cip42)', () => {
describe('custom (cip64)', () => {
const walletClient = createWalletClient({
chain: celo,
transport: http(localHttpUrl),
Expand All @@ -358,10 +358,39 @@ describe('custom (cip42)', () => {
feeCurrency: '0x765de816845861e75a25fca122bb6898b8b1282a',
maxFeePerGas: parseGwei('20'),
maxPriorityFeePerGas: parseGwei('2'),
type: 'cip42',
}),
).toMatchInlineSnapshot(
'"0x7cf8700182031184773594008504a817c80082520894765de816845861e75a25fca122bb6898b8b1282a8080808080c080a05076fb030517e3243dd38850c21923f4343ed429db3ab032178f2d8702cad17fa07e860ca7bc7c2f34ece898c3e6912dcef4608ad3fc0b76bf0d760dcb608a71b2"',
'"0x7bf86e0182031184773594008504a817c800825208808080c094765de816845861e75a25fca122bb6898b8b1282a01a0ea3e86b0fd53ada619406822e96cf0dcec1e73b7a8bba60ad355fc8a8f4780e0a0399581f2dbfacab4d301a42e8773f6db217630e861b7d56ccbd333d553a6bb9c"',
)
})
})

describe('custom (cip42)', () => {
const walletClient = createWalletClient({
chain: celo,
transport: http(localHttpUrl),
})
const tx = {
account: privateKeyToAccount(sourceAccount.privateKey),
chain: null,
...base,
feeCurrency: '0x765de816845861e75a25fca122bb6898b8b1282a',
maxFeePerGas: parseGwei('20'),
maxPriorityFeePerGas: parseGwei('2'),
gatewayFee: 4n,
gatewayFeeRecipient: '0x0f16e9b0d03470827a95cdfd0cb8a8a3b46969b9',
} as const

test('default', async () => {
expect(await signTransaction(walletClient, tx)).toMatchInlineSnapshot(
'"0x7cf8840182031184773594008504a817c80082520894765de816845861e75a25fca122bb6898b8b1282a940f16e9b0d03470827a95cdfd0cb8a8a3b46969b904808080c001a062bee7f81cccd1f430b4b66ec5a23737d6fbee9965e63ac582d09f63aef32bdca05e75bd3ef63f2c0f6fd0a87e3f8d4809a38ac955b7d97fd4af1bd2c882999d5c"',
)
})
test('sanity', async () => {
expect(
await signTransaction(walletClient, { ...tx, type: 'cip42' }),
).toMatchInlineSnapshot(
'"0x7cf8840182031184773594008504a817c80082520894765de816845861e75a25fca122bb6898b8b1282a940f16e9b0d03470827a95cdfd0cb8a8a3b46969b904808080c001a062bee7f81cccd1f430b4b66ec5a23737d6fbee9965e63ac582d09f63aef32bdca05e75bd3ef63f2c0f6fd0a87e3f8d4809a38ac955b7d97fd4af1bd2c882999d5c"',
)
})
})
Expand Down
87 changes: 83 additions & 4 deletions src/chains/celo/formatters.test.ts
Expand Up @@ -751,9 +751,9 @@ describe('transactionReceipt', () => {
})

describe('transactionRequest', () => {
test('formatter', () => {
const { transactionRequest } = celo.formatters!
const { transactionRequest } = celo.formatters!

test('formatter cip42', () => {
expect(
transactionRequest.format({
feeCurrency: '0x0f16e9b0d03470827a95cdfd0cb8a8a3b46969b9',
Expand Down Expand Up @@ -819,7 +819,6 @@ describe('transactionRequest', () => {
maxFeePerGas: 2n,
maxPriorityFeePerGas: 1n,
nonce: 1,
type: 'cip42',
value: 1n,
}),
).toMatchInlineSnapshot(`
Expand All @@ -838,6 +837,63 @@ describe('transactionRequest', () => {
}
`)

expect(
transactionRequest.format({
feeCurrency: '0x0f16e9b0d03470827a95cdfd0cb8a8a3b46969b9',
from: '0x0f16e9b0d03470827a95cdfd0cb8a8a3b46969b9',
gas: 1n,
gatewayFee: 4n,
gatewayFeeRecipient: '0x0f16e9b0d03470827a95cdfd0cb8a8a3b46969b9',
maxFeePerGas: 2n,
maxPriorityFeePerGas: 1n,
nonce: 1,
value: 1n,
}),
).toMatchInlineSnapshot(`
{
"feeCurrency": "0x0f16e9b0d03470827a95cdfd0cb8a8a3b46969b9",
"from": "0x0f16e9b0d03470827a95cdfd0cb8a8a3b46969b9",
"gas": "0x1",
"gasPrice": undefined,
"gatewayFee": "0x4",
"gatewayFeeRecipient": "0x0f16e9b0d03470827a95cdfd0cb8a8a3b46969b9",
"maxFeePerGas": "0x2",
"maxPriorityFeePerGas": "0x1",
"nonce": "0x1",
"type": "0x7c",
"value": "0x1",
}
`)

expect(
transactionRequest.format({
feeCurrency: '0x0f16e9b0d03470827a95cdfd0cb8a8a3b46969b9',
from: '0x0f16e9b0d03470827a95cdfd0cb8a8a3b46969b9',
gas: 1n,
gatewayFee: 4n,
gatewayFeeRecipient: '0x0f16e9b0d03470827a95cdfd0cb8a8a3b46969b9',
maxFeePerGas: 2n,
maxPriorityFeePerGas: 4n,
nonce: 1,
value: 1n,
}),
).toMatchInlineSnapshot(`
{
"feeCurrency": "0x0f16e9b0d03470827a95cdfd0cb8a8a3b46969b9",
"from": "0x0f16e9b0d03470827a95cdfd0cb8a8a3b46969b9",
"gas": "0x1",
"gasPrice": undefined,
"gatewayFee": "0x4",
"gatewayFeeRecipient": "0x0f16e9b0d03470827a95cdfd0cb8a8a3b46969b9",
"maxFeePerGas": "0x2",
"maxPriorityFeePerGas": "0x4",
"nonce": "0x1",
"type": "0x7c",
"value": "0x1",
}
`)
})
test('formatter cip64', () => {
expect(
transactionRequest.format({
feeCurrency: '0x0f16e9b0d03470827a95cdfd0cb8a8a3b46969b9',
Expand All @@ -846,7 +902,6 @@ describe('transactionRequest', () => {
maxFeePerGas: 2n,
maxPriorityFeePerGas: 1n,
nonce: 1,
type: 'cip64',
value: 1n,
}),
).toMatchInlineSnapshot(`
Expand All @@ -862,5 +917,29 @@ describe('transactionRequest', () => {
"value": "0x1",
}
`)

expect(
transactionRequest.format({
from: '0x0f16e9b0d03470827a95cdfd0cb8a8a3b46969b9',
to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', // Recipient (illustrative address)
value: 1n,
feeCurrency: '0x874069Fa1Eb16D44d622F2e0Ca25eeA172369bC1', // cUSD fee currency
maxFeePerGas: 2n, // Special field for dynamic fee transaction type (EIP-1559)
maxPriorityFeePerGas: 2n, // Special field for dynamic fee transaction type (EIP-1559)
}),
).toMatchInlineSnapshot(`
{
"feeCurrency": "0x874069Fa1Eb16D44d622F2e0Ca25eeA172369bC1",
"from": "0x0f16e9b0d03470827a95cdfd0cb8a8a3b46969b9",
"gas": undefined,
"gasPrice": undefined,
"maxFeePerGas": "0x2",
"maxPriorityFeePerGas": "0x2",
"nonce": undefined,
"to": "0x70997970c51812dc3a010c7d01b50e0d17dc79c8",
"type": "0x7b",
"value": "0x1",
}
`)
})
})
21 changes: 3 additions & 18 deletions src/chains/celo/formatters.ts
Expand Up @@ -19,22 +19,7 @@ import type {
CeloTransactionReceiptOverrides,
CeloTransactionRequest,
} from './types.js'

function isTransactionRequestCIP64(args: CeloTransactionRequest): boolean {
if (args.type === 'cip64') return true
if (args.type) return false
return (
'feeCurrency' in args &&
args.gatewayFee === undefined &&
args.gatewayFeeRecipient === undefined
)
}

function isTransactionRequestCIP42(args: CeloTransactionRequest): boolean {
if (args.type === 'cip42') return true
if (args.type) return false
return args.gatewayFee !== undefined || args.gatewayFeeRecipient !== undefined
}
import { isCIP42, isCIP64 } from './utils.js'

export const formattersCelo = {
block: /*#__PURE__*/ defineBlock({
Expand Down Expand Up @@ -95,7 +80,7 @@ export const formattersCelo = {

transactionRequest: /*#__PURE__*/ defineTransactionRequest({
format(args: CeloTransactionRequest): CeloRpcTransactionRequest {
if (isTransactionRequestCIP64(args))
if (isCIP64(args))
return {
type: '0x7b',
feeCurrency: args.feeCurrency,
Expand All @@ -110,7 +95,7 @@ export const formattersCelo = {
gatewayFeeRecipient: args.gatewayFeeRecipient,
} as CeloRpcTransactionRequest

if (isTransactionRequestCIP42(args)) request.type = '0x7c'
if (isCIP42(args)) request.type = '0x7c'

return request
},
Expand Down

0 comments on commit 4d0c7a2

Please sign in to comment.