Skip to content

Commit

Permalink
feat(prediction): Market statistic (pancakeswap#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChefKai authored May 10, 2021
1 parent 06e4c4a commit d4f581c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/prediction/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ export function handlePause(event: Pause): void {
market = new Market("1");
market.epoch = event.params.epoch.toString();
market.paused = true;
market.totalUsers = ZERO_BI;
market.totalBets = ZERO_BI;
market.totalBNB = ZERO_BD;
market.save();
}
market.epoch = event.params.epoch.toString();
Expand Down Expand Up @@ -56,6 +59,9 @@ export function handleUnpause(event: Unpause): void {
market = new Market("1");
market.epoch = event.params.epoch.toString();
market.paused = false;
market.totalUsers = ZERO_BI;
market.totalBets = ZERO_BI;
market.totalBNB = ZERO_BD;
market.save();
}
market.epoch = event.params.epoch.toString();
Expand All @@ -72,6 +78,9 @@ export function handleStartRound(event: StartRound): void {
if (market === null) {
market = new Market("1");
market.paused = false;
market.totalUsers = ZERO_BI;
market.totalBets = ZERO_BI;
market.totalBNB = ZERO_BD;
market.save();
}

Expand Down Expand Up @@ -137,6 +146,14 @@ export function handleEndRound(event: EndRound): void {
}

export function handleBetBull(event: BetBull): void {
let market = Market.load("1");
if (market === null) {
log.error("Tried query market with bet (bear)", []);
}
market.totalBets = market.totalBets.plus(ONE_BI);
market.totalBNB = market.totalBNB.plus(event.params.amount.divDecimal(EIGHTEEN_BD));
market.save();

let round = Round.load(event.params.currentEpoch.toString());
if (round === null) {
log.error("Tried to bet (bull) without an existing round (epoch: {}).", [event.params.currentEpoch.toString()]);
Expand All @@ -157,6 +174,9 @@ export function handleBetBull(event: BetBull): void {
user.block = event.block.number;
user.totalBets = ZERO_BI;
user.totalBNB = ZERO_BD;

market.totalUsers = market.totalUsers.plus(ONE_BI);
market.save();
}
user.updatedAt = event.block.timestamp;
user.totalBets = user.totalBets.plus(ONE_BI);
Expand All @@ -178,6 +198,14 @@ export function handleBetBull(event: BetBull): void {
}

export function handleBetBear(event: BetBear): void {
let market = Market.load("1");
if (market === null) {
log.error("Tried query market with bet (bear)", []);
}
market.totalBets = market.totalBets.plus(ONE_BI);
market.totalBNB = market.totalBNB.plus(event.params.amount.divDecimal(EIGHTEEN_BD));
market.save();

let round = Round.load(event.params.currentEpoch.toString());
if (round === null) {
log.error("Tried to bet (bear) without an existing round (epoch: {}).", [event.params.currentEpoch.toString()]);
Expand All @@ -198,6 +226,9 @@ export function handleBetBear(event: BetBear): void {
user.block = event.block.number;
user.totalBets = ZERO_BI;
user.totalBNB = ZERO_BD;

market.totalUsers = market.totalUsers.plus(ONE_BI);
market.save();
}
user.updatedAt = event.block.timestamp;
user.totalBets = user.totalBets.plus(ONE_BI);
Expand Down
4 changes: 4 additions & 0 deletions subgraphs/prediction.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ type Market @entity {

epoch: Round
paused: Boolean!

totalUsers: BigInt!
totalBets: BigInt!
totalBNB: BigDecimal!
}

type Round @entity {
Expand Down

0 comments on commit d4f581c

Please sign in to comment.