Skip to content

Instantly share code, notes, and snippets.

@belopash
Created September 8, 2022 08:47
Show Gist options
  • Save belopash/d10e8e982435fe3bddaafafd87ab7e70 to your computer and use it in GitHub Desktop.
Save belopash/d10e8e982435fe3bddaafafd87ab7e70 to your computer and use it in GitHub Desktop.
export class SystemAccountStorage {
  private readonly _chain: Chain
  private readonly blockHash: string

  constructor(ctx: BlockContext)
  constructor(ctx: ChainContext, block: Block)
  constructor(ctx: BlockContext, block?: Block) {
    block = block || ctx.block
    this.blockHash = block.hash
    this._chain = ctx._chain
  }

  /**
   *  The full account information for a particular account ID.
   */
  get isV1() {
    return this._chain.getStorageItemTypeHash('System', 'Account') === 'eb40f1d91f26d72e29c60e034d53a72b9b529014c7e108f422d8ad5f03f0c902'
  }

  /**
   *  The full account information for a particular account ID.
   */
  get asV1(): {
      get(key: Uint8Array): Promise<v1.AccountInfo>
      getMany(keys: Uint8Array[]): Promise<(v1.AccountInfo)[]>
      getAll(): Promise<(v1.AccountInfo)[]>
    } {
      assert(this.isV1)
      return this as any
  }

  private async get(...args: any[]): Promise<any> {
    return this._chain.getStorage(this.blockHash, 'System', 'Account', ...args)
  }

  private async getMany(...args: any[]): Promise<any[]> {
    let keys = Array.isArray(args[0]) ? args : args.map(k => [k])
    return this._chain.queryStorage(this.blockHash, 'System', 'Account', keys)
  }

  private async getAll(): Promise<any[]> {
    return this._chain.queryStorage(this.blockHash, 'System', 'Account')
  }

  /**
   * Checks whether the storage item is defined for the current chain version.
   */
  get isExists(): boolean {
    return this._chain.getStorageItemTypeHash('System', 'Account') != null
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment