From cacb02e0e4ce5947a4a03edf83c94af2f3080626 Mon Sep 17 00:00:00 2001 From: Nikhil Saboo <36529278+saboonikhil@users.noreply.github.com> Date: Wed, 5 Oct 2022 02:02:30 +0530 Subject: [PATCH] Fix resolved price for scalar markets (#189) * Set price relative to the resolvedOutcome * Adapt account balances based on price changes --- src/mappings/predictionMarkets/index.ts | 34 +++++++++++++++---------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/src/mappings/predictionMarkets/index.ts b/src/mappings/predictionMarkets/index.ts index 50ec0110..8621080f 100644 --- a/src/mappings/predictionMarkets/index.ts +++ b/src/mappings/predictionMarkets/index.ts @@ -475,13 +475,23 @@ export async function marketResolved(ctx: EventHandlerContext 0) { for (let i = 0; i < numOfOutcomeAssets; i++) { - const assetId = market.outcomeAssets[i]! - let asset = await store.get(Asset, { where: { assetId: assetId } }) + let asset = await store.get(Asset, { where: { assetId: market.outcomeAssets[i]! } }) if (!asset) return const oldPrice = asset.price const oldAssetQty = asset.amountInPool - asset.price = (i == +market.resolvedOutcome) ? 1 : 0 - asset.amountInPool = (i == +market.resolvedOutcome) ? oldAssetQty : BigInt(0) + + if (market.marketType.scalar && asset.assetId.indexOf('Long') > -1) { + const upperRange = Number(market.marketType.scalar!.split(',')[1]) + const lowerRange = Number(market.marketType.scalar!.split(',')[0]) + asset.price = (+market.resolvedOutcome - lowerRange)/(upperRange - lowerRange) + } else if (market.marketType.scalar && asset.assetId.indexOf('Short') > -1) { + const upperRange = Number(market.marketType.scalar!.split(',')[1]) + const lowerRange = Number(market.marketType.scalar!.split(',')[0]) + asset.price = (upperRange - +market.resolvedOutcome)/(upperRange - lowerRange) + } else { + asset.price = (i == +market.resolvedOutcome) ? 1 : 0 + asset.amountInPool = (i == +market.resolvedOutcome) ? oldAssetQty : BigInt(0) + } console.log(`[${event.name}] Saving asset: ${JSON.stringify(asset, null, 2)}`) await store.save(asset) @@ -490,7 +500,7 @@ export async function marketResolved(ctx: EventHandlerContext(ha) - const abs = await store.find(AccountBalance, { where: { assetId: assetId } }) + const abs = await store.find(AccountBalance, { where: { assetId: market.outcomeAssets[i]! } }) await Promise.all( abs.map(async ab => { const keyword = ab.id.substring(ab.id.lastIndexOf('-')+1, ab.id.length) @@ -506,7 +516,10 @@ export async function marketResolved(ctx: EventHandlerContext BigInt(0)) { const oldBalance = ab.balance const oldValue = ab.value - ab.balance = (i == +market.resolvedOutcome!) ? ab.balance : BigInt(0) + + if (market.marketType.categorical) { + ab.balance = (i == +market.resolvedOutcome!) ? ab.balance : BigInt(0) + } ab.value = asset!.price ? Number(ab.balance) * asset!.price : null console.log(`[${event.name}] Saving account balance: ${JSON.stringify(ab, null, 2)}`) await store.save(ab) @@ -534,12 +547,7 @@ export async function marketResolved(ctx: EventHandlerContext(market)