-
Notifications
You must be signed in to change notification settings - Fork 0
/
quaiActions.ts
64 lines (56 loc) · 1.69 KB
/
quaiActions.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
import {
createPublicClient,
defineChain,
http,
type PublicClient,
type Transport,
type Chain,
} from 'viem'
import type { QuaiBlock, QuaiTransaction } from './formatter'
export type QuaiClient = PublicClient & {
quai: {
getBlock: (args?: { blockHash?: `0x${string}`, blockNumber?: bigint }) => Promise<QuaiBlock>
getTransaction: (hash: `0x${string}`) => Promise<QuaiTransaction>
}
}
// need to find out how to extend client in order to pass quai_ calls instead of eth_ calls here
export function publicClientToQuaiClient(client: PublicClient): QuaiClient {
return client.extend((client) => ({
quai: {
async getBlock(args?: { blockHash?: `0x${string}`, blockNumber?: bigint }) {
if (args?.blockHash) {
return client.request({
method: 'quai_getBlockByHash',
params: [args.blockHash, true]
})
}
return client.request({
method: 'quai_getBlockByNumber',
params: [args?.blockNumber ?
`0x${args.blockNumber.toString(16)}` :
'latest',
true
]
})
},
async getTransaction(hash: `0x${string}`) {
return client.request({
method: 'quai_getTransactionByHash',
params: [hash]
})
}
}
}))
}
// Usage example
import { quaiChain } from './formatter'
export function createQuaiClient({
chain = quaiChain,
transport = http()
} = {}): QuaiClient {
const client = createPublicClient({
chain,
transport
})
return publicClientToQuaiClient(client)
}