From afc89170cea3868c2ecd37dcc48f0fc8d577960a Mon Sep 17 00:00:00 2001 From: coffee-converter Date: Wed, 16 Dec 2020 06:20:42 -0600 Subject: [PATCH] more staking counts --- schema.graphql | 14 +++++++++++ src/mapping-liquidityTransformer.ts | 29 ++++------------------ src/mapping-wiseToken.ts | 37 ++++++++++++++++++++++++++++- src/shared.ts | 28 ++++++++++++++++++++++ 4 files changed, 82 insertions(+), 26 deletions(-) create mode 100644 src/shared.ts diff --git a/schema.graphql b/schema.graphql index fedfc07..ae83084 100644 --- a/schema.graphql +++ b/schema.graphql @@ -18,6 +18,7 @@ type User @entity { reservationCount: BigInt! reservationDayCount: BigInt! referralCount: BigInt! + stakeCount: BigInt! } type Reservation @entity { @@ -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 } \ No newline at end of file diff --git a/src/mapping-liquidityTransformer.ts b/src/mapping-liquidityTransformer.ts index 38f5e40..e5a4689 100644 --- a/src/mapping-liquidityTransformer.ts +++ b/src/mapping-liquidityTransformer.ts @@ -1,4 +1,8 @@ import { BigInt, ethereum } from "@graphprotocol/graph-ts" +import { + getOrCreateGlobal, + createUser +} from "./shared" import { ReferralAdded, WiseReservation, @@ -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)) @@ -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) { diff --git a/src/mapping-wiseToken.ts b/src/mapping-wiseToken.ts index 7f79648..3433f76 100644 --- a/src/mapping-wiseToken.ts +++ b/src/mapping-wiseToken.ts @@ -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() } diff --git a/src/shared.ts b/src/shared.ts new file mode 100644 index 0000000..5610294 --- /dev/null +++ b/src/shared.ts @@ -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 +} \ No newline at end of file