Adjust for new elections (use API derives) (#2485)

This commit is contained in:
Jaco Greeff 2020-03-30 14:25:53 +02:00 committed by GitHub
parent df3005de27
commit fda189f15f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 77 additions and 103 deletions

View File

@ -8,10 +8,10 @@
"packages/*"
],
"resolutions": {
"@polkadot/api": "^1.9.0-beta.20",
"@polkadot/api-contract": "^1.9.0-beta.20",
"@polkadot/api": "^1.9.0-beta.21",
"@polkadot/api-contract": "^1.9.0-beta.21",
"@polkadot/keyring": "^2.7.0-beta.8",
"@polkadot/types": "^1.9.0-beta.20",
"@polkadot/types": "^1.9.0-beta.21",
"@polkadot/util": "^2.7.0-beta.8",
"@polkadot/util-crypto": "^2.7.0-beta.8",
"babel-core": "^7.0.0-bridge.0",

View File

@ -12,6 +12,6 @@
"license": "Apache-2.0",
"dependencies": {
"@babel/runtime": "^7.9.2",
"@polkadot/api-contract": "^1.9.0-beta.20"
"@polkadot/api-contract": "^1.9.0-beta.21"
}
}

View File

@ -1,4 +1,3 @@
/* eslint-disable @typescript-eslint/camelcase */
// Copyright 2017-2020 @polkadot/app-democracy authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.

View File

@ -4,7 +4,6 @@
import { ApiProps } from '@polkadot/react-api/types';
import { AccountId, VoteIndex } from '@polkadot/types/interfaces';
import { Codec } from '@polkadot/types/types';
import { ComponentProps } from './types';
import BN from 'bn.js';
@ -125,9 +124,9 @@ class Vote extends TxModal<Props, State> {
this.setState({ accountId });
if (accountId) {
(api.query.electionsPhragmen || api.query.elections)
.votesOf<([AccountId[]] & Codec) | AccountId[]>(accountId)
.then((existingVotes): void => {
api.derive.council
.votesOf(accountId)
.then(({ votes }): void => {
if (!this.props.electionsInfo) {
return;
}
@ -139,11 +138,7 @@ class Vote extends TxModal<Props, State> {
.concat(candidates.map((accountId): string => accountId.toString()));
this.setState({
votes: (
Array.isArray(existingVotes[0])
? existingVotes[0]
: (existingVotes as AccountId[])
)
votes: votes
.map((accountId): string => accountId.toString())
.filter((accountId): boolean => available.includes(accountId))
});

View File

@ -2,13 +2,12 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { DeriveElectionsInfo } from '@polkadot/api-derive/types';
import { DeriveCouncilVotes, DeriveElectionsInfo } from '@polkadot/api-derive/types';
import { AccountId, BlockNumber } from '@polkadot/types/interfaces';
import React from 'react';
import { Button } from '@polkadot/react-components';
import { useApi, useCall } from '@polkadot/react-hooks';
import { StorageKey } from '@polkadot/types';
import Candidates from './Candidates';
import Members from './Members';
@ -21,27 +20,9 @@ interface Props {
prime: AccountId | null;
}
function transformVotesPrev ([voters, casted]: [AccountId[], AccountId[][]]): Record<string, AccountId[]> {
return voters.reduce((result: Record<string, AccountId[]>, voter, index): Record<string, AccountId[]> => {
casted[index].forEach((candidate): void => {
const address = candidate.toString();
if (!result[address]) {
result[address] = [];
}
result[address].push(voter);
});
return result;
}, {});
}
function transformVotes (entries: [StorageKey, AccountId[]][]): Record<string, AccountId[]> {
return entries.reduce((result: Record<string, AccountId[]>, [key, casted]): Record<string, AccountId[]> => {
const voter = key.args[0] as AccountId;
casted.forEach((candidate): void => {
function transformVotes (entries: DeriveCouncilVotes): Record<string, AccountId[]> {
return entries.reduce((result: Record<string, AccountId[]>, [voter, { votes }]): Record<string, AccountId[]> => {
votes.forEach((candidate): void => {
const address = candidate.toString();
if (!result[address]) {
@ -59,15 +40,9 @@ function Overview ({ className, prime }: Props): React.ReactElement<Props> {
const { api } = useApi();
const bestNumber = useCall<BlockNumber>(api.derive.chain.bestNumber, []);
const electionsInfo = useCall<DeriveElectionsInfo>(api.derive.elections.info, []);
const allVotes = api.query.electionsPhragmen?.votesOf.creator.meta.type.asMap.linked.isTrue
// eslint-disable-next-line react-hooks/rules-of-hooks
? useCall<Record<string, AccountId[]>>(api.query.electionsPhragmen?.votesOf, [], {
transform: transformVotesPrev
})
// eslint-disable-next-line react-hooks/rules-of-hooks
: useCall<Record<string, AccountId[]>>(api.query.electionsPhragmen?.votesOf.entries as any, [], {
transform: transformVotes
});
const allVotes = useCall<Record<string, AccountId[]>>(api.derive.council.votes, [], {
transform: transformVotes
});
return (
<div className={className}>

View File

@ -23,7 +23,7 @@ function ProposalsBase ({ className, isApprovals, proposals }: Props): React.Rea
const { t } = useTranslation();
const { api } = useApi();
const { allAccounts } = useAccounts();
const members = useCall<[AccountId, Balance][]>(api.query.electionsPhragmen?.members || api.query.elections.members, []);
const members = useCall<[AccountId, Balance][]>((api.query.electionsPhragmen || api.query.elections).members, []);
const [isMember, setIsMember] = useState(false);
const history = useHistory();

View File

@ -20,7 +20,7 @@ interface Props {
function Submission ({ councilProposals, id, isDisabled }: Props): React.ReactElement<Props> | null {
const { t } = useTranslation();
const { api } = useApi();
const councilThreshold = useCall<number>(api.query.electionsPhragmen?.members || api.query.elections.members, [], {
const councilThreshold = useCall<number>((api.query.electionsPhragmen || api.query.elections).members, [], {
transform: (value: any[]): number =>
Math.ceil(value.length * 0.6)
});

View File

@ -31,7 +31,7 @@
"homepage": "https://github.com/polkadot-js/ui/tree/master/packages/ui-reactive#readme",
"dependencies": {
"@babel/runtime": "^7.9.2",
"@polkadot/api": "^1.9.0-beta.20",
"@polkadot/api": "^1.9.0-beta.21",
"@polkadot/extension-dapp": "^0.24.0-beta.0",
"rxjs-compat": "^6.5.4"
}

View File

@ -19,7 +19,7 @@ export default function useMembers (collective: 'council' | 'technicalCommittee'
const retrieved = (
collective === 'council'
// eslint-disable-next-line react-hooks/rules-of-hooks
? useCall<string[]>(hasAccounts && (api.query.electionsPhragmen?.members || api.query.elections.members), [], {
? useCall<string[]>(hasAccounts && ((api.query.electionsPhragmen || api.query.elections).members), [], {
transform: (accounts: [AccountId, Balance][]): string[] =>
accounts.map(([accountId]) => accountId.toString())
})

View File

@ -2,8 +2,9 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { DeriveCouncilVote } from '@polkadot/api-derive/types';
import { BareProps } from '@polkadot/react-api/types';
import { AccountId, AccountIndex, Address, BalanceOf } from '@polkadot/types/interfaces';
import { AccountId, AccountIndex, Address } from '@polkadot/types/interfaces';
import React from 'react';
import { useApi, useCall } from '@polkadot/react-hooks';
@ -16,15 +17,19 @@ interface Props extends BareProps {
params?: AccountId | AccountIndex | Address | string | Uint8Array | null;
}
function LockedVote ({ children, className, label, params }: Props): React.ReactElement<Props> {
function LockedVote ({ children, className, label, params }: Props): React.ReactElement<Props> | null {
const { api } = useApi();
const stakeOf = useCall<BalanceOf>(api.query.electionsPhragmen.stakeOf, [params]);
const info = useCall<DeriveCouncilVote>(api.derive.council.votesOf as any, [params]);
if (!info?.stake.gtn(0)) {
return null;
}
return (
<FormatBalance
className={className}
label={label}
value={stakeOf}
value={info?.stake}
>
{children}
</FormatBalance>

View File

@ -2836,56 +2836,56 @@ __metadata:
languageName: node
linkType: hard
"@polkadot/api-contract@npm:^1.9.0-beta.20":
version: 1.9.0-beta.20
resolution: "@polkadot/api-contract@npm:1.9.0-beta.20"
"@polkadot/api-contract@npm:^1.9.0-beta.21":
version: 1.9.0-beta.21
resolution: "@polkadot/api-contract@npm:1.9.0-beta.21"
dependencies:
"@babel/runtime": ^7.9.2
"@polkadot/api": 1.9.0-beta.20
"@polkadot/rpc-core": 1.9.0-beta.20
"@polkadot/types": 1.9.0-beta.20
"@polkadot/api": 1.9.0-beta.21
"@polkadot/rpc-core": 1.9.0-beta.21
"@polkadot/types": 1.9.0-beta.21
"@polkadot/util": ^2.7.0-beta.8
bn.js: ^5.1.1
rxjs: ^6.5.4
checksum: 2/acb336a5c586a9957972bec25903d5046131e2d50cec8bd355a1f3a4d8bd395649fca64bb24d90af083ec435d454254c000e79034d8e789d28ecc13ff412d33e
checksum: 2/17deb263029be98adcff905f88f1de1b9ece50764d56877d59315599db9c50f47dc0a3e89607d58a41d94c0e3dd8cc1f9447fa4e0c2acb3bf3f73b50cf45a5cf
languageName: node
linkType: hard
"@polkadot/api-derive@npm:1.9.0-beta.20":
version: 1.9.0-beta.20
resolution: "@polkadot/api-derive@npm:1.9.0-beta.20"
"@polkadot/api-derive@npm:1.9.0-beta.21":
version: 1.9.0-beta.21
resolution: "@polkadot/api-derive@npm:1.9.0-beta.21"
dependencies:
"@babel/runtime": ^7.9.2
"@polkadot/api": 1.9.0-beta.20
"@polkadot/rpc-core": 1.9.0-beta.20
"@polkadot/rpc-provider": 1.9.0-beta.20
"@polkadot/types": 1.9.0-beta.20
"@polkadot/api": 1.9.0-beta.21
"@polkadot/rpc-core": 1.9.0-beta.21
"@polkadot/rpc-provider": 1.9.0-beta.21
"@polkadot/types": 1.9.0-beta.21
"@polkadot/util": ^2.7.0-beta.8
"@polkadot/util-crypto": ^2.7.0-beta.8
bn.js: ^5.1.1
memoizee: ^0.4.14
rxjs: ^6.5.4
checksum: 2/3a40b00ceb80bfadf43ed12f0574f248e3eab67136f237d6bcf1868043183cc397d2dc6a154a3d36f59b1bd00be460afb64a508d466b9692514d310aa001af27
checksum: 2/6b5acab49163be4f32aa71a9c816403a774e0bf75d0e384360aa671f0ddf68eb03bca6a340c35876451bda23f035e8a95c2e9f1e33bfc37618b2ea0911ec2f1c
languageName: node
linkType: hard
"@polkadot/api@npm:^1.9.0-beta.20":
version: 1.9.0-beta.20
resolution: "@polkadot/api@npm:1.9.0-beta.20"
"@polkadot/api@npm:^1.9.0-beta.21":
version: 1.9.0-beta.21
resolution: "@polkadot/api@npm:1.9.0-beta.21"
dependencies:
"@babel/runtime": ^7.9.2
"@polkadot/api-derive": 1.9.0-beta.20
"@polkadot/api-derive": 1.9.0-beta.21
"@polkadot/keyring": ^2.7.0-beta.8
"@polkadot/metadata": 1.9.0-beta.20
"@polkadot/rpc-core": 1.9.0-beta.20
"@polkadot/rpc-provider": 1.9.0-beta.20
"@polkadot/types": 1.9.0-beta.20
"@polkadot/metadata": 1.9.0-beta.21
"@polkadot/rpc-core": 1.9.0-beta.21
"@polkadot/rpc-provider": 1.9.0-beta.21
"@polkadot/types": 1.9.0-beta.21
"@polkadot/util": ^2.7.0-beta.8
"@polkadot/util-crypto": ^2.7.0-beta.8
bn.js: ^5.1.1
eventemitter3: ^4.0.0
rxjs: ^6.5.4
checksum: 2/c296e04caac00dd78fb9380010b2c38ed4afc75979f191e2a23975785e1f26538a487cbc4f9a1140e5036d869f7adddc125210e804c8051ef145ccc298544e65
checksum: 2/3ca5a70cc3f032c015481bc188181feb98139cfa60cc3bf6270ebb1bdf67f74743ca516e7968eea3837895834af2b586c0256e08d476a43aff0dece229c739ad
languageName: node
linkType: hard
@ -2926,7 +2926,7 @@ __metadata:
resolution: "@polkadot/app-contracts@workspace:packages/page-contracts"
dependencies:
"@babel/runtime": ^7.9.2
"@polkadot/api-contract": ^1.9.0-beta.20
"@polkadot/api-contract": ^1.9.0-beta.21
languageName: unknown
linkType: soft
@ -3250,16 +3250,16 @@ __metadata:
languageName: node
linkType: hard
"@polkadot/metadata@npm:1.9.0-beta.20":
version: 1.9.0-beta.20
resolution: "@polkadot/metadata@npm:1.9.0-beta.20"
"@polkadot/metadata@npm:1.9.0-beta.21":
version: 1.9.0-beta.21
resolution: "@polkadot/metadata@npm:1.9.0-beta.21"
dependencies:
"@babel/runtime": ^7.9.2
"@polkadot/types": 1.9.0-beta.20
"@polkadot/types": 1.9.0-beta.21
"@polkadot/util": ^2.7.0-beta.8
"@polkadot/util-crypto": ^2.7.0-beta.8
bn.js: ^5.1.1
checksum: 2/f0ea1a96ad69ec035ce568fd613bf7ce3d75eda4f2eba066072ed3f50d09c9c597f23f6dd1cddf670820c6cc05cfd03e2f7f3820632b14fc828c3559c2945e08
checksum: 2/e7fff7f2fcddd713f59a3e0fb47ef81a65677cc2ed28800aae0ca5ae8c571f086d5745f92c76a3c34da75c302ec00779f9608c569a386fc1dc4462b36308c48e
languageName: node
linkType: hard
@ -3268,7 +3268,7 @@ __metadata:
resolution: "@polkadot/react-api@workspace:packages/react-api"
dependencies:
"@babel/runtime": ^7.9.2
"@polkadot/api": ^1.9.0-beta.20
"@polkadot/api": ^1.9.0-beta.21
"@polkadot/extension-dapp": ^0.24.0-beta.0
rxjs-compat: ^6.5.4
languageName: unknown
@ -3385,35 +3385,35 @@ __metadata:
languageName: unknown
linkType: soft
"@polkadot/rpc-core@npm:1.9.0-beta.20":
version: 1.9.0-beta.20
resolution: "@polkadot/rpc-core@npm:1.9.0-beta.20"
"@polkadot/rpc-core@npm:1.9.0-beta.21":
version: 1.9.0-beta.21
resolution: "@polkadot/rpc-core@npm:1.9.0-beta.21"
dependencies:
"@babel/runtime": ^7.9.2
"@polkadot/metadata": 1.9.0-beta.20
"@polkadot/rpc-provider": 1.9.0-beta.20
"@polkadot/types": 1.9.0-beta.20
"@polkadot/metadata": 1.9.0-beta.21
"@polkadot/rpc-provider": 1.9.0-beta.21
"@polkadot/types": 1.9.0-beta.21
"@polkadot/util": ^2.7.0-beta.8
memoizee: ^0.4.14
rxjs: ^6.5.4
checksum: 2/2285ac6ff3d610fa840fae2bf4cd66fad1833af037ea61d4c2b85fbdd03752296ee29959b6b09fd22af4d7baca3c8a92aeeb0108a8c1f4ea3222d8ebdab258c9
checksum: 2/6b4821fe8130d4df29b7a066f59d56264c00721aafce2990383fcd42c18d8f9568b3f8a47f25f54ff94d7c32ffaea2c7ee7aa21ca7a29a9c115cd922bc0d3559
languageName: node
linkType: hard
"@polkadot/rpc-provider@npm:1.9.0-beta.20":
version: 1.9.0-beta.20
resolution: "@polkadot/rpc-provider@npm:1.9.0-beta.20"
"@polkadot/rpc-provider@npm:1.9.0-beta.21":
version: 1.9.0-beta.21
resolution: "@polkadot/rpc-provider@npm:1.9.0-beta.21"
dependencies:
"@babel/runtime": ^7.9.2
"@polkadot/metadata": 1.9.0-beta.20
"@polkadot/types": 1.9.0-beta.20
"@polkadot/metadata": 1.9.0-beta.21
"@polkadot/types": 1.9.0-beta.21
"@polkadot/util": ^2.7.0-beta.8
"@polkadot/util-crypto": ^2.7.0-beta.8
bn.js: ^5.1.1
eventemitter3: ^4.0.0
isomorphic-fetch: ^2.2.1
websocket: ^1.0.31
checksum: 2/525ae861b48ebfbaf2bff82f37505b01504e14541519dc2373cb200aaa7c52a8036ea039c9cf525fb9b95bbaf483a0d62bed1cfae57be3138bbdfa36459b5b35
checksum: 2/89c100a1682ffa97c719289f26d410e9ea0cc14073ca3f463e6a82b81c210da558e533ef3b4a827f4409535f99e42744895c3f46f0c943ac341d03c8af863b54
languageName: node
linkType: hard
@ -3426,19 +3426,19 @@ __metadata:
languageName: node
linkType: hard
"@polkadot/types@npm:^1.9.0-beta.20":
version: 1.9.0-beta.20
resolution: "@polkadot/types@npm:1.9.0-beta.20"
"@polkadot/types@npm:^1.9.0-beta.21":
version: 1.9.0-beta.21
resolution: "@polkadot/types@npm:1.9.0-beta.21"
dependencies:
"@babel/runtime": ^7.9.2
"@polkadot/metadata": 1.9.0-beta.20
"@polkadot/metadata": 1.9.0-beta.21
"@polkadot/util": ^2.7.0-beta.8
"@polkadot/util-crypto": ^2.7.0-beta.8
"@types/bn.js": ^4.11.6
bn.js: ^5.1.1
memoizee: ^0.4.14
rxjs: ^6.5.4
checksum: 2/096de658234ee0e41171278a27c45f1c50ce45c38ab71bc7025b60878c8e077035bd77c95a1a399766ba366a50dc98c3cd824c5dd258b5e010a53bee6f0f3830
checksum: 2/2d57076bdf3dfad361746b781ab8c202799090c851b52fe26ca563cf613d8a221229d06b2e127733bcd057e3ca7a78c603ef75189f57a08d51600d55aed99bda
languageName: node
linkType: hard