From a371d6449a92ff3fbe01c96b5ec0a5fc70f2ba9c Mon Sep 17 00:00:00 2001 From: aitrean Date: Mon, 12 Feb 2018 14:02:49 -0500 Subject: [PATCH] More typings, declaration files. --- .../BalanceSidebar/TokenBalances/Balances.tsx | 4 ++-- .../WalletDecrypt/components/LedgerNano.tsx | 2 +- common/config/contracts/index.ts | 13 ++++++++++++- common/config/networks.ts | 6 +++++- common/config/tokens/index.ts | 14 +++++++++++++- common/typescript/ethjs-util.d.ts | 15 +++++++++++++++ common/typescript/hdkey.d.ts | 3 +++ package.json | 2 ++ spec/integration/data.int.ts | 8 ++++++-- spec/integration/derivationChecker.int.ts | 2 +- spec/reducers/swap.spec.ts | 3 ++- spec/sagas/config.spec.ts | 6 +++--- spec/sagas/swap/rates.spec.ts | 4 ++-- spec/sagas/transaction/broadcast/helpers.spec.tsx | 2 +- .../transaction/current/currentValue.spec.ts | 8 ++++---- spec/sagas/transaction/fields/fields.spec.ts | 3 ++- spec/sagas/transaction/meta/token.spec.ts | 3 ++- spec/sagas/transaction/meta/unitSwap.spec.ts | 6 +++--- spec/sagas/transaction/network/from.spec.ts | 2 +- spec/sagas/transaction/sendEverything.spec.ts | 3 ++- spec/sagas/transaction/signing/helpers.spec.ts | 2 +- spec/sagas/transaction/validationHelpers.spec.ts | 3 ++- spec/utils/shallowWithStore.ts | 2 +- 23 files changed, 86 insertions(+), 30 deletions(-) create mode 100644 common/typescript/ethjs-util.d.ts create mode 100644 common/typescript/hdkey.d.ts diff --git a/common/components/BalanceSidebar/TokenBalances/Balances.tsx b/common/components/BalanceSidebar/TokenBalances/Balances.tsx index d8c2d0a8..90eb73a0 100644 --- a/common/components/BalanceSidebar/TokenBalances/Balances.tsx +++ b/common/components/BalanceSidebar/TokenBalances/Balances.tsx @@ -16,7 +16,7 @@ interface Props { } interface TrackedTokens { - [symbol: string]: any; + [symbol: string]: boolean; } interface State { @@ -25,7 +25,7 @@ interface State { } export default class TokenBalances extends React.PureComponent { public state = { - trackedTokens: {}, + trackedTokens: {} as TrackedTokens, showCustomTokenForm: false }; diff --git a/common/components/WalletDecrypt/components/LedgerNano.tsx b/common/components/WalletDecrypt/components/LedgerNano.tsx index fca26640..ef9b8bd3 100644 --- a/common/components/WalletDecrypt/components/LedgerNano.tsx +++ b/common/components/WalletDecrypt/components/LedgerNano.tsx @@ -147,7 +147,7 @@ class LedgerNanoSDecryptClass extends PureComponent { ledger.comm_u2f.create_async().then((comm: any) => { new ledger.eth(comm) .getAddress_async(dPath, false, true) - .then((res: any) => { + .then(res => { this.setState({ publicKey: res.publicKey, chainCode: res.chainCode, diff --git a/common/config/contracts/index.ts b/common/config/contracts/index.ts index 95054404..773dd66b 100644 --- a/common/config/contracts/index.ts +++ b/common/config/contracts/index.ts @@ -6,7 +6,16 @@ import Ropsten from './ropsten.json'; import RSK from './rsk.json'; import UBQ from './ubq.json'; -export default { +export interface ContractData { + name: string; + address: string; + abi: string; +} +export interface Networks { + [key: string]: ContractData[]; +} + +const NetworkTypes: Networks = { ETC, ETH, EXP, @@ -15,3 +24,5 @@ export default { RSK, UBQ }; + +export default NetworkTypes; diff --git a/common/config/networks.ts b/common/config/networks.ts index 176d5bcc..d5270004 100644 --- a/common/config/networks.ts +++ b/common/config/networks.ts @@ -81,6 +81,10 @@ export interface CustomNodeConfig { }; } +export interface Networks { + [key: string]: NetworkConfig; +} + // Must be a website that follows the ethplorer convention of /tx/[hash] and // address/[address] to generate the correct functions. function makeExplorer(origin: string): BlockExplorerConfig { @@ -205,7 +209,7 @@ const EXP: NetworkConfig = { } }; -export const NETWORKS = { +export const NETWORKS: Networks = { ETH, Ropsten, Kovan, diff --git a/common/config/tokens/index.ts b/common/config/tokens/index.ts index 27c8a518..01ed8ee4 100644 --- a/common/config/tokens/index.ts +++ b/common/config/tokens/index.ts @@ -7,7 +7,17 @@ import Ropsten from './ropsten.json'; import RSK from './rsk.json'; import UBQ from './ubq.json'; -export default { +export interface Token { + address: string; + symbol: string; + decimal: number; + error?: string | null; +} +export interface Tokens { + [key: string]: Token; +} + +const TokenTypes: Tokens = { ETC, ETH, EXP, @@ -17,3 +27,5 @@ export default { RSK, UBQ }; + +export default TokenTypes; diff --git a/common/typescript/ethjs-util.d.ts b/common/typescript/ethjs-util.d.ts new file mode 100644 index 00000000..d3cf3039 --- /dev/null +++ b/common/typescript/ethjs-util.d.ts @@ -0,0 +1,15 @@ +declare module 'ethjs-util' { + export function arrayContainsArray(arrayA: any[], arrayB: any[]): Boolean; + export function getBinarySize(num: string): number; + export function intToBuffer(integer: number): Buffer; + export function isHexPrefixed(hex: string): boolean; + export function stripHexPrefix(hexWithPrefix: string): string; + export function padToEven(unpaddedNumber: string): string; + export function intToHex(integer: number): string; + export function fromAscii(ascii: string): string; + export function fromUtf8(utf8: string): string; + export function toAscii(nonAscii: string): string; + export function toUtf8(nonUtf8: string): string; + export function getKeys(keys: any[], query: string): any[]; + export function isHexString(inputString: string): boolean; +} diff --git a/common/typescript/hdkey.d.ts b/common/typescript/hdkey.d.ts new file mode 100644 index 00000000..ecad851f --- /dev/null +++ b/common/typescript/hdkey.d.ts @@ -0,0 +1,3 @@ +declare module 'hdkey' { + export function fromMasterSeed(seed: Buffer): any; +} diff --git a/package.json b/package.json index 8e9e08bc..f7c106be 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,8 @@ "npm": ">= 5.0.0" }, "dependencies": { + "@types/enzyme": "3.1.8", + "@types/enzyme-adapter-react-16": "1.0.1", "babel-polyfill": "6.26.0", "bip39": "2.5.0", "bn.js": "4.11.8", diff --git a/spec/integration/data.int.ts b/spec/integration/data.int.ts index 8cd56153..645cd0be 100644 --- a/spec/integration/data.int.ts +++ b/spec/integration/data.int.ts @@ -1,6 +1,6 @@ import { NODES, NodeConfig } from 'config'; import { RPCNode } from '../../common/libs/nodes'; -import { Validator } from 'jsonschema'; +import { Validator, ValidatorResult } from 'jsonschema'; import { schema } from '../../common/libs/validators'; import 'url-search-params-polyfill'; import EtherscanNode from 'libs/nodes/etherscan'; @@ -24,6 +24,10 @@ const validRequests = { } }; +interface RPCTestList { + [key: string]: ((n: RPCNode) => Promise); +} + const testGetBalance = (n: RPCNode) => { return n.client .call(n.requests.getBalance(validRequests.address)) @@ -43,7 +47,7 @@ const testGetTokenBalance = (n: RPCNode) => { .then(data => v.validate(data, schema.RpcNode)); }; -const RPCTests = { +const RPCTests: RPCTestList = { getBalance: testGetBalance, estimateGas: testEstimateGas, getTokenBalance: testGetTokenBalance diff --git a/spec/integration/derivationChecker.int.ts b/spec/integration/derivationChecker.int.ts index 487ffe78..510d5165 100644 --- a/spec/integration/derivationChecker.int.ts +++ b/spec/integration/derivationChecker.int.ts @@ -9,7 +9,7 @@ const dockerTag = 'latest'; function promiseFromChildProcess(command: string): Promise { return new Promise((resolve, reject) => { - return exec(command, (err, stdout) => { + return exec(command, (err: any, stdout: any) => { err ? reject(err) : resolve(stdout); }); }); diff --git a/spec/reducers/swap.spec.ts b/spec/reducers/swap.spec.ts index c3ff7a98..34bb4125 100644 --- a/spec/reducers/swap.spec.ts +++ b/spec/reducers/swap.spec.ts @@ -8,11 +8,12 @@ import { import { normalize } from 'normalizr'; import * as schema from 'reducers/swap/schema'; import { TypeKeys } from 'actions/swap/constants'; +import { Token } from 'config/tokens'; import tokens from 'config/tokens/eth.json'; import { SHAPESHIFT_TOKEN_WHITELIST } from 'api/shapeshift'; describe('ensure whitelist', () => { - const findToken = (tkn: string) => tokens.find(t => t.symbol === tkn); + const findToken = (tkn: string) => tokens.find((t: Token) => t.symbol === tkn); SHAPESHIFT_TOKEN_WHITELIST.forEach(t => { it(`Should find Token ${t}`, () => { expect(findToken(t)).toBeTruthy(); diff --git a/spec/sagas/config.spec.ts b/spec/sagas/config.spec.ts index 9e52e5e9..c576863b 100644 --- a/spec/sagas/config.spec.ts +++ b/spec/sagas/config.spec.ts @@ -1,5 +1,5 @@ import { configuredStore } from 'store'; -import { delay } from 'redux-saga'; +import { delay, SagaIterator } from 'redux-saga'; import { call, cancel, fork, put, take, select } from 'redux-saga/effects'; import { cloneableGenerator, createMockTask } from 'redux-saga/utils'; import { toggleOfflineConfig, changeNode, changeNodeIntent, setLatestBlock } from 'actions/config'; @@ -158,7 +158,7 @@ describe('handleNodeChangeIntent*', () => { const data = {} as any; data.gen = cloneableGenerator(handleNodeChangeIntent)(changeNodeIntentAction); - function shouldBailOut(gen, nextVal, errMsg) { + function shouldBailOut(gen: SagaIterator, nextVal: any, errMsg: any) { expect(gen.next(nextVal).value).toEqual(put(showNotification('danger', errMsg, 5000))); expect(gen.next().value).toEqual( put(changeNode(defaultNode, defaultNodeConfig, defaultNodeNetwork)) @@ -287,7 +287,7 @@ describe('unsetWeb3Node*', () => { }); describe('unsetWeb3NodeOnWalletEvent*', () => { - const fakeAction = {}; + const fakeAction = {} as any; const mockNode = 'web3'; const mockNodeConfig: Partial = { network: 'ETH' }; const gen = unsetWeb3NodeOnWalletEvent(fakeAction); diff --git a/spec/sagas/swap/rates.spec.ts b/spec/sagas/swap/rates.spec.ts index 5e8399c5..1e8d53f0 100644 --- a/spec/sagas/swap/rates.spec.ts +++ b/spec/sagas/swap/rates.spec.ts @@ -31,7 +31,7 @@ describe('loadBityRates*', () => { rate: 0.042958 } }; - let random; + let random: () => number; beforeAll(() => { random = Math.random; @@ -87,7 +87,7 @@ describe('loadShapeshiftRates*', () => { min: 7.86382979 } }; - let random; + let random: () => number; beforeAll(() => { random = Math.random; diff --git a/spec/sagas/transaction/broadcast/helpers.spec.tsx b/spec/sagas/transaction/broadcast/helpers.spec.tsx index 11eb3657..692a5ceb 100644 --- a/spec/sagas/transaction/broadcast/helpers.spec.tsx +++ b/spec/sagas/transaction/broadcast/helpers.spec.tsx @@ -36,7 +36,7 @@ describe('broadcastTransactionWrapper*', () => { blockExplorer: 'blockExplorer' }; - let random; + let random: () => number; const func: any = () => undefined; const action: any = {}; const gens: any = {}; diff --git a/spec/sagas/transaction/current/currentValue.spec.ts b/spec/sagas/transaction/current/currentValue.spec.ts index ca8a8165..412a4b7f 100644 --- a/spec/sagas/transaction/current/currentValue.spec.ts +++ b/spec/sagas/transaction/current/currentValue.spec.ts @@ -8,11 +8,11 @@ import { reparseCurrentValue, valueHandler } from 'sagas/transaction/current/currentValue'; -import { SagaIterator } from 'redux-saga'; import { cloneableGenerator, SagaIteratorClone } from 'redux-saga/utils'; import { select, call, put } from 'redux-saga/effects'; +import { SagaIterator } from 'redux-saga'; -const itShouldBeDone = (gen: any) => { +const itShouldBeDone = (gen: SagaIterator) => { it('should be done', () => { expect(gen.next().done).toEqual(true); }); @@ -85,7 +85,7 @@ describe('setCurrentValue*', () => { describe('revalidateCurrentValue*', () => { const sharedLogic = ( - gen: any, + gen: SagaIterator, etherTransaction: boolean, currVal: any, reparsedValue: boolean @@ -165,7 +165,7 @@ describe('revalidateCurrentValue*', () => { }); describe('reparseCurrentValue*', () => { - const sharedLogic = (gen: any) => { + const sharedLogic = (gen: SagaIterator) => { it('should select getDecimal', () => { expect(gen.next().value).toEqual(select(getDecimal)); }); diff --git a/spec/sagas/transaction/fields/fields.spec.ts b/spec/sagas/transaction/fields/fields.spec.ts index 459c7058..c4954315 100644 --- a/spec/sagas/transaction/fields/fields.spec.ts +++ b/spec/sagas/transaction/fields/fields.spec.ts @@ -1,4 +1,5 @@ import BN from 'bn.js'; +import { SagaIterator } from 'redux-saga'; import { call, put } from 'redux-saga/effects'; import { setDataField, setGasLimitField, setNonceField } from 'actions/transaction/actionCreators'; import { isValidHex, isValidNonce, gasPriceValidator, gasLimitValidator } from 'libs/validators'; @@ -12,7 +13,7 @@ import { import { cloneableGenerator } from 'redux-saga/utils'; import { setGasPriceField } from 'actions/transaction'; -const itShouldBeDone = gen => { +const itShouldBeDone = (gen: SagaIterator) => { it('should be done', () => { expect(gen.next().done).toEqual(true); }); diff --git a/spec/sagas/transaction/meta/token.spec.ts b/spec/sagas/transaction/meta/token.spec.ts index 19eccb92..bbdd25ec 100644 --- a/spec/sagas/transaction/meta/token.spec.ts +++ b/spec/sagas/transaction/meta/token.spec.ts @@ -1,3 +1,4 @@ +import { SagaIterator } from 'redux-saga'; import { select, call, put } from 'redux-saga/effects'; import { setDataField } from 'actions/transaction'; import { encodeTransfer } from 'libs/transaction/utils/token'; @@ -7,7 +8,7 @@ import { getTokenTo, getData } from 'selectors/transaction'; import { handleTokenTo, handleTokenValue } from 'sagas/transaction/meta/token'; import { cloneableGenerator } from 'redux-saga/utils'; -const itShouldBeDone = (gen: any) => { +const itShouldBeDone = (gen: SagaIterator) => { it('should be done', () => { expect(gen.next().done).toEqual(true); }); diff --git a/spec/sagas/transaction/meta/unitSwap.spec.ts b/spec/sagas/transaction/meta/unitSwap.spec.ts index fcf56d61..0817c8a4 100644 --- a/spec/sagas/transaction/meta/unitSwap.spec.ts +++ b/spec/sagas/transaction/meta/unitSwap.spec.ts @@ -22,14 +22,14 @@ import { rebaseUserInput, validateInput } from 'sagas/transaction/validationHelp import { handleSetUnitMeta } from 'sagas/transaction/meta/unitSwap'; import BN from 'bn.js'; -const itShouldBeDone = (gen: any) => { +const itShouldBeDone = (gen: SagaIterator) => { it('should be done', () => { expect(gen.next().done).toEqual(true); }); }; describe('handleSetUnitMeta*', () => { - const expectedStart = (gen: any, previousUnit: string, currentUnit: string) => { + const expectedStart = (gen: SagaIterator, previousUnit: string, currentUnit: string) => { it('should select getPreviousUnit', () => { expect(gen.next().value).toEqual(select(getPreviousUnit)); }); @@ -118,7 +118,7 @@ describe('handleSetUnitMeta*', () => { }; const sharedLogicB = ( - gen: any, + gen: SagaIterator, input: string, raw: string, value: BN, diff --git a/spec/sagas/transaction/network/from.spec.ts b/spec/sagas/transaction/network/from.spec.ts index a471930e..01ea1bc1 100644 --- a/spec/sagas/transaction/network/from.spec.ts +++ b/spec/sagas/transaction/network/from.spec.ts @@ -12,7 +12,7 @@ describe('handleFromRequest*', () => { const fromAddress = '0xa'; const gens: any = {}; gens.gen = cloneableGenerator(handleFromRequest)(); - let random; + let random: () => number; beforeAll(() => { random = Math.random; diff --git a/spec/sagas/transaction/sendEverything.spec.ts b/spec/sagas/transaction/sendEverything.spec.ts index 25b47fbb..8efa3121 100644 --- a/spec/sagas/transaction/sendEverything.spec.ts +++ b/spec/sagas/transaction/sendEverything.spec.ts @@ -1,3 +1,4 @@ +import { SagaIterator } from 'redux-saga'; import { apply, put, select } from 'redux-saga/effects'; import { sendEverythingFailed, @@ -24,7 +25,7 @@ describe('handleSendEverything*', () => { Math.random = random; }); - const sharedStart = (gen: any, transactionObj: any, currentBalance: BN | null) => { + const sharedStart = (gen: SagaIterator, transactionObj: any, currentBalance: BN | null) => { it('should select getTransaction', () => { expect(gen.next().value).toEqual(select(getTransaction)); }); diff --git a/spec/sagas/transaction/signing/helpers.spec.ts b/spec/sagas/transaction/signing/helpers.spec.ts index 5346ff02..c0ee6e20 100644 --- a/spec/sagas/transaction/signing/helpers.spec.ts +++ b/spec/sagas/transaction/signing/helpers.spec.ts @@ -95,7 +95,7 @@ describe('getWalletAndTransaction*', () => { describe('handleFailedTransaction*', () => { const err = new Error('Message'); const gen = handleFailedTransaction(err); - let random; + let random: () => number; beforeAll(() => { random = Math.random; diff --git a/spec/sagas/transaction/validationHelpers.spec.ts b/spec/sagas/transaction/validationHelpers.spec.ts index 5a656615..79248f1a 100644 --- a/spec/sagas/transaction/validationHelpers.spec.ts +++ b/spec/sagas/transaction/validationHelpers.spec.ts @@ -1,3 +1,4 @@ +import { SagaIterator } from 'redux-saga'; import { select, call } from 'redux-saga/effects'; import { getUnit, getDecimalFromUnit, getGasLimit, getGasPrice } from 'selectors/transaction'; import { getEtherBalance, getTokenBalance } from 'selectors/wallet'; @@ -11,7 +12,7 @@ import { import { cloneableGenerator } from 'redux-saga/utils'; import { getOffline } from 'selectors/config'; -const itShouldBeDone = gen => { +const itShouldBeDone = (gen: SagaIterator) => { it('should be done', () => { expect(gen.next().done).toEqual(true); }); diff --git a/spec/utils/shallowWithStore.ts b/spec/utils/shallowWithStore.ts index b0a07f74..232cb394 100644 --- a/spec/utils/shallowWithStore.ts +++ b/spec/utils/shallowWithStore.ts @@ -1,6 +1,6 @@ import { shallow } from 'enzyme'; -const shallowWithStore = (component, store) => { +const shallowWithStore = (component: any, store: any) => { const context = { store };