{"version":3,"file":"calculateSellMarketPrice.cjs","names":["OrderType"],"sources":["../../../src/order-builder/helpers/calculateSellMarketPrice.ts"],"sourcesContent":["import { type OrderSummary, OrderType } from \"../../types/index.js\";\n\n/**\n * calculateSellMarketPrice calculates the market price to sell a shares\n * @param positions\n * @param amountToMatch sells to share\n * @returns\n */\nexport const calculateSellMarketPrice = (\n\tpositions: OrderSummary[],\n\tamountToMatch: number,\n\torderType: OrderType,\n) => {\n\tif (!positions.length) {\n\t\tthrow new Error(\"no match\");\n\t}\n\tlet sum = 0;\n\t/*\n    Bids:\n    [\n        { price: '0.4', size: '100' },\n        { price: '0.45', size: '100' },\n        { price: '0.5', size: '100' }\n    ]\n    So, if the amount to match is 300 that will be reached at the first position so price will be 0.4\n    */\n\tfor (let i = positions.length - 1; i >= 0; i--) {\n\t\tconst p = positions[i];\n\t\tsum += parseFloat(p.size);\n\t\tif (sum >= amountToMatch) {\n\t\t\treturn parseFloat(p.price);\n\t\t}\n\t}\n\tif (orderType === OrderType.FOK) {\n\t\tthrow new Error(\"no match\");\n\t}\n\treturn parseFloat(positions[0].price);\n};\n"],"mappings":";;;;;;;;;AAQA,MAAa,4BACZ,WACA,eACA,cACI;AACJ,KAAI,CAAC,UAAU,OACd,OAAM,IAAI,MAAM,WAAW;CAE5B,IAAI,MAAM;AAUV,MAAK,IAAI,IAAI,UAAU,SAAS,GAAG,KAAK,GAAG,KAAK;EAC/C,MAAM,IAAI,UAAU;AACpB,SAAO,WAAW,EAAE,KAAK;AACzB,MAAI,OAAO,cACV,QAAO,WAAW,EAAE,MAAM;;AAG5B,KAAI,cAAcA,uBAAU,IAC3B,OAAM,IAAI,MAAM,WAAW;AAE5B,QAAO,WAAW,UAAU,GAAG,MAAM"}