scale.rb 0.2.4 → 0.2.9

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.
@@ -9,7 +9,8 @@ module Scale
9
9
  Blake2b.hex data, Blake2b::Key.none, 32
10
10
  end
11
11
 
12
- def self.decode(scale_bytes, metadata)
12
+ def self.decode(scale_bytes)
13
+ metadata = Scale::TypeRegistry.instance.metadata
13
14
  result = {}
14
15
 
15
16
  extrinsic_length = Compact.decode(scale_bytes).value
@@ -83,11 +84,11 @@ module Scale
83
84
  result[:call_index] = scale_bytes.get_next_bytes(2).bytes_to_hex[2..]
84
85
 
85
86
  else
86
- raise "Extrinsics version #{version_info} is not implemented"
87
+ raise "Extrinsic version #{version_info} is not implemented"
87
88
  end
88
89
 
89
90
  if result[:call_index]
90
- call_module, call = metadata.value.call_index[result[:call_index]]
91
+ call_module, call = metadata.call_index[result[:call_index]]
91
92
 
92
93
  result[:call_function] = call[:name].downcase
93
94
  result[:call_module] = call_module[:name].downcase
@@ -95,9 +96,10 @@ module Scale
95
96
  # decode params
96
97
  result[:params_raw] = scale_bytes.get_remaining_bytes.bytes_to_hex
97
98
  result[:params] = call[:args].map do |arg|
98
- type = Scale::Types.get(arg[:type])
99
- arg_obj = type.decode(scale_bytes)
100
- {name: arg[:name], type: type.name, value: arg_obj.value, value_raw: "0x#{arg_obj.encode}" }
99
+ type = arg[:type]
100
+ scale_type = Scale::Types.get(type)
101
+ obj = scale_type.decode(scale_bytes)
102
+ {name: arg[:name], type: type, scale_type: scale_type, value: obj.value }
101
103
  end
102
104
  end
103
105
 
@@ -107,24 +109,42 @@ module Scale
107
109
  Extrinsic.new result
108
110
  end
109
111
 
110
- def encode(metadata)
111
- puts metadata.value.value["modules"]
112
+ def encode
112
113
  result = "04" + self.value[:call_index]
113
114
 
114
- result = result +
115
- self.value[:params].map do |param|
116
- param[:type].constantize.new(param[:value]).encode
117
- end.join
115
+ result += self.value[:params].map do |param|
116
+ Scale::Types.get(param[:type]).new(param[:value]).encode
117
+ end.join
118
+
119
+ "0x" + Compact.new(result.length / 2).encode + result
120
+ end
118
121
 
119
- result = "0x" + Compact.new(result.length / 2).encode + result
120
- return result
122
+ def to_human
123
+ @value
124
+ end
125
+ end
126
+
127
+ class Phase
128
+ include Enum
129
+ items isApplyExtrinsic: "Bool", asApplyExtrinsic: "U32", isFinalization: "Bool", isInitialization: "Bool"
130
+ end
131
+
132
+ class EventRecords
133
+ include SingleValue
134
+
135
+ def self.decode(scale_bytes)
136
+
137
+ length = Compact.decode(scale_bytes).value
138
+
139
+ length.times do |i|
140
+ ev = EventRecord.decode(scale_bytes)
141
+ end
121
142
  end
122
143
  end
123
144
 
124
145
  class EventRecord
125
146
  include SingleValue
126
147
 
127
- # 0x0c000000000000001027000001010000010000000400be07e2c28688db5368445c33d32b3c7bcad15dab1ec802ba8cccc1c22b86574f6992da89ff412eaf9bafac4024
128
148
  def self.decode(scale_bytes)
129
149
  metadata = Scale::TypeRegistry.instance.metadata
130
150
 
@@ -137,22 +157,72 @@ module Scale
137
157
 
138
158
  type = scale_bytes.get_next_bytes(2).bytes_to_hex[2..]
139
159
  event = metadata.event_index[type][1]
140
- mod = metadata.event_index[type][0]
160
+ # mod = metadata.event_index[type][0]
141
161
 
142
162
  result[:params] = []
143
163
  event[:args].each do |arg_type|
144
- value = Scale::Types.get(arg_type).decode(scale_bytes).value
164
+ value = Scale::Types.get(arg_type).decode(scale_bytes).to_human
145
165
  result[:params] << {
166
+ name: event[:name],
146
167
  type: arg_type,
147
168
  value: value
148
169
  }
149
170
  end
150
171
 
151
- result[:topics] = Scale::Types.get('Vec<Hash>').decode(scale_bytes).value.map(&:value)
172
+ result[:topics] = Scale::Types.get("Vec<Hash>").decode(scale_bytes).value.map(&:value)
152
173
 
153
174
  EventRecord.new(result)
154
175
  end
155
176
  end
156
177
 
178
+ # log
179
+ class Other < Bytes; end
180
+
181
+ class AuthoritiesChange
182
+ include Vec
183
+ inner_type "AccountId"
184
+ end
185
+
186
+ class ConsensusEngineId < VecU8Length4; end
187
+
188
+ class ChangesTrieRoot < Bytes; end
189
+
190
+ class SealV0
191
+ include Struct
192
+ items(
193
+ slot: "U64",
194
+ signature: "Signature"
195
+ )
196
+ end
197
+
198
+ class Consensus
199
+ include Struct
200
+ items(
201
+ engine: "ConsensusEngineId",
202
+ data: "Hex"
203
+ )
204
+ end
205
+
206
+ class Seal
207
+ include Struct
208
+ items(
209
+ engine: "ConsensusEngineId",
210
+ data: "Hex"
211
+ )
212
+ end
213
+
214
+ class PreRuntime
215
+ include Struct
216
+ items(
217
+ engine: "ConsensusEngineId",
218
+ data: "Hex"
219
+ )
220
+ end
221
+
222
+ class LogDigest
223
+ include Enum
224
+ items %w[Other AuthoritiesChange ChangesTrieRoot SealV0 Consensus Seal PreRuntime]
225
+ end
226
+
157
227
  end
158
228
  end
@@ -46,24 +46,34 @@ module Scale
46
46
  BYTE_LENGTH = 16
47
47
  end
48
48
 
49
+ class U256
50
+ include FixedWidthUInt
51
+ BYTE_LENGTH = 32
52
+ end
53
+
49
54
  class I8
50
55
  include FixedWidthInt
56
+ BYTE_LENGTH = 1
51
57
  end
52
58
 
53
59
  class I16
54
60
  include FixedWidthInt
61
+ BYTE_LENGTH = 2
55
62
  end
56
63
 
57
64
  class I32
58
65
  include FixedWidthInt
66
+ BYTE_LENGTH = 4
59
67
  end
60
68
 
61
69
  class I64
62
70
  include FixedWidthInt
71
+ BYTE_LENGTH = 8
63
72
  end
64
73
 
65
74
  class I128
66
75
  include FixedWidthInt
76
+ BYTE_LENGTH = 16
67
77
  end
68
78
 
69
79
  class Compact
@@ -303,7 +313,7 @@ module Scale
303
313
  value /= 1000
304
314
  end
305
315
 
306
- CompactMoment.new Time.at(seconds_since_epoch_integer).to_datetime
316
+ CompactMoment.new Time.at(value).to_datetime.strftime("%F %T")
307
317
  end
308
318
  end
309
319
 
@@ -791,5 +801,19 @@ module Scale
791
801
  )
792
802
  end
793
803
 
804
+ class VecH512Length2
805
+ include SingleValue
806
+
807
+ def self.decode(scale_bytes)
808
+ end
809
+
810
+ def encode
811
+ "0x" + self.value.map do |item|
812
+ item[2..]
813
+ end.join
814
+ end
815
+ end
816
+
817
+
794
818
  end
795
819
  end
@@ -1,3 +1,3 @@
1
1
  module Scale
2
- VERSION = "0.2.4".freeze
2
+ VERSION = "0.2.9".freeze
3
3
  end
@@ -1,111 +1,299 @@
1
1
  {
2
2
  "types": {
3
- "EpochDuration": "u64",
4
- "EraIndex": "u32",
5
- "Index": "U64",
6
- "RingBalanceOf": "u128",
7
- "KtonBalanceOf": "u128",
8
- "ExtendedBalance": "u128",
9
- "Keys": {
3
+ "Address": "AccountId",
4
+ "LookupSource": "AccountId",
5
+ "BalanceLock": {
10
6
  "type": "struct",
11
7
  "type_mapping": [
12
- ["grandpa", "AccountId"],
13
- ["babe", "AccountId"]
8
+ ["id", "LockIdentifier"],
9
+ ["lock_for", "LockFor"],
10
+ ["lock_reasons", "LockReasons"]
14
11
  ]
15
12
  },
16
- "ValidatorPrefs": "ValidatorPrefsLegacy",
17
- "StakingBalance": {
13
+ "LockFor": {
18
14
  "type": "enum",
19
15
  "type_mapping": [
20
- ["Ring", "RingBalanceOf"],
21
- ["Kton", "KtonBalanceOf"]
16
+ ["Common", "Common"],
17
+ ["Staking", "StakingLock"]
18
+ ]
19
+ },
20
+ "Common": {
21
+ "type": "struct",
22
+ "type_mapping": [
23
+ ["amount", "Balance"]
24
+ ]
25
+ },
26
+ "StakingLock": {
27
+ "type": "struct",
28
+ "type_mapping": [
29
+ ["staking_amount", "Balance"],
30
+ ["unbondings", "Vec<Unbonding>"]
31
+ ]
32
+ },
33
+ "LockReasons": {
34
+ "type": "enum",
35
+ "type_mapping": [
36
+ ["Fee", "Null"],
37
+ ["Misc", "Null"],
38
+ ["All", "Null"]
39
+ ]
40
+ },
41
+ "Unbonding": {
42
+ "type": "struct",
43
+ "type_mapping": [
44
+ ["amount", "Balance"],
45
+ ["until", "BlockNumber"]
46
+ ]
47
+ },
48
+ "AccountData": {
49
+ "type": "struct",
50
+ "type_mapping": [
51
+ ["free", "Balance"],
52
+ ["reserved", "Balance"],
53
+ ["free_kton", "Balance"],
54
+ ["reserved_kton", "Balance"]
55
+ ]
56
+ },
57
+ "RingBalance": "Balance",
58
+ "KtonBalance": "Balance",
59
+ "TsInMs": "u64",
60
+ "Power": "u32",
61
+ "DepositId": "U256",
62
+ "StakingBalanceT": {
63
+ "type": "enum",
64
+ "type_mapping": [
65
+ ["RingBalance", "Balance"],
66
+ ["KtonBalance", "Balance"]
67
+ ]
68
+ },
69
+ "StakingLedgerT": {
70
+ "type": "struct",
71
+ "type_mapping": [
72
+ ["stash", "AccountId"],
73
+ ["active_ring", "Compact<Balance>"],
74
+ ["active_deposit_ring", "Compact<Balance>"],
75
+ ["active_kton", "Compact<Balance>"],
76
+ ["deposit_items", "Vec<TimeDepositItem>"],
77
+ ["ring_staking_lock", "StakingLock"],
78
+ ["kton_staking_lock", "StakingLock"],
79
+ ["claimed_rewards", "Vec<EraIndex>"]
22
80
  ]
23
81
  },
24
82
  "TimeDepositItem": {
25
83
  "type": "struct",
26
84
  "type_mapping": [
27
- ["value", "Compact<RingBalanceOf>"],
28
- ["start_time", "Compact<Moment>"],
29
- ["expire_time", "Compact<Moment>"]
85
+ ["value", "Compact<Balance>"],
86
+ ["start_time", "Compact<TsInMs>"],
87
+ ["expire_time", "Compact<TsInMs>"]
88
+ ]
89
+ },
90
+ "ExposureT": {
91
+ "type": "struct",
92
+ "type_mapping": [
93
+ ["own_ring_balance", "Compact<Balance>"],
94
+ ["own_kton_balance", "Compact<Balance>"],
95
+ ["own_power", "Power"],
96
+ ["total_power", "Power"],
97
+ ["others", "Vec<IndividualExposure>"]
98
+ ]
99
+ },
100
+ "IndividualExposure": {
101
+ "type": "struct",
102
+ "type_mapping": [
103
+ ["who", "AccountId"],
104
+ ["ring_balance", "Compact<Balance>"],
105
+ ["kton_balance", "Compact<Balance>"],
106
+ ["power", "Power"]
107
+ ]
108
+ },
109
+ "ElectionResultT": {
110
+ "type": "struct",
111
+ "type_mapping": [
112
+ ["elected_stashes", "Vec<AccountId>"],
113
+ ["exposures", "Vec<(AccountId, ExposureT)>"],
114
+ ["compute", "ElectionCompute"]
115
+ ]
116
+ },
117
+ "RKT": {
118
+ "type": "struct",
119
+ "type_mapping": [
120
+ ["r", "Balance"],
121
+ ["k", "Balance"]
30
122
  ]
31
123
  },
32
- "UnlockChunk": {
124
+ "SpanRecord": {
33
125
  "type": "struct",
34
126
  "type_mapping": [
35
- ["value", "StakingBalance"],
36
- ["era", "Compact<EraIndex>"],
37
- ["is_time_deposit", "bool"]
127
+ ["slashed", "RKT"],
128
+ ["paid_out", "RKT"]
38
129
  ]
39
130
  },
40
- "StakingLedgers": {
131
+ "UnappliedSlash": {
41
132
  "type": "struct",
42
133
  "type_mapping": [
43
- ["stash", "AccountId"],
44
- ["total_ring", "Compact<RingBalanceOf>"],
45
- ["total_deposit_ring", "Compact<RingBalanceOf>"],
46
- ["active_ring", "Compact<RingBalanceOf>"],
47
- ["active_deposit_ring", "Compact<RingBalanceOf>"],
48
- ["total_kton", "Compact<KtonBalanceOf>"],
49
- ["active_kton", "Compact<KtonBalanceOf>"],
50
- ["deposit_items", "Vec<TimeDepositItem>"],
51
- ["unlocking", "Vec<UnlockChunk>"]
134
+ ["validator", "AccountId"],
135
+ ["own", "RKT"],
136
+ ["others", "Vec<(AccountId, RKT)>"],
137
+ ["reporters", "Vec<AccountId>"],
138
+ ["payout", "RKT"]
52
139
  ]
53
140
  },
54
- "IndividualExpo": {
141
+ "TreasuryProposal": {
55
142
  "type": "struct",
56
143
  "type_mapping": [
57
- ["who", "AccountId"],
58
- ["value", "ExtendedBalance"]
144
+ ["proposer", "AccountId"],
145
+ ["beneficiary", "AccountId"],
146
+ ["ring_value", "Balance"],
147
+ ["kton_value", "Balance"],
148
+ ["ring_bond", "Balance"],
149
+ ["kton_bond", "Balance"]
59
150
  ]
60
151
  },
61
- "Exposures": {
152
+ "MappedRing": "u128",
153
+ "EthereumTransactionIndex": "(H256, u64)",
154
+ "EthereumHeaderBrief": {
62
155
  "type": "struct",
63
156
  "type_mapping": [
64
- ["total", "ExtendedBalance"],
65
- ["own", "ExtendedBalance"],
66
- ["others", "Vec<IndividualExpo>"]
157
+ ["total_difficulty", "U256"],
158
+ ["parent_hash", "H256"],
159
+ ["number", "EthereumBlockNumber"],
160
+ ["relayer", "AccountId"]
161
+ ]
162
+ },
163
+ "EthereumBlockNumber": "u64",
164
+ "EthereumHeaderThingWithProof": {
165
+ "type": "struct",
166
+ "type_mapping": [
167
+ ["header", "EthereumHeader"],
168
+ ["ethash_proof", "Vec<EthashProof>"],
169
+ ["mmr_root", "H256"],
170
+ ["mmr_proof", "Vec<H256>"]
171
+ ]
172
+ },
173
+ "EthereumHeaderThing": {
174
+ "type": "struct",
175
+ "type_mapping": [
176
+ ["header", "EthereumHeader"],
177
+ ["mmr_root", "H256"]
178
+ ]
179
+ },
180
+ "EthereumHeader": {
181
+ "type": "struct",
182
+ "type_mapping": [
183
+ ["parent_hash", "H256"],
184
+ ["timestamp", "u64"],
185
+ ["number", "EthereumBlockNumber"],
186
+ ["author", "EthereumAddress"],
187
+ ["transactions_root", "H256"],
188
+ ["uncles_hash", "H256"],
189
+ ["extra_data", "Bytes"],
190
+ ["state_root", "H256"],
191
+ ["receipts_root", "H256"],
192
+ ["log_bloom", "Bloom"],
193
+ ["gas_used", "U256"],
194
+ ["gas_limit", "U256"],
195
+ ["difficulty", "U256"],
196
+ ["seal", "Vec<Bytes>"],
197
+ ["hash", "Option<H256>"]
198
+ ]
199
+ },
200
+ "EthereumAddress": "H160",
201
+ "Bloom": "[u8; 256]",
202
+ "H128": "[u8; 16]",
203
+ "EthashProof": {
204
+ "type": "struct",
205
+ "type_mapping": [
206
+ ["dag_nodes", "(H512, H512)"],
207
+ ["proof", "Vec<H128>"]
208
+ ]
209
+ },
210
+ "EthereumReceipt": {
211
+ "type": "struct",
212
+ "type_mapping": [
213
+ ["gas_used", "U256"],
214
+ ["log_bloom", "Bloom"],
215
+ ["logs", "Vec<LogEntry>"],
216
+ ["outcome", "TransactionOutcome"]
217
+ ]
218
+ },
219
+ "EthereumNetworkType": {
220
+ "type": "enum",
221
+ "type_mapping": [
222
+ ["Mainnet", "Null"],
223
+ ["Ropsten", "Null"]
224
+ ]
225
+ },
226
+ "RedeemFor": {
227
+ "type": "enum",
228
+ "type_mapping": [
229
+ ["Token", "Null"],
230
+ ["Deposit", "Null"]
231
+ ]
232
+ },
233
+ "EthereumReceiptProof": {
234
+ "type": "struct",
235
+ "type_mapping": [
236
+ ["index", "u64"],
237
+ ["proof", "Bytes"],
238
+ ["header_hash", "H256"]
239
+ ]
240
+ },
241
+ "EthereumReceiptProofThing": "(EthereumHeader, EthereumReceiptProof, MMRProof)",
242
+ "MMRProof": {
243
+ "type": "struct",
244
+ "type_mapping": [
245
+ ["member_leaf_index", "u64"],
246
+ ["last_leaf_index", "u64"],
247
+ ["proof", "Vec<H256>"]
248
+ ]
249
+ },
250
+ "OtherSignature": {
251
+ "type": "enum",
252
+ "type_mapping": [
253
+ ["Eth", "EcdsaSignature"],
254
+ ["Tron", "EcdsaSignature"]
255
+ ]
256
+ },
257
+ "EcdsaSignature": "[u8; 65]",
258
+ "OtherAddress": {
259
+ "type": "enum",
260
+ "type_mapping": [
261
+ ["Eth", "[u8; 20]"],
262
+ ["Tron", "[u8; 20]"]
67
263
  ]
68
264
  },
69
- "TokenBalance": "u128",
70
- "Currency": "u128",
71
- "CurrencyOf": "u128",
72
- "Auction": {
265
+ "AddressT": "[u8; 20]",
266
+ "MerkleMountainRangeRootLog": {
73
267
  "type": "struct",
74
268
  "type_mapping": [
75
- ["seller", "AccountId"],
76
- ["startAt", "Moment"],
77
- ["duration", "u64"],
78
- ["startingPrice", "TokenBalance"],
79
- ["endingPrice", "TokenBalance"],
80
- ["lastRecord", "TokenBalance"],
81
- ["lastBidder", "AccountId"],
82
- ["lastBidStartAt", "Moment"]
269
+ ["prefix", "[u8; 4]"],
270
+ ["mmr_root", "Hash"]
83
271
  ]
84
272
  },
85
- "DepositInfo": {
273
+ "Round": "u64",
274
+ "TcHeaderThingWithProof": "EthereumHeaderThingWithProof",
275
+ "TcHeaderThing": "EthereumHeaderThing",
276
+ "TcBlockNumber": "u64",
277
+ "TcHeaderHash": "H256",
278
+ "GameId": "TcBlockNumber",
279
+ "RelayProposalT": {
86
280
  "type": "struct",
87
281
  "type_mapping": [
88
- ["month", "Moment"],
89
- ["start_at", "Moment"],
90
- ["value", "CurrencyOf"],
91
- ["unit_interest", "u64"],
92
- ["claimed", "bool"]
282
+ ["relayer", "AccountId"],
283
+ ["bonded_proposal", "Vec<(Balance, TcHeaderThing)>"],
284
+ ["extend_from_header_hash", "Option<TcHeaderHash>"]
93
285
  ]
94
286
  },
95
- "Deposit": {
287
+ "BalancesRuntimeDispatchInfo": {
96
288
  "type": "struct",
97
289
  "type_mapping": [
98
- ["total_deposit", "CurrencyOf"],
99
- ["deposit_list", "Vec<DepositInfo>"]
290
+ ["usable_balance", "Balance"]
100
291
  ]
101
292
  },
102
- "Revenue": {
293
+ "StakingRuntimeDispatchInfo": {
103
294
  "type": "struct",
104
295
  "type_mapping": [
105
- ["team", "TokenBalance"],
106
- ["contribution", "TokenBalance"],
107
- ["ktoner", "TokenBalance"],
108
- ["lottery", "TokenBalance"]
296
+ ["power", "Power"]
109
297
  ]
110
298
  }
111
299
  }