scale.rb 0.2.13 → 0.2.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/scale/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Scale
2
- VERSION = "0.2.13".freeze
2
+ VERSION = "0.2.18".freeze
3
3
  end
@@ -0,0 +1,170 @@
1
+ require "faye/websocket"
2
+ require "eventmachine"
3
+
4
+ def ws_request(url, payload)
5
+ result = nil
6
+
7
+ EM.run do
8
+ ws = Faye::WebSocket::Client.new(url)
9
+
10
+ ws.on :open do |event|
11
+ ws.send(payload.to_json)
12
+ end
13
+
14
+ ws.on :message do |event|
15
+ if event.data.include?("jsonrpc")
16
+ result = JSON.parse event.data
17
+ ws.close(3001, "data received")
18
+ EM.stop
19
+ end
20
+ end
21
+
22
+ ws.on :close do |event|
23
+ ws = nil
24
+ end
25
+ end
26
+
27
+ result
28
+ end
29
+
30
+ class SubstrateClient
31
+ class WebsocketError < StandardError; end
32
+ class RpcError < StandardError; end
33
+ class RpcTimeout < StandardError; end
34
+
35
+ attr_reader :metadata
36
+ attr_reader :spec_name, :spec_version
37
+
38
+ def initialize(url)
39
+ @url = url
40
+ @request_id = 1
41
+ @metadata_cache = {}
42
+ end
43
+
44
+ def request(method, params)
45
+ payload = {
46
+ "jsonrpc" => "2.0",
47
+ "method" => method,
48
+ "params" => params,
49
+ "id" => @request_id
50
+ }
51
+
52
+ data = ws_request(@url, payload)
53
+ if data["error"]
54
+ raise RpcError, data["error"]
55
+ else
56
+ data["result"]
57
+ end
58
+ end
59
+
60
+ def init_types_and_metadata(block_hash=nil)
61
+ runtime_version = self.state_getRuntimeVersion(block_hash)
62
+ spec_name = runtime_version["specName"].downcase
63
+ spec_version = runtime_version["specVersion"]
64
+
65
+ registry = Scale::TypeRegistry.instance
66
+
67
+ # load types
68
+ if registry.types == nil
69
+ registry.load(spec_name: spec_name)
70
+ end
71
+ registry.spec_version = spec_version
72
+
73
+ # set current metadata
74
+ metadata = @metadata_cache[spec_version]
75
+ if metadata.nil?
76
+ hex = self.state_getMetadata(block_hash)
77
+ metadata = Scale::Types::Metadata.decode(Scale::Bytes.new(hex))
78
+ @metadata_cache[spec_version] = metadata
79
+ end
80
+
81
+ @metadata = metadata
82
+ registry.metadata = metadata
83
+
84
+ true
85
+ end
86
+
87
+ def get_metadata_from_cache(spec_version)
88
+
89
+ end
90
+
91
+ def invoke(method, *params)
92
+ request(method, params)
93
+ end
94
+
95
+ # ################################################
96
+ # origin rpc methods
97
+ # ################################################
98
+ def method_missing(method, *args)
99
+ invoke method, *args
100
+ end
101
+
102
+ # ################################################
103
+ # custom methods based on origin rpc methods
104
+ # ################################################
105
+ def methods
106
+ invoke("rpc_methods")["methods"]
107
+ end
108
+
109
+ def get_block_number(block_hash)
110
+ header = self.chain_getHeader(block_hash)
111
+ header["number"].to_i(16)
112
+ end
113
+
114
+ def get_metadata(block_hash=nil)
115
+ self.init_types_and_metadata(block_hash)
116
+ @metadata
117
+ end
118
+
119
+ def get_block(block_hash=nil)
120
+ self.init_types_and_metadata(block_hash)
121
+ block = self.chain_getBlock(block_hash)
122
+ SubstrateClient::Helper.decode_block(block)
123
+ rescue => ex
124
+ puts ex.message
125
+ puts ex.backtrace.join("\n\t")
126
+ end
127
+
128
+ def get_block_events(block_hash=nil)
129
+ self.init_types_and_metadata(block_hash)
130
+
131
+ storage_key = "0x26aa394eea5630e07c48ae0c9558cef780d41e5e16056765bc8461851072c9d7"
132
+ events_data = state_getStorage storage_key, block_hash
133
+
134
+ scale_bytes = Scale::Bytes.new(events_data)
135
+ decoded = Scale::Types.get("Vec<EventRecord>").decode(scale_bytes).to_human
136
+ [events_data, decoded]
137
+ end
138
+
139
+ # Plain: client.get_storage("Sudo", "Key")
140
+ # Plain: client.get_storage("Balances", "TotalIssuance")
141
+ # Map: client.get_storage("System", "Account", ["0xd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d"])
142
+ # DoubleMap: client.get_storage("ImOnline", "AuthoredBlocks", [2818, "0x749ddc93a65dfec3af27cc7478212cb7d4b0c0357fef35a0163966ab5333b757"])
143
+ def get_storage(module_name, storage_name, params = nil, block_hash = nil)
144
+ self.init_types_and_metadata(block_hash)
145
+
146
+ storage_key, return_type = SubstrateClient::Helper.generate_storage_key_from_metadata(@metadata, module_name, storage_name, params)
147
+ data = self.state_getStorage(storage_key, block_hash)
148
+ return unless data
149
+
150
+ bytes = Scale::Bytes.new(data)
151
+ type = Scale::Types.get(return_type)
152
+ type.decode(bytes)
153
+ end
154
+
155
+ def generate_storage_key(module_name, storage_name, params = nil, block_hash = nil)
156
+ self.init_types_and_metadata(block_hash)
157
+ SubstrateClient::Helper.generate_storage_key_from_metadata(@metadata, module_name, storage_name, params)
158
+ end
159
+
160
+ # compose_call "Balances", "Transfer", { dest: "0x586cb27c291c813ce74e86a60dad270609abf2fc8bee107e44a80ac00225c409", value: 1_000_000_000_000 }
161
+ def compose_call(module_name, call_name, params, block_hash=nil)
162
+ self.init_types_and_metadata(block_hash)
163
+ SubstrateClient::Helper.compose_call_from_metadata(@metadata, module_name, call_name, params)
164
+ end
165
+
166
+ def generate_storage_hash_from_data(storage_hex_data)
167
+ "0x" + Crypto.blake2_256(Scale::Bytes.new(storage_hex_data).bytes)
168
+ end
169
+
170
+ end
@@ -1,5 +1,447 @@
1
1
  {
2
+ "versioning": [
3
+ {
4
+ "runtime_range": [
5
+ 35,
6
+ 41
7
+ ],
8
+ "types": {
9
+ "ScheduledAuthoritiesChangeT": {
10
+ "type": "struct",
11
+ "type_mapping": [
12
+ [
13
+ "next_authorities",
14
+ "Vec<RelayAuthorityT>"
15
+ ],
16
+ [
17
+ "deadline",
18
+ "BlockNumber"
19
+ ]
20
+ ]
21
+ }
22
+ }
23
+ },
24
+ {
25
+ "runtime_range": [
26
+ 34,
27
+ 41
28
+ ],
29
+ "types": {
30
+ "OpCode": "[u8; 4]",
31
+ "Term": "u32"
32
+ }
33
+ },
34
+ {
35
+ "runtime_range": [
36
+ 34,
37
+ 34
38
+ ],
39
+ "types": {
40
+ "ScheduledAuthoritiesChange": {
41
+ "type": "struct",
42
+ "type_mapping": [
43
+ [
44
+ "next_authorities",
45
+ "Vec<RelayAuthorityT>"
46
+ ],
47
+ [
48
+ "deadline",
49
+ "BlockNumber"
50
+ ]
51
+ ]
52
+ }
53
+ }
54
+ },
55
+ {
56
+ "runtime_range": [
57
+ 20,
58
+ 28
59
+ ],
60
+ "types": {
61
+ "MerkleMountainRangeRootLog": {
62
+ "type": "struct",
63
+ "type_mapping": [
64
+ [
65
+ "prefix",
66
+ "[u8; 4]"
67
+ ],
68
+ [
69
+ "mmr_root",
70
+ "Hash"
71
+ ]
72
+ ]
73
+ }
74
+ }
75
+ },
76
+ {
77
+ "runtime_range": [
78
+ 29,
79
+ 41
80
+ ],
81
+ "types": {
82
+ "EcdsaMessage": "[u8; 32]",
83
+ "RelayAuthoritySigner": "EcdsaAddress",
84
+ "RelayAuthorityMessage": "EcdsaMessage",
85
+ "RelayAuthoritySignature": "EcdsaSignature",
86
+ "RelayAuthorityT": {
87
+ "type": "struct",
88
+ "type_mapping": [
89
+ [
90
+ "account_id",
91
+ "AccountId"
92
+ ],
93
+ [
94
+ "signer",
95
+ "Signer"
96
+ ],
97
+ [
98
+ "stake",
99
+ "Balance"
100
+ ],
101
+ [
102
+ "term",
103
+ "BlockNumber"
104
+ ]
105
+ ]
106
+ },
107
+ "MMRRoot": "Hash",
108
+ "MerkleMountainRangeRootLog": {
109
+ "type": "struct",
110
+ "type_mapping": [
111
+ [
112
+ "prefix",
113
+ "[u8; 4]"
114
+ ],
115
+ [
116
+ "parent_mmr_root",
117
+ "Hash"
118
+ ]
119
+ ]
120
+ },
121
+ "EthereumRelayHeaderParcel": {
122
+ "type": "struct",
123
+ "type_mapping": [
124
+ [
125
+ "header",
126
+ "EthereumHeader"
127
+ ],
128
+ [
129
+ "parent_mmr_root",
130
+ "H256"
131
+ ]
132
+ ]
133
+ }
134
+ }
135
+ },
136
+ {
137
+ "runtime_range": [
138
+ 25,
139
+ 41
140
+ ],
141
+ "types": {
142
+ "RelayProofs": "EthereumRelayProofs"
143
+ }
144
+ },
145
+ {
146
+ "runtime_range": [
147
+ 24,
148
+ 41
149
+ ],
150
+ "types": {
151
+ "RelayVotingState": {
152
+ "type": "struct",
153
+ "type_mapping": [
154
+ [
155
+ "ayes",
156
+ "Vec<AccountId>"
157
+ ],
158
+ [
159
+ "nays",
160
+ "Vec<AccountId>"
161
+ ]
162
+ ]
163
+ },
164
+ "ProxyType": {
165
+ "type": "enum",
166
+ "type_mapping": [
167
+ [
168
+ "Any",
169
+ "Null"
170
+ ],
171
+ [
172
+ "NonTransfer",
173
+ "Null"
174
+ ],
175
+ [
176
+ "Governance",
177
+ "Null"
178
+ ],
179
+ [
180
+ "Staking",
181
+ "Null"
182
+ ],
183
+ [
184
+ "IdentityJudgement",
185
+ "Null"
186
+ ],
187
+ [
188
+ "EthereumBridge",
189
+ "Null"
190
+ ]
191
+ ]
192
+ },
193
+ "RelayHeaderId": "EthereumBlockNumber",
194
+ "RelayHeaderParcel": "EthereumRelayHeaderParcel",
195
+ "RelayAffirmationId": {
196
+ "type": "struct",
197
+ "type_mapping": [
198
+ [
199
+ "game_id",
200
+ "EthereumBlockNumber"
201
+ ],
202
+ [
203
+ "round",
204
+ "u32"
205
+ ],
206
+ [
207
+ "index",
208
+ "u32"
209
+ ]
210
+ ]
211
+ },
212
+ "RelayAffirmationT": {
213
+ "type": "struct",
214
+ "type_mapping": [
215
+ [
216
+ "relayer",
217
+ "AccountId"
218
+ ],
219
+ [
220
+ "relay_header_parcels",
221
+ "EthereumRelayHeaderParcel"
222
+ ],
223
+ [
224
+ "bond",
225
+ "Balance"
226
+ ],
227
+ [
228
+ "maybe_extended_relay_affirmation_id",
229
+ "Option<RelayAffirmationId>"
230
+ ],
231
+ [
232
+ "verified",
233
+ "bool"
234
+ ]
235
+ ]
236
+ }
237
+ }
238
+ },
239
+ {
240
+ "runtime_range": [
241
+ 24,
242
+ 24
243
+ ],
244
+ "types": {
245
+ "RelayProofs": "EthereumRelayProof"
246
+ }
247
+ },
248
+ {
249
+ "runtime_range": [
250
+ 20,
251
+ 22
252
+ ],
253
+ "types": {
254
+ "EthereumHeaderBrief": {
255
+ "type": "struct",
256
+ "type_mapping": [
257
+ [
258
+ "total_difficulty",
259
+ "U256"
260
+ ],
261
+ [
262
+ "parent_hash",
263
+ "H256"
264
+ ],
265
+ [
266
+ "number",
267
+ "EthereumBlockNumber"
268
+ ],
269
+ [
270
+ "relayer",
271
+ "AccountId"
272
+ ]
273
+ ]
274
+ },
275
+ "EthereumHeaderThingWithProof": {
276
+ "type": "struct",
277
+ "type_mapping": [
278
+ [
279
+ "header",
280
+ "EthereumHeader"
281
+ ],
282
+ [
283
+ "ethash_proof",
284
+ "Vec<EthashProof>"
285
+ ],
286
+ [
287
+ "mmr_root",
288
+ "H256"
289
+ ],
290
+ [
291
+ "mmr_proof",
292
+ "Vec<H256>"
293
+ ]
294
+ ]
295
+ },
296
+ "ConfirmedEthereumHeaderInfo": {
297
+ "type": "struct",
298
+ "type_mapping": [
299
+ [
300
+ "header",
301
+ "EthereumHeader"
302
+ ],
303
+ [
304
+ "mmr_root",
305
+ "H256"
306
+ ]
307
+ ]
308
+ },
309
+ "EthereumHeaderThing": {
310
+ "type": "struct",
311
+ "type_mapping": [
312
+ [
313
+ "header",
314
+ "EthereumHeader"
315
+ ],
316
+ [
317
+ "mmr_root",
318
+ "H256"
319
+ ]
320
+ ]
321
+ },
322
+ "Round": "u64",
323
+ "TcHeaderThingWithProof": "EthereumHeaderThingWithProof",
324
+ "TcHeaderThing": "EthereumHeaderThing",
325
+ "TcBlockNumber": "u64",
326
+ "TcHeaderHash": "H256",
327
+ "GameId": "TcBlockNumber",
328
+ "RelayProposalT": {
329
+ "type": "struct",
330
+ "type_mapping": [
331
+ [
332
+ "relayer",
333
+ "AccountId"
334
+ ],
335
+ [
336
+ "bonded_proposal",
337
+ "Vec<(Balance, TcHeaderThing)>"
338
+ ],
339
+ [
340
+ "extend_from_header_hash",
341
+ "Option<TcHeaderHash>"
342
+ ]
343
+ ]
344
+ }
345
+ }
346
+ },
347
+ {
348
+ "runtime_range": [
349
+ 23,
350
+ 41
351
+ ],
352
+ "types": {
353
+ "EthereumRelayProofs": {
354
+ "type": "struct",
355
+ "type_mapping": [
356
+ [
357
+ "ethash_proof",
358
+ "Vec<EthashProof>"
359
+ ],
360
+ [
361
+ "mmr_proof",
362
+ "Vec<H256>"
363
+ ]
364
+ ]
365
+ }
366
+ }
367
+ },
368
+ {
369
+ "runtime_range": [
370
+ 23,
371
+ 28
372
+ ],
373
+ "types": {
374
+ "EthereumRelayHeaderParcel": {
375
+ "type": "struct",
376
+ "type_mapping": [
377
+ [
378
+ "header",
379
+ "EthereumHeader"
380
+ ],
381
+ [
382
+ "mmr_root",
383
+ "H256"
384
+ ]
385
+ ]
386
+ }
387
+ }
388
+ },
389
+ {
390
+ "runtime_range": [
391
+ 23,
392
+ 23
393
+ ],
394
+ "types": {
395
+ "RelayHeaderId": "Vec<u8>",
396
+ "RelayHeaderParcel": "Vec<u8>",
397
+ "RelayAffirmationId": {
398
+ "type": "struct",
399
+ "type_mapping": [
400
+ [
401
+ "relay_header_id",
402
+ "Vec<u8>"
403
+ ],
404
+ [
405
+ "round",
406
+ "u32"
407
+ ],
408
+ [
409
+ "index",
410
+ "u32"
411
+ ]
412
+ ]
413
+ },
414
+ "RelayAffirmationT": {
415
+ "type": "struct",
416
+ "type_mapping": [
417
+ [
418
+ "relayer",
419
+ "AccountId"
420
+ ],
421
+ [
422
+ "relay_header_parcels",
423
+ "Vec<u8>"
424
+ ],
425
+ [
426
+ "bond",
427
+ "Balance"
428
+ ],
429
+ [
430
+ "maybe_extended_relay_affirmation_id",
431
+ "Option<Vec<u8>>"
432
+ ],
433
+ [
434
+ "verified",
435
+ "bool"
436
+ ]
437
+ ]
438
+ },
439
+ "RelayProofs": "Vec<u8>"
440
+ }
441
+ }
442
+ ],
2
443
  "types": {
444
+ "Balance": "u64",
3
445
  "Address": "AccountId",
4
446
  "LookupSource": "AccountId",
5
447
  "BalanceLock": {
@@ -16,6 +458,14 @@
16
458
  [
17
459
  "lock_reasons",
18
460
  "LockReasons"
461
+ ],
462
+ [
463
+ "amount",
464
+ "Balance"
465
+ ],
466
+ [
467
+ "reasons",
468
+ "Reasons"
19
469
  ]
20
470
  ]
21
471
  },
@@ -102,6 +552,14 @@
102
552
  [
103
553
  "reserved_kton",
104
554
  "Balance"
555
+ ],
556
+ [
557
+ "misc_frozen",
558
+ "Balance"
559
+ ],
560
+ [
561
+ "fee_frozen",
562
+ "Balance"
105
563
  ]
106
564
  ]
107
565
  },
@@ -157,6 +615,18 @@
157
615
  [
158
616
  "claimed_rewards",
159
617
  "Vec<EraIndex>"
618
+ ],
619
+ [
620
+ "total",
621
+ "Compact<Balance>"
622
+ ],
623
+ [
624
+ "active",
625
+ "Compact<Balance>"
626
+ ],
627
+ [
628
+ "unlocking",
629
+ "Vec<UnlockChunk>"
160
630
  ]
161
631
  ]
162
632
  },
@@ -220,6 +690,10 @@
220
690
  [
221
691
  "power",
222
692
  "Power"
693
+ ],
694
+ [
695
+ "value",
696
+ "Compact<Balance>"
223
697
  ]
224
698
  ]
225
699
  },
@@ -486,32 +960,6 @@
486
960
  ]
487
961
  ]
488
962
  },
489
- "EthereumRelayHeaderParcel": {
490
- "type": "struct",
491
- "type_mapping": [
492
- [
493
- "header",
494
- "EthereumHeader"
495
- ],
496
- [
497
- "mmr_root",
498
- "H256"
499
- ]
500
- ]
501
- },
502
- "EthereumRelayProofs": {
503
- "type": "struct",
504
- "type_mapping": [
505
- [
506
- "ethash_proof",
507
- "Vec<EthashProof>"
508
- ],
509
- [
510
- "mmr_proof",
511
- "Vec<H256>"
512
- ]
513
- ]
514
- },
515
963
  "OtherSignature": {
516
964
  "type": "enum",
517
965
  "type_mapping": [
@@ -540,106 +988,6 @@
540
988
  ]
541
989
  },
542
990
  "AddressT": "[u8; 20]",
543
- "MerkleMountainRangeRootLog": {
544
- "type": "struct",
545
- "type_mapping": [
546
- [
547
- "prefix",
548
- "[u8; 4]"
549
- ],
550
- [
551
- "mmr_root",
552
- "Hash"
553
- ]
554
- ]
555
- },
556
- "RelayHeaderId": "EthereumBlockNumber",
557
- "RelayHeaderParcel": "EthereumRelayHeaderParcel",
558
- "RelayProofs": "EthereumRelayProofs",
559
- "RelayAffirmationId": {
560
- "type": "struct",
561
- "type_mapping": [
562
- [
563
- "game_id",
564
- "EthereumBlockNumber"
565
- ],
566
- [
567
- "round",
568
- "u32"
569
- ],
570
- [
571
- "index",
572
- "u32"
573
- ]
574
- ]
575
- },
576
- "RelayAffirmationT": {
577
- "type": "struct",
578
- "type_mapping": [
579
- [
580
- "relayer",
581
- "AccountId"
582
- ],
583
- [
584
- "relay_header_parcels",
585
- "EthereumRelayHeaderParcel"
586
- ],
587
- [
588
- "bond",
589
- "Balance"
590
- ],
591
- [
592
- "maybe_extended_relay_affirmation_id",
593
- "Option<RelayAffirmationId>"
594
- ],
595
- [
596
- "verified",
597
- "bool"
598
- ]
599
- ]
600
- },
601
- "RelayVotingState": {
602
- "type": "struct",
603
- "type_mapping": [
604
- [
605
- "ayes",
606
- "Vec<AccountId>"
607
- ],
608
- [
609
- "nays",
610
- "Vec<AccountId>"
611
- ]
612
- ]
613
- },
614
- "ProxyType": {
615
- "type": "enum",
616
- "type_mapping": [
617
- [
618
- "Any",
619
- "Null"
620
- ],
621
- [
622
- "NonTransfer",
623
- "Null"
624
- ],
625
- [
626
- "Governance",
627
- "Null"
628
- ],
629
- [
630
- "Staking",
631
- "Null"
632
- ],
633
- [
634
- "IdentityJudgement",
635
- "Null"
636
- ],
637
- [
638
- "EthereumBridge",
639
- "Null"
640
- ]
641
- ]
642
- },
643
991
  "BalancesRuntimeDispatchInfo": {
644
992
  "type": "struct",
645
993
  "type_mapping": [