Skip to content

Commit

Permalink
Generate indexer and entities with DB caching for multiple return types
Browse files Browse the repository at this point in the history
  • Loading branch information
nikugogoi committed Apr 27, 2023
1 parent 26c1607 commit 80ceced
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 29 deletions.
42 changes: 25 additions & 17 deletions packages/codegen/src/entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import yaml from 'js-yaml';
import Handlebars from 'handlebars';
import { Writable } from 'stream';

import { VariableDeclaration } from '@solidity-parser/parser/dist/src/ast-types';

import { getPgForTs, getTsForGql, getGqlForSol } from './utils/type-mappings';
import { Param } from './utils/types';
import { getFieldType } from './utils/subgraph';
Expand All @@ -32,7 +34,7 @@ export class Entity {
* @param params Parameters to the query.
* @param returnType Return type for the query.
*/
addQuery (name: string, params: Array<Param>, typeName: any): void {
addQuery (name: string, params: Array<Param>, returnParameters: VariableDeclaration[]): void {
// Check if the query is already added.
if (this._entities.some(entity => entity.className.toLowerCase() === name.toLowerCase())) {
return;
Expand Down Expand Up @@ -138,23 +140,29 @@ export class Entity {
})
);

const baseType = getBaseType(typeName);
assert(baseType);

const gqlReturnType = getGqlForSol(baseType);
assert(gqlReturnType);
const tsReturnType = getTsForGql(gqlReturnType);
assert(tsReturnType);
const pgReturnType = getPgForTs(tsReturnType);
assert(pgReturnType);
entityObject.columns = entityObject.columns.concat(
returnParameters.map((returnParameter, index) => {
const typeName = returnParameter.typeName;
assert(typeName);
const baseType = getBaseType(typeName);
assert(baseType);

const gqlReturnType = getGqlForSol(baseType);
assert(gqlReturnType);
const tsReturnType = getTsForGql(gqlReturnType);
assert(tsReturnType);
const pgReturnType = getPgForTs(tsReturnType);
assert(pgReturnType);

entityObject.columns.push({
name: 'value',
pgType: pgReturnType,
tsType: tsReturnType,
columnType: 'Column',
columnOptions: []
});
return {
name: returnParameters.length > 1 ? `value${index}` : 'value',
pgType: pgReturnType,
tsType: tsReturnType,
columnType: 'Column',
columnOptions: []
};
})
);

entityObject.columns.push({
name: 'proof',
Expand Down
3 changes: 1 addition & 2 deletions packages/codegen/src/indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ export class Indexer {
return;
}

// Disable DB caching if more than 1 return params.
let disableCaching = returnParameters.length > 1;
let disableCaching = false;

const returnTypes = returnParameters.map(returnParameter => {
let typeName = returnParameter.typeName;
Expand Down
24 changes: 22 additions & 2 deletions packages/codegen/src/templates/indexer-template.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,15 @@ export class Indexer implements IndexerInterface {
log('{{query.name}}: db hit.');

return {
{{~#if (compare query.returnTypes.length 1 operator=">")}}
value: {
{{#each query.returnTypes}}
value{{@index}}: entity.value{{@index}}{{#unless @last}},{{/unless}}
{{/each}}
},
{{else}}
value: entity.value,
{{/if}}
proof: JSON.parse(entity.proof)
};
}
Expand Down Expand Up @@ -249,8 +257,20 @@ export class Indexer implements IndexerInterface {
{{/if}}

{{#unless disableCaching}}
await this._db.{{query.saveQueryName}}({ blockHash, blockNumber, contractAddress
{{~#each query.params}}, {{this.name~}} {{/each}}, value: result.value, proof: JSONbigNative.stringify(result.proof) });
await this._db.{{query.saveQueryName}}({
blockHash,
blockNumber,
contractAddress,
{{~#each query.params}}{{this.name~}},{{/each}}
{{#each query.returnTypes}}
{{~#if (compare query.returnTypes.length 1 operator=">")}}
value{{@index}}: result.value.value{{@index}},
{{else}}
value: result.value,
{{/if}}
{{/each}}
proof: JSONbigNative.stringify(result.proof)
});

{{/unless}}
{{#if query.stateVariableType}}
Expand Down
16 changes: 8 additions & 8 deletions packages/codegen/src/visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,20 @@ export class Visitor {
const typeName = node.returnParameters[0].typeName;
assert(typeName);

// TODO: Check for unhandled return type params

switch (typeName.type) {
case 'ElementaryTypeName':
this._entity.addQuery(name, params, typeName);
this._database.addQuery(name, params, typeName);
this._client.addQuery(name, params, typeName);
// falls through

case 'ArrayTypeName':
this._schema.addQuery(name, params, node.returnParameters);
this._resolvers.addQuery(name, params);

assert(this._contract);
this._indexer.addQuery(this._contract.name, MODE_ETH_CALL, name, params, node.returnParameters);
this._entity.addQuery(name, params, node.returnParameters);
this._database.addQuery(name, params, typeName);
this._client.addQuery(name, params, typeName);
break;

case 'UserDefinedTypeName':
Expand Down Expand Up @@ -149,12 +150,11 @@ export class Visitor {
case 'ElementaryTypeName': {
this._schema.addQuery(name, params, [variable]);
this._resolvers.addQuery(name, params);
this._entity.addQuery(name, params, typeName);
this._database.addQuery(name, params, typeName);
this._client.addQuery(name, params, typeName);

assert(this._contract);
this._indexer.addQuery(this._contract.name, MODE_STORAGE, name, params, [variable], stateVariableType);
this._entity.addQuery(name, params, [variable]);
this._database.addQuery(name, params, typeName);
this._client.addQuery(name, params, typeName);

break;
}
Expand Down

0 comments on commit 80ceced

Please sign in to comment.