Skip to content

Commit

Permalink
more staking counts
Browse files Browse the repository at this point in the history
  • Loading branch information
coffee-converter committed Dec 16, 2020
1 parent 512f1f8 commit afc8917
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 26 deletions.
14 changes: 14 additions & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type User @entity {
reservationCount: BigInt!
reservationDayCount: BigInt!
referralCount: BigInt!
stakeCount: BigInt!
}

type Reservation @entity {
Expand Down Expand Up @@ -78,8 +79,21 @@ type Global @entity {
referrerCount: BigInt!
cmReferrerCount: BigInt!
reservationCount: BigInt!
stakeCount: BigInt!
stakerCount: BigInt!
}

type Stake @entity {
id: ID!
staker: User!
referrer: User!
principal: BigInt!
shares: BigInt!
cmShares: BigInt!
startDay: BigInt!
lockDays: BigInt!
daiEquivalent: BigInt!
reward: BigInt
closeDay: BigInt
penalty: BigInt
}
29 changes: 4 additions & 25 deletions src/mapping-liquidityTransformer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { BigInt, ethereum } from "@graphprotocol/graph-ts"
import {
getOrCreateGlobal,
createUser
} from "./shared"
import {
ReferralAdded,
WiseReservation,
Expand All @@ -13,7 +17,6 @@ import {
GlobalReservationDaySnapshot,
Referral,
Transaction,
Global,
} from "../generated/schema"

let CM_REFERRER_THRESHOLD = BigInt.fromI32(50).times(BigInt.fromI32(10).pow(18))
Expand Down Expand Up @@ -47,30 +50,6 @@ function getMinSupply (day: BigInt): BigInt {
}
}

function getOrCreateGlobal(): Global | null {
let global = Global.load("0")
if (global == null) {
global = new Global("0")
global.userCount = BigInt.fromI32(0)
global.reserverCount = BigInt.fromI32(0)
global.referrerCount = BigInt.fromI32(0)
global.cmReferrerCount = BigInt.fromI32(0)
global.reservationCount = BigInt.fromI32(0)
global.save()
}
return global
}

function createUser(id: string): User | null {
let user = new User(id)
user.reservedEth = BigInt.fromI32(0)
user.referredEth = BigInt.fromI32(0)
user.reservationCount = BigInt.fromI32(0)
user.reservationDayCount = BigInt.fromI32(0)
user.referralCount = BigInt.fromI32(0)
return user
}

function upsertTransaction(tx: ethereum.Transaction, block: ethereum.Block): Transaction | null {
let transaction = Transaction.load(tx.hash.toHexString())
if (transaction == null) {
Expand Down
37 changes: 36 additions & 1 deletion src/mapping-wiseToken.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,48 @@
import { BigInt, ethereum } from "@graphprotocol/graph-ts"
import {
getOrCreateGlobal,
createUser
} from "./shared"
import {
StakeStart
} from "../generated/WiseToken/WiseToken"
import {
Stake
Stake,
User
} from "../generated/schema"

export function handleStakeStart (event: StakeStart): void {
let global = getOrCreateGlobal();
global.stakeCount = global.stakeCount.plus(BigInt.fromI32(1))

let stakerID = event.params.stakerAddress.toHexString()
let staker = User.load(stakerID)
if (staker == null) {
staker = createUser(stakerID)
global.userCount = global.userCount.plus(BigInt.fromI32(1))
global.stakerCount = global.stakerCount.plus(BigInt.fromI32(1))
}
staker.stakeCount = staker.stakeCount.plus(BigInt.fromI32(1))
staker.save()

let referrerID = event.params.referralAddress.toHexString()
let referrer = User.load(referrerID)
if (referrer == null) {
referrer = new User(referrerID)
global.userCount = global.userCount.plus(BigInt.fromI32(1))
}
referrer.save()
global.save()

let stake = new Stake(event.params.stakeID.toHexString())
stake.staker = staker.id
stake.referrer = referrer.id
stake.principal = event.params.stakedAmount
stake.shares = event.params.stakesShares
stake.cmShares = event.params.referralShares
stake.startDay = event.params.startDay
stake.lockDays = event.params.lockDays
stake.daiEquivalent = event.params.daiEquivalent
stake.save()
}

Expand Down
28 changes: 28 additions & 0 deletions src/shared.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import {BigInt} from "@graphprotocol/graph-ts/index";
import {Global, User} from "../generated/schema";

export function getOrCreateGlobal(): Global | null {
let global = Global.load("0")
if (global == null) {
global = new Global("0")
global.userCount = BigInt.fromI32(0)
global.reserverCount = BigInt.fromI32(0)
global.referrerCount = BigInt.fromI32(0)
global.cmReferrerCount = BigInt.fromI32(0)
global.reservationCount = BigInt.fromI32(0)
global.stakeCount = BigInt.fromI32(0)
global.stakerCount = BigInt.fromI32(0)
global.save()
}
return global
}

export function createUser(id: string): User | null {
let user = new User(id)
user.reservedEth = BigInt.fromI32(0)
user.referredEth = BigInt.fromI32(0)
user.reservationCount = BigInt.fromI32(0)
user.reservationDayCount = BigInt.fromI32(0)
user.referralCount = BigInt.fromI32(0)
return user
}

0 comments on commit afc8917

Please sign in to comment.