|
1 | 1 | import { ethereum, log } from "@graphprotocol/graph-ts";
|
2 |
| -import { DepositEvent } from "../../generated/ValidatorRegistration/ValidatorRegistration"; |
| 2 | +import { |
| 3 | + BYTES_ZERO, |
| 4 | + VALIDATOR_REGISTRATION_ADDRESS, |
| 5 | + ETHEREUM_VALIDATORS_DEPOSIT_ROOT_START_BLOCK, |
| 6 | +} from "const"; |
| 7 | +import { |
| 8 | + DepositEvent, |
| 9 | + ValidatorRegistration as ValidatorRegistrationContract, |
| 10 | +} from "../../generated/ValidatorRegistration/ValidatorRegistration"; |
3 | 11 | import { Block, ValidatorRegistration } from "../../generated/schema";
|
4 | 12 |
|
5 | 13 | export function handleDepositEvent(event: DepositEvent): void {
|
6 |
| - let publicKey = event.params.pubkey.toHexString(); |
7 |
| - let validator = ValidatorRegistration.load(publicKey); |
| 14 | + let registrationId = event.transaction.hash |
| 15 | + .toHexString() |
| 16 | + .concat("-") |
| 17 | + .concat(event.logIndex.toString()); |
| 18 | + let registration = new ValidatorRegistration(registrationId); |
| 19 | + registration.publicKey = event.params.pubkey; |
| 20 | + registration.withdrawalCredentials = event.params.withdrawal_credentials; |
| 21 | + registration.createdAtBlock = event.block.number; |
| 22 | + registration.createdAtTimestamp = event.block.timestamp; |
8 | 23 |
|
9 |
| - if (validator == null) { |
10 |
| - validator = new ValidatorRegistration(publicKey); |
11 |
| - validator.withdrawalCredentials = event.params.withdrawal_credentials; |
12 |
| - validator.index = event.params.index; |
13 |
| - validator.createdAtBlock = event.block.number; |
14 |
| - validator.createdAtTimestamp = event.block.timestamp; |
15 |
| - validator.save(); |
| 24 | + if (event.block.number.ge(ETHEREUM_VALIDATORS_DEPOSIT_ROOT_START_BLOCK)) { |
| 25 | + let contract = ValidatorRegistrationContract.bind( |
| 26 | + VALIDATOR_REGISTRATION_ADDRESS |
| 27 | + ); |
| 28 | + let depositRootCall = contract.try_get_deposit_root(); |
| 29 | + if (!depositRootCall.reverted) { |
| 30 | + registration.validatorsDepositRoot = depositRootCall.value; |
| 31 | + } else { |
| 32 | + registration.validatorsDepositRoot = BYTES_ZERO; |
| 33 | + } |
| 34 | + } else { |
| 35 | + registration.validatorsDepositRoot = BYTES_ZERO; |
16 | 36 | }
|
17 | 37 |
|
| 38 | + registration.save(); |
18 | 39 | log.info("[VRC] DepositEvent publicKey={} withdrawalCredentials={}", [
|
19 |
| - validator.id, |
20 |
| - validator.withdrawalCredentials.toHexString(), |
| 40 | + registration.id, |
| 41 | + registration.withdrawalCredentials.toHexString(), |
21 | 42 | ]);
|
22 | 43 | }
|
23 | 44 |
|
|
0 commit comments