Skip to content

Commit

Permalink
chore: bump celo test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
jxom committed Nov 5, 2023
1 parent c0da695 commit 494dc53
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 53 deletions.
106 changes: 106 additions & 0 deletions src/chains/celo/formatters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
44 changes: 23 additions & 21 deletions src/chains/celo/formatters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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
},
Expand Down
26 changes: 19 additions & 7 deletions src/chains/celo/serializers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
{
Expand Down Expand Up @@ -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(
{
Expand Down Expand Up @@ -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(
{
Expand Down Expand Up @@ -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)', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/chains/celo/serializers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ function serializeTransactionCIP64(
value ? toHex(value) : '0x',
data ?? '0x',
serializeAccessList(accessList),
feeCurrency ?? '0x',
feeCurrency!,
]

if (signature) {
Expand Down
32 changes: 18 additions & 14 deletions src/chains/celo/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,21 +135,25 @@ type RpcTransactionRequest = RpcTransactionRequest_ & {
gatewayFeeRecipient?: Address
}

export type RpcTransactionCIP42<TPending extends boolean = boolean> =
TransactionBase<Quantity, Index, TPending> &
FeeValuesEIP1559<Quantity> & {
feeCurrency: Address | null
gatewayFee: Hex | null
gatewayFeeRecipient: Address | null
type: '0x7c'
}
export type RpcTransactionCIP42<TPending extends boolean = boolean> = Omit<
TransactionBase<Quantity, Index, TPending>,
'typeHex'
> &
FeeValuesEIP1559<Quantity> & {
feeCurrency: Address | null
gatewayFee: Hex | null
gatewayFeeRecipient: Address | null
type: '0x7c'
}

export type RpcTransactionCIP64<TPending extends boolean = boolean> =
TransactionBase<Quantity, Index, TPending> &
FeeValuesEIP1559<Quantity> & {
feeCurrency: Address | null
type: '0x7b'
}
export type RpcTransactionCIP64<TPending extends boolean = boolean> = Omit<
TransactionBase<Quantity, Index, TPending>,
'typeHex'
> &
FeeValuesEIP1559<Quantity> & {
feeCurrency: Address | null
type: '0x7b'
}

export type RpcTransactionRequestCIP42 = TransactionRequestBase<
Quantity,
Expand Down
8 changes: 2 additions & 6 deletions src/chains/celo/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) &&
Expand All @@ -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) &&
Expand Down
8 changes: 4 additions & 4 deletions src/chains/zksync/formatters.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ describe('block', () => {
Assign<
Partial<RpcBlock>,
{
l1BatchNumber: `0x${string}`
l1BatchTimestamp: `0x${string}`
l1BatchNumber: `0x${string}` | null
l1BatchTimestamp: `0x${string}` | null
} & {
transactions: `0x${string}`[] | ZkSyncRpcTransaction[]
}
Expand Down Expand Up @@ -96,7 +96,7 @@ describe('transactionReceipt', () => {
undefined,
undefined
> & {
l1BatchNumber: bigint
l1BatchNumber: bigint | null
transactionLogIndex: number
logType: `0x${string}` | null
})[]
Expand All @@ -106,7 +106,7 @@ describe('transactionReceipt', () => {
ReturnType<
typeof formattersZkSync.transactionReceipt.format
>['logs'][0]['l1BatchNumber']
>().toEqualTypeOf<bigint>()
>().toEqualTypeOf<bigint | null>()
expectTypeOf<
ReturnType<
typeof formattersZkSync.transactionReceipt.format
Expand Down

0 comments on commit 494dc53

Please sign in to comment.