@polkadot/types/interfaces#Votes TypeScript Examples

The following examples show how to use @polkadot/types/interfaces#Votes. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: useVotingStatus.ts    From crust-apps with Apache License 2.0 6 votes vote down vote up
function getStatus (api: ApiPromise, bestNumber: BlockNumber, votes: Votes, numMembers: number, section: 'council' | 'technicalCommittee'): State {
  if (!votes.end) {
    return {
      hasFailed: false,
      hasPassed: false,
      isCloseable: false,
      isVoteable: true,
      remainingBlocks: null
    };
  }

  const isEnd = bestNumber.gte(votes.end);
  const hasPassed = votes.threshold.lten(votes.ayes.length);
  const hasFailed = votes.threshold.gtn(Math.abs(numMembers - votes.nays.length));

  return {
    hasFailed,
    hasPassed,
    isCloseable: isFunction(api.tx[section].close)
      ? api.tx[section].close.meta.args.length === 4 // current-generation
        ? isEnd || hasPassed || hasFailed
        : isEnd
      : false,
    isVoteable: !isEnd,
    remainingBlocks: isEnd
      ? null
      : votes.end.sub(bestNumber)
  };
}
Example #2
Source File: useVotingStatus.ts    From crust-apps with Apache License 2.0 6 votes vote down vote up
export function useVotingStatus (votes: Votes | null | undefined, numMembers: number, section: 'council' | 'technicalCommittee'): State {
  const { api } = useApi();
  const bestNumber = useBestNumber();

  return useMemo(
    () => bestNumber && votes
      ? getStatus(api, bestNumber, votes, numMembers, section)
      : DEFAULT_STATUS,
    [api, bestNumber, numMembers, section, votes]
  );
}
Example #3
Source File: collective_proposal.ts    From commonwealth with GNU General Public License v3.0 6 votes vote down vote up
public updateVoters = async () => {
    const v = await this._Chain.api.query[this.collectiveName].voting<Option<Votes>>(this.data.hash);
    if (v.isSome) {
      const votes = v.unwrap();
      this.clearVotes();
      votes.ayes.map(
        (who) => this.addOrUpdateVote(
          new SubstrateCollectiveVote(this, this._Accounts.fromAddress(who.toString()), true)
        )
      );
      votes.nays.map(
        (who) => this.addOrUpdateVote(
          new SubstrateCollectiveVote(this, this._Accounts.fromAddress(who.toString()), false)
        )
      );
    }
  }
Example #4
Source File: useVotingStatus.ts    From subscan-multisig-react with Apache License 2.0 6 votes vote down vote up
// eslint-disable-next-line complexity
function getStatus(
  api: ApiPromise,
  bestNumber: BlockNumber,
  votes: Votes,
  numMembers: number,
  section: 'council' | 'membership' | 'technicalCommittee'
): State {
  if (!votes.end) {
    return {
      hasFailed: false,
      hasPassed: false,
      isCloseable: false,
      isVoteable: true,
      remainingBlocks: null,
    };
  }

  const isEnd = bestNumber.gte(votes.end);
  const hasPassed = votes.threshold.lten(votes.ayes.length);
  const hasFailed = votes.threshold.gtn(Math.abs(numMembers - votes.nays.length));

  return {
    hasFailed,
    hasPassed,
    isCloseable: isFunction(api.tx[section].close)
      ? api.tx[section].close.meta.args.length === 4 // current-generation
        ? isEnd || hasPassed || hasFailed
        : isEnd
      : false,
    isVoteable: !isEnd,
    remainingBlocks: isEnd ? null : votes.end.sub(bestNumber),
  };
}
Example #5
Source File: useVotingStatus.ts    From subscan-multisig-react with Apache License 2.0 6 votes vote down vote up
export function useVotingStatus(
  votes: Votes | null | undefined,
  numMembers: number,
  section: 'council' | 'membership' | 'technicalCommittee'
): State {
  const { api } = useApi();
  const bestNumber = useBestNumber();

  return useMemo(
    () => (bestNumber && votes ? getStatus(api, bestNumber, votes, numMembers, section) : DEFAULT_STATUS),
    [api, bestNumber, numMembers, section, votes]
  );
}
Example #6
Source File: Proposal.tsx    From crust-apps with Apache License 2.0 5 votes vote down vote up
transformVotes = {
  transform: (optVotes: Option<Votes>) => optVotes.unwrapOr(null)
}
Example #7
Source File: Proposal.tsx    From crust-apps with Apache License 2.0 4 votes vote down vote up
function Proposal ({ className = '', imageHash, members, prime }: Props): React.ReactElement<Props> | null {
  const { t } = useTranslation();
  const { api } = useApi();
  const { allAccounts } = useAccounts();
  const proposal = useCall<ProposalType | null>(api.query.technicalCommittee.proposalOf, [imageHash], transformProposal);
  const votes = useCall<Votes | null>(api.query.technicalCommittee.voting, [imageHash], transformVotes);
  const { hasFailed, isCloseable, isVoteable, remainingBlocks } = useVotingStatus(votes, members.length, 'technicalCommittee');
  const [proposalWeight, proposalLength] = useWeight(proposal);

  const [councilId, isMultiMembers] = useMemo(
    (): [string | null, boolean] => {
      const councilIds = allAccounts.filter((accountId) => members.includes(accountId));

      return [councilIds[0] || null, councilIds.length > 1];
    },
    [allAccounts, members]
  );

  if (!proposal || !votes) {
    return null;
  }

  const { ayes, end, index, nays, threshold } = votes;

  return (
    <tr className={className}>
      <td className='number'><h1>{formatNumber(index)}</h1></td>
      <ProposalCell
        imageHash={imageHash}
        proposal={proposal}
      />
      <td className='number'>
        {formatNumber(ayes.length)}/{formatNumber(threshold)}
      </td>
      <td className='number together'>
        {remainingBlocks && end && (
          <>
            <BlockToTime value={remainingBlocks} />
            #{formatNumber(end)}
          </>
        )}
      </td>
      <td className='address'>
        {ayes.map((address, index): React.ReactNode => (
          <AddressMini
            key={`${index}:${address.toHex()}`}
            value={address}
            withBalance={false}
          />
        ))}
      </td>
      <td className='address'>
        {nays.map((address, index): React.ReactNode => (
          <AddressMini
            key={`${index}:${address.toHex()}`}
            value={address}
            withBalance={false}
          />
        ))}
      </td>
      <td className='button'>
        {isVoteable && !isCloseable && (
          <Voting
            hash={imageHash}
            members={members}
            prime={prime}
            proposalId={index}
          />
        )}
        {isCloseable && (
          isMultiMembers
            ? (
              <Close
                hasFailed={hasFailed}
                hash={imageHash}
                idNumber={index}
                members={members}
                proposal={proposal}
              />
            )
            : (
              <TxButton
                accountId={councilId}
                icon='times'
                label={t<string>('Close')}
                params={
                  api.tx.technicalCommittee.close?.meta.args.length === 4
                    ? hasFailed
                      ? [imageHash, index, 0, 0]
                      : [imageHash, index, proposalWeight, proposalLength]
                    : [imageHash, index]
                }
                tx={api.tx.technicalCommittee.closeOperational || api.tx.technicalCommittee.close}
              />
            )
        )}
      </td>
    </tr>
  );
}