-
Notifications
You must be signed in to change notification settings - Fork 0
/
formatter.ts
290 lines (271 loc) · 9.18 KB
/
formatter.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
// formatter.ts
import type { Block, Transaction, Chain, FormattedBlock, RpcBlock, TransactionType, Hex } from 'viem'
import {
defineBlock,
defineTransaction,
hexToBigInt,
} from 'viem'
import type { ChainFormatters } from 'viem'
// RPC Types (what we receive from the node)
export type QuaiRpcHeader = {
baseFeePerGas: `0x${string}`
efficiencyScore: `0x${string}`
etxEligibleSlices: `0x${string}`
etxRollupRoot: `0x${string}`
etxSetRoot: `0x${string}`
evmRoot: `0x${string}`
exchangeRate: `0x${string}`
expansionNumber: `0x${string}`
extraData: `0x${string}`
gasLimit: `0x${string}`
gasUsed: `0x${string}`
interlinkRootHash: `0x${string}`
manifestHash: [`0x${string}`, `0x${string}`, `0x${string}`]
number: [`0x${string}`, `0x${string}`]
outboundEtxsRoot: `0x${string}`
parentDeltaEntropy: [`0x${string}`, `0x${string}`, `0x${string}`]
parentEntropy: [`0x${string}`, `0x${string}`, `0x${string}`]
parentHash: [`0x${string}`, `0x${string}`]
parentUncledDeltaEntropy: [`0x${string}`, `0x${string}`, `0x${string}`]
primeTerminusHash: `0x${string}`
qiToQuai: `0x${string}`
quaiStateSize: `0x${string}`
quaiToQi: `0x${string}`
receiptsRoot: `0x${string}`
secondaryCoinbase: `0x${string}`
size: `0x${string}`
stateLimit: `0x${string}`
stateUsed: `0x${string}`
thresholdCount: `0x${string}`
transactionsRoot: `0x${string}`
uncleHash: `0x${string}`
uncledEntropy: `0x${string}`
utxoRoot: `0x${string}`
}
export type QuaiRpcWorkOrderHeader = {
difficulty: `0x${string}`
headerHash: `0x${string}`
location: `0x${string}`
lock: `0x${string}`
mixHash: `0x${string}`
nonce: `0x${string}`
number: `0x${string}`
parentHash: `0x${string}`
primaryCoinbase: `0x${string}`
primeTerminusNumber: `0x${string}`
timestamp: `0x${string}`
txHash: `0x${string}`
}
export type QuaiRpcBlock = Omit<Block, 'transactions'> & {
hash: `0x${string}`
header: QuaiRpcHeader
woHeader: QuaiRpcWorkOrderHeader
totalEntropy: `0x${string}`
outboundEtxs: QuaiRpcOutboundETX[]
transactions: (`0x${string}` | QuaiRpcTransaction)[]
}
export type QuaiTransaction = Omit<Transaction, 'type'> & {
blockHash: `0x${string}` | null
blockNumber: bigint | null
from: `0x${string}`
gas: bigint
gasPrice: bigint
hash: `0x${string}`
input: `0x${string}`
nonce: number
r: `0x${string}`
s: `0x${string}`
to: `0x${string}` | null
transactionIndex: number | null
type: 'legacy' | 'quai'
value: bigint
v: bigint
chainId: number
// Quai specific fields
minerTip?: bigint
}
export type QuaiRpcTransaction = {
blockHash: Hex | null
blockNumber: Hex | null
from: Hex
gas: Hex
minerTip?: Hex
gasPrice: Hex
hash: Hex
input: Hex
nonce: Hex
to: Hex | null
transactionIndex: Hex | null
value: Hex
type: Hex
chainId: Hex
v: Hex
r: Hex
s: Hex
accessList?: unknown[]
}
type BaseETX = {
blockHash: `0x${string}`
from: `0x${string}`
hash: `0x${string}`
input: `0x${string}`
to: `0x${string}`
type: `0x${string}`
accessList: any[]
originatingTxHash: `0x${string}`
etxType: `0x${string}`
}
// RPC ETX type (what we receive from the node)
export type QuaiRpcOutboundETX = BaseETX & {
blockNumber: `0x${string}`
gas: `0x${string}`
value: `0x${string}`
transactionIndex: `0x${string}`
etxIndex: `0x${string}`
}
// Formatted ETX type (what we return to the user)
export type QuaiOutboundETX = BaseETX & {
blockNumber: bigint
gas: bigint
value: bigint
transactionIndex: number
etxIndex: number
}
// Formatted Types (what we return to the user)
// Update QuaiBlock to better match Viem's block format
export type QuaiBlock = FormattedBlock & {
hash: `0x${string}`
number: bigint // Map from woHeader.number
timestamp: bigint // Map from woHeader.timestamp
parentHash: `0x${string}` // Map from header.parentHash[0]
nonce: `0x${string}` // Map from woHeader.nonce
difficulty: bigint // Map from woHeader.difficulty
gasLimit: bigint // Map from header.gasLimit
gasUsed: bigint // Map from header.gasUsed
miner: `0x${string}` // Map from woHeader.primaryCoinbase
mixHash: `0x${string}` // Map from woHeader.mixHash
totalDifficulty: bigint // Use woHeader.difficulty as we don't have cumulative
extraData: `0x${string}` // Map from header.extraData
// Quai specific fields
header: {
baseFeePerGas: bigint
efficiencyScore: bigint
exchangeRate: bigint
quaiNumber: [bigint, bigint] // renamed from number to avoid conflict
qiToQuai: bigint
quaiStateSize: bigint
quaiToQi: bigint
size: bigint
stateLimit: bigint
stateUsed: bigint
thresholdCount: bigint
}
woHeader: QuaiRpcWorkOrderHeader
totalEntropy: bigint
outboundEtxs: QuaiOutboundETX[]
}
export const formatters = {
block: defineBlock({
format(args: QuaiRpcBlock): QuaiBlock {
const transactions = args.transactions?.map((transaction) => {
if (typeof transaction === 'string') return transaction
return formatters.transaction.format(transaction)
})
return {
...args,
// Standard Viem block fields
number: args.woHeader.number ? hexToBigInt(args.woHeader.number) : 0n,
timestamp: args.woHeader.timestamp ? hexToBigInt(args.woHeader.timestamp) : 0n,
parentHash: args.header.parentHash[0],
nonce: args.woHeader.nonce,
difficulty: args.woHeader.difficulty ? hexToBigInt(args.woHeader.difficulty) : 0n,
gasLimit: args.header.gasLimit ? hexToBigInt(args.header.gasLimit) : 0n,
gasUsed: args.header.gasUsed ? hexToBigInt(args.header.gasUsed) : 0n,
miner: args.woHeader.primaryCoinbase,
mixHash: args.woHeader.mixHash,
totalDifficulty: args.woHeader.difficulty ? hexToBigInt(args.woHeader.difficulty) : 0n,
extraData: args.header.extraData,
baseFeePerGas: null,
receiptsRoot: args.header.receiptsRoot,
stateRoot: args.header.evmRoot,
transactionsRoot: args.header.transactionsRoot,
// Quai specific fields in nested objects
header: {
...args.header,
baseFeePerGas: args.header.baseFeePerGas ? hexToBigInt(args.header.baseFeePerGas) : 0n,
efficiencyScore: args.header.efficiencyScore ? hexToBigInt(args.header.efficiencyScore) : 0n,
exchangeRate: args.header.exchangeRate ? hexToBigInt(args.header.exchangeRate) : 0n,
quaiNumber: [
args.header.number?.[0] ? hexToBigInt(args.header.number[0]) : 0n,
args.header.number?.[1] ? hexToBigInt(args.header.number[1]) : 0n
],
qiToQuai: args.header.qiToQuai ? hexToBigInt(args.header.qiToQuai) : 0n,
quaiStateSize: args.header.quaiStateSize ? hexToBigInt(args.header.quaiStateSize) : 0n,
quaiToQi: args.header.quaiToQi ? hexToBigInt(args.header.quaiToQi) : 0n,
size: args.header.size ? hexToBigInt(args.header.size) : 0n,
stateLimit: args.header.stateLimit ? hexToBigInt(args.header.stateLimit) : 0n,
stateUsed: args.header.stateUsed ? hexToBigInt(args.header.stateUsed) : 0n,
thresholdCount: args.header.thresholdCount ? hexToBigInt(args.header.thresholdCount) : 0n,
},
woHeader: args.woHeader,
totalEntropy: args.totalEntropy ? hexToBigInt(args.totalEntropy) : 0n,
outboundEtxs: args.outboundEtxs?.map(etx => ({
...etx,
blockNumber: etx?.blockNumber ? hexToBigInt(etx.blockNumber) : 0n,
gas: etx?.gas ? hexToBigInt(etx.gas) : 0n,
value: etx?.value ? hexToBigInt(etx.value) : 0n,
transactionIndex: etx?.transactionIndex ? Number(hexToBigInt(etx.transactionIndex)) : 0,
etxIndex: etx?.etxIndex ? Number(hexToBigInt(etx.etxIndex)) : 0,
})) ?? [],
transactions,
} as QuaiBlock
},
}),
// Transaction formatter remains the same
transaction: defineTransaction({
format(args: QuaiRpcTransaction): QuaiTransaction {
const formatted: QuaiTransaction = {
blockHash: args.blockHash ?? null,
blockNumber: args.blockNumber ? hexToBigInt(args.blockNumber) : null,
from: args.from,
gas: args.gas ? hexToBigInt(args.gas) : 0n,
hash: args.hash,
input: args.input,
nonce: args.nonce ? Number(hexToBigInt(args.nonce)) : 0,
r: args.r,
s: args.s,
to: args.to ?? null,
transactionIndex: args.transactionIndex ? Number(hexToBigInt(args.transactionIndex)) : null,
typeHex: args.type,
type: 'legacy',
value: args.value ? hexToBigInt(args.value) : 0n,
v: args.v ? hexToBigInt(args.v) : 0n,
minerTip: args.minerTip ? hexToBigInt(args.minerTip) : undefined,
gasPrice: args.gasPrice ? hexToBigInt(args.gasPrice) : 0n,
chainId: args.chainId ? Number(hexToBigInt(args.chainId)) : 0,
}
if (args.accessList) {
// formatted.accessList = args.accessList
}
return formatted
},
}),
} as const satisfies ChainFormatters
export const quaiChain = {
id: 12000,
name: 'Quai Network',
nativeCurrency: {
decimals: 18,
name: 'QUAI',
symbol: 'QUAI',
},
rpcUrls: {
default: {
http: ['http://localhost:9200'],
},
public: {
http: ['https://rpc.quai.network/cyprus1'],
},
},
formatters,
} as const satisfies Chain