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

Feature/celo cip 64 support #3

Closed
wants to merge 18 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
5 changes: 5 additions & 0 deletions .changeset/three-buses-kiss.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"viem": minor
---

Added support for Celo CIP-64 transactions
2 changes: 2 additions & 0 deletions .github/workflows/on-push-to-main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,5 @@ jobs:
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
# https://docs.npmjs.com/generating-provenance-statements
NPM_CONFIG_PROVENANCE: true
23 changes: 16 additions & 7 deletions src/chains/celo/formatters.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ describe('transaction', () => {
>().toEqualTypeOf<`0x${string}` | null>()
expectTypeOf<
ReturnType<typeof formattersCelo.transaction.format>['gatewayFee']
>().toEqualTypeOf<bigint | null>()
>().toEqualTypeOf<bigint | null | undefined>()
expectTypeOf<
ReturnType<typeof formattersCelo.transaction.format>['gatewayFeeRecipient']
>().toEqualTypeOf<`0x${string}` | null>()
>().toEqualTypeOf<`0x${string}` | null | undefined>()
})

describe('transactionReceipt', () => {
Expand Down Expand Up @@ -137,10 +137,10 @@ describe('smoke', () => {
).toEqualTypeOf<`0x${string}` | null>()
expectTypeOf(
block_includeTransactions.transactions[0].gatewayFee,
).toEqualTypeOf<bigint | null>()
).toEqualTypeOf<bigint | null | undefined>()
expectTypeOf(
block_includeTransactions.transactions[0].gatewayFeeRecipient,
).toEqualTypeOf<`0x${string}` | null>()
).toEqualTypeOf<`0x${string}` | null | undefined>()

const block_pending = await getBlock(client, {
blockTag: 'pending',
Expand Down Expand Up @@ -170,12 +170,14 @@ describe('smoke', () => {
})

expectTypeOf(transaction.feeCurrency).toEqualTypeOf<`0x${string}` | null>()
expectTypeOf(transaction.gatewayFee).toEqualTypeOf<bigint | null>()
expectTypeOf(transaction.gatewayFee).toEqualTypeOf<
bigint | null | undefined
>()
expectTypeOf(transaction.gatewayFeeRecipient).toEqualTypeOf<
`0x${string}` | null
`0x${string}` | null | undefined
>()
expectTypeOf(transaction.type).toEqualTypeOf<
'legacy' | 'eip2930' | 'eip1559' | 'cip42'
'legacy' | 'eip2930' | 'eip1559' | 'cip42' | 'cip64'
>()
})

Expand Down Expand Up @@ -228,6 +230,13 @@ describe('smoke', () => {
type: 'cip42',
})

// @ts-expect-error `gasPrice` is not defined
prepareTransactionRequest(client, {
feeCurrency: '0x',
gasPrice: 0n,
type: 'cip64',
})

// @ts-expect-error `gasPrice` is not defined
prepareTransactionRequest(client, {
feeCurrency: '0x',
Expand Down
29 changes: 27 additions & 2 deletions src/chains/celo/formatters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -777,7 +777,7 @@ describe('transactionRequest', () => {
"maxFeePerGas": "0x2",
"maxPriorityFeePerGas": "0x1",
"nonce": "0x1",
"type": undefined,
"type": "0x7c",
"value": "0x1",
}
`)
Expand All @@ -804,7 +804,7 @@ describe('transactionRequest', () => {
"maxFeePerGas": "0x2",
"maxPriorityFeePerGas": "0x1",
"nonce": "0x1",
"type": undefined,
"type": "0x7c",
"value": "0x1",
}
`)
Expand Down Expand Up @@ -837,5 +837,30 @@ describe('transactionRequest', () => {
"value": "0x1",
}
`)

expect(
transactionRequest.format({
feeCurrency: '0x0f16e9b0d03470827a95cdfd0cb8a8a3b46969b9',
from: '0x0f16e9b0d03470827a95cdfd0cb8a8a3b46969b9',
gas: 1n,
maxFeePerGas: 2n,
maxPriorityFeePerGas: 1n,
nonce: 1,
type: 'cip64',
value: 1n,
}),
).toMatchInlineSnapshot(`
{
"feeCurrency": "0x0f16e9b0d03470827a95cdfd0cb8a8a3b46969b9",
"from": "0x0f16e9b0d03470827a95cdfd0cb8a8a3b46969b9",
"gas": "0x1",
"gasPrice": undefined,
"maxFeePerGas": "0x2",
"maxPriorityFeePerGas": "0x1",
"nonce": "0x1",
"type": "0x7b",
"value": "0x1",
}
`)
})
})
93 changes: 77 additions & 16 deletions src/chains/celo/formatters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,34 @@ import type {
CeloTransaction,
CeloTransactionReceiptOverrides,
CeloTransactionRequest,
RpcTransactionRequestCIP64,
TransactionCIP42,
TransactionCIP64,
TransactionRequestCIP42,
TransactionRequestCIP64,
} from './types.js'

function isTransactionRequestCIP64(args: CeloTransactionRequest): boolean {
if (args.type === 'cip64') return true
if (args.type) return false
return (
'feeCurrency' in args &&
// @ts-expect-error `gatewayFee` is not defined
args.gatewayFee === undefined &&
// @ts-expect-error `gatewayFeeRecipient` is not defined
args.gatewayFeeRecipient === undefined
)
}

function isTransactionRequestCIP42(args: CeloTransactionRequest): boolean {
if (args.type === 'cip42') return true
if (args.type) return false
return (
(args as TransactionRequestCIP42).gatewayFee !== undefined ||
(args as TransactionRequestCIP42).gatewayFeeRecipient !== undefined
)
}

export const formattersCelo = {
block: /*#__PURE__*/ defineBlock({
exclude: ['difficulty', 'gasLimit', 'mixHash', 'nonce', 'uncles'],
Expand All @@ -35,10 +61,15 @@ export const formattersCelo = {
return {
...formatTransaction(transaction as RpcTransaction),
feeCurrency: transaction.feeCurrency,
gatewayFee: transaction.gatewayFee
? hexToBigInt(transaction.gatewayFee)
: null,
gatewayFeeRecipient: transaction.gatewayFeeRecipient,

...(transaction.type !== '0x7b'
? {
gatewayFee: transaction.gatewayFee
? hexToBigInt(transaction.gatewayFee)
: null,
gatewayFeeRecipient: transaction.gatewayFeeRecipient || null,
}
: {}),
}
}) as Hash[] | CeloTransaction[]
return {
Expand All @@ -49,11 +80,24 @@ export const formattersCelo = {
}),
transaction: /*#__PURE__*/ defineTransaction({
format(args: CeloRpcTransaction): CeloTransaction {
return {
feeCurrency: args.feeCurrency,
gatewayFee: args.gatewayFee ? hexToBigInt(args.gatewayFee) : null,
gatewayFeeRecipient: args.gatewayFeeRecipient,
} as CeloTransaction
switch (args.type) {
case '0x7c':
return {
feeCurrency: args.feeCurrency,
gatewayFee: args.gatewayFee ? hexToBigInt(args.gatewayFee) : null,
gatewayFeeRecipient: args.gatewayFeeRecipient,
} as TransactionCIP42
case '0x7b':
return {
feeCurrency: args.feeCurrency,
} as TransactionCIP64
default:
return {
feeCurrency: args.feeCurrency,
gatewayFee: args.gatewayFee ? hexToBigInt(args.gatewayFee) : null,
gatewayFeeRecipient: args.gatewayFeeRecipient,
} as CeloTransaction
}
},
}),
transactionReceipt: /*#__PURE__*/ defineTransactionReceipt({
Expand All @@ -67,18 +111,35 @@ export const formattersCelo = {
}
},
}),

transactionRequest: /*#__PURE__*/ defineTransactionRequest({
format(args: CeloTransactionRequest): CeloRpcTransactionRequest {
format(args: CeloTransactionRequest) {
if (isTransactionRequestCIP64(args)) {
return {
type: '0x7b',
feeCurrency: args.feeCurrency,
} as RpcTransactionRequestCIP64
}

const _args = args as Exclude<
CeloTransactionRequest,
TransactionRequestCIP64
>

const request = {
feeCurrency: args.feeCurrency,
feeCurrency: _args.feeCurrency,
gatewayFee:
typeof args.gatewayFee !== 'undefined'
? numberToHex(args.gatewayFee)
typeof _args.gatewayFee !== 'undefined'
? numberToHex(_args.gatewayFee)
: undefined,
gatewayFeeRecipient: args.gatewayFeeRecipient,
gatewayFeeRecipient: _args.gatewayFeeRecipient,
} as CeloRpcTransactionRequest
if (args.type === 'cip42') request.type = '0x7c'
return request

if (isTransactionRequestCIP42(args)) {
request.type = '0x7c'
}

return request as CeloRpcTransactionRequest
},
}),
} as const satisfies ChainFormatters
Loading
Loading