Skip to content

Commit

Permalink
Fix election candidate derive (#3239)
Browse files Browse the repository at this point in the history
  • Loading branch information
jacogr committed Feb 28, 2021
1 parent 6d3491b commit 73045de
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
21 changes: 21 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
# CHANGELOG

## 3.11.1 Feb 28, 2021

Upgrade priority: Low. Recommended for users wanting to keep updated with chain changes.

Contributed:

- Clean Websocket states on disconnects (Thanks to https://github.com/ianhe8x)
- Support for C-like indexed enums (Thanks to https://github.com/xlc)

Changes:

- Adjust council derives to cater for current-generation candidate mapping
- Allow for override of codec hasher (& output type)
- Adjust submittables to submit hex-encoded addresses to queries
- Adjust initialization to always retrieve tx version from Metadata only
- Update election types as per latest Substrate
- Add types for the new gilt module
- Upgrade to the latest Substrate metadata
- Adjust package detection to check for local monorepo dependencies


## 3.10.2 Feb 23, 2021

Upgrade priority: Medium. Recommended for users of chains where `MultiAddress` is in-use and blocks are decoded.
Expand Down
16 changes: 14 additions & 2 deletions packages/api-derive/src/elections/info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,36 @@ import { memo } from '../util';
// SeatHolder is current tuple is 2.x-era Substrate
type Member = SeatHolder | ITuple<[AccountId, Balance]>;

type Candidate = AccountId | ITuple<[AccountId, Balance]>;

function isSeatHolder (value: Member): value is SeatHolder {
return !Array.isArray(value);
}

function isCandidateTuple (value: Candidate): value is ITuple<[AccountId, Balance]> {
return Array.isArray(value);
}

function getAccountTuple (value: Member): [AccountId, Balance] {
return isSeatHolder(value)
? [value.who, value.stake]
: value;
}

function getCandidate (value: Candidate): AccountId {
return isCandidateTuple(value)
? value[0]
: value;
}

function sortAccounts ([, balanceA]: [AccountId, Balance], [, balanceB]: [AccountId, Balance]): number {
return balanceB.cmp(balanceA);
}

function queryElections (api: ApiInterfaceRx): Observable<DeriveElectionsInfo> {
const section = api.query.electionsPhragmen ? 'electionsPhragmen' : 'elections';

return api.queryMulti<[Vec<AccountId>, Vec<AccountId>, Vec<Member>, Vec<Member>]>([
return api.queryMulti<[Vec<AccountId>, Vec<Candidate>, Vec<Member>, Vec<Member>]>([
api.query.council.members,
api.query[section].candidates,
api.query[section].members,
Expand All @@ -41,7 +53,7 @@ function queryElections (api: ApiInterfaceRx): Observable<DeriveElectionsInfo> {
map(([councilMembers, candidates, members, runnersUp]): DeriveElectionsInfo => ({
candidacyBond: api.consts[section].candidacyBond as Balance,
candidateCount: api.registry.createType('u32', candidates.length),
candidates,
candidates: candidates.map(getCandidate),
desiredRunnersUp: api.consts[section].desiredRunnersUp as u32,
desiredSeats: api.consts[section].desiredMembers as u32,
members: members.length
Expand Down

0 comments on commit 73045de

Please sign in to comment.