Skip to content

Commit a66b92b

Browse files
Merge pull request #128 from threefoldtech/development_council
Add council module to tfchain client
2 parents fff0b66 + b558f15 commit a66b92b

File tree

4 files changed

+121
-0
lines changed

4 files changed

+121
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import 'package:moment_dart/moment_dart.dart';
2+
3+
class CouncilProposal {
4+
int index;
5+
int threshold;
6+
Moment end;
7+
String hash;
8+
String module;
9+
String method;
10+
Map<String, dynamic> args;
11+
bool active;
12+
13+
CouncilProposal({
14+
required this.index,
15+
required this.threshold,
16+
required this.end,
17+
required this.hash,
18+
required this.module,
19+
required this.method,
20+
required this.args,
21+
required this.active,
22+
});
23+
}

packages/tfchain_client/lib/src/client.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class QueryClient {
1414
QueryBridge? _bridge;
1515
QueryTFTPrice? _price;
1616
Dao.QueryDao? _dao;
17+
Council.QueryCouncil? _council;
1718

1819
QueryClient(this.url) {}
1920

@@ -62,6 +63,11 @@ class QueryClient {
6263
return _dao!;
6364
}
6465

66+
Council.QueryCouncil get council {
67+
if (_council == null) _council = Council.QueryCouncil(this);
68+
return _council!;
69+
}
70+
6571
void _checkInputs() {
6672
if (url.isEmpty) {
6773
throw FormatException("URL should be provided");
@@ -146,6 +152,12 @@ class Client extends QueryClient {
146152
return _dao as Dao.Dao;
147153
}
148154

155+
@override
156+
Council.Council get council {
157+
if (_council == null) _council = Council.Council(this);
158+
return _council as Council.Council;
159+
}
160+
149161
KVStore get kvStore {
150162
if (_kvStore == null) _kvStore = KVStore(this);
151163
return _kvStore as KVStore;
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import 'package:moment_dart/moment_dart.dart';
2+
import 'package:polkadart/multisig/multisig_base.dart';
3+
import 'package:polkadart_keyring/polkadart_keyring.dart';
4+
import 'package:tfchain_client/generated/dev/types/pallet_collective/votes.dart';
5+
import 'package:tfchain_client/models/council.dart';
6+
import 'package:tfchain_client/models/dao.dart';
7+
import 'package:tfchain_client/tfchain_client.dart';
8+
9+
class QueryCouncil {
10+
final QueryClient client;
11+
QueryCouncil(this.client);
12+
13+
Future<List<String>> getProposals() async {
14+
final hashesJson = await client.api.query.council.proposals();
15+
List<String> hashes =
16+
hashesJson.map((hashList) => hashList.toHex()).toList();
17+
return hashes;
18+
}
19+
20+
Future<ProposalInfo?> getProposal({required String hash}) async {
21+
try {
22+
final proposal =
23+
await client.api.query.council.proposalOf(hash.hexToListInt());
24+
final ProposalJson = proposal!.toJson();
25+
return ProposalInfo.fromJson(ProposalJson);
26+
} catch (error) {
27+
print(error);
28+
return null;
29+
}
30+
}
31+
32+
Future<Votes> getProposalVotes({required String hash}) async {
33+
final votes = await client.api.query.council.voting(hash.hexToListInt());
34+
return votes!;
35+
}
36+
37+
Future<List<String>> members() async {
38+
final keyring = Keyring();
39+
final members = await client.api.query.council.members();
40+
return members.map((member) => keyring.encodeAddress(member)).toList();
41+
}
42+
43+
Future<List<CouncilProposal>> get() async {
44+
List<String> hashes = await getProposals();
45+
46+
List<CouncilProposal> proposals = [];
47+
48+
for (int i = 0; i < hashes.length; i++) {
49+
final proposal = await getProposal(hash: hashes[i]);
50+
final proposalVotes = await getProposalVotes(hash: hashes[i]);
51+
final nowBlock = await client.api.query.system.number();
52+
final timeUntilEnd = (proposalVotes.end - nowBlock) * 6;
53+
if (proposal != null) {
54+
final p = CouncilProposal(
55+
index: proposalVotes.index,
56+
threshold: proposalVotes.threshold,
57+
end: Moment(DateTime.now()).add(Duration(seconds: timeUntilEnd)),
58+
hash: hashes[i],
59+
module: proposal.module,
60+
method: proposal.method,
61+
args: proposal.args,
62+
active: proposalVotes.end > nowBlock);
63+
proposals.add(p);
64+
}
65+
}
66+
67+
return proposals;
68+
}
69+
}
70+
71+
class Council extends QueryCouncil {
72+
Council(Client this.client) : super(client);
73+
final Client client;
74+
75+
Future<Votes> vote({required String hash, required bool approve}) async {
76+
final votes = await getProposalVotes(hash: hash);
77+
final extrinsic = client.api.tx.council.vote(
78+
index: BigInt.from(votes.index),
79+
proposal: hash.hexToListInt(),
80+
approve: approve);
81+
await client.apply(extrinsic);
82+
83+
return getProposalVotes(hash: hash);
84+
}
85+
}

packages/tfchain_client/lib/tfchain_client.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import 'package:polkadart/scale_codec.dart';
1616
import 'package:polkadart_keyring/polkadart_keyring.dart';
1717
import 'package:signer/signer.dart' as Signer;
1818
import 'package:tfchain_client/generated/dev/types/tfchain_runtime/runtime_call.dart';
19+
import 'package:tfchain_client/src/council.dart' as Council;
1920
import 'package:tfchain_client/src/error_mapper.dart';
2021
import 'package:tfchain_client/src/balances.dart' as balance;
2122
import 'package:tfchain_client/src/contracts.dart';

0 commit comments

Comments
 (0)