diff --git a/schema.graphql b/schema.graphql index 747778d..a753aec 100644 --- a/schema.graphql +++ b/schema.graphql @@ -675,7 +675,7 @@ type PredictPrediction @entity{ slot: PredictSlot! user: User! stake: BigDecimal! - payout: PredictPayout! + payout: PredictPayout "txId" txId: String eventIndex:Int! @@ -705,7 +705,7 @@ type PredictSlot @entity{ predictions:[PredictPrediction!] @derivedFrom(field: "slot") trueValues: [PredictTrueVals!] @derivedFrom(field: "slot") revenue: BigDecimal! - status:PredictSlotStatus! + status: PredictSlotStatus! } type PredictSettingUpdate @entity{ diff --git a/src/mappings/erc20Template3.ts b/src/mappings/erc20Template3.ts index 326d79d..9dcbabc 100644 --- a/src/mappings/erc20Template3.ts +++ b/src/mappings/erc20Template3.ts @@ -18,13 +18,61 @@ import { RevenueAdded } from '../@types/templates/ERC20Template3/ERC20Template3' -import { integer } from './utils/constants' import { weiToDecimal } from './utils/generic' import { getPredictContract, getToken } from './utils/tokenUtils' import { getUser } from './utils/userUtils' +function getPredictSlot( + predictContractAddress: string, + slot: BigInt +): PredictSlot { + const id = predictContractAddress + '-' + slot.toString() + let newPredictSlot = PredictSlot.load(id) + if (newPredictSlot === null) { + newPredictSlot = new PredictSlot(id) + newPredictSlot.predictContract = predictContractAddress + newPredictSlot.slot = slot + newPredictSlot.revenue = BigDecimal.zero() + newPredictSlot.status = 'Pending' + newPredictSlot.save() + } + return newPredictSlot +} + export function handlePredictionSubmitted(event: PredictionSubmitted): void { - // TODO + const predictSlot = getPredictSlot( + event.address.toHexString(), + event.params.slot + ) + const user = getUser(event.params.predictoor.toHex()) + const id = + event.address.toHexString() + + '-' + + event.params.slot.toString() + + '-' + + user.id + const predictPrediction = new PredictPrediction(id) + predictPrediction.slot = predictSlot.id + predictPrediction.user = user.id + const predictContract = getPredictContract(event.address) + let decimals = 18 + if (predictContract.stakeToken) { + const stakeToken = getToken( + Address.fromString(predictContract.stakeToken!), + false + ) + decimals = stakeToken.decimals + } + predictPrediction.stake = weiToDecimal( + event.params.stake.toBigDecimal(), + BigInt.fromI32(decimals).toI32() + ) + predictPrediction.payout = null + predictPrediction.block = event.block.number.toI32() + predictPrediction.txId = event.transaction.hash.toHexString() + predictPrediction.eventIndex = event.logIndex.toI32() + predictPrediction.timestamp = event.block.timestamp.toI32() + predictPrediction.save() } export function handlePredictionPayout(event: PredictionPayout): void {