Skip to content

Commit 23ce369

Browse files
Merge pull request #24 from codescalers/main_tfchain_errors
Add error mapper to the tfchain client
2 parents 50ed15d + 1524871 commit 23ce369

File tree

4 files changed

+124
-3
lines changed

4 files changed

+124
-3
lines changed

packages/tfchain_client/lib/src/client.dart

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -291,8 +291,8 @@ class Client extends QueryClient {
291291
await AuthorApi(provider!).submitAndWatchExtrinsic(
292292
extrinsic,
293293
(p0) async {
294-
print("Extrinsic result: ${p0.type} - ${p0.value}");
295294
if (p0.type == 'inBlock') {
295+
print("Extrinsic result: ${p0.type} - ${p0.value}");
296296
final finalizedBlockHash = p0.value;
297297
final moduleHash =
298298
Hasher.twoxx128.hash(Uint8List.fromList('System'.codeUnits));
@@ -322,8 +322,20 @@ class Client extends QueryClient {
322322
// TODO: get the error name and type
323323
final error = event.value.value["DispatchError"].value;
324324
final errorType = event.value.value["DispatchError"].key;
325-
_complete.completeError(
326-
"Failed to apply extrinsic: ${errorType}${error}");
325+
String? errorName;
326+
try {
327+
if (errorType == "Module")
328+
errorName = Errors(
329+
moduleIndex: error["index"],
330+
errorIndex:
331+
dynamicListToUint8List(error["error"]))
332+
.decode();
333+
} catch (e) {
334+
_complete.completeError("Failed to apply extrinsic: $e");
335+
}
336+
if (errorName != null)
337+
_complete.completeError(
338+
"Failed to apply extrinsic: $errorName");
327339
} else if (event.key == runtimeCall.runtimeType.toString()) {
328340
targetModuleEventOccur = true;
329341
} else if (targetModuleEventOccur &&
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import 'dart:typed_data';
2+
3+
import 'package:polkadart/scale_codec.dart';
4+
import 'package:tfchain_client/generated/dev/types/pallet_balances/pallet/error.dart'
5+
as balances;
6+
import 'package:tfchain_client/generated/dev/types/frame_system/pallet/error.dart'
7+
as system;
8+
import 'package:tfchain_client/generated/dev/types/pallet_utility/pallet/error.dart'
9+
as utility;
10+
import 'package:tfchain_client/generated/dev/types/pallet_scheduler/pallet/error.dart'
11+
as scheduler;
12+
import 'package:tfchain_client/generated/dev/types/substrate_validator_set/pallet/error.dart'
13+
as validatorSet;
14+
import 'package:tfchain_client/generated/dev/types/pallet_session/pallet/error.dart'
15+
as session;
16+
import 'package:tfchain_client/generated/dev/types/pallet_grandpa/pallet/error.dart'
17+
as grandpa;
18+
import 'package:tfchain_client/generated/dev/types/pallet_tfgrid/pallet/error.dart'
19+
as tfgrid;
20+
import 'package:tfchain_client/generated/dev/types/pallet_smart_contract/pallet/error.dart'
21+
as smartContract;
22+
23+
import 'package:tfchain_client/generated/dev/types/pallet_tft_bridge/pallet/error.dart'
24+
as tftBridge;
25+
import 'package:tfchain_client/generated/dev/types/pallet_tft_price/pallet/error.dart'
26+
as tftPrice;
27+
import 'package:tfchain_client/generated/dev/types/pallet_burning/pallet/error.dart'
28+
as burning;
29+
import 'package:tfchain_client/generated/dev/types/pallet_kvstore/pallet/error.dart'
30+
as kvstore;
31+
import 'package:tfchain_client/generated/dev/types/pallet_collective/pallet/error.dart'
32+
as council;
33+
import 'package:tfchain_client/generated/dev/types/pallet_membership/pallet/error.dart'
34+
as membership;
35+
import 'package:tfchain_client/generated/dev/types/pallet_dao/pallet/error.dart'
36+
as dao;
37+
38+
import 'package:tfchain_client/generated/dev/types/pallet_validator/pallet/error.dart'
39+
as validator;
40+
41+
class Errors {
42+
final int moduleIndex;
43+
final Uint8List errorIndex;
44+
45+
Errors({required this.moduleIndex, required this.errorIndex});
46+
47+
_findModule() {
48+
switch (moduleIndex) {
49+
case 0:
50+
return system.$ErrorCodec();
51+
case 3:
52+
return utility.$ErrorCodec();
53+
case 4:
54+
return scheduler.$ErrorCodec();
55+
case 10:
56+
return validatorSet.$ErrorCodec();
57+
case 11:
58+
return session.$ErrorCodec();
59+
case 13:
60+
return grandpa.$ErrorCodec();
61+
case 20:
62+
return balances.$ErrorCodec();
63+
case 25:
64+
return tfgrid.$ErrorCodec();
65+
case 26:
66+
return smartContract.$ErrorCodec();
67+
case 27:
68+
return tftBridge.$ErrorCodec();
69+
case 28:
70+
return tftPrice.$ErrorCodec();
71+
case 29:
72+
return burning.$ErrorCodec();
73+
case 30:
74+
return kvstore.$ErrorCodec();
75+
case 40:
76+
return council.$ErrorCodec();
77+
case 41:
78+
return membership.$ErrorCodec();
79+
case 43:
80+
return dao.$ErrorCodec();
81+
case 50:
82+
return validator.$ErrorCodec();
83+
default:
84+
throw Exception(
85+
'Error: Invalid module index. module: "$moduleIndex", error: "$errorIndex"');
86+
}
87+
}
88+
89+
decode() {
90+
final module = _findModule();
91+
try {
92+
final x = module.decode(Input.fromBytes(errorIndex));
93+
return x.variantName;
94+
} catch (e) {
95+
throw Exception(
96+
'Error: Invalid error index. module: "$moduleIndex", error: "$errorIndex"');
97+
}
98+
}
99+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1+
import 'dart:typed_data';
2+
13
bool isValidSeed(String seed) {
24
final RegExp hexRegex = RegExp(r'^[0-9a-fA-F]+$');
35
return hexRegex.hasMatch(seed);
46
}
7+
8+
Uint8List dynamicListToUint8List(dynamicList) {
9+
List<int> intList = dynamicList.cast<int>().toList();
10+
Uint8List data = Uint8List.fromList(intList);
11+
return data;
12+
}

packages/tfchain_client/lib/tfchain_client.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import 'package:polkadart/polkadart.dart'
1515
import 'package:polkadart/scale_codec.dart';
1616
import 'package:polkadart_keyring/polkadart_keyring.dart';
1717
import 'package:tfchain_client/generated/dev/types/tfchain_runtime/runtime_call.dart';
18+
import 'package:tfchain_client/src/Error_mapping.dart';
1819
import 'package:tfchain_client/src/balances.dart' as balance;
1920
import 'package:tfchain_client/src/contracts.dart';
2021
import 'package:tfchain_client/generated/dev/dev.dart' as polkadot;
@@ -30,5 +31,6 @@ import 'package:bip39/bip39.dart';
3031
import 'package:convert/convert.dart';
3132
import 'package:signer/signer.dart' as Signer;
3233
import 'package:collection/collection.dart';
34+
import 'package:tfchain_client/src/utils.dart';
3335

3436
part 'src/client.dart';

0 commit comments

Comments
 (0)