Skip to content

Commit

Permalink
feat: add deposit and exit SystemEvents for integration (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
gauthiermyr authored Nov 6, 2023
1 parent 4c06aeb commit 581bb15
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 2 deletions.
53 changes: 53 additions & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -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!
}
29 changes: 27 additions & 2 deletions src/ERC20.mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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();
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
70 changes: 70 additions & 0 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import {
CoverageRecipientUpdatedEthSystemEvent,
CoverageRecipientUpdatedSharesSystemEvent,
DuplicateKeySystemAlert,
ERC20DepositSystemEvent,
ERC20ExitSystemEvent,
ExternalFundingSystemAlert,
FundedValidationKeySystemEvent,
G,
Expand Down Expand Up @@ -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();
}

0 comments on commit 581bb15

Please sign in to comment.