Skip to content

Commit e295a2f

Browse files
authored
Merge pull request #12 from algorandfoundation/feat-arc4-dynamic-types
feat: arc4 types
2 parents 38d47f9 + 4de4c08 commit e295a2f

30 files changed

+10002
-105
lines changed

.nsprc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
{
2-
}
1+
{}

package-lock.json

Lines changed: 9 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export const MAX_UINT8 = 2 ** 8 - 1
66
export const MAX_UINT64 = 2n ** 64n - 1n
77
export const MAX_UINT512 = 2n ** 512n - 1n
88
export const MAX_BYTES_SIZE = 4096
9+
export const MAX_LOG_SIZE = 1024
910
export const MAX_ITEMS_IN_LOG = 32
1011
export const MAX_BOX_SIZE = 32768
1112
export const BITS_IN_BYTE = 8

src/encoders.ts

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,61 @@
1-
import { biguint, BigUint, bytes, Bytes, internal, TransactionType, uint64, Uint64 } from '@algorandfoundation/algorand-typescript'
1+
import { biguint, BigUint, bytes, internal, TransactionType, uint64, Uint64 } from '@algorandfoundation/algorand-typescript'
22
import { OnCompleteAction } from '@algorandfoundation/algorand-typescript/arc4'
33
import { AccountCls } from './impl/account'
44
import { ApplicationCls } from './impl/application'
55
import { AssetCls } from './impl/asset'
6+
import { arc4Encoders, getArc4Encoder } from './impl/encoded-types'
7+
import { DeliberateAny } from './typescript-helpers'
8+
import { asBytes, asUint8Array } from './util'
69

7-
export interface TypeInfo {
10+
export type TypeInfo = {
811
name: string
912
genericArgs?: TypeInfo[] | Record<string, TypeInfo>
1013
}
1114

12-
type fromBytes<T> = (val: Uint8Array, typeInfo: TypeInfo) => T
15+
export type fromBytes<T> = (val: Uint8Array | internal.primitives.StubBytesCompat, typeInfo: TypeInfo, prefix?: 'none' | 'log') => T
1316

1417
const booleanFromBytes: fromBytes<boolean> = (val) => {
15-
return internal.encodingUtil.uint8ArrayToBigInt(val) > 0n
18+
return internal.encodingUtil.uint8ArrayToBigInt(asUint8Array(val)) > 0n
1619
}
1720

1821
const bigUintFromBytes: fromBytes<biguint> = (val) => {
19-
return BigUint(internal.encodingUtil.uint8ArrayToBigInt(val))
22+
return BigUint(internal.encodingUtil.uint8ArrayToBigInt(asUint8Array(val)))
2023
}
2124

2225
const bytesFromBytes: fromBytes<bytes> = (val) => {
23-
return Bytes(val)
26+
return asBytes(val)
2427
}
2528

2629
const stringFromBytes: fromBytes<string> = (val) => {
27-
return Bytes(val).toString()
30+
return asBytes(val).toString()
2831
}
2932

3033
const uint64FromBytes: fromBytes<uint64> = (val) => {
31-
return Uint64(internal.encodingUtil.uint8ArrayToBigInt(val))
34+
return Uint64(internal.encodingUtil.uint8ArrayToBigInt(asUint8Array(val)))
3235
}
3336

3437
const onCompletionFromBytes: fromBytes<OnCompleteAction> = (val) => {
35-
return Uint64(internal.encodingUtil.uint8ArrayToBigInt(val)) as OnCompleteAction
38+
return Uint64(internal.encodingUtil.uint8ArrayToBigInt(asUint8Array(val))) as OnCompleteAction
3639
}
3740

3841
const transactionTypeFromBytes: fromBytes<TransactionType> = (val) => {
39-
return Uint64(internal.encodingUtil.uint8ArrayToBigInt(val)) as TransactionType
42+
return Uint64(internal.encodingUtil.uint8ArrayToBigInt(asUint8Array(val))) as TransactionType
4043
}
4144

42-
export const encoders = {
45+
export const encoders: Record<string, fromBytes<DeliberateAny>> = {
4346
account: AccountCls.fromBytes,
4447
application: ApplicationCls.fromBytes,
4548
asset: AssetCls.fromBytes,
46-
'bool(ean)?': booleanFromBytes,
49+
boolean: booleanFromBytes,
4750
biguint: bigUintFromBytes,
4851
bytes: bytesFromBytes,
4952
string: stringFromBytes,
5053
uint64: uint64FromBytes,
5154
OnCompleteAction: onCompletionFromBytes,
5255
TransactionType: transactionTypeFromBytes,
53-
// 'Tuple<*>': tupleFromBytes,
56+
...arc4Encoders,
5457
}
5558

5659
export const getEncoder = <T>(typeInfo: TypeInfo): fromBytes<T> => {
57-
const encoder = Object.entries(encoders).find(([k, _]) => new RegExp(`^${k}$`, 'i').test(typeInfo.name))?.[1]
58-
if (!encoder) {
59-
throw new Error(`No encoder found for type ${typeInfo.name}`)
60-
}
61-
return encoder as fromBytes<T>
60+
return getArc4Encoder<T>(typeInfo, encoders)
6261
}

src/impl/base.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Bytes, bytes, Uint64, uint64 } from '@algorandfoundation/algorand-typescript'
1+
import { bytes, internal, Uint64, uint64 } from '@algorandfoundation/algorand-typescript'
22
import { encodingUtil } from '@algorandfoundation/puya-ts'
33
import type { TypeInfo } from '../encoders'
44

@@ -14,8 +14,12 @@ export abstract class BytesBackedCls {
1414
// this.#typeInfo = typeInfo
1515
}
1616

17-
static fromBytes<T extends BytesBackedCls>(this: { new (v: bytes, typeInfo?: TypeInfo): T }, value: Uint8Array, typeInfo?: TypeInfo) {
18-
return new this(Bytes(value), typeInfo)
17+
static fromBytes<T extends BytesBackedCls>(
18+
this: { new (v: bytes, typeInfo?: TypeInfo): T },
19+
value: internal.primitives.StubBytesCompat | Uint8Array,
20+
typeInfo?: TypeInfo,
21+
) {
22+
return new this(internal.primitives.BytesCls.fromCompat(value).asAlgoTs(), typeInfo)
1923
}
2024
}
2125

@@ -30,8 +34,9 @@ export abstract class Uint64BackedCls {
3034
this.#value = value
3135
}
3236

33-
static fromBytes<T extends Uint64BackedCls>(this: { new (v: uint64): T }, value: Uint8Array) {
34-
const uint64Value = Uint64(encodingUtil.uint8ArrayToBigInt(value))
37+
static fromBytes<T extends Uint64BackedCls>(this: { new (v: uint64): T }, value: internal.primitives.StubBytesCompat | Uint8Array) {
38+
const uint8ArrayValue = value instanceof Uint8Array ? value : internal.primitives.BytesCls.fromCompat(value).asUint8Array()
39+
const uint64Value = Uint64(encodingUtil.uint8ArrayToBigInt(uint8ArrayValue))
3540
return new this(uint64Value)
3641
}
3742
}

0 commit comments

Comments
 (0)