import { Protobuf } from 'as-proto/assembly'; import { hexToUint8Array } from '../../common'; import { TraceApi } from '../../hostapi'; import { EthStateChange, EthStateChangeIndices, EthStateChanges, StateChangeQuery, } from '../../proto'; export class State { readonly account: string; readonly value: T; readonly callIdx: u64; constructor(account: string, value: T, callIdx: u64) { this.account = account; this.value = value; this.callIdx = callIdx; } } export abstract class StateChange { protected readonly changes: Array>; protected constructor(protected readonly properties: StateChangeProperties) { this.changes = new Array>(); const response = TraceApi.instance().queryStateChange( new StateChangeQuery( hexToUint8Array(this.properties.account), this.properties.stateVar, this.properties.indices, ), ); if (response.length == 0) { this.changes = []; } else { const changes = Protobuf.decode(response, EthStateChanges.decode); for (let i = 0; i < changes.all.length; i++) { this.changes.push(this.unmarshalState(changes.all[i])); } } } public original(): T | null { return this.changes.length > 0 ? this.changes[0].value : null; } public all(): Array> { return this.changes; } public current(): T | null { return this.changes.length > 0 ? this.changes[this.changes.length - 1].value : null; } public abstract unmarshalState(raw: EthStateChange): State; } export class StateChangeProperties { constructor( readonly account: string, readonly stateVar: string, readonly indices: Uint8Array[] = [], ) {} } export abstract class StateKey { protected readonly __children__: Uint8Array[]; protected constructor(protected readonly __properties__: StateChangeProperties) { const response = TraceApi.instance().queryStateChange( new StateChangeQuery( hexToUint8Array(__properties__.account), __properties__.stateVar, __properties__.indices || [], ), ); if (response.length == 0) { this.__children__ = []; } else { const children = Protobuf.decode( response, EthStateChangeIndices.decode, ); this.__children__ = children.indices; } } get childrenCount(): u64 { return this.__children__.length; } protected abstract parseKey(key: K): Uint8Array; }