Skip to content

Commit a8cf7cf

Browse files
authored
Merge pull request #93 from algorandfoundation/feat/replace-box-ref
feat: make BoxRef methods directly accessible on Box class
2 parents 4cfc405 + dcb3dd0 commit a8cf7cf

26 files changed

+389
-381
lines changed

docs/coverage.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ See which `algorand-typescript` stubs are implemented by the `algorand-typescrip
1515
| BigUint | Native |
1616
| Box | Emulated |
1717
| BoxMap | Emulated |
18-
| BoxRef | Emulated |
1918
| Bytes | Native |
2019
| CompiledContract | Mockable |
2120
| CompiledLogicSig | Mockable |

examples/proof-of-attendance/contract.algo.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import {
44
assert,
55
Box,
66
BoxMap,
7-
BoxRef,
87
Bytes,
98
ensureBudget,
109
Global,
@@ -63,11 +62,11 @@ export default class ProofOfAttendance extends arc4.Contract {
6362
const mintedAsset = this.mintPoa(Txn.sender)
6463
this.totalAttendees.value += 1
6564

66-
const boxRef = BoxRef({ key: Txn.sender.bytes })
65+
const boxRef = Box<bytes>({ key: Txn.sender.bytes })
6766
const hasClaimed = boxRef.exists
6867
assert(!hasClaimed, 'Already claimed POA')
6968

70-
boxRef.put(op.itob(mintedAsset.id))
69+
boxRef.value = op.itob(mintedAsset.id)
7170
}
7271

7372
@arc4.abimethod()
@@ -99,7 +98,7 @@ export default class ProofOfAttendance extends arc4.Contract {
9998

10099
@readonly
101100
getPoaIdWithBoxRef(): uint64 {
102-
const boxRef = BoxRef({ key: Txn.sender.bytes })
101+
const boxRef = Box<bytes>({ key: Txn.sender.bytes })
103102
const [poaId, exists] = boxRef.maybe()
104103
assert(exists, 'POA not found')
105104
return op.btoi(poaId)
@@ -153,7 +152,7 @@ export default class ProofOfAttendance extends arc4.Contract {
153152

154153
@arc4.abimethod()
155154
claimPoaWithBoxRef(optInTxn: gtxn.AssetTransferTxn) {
156-
const boxRef = BoxRef({ key: Txn.sender.bytes })
155+
const boxRef = Box<bytes>({ key: Txn.sender.bytes })
157156
const [poaId, exists] = boxRef.maybe()
158157
assert(exists, 'POA not found, attendance validation failed!')
159158
assert(optInTxn.xferAsset.id === op.btoi(poaId), 'POA ID mismatch')

examples/simple-voting/contract.algo.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ describe('Simple voting contract', () => {
3131
.execute(contract.approvalProgram)
3232

3333
expect(result).toEqual(1)
34-
expect(contract.topic.value).toBe(topic)
34+
expect(contract.topic.value).toEqual(topic)
3535
})
3636
})
3737
})

examples/voting/contract.algo.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import {
44
arc4,
55
assert,
66
assertMatch,
7+
Box,
78
BoxMap,
8-
BoxRef,
99
Bytes,
1010
clone,
1111
ensureBudget,
@@ -46,7 +46,7 @@ export class VotingRoundApp extends arc4.Contract {
4646
isBootstrapped = GlobalState<boolean>({ initialValue: false })
4747
voterCount = GlobalState({ initialValue: Uint64(0) })
4848
closeTime = GlobalState<uint64>()
49-
tallyBox = BoxRef({ key: Bytes`V` })
49+
tallyBox = Box<bytes>({ key: Bytes`V` })
5050
votesByAccount = BoxMap<Account, VoteIndexArray>({ keyPrefix: Bytes() })
5151
voteId = GlobalState<string>()
5252
snapshotPublicKey = GlobalState<bytes<32>>()

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.81",
72-
"@algorandfoundation/puya-ts": "1.0.0-alpha.81",
71+
"@algorandfoundation/algorand-typescript": "1.0.0-alpha.82",
72+
"@algorandfoundation/puya-ts": "1.0.0-alpha.82",
7373
"elliptic": "^6.6.1",
7474
"js-sha256": "^0.11.0",
7575
"js-sha3": "^0.9.3",

src/impl/box.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { bytes, op, uint64 } from '@algorandfoundation/algorand-typescript'
22
import { MAX_BOX_SIZE } from '../constants'
33
import { lazyContext } from '../context-helpers/internal-context'
44
import { AvmError, InternalError } from '../errors'
5-
import { asBytes, asBytesCls, asNumber, asUint8Array, conactUint8Arrays } from '../util'
5+
import { asBytes, asBytesCls, asNumber, asUint8Array, concatUint8Arrays } from '../util'
66
import { toBytes } from './encoded-types'
77
import type { StubBytesCompat, StubUint64Compat } from './primitives'
88

@@ -79,7 +79,7 @@ export const Box: typeof op.Box = {
7979
if (start + newContent.length > boxContent.length) {
8080
throw new InternalError('Replacement content exceeds box size')
8181
}
82-
const updatedContent = conactUint8Arrays(boxContent.slice(0, start), newContent, boxContent.slice(start + newContent.length))
82+
const updatedContent = concatUint8Arrays(boxContent.slice(0, start), newContent, boxContent.slice(start + newContent.length))
8383
lazyContext.ledger.setBox(app, name, updatedContent)
8484
},
8585
resize(a: StubBytesCompat, b: StubUint64Compat): void {
@@ -93,7 +93,7 @@ export const Box: typeof op.Box = {
9393
const size = boxContent.length
9494
let updatedContent
9595
if (newSize > size) {
96-
updatedContent = conactUint8Arrays(boxContent, new Uint8Array(newSize - size))
96+
updatedContent = concatUint8Arrays(boxContent, new Uint8Array(newSize - size))
9797
} else {
9898
updatedContent = boxContent.slice(0, newSize)
9999
}
@@ -114,12 +114,12 @@ export const Box: typeof op.Box = {
114114
throw new InternalError('Start index exceeds box size')
115115
}
116116
const end = Math.min(start + length, size)
117-
let updatedContent = conactUint8Arrays(boxContent.slice(0, start), newContent, boxContent.slice(end))
117+
let updatedContent = concatUint8Arrays(boxContent.slice(0, start), newContent, boxContent.slice(end))
118118
// Adjust the size if necessary
119119
if (updatedContent.length > size) {
120120
updatedContent = updatedContent.slice(0, size)
121121
} else if (updatedContent.length < size) {
122-
updatedContent = conactUint8Arrays(updatedContent, new Uint8Array(size - asNumber(updatedContent.length)))
122+
updatedContent = concatUint8Arrays(updatedContent, new Uint8Array(size - asNumber(updatedContent.length)))
123123
}
124124
lazyContext.ledger.setBox(app, name, updatedContent)
125125
},

src/impl/crypto.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import nacl from 'tweetnacl'
88
import { LOGIC_DATA_PREFIX, PROGRAM_TAG } from '../constants'
99
import { lazyContext } from '../context-helpers/internal-context'
1010
import { InternalError, NotImplementedError } from '../errors'
11-
import { asBytes, asBytesCls, asUint8Array, conactUint8Arrays } from '../util'
11+
import { asBytes, asBytesCls, asUint8Array, concatUint8Arrays } from '../util'
1212
import type { StubBytesCompat, StubUint64Compat } from './primitives'
1313
import { Bytes, BytesCls, FixedBytes, Uint64Cls } from './primitives'
1414

@@ -57,7 +57,7 @@ export const ed25519verify = (a: StubBytesCompat, b: StubBytesCompat, c: StubByt
5757
const txn = lazyContext.activeGroup.activeTransaction as gtxn.ApplicationCallTxn
5858
const programBytes = asBytesCls(txn.onCompletion == OnCompleteAction.ClearState ? txn.clearStateProgram : txn.approvalProgram)
5959

60-
const logicSig = conactUint8Arrays(asUint8Array(PROGRAM_TAG), programBytes.asUint8Array())
60+
const logicSig = concatUint8Arrays(asUint8Array(PROGRAM_TAG), programBytes.asUint8Array())
6161
const logicSigAddress = js_sha512.sha512_256.array(logicSig)
6262

6363
const addressBytes = Bytes(logicSigAddress)

0 commit comments

Comments
 (0)