import Decimal from 'decimal.js' ; import { CreatedOrder , Store } from '../types' ; private param { Store } store param { Function } calculateCommission return { Function } export const createOrderCreator = ( store ) => ( calculateCommission ) => createOrder ( store ) ( calculateCommission ) ; private param { Order } order return { CreatedOrder } export const createOrder = ( store ) => ( calculateCommission ) => ( order ) : CreatedOrder => { const s = store ; const createdOrder = { } as CreatedOrder ; if ( typeof order . timestamp === 'undefined' ) { throw new Error ( 'missing order timestamp' ) ; } else { createdOrder . timestamp = order . timestamp ; } if ( typeof order . identifier === 'undefined' ) { throw new Error ( 'missing order identifier' ) ; } else { createdOrder . identifier = order . identifier ; } if ( typeof order . price === 'undefined' ) { throw new Error ( 'market orders not yet supported' ) ; } else { createdOrder . price = new Decimal ( order . price ) ; } if ( typeof order . quantity !== 'undefined' && typeof order . percent === 'undefined' ) { createdOrder . quantity = new Decimal ( order . quantity ) ; } else if ( typeof order . quantity === 'undefined' && typeof order . percent !== 'undefined' ) { throw new Error ( 'percentage orders not yet supported' ) ; } else { throw new Error ( 'please specify quantity or percent, not both' ) ; } if ( typeof order . trigger !== 'undefined' || typeof order . threshold !== 'undefined' ) { throw new Error ( 'stop loss orders not yet supported' ) ; } createdOrder . commission = new Decimal ( calculateCommission ( order ) ) ; return createdOrder ; } ;