Skip to content

Commit

Permalink
Remove CIP-42 support (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
shazarre authored Apr 26, 2024
1 parent 978ece2 commit baa5116
Show file tree
Hide file tree
Showing 13 changed files with 153 additions and 599 deletions.
7 changes: 7 additions & 0 deletions .changeset/rude-vans-decide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"viem": minor
---

Removed CIP-42 support.

Any transactions with feeCurrency field will be treated as CIP-64.
27 changes: 11 additions & 16 deletions src/celo/formatters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -568,12 +568,13 @@ describe('transaction', () => {
describe('transactionRequest', () => {
const { transactionRequest } = celo.formatters!

test('formatter cip42', () => {
test('formatter CIP-42 (removed, should produce CIP-64 when feeCurrency specified, EIP-1559 otherwise)', () => {
expect(
transactionRequest.format({
feeCurrency: '0x0f16e9b0d03470827a95cdfd0cb8a8a3b46969b9',
from: '0x0f16e9b0d03470827a95cdfd0cb8a8a3b46969b9',
gas: 1n,
// @ts-ignore
gatewayFee: 4n,
gatewayFeeRecipient: '0x0f16e9b0d03470827a95cdfd0cb8a8a3b46969b9',
maxFeePerGas: 2n,
Expand All @@ -586,12 +587,10 @@ describe('transactionRequest', () => {
"feeCurrency": "0x0f16e9b0d03470827a95cdfd0cb8a8a3b46969b9",
"from": "0x0f16e9b0d03470827a95cdfd0cb8a8a3b46969b9",
"gas": "0x1",
"gatewayFee": "0x4",
"gatewayFeeRecipient": "0x0f16e9b0d03470827a95cdfd0cb8a8a3b46969b9",
"maxFeePerGas": "0x2",
"maxPriorityFeePerGas": "0x1",
"nonce": "0x1",
"type": "0x7c",
"type": "0x7b",
"value": "0x1",
}
`)
Expand All @@ -601,6 +600,7 @@ describe('transactionRequest', () => {
feeCurrency: '0x0f16e9b0d03470827a95cdfd0cb8a8a3b46969b9',
from: '0x0f16e9b0d03470827a95cdfd0cb8a8a3b46969b9',
gas: 1n,
// @ts-ignore
gatewayFeeRecipient: '0x0f16e9b0d03470827a95cdfd0cb8a8a3b46969b9',
maxFeePerGas: 2n,
maxPriorityFeePerGas: 1n,
Expand All @@ -612,12 +612,10 @@ describe('transactionRequest', () => {
"feeCurrency": "0x0f16e9b0d03470827a95cdfd0cb8a8a3b46969b9",
"from": "0x0f16e9b0d03470827a95cdfd0cb8a8a3b46969b9",
"gas": "0x1",
"gatewayFee": undefined,
"gatewayFeeRecipient": "0x0f16e9b0d03470827a95cdfd0cb8a8a3b46969b9",
"maxFeePerGas": "0x2",
"maxPriorityFeePerGas": "0x1",
"nonce": "0x1",
"type": "0x7c",
"type": "0x7b",
"value": "0x1",
}
`)
Expand All @@ -627,6 +625,7 @@ describe('transactionRequest', () => {
feeCurrency: '0x0f16e9b0d03470827a95cdfd0cb8a8a3b46969b9',
from: '0x0f16e9b0d03470827a95cdfd0cb8a8a3b46969b9',
gas: 1n,
// @ts-ignore
gatewayFee: 4n,
gatewayFeeRecipient: '0x0f16e9b0d03470827a95cdfd0cb8a8a3b46969b9',
maxFeePerGas: 2n,
Expand All @@ -639,12 +638,10 @@ describe('transactionRequest', () => {
"feeCurrency": "0x0f16e9b0d03470827a95cdfd0cb8a8a3b46969b9",
"from": "0x0f16e9b0d03470827a95cdfd0cb8a8a3b46969b9",
"gas": "0x1",
"gatewayFee": "0x4",
"gatewayFeeRecipient": "0x0f16e9b0d03470827a95cdfd0cb8a8a3b46969b9",
"maxFeePerGas": "0x2",
"maxPriorityFeePerGas": "0x1",
"nonce": "0x1",
"type": "0x7c",
"type": "0x7b",
"value": "0x1",
}
`)
Expand All @@ -654,6 +651,7 @@ describe('transactionRequest', () => {
feeCurrency: '0x0f16e9b0d03470827a95cdfd0cb8a8a3b46969b9',
from: '0x0f16e9b0d03470827a95cdfd0cb8a8a3b46969b9',
gas: 1n,
// @ts-ignore
gatewayFee: 4n,
gatewayFeeRecipient: '0x0f16e9b0d03470827a95cdfd0cb8a8a3b46969b9',
maxFeePerGas: 2n,
Expand All @@ -666,12 +664,10 @@ describe('transactionRequest', () => {
"feeCurrency": "0x0f16e9b0d03470827a95cdfd0cb8a8a3b46969b9",
"from": "0x0f16e9b0d03470827a95cdfd0cb8a8a3b46969b9",
"gas": "0x1",
"gatewayFee": "0x4",
"gatewayFeeRecipient": "0x0f16e9b0d03470827a95cdfd0cb8a8a3b46969b9",
"maxFeePerGas": "0x2",
"maxPriorityFeePerGas": "0x1",
"nonce": "0x1",
"type": "0x7c",
"type": "0x7b",
"value": "0x1",
}
`)
Expand All @@ -681,6 +677,7 @@ describe('transactionRequest', () => {
feeCurrency: '0x0f16e9b0d03470827a95cdfd0cb8a8a3b46969b9',
from: '0x0f16e9b0d03470827a95cdfd0cb8a8a3b46969b9',
gas: 1n,
// @ts-ignore
gatewayFee: 4n,
gatewayFeeRecipient: '0x0f16e9b0d03470827a95cdfd0cb8a8a3b46969b9',
maxFeePerGas: 2n,
Expand All @@ -693,12 +690,10 @@ describe('transactionRequest', () => {
"feeCurrency": "0x0f16e9b0d03470827a95cdfd0cb8a8a3b46969b9",
"from": "0x0f16e9b0d03470827a95cdfd0cb8a8a3b46969b9",
"gas": "0x1",
"gatewayFee": "0x4",
"gatewayFeeRecipient": "0x0f16e9b0d03470827a95cdfd0cb8a8a3b46969b9",
"maxFeePerGas": "0x2",
"maxPriorityFeePerGas": "0x4",
"nonce": "0x1",
"type": "0x7c",
"type": "0x7b",
"value": "0x1",
}
`)
Expand Down
14 changes: 3 additions & 11 deletions src/celo/formatters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { type ChainFormatters } from '../types/chain.js'
import type { Hash } from '../types/misc.js'
import type { RpcTransaction } from '../types/rpc.js'
import { hexToBigInt } from '../utils/encoding/fromHex.js'
import { numberToHex } from '../utils/encoding/toHex.js'
import { defineBlock } from '../utils/formatters/block.js'
import {
defineTransaction,
Expand All @@ -16,7 +15,7 @@ import type {
CeloTransaction,
CeloTransactionRequest,
} from './types.js'
import { isCIP42, isCIP64 } from './utils.js'
import { isCIP64 } from './utils.js'

export const formatters = {
block: /*#__PURE__*/ defineBlock({
Expand Down Expand Up @@ -73,15 +72,8 @@ export const formatters = {
feeCurrency: args.feeCurrency,
} as CeloRpcTransactionRequest

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
if (isCIP64(args)) {
request.type = '0x7b'
}

return request
Expand Down
94 changes: 94 additions & 0 deletions src/celo/getTransaction.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { describe, expect, test, vi } from 'vitest'
import { celo } from '../chains/index.js'
import {
type EIP1193RequestFn,
type PublicRpcSchema,
type WalletRpcSchema,
createPublicClient,
createTransport,
} from '../index.js'

describe('getTransaction()', () => {
const transportRequestMock = vi.fn(async (request) => {
if (request.method === 'eth_getTransactionByHash') {
return {
hash: '0xb733188a557d376b29bd8fd7bbd9cbefea6beaa90b993bd967c3eab9826b1e59',
nonce: '0x23cb',
blockHash:
'0x81d734d38a2c62726a0ce029e3f0291c7c54cac35db7b1f60e558133c6f44d18',
blockNumber: '0xefa8eb',
transactionIndex: '0x2',
from: '0xeb350f3c0d8519ff63540c6f26950935f5b93efa',
to: '0x4aad04d41fd7fd495503731c5a2579e19054c432',
value: '0x0',
gas: '0x1d3c9',
maxFeePerGas: '0x83215600',
maxPriorityFeePerGas: '0x77359400',
input:
'0xa020c8de0000000000000000000000007550d8d538bc883c965dfac89a8c76de4a2672e3000000000000000000000000c8a81d473992c7c6d3f469a8263f24914625709d00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005',
r: '0x198e15feb6f615c3ae9ba42e1721b518d391aa6b098005b03c6c4051f694bf0c',
s: '0x584546ab557daf99b3118f3efb0583d57cc9d3136bce0d436d0acc821eddddf1',
v: '0x1',
chainId: '0xa4ec',
accessList: [],
type: '0x7c',
ethCompatible: false,
feeCurrency: null,
gatewayFee: '0x0',
gatewayFeeRecipient: '0x7a41b0e608a6bcf8c8ad78671e175d6df7f71f95',
gasPrice: null,
}
}
return null
}) as EIP1193RequestFn<WalletRpcSchema & PublicRpcSchema>

const mockTransport = () =>
createTransport({
key: 'mock',
name: 'Mock Transport',
request: transportRequestMock,
type: 'mock',
})

const client = createPublicClient({
transport: mockTransport,
chain: celo,
})

test('returns CIP-42 transaction', async () => {
expect(
await client.getTransaction({
hash: '0xb733188a557d376b29bd8fd7bbd9cbefea6beaa90b993bd967c3eab9826b1e59',
}),
).toMatchInlineSnapshot(`
{
"accessList": [],
"blockHash": "0x81d734d38a2c62726a0ce029e3f0291c7c54cac35db7b1f60e558133c6f44d18",
"blockNumber": 15706347n,
"chainId": 42220,
"ethCompatible": false,
"feeCurrency": null,
"from": "0xeb350f3c0d8519ff63540c6f26950935f5b93efa",
"gas": 119753n,
"gasPrice": undefined,
"gatewayFee": 0n,
"gatewayFeeRecipient": "0x7a41b0e608a6bcf8c8ad78671e175d6df7f71f95",
"hash": "0xb733188a557d376b29bd8fd7bbd9cbefea6beaa90b993bd967c3eab9826b1e59",
"input": "0xa020c8de0000000000000000000000007550d8d538bc883c965dfac89a8c76de4a2672e3000000000000000000000000c8a81d473992c7c6d3f469a8263f24914625709d00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005",
"maxFeePerBlobGas": undefined,
"maxFeePerGas": 2200000000n,
"maxPriorityFeePerGas": 2000000000n,
"nonce": 9163,
"r": "0x198e15feb6f615c3ae9ba42e1721b518d391aa6b098005b03c6c4051f694bf0c",
"s": "0x584546ab557daf99b3118f3efb0583d57cc9d3136bce0d436d0acc821eddddf1",
"to": "0x4aad04d41fd7fd495503731c5a2579e19054c432",
"transactionIndex": 2,
"type": "cip42",
"typeHex": "0x7c",
"v": 1n,
"value": 0n,
"yParity": 1,
}
`)
})
})
2 changes: 0 additions & 2 deletions src/celo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@ export type {
CeloTransactionType,
RpcTransactionCIP42,
RpcTransactionCIP64,
RpcTransactionRequestCIP42,
RpcTransactionRequestCIP64,
TransactionCIP42,
TransactionCIP64,
TransactionRequestCIP42,
TransactionRequestCIP64,
TransactionSerializableCIP42,
TransactionSerializableCIP64,
Expand Down
70 changes: 24 additions & 46 deletions src/celo/parsers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ import {
} from '../index.js'
import { parseTransaction } from './parsers.js'
import { serializeTransaction } from './serializers.js'
import type {
TransactionSerializableCIP42,
TransactionSerializableCIP64,
} from './types.js'
import type { TransactionSerializableCIP64 } from './types.js'

test('should be able to parse a cip42 transaction', () => {
const signedTransaction =
Expand Down Expand Up @@ -49,17 +46,12 @@ test('should return same result as standard parser when not CIP42 or CIP64', ()
})

describe('should parse a CIP42 transaction', () => {
const transactionWithGatewayFee = {
...transaction,
chainId: 42270,
gatewayFee: parseEther('0.1'),
gatewayFeeRecipient: accounts[1].address,
}

test('with gatewayFee', () => {
const serialized = serializeTransaction(transactionWithGatewayFee)

expect(parseTransaction(serialized)).toMatchInlineSnapshot(`
expect(
parseTransaction(
'0x7cf85282a51e82031184773594008477359400825209809470997970c51812dc3a010c7d01b50e0d17dc79c888016345785d8a00009490f79bf6eb2c4f870365e785982e1f101e93b906880de0b6b3a764000080c0',
),
).toMatchInlineSnapshot(`
{
"chainId": 42270,
"gas": 21001n,
Expand All @@ -74,23 +66,13 @@ describe('should parse a CIP42 transaction', () => {
}
`)
})
test('with access list', () => {
const transactionWithAccessList: TransactionSerializableCIP42 = {
...transactionWithGatewayFee,
accessList: [
{
address: '0x0000000000000000000000000000000000000000',
storageKeys: [
'0x0000000000000000000000000000000000000000000000000000000000000001',
'0x60fdd29ff912ce880cd3edaf9f932dc61d3dae823ea77e0323f94adb9f6a72fe',
],
},
],
}

const serialized = serializeTransaction(transactionWithAccessList)

expect(parseTransaction(serialized)).toMatchInlineSnapshot(`
test('with access list', () => {
expect(
parseTransaction(
'0x7cf8ae82a51e82031184773594008477359400825209809470997970c51812dc3a010c7d01b50e0d17dc79c888016345785d8a00009490f79bf6eb2c4f870365e785982e1f101e93b906880de0b6b3a764000080f85bf859940000000000000000000000000000000000000000f842a00000000000000000000000000000000000000000000000000000000000000001a060fdd29ff912ce880cd3edaf9f932dc61d3dae823ea77e0323f94adb9f6a72fe',
),
).toMatchInlineSnapshot(`
{
"accessList": [
{
Expand All @@ -114,15 +96,13 @@ describe('should parse a CIP42 transaction', () => {
}
`)
})
test('with data as 0x', () => {
const transactionWithData: TransactionSerializableCIP42 = {
...transactionWithGatewayFee,
data: '0x',
}

const serialized = serializeTransaction(transactionWithData)

expect(parseTransaction(serialized)).toMatchInlineSnapshot(`
test('with data as 0x', () => {
expect(
parseTransaction(
'0x7cf85282a51e82031184773594008477359400825209809470997970c51812dc3a010c7d01b50e0d17dc79c888016345785d8a00009490f79bf6eb2c4f870365e785982e1f101e93b906880de0b6b3a764000080c0',
),
).toMatchInlineSnapshot(`
{
"chainId": 42270,
"gas": 21001n,
Expand All @@ -137,15 +117,13 @@ describe('should parse a CIP42 transaction', () => {
}
`)
})
test('with data', () => {
const transactionWithData: TransactionSerializableCIP42 = {
...transactionWithGatewayFee,
data: '0x1234',
}

const serialized = serializeTransaction(transactionWithData)

expect(parseTransaction(serialized)).toMatchInlineSnapshot(`
test('with data', () => {
expect(
parseTransaction(
'0x7cf85482a51e82031184773594008477359400825209809470997970c51812dc3a010c7d01b50e0d17dc79c888016345785d8a00009490f79bf6eb2c4f870365e785982e1f101e93b906880de0b6b3a7640000821234c0',
),
).toMatchInlineSnapshot(`
{
"chainId": 42270,
"data": "0x1234",
Expand Down
4 changes: 2 additions & 2 deletions src/celo/sendTransaction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ describe('sendTransaction()', () => {
})
})

test('provides valid transaction params to sign for eth_sendRawTransaction (local account) for CIP-42', async () => {
test('provides valid transaction params to sign for eth_sendRawTransaction (local account) for CIP-42 - sending as CIP-64', async () => {
const hash = await client.sendTransaction({
value: 1n,
to: toAddress,
Expand All @@ -99,7 +99,7 @@ describe('sendTransaction()', () => {
expect(transportRequestMock).toHaveBeenLastCalledWith({
method: 'eth_sendRawTransaction',
params: [
'0x7cf88d82a4ec80010201940000000000000000000000000000000000000fee9400000000000000000000000000000000000000017b94f39fd6e51aad88f6f4ce6ab8827279cfffb922660180c080a049b4b40c685a0bf9e3d1cca92a9175382bfa3a5e1bbd65610abcb0bd28b4ad90a009b40f809939683763bec0943101c7a7c79ea60239fd0d73975f555e6777ee1d',
'0x7bf87782a4ec8001020194f39fd6e51aad88f6f4ce6ab8827279cfffb922660180c0940000000000000000000000000000000000000fee80a0a3163f9ff91200f4c8000f0217d85d16c329c2f38d48a7b4b70119989e475e57a0555fd5b2a6eac95426e33cd07ca5fec121ad46194611a013001f76bbc4b33136',
],
})
})
Expand Down
Loading

0 comments on commit baa5116

Please sign in to comment.