import BN from 'bn.js' ; import { IMatchableOrder } from './IMatchableOrder' ; import { IMatchableProduct } from './IMatchableProduct' ; import { OrderSide } from './OrderSide' ; export class Order < TProduct , TProductFilter > implements IMatchableOrder < TProduct , TProductFilter > { protected _volume : BN ; public get volume ( ) { return this . _volume ; } public get isFilled ( ) { return this . volume . isZero ( ) ; } constructor ( public readonly id , public readonly side , public readonly validFrom , protected readonly matchableProduct , public readonly price , volume , public readonly userId , public readonly createdAt , public readonly assetId ? ) { if ( volume . isZero ( ) || volume . isNeg ( ) ) { throw new Error ( 'Incorrect negative volume' ) ; } if ( price <= 0 ) { throw new Error ( 'Incorrect negative price' ) ; } this . _volume = new BN ( volume ) ; } public updateWithTradedVolume ( tradedVolume ) { if ( tradedVolume . gt ( this . volume ) ) { throw new Error ( 'Order overmatched' ) ; } this . _volume = this . _volume . sub ( tradedVolume ) ; return this ; } public filterBy ( productFilter ) { return this . matchableProduct . filter ( productFilter ) ; } public matches ( order ) { return this . matchableProduct . matches ( order . product ) ; } public clone ( ) { return new Order ( this . id , this . side , this . validFrom , this . matchableProduct , this . price , this . volume , this . userId , this . createdAt , this . assetId ) ; } public get product ( ) : TProduct { return this . matchableProduct . product ; } }