@graphprotocol/graph-cli
Version:
CLI for building for and deploying to The Graph
66 lines (57 loc) • 2.66 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.generatePlaceholderHandlers = void 0;
const mapping_1 = require("../../../scaffold/mapping");
const generatePlaceholderHandlers = ({ abi, events, contractName, }) => `
import { BigInt } from '@graphprotocol/graph-ts'
import { ${contractName}, ${events.map(event => event._alias)} }
from '../generated/${contractName}/${contractName}'
import { ExampleEntity } from '../generated/schema'
${events
.map((event, index) => index === 0
? `
export function handle${event._alias}(event: ${event._alias}): void {
// Entities can be loaded from the store using a string ID; this ID
// needs to be unique across all entities of the same type
let entity = ExampleEntity.load(event.transaction.from)
// Entities only exist after they have been saved to the store;
// \`null\` checks allow to create entities on demand
if (!entity) {
entity = new ExampleEntity(event.transaction.from)
// Entity fields can be set using simple assignments
entity.count = BigInt.fromI32(0)
}
// BigInt and BigDecimal math are supported
entity.count = entity.count + BigInt.fromI32(1)
// Entity fields can be set based on event parameters
${(0, mapping_1.generateEventFieldAssignments)(event, contractName).slice(0, 2).join('\n')}
// Entities can be written to the store with \`.save()\`
entity.save()
// Note: If a handler doesn't require existing field values, it is faster
// _not_ to load the entity from the store. Instead, create it fresh with
// \`new Entity(...)\`, set the fields that should be updated and save the
// entity back to the store. Fields that were not set or unset remain
// unchanged, allowing for partial updates to be applied.
// It is also possible to access smart contracts from mappings. For
// example, the contract that has emitted the event can be connected to
// with:
//
// let contract = Contract.bind(event.address)
//
// The following functions can then be called on this contract to access
// state variables and other data:
//
// ${abi.codeGenerator().callableFunctions().isEmpty()
? 'None'
: abi
.codeGenerator()
.callableFunctions()
.map((fn) => `- contract.${fn.get('name')}(...)`)
.join('\n// ')}
}
`
: `
export function handle${event._alias}(event: ${event._alias}): void {}
`)
.join('\n')}`;
exports.generatePlaceholderHandlers = generatePlaceholderHandlers;