diff --git a/schema.graphql b/schema.graphql index 4c4276b..289ee3d 100644 --- a/schema.graphql +++ b/schema.graphql @@ -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" @@ -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" @@ -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! @@ -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" @@ -82,7 +82,7 @@ interface Transaction { TokensAdded Transaction """ type TokensAdded implements Transaction @entity { - id: ID! + id: Bytes! hash: Bytes! blockNumber: Int! timestamp: Int! @@ -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! @@ -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! @@ -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! @@ -143,7 +143,7 @@ type InsufficientBalanceForRemoval @entity { type BillingDailyData @entity { "-" - 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" @@ -174,7 +174,7 @@ type BillingDailyData @entity { type UserDailyData @entity { "-" - 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" diff --git a/src/mappings/billing.ts b/src/mappings/billing.ts index 65936b7..13d611a 100644 --- a/src/mappings/billing.ts +++ b/src/mappings/billing.ts @@ -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 @@ -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() @@ -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 @@ -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() @@ -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() diff --git a/src/mappings/helpers.ts b/src/mappings/helpers.ts index 9cee6cf..d3b79a5 100644 --- a/src/mappings/helpers.ts +++ b/src/mappings/helpers.ts @@ -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 */ @@ -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) @@ -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 @@ -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( @@ -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 @@ -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( @@ -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) }