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

CIP-66 support #13

Closed
wants to merge 22 commits into from
Closed
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
6 changes: 6 additions & 0 deletions .changeset/nasty-mayflies-search.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"viem": minor
---

Adds support for "[CIP-66: New Transaction Type: Celo Denominated Fees](https://github.com/celo-org/celo-proposals/blob/master/CIPs/cip-0066.md)" on Celo. Note that this transaction type is only supported on Cel2 and can be created by adding the additional maxFeeInFeeCurrency field to your transaction params.

19 changes: 11 additions & 8 deletions src/celo/formatters.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { http } from '../clients/transports/http.js'
import type { Hash } from '../types/misc.js'
import { formatters } from './formatters.js'
import type { CeloRpcBlock, CeloTransactionRequest } from './types.js'
import { EMPTY_HEX_VALUE } from './utils.js'

describe('block', () => {
expectTypeOf(formatters.block.format)
Expand Down Expand Up @@ -146,19 +147,21 @@ describe('smoke', () => {
| 'eip4844'
| 'cip42'
| 'cip64'
| 'cip66'
| 'deposit'
>()
})

test('transactionRequest (prepareTransactionRequest)', async () => {
const client = createWalletClient({
account: '0x',
account: EMPTY_HEX_VALUE,
chain: celo,
transport: http(),
})

prepareTransactionRequest(client, {
feeCurrency: '0x',
feeCurrency: EMPTY_HEX_VALUE,
maxFeeInFeeCurrency: 2000000000n,
})

// @ts-expect-error `gasPrice` is not defined
Expand All @@ -171,37 +174,37 @@ describe('smoke', () => {

test('transactionRequest (sendTransaction)', async () => {
const client = createWalletClient({
account: '0x',
account: EMPTY_HEX_VALUE,
chain: celo,
transport: http(),
})

sendTransaction(client, {
feeCurrency: '0x',
feeCurrency: EMPTY_HEX_VALUE,
})
})

test('transactionRequest (signTransaction)', async () => {
const client = createWalletClient({
account: '0x',
account: EMPTY_HEX_VALUE,
chain: celo,
transport: http(),
})

signTransaction(client, {
feeCurrency: '0x',
feeCurrency: EMPTY_HEX_VALUE,
})
})

test('transactionRequest (chain on action)', async () => {
const client = createWalletClient({
account: '0x',
account: EMPTY_HEX_VALUE,
transport: http(),
})

sendTransaction(client, {
chain: celo,
feeCurrency: '0x',
feeCurrency: EMPTY_HEX_VALUE,
})
})
})
118 changes: 118 additions & 0 deletions src/celo/formatters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@
blockNumber: '0x1',
chainId: '0x1',
feeCurrency: null,
maxFeeInFeeCurrency: undefined,
from: '0x045d685d23e8aa34dc408a66fb408f20dc84d785',
gas: '0x69420',
maxFeePerGas: '0x0',
Expand Down Expand Up @@ -240,6 +241,7 @@
"gatewayFeeRecipient": null,
"hash": "0x487efb864b308ee85afd7ed5954e968457cfe84e71726114b0a44f31fb876e85",
"input": "0x389ec778",
"maxFeeInFeeCurrency": undefined,
"maxFeePerGas": 0n,
"maxPriorityFeePerGas": 0n,
"nonce": 1,
Expand Down Expand Up @@ -338,6 +340,7 @@
blockNumber: '0x1',
chainId: '0x1',
feeCurrency: null,
maxFeeInFeeCurrency: undefined,
from: '0xa152f8bb749c55e9943a3a0a3111d18ee2b3f94e',
gas: '0x2',
gasPrice: undefined,
Expand Down Expand Up @@ -372,6 +375,7 @@
"gatewayFeeRecipient": null,
"hash": "0xa4b1f606b66105fa45cb5db23d2f6597075701e7f0e2367f4e6a39d17a8cf98b",
"input": "0x23b872dd000000000000000000000000a00f99bc38b1ecda1fd70eaa1cd31d576a9f46b0000000000000000000000000f16e9b0d03470827a95cdfd0cb8a8a3b46969b910000000000000000000000000000000000000000000000000000002b3b6fb3d0",
"maxFeeInFeeCurrency": undefined,
"maxFeePerGas": 4n,
"maxPriorityFeePerGas": 5n,
"nonce": 6,
Expand Down Expand Up @@ -444,6 +448,7 @@
}
`)

// CIP-42
expect(
transaction.format({
accessList: [],
Expand Down Expand Up @@ -502,6 +507,7 @@
}
`)

// CIP-64
expect(
transaction.format({
accessList: [],
Expand Down Expand Up @@ -556,6 +562,63 @@
}
`)

// CIP-66
expect(
transaction.format({

Check failure on line 567 in src/celo/formatters.test.ts

View workflow job for this annotation

GitHub Actions / Verify / Types (5.0.4)

Argument of type '{ accessList: never[]; blockHash: "0x89644bbd5c8d682a2e9611170e6c1f02573d866d286f006cbf517eec7254ec2d"; blockNumber: "0x1"; chainId: "0x1"; feeCurrency: "0x0f16e9b0d03470827a95cdfd0cb8a8a3b46969b9"; ... 16 more ...; yParity: "0x1"; }' is not assignable to parameter of type 'CeloRpcTransaction'.
accessList: [],
blockHash:
'0x89644bbd5c8d682a2e9611170e6c1f02573d866d286f006cbf517eec7254ec2d',
blockNumber: '0x1',
chainId: '0x1',
feeCurrency: '0x0f16e9b0d03470827a95cdfd0cb8a8a3b46969b9',
maxFeeInFeeCurrency: '0x12345',

Check failure on line 574 in src/celo/formatters.test.ts

View workflow job for this annotation

GitHub Actions / Verify / Types (5.1.6)

Type 'string' is not assignable to type 'bigint | `0x${string}` | undefined'.

Check failure on line 574 in src/celo/formatters.test.ts

View workflow job for this annotation

GitHub Actions / Verify / Types (5.2.2)

Type 'string' is not assignable to type 'bigint | `0x${string}` | undefined'.

Check failure on line 574 in src/celo/formatters.test.ts

View workflow job for this annotation

GitHub Actions / Verify / Types (5.3.3)

Type 'string' is not assignable to type 'bigint | `0x${string}` | undefined'.

Check failure on line 574 in src/celo/formatters.test.ts

View workflow job for this annotation

GitHub Actions / Verify / Types (5.4.5)

Type 'string' is not assignable to type 'bigint | `0x${string}` | undefined'.

Check failure on line 574 in src/celo/formatters.test.ts

View workflow job for this annotation

GitHub Actions / Verify / Types (5.5.2)

Type 'string' is not assignable to type 'bigint | `0x${string}` | undefined'.
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: '0x7a',
v: '0x1',
value: '0x0',
yParity: '0x1',
}),
).toMatchInlineSnapshot(`
{
"accessList": [],
"blockHash": "0x89644bbd5c8d682a2e9611170e6c1f02573d866d286f006cbf517eec7254ec2d",
"blockNumber": 1n,
"chainId": 1,
"feeCurrency": "0x0f16e9b0d03470827a95cdfd0cb8a8a3b46969b9",
"from": "0xa152f8bb749c55e9943a3a0a3111d18ee2b3f94e",
"gas": 2n,
"gasPrice": undefined,
"hash": "0xa4b1f606b66105fa45cb5db23d2f6597075701e7f0e2367f4e6a39d17a8cf98b",
"input": "0x23b872dd000000000000000000000000a00f99bc38b1ecda1fd70eaa1cd31d576a9f46b0000000000000000000000000f16e9b0d03470827a95cdfd0cb8a8a3b46969b910000000000000000000000000000000000000000000000000000002b3b6fb3d0",
"maxFeeInFeeCurrency": 74565n,
"maxFeePerBlobGas": undefined,
"maxFeePerGas": 4n,
"maxPriorityFeePerGas": 5n,
"nonce": 6,
"r": "0x5e49a7bd0534c6b6d3bbe581659424d3747f920d40ce56e48d26e5d94aac32ca",
"s": "0x1746abe27b7c4f00bda1ec714ac1f7083e9025b6ca3b2248e439a173e4ab55e0",
"to": "0x15d4c048f83bd7e37d49ea4c83a07267ec4203da",
"transactionIndex": 7,
"type": "cip66",
"typeHex": "0x7a",
"v": 1n,
"value": 0n,
"yParity": 1,
}
`)

expect(
transaction.format({
blockHash:
Expand Down Expand Up @@ -660,6 +723,7 @@
gas: 1n,
// @ts-ignore
gatewayFee: 4n,
// @ts-ignore
gatewayFeeRecipient: '0x0f16e9b0d03470827a95cdfd0cb8a8a3b46969b9',
maxFeePerGas: 2n,
maxPriorityFeePerGas: 1n,
Expand Down Expand Up @@ -711,6 +775,7 @@
gas: 1n,
// @ts-ignore
gatewayFee: 4n,
// @ts-ignore
gatewayFeeRecipient: '0x0f16e9b0d03470827a95cdfd0cb8a8a3b46969b9',
maxFeePerGas: 2n,
maxPriorityFeePerGas: 1n,
Expand All @@ -737,6 +802,7 @@
gas: 1n,
// @ts-ignore
gatewayFee: 4n,
// @ts-ignore
gatewayFeeRecipient: '0x0f16e9b0d03470827a95cdfd0cb8a8a3b46969b9',
maxFeePerGas: 2n,
maxPriorityFeePerGas: 1n,
Expand All @@ -763,6 +829,7 @@
gas: 1n,
// @ts-ignore
gatewayFee: 4n,
// @ts-ignore
gatewayFeeRecipient: '0x0f16e9b0d03470827a95cdfd0cb8a8a3b46969b9',
maxFeePerGas: 2n,
maxPriorityFeePerGas: 4n,
Expand All @@ -782,6 +849,7 @@
}
`)
})

test('formatter cip64', () => {
expect(
transactionRequest.format({
Expand Down Expand Up @@ -827,4 +895,54 @@
}
`)
})

test('formatter cip66', () => {
expect(
transactionRequest.format({
feeCurrency: '0x0f16e9b0d03470827a95cdfd0cb8a8a3b46969b9',
maxFeeInFeeCurrency: 123n,
from: '0x0f16e9b0d03470827a95cdfd0cb8a8a3b46969b9',
gas: 1n,
maxFeePerGas: 2n,
maxPriorityFeePerGas: 1n,
nonce: 1,
value: 1n,
}),
).toMatchInlineSnapshot(`
{
"feeCurrency": "0x0f16e9b0d03470827a95cdfd0cb8a8a3b46969b9",
"from": "0x0f16e9b0d03470827a95cdfd0cb8a8a3b46969b9",
"gas": "0x1",
"maxFeeInFeeCurrency": "0x7b",
"maxFeePerGas": "0x2",
"maxPriorityFeePerGas": "0x1",
"nonce": "0x1",
"type": "0x7a",
"value": "0x1",
}
`)

expect(
transactionRequest.format({
from: '0x0f16e9b0d03470827a95cdfd0cb8a8a3b46969b9',
to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', // Recipient (illustrative address)
value: 1n,
feeCurrency: '0x874069Fa1Eb16D44d622F2e0Ca25eeA172369bC1', // cUSD fee currency
maxFeeInFeeCurrency: 123n,
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",
"maxFeeInFeeCurrency": "0x7b",
"maxFeePerGas": "0x2",
"maxPriorityFeePerGas": "0x2",
"to": "0x70997970c51812dc3a010c7d01b50e0d17dc79c8",
"type": "0x7a",
"value": "0x1",
}
`)
})
})
33 changes: 29 additions & 4 deletions src/celo/formatters.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { ChainFormatters } from '../types/chain.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 @@ -15,16 +16,25 @@ import type {
CeloTransaction,
CeloTransactionRequest,
} from './types.js'
import { isCIP64 } from './utils.js'
import { isCIP64, isCIP66 } from './utils.js'

export const formatters = {
block: /*#__PURE__*/ defineBlock({
format(args: CeloRpcBlock): CeloBlock {
const transactions = args.transactions?.map((transaction) => {
if (typeof transaction === 'string') return transaction
const formatted = formatTransaction(transaction as RpcTransaction)

return {
...formatted,
...(transaction.maxFeeInFeeCurrency
? {
maxFeeInFeeCurrency:
typeof transaction.maxFeeInFeeCurrency === 'string'
? hexToBigInt(transaction.maxFeeInFeeCurrency)
: transaction.maxFeeInFeeCurrency,
}
: {}),
...(transaction.gatewayFee
? {
gatewayFee: hexToBigInt(transaction.gatewayFee),
Expand Down Expand Up @@ -52,8 +62,14 @@ export const formatters = {

const transaction = { feeCurrency: args.feeCurrency } as CeloTransaction

if (args.type === '0x7b') transaction.type = 'cip64'
else {
if (args.type === '0x7a') {
transaction.type = 'cip66'
transaction.maxFeeInFeeCurrency = args.maxFeeInFeeCurrency
aaronmgdr marked this conversation as resolved.
Show resolved Hide resolved
? hexToBigInt(args.maxFeeInFeeCurrency)
: undefined
} else if (args.type === '0x7b') {
transaction.type = 'cip64'
} else {
if (args.type === '0x7c') transaction.type = 'cip42'

transaction.gatewayFee = args.gatewayFee
Expand All @@ -69,8 +85,17 @@ export const formatters = {
format(args: CeloTransactionRequest): CeloRpcTransactionRequest {
const request = {} as CeloRpcTransactionRequest

if (isCIP66(args)) {
request.type = '0x7a'
request.maxFeeInFeeCurrency =
typeof args.maxFeeInFeeCurrency !== 'undefined'
? numberToHex(args.maxFeeInFeeCurrency)
: undefined
} else if (isCIP64(args)) {
request.type = '0x7b'
}

if (args.feeCurrency) request.feeCurrency = args.feeCurrency
if (isCIP64(args)) request.type = '0x7b'

return request
},
Expand Down
7 changes: 7 additions & 0 deletions src/celo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export { type ParseTransactionReturnType, parseTransaction } from './parsers.js'

export {
type SerializeTransactionCIP64ReturnType,
type SerializeTransactionCIP66ReturnType,
serializeTransaction,
} from './serializers.js'

Expand All @@ -20,12 +21,18 @@ export type {
CeloTransactionType,
RpcTransactionCIP42,
RpcTransactionCIP64,
RpcTransactionCIP66,
RpcTransactionRequestCIP64,
RpcTransactionRequestCIP66,
TransactionCIP42,
TransactionCIP64,
TransactionCIP66,
TransactionRequestCIP64,
TransactionRequestCIP66,
TransactionSerializableCIP42,
TransactionSerializableCIP64,
TransactionSerializableCIP66,
TransactionSerializedCIP42,
TransactionSerializedCIP64,
TransactionSerializedCIP66,
} from './types.js'
Loading
Loading