Skip to content

Commit

Permalink
various refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
coffee-converter committed Dec 18, 2020
1 parent 7894fbe commit 6a2e4d4
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 214 deletions.
18 changes: 9 additions & 9 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ type Transaction @entity {
blockNumber: BigInt!
timestamp: BigInt!
sender: User!
referral: Referral
referral: ReservationReferral
reservations: [Reservation!]! @derivedFrom(field: "transaction")
}

Expand All @@ -12,13 +12,13 @@ type User @entity {
transactions: [Transaction!]! @derivedFrom(field: "sender")
reservations: [Reservation!]! @derivedFrom(field: "user")
reservationDays: [UserReservationDay!]! @derivedFrom(field: "user")
referrals: [Referral!]! @derivedFrom(field: "referrer")
reservationEffectiveWei: BigInt!
reservationActualWei: BigInt!
referralActualWei: BigInt!
reservationCount: BigInt!
reservationDayCount: BigInt!
referralCount: BigInt!
reservationReferrals: [ReservationReferral!]! @derivedFrom(field: "referrer")
reservationReferralActualWei: BigInt!
reservationReferralCount: BigInt!
stakeCount: BigInt!
cmStatus: Boolean!
cmStatusInLaunch: Boolean!
Expand All @@ -32,10 +32,10 @@ type Reservation @entity {
investmentDay: BigInt!
effectiveWei: BigInt!
actualWei: BigInt!
referral: Referral
referral: ReservationReferral
}

type Referral @entity {
type ReservationReferral @entity {
id: ID!
transaction: Transaction!
timestamp: BigInt!
Expand Down Expand Up @@ -79,14 +79,14 @@ type Global @entity {
id: ID!
userCount: BigInt!
reserverCount: BigInt!
referrerCount: BigInt!
reservationReferrerCount: BigInt!
cmStatusCount: BigInt!
cmStatusInLaunchCount: BigInt!
reservationCount: BigInt!
stakeCount: BigInt!
stakerCount: BigInt!
reservationEffectiveWei: BigInt!
reservationActualWei: BigInt!
stakeCount: BigInt!
stakerCount: BigInt!
}

type Stake @entity {
Expand Down
147 changes: 40 additions & 107 deletions src/mapping-liquidityTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import { BigInt, ethereum } from "@graphprotocol/graph-ts"
import {
getOrCreateGlobal,
createUser,
ZERO,
ONE,
ethVal,
} from "./shared"
import {
ReferralAdded,
Expand All @@ -15,20 +18,20 @@ import {
UserReservationDay,
GlobalReservationDay,
GlobalReservationDaySnapshot,
Referral,
ReservationReferral,
Transaction,
} from "../generated/schema"

let CM_REFERRER_THRESHOLD = BigInt.fromI32(50).times(BigInt.fromI32(10).pow(18))
let CM_REFERRER_THRESHOLD = ethVal(50)

let NORMAL_SUPPLY = BigInt.fromI32(5000000).times(BigInt.fromI32(10).pow(18)),
let NORMAL_SUPPLY = ethVal(5000000),
MAX_SUPPLY = NORMAL_SUPPLY.plus(NORMAL_SUPPLY),
MIN_SUPPLY_1 = BigInt.fromI32(4500000).times(BigInt.fromI32(10).pow(18)),
MIN_SUPPLY_2 = BigInt.fromI32(4000000).times(BigInt.fromI32(10).pow(18)),
MIN_SUPPLY_3 = BigInt.fromI32(3500000).times(BigInt.fromI32(10).pow(18)),
MIN_SUPPLY_4 = BigInt.fromI32(3000000).times(BigInt.fromI32(10).pow(18)),
MIN_SUPPLY_5 = BigInt.fromI32(2500000).times(BigInt.fromI32(10).pow(18)),
MIN_SUPPLY_6 = BigInt.fromI32(1).times(BigInt.fromI32(10).pow(18))
MIN_SUPPLY_1 = ethVal(4500000),
MIN_SUPPLY_2 = ethVal(4000000),
MIN_SUPPLY_3 = ethVal(3500000),
MIN_SUPPLY_4 = ethVal(3000000),
MIN_SUPPLY_5 = ethVal(2500000),
MIN_SUPPLY_6 = ethVal(1)

function getMinSupply (day: BigInt): BigInt {
let dayVal = day.toI32()
Expand Down Expand Up @@ -72,39 +75,40 @@ export function handleReferralAdded(event: ReferralAdded): void {
let referrer = User.load(referrerID)
if (referrer == null) {
referrer = createUser(referrerID)
global.userCount = global.userCount.plus(BigInt.fromI32(1))
global.userCount = global.userCount.plus(ONE)
}
if (referrer.referralCount == BigInt.fromI32(0)) {
global.referrerCount = global.referrerCount.plus(BigInt.fromI32(1))
// TODO refactor more == and maybe other operators
if (referrer.reservationReferralCount.equals(ZERO)) {
global.reservationReferrerCount = global.reservationReferrerCount.plus(ONE)
}

let refereeID = event.params.referee.toHexString()
let referee = User.load(refereeID)
if (referee == null) {
referee = createUser(refereeID)
global.userCount = global.userCount.plus(BigInt.fromI32(1))
global.userCount = global.userCount.plus(ONE)
}
let reservedEffectiveEth = event.params.amount.times(BigInt.fromI32(11)).div(BigInt.fromI32(10))
referee.reservationActualWei = referee.reservationActualWei.plus(event.params.amount).minus(reservedEffectiveEth)
global.reservationActualWei = global.reservationActualWei.plus(event.params.amount).minus(reservedEffectiveEth)
referee.save()

let referralID = event.transaction.hash.toHexString()
let referral = new Referral(referralID)
let referral = new ReservationReferral(referralID)
referral.transaction = transaction.id
referral.timestamp = transaction.timestamp
referral.referrer = referrer.id
referral.referee = referee.id
referral.actualWei = event.params.amount
referral.save()

let wasBelowCm = referrer.referralActualWei < CM_REFERRER_THRESHOLD;
referrer.referralActualWei = referrer.referralActualWei.plus(referral.actualWei)
referrer.referralCount = referrer.referralCount.plus(BigInt.fromI32(1))
let wasBelowCm = referrer.reservationReferralActualWei < CM_REFERRER_THRESHOLD
referrer.reservationReferralActualWei = referrer.reservationReferralActualWei.plus(referral.actualWei)
referrer.reservationReferralCount = referrer.reservationReferralCount.plus(ONE)
referrer.save()
if (wasBelowCm && referrer.referralActualWei >= CM_REFERRER_THRESHOLD) {
if (wasBelowCm && referrer.reservationReferralActualWei >= CM_REFERRER_THRESHOLD) {
referrer.cmStatusInLaunch = true
global.cmStatusInLaunchCount = global.cmStatusInLaunchCount.plus(BigInt.fromI32(1))
global.cmStatusInLaunchCount = global.cmStatusInLaunchCount.plus(ONE)
}
global.save()

Expand All @@ -118,6 +122,7 @@ export function handleReferralAdded(event: ReferralAdded): void {
let reservation = Reservation.load(resID)
if (reservation != null) {
resList.push(reservation)
// TODO populate reservation.referral and save? Too costly?
}
}

Expand Down Expand Up @@ -149,18 +154,18 @@ export function handleReferralAdded(event: ReferralAdded): void {

export function handleWiseReservation(event: WiseReservation): void {
let global = getOrCreateGlobal()
global.reservationCount = global.reservationCount.plus(BigInt.fromI32(1))
global.reservationCount = global.reservationCount.plus(ONE)

let transaction = upsertTransaction(event.transaction, event.block)

let userID = event.transaction.from.toHexString()
let user = User.load(userID)
if (user == null) {
user = createUser(userID)
global.userCount = global.userCount.plus(BigInt.fromI32(1))
global.userCount = global.userCount.plus(ONE)
}
if (user.reservationCount == BigInt.fromI32(0)) {
global.reserverCount = global.reserverCount.plus(BigInt.fromI32(1))
if (user.reservationCount == ZERO) {
global.reserverCount = global.reserverCount.plus(ONE)
}

let reservationID = event.transaction.hash.toHexString() + "-" + event.params.investmentDay.toString()
Expand All @@ -174,7 +179,7 @@ export function handleWiseReservation(event: WiseReservation): void {
reservation.referral = null
reservation.save()

user.reservationCount = user.reservationCount.plus(BigInt.fromI32(1))
user.reservationCount = user.reservationCount.plus(ONE)
user.reservationEffectiveWei = user.reservationEffectiveWei.plus(reservation.effectiveWei)
user.reservationActualWei = user.reservationActualWei.plus(reservation.effectiveWei)
global.reservationEffectiveWei = global.reservationEffectiveWei.plus(reservation.effectiveWei)
Expand All @@ -188,14 +193,14 @@ export function handleWiseReservation(event: WiseReservation): void {
gResDay.investmentDay = reservation.investmentDay
gResDay.minSupply = getMinSupply(gResDay.investmentDay)
gResDay.maxSupply = MAX_SUPPLY.minus(gResDay.minSupply)
gResDay.effectiveWei = BigInt.fromI32(0)
gResDay.actualWei = BigInt.fromI32(0)
gResDay.reservationCount = BigInt.fromI32(0)
gResDay.userCount = BigInt.fromI32(0)
gResDay.effectiveWei = ZERO
gResDay.actualWei = ZERO
gResDay.reservationCount = ZERO
gResDay.userCount = ZERO
}
gResDay.effectiveWei = gResDay.effectiveWei.plus(reservation.effectiveWei)
gResDay.actualWei = gResDay.actualWei.plus(reservation.effectiveWei)
gResDay.reservationCount = gResDay.reservationCount.plus(BigInt.fromI32(1))
gResDay.reservationCount = gResDay.reservationCount.plus(ONE)

let gResDaySnapshotID = reservation.investmentDay.toString() + "-" + event.block.timestamp.toString()
let gResDaySnapshot = new GlobalReservationDaySnapshot(gResDaySnapshotID)
Expand All @@ -211,15 +216,15 @@ export function handleWiseReservation(event: WiseReservation): void {
uResDay = new UserReservationDay(uResDayID)
uResDay.user = user.id
uResDay.investmentDay = reservation.investmentDay
uResDay.effectiveWei = BigInt.fromI32(0)
uResDay.actualWei = BigInt.fromI32(0)
uResDay.reservationCount = BigInt.fromI32(0)
gResDay.userCount = gResDay.userCount.plus(BigInt.fromI32(1))
user.reservationDayCount = user.reservationDayCount.plus(BigInt.fromI32(1))
uResDay.effectiveWei = ZERO
uResDay.actualWei = ZERO
uResDay.reservationCount = ZERO
gResDay.userCount = gResDay.userCount.plus(ONE)
user.reservationDayCount = user.reservationDayCount.plus(ONE)
}
uResDay.effectiveWei = uResDay.effectiveWei.plus(reservation.effectiveWei)
uResDay.actualWei = uResDay.actualWei.plus(reservation.effectiveWei)
uResDay.reservationCount = uResDay.reservationCount.plus(BigInt.fromI32(1))
uResDay.reservationCount = uResDay.reservationCount.plus(ONE)
uResDay.save()

gResDay.save()
Expand All @@ -241,75 +246,3 @@ export function handleGeneratedRandomSupply(event: GeneratedRandomSupply): void
day.supply = event.params.randomSupply
day.save()
}

/*
// Entities can be loaded from the store using a string ID this ID
// needs to be unique across all entities of the same type
let entity = ExampleEntity.load(event.transaction.from.toHex())
// Entities only exist after they have been saved to the store
// `null` checks allow to create entities on demand
if (entity == null) {
entity = new ExampleEntity(event.transaction.from.toHex())
// Entity fields can be set using simple assignments
entity.count = BigInt.fromI32(0)
}
// BigInt and BigDecimal math are supported
entity.count = entity.count + BigInt.fromI32(1)
// Entity fields can be set based on event parameters
entity.investmentDay = event.params.investmentDay
entity.randomSupply = event.params.randomSupply
// Entities can be written to the store with `.save()`
entity.save()
// Note: If a handler doesn't require existing field values, it is faster
// _not_ to load the entity from the store. Instead, create it fresh with
// `new Entity(...)`, set the fields that should be updated and save the
// entity back to the store. Fields that were not set or unset remain
// unchanged, allowing for partial updates to be applied.
// It is also possible to access smart contracts from mappings. For
// example, the contract that has emitted the event can be connected to
// with:
//
// let contract = Contract.bind(event.address)
//
// The following functions can then be called on this contract to access
// state variables and other data:
//
// - contract.REFUND_SPONSOR(...)
// - contract.TOKEN_DEFINER(...)
// - contract.UNISWAP_PAIR(...)
// - contract.UNISWAP_ROUTER(...)
// - contract.WISE_CONTRACT(...)
// - contract._currentWiseDay(...)
// - contract.dailyTotalInvestment(...)
// - contract.dailyTotalSupply(...)
// - contract.fundedDays(...)
// - contract.g(...)
// - contract.investmentsOnAllDays(...)
// - contract.investorAccountCount(...)
// - contract.investorAccounts(...)
// - contract.investorBalances(...)
// - contract.investorTotalBalance(...)
// - contract.investorsOnAllDays(...)
// - contract.investorsOnDay(...)
// - contract.myInvestmentAmount(...)
// - contract.myInvestmentAmountAllDays(...)
// - contract.myTotalInvestmentAmount(...)
// - contract.payoutInvestorAddress(...)
// - contract.payoutReferralAddress(...)
// - contract.referralAccountCount(...)
// - contract.referralAccounts(...)
// - contract.referralAmount(...)
// - contract.referralTokens(...)
// - contract.requestRefund(...)
// - contract.supplyOnAllDays(...)
// - contract.uniqueInvestorCount(...)
// - contract.uniqueInvestors(...)
}
*/
Loading

0 comments on commit 6a2e4d4

Please sign in to comment.