Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/database/scripts/rerun-market-resolution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { createMarketResolveWinTransactions } from '@play-money/markets/lib/crea
import { getMarket } from '@play-money/markets/lib/getMarket'
import db from '../prisma'

const marketId = 'cm0amvq8v001910v7iq2mvf4m'
const marketId = 'cm5od3mim075xt20i1ummyi2u'

async function main() {
try {
Expand Down Expand Up @@ -32,7 +32,7 @@ async function main() {
const liquidityTransactions = await createMarketExcessLiquidityTransactions({ marketId, initiatorId: '' })

console.log(
`Market ${marketId} resolution re-run. ${lossTransactions.flat().length} losses, ${winTransactions.length} wins. ${liquidityTransactions.length} liquidity accounts returned.`
`Market ${marketId} resolution re-run. ${lossTransactions.length} losses, ${winTransactions.length} wins. ${liquidityTransactions.length} liquidity accounts returned.`
)
return
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,6 @@ describe('createMarketResolveLossTransactions', () => {
assetId: 'option-2',
amount: new Decimal(60),
},
],
})
)

expect(executeTransaction).toHaveBeenCalledWith(
expect.objectContaining({
type: 'TRADE_LOSS',
marketId: 'market-1',
entries: [
{
fromAccountId: 'user-2',
toAccountId: 'amm-account-id',
Expand Down
94 changes: 46 additions & 48 deletions packages/markets/lib/createMarketResolveLossTransactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,55 +44,53 @@ export async function createMarketResolveLossTransactions({
{} as Record<string, Record<string, Decimal>>
)

const transactions: Array<Promise<Transaction>> = []

// Transfer all losing shares back to the AMM
for (const [accountId, optionQuantity] of Object.entries(summedLosingQuantities)) {
const entries = []

for (const [optionId, quantity] of Object.entries(optionQuantity)) {
if (quantity.toDecimalPlaces(0).gt(0)) {
entries.push({
fromAccountId: accountId,
toAccountId: ammAccount.id,
assetType: 'MARKET_OPTION',
assetId: optionId,
amount: quantity,
} as const)
}
}
const entries = Object.entries(summedLosingQuantities).flatMap(([accountId, optionQuantity]) =>
Object.entries(optionQuantity)
.filter(([_, quantity]) => quantity.toDecimalPlaces(0).gt(0))
.map(([optionId, quantity]) => ({
fromAccountId: accountId,
toAccountId: ammAccount.id,
assetType: 'MARKET_OPTION' as const,
assetId: optionId,
amount: quantity,
}))
)

entries.length &&
transactions.push(
executeTransaction({
type: 'TRADE_LOSS',
entries,
marketId,
additionalLogic: async (txParams) => {
return Promise.all([
...Object.entries(optionQuantity).map(([optionId, quantity]) => {
return txParams.tx.marketOptionPosition.update({
where: {
accountId_optionId: {
accountId,
optionId,
},
},
data: {
quantity: {
decrement: quantity.toNumber(),
},
value: 0,
updatedAt: new Date(),
},
})
}),
updateMarketBalances({ ...txParams, marketId }),
])
},
})
)
// Short circuit if no entries
if (entries.length === 0) {
return []
}

return Promise.all(transactions)
await executeTransaction({
type: 'TRADE_LOSS',
entries,
marketId,
additionalLogic: async (txParams) => {
return Promise.all([
// Batch update all positions in a single transaction
...Object.entries(summedLosingQuantities).flatMap(([accountId, optionQuantity]) =>
Object.entries(optionQuantity).map(([optionId, quantity]) =>
txParams.tx.marketOptionPosition.update({
where: {
accountId_optionId: {
accountId,
optionId,
},
},
data: {
quantity: {
decrement: quantity.toNumber(),
},
value: 0,
updatedAt: new Date(),
},
})
)
),
updateMarketBalances({ ...txParams, marketId }),
])
},
})

return entries
}