diff options
author | Leonid <logvinov.leon@gmail.com> | 2018-01-31 03:27:14 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-01-31 03:27:14 +0800 |
commit | 2bcb7d56394c43acd68d1823092de2ad047abc6d (patch) | |
tree | 55684889214373fcd66ca52efa079f381cfb388b /packages/0x.js/src | |
parent | d004df56e38726ed7f1b775da33d8f37aae270db (diff) | |
parent | 7ad314472d27e8a6c6ee8242f4f335d414709f4d (diff) | |
download | dexon-sol-tools-2bcb7d56394c43acd68d1823092de2ad047abc6d.tar.gz dexon-sol-tools-2bcb7d56394c43acd68d1823092de2ad047abc6d.tar.zst dexon-sol-tools-2bcb7d56394c43acd68d1823092de2ad047abc6d.zip |
Merge pull request #332 from 0xProject/feature/contracts-abi-gen
Contracts refactor. Bye, truffle!!! :clap:
Diffstat (limited to 'packages/0x.js/src')
-rw-r--r-- | packages/0x.js/src/0x.ts | 18 | ||||
-rw-r--r-- | packages/0x.js/src/contract_wrappers/contract_wrapper.ts | 6 | ||||
-rw-r--r-- | packages/0x.js/src/contract_wrappers/ether_token_wrapper.ts | 5 | ||||
-rw-r--r-- | packages/0x.js/src/contract_wrappers/exchange_wrapper.ts | 10 | ||||
-rw-r--r-- | packages/0x.js/src/contract_wrappers/token_wrapper.ts | 5 | ||||
-rw-r--r-- | packages/0x.js/src/globals.d.ts | 16 | ||||
-rw-r--r-- | packages/0x.js/src/index.ts | 5 | ||||
-rw-r--r-- | packages/0x.js/src/order_watcher/order_state_watcher.ts | 11 | ||||
-rw-r--r-- | packages/0x.js/src/types.ts | 34 | ||||
-rw-r--r-- | packages/0x.js/src/utils/abi_decoder.ts | 72 | ||||
-rw-r--r-- | packages/0x.js/src/utils/utils.ts | 3 |
11 files changed, 25 insertions, 160 deletions
diff --git a/packages/0x.js/src/0x.ts b/packages/0x.js/src/0x.ts index f8a484c5d..6cfa65cc2 100644 --- a/packages/0x.js/src/0x.ts +++ b/packages/0x.js/src/0x.ts @@ -1,5 +1,6 @@ import { schemas, SchemaValidator } from '@0xproject/json-schemas'; -import { BigNumber, intervalUtils } from '@0xproject/utils'; +import { TransactionReceiptWithDecodedLogs } from '@0xproject/types'; +import { AbiDecoder, BigNumber, intervalUtils } from '@0xproject/utils'; import { Web3Wrapper } from '@0xproject/web3-wrapper'; import * as ethUtil from 'ethereumjs-util'; import * as _ from 'lodash'; @@ -12,16 +13,7 @@ import { TokenTransferProxyWrapper } from './contract_wrappers/token_transfer_pr import { TokenWrapper } from './contract_wrappers/token_wrapper'; import { OrderStateWatcher } from './order_watcher/order_state_watcher'; import { zeroExConfigSchema } from './schemas/zero_ex_config_schema'; -import { - ECSignature, - Order, - SignedOrder, - TransactionReceiptWithDecodedLogs, - Web3Provider, - ZeroExConfig, - ZeroExError, -} from './types'; -import { AbiDecoder } from './utils/abi_decoder'; +import { ECSignature, Order, SignedOrder, Web3Provider, ZeroExConfig, ZeroExError } from './types'; import { assert } from './utils/assert'; import { constants } from './utils/constants'; import { decorators } from './utils/decorators'; @@ -334,8 +326,8 @@ export class ZeroEx { ); }, ); - - return txReceiptPromise; + const txReceipt = await txReceiptPromise; + return txReceipt; } /* * HACK: `TokenWrapper` needs a token transfer proxy address. `TokenTransferProxy` address is fetched from diff --git a/packages/0x.js/src/contract_wrappers/contract_wrapper.ts b/packages/0x.js/src/contract_wrappers/contract_wrapper.ts index 27551c01d..5f11d810a 100644 --- a/packages/0x.js/src/contract_wrappers/contract_wrapper.ts +++ b/packages/0x.js/src/contract_wrappers/contract_wrapper.ts @@ -1,4 +1,5 @@ -import { intervalUtils } from '@0xproject/utils'; +import { LogWithDecodedArgs, RawLog } from '@0xproject/types'; +import { AbiDecoder, intervalUtils } from '@0xproject/utils'; import { Web3Wrapper } from '@0xproject/web3-wrapper'; import { Block, BlockAndLogStreamer } from 'ethereumjs-blockstream'; import * as _ from 'lodash'; @@ -13,11 +14,8 @@ import { EventCallback, IndexedFilterValues, InternalZeroExError, - LogWithDecodedArgs, - RawLog, ZeroExError, } from '../types'; -import { AbiDecoder } from '../utils/abi_decoder'; import { constants } from '../utils/constants'; import { filterUtils } from '../utils/filter_utils'; diff --git a/packages/0x.js/src/contract_wrappers/ether_token_wrapper.ts b/packages/0x.js/src/contract_wrappers/ether_token_wrapper.ts index b03571636..8fa7aa78b 100644 --- a/packages/0x.js/src/contract_wrappers/ether_token_wrapper.ts +++ b/packages/0x.js/src/contract_wrappers/ether_token_wrapper.ts @@ -1,5 +1,6 @@ import { schemas } from '@0xproject/json-schemas'; -import { BigNumber } from '@0xproject/utils'; +import { LogWithDecodedArgs } from '@0xproject/types'; +import { AbiDecoder, BigNumber } from '@0xproject/utils'; import { Web3Wrapper } from '@0xproject/web3-wrapper'; import * as _ from 'lodash'; @@ -10,11 +11,9 @@ import { EtherTokenEvents, EventCallback, IndexedFilterValues, - LogWithDecodedArgs, TransactionOpts, ZeroExError, } from '../types'; -import { AbiDecoder } from '../utils/abi_decoder'; import { assert } from '../utils/assert'; import { ContractWrapper } from './contract_wrapper'; diff --git a/packages/0x.js/src/contract_wrappers/exchange_wrapper.ts b/packages/0x.js/src/contract_wrappers/exchange_wrapper.ts index 2b6117729..fceab851a 100644 --- a/packages/0x.js/src/contract_wrappers/exchange_wrapper.ts +++ b/packages/0x.js/src/contract_wrappers/exchange_wrapper.ts @@ -1,5 +1,6 @@ import { schemas } from '@0xproject/json-schemas'; -import { BigNumber } from '@0xproject/utils'; +import { DecodedLogArgs, LogWithDecodedArgs } from '@0xproject/types'; +import { AbiDecoder, BigNumber } from '@0xproject/utils'; import { Web3Wrapper } from '@0xproject/web3-wrapper'; import * as _ from 'lodash'; import * as Web3 from 'web3'; @@ -8,7 +9,6 @@ import { artifacts } from '../artifacts'; import { BlockParamLiteral, BlockRange, - DecodedLogArgs, ECSignature, EventCallback, ExchangeContractErrCodes, @@ -17,7 +17,6 @@ import { ExchangeEvents, IndexedFilterValues, LogErrorContractEventArgs, - LogWithDecodedArgs, MethodOpts, Order, OrderAddresses, @@ -28,7 +27,6 @@ import { SignedOrder, ValidateOrderFillableOpts, } from '../types'; -import { AbiDecoder } from '../utils/abi_decoder'; import { assert } from '../utils/assert'; import { decorators } from '../utils/decorators'; import { ExchangeTransferSimulator } from '../utils/exchange_transfer_simulator'; @@ -846,9 +844,9 @@ export class ExchangeWrapper extends ContractWrapper { public throwLogErrorsAsErrors(logs: Array<LogWithDecodedArgs<DecodedLogArgs> | Web3.LogEntry>): void { const errLog = _.find(logs, { event: ExchangeEvents.LogError, - }) as LogWithDecodedArgs<LogErrorContractEventArgs> | undefined; + }); if (!_.isUndefined(errLog)) { - const logArgs = errLog.args; + const logArgs = (errLog as LogWithDecodedArgs<LogErrorContractEventArgs>).args; const errCode = logArgs.errorId.toNumber(); const errMessage = this._exchangeContractErrCodesToMsg[errCode]; throw new Error(errMessage); diff --git a/packages/0x.js/src/contract_wrappers/token_wrapper.ts b/packages/0x.js/src/contract_wrappers/token_wrapper.ts index 7943f4a60..4216ff462 100644 --- a/packages/0x.js/src/contract_wrappers/token_wrapper.ts +++ b/packages/0x.js/src/contract_wrappers/token_wrapper.ts @@ -1,5 +1,6 @@ import { schemas } from '@0xproject/json-schemas'; -import { BigNumber } from '@0xproject/utils'; +import { LogWithDecodedArgs } from '@0xproject/types'; +import { AbiDecoder, BigNumber } from '@0xproject/utils'; import { Web3Wrapper } from '@0xproject/web3-wrapper'; import * as _ from 'lodash'; @@ -8,14 +9,12 @@ import { BlockRange, EventCallback, IndexedFilterValues, - LogWithDecodedArgs, MethodOpts, TokenContractEventArgs, TokenEvents, TransactionOpts, ZeroExError, } from '../types'; -import { AbiDecoder } from '../utils/abi_decoder'; import { assert } from '../utils/assert'; import { constants } from '../utils/constants'; diff --git a/packages/0x.js/src/globals.d.ts b/packages/0x.js/src/globals.d.ts index 4f4932b6e..0e103d057 100644 --- a/packages/0x.js/src/globals.d.ts +++ b/packages/0x.js/src/globals.d.ts @@ -41,19 +41,3 @@ declare module 'truffle-hdwallet-provider' { } export = HDWalletProvider; } - -// abi-decoder declarations -interface DecodedLogArg {} -interface DecodedLog { - name: string; - events: DecodedLogArg[]; -} -declare module 'abi-decoder' { - import * as Web3 from 'web3'; - const addABI: (abi: Web3.AbiDefinition) => void; - const decodeLogs: (logs: Web3.LogEntry[]) => DecodedLog[]; -} - -declare module 'web3/lib/solidity/coder' { - const decodeParams: (types: string[], data: string) => any[]; -} diff --git a/packages/0x.js/src/index.ts b/packages/0x.js/src/index.ts index 599c3a2b0..41e67e177 100644 --- a/packages/0x.js/src/index.ts +++ b/packages/0x.js/src/index.ts @@ -28,12 +28,9 @@ export { WithdrawalContractEventArgs, DepositContractEventArgs, ContractEventArgs, - ContractEventArg, Web3Provider, ZeroExConfig, EtherTokenEvents, - TransactionReceiptWithDecodedLogs, - LogWithDecodedArgs, MethodOpts, OrderTransactionOpts, TransactionOpts, @@ -47,4 +44,6 @@ export { OrderState, } from './types'; +export { ContractEventArg, LogWithDecodedArgs, TransactionReceiptWithDecodedLogs } from '@0xproject/types'; + export { TransactionReceipt } from '@0xproject/types'; diff --git a/packages/0x.js/src/order_watcher/order_state_watcher.ts b/packages/0x.js/src/order_watcher/order_state_watcher.ts index 12ac60960..576be00c8 100644 --- a/packages/0x.js/src/order_watcher/order_state_watcher.ts +++ b/packages/0x.js/src/order_watcher/order_state_watcher.ts @@ -1,5 +1,6 @@ import { schemas } from '@0xproject/json-schemas'; -import { intervalUtils } from '@0xproject/utils'; +import { LogWithDecodedArgs } from '@0xproject/types'; +import { AbiDecoder, intervalUtils } from '@0xproject/utils'; import { Web3Wrapper } from '@0xproject/web3-wrapper'; import * as _ from 'lodash'; @@ -19,7 +20,6 @@ import { LogCancelContractEventArgs, LogEvent, LogFillContractEventArgs, - LogWithDecodedArgs, OnOrderStateChangeCallback, OrderState, OrderStateWatcherConfig, @@ -29,7 +29,6 @@ import { WithdrawalContractEventArgs, ZeroExError, } from '../types'; -import { AbiDecoder } from '../utils/abi_decoder'; import { assert } from '../utils/assert'; import { OrderStateUtils } from '../utils/order_state_utils'; import { utils } from '../utils/utils'; @@ -224,12 +223,12 @@ export class OrderStateWatcher { return; } const log = logIfExists as LogEvent; // At this moment we are sure that no error occured and log is defined. - const maybeDecodedLog = this._abiDecoder.tryToDecodeLogOrNoop(log); - const isLogDecoded = !_.isUndefined((maybeDecodedLog as LogWithDecodedArgs<any>).event); + const maybeDecodedLog = this._abiDecoder.tryToDecodeLogOrNoop<ContractEventArgs>(log); + const isLogDecoded = !_.isUndefined(((maybeDecodedLog as any) as LogWithDecodedArgs<ContractEventArgs>).event); if (!isLogDecoded) { return; // noop } - const decodedLog = maybeDecodedLog as LogWithDecodedArgs<ContractEventArgs>; + const decodedLog = (maybeDecodedLog as any) as LogWithDecodedArgs<ContractEventArgs>; let makerToken: string; let makerAddress: string; switch (decodedLog.event) { diff --git a/packages/0x.js/src/types.ts b/packages/0x.js/src/types.ts index 3c93910e9..a0deb91c9 100644 --- a/packages/0x.js/src/types.ts +++ b/packages/0x.js/src/types.ts @@ -1,4 +1,4 @@ -import { TransactionReceipt } from '@0xproject/types'; +import { ContractEventArg, LogWithDecodedArgs } from '@0xproject/types'; import { BigNumber } from '@0xproject/utils'; import * as Web3 from 'web3'; @@ -53,13 +53,6 @@ export interface DecodedLogEvent<ArgsType> { export type EventCallback<ArgsType> = (err: null | Error, log?: DecodedLogEvent<ArgsType>) => void; export type EventWatcherCallback = (err: null | Error, log?: LogEvent) => void; -export enum SolidityTypes { - Address = 'address', - Uint256 = 'uint256', - Uint8 = 'uint8', - Uint = 'uint', -} - export enum ExchangeContractErrCodes { ERROR_FILL_EXPIRED, // Order has already expired ERROR_FILL_NO_VALUE, // Order has already been fully filled or cancelled @@ -94,8 +87,6 @@ export enum ExchangeContractErrs { BatchOrdersMustHaveAtLeastOneItem = 'BATCH_ORDERS_MUST_HAVE_AT_LEAST_ONE_ITEM', } -export type RawLog = Web3.LogEntry; - export interface ContractEvent { logIndex: number; transactionIndex: number; @@ -163,7 +154,6 @@ export type EtherTokenContractEventArgs = | DepositContractEventArgs | WithdrawalContractEventArgs; export type ContractEventArgs = ExchangeContractEventArgs | TokenContractEventArgs | EtherTokenContractEventArgs; -export type ContractEventArg = string | BigNumber; export interface Order { maker: string; @@ -267,11 +257,6 @@ export type SyncMethod = (...args: any[]) => any; */ export type Web3Provider = Web3.Provider; -export interface JSONRPCPayload { - params: any[]; - method: string; -} - /* * orderExpirationCheckingIntervalMs: How often to check for expired orders. Default: 50 * eventPollingIntervalMs: How often to poll the Ethereum node for new events. Defaults: 200 @@ -305,23 +290,6 @@ export interface ZeroExConfig { orderWatcherConfig?: OrderStateWatcherConfig; } -export enum AbiType { - Function = 'function', - Constructor = 'constructor', - Event = 'event', - Fallback = 'fallback', -} - -export interface DecodedLogArgs { - [argName: string]: ContractEventArg; -} - -export interface LogWithDecodedArgs<ArgsType> extends Web3.DecodedLogEntry<ArgsType> {} - -export interface TransactionReceiptWithDecodedLogs extends TransactionReceipt { - logs: Array<LogWithDecodedArgs<DecodedLogArgs> | Web3.LogEntry>; -} - export type ArtifactContractName = 'ZRX' | 'TokenTransferProxy' | 'TokenRegistry' | 'Token' | 'Exchange' | 'EtherToken'; export interface Artifact { diff --git a/packages/0x.js/src/utils/abi_decoder.ts b/packages/0x.js/src/utils/abi_decoder.ts deleted file mode 100644 index bbd2a0b1d..000000000 --- a/packages/0x.js/src/utils/abi_decoder.ts +++ /dev/null @@ -1,72 +0,0 @@ -import { BigNumber } from '@0xproject/utils'; -import * as _ from 'lodash'; -import * as Web3 from 'web3'; -import * as SolidityCoder from 'web3/lib/solidity/coder'; - -import { AbiType, ContractEventArgs, DecodedLogArgs, LogWithDecodedArgs, RawLog, SolidityTypes } from '../types'; - -export class AbiDecoder { - private _savedABIs: Web3.AbiDefinition[] = []; - private _methodIds: { [signatureHash: string]: Web3.EventAbi } = {}; - private static _padZeros(address: string) { - let formatted = address; - if (_.startsWith(formatted, '0x')) { - formatted = formatted.slice(2); - } - - formatted = _.padStart(formatted, 40, '0'); - return `0x${formatted}`; - } - constructor(abiArrays: Web3.AbiDefinition[][]) { - _.map(abiArrays, this._addABI.bind(this)); - } - // This method can only decode logs from the 0x & ERC20 smart contracts - public tryToDecodeLogOrNoop<ArgsType extends ContractEventArgs>( - log: Web3.LogEntry, - ): LogWithDecodedArgs<ArgsType> | RawLog { - const methodId = log.topics[0]; - const event = this._methodIds[methodId]; - if (_.isUndefined(event)) { - return log; - } - const logData = log.data; - const decodedParams: DecodedLogArgs = {}; - let dataIndex = 0; - let topicsIndex = 1; - - const nonIndexedInputs = _.filter(event.inputs, input => !input.indexed); - const dataTypes = _.map(nonIndexedInputs, input => input.type); - const decodedData = SolidityCoder.decodeParams(dataTypes, logData.slice('0x'.length)); - - _.map(event.inputs, (param: Web3.EventParameter) => { - // Indexed parameters are stored in topics. Non-indexed ones in decodedData - let value = param.indexed ? log.topics[topicsIndex++] : decodedData[dataIndex++]; - if (param.type === SolidityTypes.Address) { - value = AbiDecoder._padZeros(new BigNumber(value).toString(16)); - } else if ( - param.type === SolidityTypes.Uint256 || - param.type === SolidityTypes.Uint8 || - param.type === SolidityTypes.Uint - ) { - value = new BigNumber(value); - } - decodedParams[param.name] = value; - }); - - return { - ...log, - event: event.name, - args: decodedParams, - }; - } - private _addABI(abiArray: Web3.AbiDefinition[]): void { - _.map(abiArray, (abi: Web3.AbiDefinition) => { - if (abi.type === AbiType.Event) { - const signature = `${abi.name}(${_.map(abi.inputs, input => input.type).join(',')})`; - const signatureHash = new Web3().sha3(signature); - this._methodIds[signatureHash] = abi; - } - }); - this._savedABIs = this._savedABIs.concat(abiArray); - } -} diff --git a/packages/0x.js/src/utils/utils.ts b/packages/0x.js/src/utils/utils.ts index 42cf5d956..74f2c5995 100644 --- a/packages/0x.js/src/utils/utils.ts +++ b/packages/0x.js/src/utils/utils.ts @@ -1,10 +1,11 @@ +import { SolidityTypes } from '@0xproject/types'; import { BigNumber } from '@0xproject/utils'; import BN = require('bn.js'); import * as ethABI from 'ethereumjs-abi'; import * as ethUtil from 'ethereumjs-util'; import * as _ from 'lodash'; -import { Order, SignedOrder, SolidityTypes } from '../types'; +import { Order, SignedOrder } from '../types'; export const utils = { /** |