|
| 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 | +} |
0 commit comments