Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: export stub implementation of op codes under internal namespace #41

Merged
merged 1 commit into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/decode-logs.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { bytes } from '@algorandfoundation/algorand-typescript'
import { op } from '@algorandfoundation/algorand-typescript'
import { ABI_RETURN_VALUE_LOG_PREFIX } from './constants'
import { btoi } from './impl/pure'
import { asNumber } from './util'

export type LogDecoding = 'i' | 's' | 'b'
Expand All @@ -18,7 +18,7 @@ export function decodeLogs<const T extends [...LogDecoding[]]>(logs: bytes[], de
: log
switch (decoding[i]) {
case 'i':
return op.btoi(value)
return btoi(value)
case 's':
return value.toString()
default:
Expand Down
5 changes: 2 additions & 3 deletions src/impl/acct-params.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { gtxn, internal, uint64 } from '@algorandfoundation/algorand-typescript'
import type { Account, Application } from '@algorandfoundation/algorand-typescript'
import type { Account, Application, gtxn, internal, op, uint64 } from '@algorandfoundation/algorand-typescript'
import { lazyContext } from '../context-helpers/internal-context'
import { asMaybeUint64Cls } from '../util'
import { getApp } from './app-params'
Expand Down Expand Up @@ -37,7 +36,7 @@ export const appOptedIn = (
return account.isOptedIn(app)
}

export const AcctParams: internal.opTypes.AcctParamsType = {
export const AcctParams: typeof op.AcctParams = {
acctBalance(a: Account | internal.primitives.StubUint64Compat): readonly [uint64, boolean] {
const acct = getAccount(a)
return [acct.balance, acct.balance !== 0]
Expand Down
4 changes: 2 additions & 2 deletions src/impl/app-global.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { Application, bytes, internal, uint64 } from '@algorandfoundation/algorand-typescript'
import type { Application, bytes, internal, op, uint64 } from '@algorandfoundation/algorand-typescript'
import { Bytes, Uint64 } from '@algorandfoundation/algorand-typescript'
import { lazyContext } from '../context-helpers/internal-context'
import { toBytes } from '../encoders'
import { asBytes } from '../util'
import { getApp } from './app-params'

export const AppGlobal: internal.opTypes.AppGlobalType = {
export const AppGlobal: typeof op.AppGlobal = {
delete(a: internal.primitives.StubBytesCompat): void {
lazyContext.ledger.setGlobalState(lazyContext.activeApplication, a, undefined)
},
Expand Down
4 changes: 2 additions & 2 deletions src/impl/app-local.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type { bytes, internal, uint64, Account, Application } from '@algorandfoundation/algorand-typescript'
import type { Account, Application, bytes, internal, op, uint64 } from '@algorandfoundation/algorand-typescript'
import { Bytes, Uint64 } from '@algorandfoundation/algorand-typescript'
import { lazyContext } from '../context-helpers/internal-context'
import { toBytes } from '../encoders'
import { asBytes } from '../util'
import { getAccount } from './acct-params'
import { getApp } from './app-params'

export const AppLocal: internal.opTypes.AppLocalType = {
export const AppLocal: typeof op.AppLocal = {
delete: function (a: Account | internal.primitives.StubUint64Compat, b: internal.primitives.StubBytesCompat): void {
const app = lazyContext.activeApplication
const account = getAccount(a)
Expand Down
7 changes: 4 additions & 3 deletions src/impl/app-params.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import type {
Account as AccountType,
Application as ApplicationType,
bytes,
gtxn,
internal,
op,
uint64,
Account as AccountType,
Application as ApplicationType,
} from '@algorandfoundation/algorand-typescript'
import { Bytes, Uint64 } from '@algorandfoundation/algorand-typescript'
import { lazyContext } from '../context-helpers/internal-context'
Expand Down Expand Up @@ -32,7 +33,7 @@ export const getApp = (app: ApplicationType | internal.primitives.StubUint64Comp
}
}

export const AppParams: internal.opTypes.AppParamsType = {
export const AppParams: typeof op.AppParams = {
appApprovalProgram(a: ApplicationType | internal.primitives.StubUint64Compat): readonly [bytes, boolean] {
const app = getApp(a)
return app === undefined ? [Bytes(), false] : [app.approvalProgram, true]
Expand Down
4 changes: 2 additions & 2 deletions src/impl/asset-holding.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { internal, uint64, Account, Asset } from '@algorandfoundation/algorand-typescript'
import type { Account, Asset, internal, op, uint64 } from '@algorandfoundation/algorand-typescript'
import { Uint64 } from '@algorandfoundation/algorand-typescript'
import { lazyContext } from '../context-helpers/internal-context'
import { getAccount } from './acct-params'
Expand All @@ -23,7 +23,7 @@ const getAssetHolding = (
return holding
}

export const AssetHolding: internal.opTypes.AssetHoldingType = {
export const AssetHolding: typeof op.AssetHolding = {
assetBalance(
a: Account | internal.primitives.StubUint64Compat,
b: Asset | internal.primitives.StubUint64Compat,
Expand Down
4 changes: 2 additions & 2 deletions src/impl/asset-params.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { bytes, gtxn, internal, uint64 } from '@algorandfoundation/algorand-typescript'
import type { bytes, gtxn, internal, op, uint64 } from '@algorandfoundation/algorand-typescript'
import { type Account as AccountType, type Asset as AssetType, Bytes, Uint64 } from '@algorandfoundation/algorand-typescript'
import { lazyContext } from '../context-helpers/internal-context'
import { asMaybeUint64Cls, asUint64 } from '../util'
Expand All @@ -25,7 +25,7 @@ export const getAsset = (asset: AssetType | internal.primitives.StubUint64Compat
}
}

export const AssetParams: internal.opTypes.AssetParamsType = {
export const AssetParams: typeof op.AssetParams = {
assetTotal(a: AssetType | internal.primitives.StubUint64Compat): readonly [uint64, boolean] {
const asset = getAsset(a)
return asset === undefined ? [Uint64(0), false] : [asset.total, true]
Expand Down
4 changes: 2 additions & 2 deletions src/impl/block.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { bytes, internal, uint64, Account as AccountType } from '@algorandfoundation/algorand-typescript'
import type { Account as AccountType, bytes, internal, op, uint64 } from '@algorandfoundation/algorand-typescript'
import { Uint64 } from '@algorandfoundation/algorand-typescript'
import { lazyContext } from '../context-helpers/internal-context'
import { asUint64, getRandomBytes } from '../util'
Expand Down Expand Up @@ -30,7 +30,7 @@ export class BlockData {
}
}

export const Block: internal.opTypes.BlockType = {
export const Block: typeof op.Block = {
blkSeed: function (a: internal.primitives.StubUint64Compat): bytes {
return lazyContext.ledger.getBlockData(a).seed
},
Expand Down
4 changes: 2 additions & 2 deletions src/impl/box.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { bytes, uint64 } from '@algorandfoundation/algorand-typescript'
import type { bytes, op, uint64 } from '@algorandfoundation/algorand-typescript'
import { internal } from '@algorandfoundation/algorand-typescript'
import { MAX_BOX_SIZE } from '../constants'
import { lazyContext } from '../context-helpers/internal-context'
import { toBytes } from '../encoders'
import { asBytes, asBytesCls, asNumber, asUint8Array, conactUint8Arrays } from '../util'

export const Box: internal.opTypes.BoxType = {
export const Box: typeof op.Box = {
create(a: internal.primitives.StubBytesCompat, b: internal.primitives.StubUint64Compat): boolean {
const name = asBytes(a)
const size = asNumber(b)
Expand Down
4 changes: 2 additions & 2 deletions src/impl/crypto.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { bytes, gtxn, MimcConfigurations, VrfVerify } from '@algorandfoundation/algorand-typescript'
import type { bytes, gtxn, MimcConfigurations, op, VrfVerify } from '@algorandfoundation/algorand-typescript'
import { arc4, Bytes, Ecdsa, internal } from '@algorandfoundation/algorand-typescript'
import elliptic from 'elliptic'
import js_sha256 from 'js-sha256'
Expand Down Expand Up @@ -138,7 +138,7 @@ export const vrfVerify = (
notImplementedError('vrfVerify')
}

export const EllipticCurve = new Proxy({} as internal.opTypes.EllipticCurveType, {
export const EllipticCurve = new Proxy({} as typeof op.EllipticCurve, {
get: (_target, prop) => {
notImplementedError(`EllipticCurve.${prop.toString()}`)
},
Expand Down
8 changes: 7 additions & 1 deletion src/impl/encoded-types.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import type {
Account as AccountType,
arc4,
BigUintCompat,
bytes,
StringCompat,
uint64,
Uint64Compat,
Account as AccountType,
} from '@algorandfoundation/algorand-typescript'
import { Bytes, internal } from '@algorandfoundation/algorand-typescript'
import type { BitSize } from '@algorandfoundation/algorand-typescript/arc4'
Expand Down Expand Up @@ -36,6 +37,7 @@ import { lazyContext } from '../context-helpers/internal-context'
import type { fromBytes, TypeInfo } from '../encoders'
import type { DeliberateAny } from '../typescript-helpers'
import { asBigInt, asBigUint, asBigUintCls, asBytesCls, asUint64, asUint8Array, conactUint8Arrays, uint8ArrayToNumber } from '../util'
import { sha512_256 } from './crypto'
import { Account, AccountCls, ApplicationCls, AssetCls } from './reference'
import type { ApplicationTransaction } from './transactions'

Expand Down Expand Up @@ -1278,3 +1280,7 @@ export const getArc4Encoded = (value: DeliberateAny): ARC4Encoded => {

throw internal.errors.codeError(`Unsupported type for encoding: ${typeof value}`)
}

export const methodSelector: typeof arc4.methodSelector = (methodSignature: string): bytes => {
return sha512_256(Bytes(encodingUtil.utf8ToUint8Array(methodSignature))).slice(0, 4)
}
9 changes: 5 additions & 4 deletions src/impl/global.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { bytes, uint64, Account as AccountType, Application as ApplicationType } from '@algorandfoundation/algorand-typescript'
import { Bytes, internal, op, Uint64 } from '@algorandfoundation/algorand-typescript'
import type { Account as AccountType, Application as ApplicationType, bytes, uint64, op } from '@algorandfoundation/algorand-typescript'
import { Bytes, internal, Uint64 } from '@algorandfoundation/algorand-typescript'
import {
DEFAULT_ACCOUNT_MIN_BALANCE,
DEFAULT_ASSET_CREATE_MIN_BALANCE,
Expand All @@ -11,6 +11,7 @@ import {
} from '../constants'
import { lazyContext } from '../context-helpers/internal-context'
import { getObjectReference } from '../util'
import { sha256 } from './crypto'
import { Account, getApplicationAddress } from './reference'

export class GlobalData {
Expand Down Expand Up @@ -54,7 +55,7 @@ const getGlobalData = (): GlobalData => {
const getMissingValueErrorMessage = (name: keyof GlobalData) =>
`'Global' object has no value set for attribute named '${name}'. Use \`context.ledger.patchGlobalData({${name}: your_value})\` to set the value in your test setup."`

export const Global: internal.opTypes.GlobalType = {
export const Global: typeof op.Global = {
/**
* microalgos
*/
Expand Down Expand Up @@ -148,7 +149,7 @@ export const Global: internal.opTypes.GlobalType = {
if (data.groupId !== undefined) return data.groupId
const reference = getObjectReference(lazyContext.activeGroup)
const referenceBytes = Bytes(internal.encodingUtil.bigIntToUint8Array(reference))
return op.sha256(referenceBytes)
return sha256(referenceBytes)
},

/**
Expand Down
4 changes: 2 additions & 2 deletions src/impl/gtxn.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { bytes, internal, uint64, Account, Application, Asset } from '@algorandfoundation/algorand-typescript'
import type { Account, Application, Asset, bytes, internal, op, uint64 } from '@algorandfoundation/algorand-typescript'
import { arc4 } from '@algorandfoundation/algorand-typescript'
import { lazyContext } from '../context-helpers/internal-context'
import { asUint64, asUint64Cls } from '../util'

export const GTxn: internal.opTypes.GTxnType = {
export const GTxn: typeof op.GTxn = {
sender(t: internal.primitives.StubUint64Compat): Account {
return lazyContext.activeGroup.getTransaction(t).sender
},
Expand Down
18 changes: 14 additions & 4 deletions src/impl/itxn.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
import type { bytes, internal, itxn, TransactionType, uint64, Account, Application, Asset } from '@algorandfoundation/algorand-typescript'
import type {
Account,
Application,
Asset,
bytes,
internal,
itxn,
op,
TransactionType,
uint64,
} from '@algorandfoundation/algorand-typescript'
import { arc4 } from '@algorandfoundation/algorand-typescript'
import { lazyContext } from '../context-helpers/internal-context'
import { asBytes, asBytesCls, asUint64, asUint64Cls } from '../util'
Expand All @@ -22,7 +32,7 @@ export type InnerTxnFields = (
| itxn.ApplicationCallFields
) & { type?: TransactionType }

export const GITxn: internal.opTypes.GITxnType = {
export const GITxn: typeof op.GITxn = {
sender: function (t: internal.primitives.StubUint64Compat): Account {
return lazyContext.activeGroup.getItxnGroup().getInnerTxn(t).sender
},
Expand Down Expand Up @@ -229,7 +239,7 @@ export const GITxn: internal.opTypes.GITxnType = {
return lazyContext.activeGroup.getItxnGroup().getApplicationInnerTxn(t).numClearStateProgramPages
},
}
export const ITxn: internal.opTypes.ITxnType = {
export const ITxn: typeof op.ITxn = {
/**
* 32 byte address
*/
Expand Down Expand Up @@ -649,7 +659,7 @@ const getConstructingItxn = <T extends InnerTxnFields>(): T => {
return lazyContext.activeGroup.constructingItxn as T
}

export const ITxnCreate: internal.opTypes.ITxnCreateType = {
export const ITxnCreate: typeof op.ITxnCreate = {
begin: function (): void {
lazyContext.activeGroup.beginInnerTransactionGroup()
},
Expand Down
4 changes: 2 additions & 2 deletions src/impl/online-stake.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { internal } from '@algorandfoundation/algorand-typescript'
import type { op } from '@algorandfoundation/algorand-typescript'
import { lazyContext } from '../context-helpers/internal-context'

export const onlineStake: internal.opTypes.OnlineStakeType = () => {
export const onlineStake: typeof op.onlineStake = () => {
return lazyContext.ledger.onlineStake
}
4 changes: 2 additions & 2 deletions src/impl/pure.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { biguint, bytes, uint64 } from '@algorandfoundation/algorand-typescript'
import type { biguint, bytes, op, uint64 } from '@algorandfoundation/algorand-typescript'
import { Base64, Bytes, internal, Uint64 } from '@algorandfoundation/algorand-typescript'
import { BITS_IN_BYTE, MAX_BYTES_SIZE, MAX_UINT64, MAX_UINT8, UINT64_SIZE } from '../constants'
import { notImplementedError, testInvariant } from '../errors'
Expand Down Expand Up @@ -329,7 +329,7 @@ export const substring = (
return bytesValue.slice(start, end).asAlgoTs()
}

export const JsonRef = new Proxy({} as internal.opTypes.JsonRefType, {
export const JsonRef = new Proxy({} as typeof op.JsonRef, {
get: (_target, prop) => {
notImplementedError(`JsonRef.${prop.toString()}`)
},
Expand Down
8 changes: 4 additions & 4 deletions src/impl/scratch.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { bytes, uint64 } from '@algorandfoundation/algorand-typescript'
import type { bytes, op, uint64 } from '@algorandfoundation/algorand-typescript'
import { internal } from '@algorandfoundation/algorand-typescript'
import { lazyContext } from '../context-helpers/internal-context'

export const gloadUint64: internal.opTypes.GloadUint64Type = (
export const gloadUint64: typeof op.gloadUint64 = (
a: internal.primitives.StubUint64Compat,
b: internal.primitives.StubUint64Compat,
): uint64 => {
Expand All @@ -14,7 +14,7 @@ export const gloadUint64: internal.opTypes.GloadUint64Type = (
throw new internal.errors.InternalError('invalid scratch slot type')
}

export const gloadBytes: internal.opTypes.GloadBytesType = (
export const gloadBytes: typeof op.gloadBytes = (
a: internal.primitives.StubUint64Compat,
b: internal.primitives.StubUint64Compat,
): bytes => {
Expand All @@ -26,7 +26,7 @@ export const gloadBytes: internal.opTypes.GloadBytesType = (
throw new internal.errors.InternalError('invalid scratch slot type')
}

export const Scratch: internal.opTypes.ScratchType = {
export const Scratch: typeof op.Scratch = {
loadBytes: function (a: internal.primitives.StubUint64Compat): bytes {
const result = lazyContext.activeGroup.activeTransaction.getScratchSlot(a)
if (result instanceof internal.primitives.BytesCls) {
Expand Down
4 changes: 2 additions & 2 deletions src/impl/txn.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { bytes, uint64, Account, Application, Asset } from '@algorandfoundation/algorand-typescript'
import type { Account, Application, Asset, bytes, op, uint64 } from '@algorandfoundation/algorand-typescript'
import { arc4, internal, TransactionType } from '@algorandfoundation/algorand-typescript'
import { lazyContext } from '../context-helpers/internal-context'
import { asNumber, asUint64, asUint64Cls } from '../util'
Expand All @@ -15,7 +15,7 @@ export const gaid = (a: internal.primitives.StubUint64Compat): uint64 => {
}
}

export const Txn: internal.opTypes.TxnType = {
export const Txn: typeof op.Txn = {
get sender(): Account {
return lazyContext.activeGroup.getTransaction().sender
},
Expand Down
5 changes: 2 additions & 3 deletions src/impl/voter-params.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { internal, uint64 } from '@algorandfoundation/algorand-typescript'
import type { Account } from '@algorandfoundation/algorand-typescript'
import type { Account, internal, op, uint64 } from '@algorandfoundation/algorand-typescript'
import { lazyContext } from '../context-helpers/internal-context'
import { getAccount } from './acct-params'

Expand All @@ -18,7 +17,7 @@ const getVoterData = (a: Account | internal.primitives.StubUint64Compat): VoterD
return lazyContext.getVoterData(acct)
}

export const VoterParams: internal.opTypes.VoterParamsType = {
export const VoterParams: typeof op.VoterParams = {
voterBalance: function (a: Account | internal.primitives.StubUint64Compat): readonly [uint64, boolean] {
const data = getVoterData(a)
return [data.balance, data.balance !== 0]
Expand Down
1 change: 1 addition & 0 deletions src/internal-arc4.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from '@algorandfoundation/algorand-typescript/arc4'
export { methodSelector } from './impl/encoded-types'
Loading
Loading