From 5babe75cda4544e34f6621a4e2ae3bec5462f56a Mon Sep 17 00:00:00 2001 From: Szymon Lesisz Date: Wed, 12 Sep 2018 13:05:48 +0200 Subject: [PATCH] update flowtype --- src/flowtype/index.js | 50 ++++++++ src/flowtype/npm/bignumber.js | 4 +- src/flowtype/npm/redux_v3.x.x.js | 8 +- src/flowtype/npm/web3.js | 190 ++++++++++++------------------- 4 files changed, 133 insertions(+), 119 deletions(-) diff --git a/src/flowtype/index.js b/src/flowtype/index.js index 025df015..3fd602a0 100644 --- a/src/flowtype/index.js +++ b/src/flowtype/index.js @@ -7,6 +7,8 @@ import type { Middleware as ReduxMiddleware, ThunkAction as ReduxThunkAction, AsyncAction as ReduxAsyncAction, + PromiseAction as ReduxPromiseAction, + ThunkDispatch as ReduxThunkDispatch, PlainDispatch as ReduxPlainDispatch, } from 'redux'; @@ -15,6 +17,7 @@ import type { ReducersState } from 'reducers'; // Actions import type { SelectedAccountAction } from 'actions/SelectedAccountActions'; import type { AccountAction } from 'actions/AccountsActions'; +import type { BlockchainAction } from 'actions/BlockchainActions'; import type { DiscoveryAction } from 'actions/DiscoveryActions'; import type { StorageAction } from 'actions/LocalStorageActions'; import type { LogAction } from 'actions/LogActions'; @@ -37,6 +40,7 @@ import type { DeviceFirmwareStatus, DeviceMessageType, TransportMessageType, + BlockchainMessageType, UiMessageType, } from 'trezor-connect'; @@ -102,6 +106,11 @@ type UiEventAction = { // }, } +type BlockchainEventAction = { + type: BlockchainMessageType, + payload: any, +} + // TODO: join this message with uiMessage type IFrameHandshake = { type: 'iframe_handshake', @@ -114,9 +123,11 @@ export type Action = | TransportEventAction | DeviceEventAction | UiEventAction + | BlockchainEventAction | SelectedAccountAction | AccountAction + | BlockchainAction | DiscoveryAction | StorageAction | LogAction @@ -154,6 +165,45 @@ export type Middleware = ReduxMiddleware; export type ThunkAction = ReduxThunkAction; export type AsyncAction = ReduxAsyncAction; +export type PromiseAction = ReduxPromiseAction; export type Store = ReduxStore; export type GetState = () => State; + + +// temporary types +export type AccountDiscovery = { + transactions: number; + block: number; + balance: string; + nonce: number; +} + +import type { Token } from 'reducers/TokensReducer'; + +export type EthereumTxRequest = { + network: string; + token: ?Token; + from: string; + to: string; + amount: string; + data: string; + gasLimit: string; + gasPrice: string; + nonce: number; +} +// copypaste from trezor-connect +export type EthereumPreparedTx = { + to: string, + value: string, + gasPrice: string, + gasLimit: string, + nonce: string, + data?: string, + chainId?: number, + txType?: number, + v: string, + r: string, + s: string, +} + diff --git a/src/flowtype/npm/bignumber.js b/src/flowtype/npm/bignumber.js index 5fa3a6fe..9f34ac28 100644 --- a/src/flowtype/npm/bignumber.js +++ b/src/flowtype/npm/bignumber.js @@ -1,3 +1,5 @@ +/* @flow */ + declare module 'bignumber.js' { declare type $npm$big$number$object = number | string | T_BigNumber declare type $npm$cmp$result = -1 | 0 | 1 @@ -24,7 +26,7 @@ declare module 'bignumber.js' { constructor(value: $npm$big$number$object): T_BigNumber; // Methods - abs(): BigNumber; + abs(): T_BigNumber; cmp(n: $npm$big$number$object): $npm$cmp$result; div(n: $npm$big$number$object): T_BigNumber; dividedBy(n: $npm$big$number$object): T_BigNumber; diff --git a/src/flowtype/npm/redux_v3.x.x.js b/src/flowtype/npm/redux_v3.x.x.js index d0717bdf..cdd88212 100644 --- a/src/flowtype/npm/redux_v3.x.x.js +++ b/src/flowtype/npm/redux_v3.x.x.js @@ -1,10 +1,12 @@ +/* @flow */ + declare module 'redux' { /* S = State A = Action D = Dispatch - + R = Promise response */ declare export type DispatchAPI = (action: A) => A; @@ -13,12 +15,14 @@ declare module 'redux' { declare export type ThunkAction = (dispatch: ReduxDispatch, getState: () => S) => void; declare export type AsyncAction = (dispatch: ReduxDispatch, getState: () => S) => Promise; + declare export type PromiseAction = (dispatch: ReduxDispatch, getState: () => S) => Promise; declare export type ThunkDispatch = (action: ThunkAction) => void; declare export type AsyncDispatch = (action: AsyncAction) => Promise; + declare export type PromiseDispatch = (action: PromiseAction) => Promise; declare export type PlainDispatch}> = DispatchAPI; /* NEW: Dispatch is now a combination of these different dispatch types */ - declare export type ReduxDispatch = PlainDispatch & ThunkDispatch & AsyncDispatch; + declare export type ReduxDispatch = PlainDispatch & ThunkDispatch & AsyncDispatch & PromiseDispatch; declare export type MiddlewareAPI = { // dispatch: Dispatch; diff --git a/src/flowtype/npm/web3.js b/src/flowtype/npm/web3.js index 81e27543..eff45ab0 100644 --- a/src/flowtype/npm/web3.js +++ b/src/flowtype/npm/web3.js @@ -1,8 +1,10 @@ +/* @flow */ + import type BigNumber from 'bignumber.js'; import type { EthereumUnitT, EthereumAddressT } from 'ethereum-types'; declare module 'web3' { - declare type ProviderT = { + declare type HttpProviderT = { host: string; timeout: number; isConnected: () => boolean; @@ -10,14 +12,25 @@ declare module 'web3' { sendAsync: (payload: any, callback: (error: Error, result: any) => void) => any; }; + declare type WebsocketProviderT = { + on: (type: string, callback: () => any) => void; + removeAllListeners: (type: string) => void; + reset: () => void; + connected: boolean; + } + declare class Web3T { static providers: { - HttpProvider: (host: string, timeout?: number) => ProviderT; + HttpProvider: (host: string, timeout?: number) => HttpProviderT; + WebsocketProvider: (host: string, options?: any) => WebsocketProviderT; }; - constructor(ProviderT): Web3T; - currentProvider: ProviderT; + // constructor(HttpProviderT): Web3T; + constructor(WebsocketProviderT): Web3T; + // currentProvider: HttpProviderT; + currentProvider: WebsocketProviderT; eth: Eth; + utils: Utils; toHex: (str: string | number) => string; isAddress: (address: string) => boolean; @@ -78,20 +91,33 @@ declare module 'web3' { transactionIndex: number } + //declare function F_CardanoGetAddress(params: (P.$Common & CARDANO.$CardanoGetAddress)): Promise; + //declare function F_CardanoGetAddress(params: (P.$Common & { bundle: Array })): Promise; + + declare type PromiseEvent = { + once: typeof F_PromiseEventOn; + on: typeof F_PromiseEventOn; + off: (type: string, callback: Function) => PromiseEvent; + then: () => (result: T) => PromiseEvent; + catch: () => (error: Error) => PromiseEvent; + } + + declare function F_PromiseEventOn(type: 'transactionHash', callback: (hash: string) => void): PromiseEvent; + declare function F_PromiseEventOn(type: 'receipt', callback: (receipt: TransactionReceipt) => void): PromiseEvent; + declare function F_PromiseEventOn(type: 'confirmation', callback: (confirmations: number, receipt: TransactionReceipt) => void): PromiseEvent; + declare function F_PromiseEventOn(type: 'error', callback: (error: Error) => void): PromiseEvent; + declare class Eth { - getGasPrice: (callback: (error: Error, gasPrice: string) => void) => void, - getBalance: (address: string, callback: (error: Error, balance: BigNumber) => void) => void, - getTransactionCount: (address: string, callback: (error: Error, result: number) => void) => void, - getTransaction: (txid: string, callback: (error: Error, result: TransactionStatus) => void) => void, - getTransactionReceipt: (txid: string, callback: (error: Error, result: TransactionReceipt) => void) => void, - getBlockNumber: (callback: (error: Error, blockNumber: number) => void) => void, - getBlock: (hash: string, callback: (error: Error, result: any) => void) => void, - // getAccounts: (callback: (error: Error, accounts: Array) => void) => void, - // sign: (payload: string, signer: EthereumAddressT) => Promise, - contract: (abi: Array) => ContractFactory, - estimateGas: (options: EstimateGasOptions, callback: (error: ?Error, gas: ?number) => void) => void, - sendRawTransaction: (tx: any, callback: (error: Error, result: string) => void) => void, - filter: (type: string) => Filter; // return intance with "watch" + getBalance: (address: string) => Promise; + getTransactionCount: (address: string) => Promise; + estimateGas: (options: EstimateGasOptions) => Promise; + getGasPrice: () => Promise; + getBlockNumber: () => Promise; + Contract: (abi: Array, options?: any) => Contract; + sendSignedTransaction: (tx: string) => PromiseEvent; + getTransaction: (txid: string) => Promise; + getTransactionReceipt: (txid: string) => Promise; + subscribe: (type: string, callback: Function) => any; } declare export class Filter { @@ -99,108 +125,40 @@ declare module 'web3' { stopWatching: (callback: any) => void, } - declare export class ContractFactory { - // constructor(abi: Array); - eth: Eth; - abi: Array; - at: (address: string, callback: ?(error: Error, contract: Contract) => void) => Contract; // TODO + declare type ContractMethod = { + call: () => Promise; } declare export class Contract { - name: { - call: (callback: (error: Error, name: string) => void) => void; - }, - symbol: { - call: (callback: (error: Error, symbol: string) => void) => void; - }, - decimals: { - call: (callback: (error: Error, decimals: BigNumber) => void) => void; - }, - balanceOf: (address: string, callback: (error: Error, balance: BigNumber) => void) => void, - transfer: any, + clone: () => Contract; + + options: { + address: string; + jsonInterface: JSON; + }; + + methods: { + name: () => ContractMethod; + symbol: () => ContractMethod; + decimals: () => ContractMethod; + balanceOf: (address: string) => ContractMethod; + transfer: (to: string, amount: any) => { + encodeABI: () => string; + } + }; } - declare export default typeof Web3T; -} - - -// -// - - -/*declare module 'web3' { - - module.exports = { - eth: { - _requestManager: any; - iban: { - (iban: string): void; - fromAddress: (address: string) => any; - fromBban: (bban: string) => any; - createIndirect: (options: any) => any; - isValid: (iban: string) => boolean; - }; - sendIBANTransaction: any; - contract: (abi: any) => { - eth: any; - abi: any[]; - new: (...args: any[]) => { - _eth: any; - transactionHash: any; - address: any; - abi: any[]; - }; - at: (address: any, callback: Function) => any; - getData: (...args: any[]) => any; - }; - filter: (fil: any, callback: any, filterCreationErrorCallback: any) => { - requestManager: any; - options: any; - implementation: { - [x: string]: any; - }; - filterId: any; - callbacks: any[]; - getLogsCallbacks: any[]; - pollFilters: any[]; - formatter: any; - watch: (callback: any) => any; - stopWatching: (callback: any) => any; - get: (callback: any) => any; - }; - namereg: () => { - eth: any; - abi: any[]; - new: (...args: any[]) => { - _eth: any; - transactionHash: any; - address: any; - abi: any[]; - }; - at: (address: any, callback: Function) => any; - getData: (...args: any[]) => any; - }; - icapNamereg: () => { - eth: any; - abi: any[]; - new: (...args: any[]) => { - _eth: any; - transactionHash: any; - address: any; - abi: any[]; - }; - at: (address: any, callback: Function) => any; - getData: (...args: any[]) => any; - }; - isSyncing: (callback: any) => { - requestManager: any; - pollId: string; - callbacks: any[]; - lastSyncState: boolean; - addCallback: (callback: any) => any; - stopWatching: () => void; - }; - } + declare class Utils { + toHex: (str: string | number) => string; + hexToNumberString: (str: string) => string; + + isAddress: (address: string) => boolean; + toWei: (number: BigNumber, unit?: EthereumUnitT) => BigNumber; + toWei: (number: string, unit?: EthereumUnitT) => string; + toDecimal: (number: BigNumber) => number; + toDecimal: (number: string) => number; + soliditySha3: (payload: string | number | BigNumber | Object) => String; } -} -*/ \ No newline at end of file + + declare export default typeof Web3T; +} \ No newline at end of file