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.
|
// Check if the type is already added.
|
||||||
if (this._composer.has(name)) {
|
if (this._composer.has(name)) {
|
||||||
|
this._resolveEventConflict(name, params);
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -422,4 +424,38 @@ export class Schema {
|
|||||||
// Add a new type to the union.
|
// Add a new type to the union.
|
||||||
eventUnion.addType(this._composer.getOTC(event));
|
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 assert from 'assert';
|
||||||
|
|
||||||
|
// import { updateStateForMappingType, updateStateForElementaryType } from '@vulcanize/util';
|
||||||
|
|
||||||
import { Indexer, ResultEvent } from './indexer';
|
import { Indexer, ResultEvent } from './indexer';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -18,12 +20,16 @@ export async function createInitialState (indexer: Indexer, contractAddress: str
|
|||||||
assert(blockHash);
|
assert(blockHash);
|
||||||
assert(contractAddress);
|
assert(contractAddress);
|
||||||
|
|
||||||
// Store an empty state in an IPLDBlock.
|
// Store the desired initial state in an IPLDBlock.
|
||||||
const ipldBlockData: any = {
|
const ipldBlockData: any = {
|
||||||
state: {}
|
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 initial state data to be saved.
|
||||||
return ipldBlockData;
|
return ipldBlockData;
|
||||||
@ -38,7 +44,7 @@ export async function createStateDiff (indexer: Indexer, blockHash: string): Pro
|
|||||||
assert(indexer);
|
assert(indexer);
|
||||||
assert(blockHash);
|
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.
|
// 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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -66,4 +74,7 @@ export async function createStateCheckpoint (indexer: Indexer, contractAddress:
|
|||||||
export async function handleEvent (indexer: Indexer, eventData: ResultEvent): Promise<void> {
|
export async function handleEvent (indexer: Indexer, eventData: ResultEvent): Promise<void> {
|
||||||
assert(indexer);
|
assert(indexer);
|
||||||
assert(eventData);
|
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}}
|
||||||
|
|
||||||
{{~#each subgraphQueries}}
|
{{~#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);
|
log('{{this.queryName}}', id, block);
|
||||||
|
|
||||||
return indexer.getSubgraphEntity({{this.entityName}}, id, block);
|
return indexer.getSubgraphEntity({{this.entityName}}, id, block);
|
||||||
|
@ -57,18 +57,21 @@ type ResultEvent {
|
|||||||
proof: Proof
|
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!
|
from: String!
|
||||||
to: String!
|
to: String!
|
||||||
value: BigInt!
|
value: BigInt
|
||||||
|
tokenId: BigInt
|
||||||
}
|
}
|
||||||
|
|
||||||
type ApprovalERC20Event {
|
type ApprovalEvent {
|
||||||
owner: String!
|
owner: String!
|
||||||
spender: String!
|
spender: String
|
||||||
value: BigInt!
|
value: BigInt
|
||||||
|
approved: String
|
||||||
|
tokenId: BigInt
|
||||||
}
|
}
|
||||||
|
|
||||||
type AuthorizationUsedEvent {
|
type AuthorizationUsedEvent {
|
||||||
@ -120,18 +123,6 @@ type WithdrawEvent {
|
|||||||
withdrawalAmount: BigInt!
|
withdrawalAmount: BigInt!
|
||||||
}
|
}
|
||||||
|
|
||||||
type TransferERC721Event {
|
|
||||||
from: String!
|
|
||||||
to: String!
|
|
||||||
tokenId: BigInt!
|
|
||||||
}
|
|
||||||
|
|
||||||
type ApprovalERC721Event {
|
|
||||||
owner: String!
|
|
||||||
approved: String!
|
|
||||||
tokenId: BigInt!
|
|
||||||
}
|
|
||||||
|
|
||||||
type ApprovalForAllEvent {
|
type ApprovalForAllEvent {
|
||||||
owner: String!
|
owner: String!
|
||||||
operator: String!
|
operator: String!
|
||||||
|
Loading…
Reference in New Issue
Block a user