diff --git a/schema.graphql b/schema.graphql index fdff6b5..fec3e38 100644 --- a/schema.graphql +++ b/schema.graphql @@ -2120,3 +2120,56 @@ type ClaimedExitQueueTicketSystemEvent implements SystemEvent @entity { createdAt: BigInt! createdAtBlock: BigInt! } + + +type ERC20DepositSystemEvent implements SystemEvent @entity { + id: String! + index: BigInt! + type: String! + + tx: Bytes! + who: Bytes! + + factory: vFactory + treasury: vTreasury + pool: vPool + withdrawalRecipient: vWithdrawalRecipient + execLayerRecipient: vExecLayerRecipient + coverageRecipient: vCoverageRecipient + oracleAggregator: vOracleAggregator + exitQueue: vExitQueue + + integration: ERC20! + depositedAmount: BigInt! + mintedShares: BigInt! + staker: Bytes! + + createdAt: BigInt! + createdAtBlock: BigInt! +} + +type ERC20ExitSystemEvent implements SystemEvent @entity { + id: String! + index: BigInt! + type: String! + + tx: Bytes! + who: Bytes! + + factory: vFactory + treasury: vTreasury + pool: vPool + withdrawalRecipient: vWithdrawalRecipient + execLayerRecipient: vExecLayerRecipient + coverageRecipient: vCoverageRecipient + oracleAggregator: vOracleAggregator + exitQueue: vExitQueue + + integration: ERC20! + exitedAmount: BigInt! + exitedShares: BigInt! + staker: Bytes! + + createdAt: BigInt! + createdAtBlock: BigInt! +} \ No newline at end of file diff --git a/src/ERC20.mapping.ts b/src/ERC20.mapping.ts index 1241520..536fef6 100644 --- a/src/ERC20.mapping.ts +++ b/src/ERC20.mapping.ts @@ -43,7 +43,9 @@ import { externalEntityUUID, getOrCreateUnassignedCommissionSold, _computeEthAfterCommission, - _computeIntegratorCommissionEarned + _computeIntegratorCommissionEarned, + createERC20DepositSystemEvent, + createERC20ExitSystemEvent } from './utils/utils'; import { CommissionSharesSold, ExitedCommissionShares } from '../generated/templates/ERC1155/Liquid1155'; import { getOrCreateBalance } from './vPool.mapping'; @@ -348,7 +350,7 @@ export function handleCommissionWithdrawn(event: CommissionWithdrawn): void { commission.editedAt = ts; commission.editedAtBlock = blockId; commission.save(); - + erc20!.editedAt = ts; erc20!.editedAtBlock = blockId; erc20!.save(); @@ -374,6 +376,21 @@ export function handleStake_1_0_0_rc4(event: Stake_1_0_0_rc4): void { deposit.depositAmount = BigInt.zero(); if (ucs.active && ucs.tx.equals(event.transaction.hash)) { deposit.depositAmount = deposit.depositAmount.plus(ucs.amount); + createERC20DepositSystemEvent( + event, + event.address, + event.params.staker, + event.params.ethValue.plus(ucs.amount), + event.params.sharesBought + ); + } else { + createERC20DepositSystemEvent( + event, + event.address, + event.params.staker, + event.params.ethValue, + event.params.sharesBought + ); } deposit.mintedShares = event.params.sharesBought; deposit.hash = event.transaction.hash; @@ -447,6 +464,8 @@ export function handleStake(event: Stake): void { const blockId = event.block.number; const staker = event.params.staker; + createERC20DepositSystemEvent(event, event.address, staker, event.params.depositedEth, event.params.mintedTokens); + const deposit = new ERC20Deposit(eventUUID(event, [event.address.toHexString(), staker.toHexString()])); deposit.integration = event.address; deposit.mintedShares = event.params.mintedTokens; @@ -511,18 +530,24 @@ export function handleExit(event: Exit): void { const details = event.params.exitDetails; const depositDataEntry = new ExitDataEntry(eventUUID(event, ['ExitDataEntry'])); depositDataEntry.exitedEth = BigInt.zero(); + + let totalETH = BigInt.zero(); for (let idx = 0; idx < details.length; ++idx) { const poolId = details[idx].poolId; const exitedShares = details[idx].exitedPoolShares; const multiPool = MultiPool.load(entityUUID(event, [poolId.toString()])); const pool = vPool.load(multiPool!.pool); const ethValue = exitedShares.times(pool!.totalUnderlyingSupply).div(pool!.totalSupply); + totalETH = totalETH.plus(ethValue); depositDataEntry.exitedEth = depositDataEntry.exitedEth.plus(ethValue); const exitQueue = vExitQueue.load(pool!.exitQueue); const nextTicketIdx = exitQueue!.ticketCount; const linkedTicketId = externalEntityUUID(Address.fromBytes(exitQueue!.address), [nextTicketIdx.toString()]); tickets.push(linkedTicketId); } + + createERC20ExitSystemEvent(event, event.address, event.params.staker, totalETH, event.params.exitedTokens); + depositDataEntry.type = 'ExitDataEntry'; depositDataEntry.createdAt = event.block.timestamp; depositDataEntry.editedAt = event.block.timestamp; diff --git a/src/utils/utils.ts b/src/utils/utils.ts index 79bbe85..f60832f 100644 --- a/src/utils/utils.ts +++ b/src/utils/utils.ts @@ -9,6 +9,8 @@ import { CoverageRecipientUpdatedEthSystemEvent, CoverageRecipientUpdatedSharesSystemEvent, DuplicateKeySystemAlert, + ERC20DepositSystemEvent, + ERC20ExitSystemEvent, ExternalFundingSystemAlert, FundedValidationKeySystemEvent, G, @@ -861,3 +863,71 @@ export function _computeEthAfterCommission( ) ); } + +export function createERC20DepositSystemEvent( + event: ethereum.Event, + erc20Address: Address, + depositor: Address, + amount: BigInt, + shares: BigInt +): void { + const g = getOrCreateG(); + + const id = `ERC20DepositSystemEvent/${event.transaction.hash.toHexString()}/${event.logIndex.toString()}/${erc20Address.toHexString()}/${depositor.toHexString()}`; + let systemEvent = ERC20DepositSystemEvent.load(id); + if (systemEvent == null) { + systemEvent = new ERC20DepositSystemEvent(id); + systemEvent.index = g.systemLogIndex; + systemEvent.type = 'ERC20DepositSystemEvent'; + + systemEvent.tx = event.transaction.hash; + systemEvent.who = event.transaction.from; + + systemEvent.integration = erc20Address; + systemEvent.staker = depositor; + systemEvent.depositedAmount = amount; + systemEvent.mintedShares = shares; + + systemEvent.createdAt = event.block.timestamp; + systemEvent.createdAtBlock = event.block.number; + + g.systemLogIndex = g.systemLogIndex.plus(BigInt.fromI32(1)); + g.save(); + } + + systemEvent.save(); +} + +export function createERC20ExitSystemEvent( + event: ethereum.Event, + erc20Address: Address, + depositor: Address, + amount: BigInt, + shares: BigInt +): void { + const g = getOrCreateG(); + + const id = `ERC20ExitSystemEvent/${event.transaction.hash.toHexString()}/${event.logIndex.toString()}/${erc20Address.toHexString()}/${depositor.toHexString()}`; + let systemEvent = ERC20ExitSystemEvent.load(id); + if (systemEvent == null) { + systemEvent = new ERC20ExitSystemEvent(id); + systemEvent.index = g.systemLogIndex; + systemEvent.type = 'ERC20ExitSystemEvent'; + + systemEvent.tx = event.transaction.hash; + systemEvent.who = event.transaction.from; + + systemEvent.integration = erc20Address; + systemEvent.staker = depositor; + systemEvent.exitedAmount = amount; + systemEvent.exitedShares = shares; + + systemEvent.createdAt = event.block.timestamp; + systemEvent.createdAtBlock = event.block.number; + + g.systemLogIndex = g.systemLogIndex.plus(BigInt.fromI32(1)); + g.save(); + } + + systemEvent.save(); +}