Skip to content

Commit

Permalink
Use contract wise artifacts in the generated indexer methods
Browse files Browse the repository at this point in the history
  • Loading branch information
prathamesh0 committed Dec 23, 2021
1 parent 186b9a5 commit d7c0a5c
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 13 deletions.
4 changes: 2 additions & 2 deletions packages/codegen/src/generate-code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ const main = async (): Promise<void> => {
: fs.readFileSync(path.resolve(inputFile)).toString();
}

contracts.push({ contractString, contractKind: contract.kind });
contracts.push({ contractString, contractName: contract.name, contractKind: contract.kind });
}

const visitor = new Visitor();
Expand Down Expand Up @@ -99,7 +99,7 @@ function parseAndVisit (visitor: Visitor, contracts: any[], mode: string) {
// Filter out library nodes.
ast.children = ast.children.filter(child => !(child.type === 'ContractDefinition' && child.kind === 'library'));

visitor.setContractKind(contract.contractKind);
visitor.setContract(contract.contractName, contract.contractKind);

visit(ast, {
FunctionDefinition: functionDefinitionVisitor,
Expand Down
5 changes: 3 additions & 2 deletions packages/codegen/src/indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class Indexer {
* @param returnType Return type for the query.
* @param stateVariableTypeName Type of the state variable in case of state variable query.
*/
addQuery (mode: string, name: string, params: Array<Param>, returnType: string, stateVariableType?: string): void {
addQuery (contract: string, mode: string, name: string, params: Array<Param>, returnType: string, stateVariableType?: string): void {
// Check if the query is already added.
if (this._queries.some(query => query.name === name)) {
return;
Expand All @@ -50,7 +50,8 @@ export class Indexer {
params: _.cloneDeep(params),
returnType,
mode,
stateVariableType
stateVariableType,
contract
};

if (name.charAt(0) === '_') {
Expand Down
10 changes: 8 additions & 2 deletions packages/codegen/src/templates/indexer-template.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,10 @@ export class Indexer implements IndexerInterface {
const blockNumber = ethers.BigNumber.from(number).toNumber();

{{#if (compare query.mode @root.constants.MODE_ETH_CALL)}}
const contract = new ethers.Contract(contractAddress, this._abi, this._ethProvider);
const abi = this._abiMap.get(KIND_{{capitalize query.contract}});
assert(abi);

const contract = new ethers.Contract(contractAddress, abi, this._ethProvider);
{{#if (compare query.returnType 'bigint')}}
let value = await contract.{{query.name}}(
{{~#each query.params}}{{this.name}}, {{/each}}{ blockTag: blockHash });
Expand All @@ -249,8 +252,11 @@ export class Indexer implements IndexerInterface {
{{/if}}

{{~#if (compare query.mode @root.constants.MODE_STORAGE)}}
const storageLayout = this._storageLayoutMap.get(KIND_{{capitalize query.contract}});
assert(storageLayout);

const result = await this._baseIndexer.getStorageValue(
this._storageLayout,
storageLayout,
blockHash,
contractAddress,
'{{query.name}}'{{#if query.params.length}},{{/if}}
Expand Down
21 changes: 14 additions & 7 deletions packages/codegen/src/visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class Visitor {
_reset: Reset;
_types: Types;

_contractKind?: string;
_contract?: { name: string, kind: string };

constructor () {
this._schema = new Schema();
Expand All @@ -40,8 +40,11 @@ export class Visitor {
this._types = new Types();
}

setContractKind (kind: string): void {
this._contractKind = kind;
setContract (name: string, kind: string): void {
this._contract = {
name,
kind
};
}

/**
Expand All @@ -68,11 +71,13 @@ export class Visitor {

this._schema.addQuery(name, params, returnType);
this._resolvers.addQuery(name, params, returnType);
this._indexer.addQuery(MODE_ETH_CALL, name, params, returnType);
this._entity.addQuery(name, params, returnType);
this._database.addQuery(name, params, returnType);
this._client.addQuery(name, params, returnType);
this._reset.addQuery(name);

assert(this._contract);
this._indexer.addQuery(this._contract.name, MODE_ETH_CALL, name, params, returnType);
}
}

Expand Down Expand Up @@ -110,11 +115,13 @@ export class Visitor {

this._schema.addQuery(name, params, returnType);
this._resolvers.addQuery(name, params, returnType);
this._indexer.addQuery(MODE_STORAGE, name, params, returnType, stateVariableType);
this._entity.addQuery(name, params, returnType);
this._database.addQuery(name, params, returnType);
this._client.addQuery(name, params, returnType);
this._reset.addQuery(name);

assert(this._contract);
this._indexer.addQuery(this._contract.name, MODE_STORAGE, name, params, returnType, stateVariableType);
}

/**
Expand All @@ -129,8 +136,8 @@ export class Visitor {

this._schema.addEventType(name, params);

assert(this._contractKind);
this._indexer.addEvent(name, params, this._contractKind);
assert(this._contract);
this._indexer.addEvent(name, params, this._contract.kind);
}

visitSubgraph (subgraphPath?: string): void {
Expand Down

0 comments on commit d7c0a5c

Please sign in to comment.