import { BinaryReader , BinaryWriter , createSerializeWire , InvalidFormatError , JSONHelper , StorageItemJSON , } from '@neo-one/client-common' ; import { BN } from 'bn.js' ; import { DeserializeWireBaseOptions , DeserializeWireOptions , SerializableWire } from './Serializable' ; import { StorageKey } from './StorageKey' ; export interface StorageItemCacheAdd { readonly cache : BN ; } export interface StorageItemValueAdd { readonly value : Buffer ; } const isStorageItemCacheAdd = ( value ) : value is StorageItemCacheAdd => value ?. cache !== undefined && BN . isBN ( value . cache ) ; type StorageItemAdd = StorageItemCacheAdd | StorageItemValueAdd ; export class StorageItem implements SerializableWire { public static deserializeWireBase ( options ) { const { reader } = options ; const value = reader . readBytes ( reader . buffer . length ) ; return new this ( { value } ) ; } public static deserializeWire ( options ) { return this . deserializeWireBase ( { context : options . context , reader : new BinaryReader ( options . buffer ) } ) ; } public readonly serializeWire = createSerializeWire ( this . serializeWireBase . bind ( this ) ) ; private mutableValue : Buffer | undefined ; private mutableCache : BN | undefined ; public constructor ( options ) { this . mutableValue = isStorageItemCacheAdd ( options ) ? undefined : options . value ; this . mutableCache = isStorageItemCacheAdd ( options ) ? options . cache : undefined ; } public get value ( ) : Buffer { if ( this . mutableValue !== undefined ) { return this . mutableValue ; } if ( this . mutableCache === undefined ) { throw new InvalidFormatError ( ) ; } this . mutableValue = this . mutableCache . toBuffer ( ) ; this . mutableCache = undefined ; return this . mutableValue ; } public set value ( value ) { this . mutableValue = value ; this . mutableCache = undefined ; } public set ( integer ) { this . mutableCache = integer ; this . mutableValue = undefined ; } public add ( integer ) { if ( BN . isBN ( this . mutableCache ) ) { this . set ( this . mutableCache . add ( integer ) ) ; } else if ( this . mutableValue !== undefined ) { this . set ( new BN ( this . mutableValue ) . add ( integer ) ) ; } else { throw new InvalidFormatError ( ) ; } } public serializeWireBase ( writer ) { writer . writeBytes ( this . value ) ; } public clone ( ) { return new StorageItem ( { value : this . value } ) ; } public serializeJSON ( key ) { return { key : JSONHelper . writeBase64Buffer ( key . serializeWire ( ) ) , value : JSONHelper . writeBase64Buffer ( this . value ) } ; } }