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: cip64 regressions #6

Merged
merged 4 commits into from
Nov 2, 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
35 changes: 32 additions & 3 deletions src/actions/wallet/signTransaction.test.ts
Original file line number Diff line number Diff line change
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
Copy link

@arthurgousset arthurgousset Nov 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: (not related to changes in this PR)

Based on this recent Slack convo with @mcortesi, transaction receipts do not include feeCurrency, gatewayFee, or gatewayFeeRecipient fields, which means they are undefined in every transaction receipt object (by default). We can thus look into removing them from the expected and actual transaction receipt (in tests), because they will always trivially be equal.

I suggest we fix this in a separate PR to keep this one clean and get the regression fix merged as soon as possible.

describe('transactionReceipt', () => {
  test('formatter', () => {
    const { transactionReceipt } = celo.formatters!
    expect(
      transactionReceipt.format({
        blockHash:
          '0x89644bbd5c8d682a2e9611170e6c1f02573d866d286f006cbf517eec7254ec2d',
        blockNumber: '0x1',
        contractAddress: '0xa152f8bb749c55e9943a3a0a3111d18ee2b3f94e',
        cumulativeGasUsed: '0x2',
        effectiveGasPrice: '0x3',
-       feeCurrency: null,
        from: '0xa152f8bb749c55e9943a3a0a3111d18ee2b3f94e',
        gasUsed: '0x4',
-       gatewayFee: null,
-       gatewayFeeRecipient: null,
        logs: [],
        to: '0x15d4c048f83bd7e37d49ea4c83a07267ec4203da',
        status: '0x0',
        type: '0x0',
      }),
    ).toMatchInlineSnapshot(`
      {
        "blockHash": "0x89644bbd5c8d682a2e9611170e6c1f02573d866d286f006cbf517eec7254ec2d",
        "blockNumber": 1n,
        "contractAddress": "0xa152f8bb749c55e9943a3a0a3111d18ee2b3f94e",
        "cumulativeGasUsed": 2n,
        "effectiveGasPrice": 3n,
-       "feeCurrency": null,
        "from": "0xa152f8bb749c55e9943a3a0a3111d18ee2b3f94e",
        "gasUsed": 4n,
-       "gatewayFee": null,
-       "gatewayFeeRecipient": null,
        "logs": [],
        "status": "reverted",
        "to": "0x15d4c048f83bd7e37d49ea4c83a07267ec4203da",
        "transactionIndex": null,
        "type": "legacy",
      }
    `)

Original file line number Diff line number Diff line change
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(
nicolasbrugneaux marked this conversation as resolved.
Show resolved Hide resolved
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
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
Loading