Skip to content

Commit

Permalink
save
Browse files Browse the repository at this point in the history
  • Loading branch information
belopash committed Jul 25, 2024
1 parent d43e70d commit fcd565c
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 6 deletions.
36 changes: 34 additions & 2 deletions substrate/substrate-typegen/src/typegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export class Typegen {
}

private sts = new Map<Runtime, Sts>()
private palletModules = new Map<string, Set<string>>

public readonly dir: OutDir

Expand All @@ -58,6 +59,7 @@ export class Typegen {
this.generateEnums('calls')
this.generateStorage()
this.generateConsts()
this.generatePalletModules()

let index = this.dir.file('index.ts')

Expand Down Expand Up @@ -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}', {`)
Expand Down Expand Up @@ -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}', {`)
Expand All @@ -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)

Expand Down Expand Up @@ -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<EACDefinition>[] {
return this.collectItems(
Expand Down Expand Up @@ -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<Runtime>()
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') {
Expand Down
8 changes: 4 additions & 4 deletions test/balances/src/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -16,7 +16,7 @@ const processor = new SubstrateBatchProcessor()
})
.setBlockRange({from: 19_666_100})
.addEvent({
name: [events.balances.transfer.name]
name: [balances.events.transfer.name]
})


Expand All @@ -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': {
Expand All @@ -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}
Expand Down
4 changes: 4 additions & 0 deletions test/balances/src/types/balances/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * as events from './events'
export * as calls from './calls'
export * as storage from './storage'
export * as constants from './constants'

0 comments on commit fcd565c

Please sign in to comment.