Skip to content

Commit ec213fe

Browse files
authored
Merge pull request #136 from casesandberg/fix/market-re-resolution-crashing
Fix market resolve loss deadlock bug
2 parents 931a8cf + 47dada1 commit ec213fe

File tree

3 files changed

+48
-59
lines changed

3 files changed

+48
-59
lines changed

packages/database/scripts/rerun-market-resolution.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { createMarketResolveWinTransactions } from '@play-money/markets/lib/crea
44
import { getMarket } from '@play-money/markets/lib/getMarket'
55
import db from '../prisma'
66

7-
const marketId = 'cm0amvq8v001910v7iq2mvf4m'
7+
const marketId = 'cm5od3mim075xt20i1ummyi2u'
88

99
async function main() {
1010
try {
@@ -32,7 +32,7 @@ async function main() {
3232
const liquidityTransactions = await createMarketExcessLiquidityTransactions({ marketId, initiatorId: '' })
3333

3434
console.log(
35-
`Market ${marketId} resolution re-run. ${lossTransactions.flat().length} losses, ${winTransactions.length} wins. ${liquidityTransactions.length} liquidity accounts returned.`
35+
`Market ${marketId} resolution re-run. ${lossTransactions.length} losses, ${winTransactions.length} wins. ${liquidityTransactions.length} liquidity accounts returned.`
3636
)
3737
return
3838
}

packages/markets/lib/createMarketResolveLossTransactions.test.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,6 @@ describe('createMarketResolveLossTransactions', () => {
8585
assetId: 'option-2',
8686
amount: new Decimal(60),
8787
},
88-
],
89-
})
90-
)
91-
92-
expect(executeTransaction).toHaveBeenCalledWith(
93-
expect.objectContaining({
94-
type: 'TRADE_LOSS',
95-
marketId: 'market-1',
96-
entries: [
9788
{
9889
fromAccountId: 'user-2',
9990
toAccountId: 'amm-account-id',

packages/markets/lib/createMarketResolveLossTransactions.ts

Lines changed: 46 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)