Skip to content

Commit

Permalink
refactor: avoid using ctxMgr for stubbing Account, Application, Asset…
Browse files Browse the repository at this point in the history
…, and log functions
  • Loading branch information
boblat committed Jan 21, 2025
1 parent 32892bd commit 41cdd7e
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 35 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/algo-ts/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@algorandfoundation/algorand-typescript",
"version": "1.0.0-beta.9",
"version": "1.0.0-beta.10",
"description": "This package contains definitions for the types which comprise Algorand TypeScript which can be compiled to run on the Algorand Virtual Machine using the Puya compiler.",
"private": false,
"main": "index.js",
Expand Down
6 changes: 0 additions & 6 deletions packages/algo-ts/src/execution-context.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
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'
import { Account, Application, Asset } from './reference'

export type ExecutionContext = {
log(value: bytes): void
application(id?: uint64): Application
asset(id?: uint64): Asset
account(address?: bytes): Account
op: Partial<OpsNamespace>
abiMetadata: {
captureMethodConfig<T extends Contract>(contract: T, methodName: string, config?: AbiMethodConfig<T> | BareMethodConfig): void
Expand Down
8 changes: 4 additions & 4 deletions packages/algo-ts/src/reference.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ctxMgr } from './execution-context'
import { NoImplementation } from './impl/errors'
import { bytes, uint64 } from './primitives'

export type Account = {
Expand Down Expand Up @@ -100,13 +100,13 @@ export type Account = {
export function Account(): Account
export function Account(address: bytes): Account
export function Account(address?: bytes): Account {
return ctxMgr.instance.account(address)
throw new NoImplementation()
}

export function Asset(): Asset
export function Asset(assetId: uint64): Asset
export function Asset(assetId?: uint64): Asset {
return ctxMgr.instance.asset(assetId)
throw new NoImplementation()
}
/**
* An Asset on the Algorand network.
Expand Down Expand Up @@ -199,7 +199,7 @@ export type Asset = {
export function Application(): Application
export function Application(applicationId: uint64): Application
export function Application(applicationId?: uint64): Application {
return ctxMgr.instance.application(applicationId)
throw new NoImplementation()
}

/**
Expand Down
26 changes: 3 additions & 23 deletions packages/algo-ts/src/util.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,8 @@
import { ARC4Encoded } from './arc4'
import { ctxMgr } from './execution-context'
import { AssertError, AvmError, internalError } from './impl/errors'
import { nameOfType } from './impl/name-of-type'
import { AlgoTsPrimitiveCls, BigUintCls, BytesCls, Uint64Cls } from './impl/primitives'
import { biguint, BigUintCompat, bytes, BytesBacked, BytesCompat, StringCompat, uint64, Uint64Compat } from './primitives'

export const toBytes = (val: unknown): bytes => {
if (val instanceof AlgoTsPrimitiveCls) return val.toBytes().asAlgoTs()
if (val instanceof ARC4Encoded) return val.bytes

switch (typeof val) {
case 'string':
return BytesCls.fromCompat(val).asAlgoTs()
case 'bigint':
return BigUintCls.fromCompat(val).toBytes().asAlgoTs()
case 'number':
return Uint64Cls.fromCompat(val).toBytes().asAlgoTs()
default:
internalError(`Unsupported arg type ${nameOfType(val)}`)
}
}
import { AssertError, AvmError, NoImplementation } from './impl/errors'
import { biguint, BigUintCompat, BytesBacked, BytesCompat, StringCompat, uint64, Uint64Compat } from './primitives'

export function log(...args: Array<Uint64Compat | BytesCompat | BigUintCompat | StringCompat | BytesBacked>): void {
ctxMgr.instance.log(args.map(toBytes).reduce((left, right) => left.concat(right)))
throw new NoImplementation()
}

export function assert(condition: unknown, message?: string): asserts condition {
Expand Down

0 comments on commit 41cdd7e

Please sign in to comment.