Skip to content

Commit 9490b44

Browse files
authored
Merge pull request #26 from algorandfoundation/refactor-util
refactor: reduce test run time, remove extra util methods
2 parents dafba04 + db3c627 commit 9490b44

23 files changed

+103
-113
lines changed

examples/htlc-logicsig/signature.algo.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import { Account, Bytes, Global, LogicSig, op, TransactionType, Txn, Uint64, uint64 } from '@algorandfoundation/algorand-typescript'
2-
import algosdk from 'algosdk'
32

43
export default class HashedTimeLockedLogicSig extends LogicSig {
54
program(): boolean | uint64 {
65
// Participants
7-
const sellerAddress = Bytes(algosdk.decodeAddress('6ZHGHH5Z5CTPCF5WCESXMGRSVK7QJETR63M3NY5FJCUYDHO57VTCMJOBGY').publicKey)
8-
const buyerAddress = Bytes(algosdk.decodeAddress('7Z5PWO2C6LFNQFGHWKSK5H47IQP5OJW2M3HA2QPXTY3WTNP5NU2MHBW27M').publicKey)
6+
const sellerAddress = Bytes.fromBase32('6ZHGHH5Z5CTPCF5WCESXMGRSVK7QJETR63M3NY5FJCUYDHO57VTC')
7+
const buyerAddress = Bytes.fromBase32('7Z5PWO2C6LFNQFGHWKSK5H47IQP5OJW2M3HA2QPXTY3WTNP5NU2M')
98
const seller = Account(sellerAddress)
109
const buyer = Account(buyerAddress)
1110

examples/htlc-logicsig/signature.spec.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import { Account, Bytes } from '@algorandfoundation/algorand-typescript'
22
import { TestExecutionContext } from '@algorandfoundation/algorand-typescript-testing'
3-
import algosdk from 'algosdk'
43
import { afterEach, describe, expect, it } from 'vitest'
54
import HashedTimeLockedLogicSig from './signature.algo'
65

7-
const ZERO_ADDRESS_B32 = 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAY5HFKQ'
8-
const ZERO_ADDRESS = Bytes.fromBase32(ZERO_ADDRESS_B32)
6+
const ZERO_ADDRESS = Bytes.fromBase32('AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA')
97

108
describe('HTLC LogicSig', () => {
119
const ctx = new TestExecutionContext()
@@ -15,7 +13,7 @@ describe('HTLC LogicSig', () => {
1513
})
1614

1715
it('seller receives payment if correct secret is provided', () => {
18-
const receiverAddress = Bytes(algosdk.decodeAddress('6ZHGHH5Z5CTPCF5WCESXMGRSVK7QJETR63M3NY5FJCUYDHO57VTCMJOBGY').publicKey)
16+
const receiverAddress = Bytes.fromBase32('6ZHGHH5Z5CTPCF5WCESXMGRSVK7QJETR63M3NY5FJCUYDHO57VTC')
1917
ctx.txn
2018
.createScope([
2119
ctx.any.txn.payment({

examples/rollup.config.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,17 @@ import { puyaTsTransformer } from '../src/test-transformer'
66

77
const config: RollupOptions = {
88
input: [
9+
'examples/auction/contract.algo.ts',
910
'examples/calculator/contract.algo.ts',
1011
'examples/hello-world-abi/contract.algo.ts',
1112
'examples/hello-world/contract.algo.ts',
12-
'examples/auction/contract.algo.ts',
13-
'examples/voting/contract.algo.ts',
13+
'examples/htlc-logicsig/signature.algo.ts',
14+
'examples/precompiled/contract.algo.ts',
15+
'examples/scratch-storage/contract.algo.ts',
1416
'examples/simple-voting/contract.algo.ts',
17+
'examples/tealscript/example.algo.ts',
18+
'examples/voting/contract.algo.ts',
1519
'examples/zk-whitelist/contract.algo.ts',
16-
'examples/precompiled/contract.algo.ts',
1720
],
1821
output: [
1922
{

src/constants.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ export const DEFAULT_GLOBAL_GENESIS_HASH = Bytes(
2828
)
2929

3030
// algorand encoded address of 32 zero bytes
31-
export const ZERO_ADDRESS_B32 = 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAY5HFKQ'
32-
export const ZERO_ADDRESS = Bytes.fromBase32(ZERO_ADDRESS_B32)
31+
export const ZERO_ADDRESS = Bytes.fromBase32('AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA')
3332

3433
/**
3534
"\x09" # pragma version 9

src/impl/account.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,6 @@ export class AccountCls extends BytesBackedCls implements Account {
9797
if (assetOrApp instanceof ApplicationCls) {
9898
return this.data.optedApplications.has(asUint64Cls(assetOrApp.id).asBigInt())
9999
}
100-
throw new internal.errors.InternalError('Invalid argument type. Must be an `algopy.Asset` or `algopy.Application` instance.')
100+
throw new internal.errors.InternalError('Invalid argument type. Must be an `Asset` or `Application` instance.')
101101
}
102102
}

src/impl/global.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ const getGlobalData = (): GlobalData => {
4242
}
4343

4444
const getMissingValueErrorMessage = (name: keyof GlobalData) =>
45-
`'algopy.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."`
45+
`'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."`
4646

4747
export const Global: internal.opTypes.GlobalType = {
4848
/**

src/util.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,18 @@ export const asBigInt = (v: internal.primitives.StubUint64Compat): bigint => asU
3737

3838
export const asNumber = (v: internal.primitives.StubUint64Compat): number => asUint64Cls(v).asNumber()
3939

40-
export function extractGenericTypeArgs(t: string): string[] {
41-
const match = t.match(/<(.*)>/)
42-
if (!match) return []
43-
return match[1].split(',').map((x) => x.trim())
44-
}
45-
4640
export const asUint64Cls = (val: internal.primitives.StubUint64Compat) => internal.primitives.Uint64Cls.fromCompat(val)
4741

48-
export const asBigUintCls = (val: internal.primitives.StubBigUintCompat) => internal.primitives.BigUintCls.fromCompat(val)
42+
export const asBigUintCls = (val: internal.primitives.StubBigUintCompat | Uint8Array) =>
43+
internal.primitives.BigUintCls.fromCompat(
44+
val instanceof Uint8Array ? asBytes(val) : Array.isArray(val) ? asBytes(new Uint8Array(val)) : val,
45+
)
4946

5047
export const asBytesCls = (val: internal.primitives.StubBytesCompat | Uint8Array) => internal.primitives.BytesCls.fromCompat(val)
5148

5249
export const asUint64 = (val: internal.primitives.StubUint64Compat) => asUint64Cls(val).asAlgoTs()
5350

54-
export const asBigUint = (val: internal.primitives.StubBigUintCompat) => asBigUintCls(val).asAlgoTs()
51+
export const asBigUint = (val: internal.primitives.StubBigUintCompat | Uint8Array) => asBigUintCls(val).asAlgoTs()
5552

5653
export const asBytes = (val: internal.primitives.StubBytesCompat | Uint8Array) => asBytesCls(val).asAlgoTs()
5754

tests/arc4/address.spec.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ import { TestExecutionContext } from '@algorandfoundation/algorand-typescript-te
44
import { Address, interpretAsArc4 } from '@algorandfoundation/algorand-typescript/arc4'
55
import { afterEach, describe, expect, test } from 'vitest'
66
import { ABI_RETURN_VALUE_LOG_PREFIX } from '../../src/constants'
7-
import { encodeAddress } from '../../src/util'
8-
import { asUint8Array } from '../util'
7+
import { asUint8Array, encodeAddress } from '../../src/util'
98

109
const abiTypeString = 'address'
1110
const testData = [

tests/arc4/bool.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import { TestExecutionContext } from '@algorandfoundation/algorand-typescript-te
44
import { Bool, interpretAsArc4 } from '@algorandfoundation/algorand-typescript/arc4'
55
import { afterEach, describe, expect, test } from 'vitest'
66
import { ABI_RETURN_VALUE_LOG_PREFIX } from '../../src/constants'
7+
import { asUint8Array } from '../../src/util'
78
import appSpecJson from '../artifacts/arc4-primitive-ops/data/Arc4PrimitiveOpsContract.arc32.json'
89
import { getAlgorandAppClient, getAvmResult } from '../avm-invoker'
9-
import { asUint8Array } from '../util'
1010

1111
describe('arc4.Bool', async () => {
1212
const appClient = await getAlgorandAppClient(appSpecJson as AppSpec)

tests/arc4/byte.spec.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@ import { Byte, interpretAsArc4 } from '@algorandfoundation/algorand-typescript/a
55
import { encodingUtil } from '@algorandfoundation/puya-ts'
66
import { afterEach, describe, expect, it, test } from 'vitest'
77
import { ABI_RETURN_VALUE_LOG_PREFIX, MAX_UINT64 } from '../../src/constants'
8-
import { asBigUintCls } from '../../src/util'
8+
import { asBigUintCls, asUint8Array } from '../../src/util'
99
import appSpecJson from '../artifacts/arc4-primitive-ops/data/Arc4PrimitiveOpsContract.arc32.json'
1010
import { getAlgorandAppClient, getAvmResult } from '../avm-invoker'
11-
import { asUint8Array } from '../util'
1211

1312
const invalidBytesLengthError = 'byte string must be 1 byte long'
1413
describe('arc4.Byte', async () => {

0 commit comments

Comments
 (0)