diff --git a/substrate/substrate-typegen/src/typegen.ts b/substrate/substrate-typegen/src/typegen.ts index 94086706e..5e4fb3036 100644 --- a/substrate/substrate-typegen/src/typegen.ts +++ b/substrate/substrate-typegen/src/typegen.ts @@ -45,6 +45,7 @@ export class Typegen { } private sts = new Map() + private palletModules = new Map> public readonly dir: OutDir @@ -58,6 +59,7 @@ export class Typegen { this.generateEnums('calls') this.generateStorage() this.generateConsts() + this.generatePalletModules() let index = this.dir.file('index.ts') @@ -100,6 +102,8 @@ export class Typegen { let file = new ItemFile(this, pallet, fix) let out = file.out + this.getPalletModule(pallet).add(file.name) + for (let [name, versions] of groupBy(palletItems, it => it.def.name)) { out.line() out.line(`export const ${toJsName(name)} = ${fix.toLowerCase()}('${pallet}.${name}', {`) @@ -146,6 +150,8 @@ export class Typegen { let file = new ItemFile(this, pallet, 'Constant') let out = file.out + this.getPalletModule(pallet).add(file.name) + for (let [name, versions] of groupBy(palletItems, it => splitQualifiedName(it.name)[1])) { out.line() out.line(`export const ${toJsName(name)} = constant('${pallet}.${name}', {`) @@ -171,6 +177,8 @@ export class Typegen { let file = new ItemFile(this, pallet, 'Storage') let out = file.out + this.getPalletModule(pallet).add(file.name) + for (let [name, versions] of groupBy(palletItems, it => splitQualifiedName(it.name)[1])) { let jsName = toJsName(name) @@ -225,6 +233,19 @@ export class Typegen { } } + private generatePalletModules() { + for (let [pallet, modules] of this.palletModules) { + let out = this.dir.child(getPalletDir(pallet)).file('index.ts') + + for (let module of modules) { + let moduleName = module.slice(0, module.length - 3) // remove '.ts' + out.line(`export * as ${moduleName} from './${moduleName}'`) + } + + out.write() + } + } + @def private events(): Item[] { return this.collectItems( @@ -373,21 +394,32 @@ export class Typegen { this.sts.set(runtime, sts) return sts } + + getPalletModule(pallet: string) { + let module = this.palletModules.get(pallet) + if (module == null) { + module = new Set() + this.palletModules.set(pallet, module) + } + return module + } } class ItemFile { private imported = new Set() - public readonly out: FileOutput + readonly out: FileOutput + readonly name: string constructor( private typegen: Typegen, pallet: string, type: 'Event' | 'Call' | 'Constant' | 'Storage' ) { + this.name = type == 'Storage' ? 'storage.ts' : type.toLowerCase() + 's.ts' this.out = this.typegen.dir .child(getPalletDir(pallet)) - .file(type == 'Storage' ? 'storage.ts' : type.toLowerCase() + 's.ts') + .file(this.name) let imports = ['sts', 'Block', 'Bytes', 'Option', 'Result', `${type}Type`, `${type.toLowerCase()}`, 'RuntimeCtx' ] if (type === 'Storage') { diff --git a/test/balances/src/processor.ts b/test/balances/src/processor.ts index c675164ab..11d488640 100644 --- a/test/balances/src/processor.ts +++ b/test/balances/src/processor.ts @@ -3,7 +3,7 @@ import * as ss58 from '@subsquid/ss58' import {SubstrateBatchProcessor} from '@subsquid/substrate-processor' import {TypeormDatabase} from '@subsquid/typeorm-store' import {Transfer} from './model' -import {events, storage} from './types' +import * as balances from './types/balances' const processor = new SubstrateBatchProcessor() @@ -16,7 +16,7 @@ const processor = new SubstrateBatchProcessor() }) .setBlockRange({from: 19_666_100}) .addEvent({ - name: [events.balances.transfer.name] + name: [balances.events.transfer.name] }) @@ -25,7 +25,7 @@ processor.run(new TypeormDatabase(), async ctx => { for (let block of ctx.blocks) { for (let event of block.events) { - let rec = events.balances.transfer.at(block.header, function (e, v) { + let rec = balances.events.transfer.at(block.header, function (e, v) { switch (v) { case 'v1020': case 'v1050': { @@ -41,7 +41,7 @@ processor.run(new TypeormDatabase(), async ctx => { /** * Just a demo */ - // let balances = await storage.balances.account.at(block.header, async (s, _) => { + // let data = await balances.storage.account.at(block.header, async (s, _) => { // let d = s.getDefault() // let [from, to] = await s.getMany([rec.from, rec.to]) // return {from: from?.free ?? d.free, to: to?.free ?? d.free} diff --git a/test/balances/src/types/balances/index.ts b/test/balances/src/types/balances/index.ts new file mode 100644 index 000000000..c181256f1 --- /dev/null +++ b/test/balances/src/types/balances/index.ts @@ -0,0 +1,4 @@ +export * as events from './events' +export * as calls from './calls' +export * as storage from './storage' +export * as constants from './constants'