Skip to content

Commit

Permalink
Merge pull request #58 from algorandfoundation/feat-arc4-types-in-tests
Browse files Browse the repository at this point in the history
refactor: support including arc4 types in tests in testing package
  • Loading branch information
boblat authored Dec 18, 2024
2 parents 7e0cb8d + 1d7748f commit 895dfbb
Show file tree
Hide file tree
Showing 9 changed files with 278 additions and 260 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": "0.0.1-alpha.21",
"version": "0.0.1-alpha.22",
"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
7 changes: 4 additions & 3 deletions packages/algo-ts/src/arc4/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { BaseContract } from '../base-contract'
import { ctxMgr } from '../execution-context'
import { NoImplementation } from '../impl/errors'
import { bytes, Uint64 } from '../primitives'
import { encodingUtil } from '../internal'
import { sha512_256 } from '../op'
import { Bytes, bytes, Uint64 } from '../primitives'
import { DeliberateAny } from '../typescript-helpers'

export * from './encoded-types'
Expand Down Expand Up @@ -91,5 +92,5 @@ export function baremethod<TContract extends Contract>(config?: BareMethodConfig
* @returns The ARC4 method selector. Eg. `02BECE11`
*/
export function methodSelector(methodSignature: string): bytes {
throw new NoImplementation()
return sha512_256(Bytes(encodingUtil.utf8ToUint8Array(methodSignature))).slice(0, 4)
}
12 changes: 11 additions & 1 deletion packages/algo-ts/src/base-contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,16 @@ type ContractOptions = {
* The contract decorator can be used to specify additional configuration options for a smart contract
* @param options An object containing the configuration options
*/
export const ContractOptionsSymbol = Symbol('ContractOptions')
export function contract(options: ContractOptions) {
return <T extends ConstructorFor<Contract>>(contract: T) => contract
return <T extends ConstructorFor<Contract>>(contract: T, ctx: ClassDecoratorContext) => {
ctx.addInitializer(function () {
Object.defineProperty(this, ContractOptionsSymbol, {
value: options,
writable: false,
enumerable: false,
})
})
return contract
}
}
14 changes: 0 additions & 14 deletions packages/algo-ts/src/impl/primitives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,6 @@ export function toExternalValue(val: uint64 | biguint | bytes | string) {
if (instance instanceof BigUintCls) return instance.asBigInt()
if (typeof val === 'string') return val
}
export const toBytes = (val: unknown): bytes => {
if (val instanceof AlgoTsPrimitiveCls) return val.toBytes().asAlgoTs()

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)}`)
}
}

/**
* Convert a StubUint64Compat value into a 'number' if possible.
Expand Down
1 change: 1 addition & 0 deletions packages/algo-ts/src/internal.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { ContractOptionsSymbol } from './base-contract'
export { ExecutionContext, ctxMgr } from './execution-context'
export * as encodingUtil from './impl/encoding-util'
export * as errors from './impl/errors'
Expand Down
Loading

0 comments on commit 895dfbb

Please sign in to comment.