Skip to content

Commit

Permalink
extracts generic functions
Browse files Browse the repository at this point in the history
drops duplicate abi generation
adds a dummy definition for local builds

Signed-off-by: stadolf <[email protected]>
  • Loading branch information
elmariachi111 committed Oct 27, 2023
1 parent 0f8c75e commit 69bfecb
Show file tree
Hide file tree
Showing 6 changed files with 269 additions and 271 deletions.
13 changes: 13 additions & 0 deletions subgraph/abis/IPNFT.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,19 @@
"name": "ApprovalForAll",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": false,
"internalType": "address",
"name": "authorizer",
"type": "address"
}
],
"name": "AuthorizerUpdated",
"type": "event"
},
{
"anonymous": false,
"inputs": [
Expand Down
45 changes: 45 additions & 0 deletions subgraph/src/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { BigInt } from '@graphprotocol/graph-ts'
import { IERC20Metadata } from '../generated/CrowdSale/IERC20Metadata'

import { ERC20Token, IPT, TimelockedToken } from '../generated/schema'

export function makeTimelockedToken(
_contract: IERC20Metadata,
underlyingToken: ERC20Token
): TimelockedToken {
let token = TimelockedToken.load(_contract._address)

if (!token) {
token = new TimelockedToken(_contract._address)
token.id = _contract._address
token.decimals = BigInt.fromI32(_contract.decimals())
token.symbol = _contract.symbol()
token.name = _contract.name()
token.underlyingToken = underlyingToken.id

let ipt = IPT.load(underlyingToken.id.toHexString())
if (ipt) {
token.ipt = ipt.id
ipt.lockedToken = token.id
ipt.save()
}
token.save()
}

return token
}

export function makeERC20Token(_contract: IERC20Metadata): ERC20Token {
let token = ERC20Token.load(_contract._address)

if (!token) {
token = new ERC20Token(_contract._address)
token.id = _contract._address
token.decimals = BigInt.fromI32(_contract.decimals())
token.symbol = _contract.symbol()
token.name = _contract.name()
token.save()
}

return token
}
224 changes: 30 additions & 194 deletions subgraph/src/crowdSaleMapping.ts
Original file line number Diff line number Diff line change
@@ -1,182 +1,18 @@
import { BigInt, log, Address, ethereum } from '@graphprotocol/graph-ts'
import { IERC20Metadata } from '../generated/CrowdSale/IERC20Metadata'
import { BigInt, log } from '@graphprotocol/graph-ts'
import {
Started as StartedEvent,
Settled as SettledEvent,
Failed as FailedEvent,
Bid as BidEvent,
Claimed as ClaimedEvent,
ClaimedAuctionTokens as ClaimedAuctionTokensEvent,
ClaimedFundingGoal as ClaimedFundingGoalEvent
Claimed as ClaimedEvent,
ClaimedFundingGoal as ClaimedFundingGoalEvent,
Failed as FailedEvent,
Settled as SettledEvent,
Started as StartedEvent
} from '../generated/CrowdSale/CrowdSale'
import { IERC20Metadata } from '../generated/CrowdSale/IERC20Metadata'

import { CrowdSale, ERC20Token, IPT, Contribution } from '../generated/schema'

// Helpers & Generic Handlers to handle different types of CrowdSales
export class BidEventParams {
saleId: BigInt
bidder: Address
amount: BigInt
blockTimestamp: BigInt

constructor(
saleId: BigInt,
bidder: Address,
amount: BigInt,
blockTimestamp: BigInt
) {
this.saleId = saleId
this.bidder = bidder
this.amount = amount
this.blockTimestamp = blockTimestamp
}
}

export class ClaimedEventParams {
saleId: BigInt
claimer: Address
claimed: BigInt
refunded: BigInt
blockTimestamp: BigInt
transaction: ethereum.Transaction

constructor(
saleId: BigInt,
claimer: Address,
claimed: BigInt,
refunded: BigInt,
blockTimestamp: BigInt,
transaction: ethereum.Transaction
) {
this.saleId = saleId
this.claimer = claimer
this.claimed = claimed
this.refunded = refunded
this.blockTimestamp = blockTimestamp
this.transaction = transaction
}
}

export function makeERC20Token(_contract: IERC20Metadata): ERC20Token {
let token = ERC20Token.load(_contract._address)

if (!token) {
token = new ERC20Token(_contract._address)
token.id = _contract._address
token.decimals = BigInt.fromI32(_contract.decimals())
token.symbol = _contract.symbol()
token.name = _contract.name()
token.save()
}

return token
}

export function handleSettledGeneric(saleId: string): void {
let crowdSale = CrowdSale.load(saleId)
if (!crowdSale) {
return log.error('[handleSettled] CrowdSale not found for id: {}', [saleId])
}
crowdSale.state = 'SETTLED'
crowdSale.save()
}

export function handleFailedGeneric(saleId: string): void {
let crowdSale = CrowdSale.load(saleId)
if (!crowdSale) {
return log.error('[handleFailed] CrowdSale not found for id: {}', [saleId])
}
crowdSale.state = 'FAILED'
crowdSale.save()
}

export function handleBidGeneric(params: BidEventParams): void {
let crowdSale = CrowdSale.load(params.saleId.toString())
if (!crowdSale) {
log.error('[HANDLEBID] CrowdSale not found for id: {}', [
params.saleId.toString()
])
return
}

// Update CrowdSale
crowdSale.amountRaised = crowdSale.amountRaised.plus(params.amount)
crowdSale.save()

let contributionId = params.saleId.toString() + '-' + params.bidder.toHex()

// Load or Create Contribution
let contribution = Contribution.load(contributionId)
if (!contribution) {
contribution = new Contribution(contributionId)
contribution.amount = BigInt.fromI32(0)
contribution.stakedAmount = BigInt.fromI32(0)
}

contribution.contributor = params.bidder
contribution.createdAt = params.blockTimestamp
contribution.amount = contribution.amount.plus(params.amount)
contribution.crowdSale = crowdSale.id

contribution.save()
}

export function handleClaimedGeneric(params: ClaimedEventParams): void {
let crowdSale = CrowdSale.load(params.saleId.toString())
if (!crowdSale) {
log.error('[HANDLECLAIMED] CrowdSale not found for id: {}', [
params.saleId.toString()
])
return
}

let contributionId = params.saleId.toString() + '-' + params.claimer.toHex()
// Load Contribution
let contribution = Contribution.load(contributionId)

if (contribution === null) {
log.error(
'[HANDLECLAIMED] No contribution found for CrowdSale | user : {} | {}',
[params.saleId.toString(), params.claimer.toHexString()]
)
return
}
contribution.claimedAt = params.blockTimestamp
contribution.claimedTx = params.transaction.hash.toHex()
contribution.claimedTokens = params.claimed
contribution.refundedTokens = params.refunded
contribution.save()
}

export function handleClaimedSuccessfulSaleGeneric(
saleId: string,
timestamp: BigInt
): void {
let crowdSale = CrowdSale.load(saleId)
if (!crowdSale) {
log.error('[handleClaimed] CrowdSale not found for id: {}', [saleId])
return
}
crowdSale.claimedAt = timestamp
crowdSale.save()
}

export function handleClaimedFailedSaleGeneric(
saleId: string,
timestamp: BigInt
): void {
let crowdSale = CrowdSale.load(saleId)
if (!crowdSale) {
log.error('[handleClaimedFailedSale] CrowdSale not found for id: {}', [
saleId
])
return
}
crowdSale.claimedAt = timestamp
crowdSale.save()
}

// Actual Event handlers
import { CrowdSale, IPT } from '../generated/schema'
import { makeERC20Token } from './common'
import * as GenericCrowdSale from './genericCrowdSale'

export function handleStarted(event: StartedEvent): void {
let crowdSale = new CrowdSale(event.params.saleId.toString())
Expand Down Expand Up @@ -214,35 +50,35 @@ export function handleStarted(event: StartedEvent): void {
}

export function handleSettled(event: SettledEvent): void {
handleSettledGeneric(event.params.saleId.toString())
GenericCrowdSale.handleSettled(event.params.saleId.toString())
}

export function handleFailed(event: FailedEvent): void {
handleFailedGeneric(event.params.saleId.toString())
GenericCrowdSale.handleFailed(event.params.saleId.toString())
}

export function handleBid(event: BidEvent): void {
let params: BidEventParams = new BidEventParams(
event.params.saleId,
event.params.bidder,
event.params.amount,
event.block.timestamp
GenericCrowdSale.handleBid(
new GenericCrowdSale.BidEventParams(
event.params.saleId,
event.params.bidder,
event.params.amount,
event.block.timestamp
)
)

handleBidGeneric(params)
}

export function handleClaimed(event: ClaimedEvent): void {
let params: ClaimedEventParams = new ClaimedEventParams(
event.params.saleId,
event.params.claimer,
event.params.claimed,
event.params.refunded,
event.block.timestamp,
event.transaction
GenericCrowdSale.handleClaimed(
new GenericCrowdSale.ClaimedEventParams(
event.params.saleId,
event.params.claimer,
event.params.claimed,
event.params.refunded,
event.block.timestamp,
event.transaction
)
)

handleClaimedGeneric(params)
}

/**
Expand All @@ -251,7 +87,7 @@ export function handleClaimed(event: ClaimedEvent): void {
export function handleClaimedSuccessfulSale(
event: ClaimedFundingGoalEvent
): void {
handleClaimedSuccessfulSaleGeneric(
GenericCrowdSale.handleClaimedSuccessfulSale(
event.params.saleId.toString(),
event.block.timestamp
)
Expand All @@ -263,7 +99,7 @@ export function handleClaimedSuccessfulSale(
export function handleClaimedFailedSale(
event: ClaimedAuctionTokensEvent
): void {
handleClaimedFailedSaleGeneric(
GenericCrowdSale.handleClaimedFailedSale(
event.params.saleId.toString(),
event.block.timestamp
)
Expand Down
Loading

0 comments on commit 69bfecb

Please sign in to comment.