mirror of
https://github.com/cerc-io/watcher-ts
synced 2024-11-19 20:36:19 +00:00
Handle event name conflicts in schema and update hooks in codegen (#97)
* Change return type for subgraph resolvers and update hooks generation * Handle event name conflicts in schema generation
This commit is contained in:
parent
7a4d0b6bb4
commit
5f4dd14d7a
@ -71,6 +71,8 @@ export class Schema {
|
||||
|
||||
// Check if the type is already added.
|
||||
if (this._composer.has(name)) {
|
||||
this._resolveEventConflict(name, params);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
@ -422,4 +424,38 @@ export class Schema {
|
||||
// Add a new type to the union.
|
||||
eventUnion.addType(this._composer.getOTC(event));
|
||||
}
|
||||
|
||||
_resolveEventConflict (name: string, params: Array<Param>): void {
|
||||
const eventTC = this._composer.getOTC(name);
|
||||
const currentFields = eventTC.getFieldNames();
|
||||
|
||||
// Get the common fields.
|
||||
let commonFields: string[] = [];
|
||||
commonFields = params.reduce((acc, curr) => {
|
||||
if (currentFields.includes(curr.name)) {
|
||||
acc.push(curr.name);
|
||||
}
|
||||
return acc;
|
||||
}, commonFields);
|
||||
|
||||
// Make the current fields that are uncommon nullable.
|
||||
currentFields.forEach((field: string) => {
|
||||
if (!commonFields.includes(field)) {
|
||||
eventTC.makeFieldNullable(field);
|
||||
}
|
||||
});
|
||||
|
||||
// Get the new fields.
|
||||
const newFields: any = {};
|
||||
params.forEach((param: Param) => {
|
||||
if (!commonFields.includes(param.name)) {
|
||||
const tsCurrType = getTsForSol(param.type);
|
||||
assert(tsCurrType, `ts type for sol type ${param.type} for ${param.name} not found`);
|
||||
newFields[param.name] = `${getGqlForTs(tsCurrType)}`;
|
||||
}
|
||||
});
|
||||
|
||||
// Add the new fields to the current type.
|
||||
eventTC.addFields(newFields);
|
||||
}
|
||||
}
|
||||
|
@ -4,6 +4,8 @@
|
||||
|
||||
import assert from 'assert';
|
||||
|
||||
// import { updateStateForMappingType, updateStateForElementaryType } from '@vulcanize/util';
|
||||
|
||||
import { Indexer, ResultEvent } from './indexer';
|
||||
|
||||
/**
|
||||
@ -18,12 +20,16 @@ export async function createInitialState (indexer: Indexer, contractAddress: str
|
||||
assert(blockHash);
|
||||
assert(contractAddress);
|
||||
|
||||
// Store an empty state in an IPLDBlock.
|
||||
// Store the desired initial state in an IPLDBlock.
|
||||
const ipldBlockData: any = {
|
||||
state: {}
|
||||
};
|
||||
|
||||
// Use updateStateForMappingType to update initial state.
|
||||
// Use updateStateForElementaryType to update initial state with an elementary property.
|
||||
// Eg. const ipldBlockData = updateStateForElementaryType(ipldBlockData, '_totalBalance', result.value.toString());
|
||||
|
||||
// Use updateStateForMappingType to update initial state with a nested property.
|
||||
// Eg. const ipldBlockData = updateStateForMappingType(ipldBlockData, '_allowances', [owner, spender], allowance.value.toString());
|
||||
|
||||
// Return initial state data to be saved.
|
||||
return ipldBlockData;
|
||||
@ -38,7 +44,7 @@ export async function createStateDiff (indexer: Indexer, blockHash: string): Pro
|
||||
assert(indexer);
|
||||
assert(blockHash);
|
||||
|
||||
// Use indexer.createStateDiff() method to create a custom diff.
|
||||
// Use indexer.createStateDiff() method to save custom state diff(s).
|
||||
}
|
||||
|
||||
/**
|
||||
@ -55,6 +61,8 @@ export async function createStateCheckpoint (indexer: Indexer, contractAddress:
|
||||
|
||||
// Use indexer.createStateCheckpoint() method to create a custom checkpoint.
|
||||
|
||||
// Return false to update the state created by this hook by auto-generated checkpoint state.
|
||||
// Return true to disable update of the state created by this hook by auto-generated checkpoint state.
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -66,4 +74,7 @@ export async function createStateCheckpoint (indexer: Indexer, contractAddress:
|
||||
export async function handleEvent (indexer: Indexer, eventData: ResultEvent): Promise<void> {
|
||||
assert(indexer);
|
||||
assert(eventData);
|
||||
|
||||
// Use indexer methods to index data.
|
||||
// Pass `diff` parameter to indexer methods as true to save an auto-generated state from the indexed data.
|
||||
}
|
||||
|
@ -75,7 +75,7 @@ export const createResolvers = async (indexer: Indexer, eventWatcher: EventWatch
|
||||
{{/each}}
|
||||
|
||||
{{~#each subgraphQueries}}
|
||||
{{this.queryName}}: async (_: any, { id, block = {} }: { id: string, block: BlockHeight }): Promise<{{this.entityName}} | undefined> => {
|
||||
{{this.queryName}}: async (_: any, { id, block = {} }: { id: string, block: BlockHeight }) => {
|
||||
log('{{this.queryName}}', id, block);
|
||||
|
||||
return indexer.getSubgraphEntity({{this.entityName}}, id, block);
|
||||
|
@ -57,18 +57,21 @@ type ResultEvent {
|
||||
proof: Proof
|
||||
}
|
||||
|
||||
union Event = TransferERC20Event | ApprovalERC20Event | AuthorizationUsedEvent | AdminUpdatedEvent | TaxRateUpdatedEvent | SlotClaimedEvent | SlotDelegateUpdatedEvent | StakeEvent | UnstakeEvent | WithdrawEvent | TransferERC721Event | ApprovalERC721Event | ApprovalForAllEvent | BlockProducerAddedEvent | BlockProducerRemovedEvent | BlockProducerRewardCollectorChangedEvent | ClaimedEvent | SlashedEvent | MerkleRootUpdatedEvent | AccountUpdatedEvent | PermanentURIEvent | GovernanceChangedEvent | UpdateThresholdChangedEvent | RoleAdminChangedEvent | RoleGrantedEvent | RoleRevokedEvent | RewardScheduleChangedEvent
|
||||
union Event = TransferEvent | ApprovalEvent | AuthorizationUsedEvent | AdminUpdatedEvent | TaxRateUpdatedEvent | SlotClaimedEvent | SlotDelegateUpdatedEvent | StakeEvent | UnstakeEvent | WithdrawEvent | ApprovalForAllEvent | BlockProducerAddedEvent | BlockProducerRemovedEvent | BlockProducerRewardCollectorChangedEvent | ClaimedEvent | SlashedEvent | MerkleRootUpdatedEvent | AccountUpdatedEvent | PermanentURIEvent | GovernanceChangedEvent | UpdateThresholdChangedEvent | RoleAdminChangedEvent | RoleGrantedEvent | RoleRevokedEvent | RewardScheduleChangedEvent
|
||||
|
||||
type TransferERC20Event {
|
||||
type TransferEvent {
|
||||
from: String!
|
||||
to: String!
|
||||
value: BigInt!
|
||||
value: BigInt
|
||||
tokenId: BigInt
|
||||
}
|
||||
|
||||
type ApprovalERC20Event {
|
||||
type ApprovalEvent {
|
||||
owner: String!
|
||||
spender: String!
|
||||
value: BigInt!
|
||||
spender: String
|
||||
value: BigInt
|
||||
approved: String
|
||||
tokenId: BigInt
|
||||
}
|
||||
|
||||
type AuthorizationUsedEvent {
|
||||
@ -120,18 +123,6 @@ type WithdrawEvent {
|
||||
withdrawalAmount: BigInt!
|
||||
}
|
||||
|
||||
type TransferERC721Event {
|
||||
from: String!
|
||||
to: String!
|
||||
tokenId: BigInt!
|
||||
}
|
||||
|
||||
type ApprovalERC721Event {
|
||||
owner: String!
|
||||
approved: String!
|
||||
tokenId: BigInt!
|
||||
}
|
||||
|
||||
type ApprovalForAllEvent {
|
||||
owner: String!
|
||||
operator: String!
|
||||
|
Loading…
Reference in New Issue
Block a user