Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export function getValueOfUnit(_unit: string): BigNumberType {
let unit = _unit ? _unit.toLowerCase() : 'ether'
let unitValue = unitMap[unit]
if (unitValue === undefined) {
throw new Error(
"This unit doesn't exists, please use the one of the following units" + JSON.stringify(unitMap, null, 2)
)
}
return new BigNumber(unitValue, 10)
}
export function toBigNumber(_num: number | string | BigNumberType): BigNumberType {
let num: any = _num || 0
if (isBigNumber(num)) {
return num as BigNumber
}
if (typeof num === 'string' && (num.indexOf('0x') === 0 || num.indexOf('-0x') === 0)) {
return new BigNumber(num.replace('0x', ''), 16)
}
return new BigNumber(num.toString(10), 10)
}
private getTokenAmount(tokens: string): string {
const tokenNumber = new BigNumber(tokens);
if (this.decimalPartIsZero(tokenNumber)) {
return tokenNumber.toString();
}
if (tokenNumber.decimalPlaces() > 4) {
return tokenNumber.toFixed(this._decimals);
} else {
return tokens;
}
}
private resetState() {
this.coins = new BigNumber('0');
this.price = null;
this.balance = null;
this.balanceObtained = false;
this.isBlockchainLoading = false;
this.percentage = null;
this.current = null;
this.highest = null;
}
function ensureNoNegative(operations) {
let total = BigNumber(0);
for (let i = operations.length - 1; i >= 0; i--) {
const op = operations[i];
const amount = getOperationAmountNumber(op);
if (total.plus(amount).isNegative()) {
if (op.type === "IN") {
op.type = "OUT";
} else if (op.type === "OUT") {
op.type = "IN";
}
}
total = total.plus(getOperationAmountNumber(op));
}
return total;
}
getAmountBuffer (amount) {
let hexAmount = BigNumber(amount).toString(16)
hexAmount = padHexStart(hexAmount, 16)
const valueBuffer = Buffer.from(hexAmount, 'hex')
return valueBuffer.reverse()
}
handleChangeAmount = (changedField: string) => (val: BigNumber) => {
const { getReverseCounterValue, max, onChange } = this.props
if (changedField === 'left') {
onChange(val.gt(max) ? max : val)
} else if (changedField === 'right') {
const leftVal = getReverseCounterValue(val) || BigNumber(0)
onChange(leftVal.gt(max) ? max : leftVal)
}
}
getBlockDeltaFromNow(time) {
const { block } = this.props;
const now = block.timestamp.unix();
const delta = Math.abs(time.subtract(now, "second").unix());
const deltaInBlocks = BN(delta).dividedBy(MAINNET_BLOCK_TIME_AVERAGE);
return BN(block.number.plus(deltaInBlocks).toFixed(0));
}