-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move tests for primitives out into puya-ts
- Loading branch information
Showing
8 changed files
with
98 additions
and
10 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import * as algokit from '@algorandfoundation/algokit-utils' | ||
import { ABIAppCallArg, ABIReturn } from '@algorandfoundation/algokit-utils/types/app' | ||
import { ApplicationClient } from '@algorandfoundation/algokit-utils/types/app-client' | ||
import { AppSpec } from '@algorandfoundation/algokit-utils/types/app-spec' | ||
import { nullLogger } from '@algorandfoundation/algokit-utils/types/logging' | ||
import { SendTransactionParams } from '@algorandfoundation/algokit-utils/types/transaction' | ||
import { ABIValue } from 'algosdk' | ||
import { randomUUID } from 'crypto' | ||
import { Lazy } from './util' | ||
|
||
algokit.Config.configure({ logger: nullLogger }) | ||
const ARC4_PREFIX_LENGTH = 2 | ||
|
||
const algorandClient = Lazy(() => algokit.AlgorandClient.defaultLocalNet()) | ||
|
||
export const INITIAL_BALANCE_MICRO_ALGOS = Number(20e6) | ||
|
||
export const getAlgorandAppClient = async (appSpec: AppSpec) => { | ||
const client = algorandClient() | ||
const defaultSigner = await client.account.kmd.getLocalNetDispenserAccount() | ||
const appClient = algokit.getAppClient({ app: appSpec, resolveBy: 'id', id: 0, sender: defaultSigner.account }, client.client.algod) | ||
await appClient.create({ note: randomUUID() }) | ||
return appClient | ||
} | ||
|
||
const inovkeMethod = async ( | ||
appClient: ApplicationClient, | ||
method: string, | ||
sendParams?: SendTransactionParams, | ||
...methodArgs: ABIAppCallArg[] | ||
): Promise<ABIReturn> => { | ||
const response = await appClient.call({ method, methodArgs, note: randomUUID(), sendParams }) | ||
if (!response.return) { | ||
throw new Error(`${method} did not return a value`) | ||
} | ||
if (response.return.decodeError) { | ||
throw response.return.decodeError | ||
} | ||
return response.return | ||
} | ||
|
||
export const getAvmResult = async <TResult extends ABIValue>( | ||
{ appClient, sendParams }: { appClient: ApplicationClient; sendParams?: SendTransactionParams }, | ||
method: string, | ||
...methodArgs: ABIAppCallArg[] | ||
): Promise<TResult> => { | ||
const result = await inovkeMethod(appClient, method, sendParams, ...methodArgs) | ||
return result.returnValue as TResult | ||
} | ||
|
||
export const getAvmResultRaw = async ( | ||
{ appClient, sendParams }: { appClient: ApplicationClient; sendParams?: SendTransactionParams }, | ||
method: string, | ||
...methodArgs: ABIAppCallArg[] | ||
): Promise<Uint8Array | undefined> => { | ||
const result = await inovkeMethod(appClient, method, sendParams, ...methodArgs) | ||
return result.rawReturnValue?.slice(ARC4_PREFIX_LENGTH) | ||
} | ||
|
8 changes: 5 additions & 3 deletions
8
...-testing/tests/primitives/biguint.spec.ts → tests/primitives/biguint.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 5 additions & 4 deletions
9
...ts-testing/tests/primitives/bytes.spec.ts → tests/primitives/bytes.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
...s-testing/tests/primitives/uint64.spec.ts → tests/primitives/uint64.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { internal } from '@algorandfoundation/algo-ts' | ||
import { createHash } from 'crypto' | ||
|
||
export const padUint8Array = (arr: Uint8Array, padSize: number): Uint8Array => { | ||
const paddedUint8Array = new Uint8Array(arr.length + padSize).fill(0) | ||
arr.forEach((v, i) => (paddedUint8Array[padSize + i] = v)) | ||
return paddedUint8Array | ||
} | ||
|
||
export const asUint8Array = (value: internal.primitives.StubBytesCompat): Uint8Array => | ||
internal.primitives.BytesCls.fromCompat(value).asUint8Array() | ||
|
||
export const getSha256Hash = (value: Uint8Array): Uint8Array => new Uint8Array(createHash('sha256').update(value).digest()) | ||
|
||
const NoValue = Symbol('no-value') | ||
type LazyInstance<T> = () => T | ||
export const Lazy = <T>(factory: () => T): LazyInstance<T> => { | ||
let val: T | typeof NoValue = NoValue | ||
|
||
return () => { | ||
if (val === NoValue) { | ||
val = factory() | ||
} | ||
return val | ||
} | ||
} |