Skip to content

Commit

Permalink
Fix resolved price for scalar markets (#189)
Browse files Browse the repository at this point in the history
* Set price relative to the resolvedOutcome

* Adapt account balances based on price changes
  • Loading branch information
saboonikhil committed Oct 4, 2022
1 parent 9605e91 commit cacb02e
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions src/mappings/predictionMarkets/index.ts
Expand Up @@ -475,13 +475,23 @@ export async function marketResolved(ctx: EventHandlerContext<Store, {event: {ar
const numOfOutcomeAssets = market.outcomeAssets.length;
if (market.resolvedOutcome && numOfOutcomeAssets > 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>(asset)

Expand All @@ -490,23 +500,26 @@ export async function marketResolved(ctx: EventHandlerContext<Store, {event: {ar
ha.assetId = asset.assetId
ha.newPrice = asset.price
ha.newAmountInPool = asset.amountInPool
ha.dPrice = oldPrice ? asset.price - oldPrice : null
ha.dPrice = oldPrice && asset.price ? asset.price - oldPrice : null
ha.dAmountInPool = oldAssetQty && asset.amountInPool ? asset.amountInPool - oldAssetQty : null
ha.event = event.name.split('.')[1]
ha.blockNumber = block.height
ha.timestamp = new Date(block.timestamp)
console.log(`[${event.name}] Saving historical asset: ${JSON.stringify(ha, null, 2)}`)
await store.save<HistoricalAsset>(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)
let acc = await store.get(Account, { where: { id: Like(`%${keyword}%`), poolId: undefined}})
if (acc != null && ab.balance > 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<AccountBalance>(ab)
Expand Down Expand Up @@ -534,12 +547,7 @@ export async function marketResolved(ctx: EventHandlerContext<Store, {event: {ar
);
}
}

if (status.length < 2) {
market.status = 'Resolved'
} else {
market.status = status
}
market.status = status.length < 2 ? 'Resolved' : status
console.log(`[${event.name}] Saving market: ${JSON.stringify(market, null, 2)}`)
await store.save<Market>(market)

Expand Down

0 comments on commit cacb02e

Please sign in to comment.