Skip to content

Commit

Permalink
Adjust Historical Activity Amounts to Reflect ICR migration (#123)
Browse files Browse the repository at this point in the history
* historical amount handling

* handle historical activity migration amounts before migration
  • Loading branch information
psparacino authored Apr 3, 2024
1 parent 40e5330 commit 0b0c16b
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 10 deletions.
29 changes: 20 additions & 9 deletions carbonmark/src/Carbonmark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ import {
loadOrCreateProject,
loadOrCreatePurchase,
loadOrCreateUser,
loadProject,
} from './Entities'
import { ZERO_BI } from '../../lib/utils/Decimals'
import { ZERO_BI, handleMigrationDecimals } from '../../lib/utils/Decimals'
import { ZERO_ADDRESS } from '../../lib/utils/Constants'
import { ERC20 } from '../generated/Carbonmark/ERC20'
import { ERC1155 } from '../generated/Carbonmark/ERC1155'
import { Bytes, log } from '@graphprotocol/graph-ts'

export function handleListingCreated(event: ListingCreated): void {
let blockNumber = event.block.number
// Ensure the user entity exists
loadOrCreateUser(event.params.account)
loadOrCreateUser(event.transaction.from)
Expand Down Expand Up @@ -53,7 +55,7 @@ export function handleListingCreated(event: ListingCreated): void {
listing.save()

let activity = loadOrCreateActivity(event.transaction.hash.toHexString().concat('ListingCreated'))
activity.amount = event.params.amount
activity.amount = handleMigrationDecimals(project.registry, blockNumber, event.params.amount)
activity.price = event.params.price
activity.timeStamp = event.block.timestamp
activity.activityType = 'CreatedListing'
Expand All @@ -65,13 +67,19 @@ export function handleListingCreated(event: ListingCreated): void {
}

export function handleListingUpdated(event: ListingUpdated): void {
let blockNumber = event.block.number
// User should already exist from creating the listing.

let listing = loadOrCreateListing(event.params.id.toHexString())
let activity = loadOrCreateActivity(event.transaction.hash.toHexString().concat('ListingUpdated'))

// always ensure the minFillAmount is updated
listing.minFillAmount = event.params.newMinFillAmount
let project = loadProject(listing.project)

// always ensure the minFillAmount is updated
listing.minFillAmount = event.params.newMinFillAmount

// only handling historical activity amounts for ICR migration
activity.amount = handleMigrationDecimals(project.registry, blockNumber, event.params.newAmount)

if (event.params.oldAmount != event.params.newAmount) {
listing.totalAmountToSell = event.params.newAmount
Expand All @@ -82,7 +90,7 @@ export function handleListingUpdated(event: ListingUpdated): void {
activity.activityType = 'UpdatedQuantity'
activity.project = listing.project
activity.user = event.transaction.from
activity.previousAmount = event.params.oldAmount
activity.previousAmount = handleMigrationDecimals(project.registry, blockNumber, event.params.oldAmount)
activity.amount = event.params.newAmount
activity.timeStamp = event.block.timestamp
activity.seller = listing.seller
Expand Down Expand Up @@ -136,21 +144,24 @@ export function handleListingUpdated(event: ListingUpdated): void {
}

export function handleListingFilled(event: ListingFilled): void {
let blockNumber = event.block.number
// Ensure the buyer user entity exists
loadOrCreateUser(event.transaction.from)

let listing = loadOrCreateListing(event.params.id.toHexString())
let buyerActivty = loadOrCreateActivity(event.transaction.hash.toHexString().concat('Purchase'))
let sellerActivity = loadOrCreateActivity(event.transaction.hash.toHexString().concat('Sold'))

listing.leftToSell = listing.leftToSell.minus(event.params.amount)
let amount = handleMigrationDecimals(loadProject(listing.project).registry, blockNumber, event.params.amount)

listing.leftToSell = listing.leftToSell.minus(amount)
if (listing.leftToSell == ZERO_BI) {
listing.active = false
}
listing.updatedAt = event.block.timestamp
listing.save()

buyerActivty.amount = event.params.amount
buyerActivty.amount = amount
buyerActivty.price = listing.singleUnitPrice
buyerActivty.timeStamp = event.block.timestamp
buyerActivty.activityType = 'Purchase'
Expand All @@ -161,7 +172,7 @@ export function handleListingFilled(event: ListingFilled): void {
buyerActivty.buyer = event.transaction.from
buyerActivty.save()

sellerActivity.amount = event.params.amount
sellerActivity.amount = amount
sellerActivity.price = listing.singleUnitPrice
sellerActivity.timeStamp = event.block.timestamp
sellerActivity.activityType = 'Sold'
Expand All @@ -174,7 +185,7 @@ export function handleListingFilled(event: ListingFilled): void {

let purchase = loadOrCreatePurchase(event.transaction.hash)
purchase.price = listing.singleUnitPrice
purchase.amount = event.params.amount
purchase.amount = amount
purchase.timeStamp = event.block.timestamp
purchase.user = event.transaction.from
purchase.listing = listing.id
Expand Down
8 changes: 8 additions & 0 deletions carbonmark/src/Entities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,14 @@ export function loadOrCreatePurchase(id: Bytes): Purchase {
return purchase
}

export function loadProject(projectId: string): Project {
let project = Project.load(projectId)
if (project == null) {
throw new Error('Project does not exist')
}
return project
}

function createCountry(id: string): void {
let country = Country.load(id)
if (country == null) {
Expand Down
2 changes: 2 additions & 0 deletions lib/utils/Constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ export const NFT_CO2COMPOUND_INIT_TIMESTAMP = BigInt.fromString('1638486000') //

export const ZERO_ADDRESS = Address.fromString('0x0000000000000000000000000000000000000000')

export const ICR_MIGRATION_BLOCK = 55190341;

// Klima Infinity Addresses
export const KLIMA_CARBON_RETIREMENTS_CONTRACT = Address.fromString('0xac298cd34559b9acfaedea8344a977eceff1c0fd')
export const KLIMA_INFINITY_DIAMOND = Address.fromString('0x8cE54d9625371fb2a068986d32C85De8E6e995f8')
Expand Down
19 changes: 18 additions & 1 deletion lib/utils/Decimals.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { BigDecimal, BigInt } from '@graphprotocol/graph-ts'
import { BigDecimal, BigInt, log } from '@graphprotocol/graph-ts'
import { ICR_MIGRATION_BLOCK } from './Constants'

export const DEFAULT_DECIMALS = 18

Expand Down Expand Up @@ -30,3 +31,19 @@ export function toDecimal(value: BigInt, decimals: number = DEFAULT_DECIMALS): B

return value.divDecimal(precision)
}

export function toWei(value: BigInt): BigInt {
let weiDecimals: BigInt = BigInt.fromI32(10).pow(DEFAULT_DECIMALS as u8)
return value.times(weiDecimals)
}

export function handleMigrationDecimals(registry: string, blockNumber: BigInt, amount: BigInt): BigInt {
log.info('qwe1: {} qwe2: {} qwe3: {}', [blockNumber.toString(), BigInt.fromI32(ICR_MIGRATION_BLOCK).toString(), registry])

if (registry == 'ICR' && blockNumber.lt(BigInt.fromI32(ICR_MIGRATION_BLOCK))) {
log.info('qwe4: {}', [toWei(amount).toString()])
return toWei(amount)
} else {
return amount
}
}

0 comments on commit 0b0c16b

Please sign in to comment.