aboutsummaryrefslogtreecommitdiffstats
path: root/packages/web3-wrapper/src/utils.ts
diff options
context:
space:
mode:
authorLeonid Logvinov <logvinov.leon@gmail.com>2018-07-05 22:00:50 +0800
committerGitHub <noreply@github.com>2018-07-05 22:00:50 +0800
commit01c6efeaf10524a9f412c7c638c99afcf0fd3070 (patch)
treefa5dc6f41c56970dd82e9bf0a8d407dee43f823b /packages/web3-wrapper/src/utils.ts
parent22cf7bee5b089384d43e6df0101699491c17847f (diff)
parent87a7a4ad2d55641cbe06d7157df0d8f0996d033a (diff)
downloaddexon-sol-tools-01c6efeaf10524a9f412c7c638c99afcf0fd3070.tar
dexon-sol-tools-01c6efeaf10524a9f412c7c638c99afcf0fd3070.tar.gz
dexon-sol-tools-01c6efeaf10524a9f412c7c638c99afcf0fd3070.tar.bz2
dexon-sol-tools-01c6efeaf10524a9f412c7c638c99afcf0fd3070.tar.lz
dexon-sol-tools-01c6efeaf10524a9f412c7c638c99afcf0fd3070.tar.xz
dexon-sol-tools-01c6efeaf10524a9f412c7c638c99afcf0fd3070.tar.zst
dexon-sol-tools-01c6efeaf10524a9f412c7c638c99afcf0fd3070.zip
Merge branch 'v2-prototype' into v2-contract-wrappers-WIP
Diffstat (limited to 'packages/web3-wrapper/src/utils.ts')
-rw-r--r--packages/web3-wrapper/src/utils.ts58
1 files changed, 58 insertions, 0 deletions
diff --git a/packages/web3-wrapper/src/utils.ts b/packages/web3-wrapper/src/utils.ts
new file mode 100644
index 000000000..d13eb9404
--- /dev/null
+++ b/packages/web3-wrapper/src/utils.ts
@@ -0,0 +1,58 @@
+import { BigNumber } from '@0xproject/utils';
+import * as _ from 'lodash';
+
+export const utils = {
+ isBigNumber(value: any): boolean {
+ const isBigNumber = _.isObject(value) && value.isBigNumber;
+ return isBigNumber;
+ },
+ convertHexToNumber(value: string): number {
+ 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)
+ );
+ },
+};