@@ -9,6 +9,7 @@ import type {
99} from '@algorandfoundation/algorand-typescript'
1010import { AccountMap , Uint64Map } from '../collections/custom-key-map'
1111import { MAX_UINT64 } from '../constants'
12+ import { toBytes } from '../encoders'
1213import { InternalError } from '../errors'
1314import { BlockData } from '../impl/block'
1415import { GlobalData } from '../impl/global'
@@ -340,9 +341,26 @@ export class LedgerContext {
340341 getBox ( app : ApplicationType | BaseContract , key : StubBytesCompat ) : Uint8Array {
341342 const appId = this . getAppId ( app )
342343 const appData = this . applicationDataMap . getOrFail ( appId )
344+ const materialised = appData . application . materialisedBoxes . get ( key )
345+ if ( materialised !== undefined ) {
346+ return asUint8Array ( toBytes ( materialised ) )
347+ }
343348 return appData . application . boxes . get ( key ) ?? new Uint8Array ( )
344349 }
345350
351+ /**
352+ * Retrieves a materialised box for an application by key.
353+ * @internal
354+ * @param app - The application.
355+ * @param key - The key.
356+ * @returns The materialised box data if exists or undefined.
357+ */
358+ getMaterialisedBox < T > ( app : ApplicationType | BaseContract , key : StubBytesCompat ) : T | undefined {
359+ const appId = this . getAppId ( app )
360+ const appData = this . applicationDataMap . getOrFail ( appId )
361+ return appData . application . materialisedBoxes . get ( key ) as T | undefined
362+ }
363+
346364 /**
347365 * Sets a box for an application by key.
348366 * @param app - The application.
@@ -354,6 +372,21 @@ export class LedgerContext {
354372 const appData = this . applicationDataMap . getOrFail ( appId )
355373 const uint8ArrayValue = value instanceof Uint8Array ? value : asUint8Array ( value )
356374 appData . application . boxes . set ( key , uint8ArrayValue )
375+ appData . application . materialisedBoxes . set ( key , undefined )
376+ }
377+
378+ /**
379+
380+ * Cache the materialised box for an application by key.
381+ * @internal
382+ * @param app - The application.
383+ * @param key - The key.
384+ * @param value - The box data.
385+ */
386+ setMatrialisedBox < TValue > ( app : ApplicationType | BaseContract , key : StubBytesCompat , value : TValue | undefined ) : void {
387+ const appId = this . getAppId ( app )
388+ const appData = this . applicationDataMap . getOrFail ( appId )
389+ appData . application . materialisedBoxes . set ( key , value )
357390 }
358391
359392 /**
@@ -365,6 +398,7 @@ export class LedgerContext {
365398 deleteBox ( app : ApplicationType | BaseContract , key : StubBytesCompat ) : boolean {
366399 const appId = this . getAppId ( app )
367400 const appData = this . applicationDataMap . getOrFail ( appId )
401+ appData . application . materialisedBoxes . delete ( key )
368402 return appData . application . boxes . delete ( key )
369403 }
370404
0 commit comments