aboutsummaryrefslogtreecommitdiffstats
path: root/packages/web3-wrapper/src/utils.ts
diff options
context:
space:
mode:
authorFabio Berger <me@fabioberger.com>2018-07-05 07:38:14 +0800
committerFabio Berger <me@fabioberger.com>2018-07-05 07:38:14 +0800
commitfd242a9cba7516f08579f0e97923a15740ef4d7a (patch)
treeda5e1aa87d2f69df22f5f7a396070cea00eed7a7 /packages/web3-wrapper/src/utils.ts
parent7efa17ef7d855e45cb874adedeee8dfc935fe40e (diff)
downloaddexon-sol-tools-fd242a9cba7516f08579f0e97923a15740ef4d7a.tar
dexon-sol-tools-fd242a9cba7516f08579f0e97923a15740ef4d7a.tar.gz
dexon-sol-tools-fd242a9cba7516f08579f0e97923a15740ef4d7a.tar.bz2
dexon-sol-tools-fd242a9cba7516f08579f0e97923a15740ef4d7a.tar.lz
dexon-sol-tools-fd242a9cba7516f08579f0e97923a15740ef4d7a.tar.xz
dexon-sol-tools-fd242a9cba7516f08579f0e97923a15740ef4d7a.tar.zst
dexon-sol-tools-fd242a9cba7516f08579f0e97923a15740ef4d7a.zip
Remove web3Utils dep and write necessary utility methods
Diffstat (limited to 'packages/web3-wrapper/src/utils.ts')
-rw-r--r--packages/web3-wrapper/src/utils.ts51
1 files changed, 51 insertions, 0 deletions
diff --git a/packages/web3-wrapper/src/utils.ts b/packages/web3-wrapper/src/utils.ts
index 376f7e89b..43ab73238 100644
--- a/packages/web3-wrapper/src/utils.ts
+++ b/packages/web3-wrapper/src/utils.ts
@@ -1,3 +1,4 @@
+import { BigNumber } from '@0xproject/utils';
import * as _ from 'lodash';
export const utils = {
@@ -5,4 +6,54 @@ export const utils = {
const isBigNumber = _.isObject(value) && (value as any).isBigNumber;
return isBigNumber;
},
+ convertHexToNumber(value: string): number {
+ console.log('value', value);
+ const valueBigNumber = new BigNumber(value);
+ const valueNumber = valueBigNumber.toNumber();
+ return valueNumber;
+ },
+ convertHexToNumberOrNull(hex: string | null): number | null {
+ if (_.isNull(hex)) {
+ return null;
+ }
+ const decimal = this.convertHexToNumber(hex);
+ return decimal;
+ },
+ convertAmountToBigNumber(value: string | number | BigNumber): BigNumber {
+ const num = value || 0;
+ const isBigNumber = utils.isBigNumber(num);
+ if (isBigNumber) {
+ return num as BigNumber;
+ }
+
+ if (_.isString(num) && (num.indexOf('0x') === 0 || num.indexOf('-0x') === 0)) {
+ return new BigNumber(num.replace('0x', ''), 16);
+ }
+
+ const baseTen = 10;
+ return new BigNumber((num as number).toString(baseTen), baseTen);
+ },
+ encodeAmountAsHexString(value: string | number | BigNumber): string {
+ const valueBigNumber = utils.convertAmountToBigNumber(value);
+ const hexBase = 16;
+ const valueHex = valueBigNumber.toString(hexBase);
+
+ return valueBigNumber.lessThan(0) ? '-0x' + valueHex.substr(1) : '0x' + valueHex;
+ },
+ numberToHex(value: number): string {
+ if (!isFinite(value) && !this.isHexStrict(value)) {
+ throw new Error('Given input "' + value + '" is not a number.');
+ }
+
+ const valueBigNumber = new BigNumber(value);
+ const hexBase = 16;
+ const result = valueBigNumber.toString(hexBase);
+
+ return valueBigNumber.lt(0) ? '-0x' + result.substr(1) : '0x' + result;
+ },
+ isHexStrict(hex: string | number): boolean {
+ return (
+ (_.isString(hex) || _.isNumber(hex)) && /^(-)?0x[0-9a-f]*$/i.test(_.isNumber(hex) ? hex.toString() : hex)
+ );
+ },
};