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

Shazarre/cip 66 support b #17

Closed
wants to merge 11 commits into from
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

25 changes: 13 additions & 12 deletions src/celo/formatters.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import type { RpcBlock } from '../types/rpc.js'
import type { TransactionRequest } from '../types/transaction.js'
import type { Assign, ExactPartial } from '../types/utils.js'
import { formatters } from './formatters.js'
import { EMPTY_HEX_VALUE } from './utils.js'
import type {
CeloBlockOverrides,
CeloRpcTransaction,
Expand Down Expand Up @@ -151,15 +152,15 @@ describe('smoke', () => {

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,
gatewayFee: 0n,
gatewayFeeRecipient: '0x',
gatewayFeeRecipient: EMPTY_HEX_VALUE,
})

// @ts-expect-error `gasPrice` is not defined
Expand All @@ -172,43 +173,43 @@ 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,
gatewayFee: 0n,
gatewayFeeRecipient: '0x',
gatewayFeeRecipient: 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,
gatewayFee: 0n,
gatewayFeeRecipient: '0x',
gatewayFeeRecipient: 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,
gatewayFee: 0n,
gatewayFeeRecipient: '0x',
gatewayFeeRecipient: 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 @@ -104,6 +104,7 @@ describe('block', () => {
blockNumber: '0x1',
chainId: '0x1',
feeCurrency: null,
maxFeeInFeeCurrency: null,
from: '0x045d685d23e8aa34dc408a66fb408f20dc84d785',
gas: '0x69420',
maxFeePerGas: '0x0',
Expand All @@ -129,6 +130,7 @@ describe('block', () => {
blockNumber: '0x1',
chainId: '0x1',
feeCurrency: null,
maxFeeInFeeCurrency: null,
from: '0x045d685d23e8aa34dc408a66fb408f20dc84d785',
gas: '0x69420',
maxFeePerGas: '0x0',
Expand Down Expand Up @@ -186,6 +188,7 @@ describe('block', () => {
"gatewayFeeRecipient": null,
"hash": "0x487efb864b308ee85afd7ed5954e968457cfe84e71726114b0a44f31fb876e85",
"input": "0x389ec778",
"maxFeeInFeeCurrency": null,
"maxFeePerGas": 0n,
"maxPriorityFeePerGas": 0n,
"nonce": 1,
Expand All @@ -212,6 +215,7 @@ describe('block', () => {
"gatewayFeeRecipient": null,
"hash": "0x487efb864b308ee85afd7ed5954e968457cfe84e71726114b0a44f31fb876e85",
"input": "0x389ec778",
"maxFeeInFeeCurrency": null,
"maxFeePerGas": 0n,
"maxPriorityFeePerGas": 0n,
"nonce": 1,
Expand Down Expand Up @@ -307,6 +311,7 @@ describe('transaction', () => {
blockNumber: '0x1',
chainId: '0x1',
feeCurrency: null,
maxFeeInFeeCurrency: null,
from: '0xa152f8bb749c55e9943a3a0a3111d18ee2b3f94e',
gas: '0x2',
gasPrice: undefined,
Expand Down Expand Up @@ -341,6 +346,7 @@ describe('transaction', () => {
"gatewayFeeRecipient": null,
"hash": "0xa4b1f606b66105fa45cb5db23d2f6597075701e7f0e2367f4e6a39d17a8cf98b",
"input": "0x23b872dd000000000000000000000000a00f99bc38b1ecda1fd70eaa1cd31d576a9f46b0000000000000000000000000f16e9b0d03470827a95cdfd0cb8a8a3b46969b910000000000000000000000000000000000000000000000000000002b3b6fb3d0",
"maxFeeInFeeCurrency": null,
"maxFeePerGas": 4n,
"maxPriorityFeePerGas": 5n,
"nonce": 6,
Expand All @@ -364,6 +370,7 @@ describe('transaction', () => {
blockNumber: '0x1',
chainId: '0x1',
feeCurrency: null,
maxFeeInFeeCurrency: null,
from: '0xa152f8bb749c55e9943a3a0a3111d18ee2b3f94e',
gas: '0x2',
gasPrice: undefined,
Expand Down Expand Up @@ -398,6 +405,7 @@ describe('transaction', () => {
"gatewayFeeRecipient": null,
"hash": "0xa4b1f606b66105fa45cb5db23d2f6597075701e7f0e2367f4e6a39d17a8cf98b",
"input": "0x23b872dd000000000000000000000000a00f99bc38b1ecda1fd70eaa1cd31d576a9f46b0000000000000000000000000f16e9b0d03470827a95cdfd0cb8a8a3b46969b910000000000000000000000000000000000000000000000000000002b3b6fb3d0",
"maxFeeInFeeCurrency": null,
"maxFeePerGas": 4n,
"maxPriorityFeePerGas": 5n,
"nonce": 6,
Expand All @@ -413,6 +421,7 @@ describe('transaction', () => {
}
`)

// CIP-42
expect(
transaction.format({
accessList: [],
Expand Down Expand Up @@ -471,6 +480,7 @@ describe('transaction', () => {
}
`)

// CIP-64
expect(
transaction.format({
accessList: [],
Expand Down Expand Up @@ -524,6 +534,63 @@ describe('transaction', () => {
"yParity": 1,
}
`)

// CIP-66
expect(
transaction.format({
accessList: [],
blockHash:
'0x89644bbd5c8d682a2e9611170e6c1f02573d866d286f006cbf517eec7254ec2d',
blockNumber: '0x1',
chainId: '0x1',
feeCurrency: '0x0f16e9b0d03470827a95cdfd0cb8a8a3b46969b9',
maxFeeInFeeCurrency: '0x12345',
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,
}
`)
})

test('action', async () => {
Expand Down Expand Up @@ -698,6 +765,7 @@ describe('transactionRequest', () => {
}
`)
})

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

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",
}
`)
})
})
21 changes: 17 additions & 4 deletions src/celo/formatters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ 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 @@ -15,7 +16,7 @@ import type {
CeloTransaction,
CeloTransactionRequest,
} from './types.js'
import { isCIP64 } from './utils.js'
import { isCIP64, isCIP66 } from './utils.js'

export const formatters = {
block: /*#__PURE__*/ defineBlock({
Expand Down Expand Up @@ -52,8 +53,14 @@ export const formatters = {
format(args: CeloRpcTransaction): CeloTransaction {
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
? hexToBigInt(args.maxFeeInFeeCurrency)
: null
} else if (args.type === '0x7b') {
transaction.type = 'cip64'
} else {
if (args.type === '0x7c') transaction.type = 'cip42'

transaction.gatewayFee = args.gatewayFee
Expand All @@ -72,7 +79,13 @@ export const formatters = {
feeCurrency: args.feeCurrency,
} as CeloRpcTransactionRequest

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

Expand Down
6 changes: 6 additions & 0 deletions src/celo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,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