How to use number-precision - 10 common examples

To help you get started, we’ve selected a few number-precision examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github bodhiproject / bodhi-ui / src / scenes / Event / store.js View on Github external
disableEventActionsIfNecessary = () => {
    if (!this.event) return;
    const { status, centralizedOracle, isOpenResultSetting, consensusThreshold } = this.event;
    const { wallet } = this.app;
    const currentWalletNbot = wallet.currentWalletAddress ? wallet.currentWalletAddress.nbot : -1; // when no wallet, still can click on action buttons

    this.buttonDisabled = false;
    this.warningType = '';
    this.eventWarningMessageId = '';
    this.error = INIT.error;

    // Trying to vote over the consensus threshold - currently not way to trigger
    const amountNum = Number(this.amount);
    if (status === ARBITRATION && this.amount && this.selectedOptionIdx >= 0) {
      const maxVote = NP.minus(consensusThreshold, this.selectedOption.amount);
      if (amountNum > maxVote) {
        this.buttonDisabled = true;
        this.error.amount = 'oracle.maxVoteText';
        return;
      }
    }

    // Has not reached betting start time
    if (status === PRE_BETTING) {
      this.buttonDisabled = true;
      this.warningType = EventWarningType.INFO;
      this.eventWarningMessageId = 'oracle.betStartTimeDisabledText';
      return;
    }

    // Has not reached result setting start time
github bodhiproject / bodhi-ui / src / scenes / Event / store.js View on Github external
if (!this.event) return;
    const { status, centralizedOracle, resultSetStartTime, isOpenResultSetting, consensusThreshold } = this.event;
    const { global: { syncBlockTime }, wallet } = this.app;
    const currBlockTime = moment.unix(syncBlockTime);
    const currentWalletNbot = wallet.currentWalletAddress ? wallet.currentWalletAddress.nbot : 0;
    const notEnoughNbot = currentWalletNbot < maxTransactionFee;

    this.buttonDisabled = false;
    this.warningType = '';
    this.eventWarningMessageId = '';
    this.error = INIT.error;

    // Trying to vote over the consensus threshold - currently not way to trigger
    const amountNum = Number(this.amount);
    if (status === ARBITRATION && this.amount && this.selectedOptionIdx >= 0) {
      const maxVote = NP.minus(consensusThreshold, this.selectedOption.amount);
      if (amountNum > maxVote) {
        this.buttonDisabled = true;
        this.error.amount = 'oracle.maxVoteText';
        return;
      }
    }

    // Has not reached betting start time
    if (status === BETTING
      && currBlockTime.isBefore(moment.unix(this.event.betStartTime))) {
      this.buttonDisabled = true;
      this.warningType = EventWarningType.INFO;
      this.eventWarningMessageId = 'oracle.betStartTimeDisabledText';
      return;
    }
github bodhiproject / bodhi-ui / src / scenes / Event / components / EventOption / index.js View on Github external
handleAmountBlur = ({ target: { value } }) => {
    let { phase, amount, consensusThreshold, onAmountChange } = this.props; // eslint-disable-line
    if (phase === Phases.VOTING) {
      [amount, consensusThreshold] = [parseFloat(amount, 10), parseFloat(consensusThreshold, 10)];
      if (amount + Number(value) > consensusThreshold) {
        const val = toFixed(NP.minus(consensusThreshold, amount));
        onAmountChange(val);
      }
    }
  }
github bodhiproject / bodhi-ui / src / scenes / Event / scenes / oracle.js View on Github external
}

    // Did not enter an amount
    if ((isBettingPhase || isVotingPhase) && (voteAmount <= 0 || Number.isNaN(voteAmount))) {
      return {
        disabled: true,
        id: 'oracle.enterAmountDisabledText',
        message: 'Please entered a valid amount.',
        warningType: EventWarningType.INFO,
      };
    }

    // Trying to vote over the consensus threshold
    const optionAmount = oracle.amounts[currentOptionIdx];
    const maxVote = token === Token.BOT && status === OracleStatus.VOTING
      ? NP.minus(oracle.consensusThreshold, optionAmount) : 0;
    if (token === Token.BOT
      && status === OracleStatus.VOTING
      && currentOptionIdx >= 0
      && voteAmount > maxVote) {
      return {
        disabled: true,
        id: 'oracle.maxVoteText',
        message: 'You can only vote up to the Consensus Threshold for any one outcome. Current max vote is {amount} BOT.',
        values: { amount: toFixed(maxVote) },
        warningTypeClass: EventWarningType.ERROR,
      };
    }

    return {
      disabled: false,
    };
github bodhiproject / bodhi-ui / src / helpers / utility.js View on Github external
let bn;
  if (isNaN(Number(number))) {
    bn = toBN(number, 16);
  } else {
    const toStringNumber = String(number);
    const splitArr = toStringNumber.split('.');
    const integerPart = splitArr[0];
    bn = toBN(integerPart);
  }

  const conversionBN = toBN(SATOSHI_CONVERSION);
  const integerPart = bn.div(conversionBN).toNumber();
  const decimalPartBN = bn.sub(conversionBN.mul(toBN(integerPart)));
  const decimalPart = NP.divide(decimalPartBN.toNumber(), SATOSHI_CONVERSION);
  return NP.plus(integerPart, decimalPart);
}
github bodhiproject / bodhi-ui / src / helpers / utility.js View on Github external
}

  let bn;
  if (isNaN(Number(number))) {
    bn = toBN(number, 16);
  } else {
    const toStringNumber = String(number);
    const splitArr = toStringNumber.split('.');
    const integerPart = splitArr[0];
    bn = toBN(integerPart);
  }

  const conversionBN = toBN(SATOSHI_CONVERSION);
  const integerPart = bn.div(conversionBN).toNumber();
  const decimalPartBN = bn.sub(conversionBN.mul(toBN(integerPart)));
  const decimalPart = NP.divide(decimalPartBN.toNumber(), SATOSHI_CONVERSION);
  return NP.plus(integerPart, decimalPart);
}
github bodhiproject / bodhi-ui / src / helpers / utility.js View on Github external
export function decimalToSatoshi(number) {
  if (!number) {
    return number;
  }
  const toStringNumber = Number(number).toFixed(8);
  const splitArr = toStringNumber.split('.');
  const integerPart = splitArr[0];
  const decimalPart = splitArr.length > 1 ? NP.times(Number(`.${splitArr[1]}`), SATOSHI_CONVERSION) : 0;
  const conversionBN = toBN(SATOSHI_CONVERSION);
  return toBN(integerPart).mul(conversionBN).add(toBN(decimalPart)).toString(10);
}
github bodhiproject / bodhi-ui / src / scenes / Event / store.js View on Github external
fixAmount = () => {
    if (this.event.status !== ARBITRATION) return;

    const inputAmount = parseFloat(this.amount, 10);
    const consensusThreshold = parseFloat(this.event.consensusThreshold, 10);
    if (inputAmount + Number(this.selectedOption.amount) > consensusThreshold) {
      this.amount = String(toFixed(NP.minus(
        consensusThreshold,
        Number(this.selectedOption.amount)
      )));
    }
  }
github bodhiproject / bodhi-ui / src / scenes / Event / store.js View on Github external
@computed get remainingConsensusThreshold() {
    const consensusThreshold = parseFloat(this.event.consensusThreshold, 10);
    return toFixed(NP.minus(
      consensusThreshold,
      Number(this.selectedOption.amount)
    ), true);
  }
github bodhiproject / bodhi-ui / src / scenes / Event / store.js View on Github external
fixAmount = () => {
    if (this.event.status !== ARBITRATION) return;

    const inputAmount = parseFloat(this.amount, 10);
    const consensusThreshold = parseFloat(this.event.consensusThreshold, 10);
    if (inputAmount + Number(this.selectedOption.amount) > consensusThreshold) {
      this.amount = String(NP.minus(
        consensusThreshold,
        Number(this.selectedOption.amount)
      ).toFixed(8));
    }
  }

number-precision

Perform addition, subtraction, multiplication and division operations precisely using javascript

MIT
Latest version published 2 years ago

Package Health Score

58 / 100
Full package analysis