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

Expand matchstick suite #198

Closed
wants to merge 13 commits into from
40 changes: 40 additions & 0 deletions polygon-digital-carbon/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,46 @@ type KlimaRetire @entity {
specific: Boolean!
}

type DailyKlimaRetireSnapshot @entity {
id: ID!
timestamp: BigInt!
pool: Bytes!
credit: CarbonCredit!
amount: BigInt!
feeAmount: BigInt!
}

type CarbonMetric @entity {
id: ID!
timestamp: BigInt!
bctSupply: BigDecimal!
nctSupply: BigDecimal!
mco2Supply: BigDecimal!
cco2Supply: BigDecimal!
uboSupply: BigDecimal!
nboSupply: BigDecimal!
bctRedeemed: BigDecimal!
nctRedeemed: BigDecimal!
uboRedeemed: BigDecimal!
nboRedeemed: BigDecimal!
totalCarbonSupply: BigDecimal!
bctCrosschainSupply: BigDecimal!
nctCrosschainSupply: BigDecimal!
totalCrosschainSupply: BigDecimal!
tco2Retired: BigDecimal!
mco2Retired: BigDecimal!
cco2Retired: BigDecimal!
c3tRetired: BigDecimal!
totalRetirements: BigDecimal!
bctKlimaRetired: BigDecimal!
nctKlimaRetired: BigDecimal!
mco2KlimaRetired: BigDecimal!
cco2KlimaRetired: BigDecimal!
uboKlimaRetired: BigDecimal!
nboKlimaRetired: BigDecimal!
totalKlimaRetirements: BigDecimal!
}

##############################
#### Bridge Specific Data ####
##############################
Expand Down
66 changes: 62 additions & 4 deletions polygon-digital-carbon/src/KlimaAggregator.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import { KlimaRetire, DailyKlimaRetireSnapshot } from '../generated/schema'
import { MossRetired } from '../generated/RetireMossCarbon/RetireMossCarbon'
import { ToucanRetired } from '../generated/RetireToucanCarbon/RetireToucanCarbon'
import { C3Retired } from '../generated/RetireC3Carbon/RetireC3Carbon'
import { CarbonRetired, CarbonRetired1 as CarbonRetiredTokenId } from '../generated/KlimaInfinity/KlimaInfinity'
import { KlimaCarbonRetirements } from '../generated/RetireC3Carbon/KlimaCarbonRetirements'
import { BigInt, dataSource } from '@graphprotocol/graph-ts'
import { Address, BigInt, Bytes, dataSource } from '@graphprotocol/graph-ts'
import { CarbonMetricUtils } from './utils/CarbonMetrics'
import { PoolTokenFactory } from './utils/pool_token/PoolTokenFactory'
import { loadOrCreateAccount } from './utils/Account'
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 All @@ -34,6 +39,7 @@ export function handleMossRetired(event: MossRetired): void {
retire.beneficiaryName = event.params.beneficiaryString
retire.retiringAddress = event.params.retiringAddress
retire.retirementMessage = event.params.retirementMessage

retire.save()

saveKlimaRetire(
Expand Down Expand Up @@ -142,9 +148,22 @@ export function handleCarbonRetired(event: CarbonRetired): void {
event.params.beneficiaryAddress,
retire.id,
index,
event.params.retiredAmount.div(BigInt.fromI32(100)),
event.params.retiredAmount.div(BigInt.fromI32(100)), // hard-coded 1% fee
false
)

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

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

}

export function handleCarbonRetiredWithTokenId(event: CarbonRetiredTokenId): void {
Expand Down Expand Up @@ -175,7 +194,46 @@ export function handleCarbonRetiredWithTokenId(event: CarbonRetiredTokenId): voi
event.params.beneficiaryAddress,
retire.id,
index,
event.params.retiredAmount.div(BigInt.fromI32(100)),
event.params.retiredAmount.div(BigInt.fromI32(100)), // hard-coded 1% fee
false
)

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

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

}

function generateDailyKlimaRetirement(klimaRetire: KlimaRetire): DailyKlimaRetireSnapshot | null {
const retire = loadRetire(klimaRetire.retire)
const dayTimestamp = dayTimestampString(retire.timestamp)
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
}

return null

}

function updateKlimaRetirementProtocolMetrics(pool: Bytes, timestamp: BigInt, retiredAmount: BigInt): void {
const token = new PoolTokenFactory().getTokenForAddress(Address.fromBytes(pool))
CarbonMetricUtils.updateKlimaRetirements(token, timestamp, retiredAmount)
}
110 changes: 110 additions & 0 deletions polygon-digital-carbon/src/utils/CarbonMetrics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import { BigInt, BigDecimal } from '@graphprotocol/graph-ts'
import { CarbonMetric } from '../../generated/schema'
import { dayTimestamp as dayTimestampString } from '../../../lib/utils/Dates'
import { IPoolToken } from './pool_token/IPoolToken'
import { ICarbonToken } from './carbon_token/ICarbonToken'
import * as constants from './Constants'

export class CarbonMetricUtils {
private static DAY_IN_SECONDS: BigInt = BigInt.fromString('86400')
private static INIT_TIMESTAMP: BigInt = BigInt.fromString(constants.METRICS_INIT_TIMESTAMP)

static updatePoolTokenSupply(poolToken: IPoolToken, timestamp: BigInt): void {
let carbonMetrics = this.loadCarbonMetrics(timestamp)
carbonMetrics = poolToken.returnUpdatedSupplyMetrics(carbonMetrics)

carbonMetrics.save()
}

static updateCrosschainTokenSupply(poolToken: IPoolToken, timestamp: BigInt, amount: BigInt): void {
let carbonMetrics = this.loadCarbonMetrics(timestamp)
carbonMetrics = poolToken.returnUpdatedCrosschainSupplyMetrics(carbonMetrics, amount)

carbonMetrics.save()
}

static updateKlimaRetirements(poolToken: IPoolToken, timestamp: BigInt, amount: BigInt): void {
let carbonMetrics = this.loadCarbonMetrics(timestamp)
carbonMetrics = poolToken.returnUpdatedKlimaRetirementMetrics(carbonMetrics, amount)

carbonMetrics.save()
}

static updatePoolTokenRedemptions(poolToken: IPoolToken, timestamp: BigInt, amount: BigInt): void {
let carbonMetrics = this.loadCarbonMetrics(timestamp)
carbonMetrics = poolToken.returnUpdatedRedemptionMetrics(carbonMetrics, amount)

carbonMetrics.save()
}

static updateCarbonTokenRetirements(carbonToken: ICarbonToken, timestamp: BigInt, amount: BigInt): void {
let carbonMetrics = this.loadCarbonMetrics(timestamp)
carbonMetrics = carbonToken.returnUpdatedRetirementMetrics(carbonMetrics, amount)

carbonMetrics.save()
}

private static loadCarbonMetrics(timestamp: BigInt): CarbonMetric {
const dayTimestamp = dayTimestampString(timestamp)
let carbonMetrics = CarbonMetric.load(dayTimestamp)
if (carbonMetrics == null) {
carbonMetrics = this.getTheMostRecentCarbonMetric(timestamp)
carbonMetrics.id = dayTimestamp
carbonMetrics.timestamp = BigInt.fromString(dayTimestamp)
}

return carbonMetrics
}

private static getTheMostRecentCarbonMetric(timestamp: BigInt): CarbonMetric {
const prevTimestamp = timestamp.minus(this.DAY_IN_SECONDS)
const prevDayString = dayTimestampString(prevTimestamp)
if (prevTimestamp.lt(this.INIT_TIMESTAMP)) {
return this.createAndReturnEmptyCarbonMetrics(prevDayString)
} else {
const carbonMetric = CarbonMetric.load(prevDayString)
if (carbonMetric != null) {
return carbonMetric
} else {
return this.getTheMostRecentCarbonMetric(prevTimestamp)
}
}
}

private static createAndReturnEmptyCarbonMetrics(id: string): CarbonMetric {
let carbonMetrics = new CarbonMetric(id)
carbonMetrics.timestamp = BigInt.zero()
carbonMetrics.bctSupply = BigDecimal.zero()
carbonMetrics.nctSupply = BigDecimal.zero()
carbonMetrics.mco2Supply = BigDecimal.zero()
carbonMetrics.cco2Supply = BigDecimal.zero()
carbonMetrics.uboSupply = BigDecimal.zero()
carbonMetrics.nboSupply = BigDecimal.zero()
carbonMetrics.bctRedeemed = BigDecimal.zero()
carbonMetrics.nctRedeemed = BigDecimal.zero()
carbonMetrics.uboRedeemed = BigDecimal.zero()
carbonMetrics.nboRedeemed = BigDecimal.zero()
carbonMetrics.totalCarbonSupply = BigDecimal.zero()

carbonMetrics.bctCrosschainSupply = BigDecimal.zero()
carbonMetrics.nctCrosschainSupply = BigDecimal.zero()
carbonMetrics.totalCrosschainSupply = BigDecimal.zero()

carbonMetrics.tco2Retired = BigDecimal.zero()
carbonMetrics.mco2Retired = BigDecimal.zero()
carbonMetrics.cco2Retired = BigDecimal.zero()
carbonMetrics.c3tRetired = BigDecimal.zero()

carbonMetrics.totalRetirements = BigDecimal.zero()
carbonMetrics.bctKlimaRetired = BigDecimal.zero()
carbonMetrics.nctKlimaRetired = BigDecimal.zero()
carbonMetrics.cco2KlimaRetired = BigDecimal.zero()
carbonMetrics.mco2KlimaRetired = BigDecimal.zero()
carbonMetrics.uboKlimaRetired = BigDecimal.zero()
carbonMetrics.nboKlimaRetired = BigDecimal.zero()
carbonMetrics.totalKlimaRetirements = BigDecimal.zero()
carbonMetrics.save()

return carbonMetrics
}
}
20 changes: 20 additions & 0 deletions polygon-digital-carbon/src/utils/Constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//Token Addresses
export const BCT_TOKEN: string = 'BCT'
export const BCT_ERC20_CONTRACT: string = '0x2f800db0fdb5223b3c3f354886d907a671414a7f'
export const NCT_TOKEN: string = 'NCT'
export const NCT_ERC20_CONTRACT: string = '0xd838290e877e0188a4a44700463419ed96c16107'
export const MCO2_TOKEN: string = 'MCO2'
export const MCO2_ERC20_CONTRACT: string = '0xaa7dbd1598251f856c12f63557a4c4397c253cea'
export const UBO_TOKEN: string = 'UBO'
export const UBO_ERC20_CONTRACT: string = '0x2b3ecb0991af0498ece9135bcd04013d7993110c'
export const NBO_TOKEN: string = 'NBO'
export const NBO_ERC20_CONTRACT: string = '0x6bca3b77c1909ce1a4ba1a20d1103bde8d222e48'
export const CCO2_TOKEN: string = 'CCO2'
export const CCO2_ERC20_CONTRACT: string = '0x82B37070e43C1BA0EA9e2283285b674eF7f1D4E2'

// Klima Retirement Contracts
export const KLIMA_CARBON_RETIREMENTS_CONTRACT = '0xac298cd34559b9acfaedea8344a977eceff1c0fd'
export const KLIMA_INFINITY_DIAMOND = '0x8cE54d9625371fb2a068986d32C85De8E6e995f8'

//Metrics init timestamp - 10th of Oct 2021 (Day of BCT ERC20 contract creation)
export const METRICS_INIT_TIMESTAMP = '1633824000'
16 changes: 16 additions & 0 deletions polygon-digital-carbon/src/utils/DailyKlimaRetireSnapshot.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { BigDecimal, BigInt, Bytes } from '@graphprotocol/graph-ts'
import { DailyKlimaRetireSnapshot } from '../../generated/schema'

export function loadOrCreateDailyKlimaRetireSnapshot(id: string): DailyKlimaRetireSnapshot {
let retire = DailyKlimaRetireSnapshot.load(id)
if (retire == null) {
retire = new DailyKlimaRetireSnapshot(id)
retire.timestamp = BigInt.zero()
retire.pool = new Bytes(0x0)
retire.credit = new Bytes(0x0)
retire.amount = BigInt.fromString('0')
retire.feeAmount = BigInt.fromString('0')
}

return retire as DailyKlimaRetireSnapshot
}
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))
}
7 changes: 7 additions & 0 deletions polygon-digital-carbon/src/utils/carbon_token/ICarbonToken.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { BigDecimal, BigInt } from '@graphprotocol/graph-ts'
import { CarbonMetric } from '../../../generated/schema'

export interface ICarbonToken {
getDecimals(): number
returnUpdatedRetirementMetrics(carbonMetrics: CarbonMetric, amount: BigInt): CarbonMetric
}
17 changes: 17 additions & 0 deletions polygon-digital-carbon/src/utils/carbon_token/impl/C3T.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { BigDecimal, BigInt } from '@graphprotocol/graph-ts'
import { toDecimal } from '../../../../../lib/utils/Decimals'
import { CarbonMetric } from '../../../../generated/schema'
import { ICarbonToken } from '../ICarbonToken'

export class C3T implements ICarbonToken {
getDecimals(): number {
return 18
}

returnUpdatedRetirementMetrics(carbonMetrics: CarbonMetric, amount: BigInt): CarbonMetric {
carbonMetrics.c3tRetired = carbonMetrics.c3tRetired.plus(toDecimal(amount, this.getDecimals()))
carbonMetrics.totalRetirements = carbonMetrics.totalRetirements.plus(toDecimal(amount, this.getDecimals()))

return carbonMetrics
}
}
17 changes: 17 additions & 0 deletions polygon-digital-carbon/src/utils/carbon_token/impl/MCO2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { BigDecimal, BigInt } from '@graphprotocol/graph-ts'
import { toDecimal } from '../../../../../lib/utils/Decimals'
import { CarbonMetric } from '../../../../generated/schema'
import { ICarbonToken } from '../ICarbonToken'

export class MCO2 implements ICarbonToken {
getDecimals(): number {
return 18
}

returnUpdatedRetirementMetrics(carbonMetrics: CarbonMetric, amount: BigInt): CarbonMetric {
carbonMetrics.mco2Retired = carbonMetrics.mco2Retired.plus(toDecimal(amount, this.getDecimals()))
carbonMetrics.totalRetirements = carbonMetrics.totalRetirements.plus(toDecimal(amount, this.getDecimals()))

return carbonMetrics
}
}
17 changes: 17 additions & 0 deletions polygon-digital-carbon/src/utils/carbon_token/impl/TCO2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { BigDecimal, BigInt } from '@graphprotocol/graph-ts'
import { toDecimal } from '../../../../../lib/utils/Decimals'
import { CarbonMetric } from '../../../../generated/schema'
import { ICarbonToken } from '../ICarbonToken'

export class TCO2 implements ICarbonToken {
getDecimals(): number {
return 18
}

returnUpdatedRetirementMetrics(carbonMetrics: CarbonMetric, amount: BigInt): CarbonMetric {
carbonMetrics.tco2Retired = carbonMetrics.tco2Retired.plus(toDecimal(amount, this.getDecimals()))
carbonMetrics.totalRetirements = carbonMetrics.totalRetirements.plus(toDecimal(amount, this.getDecimals()))

return carbonMetrics
}
}
10 changes: 10 additions & 0 deletions polygon-digital-carbon/src/utils/pool_token/IPoolToken.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { BigDecimal, BigInt } from '@graphprotocol/graph-ts'
import { CarbonMetric } from '../../../generated/schema'

export interface IPoolToken {
getDecimals(): number
returnUpdatedSupplyMetrics(carbonMetrics: CarbonMetric): CarbonMetric
returnUpdatedRedemptionMetrics(carbonMetrics: CarbonMetric, amount: BigInt): CarbonMetric
returnUpdatedCrosschainSupplyMetrics(carbonMetrics: CarbonMetric, amountRaw: BigInt): CarbonMetric
returnUpdatedKlimaRetirementMetrics(carbonMetrics: CarbonMetric, amount: BigInt): CarbonMetric
}
Loading
Loading