Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: change type ID to Bytes #8

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Billing holds global variables
"""
type Billing @entity {
"Set to 1"
id: ID!
id: Bytes!
"Address of the collectors"
collectors: [Collector!]! @derivedFrom(field: "billing")
"Address of the governor"
Expand All @@ -27,7 +27,7 @@ Curator Name Signal for a single Subgraph
"""
type User @entity {
"EVM address"
id: ID!
id: Bytes!
"Balance of the user in the Billing contract"
billingBalance: BigInt!
"[CUMULATIVE] Total amount of tokens added"
Expand All @@ -52,7 +52,7 @@ A collector authorized to pull tokens
"""
type Collector @entity {
"EVM address"
id: ID!
id: Bytes!
"Billing contract"
billing: Billing!
enabled: Boolean!
Expand All @@ -63,7 +63,7 @@ A generic transaction
"""
interface Transaction {
"Transaction hash concatenated with event log index"
id: ID!
id: Bytes!
"Transaction hash"
hash: Bytes!
"Block number for the transaction"
Expand All @@ -82,7 +82,7 @@ interface Transaction {
TokensAdded Transaction
"""
type TokensAdded implements Transaction @entity {
id: ID!
id: Bytes!
hash: Bytes!
blockNumber: Int!
timestamp: Int!
Expand All @@ -95,7 +95,7 @@ type TokensAdded implements Transaction @entity {
TokensRemoved Transaction
"""
type TokensRemoved implements Transaction @entity {
id: ID!
id: Bytes!
hash: Bytes!
blockNumber: Int!
timestamp: Int!
Expand All @@ -110,7 +110,7 @@ type TokensRemoved implements Transaction @entity {
TokensPulled Transaction. Where a collector pulls tokens from the user
"""
type TokensPulled implements Transaction @entity {
id: ID!
id: Bytes!
hash: Bytes!
blockNumber: Int!
timestamp: Int!
Expand All @@ -129,7 +129,7 @@ enum TransactionType {
InsufficientBalanceForRemoval event, where a user tried to remove funds from L1 but the balance was insufficient
"""
type InsufficientBalanceForRemoval @entity {
id: ID!
id: Bytes!
hash: Bytes!
blockNumber: Int!
timestamp: Int!
Expand All @@ -143,7 +143,7 @@ type InsufficientBalanceForRemoval @entity {

type BillingDailyData @entity {
"<SUBGRAPH ID>-<DAY NUMBER>"
id: ID!
id: Bytes!
"Timestamp for the start of the day that this entity represents. UTC+0"
dayStart: BigInt!
"Timestamp for the end of the day that this entity represents. UTC+0"
Expand Down Expand Up @@ -174,7 +174,7 @@ type BillingDailyData @entity {

type UserDailyData @entity {
"<SUBGRAPH ID>-<DAY NUMBER>"
id: ID!
id: Bytes!
"Timestamp for the start of the day that this entity represents. UTC+0"
dayStart: BigInt!
"Timestamp for the end of the day that this entity represents. UTC+0"
Expand Down
20 changes: 10 additions & 10 deletions src/mappings/billing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ import {
* @dev handleEpochRun - Sets the gateways ("collectors") on the Billing Entity. Creates entity on first try
*/
export function handleCollectorUpdated(event: CollectorUpdated): void {
let collector = Collector.load(event.params.collector.toHexString())
let collector = Collector.load(event.params.collector)
if (collector == null) {
collector = new Collector(event.params.collector.toHexString())
collector = new Collector(event.params.collector)
collector.billing = DEFAULT_BILLING_ID
}
collector.enabled = event.params.enabled
Expand Down Expand Up @@ -71,12 +71,12 @@ export function handleTokensAdded(event: AddedEvent): void {
billing.save()

let tx = new TokensAdded(
event.transaction.hash.toHexString().concat(event.transactionLogIndex.toString()),
event.transaction.hash.concatI32(event.logIndex.toI32())
)
tx.hash = event.transaction.hash
tx.blockNumber = event.block.number.toI32()
tx.timestamp = event.block.timestamp.toI32()
tx.user = event.params.user.toHexString()
tx.user = event.params.user
tx.amount = event.params.amount
tx.type = 'TokensAdded'
tx.save()
Expand All @@ -102,12 +102,12 @@ export function handleTokensRemoved(event: RemovedEvent): void {
billing.save()

let tx = new TokensRemoved(
event.transaction.hash.toHexString().concat(event.transactionLogIndex.toString()),
event.transaction.hash.concatI32(event.logIndex.toI32())
)
tx.hash = event.transaction.hash
tx.blockNumber = event.block.number.toI32()
tx.timestamp = event.block.timestamp.toI32()
tx.user = event.params.from.toHexString()
tx.user = event.params.from
tx.amount = event.params.amount
tx.type = 'TokensRemoved'
tx.to = event.params.to
Expand All @@ -123,12 +123,12 @@ export function handleInsufficientBalanceForRemoval(
event: InsufficientBalanceForRemovalEvent,
): void {
let ev = new InsufficientBalanceForRemoval(
event.transaction.hash.toHexString().concat(event.transactionLogIndex.toString()),
event.transaction.hash.concatI32(event.logIndex.toI32())
)
ev.hash = event.transaction.hash
ev.blockNumber = event.block.number.toI32()
ev.timestamp = event.block.timestamp.toI32()
ev.user = event.params.from.toHexString()
ev.user = event.params.from
ev.amount = event.params.amount
ev.to = event.params.to
ev.save()
Expand All @@ -154,12 +154,12 @@ export function handleTokensPulled(event: PulledEvent): void {
billing.save()

let tx = new TokensPulled(
event.transaction.hash.toHexString().concat(event.transactionLogIndex.toString()),
event.transaction.hash.concatI32(event.logIndex.toI32())
)
tx.hash = event.transaction.hash
tx.blockNumber = event.block.number.toI32()
tx.timestamp = event.block.timestamp.toI32()
tx.user = event.params.user.toHexString()
tx.user = event.params.user
tx.amount = event.params.amount
tx.type = 'TokensPulled'
tx.save()
Expand Down
36 changes: 18 additions & 18 deletions src/mappings/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Billing as BillingContract } from '../types/Billing/Billing'
import { Billing, User, BillingDailyData, UserDailyData } from '../types/schema'
import { BigInt, Address } from '@graphprotocol/graph-ts'
import { BigInt, Address, Bytes } from '@graphprotocol/graph-ts'

export const LAUNCH_DAY = 18613 // 1608163200 / 86400. 1608163200 = 17 Dec 2020 00:00:00 GMT
export const SECONDS_PER_DAY = 86400
export const DEFAULT_BILLING_ID = '1'
export const DEFAULT_BILLING_ID = Bytes.fromI32(1)
/**
* @dev Helper function to load Billing
*/
Expand All @@ -28,7 +28,7 @@ export function getBilling(eventAddress: Address): Billing {
* @dev Helper function to load or create a User
*/
export function createOrLoadUser(userAddress: Address): User {
let id = userAddress.toHexString()
let id = userAddress
let user = User.load(id)
if (user == null) {
user = new User(id)
Expand All @@ -43,18 +43,18 @@ export function createOrLoadUser(userAddress: Address): User {

export function getAndUpdateBillingDailyData(entity: Billing, timestamp: BigInt): BillingDailyData {
let dayNumber = timestamp.toI32() / SECONDS_PER_DAY - LAUNCH_DAY
let id = compoundId(entity.id, BigInt.fromI32(dayNumber).toString())
let id = compoundId(entity.id, Bytes.fromI32(dayNumber))
let dailyData = BillingDailyData.load(id)

if (dailyData == null) {
if (dailyData === null) {
dailyData = new BillingDailyData(id)

dailyData.dayStart = BigInt.fromI32((timestamp.toI32() / SECONDS_PER_DAY) * SECONDS_PER_DAY)
dailyData.dayEnd = dailyData.dayStart + BigInt.fromI32(SECONDS_PER_DAY)
dailyData.dayEnd = dailyData.dayStart.plus(BigInt.fromI32(SECONDS_PER_DAY))
dailyData.dayNumber = dayNumber
dailyData.entity = entity.id

if (entity.currentDailyDataEntity != null) {
if (entity.currentDailyDataEntity !== null) {
entity.previousDailyDataEntity = entity.currentDailyDataEntity
}
entity.currentDailyDataEntity = dailyData.id
Expand All @@ -66,7 +66,7 @@ export function getAndUpdateBillingDailyData(entity: Billing, timestamp: BigInt)
dailyData.totalCurrentBalance = entity.totalCurrentBalance
dailyData.governor = entity.governor

if (entity.previousDailyDataEntity != null) {
if (entity.previousDailyDataEntity !== null) {
let previousDailyDataPoint = BillingDailyData.load(entity.previousDailyDataEntity!)!

dailyData.totalTokensAddedDelta = entity.totalTokensAdded.minus(
Expand All @@ -90,23 +90,23 @@ export function getAndUpdateBillingDailyData(entity: Billing, timestamp: BigInt)

dailyData.save()

return dailyData as BillingDailyData
return dailyData
}

export function getAndUpdateUserDailyData(entity: User, timestamp: BigInt): UserDailyData {
let dayNumber = timestamp.toI32() / SECONDS_PER_DAY - LAUNCH_DAY
let id = compoundId(entity.id, BigInt.fromI32(dayNumber).toString())
let dayNumber = (timestamp.toI32() / SECONDS_PER_DAY) - LAUNCH_DAY
let id = compoundId(entity.id, Bytes.fromI32(dayNumber))
let dailyData = UserDailyData.load(id)

if (dailyData == null) {
if (dailyData === null) {
dailyData = new UserDailyData(id)

dailyData.dayStart = BigInt.fromI32((timestamp.toI32() / SECONDS_PER_DAY) * SECONDS_PER_DAY)
dailyData.dayEnd = dailyData.dayStart + BigInt.fromI32(SECONDS_PER_DAY)
dailyData.dayEnd = dailyData.dayStart.plus(BigInt.fromI32(SECONDS_PER_DAY))
dailyData.dayNumber = dayNumber
dailyData.entity = entity.id

if (entity.currentDailyDataEntity != null) {
if (entity.currentDailyDataEntity !== null) {
entity.previousDailyDataEntity = entity.currentDailyDataEntity
}
entity.currentDailyDataEntity = dailyData.id
Expand All @@ -117,7 +117,7 @@ export function getAndUpdateUserDailyData(entity: User, timestamp: BigInt): User
dailyData.totalTokensRemoved = entity.totalTokensRemoved
dailyData.billingBalance = entity.billingBalance

if (entity.previousDailyDataEntity != null) {
if (entity.previousDailyDataEntity !== null) {
let previousDailyDataPoint = UserDailyData.load(entity.previousDailyDataEntity!)!

dailyData.totalTokensAddedDelta = entity.totalTokensAdded.minus(
Expand All @@ -141,9 +141,9 @@ export function getAndUpdateUserDailyData(entity: User, timestamp: BigInt): User

dailyData.save()

return dailyData as UserDailyData
return dailyData
}

export function compoundId(idA: string, idB: string): string {
return idA.concat('-').concat(idB)
export function compoundId(idA: Bytes, idB: Bytes): Bytes {
return idA.concat(idB)
Comment on lines +147 to +148
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small caveat with this function, there are some cases (unsure if it happens in this particular subgraph though) where you can end up with ID collision by using this approach (no separator when joining IDs).
It happened in the activity subgraph, where we had an ID generation logic that took an address and 2 numbers (which I can't recall if it was an address + log index and a counter, or the other way around).
You could have multiple collisions as long as logIndex.concat(counter) would be equals, which can happen in many different ways, like having a log index of 11 and a count equals to 0, and a log index of 1 and a count equals to 10.

There are 2 different ways of avoiding such collisions, one is to simply use a separator (when using strings it was often times quite easy since you simple used -and that's it, but with bytes you can't, so you need to use a random set of characters that you know) or use fixed bytes length for each different type of data (that way, if you know you are having 32 bytes for each number, 11 + 0 and 1 + 10 won't collide, since the padding will be different).

}