Skip to content

Commit

Permalink
fix: refactor cip64 to be more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasbrugneaux committed Oct 31, 2023
1 parent 6f48bd0 commit 3e41cd9
Show file tree
Hide file tree
Showing 6 changed files with 452 additions and 206 deletions.
82 changes: 80 additions & 2 deletions src/chains/celo/formatters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -819,7 +819,6 @@ describe('transactionRequest', () => {
maxFeePerGas: 2n,
maxPriorityFeePerGas: 1n,
nonce: 1,
type: 'cip42',
value: 1n,
}),
).toMatchInlineSnapshot(`
Expand All @@ -846,7 +845,6 @@ describe('transactionRequest', () => {
maxFeePerGas: 2n,
maxPriorityFeePerGas: 1n,
nonce: 1,
type: 'cip64',
value: 1n,
}),
).toMatchInlineSnapshot(`
Expand All @@ -862,5 +860,85 @@ 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",
}
`)

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",
}
`)
})
})
21 changes: 3 additions & 18 deletions src/chains/celo/formatters.ts
Original file line number Diff line number Diff line change
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
Loading

0 comments on commit 3e41cd9

Please sign in to comment.