Skip to content

Commit

Permalink
Fix typing issues
Browse files Browse the repository at this point in the history
  • Loading branch information
0xAurelius committed Oct 7, 2024
1 parent 111351a commit 717a372
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 25 deletions.
7 changes: 3 additions & 4 deletions polygon-digital-carbon/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -413,11 +413,10 @@ type KlimaRetire @entity {
type DailyKlimaRetireSnapshot @entity {
id: ID!
timestamp: BigInt!
pool: String!
token: String!
pool: Bytes!
credit: CarbonCredit!
amount: BigDecimal!
feeAmount: BigDecimal!
amount: BigInt!
feeAmount: BigInt!
}

type CarbonMetric @entity {
Expand Down
45 changes: 28 additions & 17 deletions polygon-digital-carbon/src/KlimaAggregator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { loadRetire } from './utils/Retire'
import { loadOrCreateDailyKlimaRetireSnapshot } from './utils/DailyKlimaRetireSnapshot'
import { dayTimestamp as dayTimestampString } from '../../lib/utils/Dates'
import { ZERO_ADDRESS } from '../../lib/utils/Constants'
import { saveKlimaRetire } from './utils/KlimaRetire'
import { loadKlimaRetire, saveKlimaRetire } from './utils/KlimaRetire'
import { ZERO_BI } from '../../lib/utils/Decimals'
import { getRetirementsContractAddress } from '../utils/helpers'

Expand Down Expand Up @@ -40,11 +40,6 @@ export function handleMossRetired(event: MossRetired): void {
retire.retiringAddress = event.params.retiringAddress
retire.retirementMessage = event.params.retirementMessage

const dailyRetirement = generateDailyKlimaRetirement(retire)
dailyRetirement.save()

updateKlimaRetirementProtocolMetrics(retire.pool, event.block.timestamp, event.params.retiredAmount)

retire.save()

saveKlimaRetire(
Expand All @@ -55,6 +50,17 @@ export function handleMossRetired(event: MossRetired): void {
false
)

let klimaRetire = loadKlimaRetire(retire.klimaRetire._id)
if (klimaRetire !== null ){
const dailyRetirement = generateDailyKlimaRetirement(klimaRetire)
if (dailyRetirement !== null) {
dailyRetirement.save()
}
}

if (retire.pool !== null) {
updateKlimaRetirementProtocolMetrics(retire.pool as Bytes, event.block.timestamp, event.params.retiredAmount)
}
}

export function handleToucanRetired(event: ToucanRetired): void {
Expand Down Expand Up @@ -192,23 +198,28 @@ export function handleCarbonRetiredWithTokenId(event: CarbonRetiredTokenId): voi
)
}

function generateDailyKlimaRetirement(klimaRetire: KlimaRetire): DailyKlimaRetireSnapshot {
function generateDailyKlimaRetirement(klimaRetire: KlimaRetire): DailyKlimaRetireSnapshot | null {
const retire = loadRetire(klimaRetire.retire)
const dayTimestamp = dayTimestampString(retire.timestamp)
const id = dayTimestamp + retire.credit
const id = dayTimestamp + retire.credit.toString()

if (retire.pool !== null) {
const dailyKlimaRetirement = loadOrCreateDailyKlimaRetireSnapshot(id)
dailyKlimaRetirement.amount = dailyKlimaRetirement.amount.plus(retire.amount)
dailyKlimaRetirement.feeAmount = dailyKlimaRetirement.feeAmount.plus(klimaRetire.feeAmount)
dailyKlimaRetirement.credit = retire.credit
dailyKlimaRetirement.pool = retire.pool as Bytes
dailyKlimaRetirement.credit = retire.credit
dailyKlimaRetirement.timestamp = BigInt.fromString(dayTimestamp)

return dailyKlimaRetirement
}

const dailyKlimaRetirement = loadOrCreateDailyKlimaRetireSnapshot(id)
dailyKlimaRetirement.amount = dailyKlimaRetirement.amount.plus(klimaRetire.amount)
dailyKlimaRetirement.feeAmount = dailyKlimaRetirement.feeAmount.plus(klimaRetire.feeAmount)
dailyKlimaRetirement.credit = klimaRetire.credit
dailyKlimaRetirement.pool = klimaRetire.pool
dailyKlimaRetirement.token = klimaRetire.token
dailyKlimaRetirement.timestamp = BigInt.fromString(dayTimestamp)
return null

return dailyKlimaRetirement
}

function updateKlimaRetirementProtocolMetrics(pool: Bytes, timestamp: BigInt, retiredAmount: BigInt): void {
const token = new PoolTokenFactory().getTokenForAddress(Address.fromString(pool))
const token = new PoolTokenFactory().getTokenForAddress(Address.fromBytes(pool))
CarbonMetricUtils.updateKlimaRetirements(token, timestamp, retiredAmount)
}
8 changes: 4 additions & 4 deletions polygon-digital-carbon/src/utils/DailyKlimaRetireSnapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ export function loadOrCreateDailyKlimaRetireSnapshot(id: string): DailyKlimaReti
if (retire == null) {
retire = new DailyKlimaRetireSnapshot(id)
retire.timestamp = BigInt.zero()
retire.pool = ""
retire.credit = new Bytes()
retire.amount = BigDecimal.fromString('0')
retire.feeAmount = BigDecimal.fromString('0')
retire.pool = new Bytes(0x0)
retire.credit = new Bytes(0x0)
retire.amount = BigInt.fromString('0')
retire.feeAmount = BigInt.fromString('0')
}

return retire as DailyKlimaRetireSnapshot
Expand Down
4 changes: 4 additions & 0 deletions polygon-digital-carbon/src/utils/KlimaRetire.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,7 @@ export function saveKlimaRetire(
}
return klimaRetire as KlimaRetire
}

export function loadKlimaRetire(id: String): KlimaRetire | null {
return KlimaRetire.load(Bytes.fromUTF8(id))
}

0 comments on commit 717a372

Please sign in to comment.