diff options
author | Leonid Logvinov <logvinov.leon@gmail.com> | 2017-12-19 21:15:14 +0800 |
---|---|---|
committer | Leonid Logvinov <logvinov.leon@gmail.com> | 2017-12-20 21:01:59 +0800 |
commit | 613fada49f9d168fb949a370b884367f99deb401 (patch) | |
tree | 693354e8edb716a0a35e0a39e79267288c0b782a /packages/0x.js/src | |
parent | b603197ae837dca86d712760f9b18f626628096a (diff) | |
download | dexon-0x-contracts-613fada49f9d168fb949a370b884367f99deb401.tar.gz dexon-0x-contracts-613fada49f9d168fb949a370b884367f99deb401.tar.zst dexon-0x-contracts-613fada49f9d168fb949a370b884367f99deb401.zip |
Add etherToken.getLogsAsync and etherToken.subscribe with tests
Diffstat (limited to 'packages/0x.js/src')
-rw-r--r-- | packages/0x.js/src/0x.ts | 2 | ||||
-rw-r--r-- | packages/0x.js/src/contract_wrappers/ether_token_wrapper.ts | 74 | ||||
-rw-r--r-- | packages/0x.js/src/index.ts | 2 | ||||
-rw-r--r-- | packages/0x.js/src/types.ts | 21 |
4 files changed, 93 insertions, 6 deletions
diff --git a/packages/0x.js/src/0x.ts b/packages/0x.js/src/0x.ts index e4965f9a2..7393cc814 100644 --- a/packages/0x.js/src/0x.ts +++ b/packages/0x.js/src/0x.ts @@ -201,7 +201,7 @@ export class ZeroEx { this._web3Wrapper, config.networkId, config.tokenRegistryContractAddress, ); this.etherToken = new EtherTokenWrapper( - this._web3Wrapper, config.networkId, this.token, + this._web3Wrapper, config.networkId, this._abiDecoder, this.token, ); this.orderStateWatcher = new OrderStateWatcher( this._web3Wrapper, this._abiDecoder, this.token, this.exchange, config.orderWatcherConfig, 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 a6acbe45d..7b5b4d02a 100644 --- a/packages/0x.js/src/contract_wrappers/ether_token_wrapper.ts +++ b/packages/0x.js/src/contract_wrappers/ether_token_wrapper.ts @@ -1,9 +1,21 @@ +import {schemas} from '@0xproject/json-schemas'; import {Web3Wrapper} from '@0xproject/web3-wrapper'; import BigNumber from 'bignumber.js'; import * as _ from 'lodash'; import {artifacts} from '../artifacts'; -import {TransactionOpts, ZeroExError} from '../types'; +import { + BlockRange, + EtherTokenContractEventArgs, + EtherTokenEvents, + EventCallback, + IndexedFilterValues, + LogWithDecodedArgs, + TokenEvents, + TransactionOpts, + ZeroExError, +} from '../types'; +import {AbiDecoder} from '../utils/abi_decoder'; import {assert} from '../utils/assert'; import {ContractWrapper} from './contract_wrapper'; @@ -17,8 +29,8 @@ import {TokenWrapper} from './token_wrapper'; export class EtherTokenWrapper extends ContractWrapper { private _etherTokenContractIfExists?: EtherTokenContract; private _tokenWrapper: TokenWrapper; - constructor(web3Wrapper: Web3Wrapper, networkId: number, tokenWrapper: TokenWrapper) { - super(web3Wrapper, networkId); + constructor(web3Wrapper: Web3Wrapper, networkId: number, abiDecoder: AbiDecoder, tokenWrapper: TokenWrapper) { + super(web3Wrapper, networkId, abiDecoder); this._tokenWrapper = tokenWrapper; } /** @@ -75,7 +87,63 @@ export class EtherTokenWrapper extends ContractWrapper { }); return txHash; } + /** + * Gets historical logs without creating a subscription + * @param etherTokenAddress An address of the ether token that emmited the logs. + * @param eventName The ether token contract event you would like to subscribe to. + * @param blockRange Block range to get logs from. + * @param indexFilterValues An object where the keys are indexed args returned by the event and + * the value is the value you are interested in. E.g `{_owner: aUserAddressHex}` + * @return Array of logs that match the parameters + */ + public async getLogsAsync<ArgsType extends EtherTokenContractEventArgs>( + etherTokenAddress: string, eventName: EtherTokenEvents, blockRange: BlockRange, + indexFilterValues: IndexedFilterValues): Promise<Array<LogWithDecodedArgs<ArgsType>>> { + assert.isETHAddressHex('etherTokenAddress', etherTokenAddress); + assert.doesBelongToStringEnum('eventName', eventName, TokenEvents); + assert.doesConformToSchema('blockRange', blockRange, schemas.blockRangeSchema); + assert.doesConformToSchema('indexFilterValues', indexFilterValues, schemas.indexFilterValuesSchema); + const logs = await this._getLogsAsync<ArgsType>( + etherTokenAddress, eventName, blockRange, indexFilterValues, artifacts.TokenArtifact.abi, + ); + return logs; + } + /** + * Subscribe to an event type emitted by the Token contract. + * @param etherTokenAddress The hex encoded address where the ether token is deployed. + * @param eventName The ether token contract event you would like to subscribe to. + * @param indexFilterValues An object where the keys are indexed args returned by the event and + * the value is the value you are interested in. E.g `{_owner: aUserAddressHex}` + * @param callback Callback that gets called when a log is added/removed + * @return Subscription token used later to unsubscribe + */ + public subscribe<ArgsType extends EtherTokenContractEventArgs>( + etherTokenAddress: string, eventName: EtherTokenEvents, indexFilterValues: IndexedFilterValues, + callback: EventCallback<ArgsType>): string { + assert.isETHAddressHex('etherTokenAddress', etherTokenAddress); + assert.doesBelongToStringEnum('eventName', eventName, TokenEvents); + assert.doesConformToSchema('indexFilterValues', indexFilterValues, schemas.indexFilterValuesSchema); + assert.isFunction('callback', callback); + const subscriptionToken = this._subscribe<ArgsType>( + etherTokenAddress, eventName, indexFilterValues, artifacts.TokenArtifact.abi, callback, + ); + return subscriptionToken; + } + /** + * Cancel a subscription + * @param subscriptionToken Subscription token returned by `subscribe()` + */ + public unsubscribe(subscriptionToken: string): void { + this._unsubscribe(subscriptionToken); + } + /** + * Cancels all existing subscriptions + */ + public unsubscribeAll(): void { + super.unsubscribeAll(); + } private _invalidateContractInstance(): void { + this.unsubscribeAll(); delete this._etherTokenContractIfExists; } private async _getEtherTokenContractAsync(etherTokenAddress: string): Promise<EtherTokenContract> { diff --git a/packages/0x.js/src/index.ts b/packages/0x.js/src/index.ts index b75ca4fa3..b43e7f33f 100644 --- a/packages/0x.js/src/index.ts +++ b/packages/0x.js/src/index.ts @@ -2,6 +2,7 @@ export {ZeroEx} from './0x'; export { Order, + BlockParamLiteral, SignedOrder, ECSignature, ZeroExError, @@ -27,6 +28,7 @@ export { ContractEventArg, Web3Provider, ZeroExConfig, + EtherTokenEvents, TransactionReceiptWithDecodedLogs, LogWithDecodedArgs, MethodOpts, diff --git a/packages/0x.js/src/types.ts b/packages/0x.js/src/types.ts index 704e59ce5..e6a2c05d0 100644 --- a/packages/0x.js/src/types.ts +++ b/packages/0x.js/src/types.ts @@ -28,6 +28,7 @@ export enum ZeroExError { export enum InternalZeroExError { NoAbiDecoder = 'NO_ABI_DECODER', ZrxNotInTokenRegistry = 'ZRX_NOT_IN_TOKEN_REGISTRY', + WethNotInTokenRegistry = 'WETH_NOT_IN_TOKEN_REGISTRY', } /** @@ -146,8 +147,17 @@ export interface ApprovalContractEventArgs { _spender: string; _value: BigNumber; } +export interface DepositContractEventArgs { + _owner: string; + _value: BigNumber; +} +export interface WithdrawalContractEventArgs { + _owner: string; + _value: BigNumber; +} export type TokenContractEventArgs = TransferContractEventArgs|ApprovalContractEventArgs; -export type ContractEventArgs = ExchangeContractEventArgs|TokenContractEventArgs; +export type EtherTokenContractEventArgs = TokenContractEventArgs|DepositContractEventArgs|WithdrawalContractEventArgs; +export type ContractEventArgs = ExchangeContractEventArgs|TokenContractEventArgs|EtherTokenContractEventArgs; export type ContractEventArg = string|BigNumber; export interface Order { @@ -201,7 +211,14 @@ export enum TokenEvents { Approval = 'Approval', } -export type ContractEvents = TokenEvents|ExchangeEvents; +export enum EtherTokenEvents { + Transfer = 'Transfer', + Approval = 'Approval', + Deposit = 'Deposit', + Withdrawal = 'Withdrawal', +} + +export type ContractEvents = TokenEvents|ExchangeEvents|EtherTokenEvents; export interface IndexedFilterValues { [index: string]: ContractEventArg; |