-
Notifications
You must be signed in to change notification settings - Fork 73
/
rpc.ts
144 lines (138 loc) · 3.46 KB
/
rpc.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
import { z } from 'zod'
import { Context, ResponseError } from '@acala-network/chopsticks-core'
import { decodeStorageDiff } from '../../utils/decoder.js'
import { generateHtmlDiff } from '../../utils/generate-html-diff.js'
import { zHash, zHex } from '../../schema/index.js'
const zParaId = z.string().regex(/^\d+$/).transform(Number)
const schema = z.object({
raw: z.boolean().optional(),
html: z.boolean().optional(),
extrinsic: zHex
.or(
z.object({
call: zHex,
address: zHex,
}),
)
.optional(),
hrmp: z
.record(
zParaId,
z
.array(
z.object({
sentAt: z.number(),
data: zHex,
}),
)
.min(1),
)
.optional(),
dmp: z
.array(
z.object({
sentAt: z.number(),
msg: zHex,
}),
)
.min(1)
.optional(),
ump: z.record(zParaId, z.array(zHex).min(1)).optional(),
at: zHash.optional(),
})
type Params = z.infer<typeof schema>
export interface DryRunParams {
/**
* Return the raw storage diff
*/
raw: Params['raw']
/**
* Return the html storage diff
*/
html: Params['html']
/**
* The extrinsic to run
*/
extrinsic: Params['extrinsic']
/**
* The horizontal messages to run
*/
hrmp: Params['hrmp']
/**
* The downward messages to run
*/
dmp: Params['dmp']
/**
* The upward messages to run
*/
ump: Params['ump']
/**
* The block hash or number to run the extrinsic at
*/
at: Params['at']
}
/**
* Dry run an extrinsic or messages.
* If `html` is true, return the generated storage diff html string.
* If `raw` is true, return the raw storage diff.
* Otherwise, return `{ oldState, newState, delta }`.
*
* This function is a dev rpc handler. Use `dev_dryRun` as the method name when calling it.
*
* @param context - The context object of the rpc handler
* @param params - The parameters of the rpc handler
*
* @example Dry run an dmp
* ```ts
* import { WsProvider } from '@polkadot/rpc-provider'
* const ws = new WsProvider(`ws://localhost:8000`)
* const params = [
{
raw: false,
dmp: [
// https://acala.subscan.io/xcm_message/polkadot-2ab22918c567455af3563989d852f307f4cc1250
{
sentAt: 14471353,
msg: '0x02100104000100000b00280b9bba030a13000100000b00280b9bba03010300286bee0d0100040001010070c53d8e216f9c0f2e3b11c53f5f4bf3e078b995d5f0ed590f889f41e20e6531',
},
],
},
]
* await ws.send('dev_dryRun', params)
* ```
*/
export const rpc = async (context: Context, [params]: [DryRunParams]) => {
const { html, extrinsic, hrmp, dmp, ump, raw, at } = schema.parse(params)
const dryRun = async () => {
if (extrinsic) {
const { outcome, storageDiff } = await context.chain.dryRunExtrinsic(extrinsic, at)
if (outcome.isErr) {
throw new ResponseError(1, outcome.asErr.toString())
}
return storageDiff
}
if (hrmp) {
return context.chain.dryRunHrmp(hrmp, at)
}
if (dmp) {
return context.chain.dryRunDmp(dmp, at)
}
if (ump) {
return context.chain.dryRunUmp(ump, at)
}
throw new ResponseError(1, 'No extrinsic to run')
}
const storageDiff = await dryRun()
if (html) {
return generateHtmlDiff(context.chain.head, storageDiff)
}
if (raw) {
return storageDiff
}
const { oldState, newState, delta } = await decodeStorageDiff(context.chain.head, storageDiff)
return {
old: oldState,
new: newState,
delta,
}
}