From 8d76d74a1713dcec140c8d43f9ea7612b5cc38f1 Mon Sep 17 00:00:00 2001 From: Ara Kevonian <=> Date: Mon, 9 Apr 2018 04:49:05 -0700 Subject: Implement `zeroEx.exchange.getOrderStateAsync` --- packages/0x.js/CHANGELOG.md | 2 + .../src/contract_wrappers/exchange_wrapper.ts | 53 +++++++++++++++------- packages/0x.js/test/exchange_wrapper_test.ts | 39 +++++++++++++++- 3 files changed, 76 insertions(+), 18 deletions(-) (limited to 'packages/0x.js') diff --git a/packages/0x.js/CHANGELOG.md b/packages/0x.js/CHANGELOG.md index c18915417..63c82ac94 100644 --- a/packages/0x.js/CHANGELOG.md +++ b/packages/0x.js/CHANGELOG.md @@ -4,6 +4,8 @@ Edit the package's CHANGELOG.json file only. --> CHANGELOG +## v0.36.0 - TBD + * Add `zeroEx.exchange.getOrderStateAsync` to allow obtaining current OrderState for a signedOrder ## v0.35.0 - _April 2, 2018_ diff --git a/packages/0x.js/src/contract_wrappers/exchange_wrapper.ts b/packages/0x.js/src/contract_wrappers/exchange_wrapper.ts index 378ae8111..fb1877f4a 100644 --- a/packages/0x.js/src/contract_wrappers/exchange_wrapper.ts +++ b/packages/0x.js/src/contract_wrappers/exchange_wrapper.ts @@ -13,35 +13,38 @@ import { Web3Wrapper } from '@0xproject/web3-wrapper'; import * as _ from 'lodash'; import { artifacts } from '../artifacts'; +import { BalanceAndProxyAllowanceLazyStore } from '../stores/balance_proxy_allowance_lazy_store'; +import { OrderFilledCancelledLazyStore } from '../stores/order_filled_cancelled_lazy_store'; import { - BlockRange, - EventCallback, - ExchangeContractErrCodes, - ExchangeContractErrs, - IndexedFilterValues, - MethodOpts, - OrderAddresses, - OrderCancellationRequest, - OrderFillRequest, - OrderTransactionOpts, - OrderValues, - ValidateOrderFillableOpts, +BlockRange, +EventCallback, +ExchangeContractErrCodes, +ExchangeContractErrs, +IndexedFilterValues, +MethodOpts, +OrderAddresses, +OrderCancellationRequest, +OrderFillRequest, +OrderState, +OrderTransactionOpts, +OrderValues, +ValidateOrderFillableOpts, } from '../types'; import { assert } from '../utils/assert'; import { decorators } from '../utils/decorators'; import { ExchangeTransferSimulator } from '../utils/exchange_transfer_simulator'; +import { OrderStateUtils } from '../utils/order_state_utils'; import { OrderValidationUtils } from '../utils/order_validation_utils'; import { utils } from '../utils/utils'; import { ContractWrapper } from './contract_wrapper'; import { - ExchangeContract, - ExchangeContractEventArgs, - ExchangeEvents, - LogErrorContractEventArgs, +ExchangeContract, +ExchangeContractEventArgs, +ExchangeEvents, +LogErrorContractEventArgs, } from './generated/exchange'; import { TokenWrapper } from './token_wrapper'; - const SHOULD_VALIDATE_BY_DEFAULT = true; interface ExchangeContractErrCodesToMsgs { @@ -873,6 +876,22 @@ export class ExchangeWrapper extends ContractWrapper { throw new Error(errMessage); } } + /** + * Gets the latest OrderState of a signedOrder + * @param signedOrder The signedOrder + */ + public async getOrderStateAsync(signedOrder: SignedOrder): Promise { + const balanceAndProxyAllowanceLazyStore = new BalanceAndProxyAllowanceLazyStore( + this._tokenWrapper, + BlockParamLiteral.Latest, + ); + const orderFilledCancelledLazyStore = new OrderFilledCancelledLazyStore(this); + const orderStateUtils = new OrderStateUtils( + balanceAndProxyAllowanceLazyStore, + orderFilledCancelledLazyStore, + ); + return orderStateUtils.getOrderStateAsync(signedOrder); + } /** * Returns the ZRX token address used by the exchange contract. * @return Address of ZRX token diff --git a/packages/0x.js/test/exchange_wrapper_test.ts b/packages/0x.js/test/exchange_wrapper_test.ts index cfc390bae..b6eaa2591 100644 --- a/packages/0x.js/test/exchange_wrapper_test.ts +++ b/packages/0x.js/test/exchange_wrapper_test.ts @@ -14,7 +14,7 @@ import { LogCancelContractEventArgs, LogFillContractEventArgs, OrderCancellationRequest, - OrderFillRequest, + OrderFillRequest, OrderState, OrderStateInvalid, SignedOrder, Token, ZeroEx, @@ -1156,4 +1156,41 @@ describe('ExchangeWrapper', () => { expect(args.maker).to.be.equal(differentMakerAddress); }); }); + describe('#getOrderState', () => { + let maker: string; + let taker: string; + let makerToken: Token; + let takerToken: Token; + let signedOrder: SignedOrder; + let orderState: OrderState; + const fillableAmount = ZeroEx.toBaseUnitAmount(new BigNumber(5), constants.ZRX_DECIMALS); + before(async () => { + [, maker, taker] = userAddresses; + tokens = await zeroEx.tokenRegistry.getTokensAsync(); + [makerToken, takerToken] = tokenUtils.getDummyTokens(); + }); + it('should report orderStateValid when order is fillable', async () => { + signedOrder = await fillScenarios.createFillableSignedOrderAsync( + makerToken.address, + takerToken.address, + maker, + taker, + fillableAmount, + ); + orderState = await zeroEx.exchange.getOrderStateAsync(signedOrder); + expect(orderState.isValid).to.be.true(); + }); + it('should report orderStateInvalid when maker allowance set to 0', async () => { + signedOrder = await fillScenarios.createFillableSignedOrderAsync( + makerToken.address, + takerToken.address, + maker, + taker, + fillableAmount, + ); + await zeroEx.token.setProxyAllowanceAsync(makerToken.address, maker, new BigNumber(0)); + orderState = await zeroEx.exchange.getOrderStateAsync(signedOrder); + expect(orderState.isValid).to.be.false(); + }); + }); }); // tslint:disable:max-file-line-count -- cgit From c80b42712a9fc73c05c32cd3d3396a6a02a7969f Mon Sep 17 00:00:00 2001 From: Ara Kevonian <=> Date: Mon, 9 Apr 2018 07:05:46 -0700 Subject: Lint/Prettify --- packages/0x.js/CHANGELOG.md | 2 ++ packages/0x.js/package.json | 25 ++++++-------- .../src/contract_wrappers/exchange_wrapper.ts | 39 ++++++++++------------ packages/0x.js/test/exchange_wrapper_test.ts | 5 +-- 4 files changed, 33 insertions(+), 38 deletions(-) (limited to 'packages/0x.js') diff --git a/packages/0x.js/CHANGELOG.md b/packages/0x.js/CHANGELOG.md index 63c82ac94..76f82d93e 100644 --- a/packages/0x.js/CHANGELOG.md +++ b/packages/0x.js/CHANGELOG.md @@ -4,7 +4,9 @@ Edit the package's CHANGELOG.json file only. --> CHANGELOG + ## v0.36.0 - TBD + * Add `zeroEx.exchange.getOrderStateAsync` to allow obtaining current OrderState for a signedOrder ## v0.35.0 - _April 2, 2018_ diff --git a/packages/0x.js/package.json b/packages/0x.js/package.json index 882b40733..e62f948b4 100644 --- a/packages/0x.js/package.json +++ b/packages/0x.js/package.json @@ -2,43 +2,38 @@ "name": "0x.js", "version": "0.35.0", "description": "A javascript library for interacting with the 0x protocol", - "keywords": [ - "0x.js", - "0xproject", - "ethereum", - "tokens", - "exchange" - ], + "keywords": ["0x.js", "0xproject", "ethereum", "tokens", "exchange"], "main": "lib/src/index.js", "types": "lib/src/index.d.ts", "scripts": { "build:watch": "tsc -w", "prebuild": "run-s clean generate_contract_wrappers", "build": "run-p build:umd:prod build:commonjs; exit 0;", - "generate_contract_wrappers": "node ../abi-gen/lib/index.js --abis 'src/artifacts/@(Exchange|Token|TokenTransferProxy|EtherToken|TokenRegistry|DummyToken).json' --template ../contract_templates/contract.handlebars --partials '../contract_templates/partials/**/*.handlebars' --output src/contract_wrappers/generated --backend ethers && prettier --write 'src/contract_wrappers/generated/**.ts'", + "generate_contract_wrappers": + "node ../abi-gen/lib/index.js --abis 'src/artifacts/@(Exchange|Token|TokenTransferProxy|EtherToken|TokenRegistry|DummyToken).json' --template ../contract_templates/contract.handlebars --partials '../contract_templates/partials/**/*.handlebars' --output src/contract_wrappers/generated --backend ethers && prettier --write 'src/contract_wrappers/generated/**.ts'", "lint": "tslint --project . 'src/**/*.ts' 'test/**/*.ts'", "test:circleci": "run-s test:coverage", "test": "run-s clean test:commonjs", "test:coverage": "nyc npm run test --all && yarn coverage:report:lcov", "coverage:report:lcov": "nyc report --reporter=text-lcov > coverage/lcov.info", - "update_contracts": "for i in ${npm_package_config_artifacts}; do copyfiles -u 4 ../contracts/build/contracts/$i.json ../0x.js/src/artifacts; done;", + "update_contracts": + "for i in ${npm_package_config_artifacts}; do copyfiles -u 4 ../contracts/build/contracts/$i.json ../0x.js/src/artifacts; done;", "clean": "shx rm -rf _bundles lib test_temp scripts", "build:umd:prod": "NODE_ENV=production webpack", - "build:commonjs": "tsc && copyfiles -u 2 './src/artifacts/**/*.json' ./lib/src/artifacts && copyfiles -u 3 './lib/src/monorepo_scripts/**/*' ./scripts", + "build:commonjs": + "tsc && copyfiles -u 2 './src/artifacts/**/*.json' ./lib/src/artifacts && copyfiles -u 3 './lib/src/monorepo_scripts/**/*' ./scripts", "test:commonjs": "run-s build:commonjs run_mocha", "run_mocha": "mocha lib/test/**/*_test.js --timeout 10000 --bail --exit", "manual:postpublish": "yarn build; node ./scripts/postpublish.js", "docs:stage": "yarn build && node ./scripts/stage_docs.js", "docs:json": "typedoc --excludePrivate --excludeExternals --target ES5 --json $JSON_FILE_PATH $PROJECT_FILES", - "upload_docs_json": "aws s3 cp generated_docs/index.json $S3_URL --profile 0xproject --grants read=uri=http://acs.amazonaws.com/groups/global/AllUsers --content-type application/json" + "upload_docs_json": + "aws s3 cp generated_docs/index.json $S3_URL --profile 0xproject --grants read=uri=http://acs.amazonaws.com/groups/global/AllUsers --content-type application/json" }, "config": { "artifacts": "TokenTransferProxy Exchange TokenRegistry Token EtherToken", "postpublish": { - "assets": [ - "packages/0x.js/_bundles/index.js", - "packages/0x.js/_bundles/index.min.js" - ], + "assets": ["packages/0x.js/_bundles/index.js", "packages/0x.js/_bundles/index.min.js"], "docPublishConfigs": { "extraFileIncludes": [ "../types/src/index.ts", diff --git a/packages/0x.js/src/contract_wrappers/exchange_wrapper.ts b/packages/0x.js/src/contract_wrappers/exchange_wrapper.ts index fb1877f4a..b6949e465 100644 --- a/packages/0x.js/src/contract_wrappers/exchange_wrapper.ts +++ b/packages/0x.js/src/contract_wrappers/exchange_wrapper.ts @@ -16,19 +16,19 @@ import { artifacts } from '../artifacts'; import { BalanceAndProxyAllowanceLazyStore } from '../stores/balance_proxy_allowance_lazy_store'; import { OrderFilledCancelledLazyStore } from '../stores/order_filled_cancelled_lazy_store'; import { -BlockRange, -EventCallback, -ExchangeContractErrCodes, -ExchangeContractErrs, -IndexedFilterValues, -MethodOpts, -OrderAddresses, -OrderCancellationRequest, -OrderFillRequest, -OrderState, -OrderTransactionOpts, -OrderValues, -ValidateOrderFillableOpts, + BlockRange, + EventCallback, + ExchangeContractErrCodes, + ExchangeContractErrs, + IndexedFilterValues, + MethodOpts, + OrderAddresses, + OrderCancellationRequest, + OrderFillRequest, + OrderState, + OrderTransactionOpts, + OrderValues, + ValidateOrderFillableOpts, } from '../types'; import { assert } from '../utils/assert'; import { decorators } from '../utils/decorators'; @@ -39,10 +39,10 @@ import { utils } from '../utils/utils'; import { ContractWrapper } from './contract_wrapper'; import { -ExchangeContract, -ExchangeContractEventArgs, -ExchangeEvents, -LogErrorContractEventArgs, + ExchangeContract, + ExchangeContractEventArgs, + ExchangeEvents, + LogErrorContractEventArgs, } from './generated/exchange'; import { TokenWrapper } from './token_wrapper'; const SHOULD_VALIDATE_BY_DEFAULT = true; @@ -886,10 +886,7 @@ export class ExchangeWrapper extends ContractWrapper { BlockParamLiteral.Latest, ); const orderFilledCancelledLazyStore = new OrderFilledCancelledLazyStore(this); - const orderStateUtils = new OrderStateUtils( - balanceAndProxyAllowanceLazyStore, - orderFilledCancelledLazyStore, - ); + const orderStateUtils = new OrderStateUtils(balanceAndProxyAllowanceLazyStore, orderFilledCancelledLazyStore); return orderStateUtils.getOrderStateAsync(signedOrder); } /** diff --git a/packages/0x.js/test/exchange_wrapper_test.ts b/packages/0x.js/test/exchange_wrapper_test.ts index b6eaa2591..dd55d06a2 100644 --- a/packages/0x.js/test/exchange_wrapper_test.ts +++ b/packages/0x.js/test/exchange_wrapper_test.ts @@ -14,7 +14,8 @@ import { LogCancelContractEventArgs, LogFillContractEventArgs, OrderCancellationRequest, - OrderFillRequest, OrderState, OrderStateInvalid, + OrderFillRequest, + OrderState, SignedOrder, Token, ZeroEx, @@ -1156,7 +1157,7 @@ describe('ExchangeWrapper', () => { expect(args.maker).to.be.equal(differentMakerAddress); }); }); - describe('#getOrderState', () => { + describe('#getOrderStateAsync', () => { let maker: string; let taker: string; let makerToken: Token; -- cgit From bf0ef055fbd4f5f798ea56868ad0beddf1111e7f Mon Sep 17 00:00:00 2001 From: Ara Kevonian Date: Tue, 10 Apr 2018 03:46:43 -0700 Subject: Modify lazy stores to implement abstract fetcher classes --- packages/0x.js/CHANGELOG.json | 4 +++ packages/0x.js/CHANGELOG.md | 4 --- packages/0x.js/package.json | 25 ++++++++++-------- .../src/fetchers/balance_and_allowance_fetcher.ts | 6 +++++ .../src/fetchers/order_filled_cancelled_fetcher.ts | 6 +++++ .../stores/balance_proxy_allowance_lazy_store.ts | 3 ++- .../stores/order_filled_cancelled_lazy_store.ts | 3 ++- packages/0x.js/src/utils/order_state_utils.ts | 30 +++++++++++----------- 8 files changed, 50 insertions(+), 31 deletions(-) create mode 100644 packages/0x.js/src/fetchers/balance_and_allowance_fetcher.ts create mode 100644 packages/0x.js/src/fetchers/order_filled_cancelled_fetcher.ts (limited to 'packages/0x.js') diff --git a/packages/0x.js/CHANGELOG.json b/packages/0x.js/CHANGELOG.json index 6d5dc0dd2..91bd75f65 100644 --- a/packages/0x.js/CHANGELOG.json +++ b/packages/0x.js/CHANGELOG.json @@ -5,6 +5,10 @@ { "note": "Moved Web3.Provider to `@0xproject/types:Provider`", "pr": 501 + }, + { + "note": "Add `zeroEx.exchange.getOrderStateAsync` to allow obtaining current OrderState for a signedOrder", + "pr": 510 } ] }, diff --git a/packages/0x.js/CHANGELOG.md b/packages/0x.js/CHANGELOG.md index 76f82d93e..c18915417 100644 --- a/packages/0x.js/CHANGELOG.md +++ b/packages/0x.js/CHANGELOG.md @@ -5,10 +5,6 @@ Edit the package's CHANGELOG.json file only. CHANGELOG -## v0.36.0 - TBD - - * Add `zeroEx.exchange.getOrderStateAsync` to allow obtaining current OrderState for a signedOrder - ## v0.35.0 - _April 2, 2018_ * Removed `ZeroExError.TransactionMiningTimeout` and moved it to '@0xproject/web3_wrapper' `Web3WrapperErrors.TransactionMiningTimeout` (#485) diff --git a/packages/0x.js/package.json b/packages/0x.js/package.json index e62f948b4..882b40733 100644 --- a/packages/0x.js/package.json +++ b/packages/0x.js/package.json @@ -2,38 +2,43 @@ "name": "0x.js", "version": "0.35.0", "description": "A javascript library for interacting with the 0x protocol", - "keywords": ["0x.js", "0xproject", "ethereum", "tokens", "exchange"], + "keywords": [ + "0x.js", + "0xproject", + "ethereum", + "tokens", + "exchange" + ], "main": "lib/src/index.js", "types": "lib/src/index.d.ts", "scripts": { "build:watch": "tsc -w", "prebuild": "run-s clean generate_contract_wrappers", "build": "run-p build:umd:prod build:commonjs; exit 0;", - "generate_contract_wrappers": - "node ../abi-gen/lib/index.js --abis 'src/artifacts/@(Exchange|Token|TokenTransferProxy|EtherToken|TokenRegistry|DummyToken).json' --template ../contract_templates/contract.handlebars --partials '../contract_templates/partials/**/*.handlebars' --output src/contract_wrappers/generated --backend ethers && prettier --write 'src/contract_wrappers/generated/**.ts'", + "generate_contract_wrappers": "node ../abi-gen/lib/index.js --abis 'src/artifacts/@(Exchange|Token|TokenTransferProxy|EtherToken|TokenRegistry|DummyToken).json' --template ../contract_templates/contract.handlebars --partials '../contract_templates/partials/**/*.handlebars' --output src/contract_wrappers/generated --backend ethers && prettier --write 'src/contract_wrappers/generated/**.ts'", "lint": "tslint --project . 'src/**/*.ts' 'test/**/*.ts'", "test:circleci": "run-s test:coverage", "test": "run-s clean test:commonjs", "test:coverage": "nyc npm run test --all && yarn coverage:report:lcov", "coverage:report:lcov": "nyc report --reporter=text-lcov > coverage/lcov.info", - "update_contracts": - "for i in ${npm_package_config_artifacts}; do copyfiles -u 4 ../contracts/build/contracts/$i.json ../0x.js/src/artifacts; done;", + "update_contracts": "for i in ${npm_package_config_artifacts}; do copyfiles -u 4 ../contracts/build/contracts/$i.json ../0x.js/src/artifacts; done;", "clean": "shx rm -rf _bundles lib test_temp scripts", "build:umd:prod": "NODE_ENV=production webpack", - "build:commonjs": - "tsc && copyfiles -u 2 './src/artifacts/**/*.json' ./lib/src/artifacts && copyfiles -u 3 './lib/src/monorepo_scripts/**/*' ./scripts", + "build:commonjs": "tsc && copyfiles -u 2 './src/artifacts/**/*.json' ./lib/src/artifacts && copyfiles -u 3 './lib/src/monorepo_scripts/**/*' ./scripts", "test:commonjs": "run-s build:commonjs run_mocha", "run_mocha": "mocha lib/test/**/*_test.js --timeout 10000 --bail --exit", "manual:postpublish": "yarn build; node ./scripts/postpublish.js", "docs:stage": "yarn build && node ./scripts/stage_docs.js", "docs:json": "typedoc --excludePrivate --excludeExternals --target ES5 --json $JSON_FILE_PATH $PROJECT_FILES", - "upload_docs_json": - "aws s3 cp generated_docs/index.json $S3_URL --profile 0xproject --grants read=uri=http://acs.amazonaws.com/groups/global/AllUsers --content-type application/json" + "upload_docs_json": "aws s3 cp generated_docs/index.json $S3_URL --profile 0xproject --grants read=uri=http://acs.amazonaws.com/groups/global/AllUsers --content-type application/json" }, "config": { "artifacts": "TokenTransferProxy Exchange TokenRegistry Token EtherToken", "postpublish": { - "assets": ["packages/0x.js/_bundles/index.js", "packages/0x.js/_bundles/index.min.js"], + "assets": [ + "packages/0x.js/_bundles/index.js", + "packages/0x.js/_bundles/index.min.js" + ], "docPublishConfigs": { "extraFileIncludes": [ "../types/src/index.ts", diff --git a/packages/0x.js/src/fetchers/balance_and_allowance_fetcher.ts b/packages/0x.js/src/fetchers/balance_and_allowance_fetcher.ts new file mode 100644 index 000000000..f7b1183f1 --- /dev/null +++ b/packages/0x.js/src/fetchers/balance_and_allowance_fetcher.ts @@ -0,0 +1,6 @@ +import { BigNumber } from '@0xproject/utils'; + +export abstract class BalanceAndAllowanceFetcher { + public abstract async getBalanceAsync(tokenAddress: string, userAddress: string): Promise; + public abstract async getProxyAllowanceAsync(tokenAddress: string, userAddress: string): Promise; +} diff --git a/packages/0x.js/src/fetchers/order_filled_cancelled_fetcher.ts b/packages/0x.js/src/fetchers/order_filled_cancelled_fetcher.ts new file mode 100644 index 000000000..81ae04b9c --- /dev/null +++ b/packages/0x.js/src/fetchers/order_filled_cancelled_fetcher.ts @@ -0,0 +1,6 @@ +import { BigNumber } from '@0xproject/utils'; + +export abstract class OrderFilledCancelledFetcher { + public abstract async getFilledTakerAmountAsync(orderHash: string): Promise; + public abstract async getCancelledTakerAmountAsync(orderHash: string): Promise; +} diff --git a/packages/0x.js/src/stores/balance_proxy_allowance_lazy_store.ts b/packages/0x.js/src/stores/balance_proxy_allowance_lazy_store.ts index ede1319fe..610385356 100644 --- a/packages/0x.js/src/stores/balance_proxy_allowance_lazy_store.ts +++ b/packages/0x.js/src/stores/balance_proxy_allowance_lazy_store.ts @@ -3,11 +3,12 @@ import { BigNumber } from '@0xproject/utils'; import * as _ from 'lodash'; import { TokenWrapper } from '../contract_wrappers/token_wrapper'; +import { BalanceAndAllowanceFetcher } from '../fetchers/balance_and_allowance_fetcher'; /** * Copy on read store for balances/proxyAllowances of tokens/accounts */ -export class BalanceAndProxyAllowanceLazyStore { +export class BalanceAndProxyAllowanceLazyStore implements BalanceAndAllowanceFetcher { private _token: TokenWrapper; private _defaultBlock: BlockParamLiteral; private _balance: { diff --git a/packages/0x.js/src/stores/order_filled_cancelled_lazy_store.ts b/packages/0x.js/src/stores/order_filled_cancelled_lazy_store.ts index 0a0d93406..9ed4d3bf4 100644 --- a/packages/0x.js/src/stores/order_filled_cancelled_lazy_store.ts +++ b/packages/0x.js/src/stores/order_filled_cancelled_lazy_store.ts @@ -3,11 +3,12 @@ import { BigNumber } from '@0xproject/utils'; import * as _ from 'lodash'; import { ExchangeWrapper } from '../contract_wrappers/exchange_wrapper'; +import { OrderFilledCancelledFetcher } from '../fetchers/order_filled_cancelled_fetcher'; /** * Copy on read store for filled/cancelled taker amounts */ -export class OrderFilledCancelledLazyStore { +export class OrderFilledCancelledLazyStore implements OrderFilledCancelledFetcher { private _exchange: ExchangeWrapper; private _filledTakerAmount: { [orderHash: string]: BigNumber; diff --git a/packages/0x.js/src/utils/order_state_utils.ts b/packages/0x.js/src/utils/order_state_utils.ts index 38189443b..6dbf7e572 100644 --- a/packages/0x.js/src/utils/order_state_utils.ts +++ b/packages/0x.js/src/utils/order_state_utils.ts @@ -4,16 +4,16 @@ import * as _ from 'lodash'; import { ZeroEx } from '../0x'; import { ExchangeWrapper } from '../contract_wrappers/exchange_wrapper'; +import { BalanceAndAllowanceFetcher } from '../fetchers/balance_and_allowance_fetcher'; +import { OrderFilledCancelledFetcher } from '../fetchers/order_filled_cancelled_fetcher'; import { RemainingFillableCalculator } from '../order_watcher/remaining_fillable_calculator'; -import { BalanceAndProxyAllowanceLazyStore } from '../stores/balance_proxy_allowance_lazy_store'; -import { OrderFilledCancelledLazyStore } from '../stores/order_filled_cancelled_lazy_store'; import { ExchangeContractErrs, OrderRelevantState, OrderState, OrderStateInvalid, OrderStateValid } from '../types'; const ACCEPTABLE_RELATIVE_ROUNDING_ERROR = 0.0001; export class OrderStateUtils { - private _balanceAndProxyAllowanceLazyStore: BalanceAndProxyAllowanceLazyStore; - private _orderFilledCancelledLazyStore: OrderFilledCancelledLazyStore; + private _balanceAndAllowanceFetcher: BalanceAndAllowanceFetcher; + private _orderFilledCancelledFetcher: OrderFilledCancelledFetcher; private static _validateIfOrderIsValid(signedOrder: SignedOrder, orderRelevantState: OrderRelevantState): void { const unavailableTakerTokenAmount = orderRelevantState.cancelledTakerTokenAmount.add( orderRelevantState.filledTakerTokenAmount, @@ -49,11 +49,11 @@ export class OrderStateUtils { } } constructor( - balanceAndProxyAllowanceLazyStore: BalanceAndProxyAllowanceLazyStore, - orderFilledCancelledLazyStore: OrderFilledCancelledLazyStore, + balanceAndProxyAllowanceFetcher: BalanceAndAllowanceFetcher, + orderFilledCancelledFetcher: OrderFilledCancelledFetcher, ) { - this._balanceAndProxyAllowanceLazyStore = balanceAndProxyAllowanceLazyStore; - this._orderFilledCancelledLazyStore = orderFilledCancelledLazyStore; + this._balanceAndAllowanceFetcher = balanceAndProxyAllowanceFetcher; + this._orderFilledCancelledFetcher = orderFilledCancelledFetcher; } public async getOrderStateAsync(signedOrder: SignedOrder): Promise { const orderRelevantState = await this.getOrderRelevantStateAsync(signedOrder); @@ -80,27 +80,27 @@ export class OrderStateUtils { // If we pass it from the instantiator - there is no opportunity to get it there // because JS doesn't support async constructors. // Moreover - it's cached under the hood so it's equivalent to an async constructor. - const exchange = (this._orderFilledCancelledLazyStore as any)._exchange as ExchangeWrapper; + const exchange = (this._orderFilledCancelledFetcher as any)._exchange as ExchangeWrapper; const zrxTokenAddress = exchange.getZRXTokenAddress(); const orderHash = ZeroEx.getOrderHashHex(signedOrder); - const makerBalance = await this._balanceAndProxyAllowanceLazyStore.getBalanceAsync( + const makerBalance = await this._balanceAndAllowanceFetcher.getBalanceAsync( signedOrder.makerTokenAddress, signedOrder.maker, ); - const makerProxyAllowance = await this._balanceAndProxyAllowanceLazyStore.getProxyAllowanceAsync( + const makerProxyAllowance = await this._balanceAndAllowanceFetcher.getProxyAllowanceAsync( signedOrder.makerTokenAddress, signedOrder.maker, ); - const makerFeeBalance = await this._balanceAndProxyAllowanceLazyStore.getBalanceAsync( + const makerFeeBalance = await this._balanceAndAllowanceFetcher.getBalanceAsync( zrxTokenAddress, signedOrder.maker, ); - const makerFeeProxyAllowance = await this._balanceAndProxyAllowanceLazyStore.getProxyAllowanceAsync( + const makerFeeProxyAllowance = await this._balanceAndAllowanceFetcher.getProxyAllowanceAsync( zrxTokenAddress, signedOrder.maker, ); - const filledTakerTokenAmount = await this._orderFilledCancelledLazyStore.getFilledTakerAmountAsync(orderHash); - const cancelledTakerTokenAmount = await this._orderFilledCancelledLazyStore.getCancelledTakerAmountAsync( + const filledTakerTokenAmount = await this._orderFilledCancelledFetcher.getFilledTakerAmountAsync(orderHash); + const cancelledTakerTokenAmount = await this._orderFilledCancelledFetcher.getCancelledTakerAmountAsync( orderHash, ); const unavailableTakerTokenAmount = await exchange.getUnavailableTakerAmountAsync(orderHash); -- cgit From 38f7c4a0d2d9269b5fd185b303ede7a44ab78c99 Mon Sep 17 00:00:00 2001 From: Ara Kevonian Date: Fri, 13 Apr 2018 03:04:51 -0700 Subject: Implement Simple Fetchers --- .../src/abstract/balance_and_allowance_fetcher.ts | 6 +++++ .../src/abstract/order_filled_cancelled_fetcher.ts | 6 +++++ .../src/contract_wrappers/exchange_wrapper.ts | 8 +++---- .../src/fetchers/balance_and_allowance_fetcher.ts | 6 ----- .../src/fetchers/order_filled_cancelled_fetcher.ts | 6 ----- .../simple_balance_and_allowance_fetcher.ts | 26 ++++++++++++++++++++++ .../simple_order_filled_cancelled_fetcher.ts | 24 ++++++++++++++++++++ .../stores/balance_proxy_allowance_lazy_store.ts | 2 +- .../stores/order_filled_cancelled_lazy_store.ts | 2 +- packages/0x.js/src/utils/order_state_utils.ts | 4 ++-- 10 files changed, 70 insertions(+), 20 deletions(-) create mode 100644 packages/0x.js/src/abstract/balance_and_allowance_fetcher.ts create mode 100644 packages/0x.js/src/abstract/order_filled_cancelled_fetcher.ts delete mode 100644 packages/0x.js/src/fetchers/balance_and_allowance_fetcher.ts delete mode 100644 packages/0x.js/src/fetchers/order_filled_cancelled_fetcher.ts create mode 100644 packages/0x.js/src/fetchers/simple_balance_and_allowance_fetcher.ts create mode 100644 packages/0x.js/src/fetchers/simple_order_filled_cancelled_fetcher.ts (limited to 'packages/0x.js') diff --git a/packages/0x.js/src/abstract/balance_and_allowance_fetcher.ts b/packages/0x.js/src/abstract/balance_and_allowance_fetcher.ts new file mode 100644 index 000000000..f7b1183f1 --- /dev/null +++ b/packages/0x.js/src/abstract/balance_and_allowance_fetcher.ts @@ -0,0 +1,6 @@ +import { BigNumber } from '@0xproject/utils'; + +export abstract class BalanceAndAllowanceFetcher { + public abstract async getBalanceAsync(tokenAddress: string, userAddress: string): Promise; + public abstract async getProxyAllowanceAsync(tokenAddress: string, userAddress: string): Promise; +} diff --git a/packages/0x.js/src/abstract/order_filled_cancelled_fetcher.ts b/packages/0x.js/src/abstract/order_filled_cancelled_fetcher.ts new file mode 100644 index 000000000..81ae04b9c --- /dev/null +++ b/packages/0x.js/src/abstract/order_filled_cancelled_fetcher.ts @@ -0,0 +1,6 @@ +import { BigNumber } from '@0xproject/utils'; + +export abstract class OrderFilledCancelledFetcher { + public abstract async getFilledTakerAmountAsync(orderHash: string): Promise; + public abstract async getCancelledTakerAmountAsync(orderHash: string): Promise; +} diff --git a/packages/0x.js/src/contract_wrappers/exchange_wrapper.ts b/packages/0x.js/src/contract_wrappers/exchange_wrapper.ts index b6949e465..65b5a2e53 100644 --- a/packages/0x.js/src/contract_wrappers/exchange_wrapper.ts +++ b/packages/0x.js/src/contract_wrappers/exchange_wrapper.ts @@ -13,8 +13,8 @@ import { Web3Wrapper } from '@0xproject/web3-wrapper'; import * as _ from 'lodash'; import { artifacts } from '../artifacts'; -import { BalanceAndProxyAllowanceLazyStore } from '../stores/balance_proxy_allowance_lazy_store'; -import { OrderFilledCancelledLazyStore } from '../stores/order_filled_cancelled_lazy_store'; +import { SimpleBalanceAndAllowanceFetcher } from '../fetchers/simple_balance_and_allowance_fetcher'; +import { SimpleOrderFilledCancelledFetcher } from '../fetchers/simple_order_filled_cancelled_fetcher'; import { BlockRange, EventCallback, @@ -881,11 +881,11 @@ export class ExchangeWrapper extends ContractWrapper { * @param signedOrder The signedOrder */ public async getOrderStateAsync(signedOrder: SignedOrder): Promise { - const balanceAndProxyAllowanceLazyStore = new BalanceAndProxyAllowanceLazyStore( + const balanceAndProxyAllowanceLazyStore = new SimpleBalanceAndAllowanceFetcher( this._tokenWrapper, BlockParamLiteral.Latest, ); - const orderFilledCancelledLazyStore = new OrderFilledCancelledLazyStore(this); + const orderFilledCancelledLazyStore = new SimpleOrderFilledCancelledFetcher(this); const orderStateUtils = new OrderStateUtils(balanceAndProxyAllowanceLazyStore, orderFilledCancelledLazyStore); return orderStateUtils.getOrderStateAsync(signedOrder); } diff --git a/packages/0x.js/src/fetchers/balance_and_allowance_fetcher.ts b/packages/0x.js/src/fetchers/balance_and_allowance_fetcher.ts deleted file mode 100644 index f7b1183f1..000000000 --- a/packages/0x.js/src/fetchers/balance_and_allowance_fetcher.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { BigNumber } from '@0xproject/utils'; - -export abstract class BalanceAndAllowanceFetcher { - public abstract async getBalanceAsync(tokenAddress: string, userAddress: string): Promise; - public abstract async getProxyAllowanceAsync(tokenAddress: string, userAddress: string): Promise; -} diff --git a/packages/0x.js/src/fetchers/order_filled_cancelled_fetcher.ts b/packages/0x.js/src/fetchers/order_filled_cancelled_fetcher.ts deleted file mode 100644 index 81ae04b9c..000000000 --- a/packages/0x.js/src/fetchers/order_filled_cancelled_fetcher.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { BigNumber } from '@0xproject/utils'; - -export abstract class OrderFilledCancelledFetcher { - public abstract async getFilledTakerAmountAsync(orderHash: string): Promise; - public abstract async getCancelledTakerAmountAsync(orderHash: string): Promise; -} diff --git a/packages/0x.js/src/fetchers/simple_balance_and_allowance_fetcher.ts b/packages/0x.js/src/fetchers/simple_balance_and_allowance_fetcher.ts new file mode 100644 index 000000000..2aa1cc2b7 --- /dev/null +++ b/packages/0x.js/src/fetchers/simple_balance_and_allowance_fetcher.ts @@ -0,0 +1,26 @@ +import { BlockParamLiteral } from '@0xproject/types'; +import { BigNumber } from '@0xproject/utils'; + +import {BalanceAndAllowanceFetcher} from '../abstract/balance_and_allowance_fetcher'; +import {TokenWrapper} from '../contract_wrappers/token_wrapper'; + +export class SimpleBalanceAndAllowanceFetcher implements BalanceAndAllowanceFetcher { + private _token: TokenWrapper; + private _defaultBlock: BlockParamLiteral; + constructor(token: TokenWrapper, defaultBlock: BlockParamLiteral) { + this._token = token; + this._defaultBlock = defaultBlock; + } + public async getBalanceAsync(tokenAddress: string, userAddress: string): Promise { + const methodOpts = { + defaultBlock: this._defaultBlock, + }; + return this._token.getBalanceAsync(tokenAddress, userAddress, methodOpts); + } + public async getProxyAllowanceAsync(tokenAddress: string, userAddress: string): Promise { + const methodOpts = { + defaultBlock: this._defaultBlock, + }; + return this._token.getProxyAllowanceAsync(tokenAddress, userAddress, methodOpts); + } +} diff --git a/packages/0x.js/src/fetchers/simple_order_filled_cancelled_fetcher.ts b/packages/0x.js/src/fetchers/simple_order_filled_cancelled_fetcher.ts new file mode 100644 index 000000000..e24f02726 --- /dev/null +++ b/packages/0x.js/src/fetchers/simple_order_filled_cancelled_fetcher.ts @@ -0,0 +1,24 @@ +import { BlockParamLiteral } from '@0xproject/types'; +import { BigNumber } from '@0xproject/utils'; + +import {OrderFilledCancelledFetcher} from '../abstract/order_filled_cancelled_fetcher'; +import {ExchangeWrapper} from '../contract_wrappers/exchange_wrapper'; + +export class SimpleOrderFilledCancelledFetcher implements OrderFilledCancelledFetcher { + private _exchange: ExchangeWrapper; + constructor(exchange: ExchangeWrapper) { + this._exchange = exchange; + } + public async getFilledTakerAmountAsync(orderHash: string): Promise { + const methodOpts = { + defaultBlock: BlockParamLiteral.Pending, + }; + return this._exchange.getFilledTakerAmountAsync(orderHash, methodOpts); + } + public async getCancelledTakerAmountAsync(orderHash: string): Promise { + const methodOpts = { + defaultBlock: BlockParamLiteral.Pending, + }; + return this._exchange.getCancelledTakerAmountAsync(orderHash, methodOpts); + } +} diff --git a/packages/0x.js/src/stores/balance_proxy_allowance_lazy_store.ts b/packages/0x.js/src/stores/balance_proxy_allowance_lazy_store.ts index 610385356..9ca622a69 100644 --- a/packages/0x.js/src/stores/balance_proxy_allowance_lazy_store.ts +++ b/packages/0x.js/src/stores/balance_proxy_allowance_lazy_store.ts @@ -3,7 +3,7 @@ import { BigNumber } from '@0xproject/utils'; import * as _ from 'lodash'; import { TokenWrapper } from '../contract_wrappers/token_wrapper'; -import { BalanceAndAllowanceFetcher } from '../fetchers/balance_and_allowance_fetcher'; +import { BalanceAndAllowanceFetcher } from '../abstract/balance_and_allowance_fetcher'; /** * Copy on read store for balances/proxyAllowances of tokens/accounts diff --git a/packages/0x.js/src/stores/order_filled_cancelled_lazy_store.ts b/packages/0x.js/src/stores/order_filled_cancelled_lazy_store.ts index 9ed4d3bf4..134508057 100644 --- a/packages/0x.js/src/stores/order_filled_cancelled_lazy_store.ts +++ b/packages/0x.js/src/stores/order_filled_cancelled_lazy_store.ts @@ -3,7 +3,7 @@ import { BigNumber } from '@0xproject/utils'; import * as _ from 'lodash'; import { ExchangeWrapper } from '../contract_wrappers/exchange_wrapper'; -import { OrderFilledCancelledFetcher } from '../fetchers/order_filled_cancelled_fetcher'; +import { OrderFilledCancelledFetcher } from '../abstract/order_filled_cancelled_fetcher'; /** * Copy on read store for filled/cancelled taker amounts diff --git a/packages/0x.js/src/utils/order_state_utils.ts b/packages/0x.js/src/utils/order_state_utils.ts index 6dbf7e572..d45b67dc2 100644 --- a/packages/0x.js/src/utils/order_state_utils.ts +++ b/packages/0x.js/src/utils/order_state_utils.ts @@ -4,8 +4,8 @@ import * as _ from 'lodash'; import { ZeroEx } from '../0x'; import { ExchangeWrapper } from '../contract_wrappers/exchange_wrapper'; -import { BalanceAndAllowanceFetcher } from '../fetchers/balance_and_allowance_fetcher'; -import { OrderFilledCancelledFetcher } from '../fetchers/order_filled_cancelled_fetcher'; +import { BalanceAndAllowanceFetcher } from '../abstract/balance_and_allowance_fetcher'; +import { OrderFilledCancelledFetcher } from '../abstract/order_filled_cancelled_fetcher'; import { RemainingFillableCalculator } from '../order_watcher/remaining_fillable_calculator'; import { ExchangeContractErrs, OrderRelevantState, OrderState, OrderStateInvalid, OrderStateValid } from '../types'; -- cgit From 5355da6cad157bb67d1b55a2f8463efe3e072385 Mon Sep 17 00:00:00 2001 From: Ara Kevonian Date: Fri, 13 Apr 2018 03:17:09 -0700 Subject: Rename BalanceAndAllowanceFetchers to BalanceAndProxyAllowanceFetchers --- .../src/abstract/balance_and_allowance_fetcher.ts | 6 ----- .../balance_and_proxy_allowance_fetcher.ts | 6 +++++ .../src/contract_wrappers/exchange_wrapper.ts | 8 +++---- .../simple_balance_and_allowance_fetcher.ts | 26 ---------------------- .../simple_balance_and_proxy_allowance_fetcher.ts | 26 ++++++++++++++++++++++ .../stores/balance_proxy_allowance_lazy_store.ts | 4 ++-- packages/0x.js/src/utils/order_state_utils.ts | 16 ++++++------- 7 files changed, 46 insertions(+), 46 deletions(-) delete mode 100644 packages/0x.js/src/abstract/balance_and_allowance_fetcher.ts create mode 100644 packages/0x.js/src/abstract/balance_and_proxy_allowance_fetcher.ts delete mode 100644 packages/0x.js/src/fetchers/simple_balance_and_allowance_fetcher.ts create mode 100644 packages/0x.js/src/fetchers/simple_balance_and_proxy_allowance_fetcher.ts (limited to 'packages/0x.js') diff --git a/packages/0x.js/src/abstract/balance_and_allowance_fetcher.ts b/packages/0x.js/src/abstract/balance_and_allowance_fetcher.ts deleted file mode 100644 index f7b1183f1..000000000 --- a/packages/0x.js/src/abstract/balance_and_allowance_fetcher.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { BigNumber } from '@0xproject/utils'; - -export abstract class BalanceAndAllowanceFetcher { - public abstract async getBalanceAsync(tokenAddress: string, userAddress: string): Promise; - public abstract async getProxyAllowanceAsync(tokenAddress: string, userAddress: string): Promise; -} diff --git a/packages/0x.js/src/abstract/balance_and_proxy_allowance_fetcher.ts b/packages/0x.js/src/abstract/balance_and_proxy_allowance_fetcher.ts new file mode 100644 index 000000000..c357f8447 --- /dev/null +++ b/packages/0x.js/src/abstract/balance_and_proxy_allowance_fetcher.ts @@ -0,0 +1,6 @@ +import { BigNumber } from '@0xproject/utils'; + +export abstract class BalanceAndProxyAllowanceFetcher { + public abstract async getBalanceAsync(tokenAddress: string, userAddress: string): Promise; + public abstract async getProxyAllowanceAsync(tokenAddress: string, userAddress: string): Promise; +} diff --git a/packages/0x.js/src/contract_wrappers/exchange_wrapper.ts b/packages/0x.js/src/contract_wrappers/exchange_wrapper.ts index 65b5a2e53..5dea70769 100644 --- a/packages/0x.js/src/contract_wrappers/exchange_wrapper.ts +++ b/packages/0x.js/src/contract_wrappers/exchange_wrapper.ts @@ -13,7 +13,7 @@ import { Web3Wrapper } from '@0xproject/web3-wrapper'; import * as _ from 'lodash'; import { artifacts } from '../artifacts'; -import { SimpleBalanceAndAllowanceFetcher } from '../fetchers/simple_balance_and_allowance_fetcher'; +import { SimpleBalanceAndProxyAllowanceFetcher } from '../fetchers/simple_balance_and_proxy_allowance_fetcher'; import { SimpleOrderFilledCancelledFetcher } from '../fetchers/simple_order_filled_cancelled_fetcher'; import { BlockRange, @@ -881,12 +881,12 @@ export class ExchangeWrapper extends ContractWrapper { * @param signedOrder The signedOrder */ public async getOrderStateAsync(signedOrder: SignedOrder): Promise { - const balanceAndProxyAllowanceLazyStore = new SimpleBalanceAndAllowanceFetcher( + const simpleBalanceAndProxyAllowanceFetcher = new SimpleBalanceAndProxyAllowanceFetcher( this._tokenWrapper, BlockParamLiteral.Latest, ); - const orderFilledCancelledLazyStore = new SimpleOrderFilledCancelledFetcher(this); - const orderStateUtils = new OrderStateUtils(balanceAndProxyAllowanceLazyStore, orderFilledCancelledLazyStore); + const simpleOrderFilledCancelledFetcher = new SimpleOrderFilledCancelledFetcher(this); + const orderStateUtils = new OrderStateUtils(simpleBalanceAndProxyAllowanceFetcher, simpleOrderFilledCancelledFetcher); return orderStateUtils.getOrderStateAsync(signedOrder); } /** diff --git a/packages/0x.js/src/fetchers/simple_balance_and_allowance_fetcher.ts b/packages/0x.js/src/fetchers/simple_balance_and_allowance_fetcher.ts deleted file mode 100644 index 2aa1cc2b7..000000000 --- a/packages/0x.js/src/fetchers/simple_balance_and_allowance_fetcher.ts +++ /dev/null @@ -1,26 +0,0 @@ -import { BlockParamLiteral } from '@0xproject/types'; -import { BigNumber } from '@0xproject/utils'; - -import {BalanceAndAllowanceFetcher} from '../abstract/balance_and_allowance_fetcher'; -import {TokenWrapper} from '../contract_wrappers/token_wrapper'; - -export class SimpleBalanceAndAllowanceFetcher implements BalanceAndAllowanceFetcher { - private _token: TokenWrapper; - private _defaultBlock: BlockParamLiteral; - constructor(token: TokenWrapper, defaultBlock: BlockParamLiteral) { - this._token = token; - this._defaultBlock = defaultBlock; - } - public async getBalanceAsync(tokenAddress: string, userAddress: string): Promise { - const methodOpts = { - defaultBlock: this._defaultBlock, - }; - return this._token.getBalanceAsync(tokenAddress, userAddress, methodOpts); - } - public async getProxyAllowanceAsync(tokenAddress: string, userAddress: string): Promise { - const methodOpts = { - defaultBlock: this._defaultBlock, - }; - return this._token.getProxyAllowanceAsync(tokenAddress, userAddress, methodOpts); - } -} diff --git a/packages/0x.js/src/fetchers/simple_balance_and_proxy_allowance_fetcher.ts b/packages/0x.js/src/fetchers/simple_balance_and_proxy_allowance_fetcher.ts new file mode 100644 index 000000000..877f07a22 --- /dev/null +++ b/packages/0x.js/src/fetchers/simple_balance_and_proxy_allowance_fetcher.ts @@ -0,0 +1,26 @@ +import { BlockParamLiteral } from '@0xproject/types'; +import { BigNumber } from '@0xproject/utils'; + +import {BalanceAndProxyAllowanceFetcher} from '../abstract/balance_and_proxy_allowance_fetcher'; +import {TokenWrapper} from '../contract_wrappers/token_wrapper'; + +export class SimpleBalanceAndProxyAllowanceFetcher implements BalanceAndProxyAllowanceFetcher { + private _token: TokenWrapper; + private _defaultBlock: BlockParamLiteral; + constructor(token: TokenWrapper, defaultBlock: BlockParamLiteral) { + this._token = token; + this._defaultBlock = defaultBlock; + } + public async getBalanceAsync(tokenAddress: string, userAddress: string): Promise { + const methodOpts = { + defaultBlock: this._defaultBlock, + }; + return this._token.getBalanceAsync(tokenAddress, userAddress, methodOpts); + } + public async getProxyAllowanceAsync(tokenAddress: string, userAddress: string): Promise { + const methodOpts = { + defaultBlock: this._defaultBlock, + }; + return this._token.getProxyAllowanceAsync(tokenAddress, userAddress, methodOpts); + } +} diff --git a/packages/0x.js/src/stores/balance_proxy_allowance_lazy_store.ts b/packages/0x.js/src/stores/balance_proxy_allowance_lazy_store.ts index 9ca622a69..97739f969 100644 --- a/packages/0x.js/src/stores/balance_proxy_allowance_lazy_store.ts +++ b/packages/0x.js/src/stores/balance_proxy_allowance_lazy_store.ts @@ -3,12 +3,12 @@ import { BigNumber } from '@0xproject/utils'; import * as _ from 'lodash'; import { TokenWrapper } from '../contract_wrappers/token_wrapper'; -import { BalanceAndAllowanceFetcher } from '../abstract/balance_and_allowance_fetcher'; +import { BalanceAndProxyAllowanceFetcher } from '../abstract/balance_and_proxy_allowance_fetcher'; /** * Copy on read store for balances/proxyAllowances of tokens/accounts */ -export class BalanceAndProxyAllowanceLazyStore implements BalanceAndAllowanceFetcher { +export class BalanceAndProxyAllowanceLazyStore implements BalanceAndProxyAllowanceFetcher { private _token: TokenWrapper; private _defaultBlock: BlockParamLiteral; private _balance: { diff --git a/packages/0x.js/src/utils/order_state_utils.ts b/packages/0x.js/src/utils/order_state_utils.ts index d45b67dc2..d8d69185f 100644 --- a/packages/0x.js/src/utils/order_state_utils.ts +++ b/packages/0x.js/src/utils/order_state_utils.ts @@ -4,7 +4,7 @@ import * as _ from 'lodash'; import { ZeroEx } from '../0x'; import { ExchangeWrapper } from '../contract_wrappers/exchange_wrapper'; -import { BalanceAndAllowanceFetcher } from '../abstract/balance_and_allowance_fetcher'; +import { BalanceAndProxyAllowanceFetcher } from '../abstract/balance_and_proxy_allowance_fetcher'; import { OrderFilledCancelledFetcher } from '../abstract/order_filled_cancelled_fetcher'; import { RemainingFillableCalculator } from '../order_watcher/remaining_fillable_calculator'; import { ExchangeContractErrs, OrderRelevantState, OrderState, OrderStateInvalid, OrderStateValid } from '../types'; @@ -12,7 +12,7 @@ import { ExchangeContractErrs, OrderRelevantState, OrderState, OrderStateInvalid const ACCEPTABLE_RELATIVE_ROUNDING_ERROR = 0.0001; export class OrderStateUtils { - private _balanceAndAllowanceFetcher: BalanceAndAllowanceFetcher; + private _balanceAndProxyAllowanceFetcher: BalanceAndProxyAllowanceFetcher; private _orderFilledCancelledFetcher: OrderFilledCancelledFetcher; private static _validateIfOrderIsValid(signedOrder: SignedOrder, orderRelevantState: OrderRelevantState): void { const unavailableTakerTokenAmount = orderRelevantState.cancelledTakerTokenAmount.add( @@ -49,10 +49,10 @@ export class OrderStateUtils { } } constructor( - balanceAndProxyAllowanceFetcher: BalanceAndAllowanceFetcher, + balanceAndProxyAllowanceFetcher: BalanceAndProxyAllowanceFetcher, orderFilledCancelledFetcher: OrderFilledCancelledFetcher, ) { - this._balanceAndAllowanceFetcher = balanceAndProxyAllowanceFetcher; + this._balanceAndProxyAllowanceFetcher = balanceAndProxyAllowanceFetcher; this._orderFilledCancelledFetcher = orderFilledCancelledFetcher; } public async getOrderStateAsync(signedOrder: SignedOrder): Promise { @@ -83,19 +83,19 @@ export class OrderStateUtils { const exchange = (this._orderFilledCancelledFetcher as any)._exchange as ExchangeWrapper; const zrxTokenAddress = exchange.getZRXTokenAddress(); const orderHash = ZeroEx.getOrderHashHex(signedOrder); - const makerBalance = await this._balanceAndAllowanceFetcher.getBalanceAsync( + const makerBalance = await this._balanceAndProxyAllowanceFetcher.getBalanceAsync( signedOrder.makerTokenAddress, signedOrder.maker, ); - const makerProxyAllowance = await this._balanceAndAllowanceFetcher.getProxyAllowanceAsync( + const makerProxyAllowance = await this._balanceAndProxyAllowanceFetcher.getProxyAllowanceAsync( signedOrder.makerTokenAddress, signedOrder.maker, ); - const makerFeeBalance = await this._balanceAndAllowanceFetcher.getBalanceAsync( + const makerFeeBalance = await this._balanceAndProxyAllowanceFetcher.getBalanceAsync( zrxTokenAddress, signedOrder.maker, ); - const makerFeeProxyAllowance = await this._balanceAndAllowanceFetcher.getProxyAllowanceAsync( + const makerFeeProxyAllowance = await this._balanceAndProxyAllowanceFetcher.getProxyAllowanceAsync( zrxTokenAddress, signedOrder.maker, ); -- cgit From 7d957538b46bbc9cd757d5b9a0351bf27e4e5419 Mon Sep 17 00:00:00 2001 From: Ara Kevonian Date: Tue, 17 Apr 2018 06:47:36 -0700 Subject: Add stateLayer param to getOrderState and clean up variable names --- .../0x.js/src/contract_wrappers/exchange_wrapper.ts | 11 +++++++---- .../simple_balance_and_proxy_allowance_fetcher.ts | 10 ++++++---- .../fetchers/simple_order_filled_cancelled_fetcher.ts | 18 +++++++++++------- .../0x.js/src/order_watcher/order_state_watcher.ts | 6 +++--- .../src/stores/balance_proxy_allowance_lazy_store.ts | 8 ++++---- .../src/stores/order_filled_cancelled_lazy_store.ts | 12 +++++++----- packages/0x.js/src/utils/order_state_utils.ts | 2 +- 7 files changed, 39 insertions(+), 28 deletions(-) (limited to 'packages/0x.js') diff --git a/packages/0x.js/src/contract_wrappers/exchange_wrapper.ts b/packages/0x.js/src/contract_wrappers/exchange_wrapper.ts index 5dea70769..eacd3ebf0 100644 --- a/packages/0x.js/src/contract_wrappers/exchange_wrapper.ts +++ b/packages/0x.js/src/contract_wrappers/exchange_wrapper.ts @@ -879,15 +879,18 @@ export class ExchangeWrapper extends ContractWrapper { /** * Gets the latest OrderState of a signedOrder * @param signedOrder The signedOrder + * @param stateLayer Optional, desired blockchain state layer (defaults to latest). + * @return OrderState of the signedOrder */ - public async getOrderStateAsync(signedOrder: SignedOrder): Promise { + public async getOrderStateAsync(signedOrder: SignedOrder, stateLayer: BlockParamLiteral = BlockParamLiteral.Latest): Promise { const simpleBalanceAndProxyAllowanceFetcher = new SimpleBalanceAndProxyAllowanceFetcher( this._tokenWrapper, - BlockParamLiteral.Latest, + stateLayer, ); - const simpleOrderFilledCancelledFetcher = new SimpleOrderFilledCancelledFetcher(this); + const simpleOrderFilledCancelledFetcher = new SimpleOrderFilledCancelledFetcher(this, stateLayer); const orderStateUtils = new OrderStateUtils(simpleBalanceAndProxyAllowanceFetcher, simpleOrderFilledCancelledFetcher); - return orderStateUtils.getOrderStateAsync(signedOrder); + const orderState = orderStateUtils.getOrderStateAsync(signedOrder); + return orderState; } /** * Returns the ZRX token address used by the exchange contract. diff --git a/packages/0x.js/src/fetchers/simple_balance_and_proxy_allowance_fetcher.ts b/packages/0x.js/src/fetchers/simple_balance_and_proxy_allowance_fetcher.ts index 877f07a22..c00dba5cf 100644 --- a/packages/0x.js/src/fetchers/simple_balance_and_proxy_allowance_fetcher.ts +++ b/packages/0x.js/src/fetchers/simple_balance_and_proxy_allowance_fetcher.ts @@ -5,22 +5,24 @@ import {BalanceAndProxyAllowanceFetcher} from '../abstract/balance_and_proxy_all import {TokenWrapper} from '../contract_wrappers/token_wrapper'; export class SimpleBalanceAndProxyAllowanceFetcher implements BalanceAndProxyAllowanceFetcher { - private _token: TokenWrapper; + private _tokenWrapper: TokenWrapper; private _defaultBlock: BlockParamLiteral; constructor(token: TokenWrapper, defaultBlock: BlockParamLiteral) { - this._token = token; + this._tokenWrapper = token; this._defaultBlock = defaultBlock; } public async getBalanceAsync(tokenAddress: string, userAddress: string): Promise { const methodOpts = { defaultBlock: this._defaultBlock, }; - return this._token.getBalanceAsync(tokenAddress, userAddress, methodOpts); + const balance = this._tokenWrapper.getBalanceAsync(tokenAddress, userAddress, methodOpts); + return balance; } public async getProxyAllowanceAsync(tokenAddress: string, userAddress: string): Promise { const methodOpts = { defaultBlock: this._defaultBlock, }; - return this._token.getProxyAllowanceAsync(tokenAddress, userAddress, methodOpts); + const proxyAllowance = this._tokenWrapper.getProxyAllowanceAsync(tokenAddress, userAddress, methodOpts); + return proxyAllowance; } } diff --git a/packages/0x.js/src/fetchers/simple_order_filled_cancelled_fetcher.ts b/packages/0x.js/src/fetchers/simple_order_filled_cancelled_fetcher.ts index e24f02726..1c8aec1a1 100644 --- a/packages/0x.js/src/fetchers/simple_order_filled_cancelled_fetcher.ts +++ b/packages/0x.js/src/fetchers/simple_order_filled_cancelled_fetcher.ts @@ -5,20 +5,24 @@ import {OrderFilledCancelledFetcher} from '../abstract/order_filled_cancelled_fe import {ExchangeWrapper} from '../contract_wrappers/exchange_wrapper'; export class SimpleOrderFilledCancelledFetcher implements OrderFilledCancelledFetcher { - private _exchange: ExchangeWrapper; - constructor(exchange: ExchangeWrapper) { - this._exchange = exchange; + private _exchangeWrapper: ExchangeWrapper; + private _defaultBlock: BlockParamLiteral; + constructor(exchange: ExchangeWrapper, defaultBlock: BlockParamLiteral) { + this._exchangeWrapper = exchange; + this._defaultBlock = defaultBlock; } public async getFilledTakerAmountAsync(orderHash: string): Promise { const methodOpts = { - defaultBlock: BlockParamLiteral.Pending, + defaultBlock: this._defaultBlock, }; - return this._exchange.getFilledTakerAmountAsync(orderHash, methodOpts); + const filledTakerAmount = this._exchangeWrapper.getFilledTakerAmountAsync(orderHash, methodOpts); + return filledTakerAmount; } public async getCancelledTakerAmountAsync(orderHash: string): Promise { const methodOpts = { - defaultBlock: BlockParamLiteral.Pending, + defaultBlock: this._defaultBlock, }; - return this._exchange.getCancelledTakerAmountAsync(orderHash, methodOpts); + const cancelledTakerAmount = this._exchangeWrapper.getCancelledTakerAmountAsync(orderHash, methodOpts); + return cancelledTakerAmount; } } 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 0aa0c33bd..a9df8ac9d 100644 --- a/packages/0x.js/src/order_watcher/order_state_watcher.ts +++ b/packages/0x.js/src/order_watcher/order_state_watcher.ts @@ -87,7 +87,7 @@ export class OrderStateWatcher { _.isUndefined(config) || _.isUndefined(config.stateLayer) ? BlockParamLiteral.Latest : config.stateLayer; this._eventWatcher = new EventWatcher(web3Wrapper, pollingIntervalIfExistsMs, stateLayer); this._balanceAndProxyAllowanceLazyStore = new BalanceAndProxyAllowanceLazyStore(token, stateLayer); - this._orderFilledCancelledLazyStore = new OrderFilledCancelledLazyStore(exchange); + this._orderFilledCancelledLazyStore = new OrderFilledCancelledLazyStore(exchange, stateLayer); this._orderStateUtils = new OrderStateUtils( this._balanceAndProxyAllowanceLazyStore, this._orderFilledCancelledLazyStore, @@ -131,7 +131,7 @@ export class OrderStateWatcher { } delete this._orderByOrderHash[orderHash]; delete this._orderStateByOrderHashCache[orderHash]; - const exchange = (this._orderFilledCancelledLazyStore as any)._exchange as ExchangeWrapper; + const exchange = (this._orderFilledCancelledLazyStore as any)._exchangeWrapper as ExchangeWrapper; const zrxTokenAddress = exchange.getZRXTokenAddress(); this._removeFromDependentOrderHashes(signedOrder.maker, zrxTokenAddress, orderHash); @@ -374,7 +374,7 @@ export class OrderStateWatcher { } } private _getZRXTokenAddress(): string { - const exchange = (this._orderFilledCancelledLazyStore as any)._exchange as ExchangeWrapper; + const exchange = (this._orderFilledCancelledLazyStore as any)._exchangeWrapper as ExchangeWrapper; const zrxTokenAddress = exchange.getZRXTokenAddress(); return zrxTokenAddress; } diff --git a/packages/0x.js/src/stores/balance_proxy_allowance_lazy_store.ts b/packages/0x.js/src/stores/balance_proxy_allowance_lazy_store.ts index 97739f969..c8395415e 100644 --- a/packages/0x.js/src/stores/balance_proxy_allowance_lazy_store.ts +++ b/packages/0x.js/src/stores/balance_proxy_allowance_lazy_store.ts @@ -9,7 +9,7 @@ import { BalanceAndProxyAllowanceFetcher } from '../abstract/balance_and_proxy_a * Copy on read store for balances/proxyAllowances of tokens/accounts */ export class BalanceAndProxyAllowanceLazyStore implements BalanceAndProxyAllowanceFetcher { - private _token: TokenWrapper; + private _tokenWrapper: TokenWrapper; private _defaultBlock: BlockParamLiteral; private _balance: { [tokenAddress: string]: { @@ -22,7 +22,7 @@ export class BalanceAndProxyAllowanceLazyStore implements BalanceAndProxyAllowan }; }; constructor(token: TokenWrapper, defaultBlock: BlockParamLiteral) { - this._token = token; + this._tokenWrapper = token; this._defaultBlock = defaultBlock; this._balance = {}; this._proxyAllowance = {}; @@ -32,7 +32,7 @@ export class BalanceAndProxyAllowanceLazyStore implements BalanceAndProxyAllowan const methodOpts = { defaultBlock: this._defaultBlock, }; - const balance = await this._token.getBalanceAsync(tokenAddress, userAddress, methodOpts); + const balance = await this._tokenWrapper.getBalanceAsync(tokenAddress, userAddress, methodOpts); this.setBalance(tokenAddress, userAddress, balance); } const cachedBalance = this._balance[tokenAddress][userAddress]; @@ -60,7 +60,7 @@ export class BalanceAndProxyAllowanceLazyStore implements BalanceAndProxyAllowan const methodOpts = { defaultBlock: this._defaultBlock, }; - const proxyAllowance = await this._token.getProxyAllowanceAsync(tokenAddress, userAddress, methodOpts); + const proxyAllowance = await this._tokenWrapper.getProxyAllowanceAsync(tokenAddress, userAddress, methodOpts); this.setProxyAllowance(tokenAddress, userAddress, proxyAllowance); } const cachedProxyAllowance = this._proxyAllowance[tokenAddress][userAddress]; diff --git a/packages/0x.js/src/stores/order_filled_cancelled_lazy_store.ts b/packages/0x.js/src/stores/order_filled_cancelled_lazy_store.ts index 134508057..eebc2b52c 100644 --- a/packages/0x.js/src/stores/order_filled_cancelled_lazy_store.ts +++ b/packages/0x.js/src/stores/order_filled_cancelled_lazy_store.ts @@ -9,15 +9,17 @@ import { OrderFilledCancelledFetcher } from '../abstract/order_filled_cancelled_ * Copy on read store for filled/cancelled taker amounts */ export class OrderFilledCancelledLazyStore implements OrderFilledCancelledFetcher { - private _exchange: ExchangeWrapper; + private _exchangeWrapper: ExchangeWrapper; + private _defaultBlock: BlockParamLiteral; private _filledTakerAmount: { [orderHash: string]: BigNumber; }; private _cancelledTakerAmount: { [orderHash: string]: BigNumber; }; - constructor(exchange: ExchangeWrapper) { - this._exchange = exchange; + constructor(exchange: ExchangeWrapper, defaultBlock: BlockParamLiteral) { + this._exchangeWrapper = exchange; + this._defaultBlock = defaultBlock; this._filledTakerAmount = {}; this._cancelledTakerAmount = {}; } @@ -26,7 +28,7 @@ export class OrderFilledCancelledLazyStore implements OrderFilledCancelledFetche const methodOpts = { defaultBlock: BlockParamLiteral.Pending, }; - const filledTakerAmount = await this._exchange.getFilledTakerAmountAsync(orderHash, methodOpts); + const filledTakerAmount = await this._exchangeWrapper.getFilledTakerAmountAsync(orderHash, methodOpts); this.setFilledTakerAmount(orderHash, filledTakerAmount); } const cachedFilled = this._filledTakerAmount[orderHash]; @@ -43,7 +45,7 @@ export class OrderFilledCancelledLazyStore implements OrderFilledCancelledFetche const methodOpts = { defaultBlock: BlockParamLiteral.Pending, }; - const cancelledTakerAmount = await this._exchange.getCancelledTakerAmountAsync(orderHash, methodOpts); + const cancelledTakerAmount = await this._exchangeWrapper.getCancelledTakerAmountAsync(orderHash, methodOpts); this.setCancelledTakerAmount(orderHash, cancelledTakerAmount); } const cachedCancelled = this._cancelledTakerAmount[orderHash]; diff --git a/packages/0x.js/src/utils/order_state_utils.ts b/packages/0x.js/src/utils/order_state_utils.ts index d8d69185f..e15ff6bc9 100644 --- a/packages/0x.js/src/utils/order_state_utils.ts +++ b/packages/0x.js/src/utils/order_state_utils.ts @@ -80,7 +80,7 @@ export class OrderStateUtils { // If we pass it from the instantiator - there is no opportunity to get it there // because JS doesn't support async constructors. // Moreover - it's cached under the hood so it's equivalent to an async constructor. - const exchange = (this._orderFilledCancelledFetcher as any)._exchange as ExchangeWrapper; + const exchange = (this._orderFilledCancelledFetcher as any)._exchangeWrapper as ExchangeWrapper; const zrxTokenAddress = exchange.getZRXTokenAddress(); const orderHash = ZeroEx.getOrderHashHex(signedOrder); const makerBalance = await this._balanceAndProxyAllowanceFetcher.getBalanceAsync( -- cgit From 8fc1b38ff20cf1b528070ecc824b4696d93a2e0f Mon Sep 17 00:00:00 2001 From: Ara Kevonian Date: Tue, 17 Apr 2018 06:51:26 -0700 Subject: Use this.defaultBlock in OrderFilledCancelledLazyStore --- packages/0x.js/src/stores/order_filled_cancelled_lazy_store.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'packages/0x.js') diff --git a/packages/0x.js/src/stores/order_filled_cancelled_lazy_store.ts b/packages/0x.js/src/stores/order_filled_cancelled_lazy_store.ts index eebc2b52c..7f3da6e1d 100644 --- a/packages/0x.js/src/stores/order_filled_cancelled_lazy_store.ts +++ b/packages/0x.js/src/stores/order_filled_cancelled_lazy_store.ts @@ -26,7 +26,7 @@ export class OrderFilledCancelledLazyStore implements OrderFilledCancelledFetche public async getFilledTakerAmountAsync(orderHash: string): Promise { if (_.isUndefined(this._filledTakerAmount[orderHash])) { const methodOpts = { - defaultBlock: BlockParamLiteral.Pending, + defaultBlock: this._defaultBlock, }; const filledTakerAmount = await this._exchangeWrapper.getFilledTakerAmountAsync(orderHash, methodOpts); this.setFilledTakerAmount(orderHash, filledTakerAmount); @@ -43,7 +43,7 @@ export class OrderFilledCancelledLazyStore implements OrderFilledCancelledFetche public async getCancelledTakerAmountAsync(orderHash: string): Promise { if (_.isUndefined(this._cancelledTakerAmount[orderHash])) { const methodOpts = { - defaultBlock: BlockParamLiteral.Pending, + defaultBlock: this._defaultBlock, }; const cancelledTakerAmount = await this._exchangeWrapper.getCancelledTakerAmountAsync(orderHash, methodOpts); this.setCancelledTakerAmount(orderHash, cancelledTakerAmount); -- cgit