diff options
author | Fabio Berger <me@fabioberger.com> | 2018-06-08 00:00:13 +0800 |
---|---|---|
committer | Fabio Berger <me@fabioberger.com> | 2018-06-08 00:00:13 +0800 |
commit | ab5e021bda5cc8e39d8595580c09c3540a09aff5 (patch) | |
tree | e40c3be709d10e1a2463cb0807419c7b276f1548 /packages/contracts/src/utils/order_info_utils.ts | |
parent | 0fc981400442e4c567ca363bdf0f4c03ba87473d (diff) | |
download | dexon-sol-tools-ab5e021bda5cc8e39d8595580c09c3540a09aff5.tar dexon-sol-tools-ab5e021bda5cc8e39d8595580c09c3540a09aff5.tar.gz dexon-sol-tools-ab5e021bda5cc8e39d8595580c09c3540a09aff5.tar.bz2 dexon-sol-tools-ab5e021bda5cc8e39d8595580c09c3540a09aff5.tar.lz dexon-sol-tools-ab5e021bda5cc8e39d8595580c09c3540a09aff5.tar.xz dexon-sol-tools-ab5e021bda5cc8e39d8595580c09c3540a09aff5.tar.zst dexon-sol-tools-ab5e021bda5cc8e39d8595580c09c3540a09aff5.zip |
POC: Generates an order from spec, get's the amount fillable
Diffstat (limited to 'packages/contracts/src/utils/order_info_utils.ts')
-rw-r--r-- | packages/contracts/src/utils/order_info_utils.ts | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/packages/contracts/src/utils/order_info_utils.ts b/packages/contracts/src/utils/order_info_utils.ts new file mode 100644 index 000000000..9df627da3 --- /dev/null +++ b/packages/contracts/src/utils/order_info_utils.ts @@ -0,0 +1,44 @@ +import { assetProxyUtils, OrderStateUtils } from '@0xproject/order-utils'; +import { SignedOrder } from '@0xproject/types'; +import { BigNumber } from '@0xproject/utils'; + +import { ExchangeContract } from '../contract_wrappers/generated/exchange'; + +import { constants } from './constants'; +import { ERC20Wrapper } from './erc20_wrapper'; +import { SimpleERC20BalanceAndProxyAllowanceFetcher } from './simple_erc20_balance_and_allowance_fetcher'; +import { SimpleOrderFilledCancelledFetcher } from './simple_filled_cancelled_fetcher'; + +export class OrderInfoUtils { + private _orderStateUtils: OrderStateUtils; + private _erc20Wrapper: ERC20Wrapper; + constructor(exchangeContract: ExchangeContract, erc20Wrapper: ERC20Wrapper, zrxAddress: string) { + this._erc20Wrapper = erc20Wrapper; + const simpleOrderFilledCancelledFetcher = new SimpleOrderFilledCancelledFetcher(exchangeContract, zrxAddress); + const simpleERC20BalanceAndProxyAllowanceFetcher = new SimpleERC20BalanceAndProxyAllowanceFetcher(erc20Wrapper); + this._orderStateUtils = new OrderStateUtils( + simpleERC20BalanceAndProxyAllowanceFetcher, + simpleOrderFilledCancelledFetcher, + ); + } + public async getFillableTakerAssetAmountAsync(signedOrder: SignedOrder, takerAddress: string): Promise<BigNumber> { + const orderRelevantState = await this._orderStateUtils.getOrderRelevantStateAsync(signedOrder); + console.log('orderRelevantState', orderRelevantState); + if (takerAddress === constants.NULL_ADDRESS) { + return orderRelevantState.remainingFillableTakerAssetAmount; + } + const takerAssetData = assetProxyUtils.decodeERC20ProxyData(signedOrder.takerAssetData); + const takerBalance = await this._erc20Wrapper.getBalanceAsync(takerAddress, takerAssetData.tokenAddress); + const takerAllowance = await this._erc20Wrapper.getProxyAllowanceAsync( + takerAddress, + takerAssetData.tokenAddress, + ); + // TODO: We also need to make sure taker has sufficient ZRX for fees... + const fillableTakerAssetAmount = BigNumber.min([ + takerBalance, + takerAllowance, + orderRelevantState.remainingFillableTakerAssetAmount, + ]); + return fillableTakerAssetAmount; + } +} |