Skip to content

Commit

Permalink
Merge pull request #11 from ava-labs/dev
Browse files Browse the repository at this point in the history
v0.9.7
  • Loading branch information
kanatliemre authored Sep 23, 2021
2 parents c82e550 + 7bededb commit cc583a2
Show file tree
Hide file tree
Showing 9 changed files with 254 additions and 174 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# CHANGELOG

## v0.9.7

#### Added

- `Utils.stringToBN` convert string to BN directly

#### Changed

- Max gas price increased to 1000 nAVAX

## v0.9.5

#### Added
Expand Down
2 changes: 1 addition & 1 deletion src/History/history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { activeNetwork, explorer_api, xChain } from '@/Network/network';
import { BN } from 'avalanche';
import { ChainIdType } from '@/types';
import { AVMConstants } from 'avalanche/dist/apis/avm';
import { bnToAvaxC, bnToAvaxP, bnToAvaxX, bnToLocaleString, parseNftPayload } from '@/utils/utils';
import { bnToAvaxC, bnToAvaxP, bnToAvaxX, bnToLocaleString, parseNftPayload } from '@/utils';
import * as Assets from '@/Asset/Assets';
import { NO_EXPLORER_API } from '@/errors';

Expand Down
2 changes: 1 addition & 1 deletion src/Wallet/MnemonicWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
KeyPair as EVMKeyPair,
} from 'avalanche/dist/apis/evm';
import { avalanche } from '@/Network/network';
import { digestMessage } from '@/utils/utils';
import { digestMessage } from '@/utils';
import { HDWalletAbstract } from '@/Wallet/HDWalletAbstract';
import { bintools } from '@/common';

Expand Down
2 changes: 1 addition & 1 deletion src/Wallet/Wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ import { PayloadBase, UnixNow } from 'avalanche/dist/utils';
import { getAssetDescription } from '@/Asset/Assets';
import { getErc20Token } from '@/Asset/Erc20';
import { NO_NETWORK } from '@/errors';
import { avaxCtoX, bnToLocaleString, waitTxC, waitTxEvm, waitTxP, waitTxX } from '@/utils/utils';
import { avaxCtoX, bnToLocaleString, waitTxC, waitTxEvm, waitTxP, waitTxX } from '@/utils';
import EvmWalletReadonly from '@/Wallet/EvmWalletReadonly';
import EventEmitter from 'events';
import {
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/gas_helper.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { web3 } from '@/Network/network';
import { BN } from 'avalanche';

const MAX_GAS = new BN(235000000000);
const MAX_GAS = new BN(1000_000_000_000);

/**
* Returns the current gas price in WEI from the network
Expand Down
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './utils';
export * from './number_utils';
187 changes: 187 additions & 0 deletions src/utils/number_utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
import { BN } from 'avalanche';
import Big from 'big.js';

declare module 'big.js' {
interface Big {
toLocaleString(toFixed?: number): string;
}
}

Big.prototype.toLocaleString = function (toFixed: number = 9) {
let fixedStr = this.toFixed(toFixed);
let split = fixedStr.split('.');
let wholeStr = parseInt(split[0]).toLocaleString('en-US');

if (split.length === 1) {
return wholeStr;
} else {
let remainderStr = split[1];

// remove trailing 0s
let lastChar = remainderStr.charAt(remainderStr.length - 1);
while (lastChar === '0') {
remainderStr = remainderStr.substring(0, remainderStr.length - 1);
lastChar = remainderStr.charAt(remainderStr.length - 1);
}

let trimmed = remainderStr.substring(0, toFixed);
if (!trimmed) return wholeStr;
return `${wholeStr}.${trimmed}`;
}
};

/**
* @param val the amount to parse
* @param denomination number of decimal places to parse with
*/
export function bnToBig(val: BN, denomination = 0): Big {
return new Big(val.toString()).div(Math.pow(10, denomination));
}

/**
* Converts a BN amount of 18 decimals to 9.
* Used for AVAX C <-> X,P conversions
* @param amount
*/
export function avaxCtoX(amount: BN) {
let tens = new BN(10).pow(new BN(9));
return amount.div(tens);
}

export function avaxXtoC(amount: BN) {
let tens = new BN(10).pow(new BN(9));
return amount.mul(tens);
}

export function avaxPtoC(amount: BN) {
return avaxXtoC(amount);
}

export function bnToBigAvaxX(val: BN): Big {
return bnToBig(val, 9);
}

export function bnToBigAvaxP(val: BN): Big {
return bnToBigAvaxX(val);
}

export function bnToBigAvaxC(val: BN): Big {
return bnToBig(val, 18);
}

/**
* Parses the value using a denomination of 18
*
* @param val the amount to parse given in WEI
*
* @example
* ```
* bnToAvaxC(new BN('22500000000000000000')
* // will return 22.5
*```
*
*/
export function bnToAvaxC(val: BN): string {
return bnToLocaleString(val, 18);
}

/**
* Parses the value using a denomination of 9
*
* @param val the amount to parse given in nAVAX
*/
export function bnToAvaxX(val: BN): string {
return bnToLocaleString(val, 9);
}

/**
* Parses the value using a denomination of 9
*
* @param val the amount to parse given in nAVAX
*/
export function bnToAvaxP(val: BN): string {
return bnToAvaxX(val);
}

/**
*
* @param val the number to parse
* @param decimals number of decimal places used to parse the number
*/
export function numberToBN(val: number | string, decimals: number): BN {
let valBig = Big(val);
let tens = Big(10).pow(decimals);
let valBN = new BN(valBig.times(tens).toFixed(0));
return valBN;
}

export function numberToBNAvaxX(val: number | string) {
return numberToBN(val, 9);
}

export function numberToBNAvaxP(val: number | string) {
return numberToBNAvaxX(val);
}

export function numberToBNAvaxC(val: number | string) {
return numberToBN(val, 18);
}

/**
* @Remarks
* A helper method to convert BN numbers to human readable strings.
*
* @param val The amount to convert
* @param decimals Number of decimal places to parse the amount with
*
* @example
* ```
* bnToLocaleString(new BN(100095),2)
* // will return '1,000.95'
* ```
*/
export function bnToLocaleString(val: BN, decimals = 9): string {
let bigVal = bnToBig(val, decimals);
return bigToLocaleString(bigVal, decimals);
}

export function bigToLocaleString(bigVal: Big, decimals: number = 9): string {
let fixedStr = bigVal.toFixed(decimals);
let split = fixedStr.split('.');
let wholeStr = parseInt(split[0]).toLocaleString('en-US');

if (split.length === 1) {
return wholeStr;
} else {
let remainderStr = split[1];

// remove trailing 0s
let lastChar = remainderStr.charAt(remainderStr.length - 1);
while (lastChar === '0') {
remainderStr = remainderStr.substring(0, remainderStr.length - 1);
lastChar = remainderStr.charAt(remainderStr.length - 1);
}

let trimmed = remainderStr.substring(0, decimals);
if (!trimmed) return wholeStr;
return `${wholeStr}.${trimmed}`;
}
}

/**
* Converts a string to a BN value of the given denomination.
* @param value The string value of the
* @param decimals
*
* @example
* ```
* stringToBN('1.32', 5) // is same as BN(132000)
* ```
*/
export function stringToBN(value: string, decimals: number) {
let big = Big(value);
let tens = Big(10).pow(decimals);
let mult = big.times(tens);
let rawStr = mult.toFixed(0);
return new BN(rawStr);
}
Loading

0 comments on commit cc583a2

Please sign in to comment.