From 494dc538129e49fa8fa01b7e6d073605b0f00bde Mon Sep 17 00:00:00 2001 From: "moxey.eth" Date: Mon, 6 Nov 2023 08:12:24 +1100 Subject: [PATCH] chore: bump celo test coverage --- src/chains/celo/formatters.test.ts | 106 +++++++++++++++++++++++++ src/chains/celo/formatters.ts | 44 +++++----- src/chains/celo/serializers.test.ts | 26 ++++-- src/chains/celo/serializers.ts | 2 +- src/chains/celo/types.ts | 32 ++++---- src/chains/celo/utils.ts | 8 +- src/chains/zksync/formatters.test-d.ts | 8 +- 7 files changed, 173 insertions(+), 53 deletions(-) diff --git a/src/chains/celo/formatters.test.ts b/src/chains/celo/formatters.test.ts index 3f1c066447..8e0b2dbfdb 100644 --- a/src/chains/celo/formatters.test.ts +++ b/src/chains/celo/formatters.test.ts @@ -399,6 +399,112 @@ describe('transaction', () => { "value": 0n, } `) + + expect( + transaction.format({ + accessList: [], + blockHash: + '0x89644bbd5c8d682a2e9611170e6c1f02573d866d286f006cbf517eec7254ec2d', + blockNumber: '0x1', + chainId: '0x1', + feeCurrency: '0x0f16e9b0d03470827a95cdfd0cb8a8a3b46969b9', + from: '0xa152f8bb749c55e9943a3a0a3111d18ee2b3f94e', + gas: '0x2', + gasPrice: undefined, + gatewayFee: '0x3', + gatewayFeeRecipient: null, + hash: '0xa4b1f606b66105fa45cb5db23d2f6597075701e7f0e2367f4e6a39d17a8cf98b', + input: + '0x23b872dd000000000000000000000000a00f99bc38b1ecda1fd70eaa1cd31d576a9f46b0000000000000000000000000f16e9b0d03470827a95cdfd0cb8a8a3b46969b910000000000000000000000000000000000000000000000000000002b3b6fb3d0', + maxFeePerGas: '0x4', + maxPriorityFeePerGas: '0x5', + nonce: '0x6', + r: '0x5e49a7bd0534c6b6d3bbe581659424d3747f920d40ce56e48d26e5d94aac32ca', + s: '0x1746abe27b7c4f00bda1ec714ac1f7083e9025b6ca3b2248e439a173e4ab55e0', + to: '0x15d4c048f83bd7e37d49ea4c83a07267ec4203da', + transactionIndex: '0x7', + type: '0x7c', + v: '0x1', + value: '0x0', + }), + ).toMatchInlineSnapshot(` + { + "accessList": [], + "blockHash": "0x89644bbd5c8d682a2e9611170e6c1f02573d866d286f006cbf517eec7254ec2d", + "blockNumber": 1n, + "chainId": 1, + "feeCurrency": "0x0f16e9b0d03470827a95cdfd0cb8a8a3b46969b9", + "from": "0xa152f8bb749c55e9943a3a0a3111d18ee2b3f94e", + "gas": 2n, + "gasPrice": undefined, + "gatewayFee": 3n, + "gatewayFeeRecipient": null, + "hash": "0xa4b1f606b66105fa45cb5db23d2f6597075701e7f0e2367f4e6a39d17a8cf98b", + "input": "0x23b872dd000000000000000000000000a00f99bc38b1ecda1fd70eaa1cd31d576a9f46b0000000000000000000000000f16e9b0d03470827a95cdfd0cb8a8a3b46969b910000000000000000000000000000000000000000000000000000002b3b6fb3d0", + "maxFeePerGas": 4n, + "maxPriorityFeePerGas": 5n, + "nonce": 6, + "r": "0x5e49a7bd0534c6b6d3bbe581659424d3747f920d40ce56e48d26e5d94aac32ca", + "s": "0x1746abe27b7c4f00bda1ec714ac1f7083e9025b6ca3b2248e439a173e4ab55e0", + "to": "0x15d4c048f83bd7e37d49ea4c83a07267ec4203da", + "transactionIndex": 7, + "type": "cip42", + "typeHex": "0x7c", + "v": 1n, + "value": 0n, + } + `) + + expect( + transaction.format({ + accessList: [], + blockHash: + '0x89644bbd5c8d682a2e9611170e6c1f02573d866d286f006cbf517eec7254ec2d', + blockNumber: '0x1', + chainId: '0x1', + feeCurrency: '0x0f16e9b0d03470827a95cdfd0cb8a8a3b46969b9', + from: '0xa152f8bb749c55e9943a3a0a3111d18ee2b3f94e', + gas: '0x2', + gasPrice: undefined, + hash: '0xa4b1f606b66105fa45cb5db23d2f6597075701e7f0e2367f4e6a39d17a8cf98b', + input: + '0x23b872dd000000000000000000000000a00f99bc38b1ecda1fd70eaa1cd31d576a9f46b0000000000000000000000000f16e9b0d03470827a95cdfd0cb8a8a3b46969b910000000000000000000000000000000000000000000000000000002b3b6fb3d0', + maxFeePerGas: '0x4', + maxPriorityFeePerGas: '0x5', + nonce: '0x6', + r: '0x5e49a7bd0534c6b6d3bbe581659424d3747f920d40ce56e48d26e5d94aac32ca', + s: '0x1746abe27b7c4f00bda1ec714ac1f7083e9025b6ca3b2248e439a173e4ab55e0', + to: '0x15d4c048f83bd7e37d49ea4c83a07267ec4203da', + transactionIndex: '0x7', + type: '0x7b', + v: '0x1', + value: '0x0', + }), + ).toMatchInlineSnapshot(` + { + "accessList": [], + "blockHash": "0x89644bbd5c8d682a2e9611170e6c1f02573d866d286f006cbf517eec7254ec2d", + "blockNumber": 1n, + "chainId": 1, + "feeCurrency": "0x0f16e9b0d03470827a95cdfd0cb8a8a3b46969b9", + "from": "0xa152f8bb749c55e9943a3a0a3111d18ee2b3f94e", + "gas": 2n, + "gasPrice": undefined, + "hash": "0xa4b1f606b66105fa45cb5db23d2f6597075701e7f0e2367f4e6a39d17a8cf98b", + "input": "0x23b872dd000000000000000000000000a00f99bc38b1ecda1fd70eaa1cd31d576a9f46b0000000000000000000000000f16e9b0d03470827a95cdfd0cb8a8a3b46969b910000000000000000000000000000000000000000000000000000002b3b6fb3d0", + "maxFeePerGas": 4n, + "maxPriorityFeePerGas": 5n, + "nonce": 6, + "r": "0x5e49a7bd0534c6b6d3bbe581659424d3747f920d40ce56e48d26e5d94aac32ca", + "s": "0x1746abe27b7c4f00bda1ec714ac1f7083e9025b6ca3b2248e439a173e4ab55e0", + "to": "0x15d4c048f83bd7e37d49ea4c83a07267ec4203da", + "transactionIndex": 7, + "type": "cip64", + "typeHex": "0x7b", + "v": 1n, + "value": 0n, + } + `) }) test('action', async () => { diff --git a/src/chains/celo/formatters.ts b/src/chains/celo/formatters.ts index 4f9201d70d..c7110049b0 100644 --- a/src/chains/celo/formatters.ts +++ b/src/chains/celo/formatters.ts @@ -55,15 +55,19 @@ export const formattersCelo = { }), transaction: /*#__PURE__*/ defineTransaction({ format(args: CeloRpcTransaction): CeloTransaction { - if (args.type === '0x7b') - return { - feeCurrency: args.feeCurrency, - } as CeloTransaction - return { - feeCurrency: args.feeCurrency, - gatewayFee: args.gatewayFee ? hexToBigInt(args.gatewayFee) : null, - gatewayFeeRecipient: args.gatewayFeeRecipient, - } as CeloTransaction + const transaction = { feeCurrency: args.feeCurrency } as CeloTransaction + + if (args.type === '0x7b') transaction.type = 'cip64' + else { + if (args.type === '0x7c') transaction.type = 'cip42' + + transaction.gatewayFee = args.gatewayFee + ? hexToBigInt(args.gatewayFee) + : null + transaction.gatewayFeeRecipient = args.gatewayFeeRecipient + } + + return transaction }, }), transactionReceipt: /*#__PURE__*/ defineTransactionReceipt({ @@ -80,22 +84,20 @@ export const formattersCelo = { transactionRequest: /*#__PURE__*/ defineTransactionRequest({ format(args: CeloTransactionRequest): CeloRpcTransactionRequest { - if (isCIP64(args)) - return { - type: '0x7b', - feeCurrency: args.feeCurrency, - } as CeloRpcTransactionRequest - const request = { feeCurrency: args.feeCurrency, - gatewayFee: - typeof args.gatewayFee !== 'undefined' - ? numberToHex(args.gatewayFee) - : undefined, - gatewayFeeRecipient: args.gatewayFeeRecipient, } as CeloRpcTransactionRequest - if (isCIP42(args)) request.type = '0x7c' + if (isCIP64(args)) request.type = '0x7b' + else { + if (isCIP42(args)) request.type = '0x7c' + + request.gatewayFee = + typeof args.gatewayFee !== 'undefined' + ? numberToHex(args.gatewayFee) + : undefined + request.gatewayFeeRecipient = args.gatewayFeeRecipient + } return request }, diff --git a/src/chains/celo/serializers.test.ts b/src/chains/celo/serializers.test.ts index 397546660d..26dfb2d494 100644 --- a/src/chains/celo/serializers.test.ts +++ b/src/chains/celo/serializers.test.ts @@ -130,11 +130,12 @@ describe('cip42', () => { ) }) - test('args: gatewayFee', () => { + test('args: gatewayFee (absent)', () => { const transaction: TransactionSerializableCIP42 = { ...baseCip42, - gatewayFeeRecipient: accounts[5].address, - gatewayFee: parseEther('0.1'), + gatewayFeeRecipient: undefined, + gatewayFee: undefined, + type: 'cip42', } expect(parseTransactionCelo(serializeTransactionCelo(transaction))).toEqual( { @@ -172,10 +173,10 @@ describe('cip42', () => { ) }) - test('args: nonce', () => { + test('args: nonce (absent)', () => { const transaction: TransactionSerializableCIP42 = { ...baseCip42, - nonce: 20, + nonce: undefined, } expect(parseTransactionCelo(serializeTransactionCelo(transaction))).toEqual( { @@ -376,10 +377,10 @@ describe('cip64', () => { ) }) - test('args: nonce', () => { + test('args: nonce (absent)', () => { const transaction: TransactionSerializableCIP64 = { ...baseCip64, - nonce: 20, + nonce: undefined, } expect(parseTransactionCelo(serializeTransactionCelo(transaction))).toEqual( { @@ -592,6 +593,17 @@ describe('Common invalid params (for CIP-42)', () => { `Chain ID "${-1}" is invalid.`, ) }) + + test('maxPriorityFeePerGas is higher than maxPriorityFee', () => { + const transaction: TransactionSerializableCIP42 = { + ...baseCip42, + maxPriorityFeePerGas: parseGwei('5000000000'), + maxFeePerGas: parseGwei('1'), + } + expect(() => serializeTransactionCelo(transaction)).toThrowError( + TipAboveFeeCapError, + ) + }) }) describe('Common invalid params (for CIP-64)', () => { diff --git a/src/chains/celo/serializers.ts b/src/chains/celo/serializers.ts index dd315369e2..2cc385e350 100644 --- a/src/chains/celo/serializers.ts +++ b/src/chains/celo/serializers.ts @@ -126,7 +126,7 @@ function serializeTransactionCIP64( value ? toHex(value) : '0x', data ?? '0x', serializeAccessList(accessList), - feeCurrency ?? '0x', + feeCurrency!, ] if (signature) { diff --git a/src/chains/celo/types.ts b/src/chains/celo/types.ts index 0cf0c4dd77..e00ebe858c 100644 --- a/src/chains/celo/types.ts +++ b/src/chains/celo/types.ts @@ -135,21 +135,25 @@ type RpcTransactionRequest = RpcTransactionRequest_ & { gatewayFeeRecipient?: Address } -export type RpcTransactionCIP42 = - TransactionBase & - FeeValuesEIP1559 & { - feeCurrency: Address | null - gatewayFee: Hex | null - gatewayFeeRecipient: Address | null - type: '0x7c' - } +export type RpcTransactionCIP42 = Omit< + TransactionBase, + 'typeHex' +> & + FeeValuesEIP1559 & { + feeCurrency: Address | null + gatewayFee: Hex | null + gatewayFeeRecipient: Address | null + type: '0x7c' + } -export type RpcTransactionCIP64 = - TransactionBase & - FeeValuesEIP1559 & { - feeCurrency: Address | null - type: '0x7b' - } +export type RpcTransactionCIP64 = Omit< + TransactionBase, + 'typeHex' +> & + FeeValuesEIP1559 & { + feeCurrency: Address | null + type: '0x7b' + } export type RpcTransactionRequestCIP42 = TransactionRequestBase< Quantity, diff --git a/src/chains/celo/utils.ts b/src/chains/celo/utils.ts index 97d82e6aa4..874de53272 100644 --- a/src/chains/celo/utils.ts +++ b/src/chains/celo/utils.ts @@ -43,9 +43,7 @@ export function isCIP42( transaction: CeloTransactionSerializable | CeloTransactionRequest, ): transaction is TransactionSerializableCIP42 { // Enable end-user to force the tx to be considered as a cip42 - if (transaction.type === 'cip42') { - return true - } + if (transaction.type) return transaction.type === 'cip42' return ( isEIP1559(transaction) && @@ -59,9 +57,7 @@ export function isCIP64( transaction: CeloTransactionSerializable | CeloTransactionRequest, ): transaction is TransactionSerializableCIP64 { // Enable end-user to force the tx to be considered as a cip64 - if (transaction.type === 'cip64') { - return true - } + if (transaction.type) return transaction.type === 'cip64' return ( isEIP1559(transaction) && diff --git a/src/chains/zksync/formatters.test-d.ts b/src/chains/zksync/formatters.test-d.ts index a7faa72069..1fbad3cfdf 100644 --- a/src/chains/zksync/formatters.test-d.ts +++ b/src/chains/zksync/formatters.test-d.ts @@ -31,8 +31,8 @@ describe('block', () => { Assign< Partial, { - l1BatchNumber: `0x${string}` - l1BatchTimestamp: `0x${string}` + l1BatchNumber: `0x${string}` | null + l1BatchTimestamp: `0x${string}` | null } & { transactions: `0x${string}`[] | ZkSyncRpcTransaction[] } @@ -96,7 +96,7 @@ describe('transactionReceipt', () => { undefined, undefined > & { - l1BatchNumber: bigint + l1BatchNumber: bigint | null transactionLogIndex: number logType: `0x${string}` | null })[] @@ -106,7 +106,7 @@ describe('transactionReceipt', () => { ReturnType< typeof formattersZkSync.transactionReceipt.format >['logs'][0]['l1BatchNumber'] - >().toEqualTypeOf() + >().toEqualTypeOf() expectTypeOf< ReturnType< typeof formattersZkSync.transactionReceipt.format