From 421b2c857ba4bd61b7e10d04c381f992e7d2cc81 Mon Sep 17 00:00:00 2001 From: Richard Moore Date: Thu, 2 Aug 2018 21:34:10 -0400 Subject: [PATCH] Fixing TypeScript compiler using host paths for resolving constants. --- src.ts/contracts/contract.ts | 5 +++-- src.ts/ethers.ts | 2 +- src.ts/utils/abi-coder.ts | 9 +++++---- src.ts/utils/bignumber.ts | 6 ------ src.ts/utils/constants.ts | 36 ++++++++++++++++++------------------ src.ts/utils/transaction.ts | 5 +++-- src.ts/utils/units.ts | 9 +++++---- 7 files changed, 35 insertions(+), 37 deletions(-) diff --git a/src.ts/contracts/contract.ts b/src.ts/contracts/contract.ts index 543acb259..e3ecaf1ba 100644 --- a/src.ts/contracts/contract.ts +++ b/src.ts/contracts/contract.ts @@ -4,8 +4,9 @@ import { Indexed, Interface } from './interface'; import { defaultAbiCoder, formatSignature, parseSignature } from '../utils/abi-coder'; import { getAddress, getContractAddress } from '../utils/address'; -import { BigNumber, bigNumberify, ConstantZero } from '../utils/bignumber'; +import { BigNumber, bigNumberify } from '../utils/bignumber'; import { hexDataLength, hexDataSlice, isHexString } from '../utils/bytes'; +import { Zero } from '../utils/constants'; import { defineReadOnly, jsonCopy, shallowCopy } from '../utils/properties'; import { poll } from '../utils/web'; @@ -139,7 +140,7 @@ function runMethod(contract: Contract, functionName: string, estimateOnly: boole // Call (constant functions) always cost 0 ether if (estimateOnly) { - return Promise.resolve(ConstantZero); + return Promise.resolve(Zero); } if (!contract.provider) { diff --git a/src.ts/ethers.ts b/src.ts/ethers.ts index f3cba4077..3557f7938 100644 --- a/src.ts/ethers.ts +++ b/src.ts/ethers.ts @@ -6,7 +6,7 @@ import * as providers from './providers'; import { HDNode, SigningKey, Wallet } from './wallet'; -import { constants } from './utils/constants'; +import * as constants from './utils/constants'; import * as errors from './utils/errors'; import * as utils from './utils'; diff --git a/src.ts/utils/abi-coder.ts b/src.ts/utils/abi-coder.ts index d8d47b09b..01872bbe3 100644 --- a/src.ts/utils/abi-coder.ts +++ b/src.ts/utils/abi-coder.ts @@ -3,8 +3,9 @@ // See: https://github.com/ethereum/wiki/wiki/Ethereum-Contract-ABI import { getAddress } from './address'; -import { BigNumber, bigNumberify, ConstantNegativeOne, ConstantZero, ConstantOne, ConstantMaxUint256 } from './bignumber'; +import { BigNumber, bigNumberify } from './bignumber'; import { arrayify, concat, hexlify, padZeros } from './bytes'; +import { NegativeOne, Zero, One, MaxUint256 } from '../utils/constants'; import { toUtf8Bytes, toUtf8String } from './utf8'; import { defineReadOnly, jsonCopy } from './properties'; @@ -449,11 +450,11 @@ class CoderNumber extends Coder { try { let v = bigNumberify(value); if (this.signed) { - let bounds = ConstantMaxUint256.maskn(this.size * 8 - 1); + let bounds = MaxUint256.maskn(this.size * 8 - 1); if (v.gt(bounds)) { throw new Error('out-of-bounds'); } - bounds = bounds.add(ConstantOne).mul(ConstantNegativeOne); + bounds = bounds.add(One).mul(NegativeOne); if (v.lt(bounds)) { throw new Error('out-of-bounds'); } - } else if (v.lt(ConstantZero) || v.gt(ConstantMaxUint256.maskn(this.size * 8))) { + } else if (v.lt(Zero) || v.gt(MaxUint256.maskn(this.size * 8))) { throw new Error('out-of-bounds'); } diff --git a/src.ts/utils/bignumber.ts b/src.ts/utils/bignumber.ts index 663bab43f..1a5291886 100644 --- a/src.ts/utils/bignumber.ts +++ b/src.ts/utils/bignumber.ts @@ -188,9 +188,3 @@ export function bigNumberify(value: BigNumberish): BigNumber { return new BigNumber(value); } -export const ConstantNegativeOne = bigNumberify(-1); -export const ConstantZero = bigNumberify(0); -export const ConstantOne = bigNumberify(1); -export const ConstantTwo = bigNumberify(2); -export const ConstantWeiPerEther = bigNumberify('1000000000000000000'); -export const ConstantMaxUint256 = bigNumberify('0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'); diff --git a/src.ts/utils/constants.ts b/src.ts/utils/constants.ts index d4a2357f6..fc5480a33 100644 --- a/src.ts/utils/constants.ts +++ b/src.ts/utils/constants.ts @@ -1,11 +1,4 @@ -import { - ConstantNegativeOne, - ConstantZero, - ConstantOne, - ConstantTwo, - ConstantWeiPerEther, - ConstantMaxUint256 -} from './bignumber'; +import { BigNumber, bigNumberify } from './bignumber'; const AddressZero = '0x0000000000000000000000000000000000000000'; const HashZero = '0x0000000000000000000000000000000000000000000000000000000000000000'; @@ -16,16 +9,23 @@ const HashZero = '0x000000000000000000000000000000000000000000000000000000000000 // NFKC (composed) const EtherSymbol = '\u039e'; -export const constants = { - AddressZero: AddressZero, - HashZero: HashZero, +const NegativeOne: BigNumber = bigNumberify(-1); +const Zero: BigNumber = bigNumberify(0); +const One: BigNumber = bigNumberify(1); +const Two: BigNumber = bigNumberify(2); +const WeiPerEther: BigNumber = bigNumberify('1000000000000000000'); +const MaxUint256: BigNumber = bigNumberify('0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'); - EtherSymbol: EtherSymbol, +export { + AddressZero, + HashZero, - NegativeOne: ConstantNegativeOne, - Zero: ConstantZero, - One: ConstantOne, - Two: ConstantTwo, - WeiPerEther: ConstantWeiPerEther, - MaxUint256: ConstantMaxUint256 + EtherSymbol, + + NegativeOne, + Zero, + One, + Two, + WeiPerEther, + MaxUint256 }; diff --git a/src.ts/utils/transaction.ts b/src.ts/utils/transaction.ts index 140f5fd3f..2b8d18215 100644 --- a/src.ts/utils/transaction.ts +++ b/src.ts/utils/transaction.ts @@ -2,8 +2,9 @@ import { recoverAddress } from './secp256k1'; import { getAddress } from './address'; -import { BigNumber, bigNumberify, ConstantZero } from './bignumber'; +import { BigNumber, bigNumberify } from './bignumber'; import { arrayify, hexlify, hexZeroPad, splitSignature, stripZeros, } from './bytes'; +import { Zero } from './constants'; import { keccak256 } from './keccak256'; import * as RLP from './rlp'; @@ -58,7 +59,7 @@ function handleAddress(value: string): string { } function handleNumber(value: string): BigNumber { - if (value === '0x') { return ConstantZero; } + if (value === '0x') { return Zero; } return bigNumberify(value); } diff --git a/src.ts/utils/units.ts b/src.ts/utils/units.ts index 5ea45e4fc..0949497e8 100644 --- a/src.ts/utils/units.ts +++ b/src.ts/utils/units.ts @@ -1,6 +1,7 @@ 'use strict'; -import { BigNumber, bigNumberify, ConstantZero, ConstantNegativeOne } from './bignumber'; +import { BigNumber, bigNumberify } from './bignumber'; +import { Zero, NegativeOne } from './constants'; // Imported Types import { BigNumberish } from './bignumber'; @@ -79,8 +80,8 @@ export function formatUnits(value: BigNumberish, unitType?: string | number, opt // Make sure wei is a big number (convert as necessary) value = bigNumberify(value); - var negative = value.lt(ConstantZero); - if (negative) { value = value.mul(ConstantNegativeOne); } + var negative = value.lt(Zero); + if (negative) { value = value.mul(NegativeOne); } var fraction = value.mod(unitInfo.tenPower).toString(); while (fraction.length < unitInfo.decimals) { fraction = '0' + fraction; } @@ -153,7 +154,7 @@ export function parseUnits(value: string, unitType?: string | number): BigNumber let wei = (wholeValue.mul(unitInfo.tenPower)).add(fractionValue); - if (negative) { wei = wei.mul(ConstantNegativeOne); } + if (negative) { wei = wei.mul(NegativeOne); } return wei; }