@polkadot/types/interfaces#VoteThreshold TypeScript Examples

The following examples show how to use @polkadot/types/interfaces#VoteThreshold. 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: util.ts    From crust-apps with Apache License 2.0 7 votes vote down vote up
// loop changes over aye, using the diffs above, returning when an outcome change is made
function calcChangeAye (threshold: VoteThreshold, sqrtElectorate: BN, { votedAye, votedNay, votedTotal }: ApproxState, isPassing: boolean, changeAye: BN, inc: BN): BN {
  while (true) {
    // if this one is passing, we only adjust the convictions (since it goes down), if it is failing
    // we assume new votes needs to be added, do those at 1x conviction
    const [newChangeAye, newAye, newTotal] = getDiffs(votedAye, votedTotal, changeAye, inc, isPassing ? 0 : 1, isPassing ? -1 : 1);
    const newResult = calcPassing(threshold, sqrtElectorate, { votedAye: newAye, votedNay, votedTotal: newTotal });

    if (newResult !== isPassing) {
      return changeAye;
    }

    changeAye = newChangeAye;
  }
}
Example #2
Source File: util.ts    From crust-apps with Apache License 2.0 7 votes vote down vote up
// loop changes over nay, using the diffs above, returning when an outcome change is made
function calcChangeNay (threshold: VoteThreshold, sqrtElectorate: BN, { votedAye, votedNay, votedTotal }: ApproxState, isPassing: boolean, changeNay: BN, inc: BN): BN {
  while (true) {
    // if this one is passing, we only adjust the convictions (since it goes down), if it is failing
    // we assume new votes needs to be added, do those at 1x conviction
    // NOTE: We use isPassing here, so it is reversed from what we find in the aye calc
    const [newChangeNay, newNay, newTotal] = getDiffs(votedNay, votedTotal, changeNay, inc, isPassing ? 1 : 0, isPassing ? 1 : -1);
    const newResult = calcPassing(threshold, sqrtElectorate, { votedAye, votedNay: newNay, votedTotal: newTotal });

    if (newResult !== isPassing) {
      return changeNay;
    }

    changeNay = newChangeNay;
  }
}
Example #3
Source File: useChangeCalc.ts    From crust-apps with Apache License 2.0 6 votes vote down vote up
export default function useChangeCalc (threshold: VoteThreshold, votedAye: BN, votedNay: BN, votedTotal: BN): Result {
  const { api } = useApi();
  const sqrtElectorate = useCall<BN>(api.derive.democracy.sqrtElectorate);
  const [result, setResult] = useState<Result>({ changeAye: BN_ZERO, changeNay: BN_ZERO });

  useEffect((): void => {
    sqrtElectorate && setResult(
      approxChanges(threshold, sqrtElectorate, { votedAye, votedNay, votedTotal })
    );
  }, [sqrtElectorate, threshold, votedAye, votedNay, votedTotal]);

  return result;
}
Example #4
Source File: util.ts    From crust-apps with Apache License 2.0 6 votes vote down vote up
// The magic happens here
export function approxChanges (threshold: VoteThreshold, sqrtElectorate: BN, state: ApproxState): Approx {
  const isPassing = calcPassing(threshold, sqrtElectorate, state);

  // simple case, we have an aye > nay to determine passing
  if (threshold.isSimplemajority) {
    const change = isPassing
      ? state.votedAye.sub(state.votedNay)
      : state.votedNay.sub(state.votedAye);

    return {
      changeAye: state.votedNay.isZero()
        ? BN_ZERO
        : change,
      changeNay: state.votedAye.isZero()
        ? BN_ZERO
        : change
    };
  }

  let changeAye = BN_ZERO;
  let changeNay = BN_ZERO;
  let inc = state.votedTotal.div(DIVISOR);

  // - starting from a large increment (total/2) see if that changes the outcome
  // - keep dividing by 2, each time adding just enough to _not_ make the state change
  // - continue the process, until we have the smallest increment
  // - on the last iteration, we add the increment, since we push over the line
  while (!inc.isZero()) {
    // calc the applied changes based on current increment
    changeAye = calcChangeAye(threshold, sqrtElectorate, state, isPassing, changeAye, inc);
    changeNay = calcChangeNay(threshold, sqrtElectorate, state, isPassing, changeNay, inc);

    // move down one level
    const nextInc = inc.div(DIVISOR);

    // on the final round (no more inc reductions), add the last increment to push it over the line
    if (nextInc.isZero()) {
      changeAye = changeAye.add(inc);
      changeNay = changeNay.add(inc);
    }

    inc = nextInc;
  }

  // - When the other vote is zero, it is not useful to show the decrease, since it ends up at all
  // - Always ensure that we don't go above max available (generally should be covered by above)
  return {
    changeAye: state.votedNay.isZero()
      ? BN_ZERO
      : isPassing
        ? BN.min(changeAye, state.votedAye)
        : changeAye,
    changeNay: state.votedAye.isZero()
      ? BN_ZERO
      : isPassing
        ? changeNay
        : BN.min(changeNay, state.votedNay)
  };
}
Example #5
Source File: referendumApproxChanges.ts    From sdk with Apache License 2.0 6 votes vote down vote up
// loop changes over aye, using the diffs above, returning when an outcome change is made
function calcChangeAye(
  threshold: VoteThreshold,
  sqrtElectorate: BN,
  { votedAye, votedNay, votedTotal }: ApproxState,
  isPassing: boolean,
  changeAye: BN,
  inc: BN
): BN {
  while (true) {
    // if this one is passing, we only adjust the convictions (since it goes down), if it is failing
    // we assume new votes needs to be added, do those at 1x conviction
    const [newChangeAye, newAye, newTotal] = getDiffs(
      votedAye,
      votedTotal,
      changeAye,
      inc,
      isPassing ? 0 : 1,
      isPassing ? -1 : 1
    );
    const newResult = calcPassing(threshold, sqrtElectorate, {
      votedAye: newAye,
      votedNay,
      votedTotal: newTotal,
    });

    if (newResult !== isPassing) {
      return changeAye;
    }

    changeAye = newChangeAye;
  }
}
Example #6
Source File: referendumApproxChanges.ts    From sdk with Apache License 2.0 6 votes vote down vote up
// loop changes over nay, using the diffs above, returning when an outcome change is made
function calcChangeNay(
  threshold: VoteThreshold,
  sqrtElectorate: BN,
  { votedAye, votedNay, votedTotal }: ApproxState,
  isPassing: boolean,
  changeNay: BN,
  inc: BN
): BN {
  while (true) {
    // if this one is passing, we only adjust the convictions (since it goes down), if it is failing
    // we assume new votes needs to be added, do those at 1x conviction
    // NOTE: We use isPassing here, so it is reversed from what we find in the aye calc
    const [newChangeNay, newNay, newTotal] = getDiffs(
      votedNay,
      votedTotal,
      changeNay,
      inc,
      isPassing ? 1 : 0,
      isPassing ? 1 : -1
    );
    const newResult = calcPassing(threshold, sqrtElectorate, {
      votedAye,
      votedNay: newNay,
      votedTotal: newTotal,
    });

    if (newResult !== isPassing) {
      return changeNay;
    }

    changeNay = newChangeNay;
  }
}
Example #7
Source File: democracy_proposals.ts    From commonwealth with GNU General Public License v3.0 5 votes vote down vote up
private _nextExternal: [ Hash, VoteThreshold ] = null;
Example #8
Source File: referendumApproxChanges.ts    From sdk with Apache License 2.0 5 votes vote down vote up
// The magic happens here
export function approxChanges(
  threshold: VoteThreshold,
  sqrtElectorate: BN,
  state: ApproxState
): Approx {
  const isPassing = calcPassing(threshold, sqrtElectorate, state);

  // simple case, we have an aye > nay to determine passing
  if (threshold.isSimplemajority) {
    const change = isPassing
      ? state.votedAye.sub(state.votedNay)
      : state.votedNay.sub(state.votedAye);

    return {
      changeAye: state.votedNay.isZero() ? BN_ZERO : change,
      changeNay: state.votedAye.isZero() ? BN_ZERO : change,
    };
  }

  let changeAye = BN_ZERO;
  let changeNay = BN_ZERO;
  let inc = state.votedTotal.div(DIVISOR);

  // - starting from a large increment (total/2) see if that changes the outcome
  // - keep dividing by 2, each time adding just enough to _not_ make the state change
  // - continue the process, until we have the smallest increment
  // - on the last iteration, we add the increment, since we push over the line
  while (!inc.isZero()) {
    // calc the applied changes based on current increment
    changeAye = calcChangeAye(
      threshold,
      sqrtElectorate,
      state,
      isPassing,
      changeAye,
      inc
    );
    changeNay = calcChangeNay(
      threshold,
      sqrtElectorate,
      state,
      isPassing,
      changeNay,
      inc
    );

    // move down one level
    const nextInc = inc.div(DIVISOR);

    // on the final round (no more inc reductions), add the last increment to push it over the line
    if (nextInc.isZero()) {
      changeAye = changeAye.add(inc);
      changeNay = changeNay.add(inc);
    }

    inc = nextInc;
  }

  // - When the other vote is zero, it is not useful to show the decrease, since it ends up at all
  // - Always ensure that we don't go above max available (generally should be covered by above)
  return {
    changeAye: state.votedNay.isZero()
      ? BN_ZERO
      : isPassing
      ? BN.min(changeAye, state.votedAye)
      : changeAye,
    changeNay: state.votedAye.isZero()
      ? BN_ZERO
      : isPassing
      ? changeNay
      : BN.min(changeNay, state.votedNay),
  };
}
Example #9
Source File: VoteThreshold.tsx    From subscan-multisig-react with Apache License 2.0 5 votes vote down vote up
function VoteThresholdParam({
  className = '',
  defaultValue: { value },
  isDisabled,
  isError,
  label,
  onChange,
  registry,
  withLabel,
}: Props): React.ReactElement<Props> {
  const _onChange = useCallback(
    (val: number) =>
      onChange &&
      onChange({
        isValid: true,
        value: val,
      }),
    [onChange]
  );

  const defaultValue =
    value instanceof registry.createClass<VoteThreshold>('VoteThreshold')
      ? value.toNumber()
      : bnToBn(value as number).toNumber();

  return (
    <Bare className={className}>
      <Dropdown
        className="full"
        defaultValue={defaultValue}
        isDisabled={isDisabled}
        isError={isError}
        label={label}
        onChange={_onChange}
        options={options}
        withLabel={withLabel}
      />
    </Bare>
  );
}