Skip to content

Commit e80d847

Browse files
committed
refactor: rename interpretAsArc4 function as convertBytes with strategy option
1 parent a8cf7cf commit e80d847

32 files changed

+240
-214
lines changed

docs/coverage.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ See which `algorand-typescript` stubs are implemented by the `algorand-typescrip
4242
| encodeArc4 | Native |
4343
| ensureBudget | Emulated |
4444
| err | Native |
45-
| interpretAsArc4 | Native |
45+
| convertBytes | Native |
4646
| itxnCompose | Emulated |
4747
| log | Emulated |
4848
| logicSig | Emulated |

examples/marketplace/contract.algo.spec.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { arc4, Bytes } from '@algorandfoundation/algorand-typescript'
22
import { TestExecutionContext } from '@algorandfoundation/algorand-typescript-testing'
3-
import { interpretAsArc4 } from '@algorandfoundation/algorand-typescript/arc4'
3+
import { convertBytes } from '@algorandfoundation/algorand-typescript/arc4'
44
import { afterEach, describe, expect, test } from 'vitest'
55
import DigitalMarketplace, { ListingKey, ListingValue } from './contract.algo'
66

@@ -39,7 +39,9 @@ describe('DigitalMarketplace', () => {
3939
asset: new arc4.Uint64(testAsset.id),
4040
nonce: testNonce,
4141
})
42-
const listingValue = interpretAsArc4<ListingValue>(Bytes(ctx.ledger.getBox(contract, Bytes('listings').concat(listingKey.bytes))))
42+
const listingValue = convertBytes<ListingValue>(Bytes(ctx.ledger.getBox(contract, Bytes('listings').concat(listingKey.bytes))), {
43+
strategy: 'unsafe-cast',
44+
})
4345
expect(listingValue.deposited.asUint64()).toEqual(10)
4446
})
4547

@@ -100,7 +102,9 @@ describe('DigitalMarketplace', () => {
100102
contract.setPrice(testAsset, testNonce, testUnitaryPrice)
101103

102104
// Assert
103-
const updatedListing = interpretAsArc4<ListingValue>(Bytes(ctx.ledger.getBox(contract, Bytes('listings').concat(listingKey.bytes))))
105+
const updatedListing = convertBytes<ListingValue>(Bytes(ctx.ledger.getBox(contract, Bytes('listings').concat(listingKey.bytes))), {
106+
strategy: 'unsafe-cast',
107+
})
104108
expect(updatedListing.unitaryPrice.asUint64()).toEqual(testUnitaryPrice.asUint64())
105109
})
106110

@@ -136,7 +140,9 @@ describe('DigitalMarketplace', () => {
136140
)
137141

138142
// Assert
139-
const updatedListing = interpretAsArc4<ListingValue>(Bytes(ctx.ledger.getBox(contract, Bytes('listings').concat(listingKey.bytes))))
143+
const updatedListing = convertBytes<ListingValue>(Bytes(ctx.ledger.getBox(contract, Bytes('listings').concat(listingKey.bytes))), {
144+
strategy: 'unsafe-cast',
145+
})
140146
expect(updatedListing.deposited.asUint64()).toEqual(initialDeposit.asUint64() - testBuyQuantity.asUint64())
141147
expect(ctx.txn.lastGroup.getItxnGroup(0).getAssetTransferInnerTxn(0).assetReceiver).toEqual(ctx.defaultSender)
142148
})

examples/zk-whitelist/contract.algo.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ export default class ZkWhitelistContract extends arc4.Contract {
5656
// The verifier expects public inputs to be in the curve field, but an
5757
// Algorand address might represent a number larger than the field
5858
// modulus, so to be safe we take the address modulo the field modulus
59-
const addressMod = arc4.interpretAsArc4<arc4.Address>(op.bzero(32).bitwiseOr(Bytes(BigUint(address.bytes) % curveMod)))
59+
const addressMod = arc4.convertBytes<arc4.Address>(op.bzero(32).bitwiseOr(Bytes(BigUint(address.bytes) % curveMod)), {
60+
strategy: 'unsafe-cast',
61+
})
6062
// Verify the proof by calling the deposit verifier app
6163
const verified = this.verifyProof(TemplateVar<uint64>('VERIFIER_APP_ID'), proof, new arc4.DynamicArray(addressMod))
6264
if (!verified.native) {
@@ -93,6 +95,6 @@ export default class ZkWhitelistContract extends arc4.Contract {
9395
onCompletion: OnCompleteAction.NoOp,
9496
})
9597
.submit().lastLog
96-
return arc4.interpretAsArc4<arc4.Bool>(verified, 'log')
98+
return arc4.convertBytes<arc4.Bool>(verified, { prefix: 'log', strategy: 'unsafe-cast' })
9799
}
98100
}

package-lock.json

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

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@
6868
"vitest": "3.2.4"
6969
},
7070
"dependencies": {
71-
"@algorandfoundation/algorand-typescript": "1.0.0-alpha.82",
72-
"@algorandfoundation/puya-ts": "1.0.0-alpha.82",
71+
"@algorandfoundation/algorand-typescript": "1.0.0-alpha.83",
72+
"@algorandfoundation/puya-ts": "1.0.0-alpha.83",
7373
"elliptic": "^6.6.1",
7474
"js-sha256": "^0.11.0",
7575
"js-sha3": "^0.9.3",

src/impl/encoded-types/encoded-types.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1217,13 +1217,13 @@ export function decodeArc4<T>(
12171217
}
12181218

12191219
/** @internal */
1220-
export function interpretAsArc4<T extends _ARC4Encoded>(
1220+
export function convertBytes<T extends _ARC4Encoded>(
12211221
typeInfoString: string,
12221222
bytes: StubBytesCompat,
1223-
prefix: 'none' | 'log' = 'none',
1223+
options: { prefix?: 'none' | 'log'; strategy: 'unsafe-cast' },
12241224
): T {
12251225
const typeInfo = JSON.parse(typeInfoString)
1226-
return getEncoder<T>(typeInfo)(bytes, typeInfo, prefix)
1226+
return getEncoder<T>(typeInfo)(bytes, typeInfo, options.prefix)
12271227
}
12281228

12291229
/** @internal */

src/impl/encoded-types/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ export {
33
Address,
44
Bool,
55
Byte,
6+
convertBytes,
67
decodeArc4,
78
DynamicArray,
89
DynamicBytes,
910
encodeArc4,
1011
FixedArray,
1112
getArc4Encoded,
1213
getEncoder,
13-
interpretAsArc4,
1414
ReferenceArray,
1515
StaticArray,
1616
StaticBytes,

src/internal/arc4.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ export {
99
Address,
1010
Bool,
1111
Byte,
12+
convertBytes,
1213
decodeArc4,
1314
DynamicArray,
1415
DynamicBytes,
1516
encodeArc4,
1617
FixedArray,
17-
interpretAsArc4,
1818
ReferenceArray,
1919
sizeOf,
2020
StaticArray,

src/test-transformer/visitors.ts

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -506,17 +506,7 @@ const tryGetStubbedFunctionName = (node: ts.CallExpression, helper: VisitorHelpe
506506
: (node.expression as ts.Identifier)
507507
const functionName = tryGetAlgoTsSymbolName(identityExpression, helper)
508508
if (functionName === undefined) return undefined
509-
const stubbedFunctionNames = [
510-
'interpretAsArc4',
511-
'decodeArc4',
512-
'encodeArc4',
513-
'emit',
514-
'methodSelector',
515-
'sizeOf',
516-
'abiCall',
517-
'clone',
518-
'Bytes',
519-
]
509+
const stubbedFunctionNames = ['convertBytes', 'decodeArc4', 'encodeArc4', 'emit', 'methodSelector', 'sizeOf', 'abiCall', 'clone', 'Bytes']
520510

521511
if (stubbedFunctionNames.includes(functionName)) {
522512
if (ts.isPropertyAccessExpression(node.expression)) {

tests/arc4/address.algo.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { getABIEncodedValue } from '@algorandfoundation/algokit-utils/types/app-arc56'
22
import { Account, Bytes } from '@algorandfoundation/algorand-typescript'
33
import { TestExecutionContext } from '@algorandfoundation/algorand-typescript-testing'
4-
import { Address, interpretAsArc4 } from '@algorandfoundation/algorand-typescript/arc4'
4+
import { Address, convertBytes } from '@algorandfoundation/algorand-typescript/arc4'
55
import { afterEach, describe, expect, test } from 'vitest'
66
import { ABI_RETURN_VALUE_LOG_PREFIX } from '../../src/constants'
77
import { encodeAddress } from '../../src/impl/reference'
@@ -71,14 +71,14 @@ describe('arc4.Address', () => {
7171

7272
test.each(testData)('fromBytes method', (value) => {
7373
const sdkResult = getABIEncodedValue(asUint8Array(value), abiTypeString, {})
74-
const result = interpretAsArc4<Address>(value)
74+
const result = convertBytes<Address>(value, { strategy: 'unsafe-cast' })
7575
expect(result.bytes).toEqual(sdkResult)
7676
})
7777

7878
test.each(testData)('fromLog method', (value) => {
7979
const sdkResult = getABIEncodedValue(asUint8Array(value), abiTypeString, {})
8080
const paddedValue = Bytes([...asUint8Array(ABI_RETURN_VALUE_LOG_PREFIX), ...asUint8Array(value)])
81-
const result = interpretAsArc4<Address>(paddedValue, 'log')
81+
const result = convertBytes<Address>(paddedValue, { prefix: 'log', strategy: 'unsafe-cast' })
8282
expect(result.bytes).toEqual(sdkResult)
8383
})
8484
})

0 commit comments

Comments
 (0)