UNPKG

@graphprotocol/graph-cli

Version:

CLI for building for and deploying to The Graph

71 lines (70 loc) 2.88 kB
import { ascTypeForProtocol, valueTypeForAsc } from '../codegen/types/index.js'; import * as util from '../codegen/util.js'; import { renameNameIfNeeded } from './mapping.js'; export function abiEvents(abi) { return util.disambiguateNames({ // @ts-expect-error improve typings of disambiguateNames to handle iterables values: abi.data.filter(item => item.get('type') === 'event'), // @ts-expect-error improve typings of disambiguateNames to handle iterables getName: event => event.get('name'), // @ts-expect-error improve typings of disambiguateNames to handle iterables setName: (event, name) => event.set('_alias', name), }); } export const protocolTypeToGraphQL = (protocol, name) => { const ascType = ascTypeForProtocol(protocol, name); // TODO: we need to figure out how to improve types // but for now this always is returning a string const convertedType = valueTypeForAsc(ascType); // TODO: this is a hack to make array type non-nullable // We should refactor the way we convert the Values from ASC to GraphQL // For arrays we always want non-nullable children return convertedType.endsWith(']') ? convertedType.replace(/\]/g, '!]') : convertedType; }; export const generateField = ({ name, type, protocolName, }) => `${name}: ${protocolTypeToGraphQL(protocolName, type)}! # ${type}`; export const generateEventFields = ({ index, input, protocolName, }) => input.type == 'tuple' ? util .unrollTuple({ value: input, path: [input.name || `param${index}`], index, }) .map(({ path, type }) => generateField({ name: path.join('_'), type, protocolName })) : [ generateField({ name: input.name || `param${index}`, type: input.type, protocolName, }), ]; export const generateEventType = (event, protocolName, contractName) => { return `type ${event.collision ? `${contractName}${event._alias}` : event._alias} @entity(immutable: true) { id: Bytes! ${event.inputs .reduce((acc, input, index) => { input.name = renameNameIfNeeded(input.name); return acc.concat(generateEventFields({ input, index, protocolName })); }, []) .join('\n')} blockNumber: BigInt! blockTimestamp: BigInt! transactionHash: Bytes! }`; }; export const generateExampleEntityType = (protocol, events) => { if (protocol.hasABIs() && events.length > 0) { return `type ExampleEntity @entity { id: Bytes! count: BigInt! ${events[0].inputs .reduce((acc, input, index) => acc.concat(generateEventFields({ input, index, protocolName: protocol.name })), []) .slice(0, 2) .join('\n')} }`; } return `type ExampleEntity @entity { id: ID! block: Bytes! count: BigInt! }`; };