Skip to content

Commit

Permalink
feat: implement Box via context manager
Browse files Browse the repository at this point in the history
  • Loading branch information
boblat authored and tristanmenzel committed Oct 30, 2024
1 parent a8d41e2 commit 11c0c15
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 11 deletions.
12 changes: 6 additions & 6 deletions packages/algo-ts/src/box.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ctxMgr } from './execution-context'
import { bytes, uint64 } from './primitives'

export type Box<TValue> = {
Expand All @@ -24,12 +25,11 @@ export type BoxMap<TKey, TValue> = {

export type BoxRef = {
readonly key: bytes

readonly exists: boolean
value: bytes
get(options: { default: bytes }): bytes
put(value: bytes): bytes
splice(start: uint64, end: uint64, value: bytes): void
put(value: bytes): void
splice(start: uint64, length: uint64, value: bytes): void
replace(start: uint64, value: bytes): void
extract(start: uint64, length: uint64): bytes
delete(): boolean
Expand All @@ -40,13 +40,13 @@ export type BoxRef = {
}

export function Box<TValue>(options: { key: bytes | string }): Box<TValue> {
throw new Error('Not implemented')
return ctxMgr.instance.state.Box(options)
}

export function BoxMap<TKey, TValue>(options: { keyPrefix: bytes | string }): BoxMap<TKey, TValue> {
throw new Error('Not implemented')
return ctxMgr.instance.state.BoxMap(options)
}

export function BoxRef(options: { key: bytes | string }): BoxRef {
throw new Error('Not implemented')
return ctxMgr.instance.state.BoxRef(options)
}
9 changes: 6 additions & 3 deletions packages/algo-ts/src/execution-context.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Contract, GlobalState, gtxn, itxn, LocalState } from '.'
import { Box, BoxMap, BoxRef, Contract, GlobalState, gtxn, itxn, LocalState } from '.'
import { AbiMethodConfig, BareMethodConfig } from './arc4'
import { OpsNamespace } from './op-types'
import { bytes, uint64 } from './primitives'
Expand Down Expand Up @@ -32,8 +32,11 @@ export type ExecutionContext = {
applicationCall: typeof itxn.applicationCall
}
state: {
createGlobalState: typeof GlobalState
createLocalState: typeof LocalState
GlobalState: typeof GlobalState
LocalState: typeof LocalState
Box: typeof Box
BoxMap: typeof BoxMap
BoxRef: typeof BoxRef
}
}

Expand Down
1 change: 1 addition & 0 deletions packages/algo-ts/src/op.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,5 +138,6 @@ export const AppParams = createObjectProxy('AppParams')
export const AssetHolding = createObjectProxy('AssetHolding')
export const AssetParams = createObjectProxy('AssetParams')
export const Block = createObjectProxy('Block')
export const Box = createObjectProxy('Box')

export { VrfVerify } from './op-types'
4 changes: 2 additions & 2 deletions packages/algo-ts/src/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export type GlobalStateOptions<ValueType> = { key?: bytes | string; initialValue

/** A single key in global state */
export function GlobalState<ValueType>(options?: GlobalStateOptions<ValueType>): GlobalState<ValueType> {
return ctxMgr.instance.state.createGlobalState(options)
return ctxMgr.instance.state.GlobalState(options)
}

/** A value saved in local state */
Expand All @@ -29,5 +29,5 @@ export type LocalState<ValueType> = {

/** A single key in local state */
export function LocalState<ValueType>(options?: { key?: bytes | string }): LocalState<ValueType> {
return ctxMgr.instance.state.createLocalState(options)
return ctxMgr.instance.state.LocalState(options)
}

0 comments on commit 11c0c15

Please sign in to comment.