@@ -44,55 +44,53 @@ export async function createMarketResolveLossTransactions({
4444 { } as Record < string , Record < string , Decimal > >
4545 )
4646
47- const transactions : Array < Promise < Transaction > > = [ ]
48-
49- // Transfer all losing shares back to the AMM
50- for ( const [ accountId , optionQuantity ] of Object . entries ( summedLosingQuantities ) ) {
51- const entries = [ ]
52-
53- for ( const [ optionId , quantity ] of Object . entries ( optionQuantity ) ) {
54- if ( quantity . toDecimalPlaces ( 0 ) . gt ( 0 ) ) {
55- entries . push ( {
56- fromAccountId : accountId ,
57- toAccountId : ammAccount . id ,
58- assetType : 'MARKET_OPTION' ,
59- assetId : optionId ,
60- amount : quantity ,
61- } as const )
62- }
63- }
47+ const entries = Object . entries ( summedLosingQuantities ) . flatMap ( ( [ accountId , optionQuantity ] ) =>
48+ Object . entries ( optionQuantity )
49+ . filter ( ( [ _ , quantity ] ) => quantity . toDecimalPlaces ( 0 ) . gt ( 0 ) )
50+ . map ( ( [ optionId , quantity ] ) => ( {
51+ fromAccountId : accountId ,
52+ toAccountId : ammAccount . id ,
53+ assetType : 'MARKET_OPTION' as const ,
54+ assetId : optionId ,
55+ amount : quantity ,
56+ } ) )
57+ )
6458
65- entries . length &&
66- transactions . push (
67- executeTransaction ( {
68- type : 'TRADE_LOSS' ,
69- entries,
70- marketId,
71- additionalLogic : async ( txParams ) => {
72- return Promise . all ( [
73- ...Object . entries ( optionQuantity ) . map ( ( [ optionId , quantity ] ) => {
74- return txParams . tx . marketOptionPosition . update ( {
75- where : {
76- accountId_optionId : {
77- accountId,
78- optionId,
79- } ,
80- } ,
81- data : {
82- quantity : {
83- decrement : quantity . toNumber ( ) ,
84- } ,
85- value : 0 ,
86- updatedAt : new Date ( ) ,
87- } ,
88- } )
89- } ) ,
90- updateMarketBalances ( { ...txParams , marketId } ) ,
91- ] )
92- } ,
93- } )
94- )
59+ // Short circuit if no entries
60+ if ( entries . length === 0 ) {
61+ return [ ]
9562 }
9663
97- return Promise . all ( transactions )
64+ await executeTransaction ( {
65+ type : 'TRADE_LOSS' ,
66+ entries,
67+ marketId,
68+ additionalLogic : async ( txParams ) => {
69+ return Promise . all ( [
70+ // Batch update all positions in a single transaction
71+ ...Object . entries ( summedLosingQuantities ) . flatMap ( ( [ accountId , optionQuantity ] ) =>
72+ Object . entries ( optionQuantity ) . map ( ( [ optionId , quantity ] ) =>
73+ txParams . tx . marketOptionPosition . update ( {
74+ where : {
75+ accountId_optionId : {
76+ accountId,
77+ optionId,
78+ } ,
79+ } ,
80+ data : {
81+ quantity : {
82+ decrement : quantity . toNumber ( ) ,
83+ } ,
84+ value : 0 ,
85+ updatedAt : new Date ( ) ,
86+ } ,
87+ } )
88+ )
89+ ) ,
90+ updateMarketBalances ( { ...txParams , marketId } ) ,
91+ ] )
92+ } ,
93+ } )
94+
95+ return entries
9896}
0 commit comments