Skip to content

Commit

Permalink
refactor: move state implementation to testing package
Browse files Browse the repository at this point in the history
  • Loading branch information
boblat committed Oct 17, 2024
1 parent 6e34ad2 commit 759d18c
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 92 deletions.
6 changes: 5 additions & 1 deletion packages/algo-ts/src/execution-context.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Contract, gtxn, itxn } from '.'
import { 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 @@ -31,6 +31,10 @@ export type ExecutionContext = {
assetFreeze: typeof itxn.assetFreeze
applicationCall: typeof itxn.applicationCall
}
state: {
createGlobalState: typeof GlobalState
createLocalState: typeof LocalState
}
}

declare global {
Expand Down
81 changes: 0 additions & 81 deletions packages/algo-ts/src/impl/state.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/algo-ts/src/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@ export { ExecutionContext, ctxMgr } from './execution-context'
export * as encodingUtil from './impl/encoding-util'
export * as errors from './impl/errors'
export * as primitives from './impl/primitives'
export * as state from './impl/state'
export * as opTypes from './op-types'
13 changes: 4 additions & 9 deletions packages/algo-ts/src/state.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { GlobalStateCls, LocalStateMapCls } from './impl/state'
import { ctxMgr } from './execution-context'
import { bytes } from './primitives'
import { Account } from './reference'

Expand All @@ -9,11 +9,11 @@ export type GlobalState<ValueType> = {
hasValue: boolean
}

type GlobalStateOptions<ValueType> = { key?: bytes | string; initialValue?: ValueType }
export type GlobalStateOptions<ValueType> = { key?: bytes | string; initialValue?: ValueType }

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

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

/** A single key in local state */
export function LocalState<ValueType>(options?: { key?: bytes | string }): LocalState<ValueType> {
function localStateInternal(account: Account): LocalStateForAccount<ValueType> {
return localStateInternal.map.getValue(account)
}
localStateInternal.key = options?.key
localStateInternal.map = new LocalStateMapCls<ValueType>()
return localStateInternal
return ctxMgr.instance.state.createLocalState(options)
}

0 comments on commit 759d18c

Please sign in to comment.