@@ -9,6 +9,7 @@ import type {
9
9
} from '@algorandfoundation/algorand-typescript'
10
10
import { AccountMap , Uint64Map } from '../collections/custom-key-map'
11
11
import { MAX_UINT64 } from '../constants'
12
+ import { toBytes } from '../encoders'
12
13
import { InternalError } from '../errors'
13
14
import { BlockData } from '../impl/block'
14
15
import { GlobalData } from '../impl/global'
@@ -340,9 +341,26 @@ export class LedgerContext {
340
341
getBox ( app : ApplicationType | BaseContract , key : StubBytesCompat ) : Uint8Array {
341
342
const appId = this . getAppId ( app )
342
343
const appData = this . applicationDataMap . getOrFail ( appId )
344
+ const materialised = appData . application . materialisedBoxes . get ( key )
345
+ if ( materialised !== undefined ) {
346
+ return asUint8Array ( toBytes ( materialised ) )
347
+ }
343
348
return appData . application . boxes . get ( key ) ?? new Uint8Array ( )
344
349
}
345
350
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
+
346
364
/**
347
365
* Sets a box for an application by key.
348
366
* @param app - The application.
@@ -354,6 +372,21 @@ export class LedgerContext {
354
372
const appData = this . applicationDataMap . getOrFail ( appId )
355
373
const uint8ArrayValue = value instanceof Uint8Array ? value : asUint8Array ( value )
356
374
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 )
357
390
}
358
391
359
392
/**
@@ -365,6 +398,7 @@ export class LedgerContext {
365
398
deleteBox ( app : ApplicationType | BaseContract , key : StubBytesCompat ) : boolean {
366
399
const appId = this . getAppId ( app )
367
400
const appData = this . applicationDataMap . getOrFail ( appId )
401
+ appData . application . materialisedBoxes . delete ( key )
368
402
return appData . application . boxes . delete ( key )
369
403
}
370
404
0 commit comments