|
| 1 | +import { |
| 2 | + BigInt, |
| 3 | + Bytes, |
| 4 | + DataSourceContext, |
| 5 | + log, |
| 6 | + ethereum |
| 7 | +} from '@graphprotocol/graph-ts' |
| 8 | +import { IERC20Metadata } from '../generated/CrowdSale/IERC20Metadata' |
| 9 | + |
| 10 | +import { Started as PlainStartedEvent } from '../generated/CrowdSale/CrowdSale' |
| 11 | + |
| 12 | +import { handleStarted as plainHandleStarted } from './crowdSaleMapping' |
| 13 | + |
| 14 | +import * as GenericCrowdSale from './genericCrowdSale' |
| 15 | + |
| 16 | +import { Contribution, CrowdSale, ERC20Token, IPT } from '../generated/schema' |
| 17 | + |
| 18 | +import { TimelockedToken as TimelockedTokenTemplate } from '../generated/templates' |
| 19 | +import { makeERC20Token, makeTimelockedToken } from './common' |
| 20 | + |
| 21 | +import { |
| 22 | + Bid as BidEvent, |
| 23 | + ClaimedAuctionTokens as ClaimedAuctionTokensEvent, |
| 24 | + Claimed as ClaimedEvent, |
| 25 | + ClaimedFundingGoal as ClaimedFundingGoalEvent, |
| 26 | + Failed as FailedEvent, |
| 27 | + LockingContractCreated as LockingContractCreatedEvent, |
| 28 | + Settled as SettledEvent, |
| 29 | + Started as StartedEvent |
| 30 | +} from '../generated/LockingCrowdSale/LockingCrowdSale' |
| 31 | + |
| 32 | +export function handleStarted(event: StartedEvent): void { |
| 33 | + const _plain = new PlainStartedEvent( |
| 34 | + event.address, |
| 35 | + event.logIndex, |
| 36 | + event.transactionLogIndex, |
| 37 | + event.logType, |
| 38 | + event.block, |
| 39 | + event.transaction, |
| 40 | + [ |
| 41 | + event.parameters[0], |
| 42 | + event.parameters[1], |
| 43 | + event.parameters[2], |
| 44 | + event.parameters[5] |
| 45 | + ], |
| 46 | + event.receipt |
| 47 | + ) |
| 48 | + |
| 49 | + plainHandleStarted(_plain) |
| 50 | + |
| 51 | + let crowdSale = CrowdSale.load(event.params.saleId.toString()) |
| 52 | + if (!crowdSale) { |
| 53 | + log.error('[Crowdsale] Creation failed for: {}', [ |
| 54 | + event.params.saleId.toHexString() |
| 55 | + ]) |
| 56 | + return |
| 57 | + } |
| 58 | + |
| 59 | + let ipt = IPT.load(event.params.sale.auctionToken.toHexString()) |
| 60 | + if (!ipt) { |
| 61 | + log.error('[Crowdsale] Ipt not found for id: {}', [ |
| 62 | + event.params.sale.auctionToken.toHexString() |
| 63 | + ]) |
| 64 | + return |
| 65 | + } |
| 66 | + |
| 67 | + if (!ipt.lockedToken) { |
| 68 | + ipt.lockedToken = event.params.lockingToken |
| 69 | + ipt.save() |
| 70 | + } else { |
| 71 | + let _ipt = changetype<Bytes>(ipt.lockedToken).toHexString() |
| 72 | + let _newToken = event.params.lockingToken.toHexString() |
| 73 | + if (_ipt != _newToken) { |
| 74 | + log.error('the locking token per IPT should be unique {} != {}', [ |
| 75 | + _ipt, |
| 76 | + _newToken |
| 77 | + ]) |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + crowdSale.amountStaked = BigInt.fromU32(0) |
| 82 | + crowdSale.auctionLockingDuration = event.params.lockingDuration |
| 83 | + |
| 84 | + crowdSale.type = 'STAKED_LOCKING_CROWDSALE' |
| 85 | + crowdSale.save() |
| 86 | + log.info('[handleStarted] locking crowdsale {}', [crowdSale.id]) |
| 87 | +} |
| 88 | + |
| 89 | +export function handleSettled(event: SettledEvent): void { |
| 90 | + GenericCrowdSale.handleSettled(event.params.saleId.toString()) |
| 91 | +} |
| 92 | + |
| 93 | +export function handleFailed(event: FailedEvent): void { |
| 94 | + GenericCrowdSale.handleFailed(event.params.saleId.toString()) |
| 95 | +} |
| 96 | + |
| 97 | +export function handleLockingContractCreated( |
| 98 | + event: LockingContractCreatedEvent |
| 99 | +): void { |
| 100 | + let context = new DataSourceContext() |
| 101 | + context.setBytes('ipt', event.params.underlyingToken) |
| 102 | + context.setBytes('lockingContract', event.params.lockingContract) |
| 103 | + TimelockedTokenTemplate.createWithContext( |
| 104 | + event.params.lockingContract, |
| 105 | + context |
| 106 | + ) |
| 107 | + const _underlyingTokenContract: IERC20Metadata = IERC20Metadata.bind( |
| 108 | + event.params.underlyingToken |
| 109 | + ) |
| 110 | + const underlyingErc20Token: ERC20Token = makeERC20Token( |
| 111 | + _underlyingTokenContract |
| 112 | + ) |
| 113 | + |
| 114 | + makeTimelockedToken( |
| 115 | + IERC20Metadata.bind(event.params.lockingContract), |
| 116 | + underlyingErc20Token |
| 117 | + ) |
| 118 | +} |
| 119 | + |
| 120 | +export function handleBid(event: BidEvent): void { |
| 121 | + GenericCrowdSale.handleBid( |
| 122 | + new GenericCrowdSale.BidEventParams( |
| 123 | + event.params.saleId, |
| 124 | + event.params.bidder, |
| 125 | + event.params.amount, |
| 126 | + event.block.timestamp |
| 127 | + ) |
| 128 | + ) |
| 129 | +} |
| 130 | + |
| 131 | +export function handleClaimed(event: ClaimedEvent): void { |
| 132 | + GenericCrowdSale.handleClaimed( |
| 133 | + new GenericCrowdSale.ClaimedEventParams( |
| 134 | + event.params.saleId, |
| 135 | + event.params.claimer, |
| 136 | + event.params.claimed, |
| 137 | + event.params.refunded, |
| 138 | + event.block.timestamp, |
| 139 | + event.transaction |
| 140 | + ) |
| 141 | + ) |
| 142 | +} |
| 143 | + |
| 144 | +/** |
| 145 | + * emitted when the auctioneer pulls / claims bidding tokens after the sale is successfully settled |
| 146 | + */ |
| 147 | +export function handleClaimedSuccessfulSale( |
| 148 | + event: ClaimedFundingGoalEvent |
| 149 | +): void { |
| 150 | + GenericCrowdSale.handleClaimedSuccessfulSale( |
| 151 | + event.params.saleId.toString(), |
| 152 | + event.block.timestamp |
| 153 | + ) |
| 154 | +} |
| 155 | + |
| 156 | +/** |
| 157 | + * emitted when the auctioneer pulls / claims back auction tokens after the sale has settled and is failed |
| 158 | + */ |
| 159 | +export function handleClaimedFailedSale( |
| 160 | + event: ClaimedAuctionTokensEvent |
| 161 | +): void { |
| 162 | + GenericCrowdSale.handleClaimedFailedSale( |
| 163 | + event.params.saleId.toString(), |
| 164 | + event.block.timestamp |
| 165 | + ) |
| 166 | +} |
0 commit comments