Skip to content

Commit

Permalink
Add start & end timestamps to period (#211)
Browse files Browse the repository at this point in the history
* Update `MarketPeriod` schema

* Reform db migration file based on schema changes

* Regenerate code

* Map market period start and end

* Compute blockCreationPeriod
  • Loading branch information
saboonikhil committed Oct 28, 2022
1 parent 4a97c85 commit 68e1fc1
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 32 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions schema.graphql
Expand Up @@ -146,8 +146,6 @@ type Market @entity {
marketType: MarketType!
"Time period expressed in block numbers or timestamps"
period: MarketPeriod!
"Timestamp at which market should end"
end: BigInt!
"Scoring rule used for the market"
scoringRule: String!
"Status of the market"
Expand Down Expand Up @@ -285,9 +283,13 @@ Time period of the market
"""
type MarketPeriod @jsonField {
"start & end block numbers"
block: String
block: [BigInt]
"Timestamp at which market should end"
end: BigInt!
"Timestamp at which market should start"
start: BigInt!
"start & end timestamps"
timestamp: String
timestamp: [BigInt]
}

"""
Expand Down
25 changes: 16 additions & 9 deletions src/mappings/predictionMarkets/index.ts
Expand Up @@ -255,17 +255,24 @@ export async function marketCreated(ctx: EventHandlerContext<Store, {event: {arg
let period = new MarketPeriod()
const p = market.period
if (p.__kind == 'Block') {
period.block = p.start.toString() + ',' + p.end.toString()

const sdk = await Tools.getSDK()
const now = +(await sdk.api.query.timestamp.now()).toString()
const head = await sdk.api.rpc.chain.getHeader()
const blockNum = head.number.toNumber()
const diffInMs = +(sdk.api.consts.timestamp.minimumPeriod).toString() * (Number(p.end) - blockNum)
newMarket.end = BigInt(now + diffInMs)
const now = BigInt((await sdk.api.query.timestamp.now()).toString())
const headBlock = (await sdk.api.rpc.chain.getHeader()).number.toBigInt()
const blockCreationPeriod = BigInt(2 * Number(sdk.api.consts.timestamp.minimumPeriod))
const startDiffInMs = blockCreationPeriod * (BigInt(p.start) - headBlock)
const endDiffInMs = blockCreationPeriod * (BigInt(p.end) - headBlock)

period.block = []
period.block.push(BigInt(p.start))
period.block.push(BigInt(p.end))
period.start = now + startDiffInMs
period.end = now + endDiffInMs
} else if (p.__kind == 'Timestamp') {
period.timestamp = p.start.toString() + ',' + p.end.toString()
newMarket.end = BigInt(p.end)
period.timestamp = []
period.timestamp.push(BigInt(p.start))
period.timestamp.push(BigInt(p.end))
period.start = BigInt(p.start)
period.end = BigInt(p.end)
}
newMarket.period = period

Expand Down
50 changes: 40 additions & 10 deletions src/model/generated/_marketPeriod.ts
Expand Up @@ -5,43 +5,73 @@ import * as marshal from "./marshal"
* Time period of the market
*/
export class MarketPeriod {
private _block!: string | undefined | null
private _timestamp!: string | undefined | null
private _block!: (bigint | undefined | null)[] | undefined | null
private _end!: bigint
private _start!: bigint
private _timestamp!: (bigint | undefined | null)[] | undefined | null

constructor(props?: Partial<Omit<MarketPeriod, 'toJSON'>>, json?: any) {
Object.assign(this, props)
if (json != null) {
this._block = json.block == null ? undefined : marshal.string.fromJSON(json.block)
this._timestamp = json.timestamp == null ? undefined : marshal.string.fromJSON(json.timestamp)
this._block = json.block == null ? undefined : marshal.fromList(json.block, val => val == null ? undefined : marshal.bigint.fromJSON(val))
this._end = marshal.bigint.fromJSON(json.end)
this._start = marshal.bigint.fromJSON(json.start)
this._timestamp = json.timestamp == null ? undefined : marshal.fromList(json.timestamp, val => val == null ? undefined : marshal.bigint.fromJSON(val))
}
}

/**
* start & end block numbers
*/
get block(): string | undefined | null {
get block(): (bigint | undefined | null)[] | undefined | null {
return this._block
}

set block(value: string | undefined | null) {
set block(value: (bigint | undefined | null)[] | undefined | null) {
this._block = value
}

/**
* Timestamp at which market should end
*/
get end(): bigint {
assert(this._end != null, 'uninitialized access')
return this._end
}

set end(value: bigint) {
this._end = value
}

/**
* Timestamp at which market should start
*/
get start(): bigint {
assert(this._start != null, 'uninitialized access')
return this._start
}

set start(value: bigint) {
this._start = value
}

/**
* start & end timestamps
*/
get timestamp(): string | undefined | null {
get timestamp(): (bigint | undefined | null)[] | undefined | null {
return this._timestamp
}

set timestamp(value: string | undefined | null) {
set timestamp(value: (bigint | undefined | null)[] | undefined | null) {
this._timestamp = value
}

toJSON(): object {
return {
block: this.block,
timestamp: this.timestamp,
block: this.block == null ? undefined : this.block.map((val: any) => val == null ? undefined : marshal.bigint.toJSON(val)),
end: marshal.bigint.toJSON(this.end),
start: marshal.bigint.toJSON(this.start),
timestamp: this.timestamp == null ? undefined : this.timestamp.map((val: any) => val == null ? undefined : marshal.bigint.toJSON(val)),
}
}
}
6 changes: 0 additions & 6 deletions src/model/generated/market.model.ts
Expand Up @@ -124,12 +124,6 @@ export class Market {
@Column_("jsonb", {transformer: {to: obj => obj.toJSON(), from: obj => new MarketPeriod(undefined, marshal.nonNull(obj))}, nullable: false})
period!: MarketPeriod

/**
* Timestamp at which market should end
*/
@Column_("numeric", {transformer: marshal.bigintTransformer, nullable: false})
end!: bigint

/**
* Scoring rule used for the market
*/
Expand Down

0 comments on commit 68e1fc1

Please sign in to comment.