Skip to content

Commit

Permalink
tx creation cleanup, performance stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
coffee-converter committed Nov 25, 2020
1 parent bdcd8cb commit e609805
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 35 deletions.
2 changes: 1 addition & 1 deletion schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ type Referral @entity {
referrer: User!
referee: User!
amount: BigInt!
}
}
59 changes: 25 additions & 34 deletions src/mapping.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { BigInt } from "@graphprotocol/graph-ts"
import { BigInt, ethereum } from "@graphprotocol/graph-ts"
import {
ReferralAdded,
WiseReservation
} from "../generated/WiseLiquidityTransformer/LiquidityTransformer"
import { User, Reservation, Referral, Transaction } from "../generated/schema"
import {
User,
Reservation,
Referral,
Transaction,
} from "../generated/schema"

function getOrCreateUser(id: string): User | null {
let user = User.load(id)
Expand All @@ -15,22 +20,17 @@ function getOrCreateUser(id: string): User | null {
return user
}

function getOrCreateTransaction(id: string, senderID: string, blockNumber: BigInt, timestamp: BigInt): Transaction | null {
let transaction = Transaction.load(id)
if (transaction == null) {
transaction = new Transaction(id)
transaction.blockNumber = blockNumber
transaction.timestamp = timestamp
transaction.sender = senderID
}
function upsertTransaction(tx: ethereum.Transaction, block: ethereum.Block): Transaction | null {
let transaction = new Transaction(tx.hash.toHexString())
transaction.blockNumber = block.number
transaction.timestamp = block.timestamp
transaction.sender = tx.from.toHexString()
transaction.save()
return transaction
}

export function handleReferralAdded(event: ReferralAdded): void {
let txHash = event.transaction.hash.toHexString()
let txFrom = event.transaction.from.toHexString()
let transaction = getOrCreateTransaction(txHash, txFrom, event.block.number, event.block.timestamp)
transaction.save()
let transaction = upsertTransaction(event.transaction, event.block)

let referrerID = event.params.referral.toHexString()
let referrer = getOrCreateUser(referrerID)
Expand All @@ -40,38 +40,29 @@ export function handleReferralAdded(event: ReferralAdded): void {
referee.save()

let referralID = event.transaction.hash.toHexString()
let referral = Referral.load(referralID)
if (referral == null) {
referral = new Referral(referralID)
referral.transaction = transaction.id
referral.referrer = referrer.id
referral.referee = referee.id
referral.amount = event.params.amount
}
let referral = new Referral(referralID)
referral.transaction = transaction.id
referral.referrer = referrer.id
referral.referee = referee.id
referral.amount = event.params.amount
referral.save()

referrer.referredEth = referrer.referredEth.plus(referral.amount)
referrer.save()
}

export function handleWiseReservation(event: WiseReservation): void {
let txHash = event.transaction.hash.toHexString()
let txFrom = event.transaction.from.toHexString()
let transaction = getOrCreateTransaction(txHash, txFrom, event.block.number, event.block.timestamp)
transaction.save()
let transaction = upsertTransaction(event.transaction, event.block)

let userID = event.transaction.from.toHexString()
let user = getOrCreateUser(userID)

let reservationID = event.transaction.hash.toHexString() + "-" + event.params.investmentDay.toString()
let reservation = Reservation.load(reservationID)
if (reservation == null) {
reservation = new Reservation(reservationID)
reservation.transaction = transaction.id
reservation.user = user.id
reservation.investmentDay = event.params.investmentDay
reservation.amount = event.params.amount
}
let reservation = new Reservation(reservationID)
reservation.transaction = transaction.id
reservation.user = user.id
reservation.investmentDay = event.params.investmentDay
reservation.amount = event.params.amount
reservation.save()

user.reservedEth = user.reservedEth.plus(reservation.amount)
Expand Down

0 comments on commit e609805

Please sign in to comment.