@polkadot/util#BN_ZERO TypeScript Examples

The following examples show how to use @polkadot/util#BN_ZERO. 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
/**
 * This is where we tweak the input values, based on what was specified, be it the input number
 * or the direction and turnout adjustments
 *
 * @param votes The votes that should be adjusted, will be either aye/nay
 * @param total The actual total of applied votes (same as turnout from derived)
 * @param change The actual change value we want to affect
 * @param inc The increment to apply here
 * @param totalInc The increment for the total. 0 for conviction-only changes, 1 of 1x added conviction vote
 * @param direction The direction, either increment (1) or decrement (-1)
 */
function getDiffs (votes: BN, total: BN, change: BN, inc: BN, totalInc: 0 | 0.1 | 1, direction: 1 | -1): [BN, BN, BN] {
  // setup
  const multiplier = direction === 1 ? BN_ONE : ONEMIN;
  const voteChange = change.add(inc);

  // since we allow 0.1 as well, we first multiply by 10, before dividing by the same
  const totalChange = BN_ONE.muln(totalInc * 10).mul(voteChange).div(BN_TEN);

  // return the change, vote with change applied and the total with the same. For the total we don't want
  // to go negative (total votes/turnout), since will do sqrt on it (and negative is non-sensical anyway)
  return [
    voteChange,
    votes.add(multiplier.mul(voteChange)),
    BN.max(BN_ZERO, total.add(multiplier.mul(totalChange)))
  ];
}
Example #2
Source File: InputNumber.tsx    From contracts-ui with GNU General Public License v3.0 6 votes vote down vote up
export function InputNumber({
  children,
  isDisabled,
  onChange: _onChange,
  value = BN_ZERO,
  ...props
}: Props) {
  const onChange = useCallback(
    (value: string) => {
      _onChange(new BN(value));
    },
    [_onChange]
  );

  return (
    <Input
      isDisabled={isDisabled}
      onChange={onChange}
      onFocus={e => e.target.select()}
      type="number"
      value={value ? value.toString() : ''}
      {...props}
    >
      {children}
    </Input>
  );
}
Example #3
Source File: gov.ts    From sdk with Apache License 2.0 6 votes vote down vote up
/**
 * Query overview of treasury and spend proposals.
 */
async function getTreasuryOverview(api: ApiPromise) {
  const [bounties, proposals, balance] = await Promise.all([
    api.derive.bounties?.bounties(),
    api.derive.treasury.proposals(),
    api.derive.balances.account(TREASURY_ACCOUNT as AccountId),
  ]);
  const pendingBounties = bounties.reduce(
    (total, { bounty: { status, value } }) => total.iadd(status.isApproved ? value : BN_ZERO),
    new BN(0)
  );
  const pendingProposals = proposals.approvals.reduce((total, { proposal: { value } }) => total.iadd(value), new BN(0));
  const burn =
    balance.freeBalance.gt(BN_ZERO) && !(api.consts.treasury.burn as any).isZero()
      ? (api.consts.treasury.burn as any).mul(balance.freeBalance).div(BN_MILLION)
      : BN_ZERO;
  const res: any = {
    ...proposals,
  };
  res["balance"] = balance.freeBalance.toString();
  res["burn"] = burn.toString();
  res["approved"] = pendingProposals.toString();
  res["spendable"] = balance.freeBalance
    .sub(pendingBounties)
    .sub(pendingProposals)
    .toString();
  res.proposals.forEach((e: any) => {
    if (e.council.length) {
      e.council = e.council.map((i: any) => ({
        ...i,
        proposal: _transfromProposalMeta(i.proposal),
      }));
    }
  });
  return res;
}
Example #4
Source File: AddressInfo.tsx    From subscan-multisig-react with Apache License 2.0 6 votes vote down vote up
// calculates the bonded, first being the own, the second being nominated
function calcBonded(stakingInfo?: DeriveStakingAccount, bonded?: boolean | BN[]): [BN, BN[]] {
  let other: BN[] = [];
  let own = BN_ZERO;

  if (Array.isArray(bonded)) {
    other = bonded.filter((_, index): boolean => index !== 0).filter((value): boolean => value.gtn(0));

    own = bonded[0];
  } else if (
    stakingInfo &&
    stakingInfo.stakingLedger &&
    stakingInfo.stakingLedger.active &&
    stakingInfo.accountId.eq(stakingInfo.stashId)
  ) {
    own = stakingInfo.stakingLedger.active.unwrap();
  }

  return [own, other];
}
Example #5
Source File: ProxyOverview.tsx    From crust-apps with Apache License 2.0 5 votes vote down vote up
EMPTY_EXISTING: [ProxyDefinition[], BN] = [[], BN_ZERO]
Example #6
Source File: InputBalance.tsx    From contracts-ui with GNU General Public License v3.0 5 votes vote down vote up
function getStringValue(api: ApiPromise, value: OrFalsy<BN>) {
  if (!value) {
    return '';
  }

  return fromBalance(fromSats(api, value || BN_ZERO));
}