From b7be18806a2e93e8cec60a43e6730ccd3be56908 Mon Sep 17 00:00:00 2001 From: stadolf Date: Mon, 16 Oct 2023 12:33:02 +0200 Subject: [PATCH] extracts generic functions drops duplicate abi generation adds a dummy definition for local builds Signed-off-by: stadolf --- subgraph/abis/IPNFT.json | 13 + subgraph/config/local.js | 4 + subgraph/src/common.ts | 45 ++++ subgraph/src/crowdSaleMapping.ts | 224 +++--------------- subgraph/src/genericCrowdSale.ts | 152 ++++++++++++ subgraph/src/stakedLockingCrowdSaleMapping.ts | 104 +++----- subgraph/subgraph.template.yaml | 2 - 7 files changed, 273 insertions(+), 271 deletions(-) create mode 100644 subgraph/src/common.ts create mode 100644 subgraph/src/genericCrowdSale.ts diff --git a/subgraph/abis/IPNFT.json b/subgraph/abis/IPNFT.json index 10746c1d..3e07646d 100644 --- a/subgraph/abis/IPNFT.json +++ b/subgraph/abis/IPNFT.json @@ -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": [ diff --git a/subgraph/config/local.js b/subgraph/config/local.js index 550ba5dc..f10bb6d1 100644 --- a/subgraph/config/local.js +++ b/subgraph/config/local.js @@ -18,6 +18,10 @@ module.exports = { address: process.env.TOKENIZER_ADDRESS, startBlock: 0 }, + crowdSale: { + address: '0xbaadf00dbaadf00dbaadf00dbaadf00dbaadf00d', + startBlock: 0 + }, stakedLockingCrowdSale: { address: process.env.STAKED_LOCKING_CROWDSALE_ADDRESS, startBlock: 0 diff --git a/subgraph/src/common.ts b/subgraph/src/common.ts new file mode 100644 index 00000000..3086d385 --- /dev/null +++ b/subgraph/src/common.ts @@ -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 +} diff --git a/subgraph/src/crowdSaleMapping.ts b/subgraph/src/crowdSaleMapping.ts index 689ed9b7..b3a29dee 100644 --- a/subgraph/src/crowdSaleMapping.ts +++ b/subgraph/src/crowdSaleMapping.ts @@ -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()) @@ -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) } /** @@ -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 ) @@ -263,7 +99,7 @@ export function handleClaimedSuccessfulSale( export function handleClaimedFailedSale( event: ClaimedAuctionTokensEvent ): void { - handleClaimedFailedSaleGeneric( + GenericCrowdSale.handleClaimedFailedSale( event.params.saleId.toString(), event.block.timestamp ) diff --git a/subgraph/src/genericCrowdSale.ts b/subgraph/src/genericCrowdSale.ts new file mode 100644 index 00000000..6b7fc6b3 --- /dev/null +++ b/subgraph/src/genericCrowdSale.ts @@ -0,0 +1,152 @@ +import { Address, BigInt, ethereum, log } from '@graphprotocol/graph-ts' + +// 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 + } +} + +import { Contribution, CrowdSale } from '../generated/schema' + +export function handleSettled(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 handleFailed(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 handleBid(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 handleClaimed(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 handleClaimedSuccessfulSale( + 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 handleClaimedFailedSale( + 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() +} diff --git a/subgraph/src/stakedLockingCrowdSaleMapping.ts b/subgraph/src/stakedLockingCrowdSaleMapping.ts index 72279f7b..0562e46e 100644 --- a/subgraph/src/stakedLockingCrowdSaleMapping.ts +++ b/subgraph/src/stakedLockingCrowdSaleMapping.ts @@ -2,68 +2,22 @@ import { BigInt, Bytes, DataSourceContext, log } from '@graphprotocol/graph-ts' import { IERC20Metadata } from '../generated/CrowdSale/IERC20Metadata' import { Bid as BidEvent, - Staked as StakedEvent, - Settled as SettledEvent, - Started as StartedEvent, - Failed as FailedEvent, + ClaimedAuctionTokens as ClaimedAuctionTokensEvent, Claimed as ClaimedEvent, + ClaimedFundingGoal as ClaimedFundingGoalEvent, ClaimedStakes as ClaimedStakesEvent, + Failed as FailedEvent, LockingContractCreated as LockingContractCreatedEvent, - ClaimedAuctionTokens as ClaimedAuctionTokensEvent, - ClaimedFundingGoal as ClaimedFundingGoalEvent + Settled as SettledEvent, + Staked as StakedEvent, + Started as StartedEvent } from '../generated/StakedLockingCrowdSale/StakedLockingCrowdSale' +import * as GenericCrowdSale from './genericCrowdSale' -import { - makeERC20Token, - handleSettledGeneric, - handleFailedGeneric, - handleClaimedFailedSaleGeneric, - handleClaimedSuccessfulSaleGeneric, - handleBidGeneric, - handleClaimedGeneric, - BidEventParams, - ClaimedEventParams -} from './crowdSaleMapping' - -import { - Contribution, - CrowdSale, - ERC20Token, - IPT, - TimelockedToken -} from '../generated/schema' +import { Contribution, CrowdSale, ERC20Token, IPT } from '../generated/schema' import { TimelockedToken as TimelockedTokenTemplate } from '../generated/templates' - -// Helpers - -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 -} - -// Actual Event handlers +import { makeERC20Token, makeTimelockedToken } from './common' export function handleStarted(event: StartedEvent): void { let crowdSale = new CrowdSale(event.params.saleId.toString()) @@ -127,11 +81,11 @@ 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 handleLockingContractCreated( @@ -158,14 +112,14 @@ export function handleLockingContractCreated( } 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 handleStaked(event: StakedEvent): void { @@ -212,16 +166,16 @@ export function handleStaked(event: StakedEvent): void { } 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) } export function handleClaimedStakes(event: ClaimedStakesEvent): void { @@ -247,7 +201,7 @@ export function handleClaimedStakes(event: ClaimedStakesEvent): void { export function handleClaimedSuccessfulSale( event: ClaimedFundingGoalEvent ): void { - handleClaimedSuccessfulSaleGeneric( + GenericCrowdSale.handleClaimedSuccessfulSale( event.params.saleId.toString(), event.block.timestamp ) @@ -259,7 +213,7 @@ export function handleClaimedSuccessfulSale( export function handleClaimedFailedSale( event: ClaimedAuctionTokensEvent ): void { - handleClaimedFailedSaleGeneric( + GenericCrowdSale.handleClaimedFailedSale( event.params.saleId.toString(), event.block.timestamp ) diff --git a/subgraph/subgraph.template.yaml b/subgraph/subgraph.template.yaml index 691e9012..95071b25 100644 --- a/subgraph/subgraph.template.yaml +++ b/subgraph/subgraph.template.yaml @@ -136,8 +136,6 @@ dataSources: abis: - name: StakedLockingCrowdSale file: ./abis/StakedLockingCrowdSale.json - - name: IERC20Metadata - file: ./abis/IERC20Metadata.json eventHandlers: - event: Started(indexed uint256,indexed address,(address,address,address,uint256,uint256,uint64,address),(address,address,uint256),address,uint256,uint256) handler: handleStarted