Skip to content

Commit ee8c1df

Browse files
authored
Merge pull request #75 from algorandfoundation/feat/box.ref
feat: include a `ref` property on the `Box` type which can be used to directly access box bytes
2 parents 5751083 + aefabb4 commit ee8c1df

File tree

6 files changed

+133
-28
lines changed

6 files changed

+133
-28
lines changed

docs/code/index/classes/ApplicationSpy.md

Lines changed: 68 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
# Class: ApplicationSpy\<TContract\>
88

9-
Defined in: [src/application-spy.ts:32](https://github.com/algorandfoundation/algorand-typescript-testing/blob/main/src/application-spy.ts#L32)
9+
Defined in: [src/application-spy.ts:34](https://github.com/algorandfoundation/algorand-typescript-testing/blob/main/src/application-spy.ts#L34)
1010

1111
## Type Parameters
1212

@@ -20,7 +20,7 @@ Defined in: [src/application-spy.ts:32](https://github.com/algorandfoundation/al
2020

2121
> **new ApplicationSpy**\<`TContract`\>(`contract`?): `ApplicationSpy`\<`TContract`\>
2222
23-
Defined in: [src/application-spy.ts:44](https://github.com/algorandfoundation/algorand-typescript-testing/blob/main/src/application-spy.ts#L44)
23+
Defined in: [src/application-spy.ts:46](https://github.com/algorandfoundation/algorand-typescript-testing/blob/main/src/application-spy.ts#L46)
2424

2525
#### Parameters
2626

@@ -38,15 +38,15 @@ Defined in: [src/application-spy.ts:44](https://github.com/algorandfoundation/al
3838

3939
> `optional` **contract**: `TContract` \| `ConstructorFor`\<`TContract`\>
4040
41-
Defined in: [src/application-spy.ts:42](https://github.com/algorandfoundation/algorand-typescript-testing/blob/main/src/application-spy.ts#L42)
41+
Defined in: [src/application-spy.ts:44](https://github.com/algorandfoundation/algorand-typescript-testing/blob/main/src/application-spy.ts#L44)
4242

4343
***
4444

4545
### on
4646

4747
> `readonly` **on**: `_TypedApplicationSpyCallBacks`\<`TContract`\>
4848
49-
Defined in: [src/application-spy.ts:39](https://github.com/algorandfoundation/algorand-typescript-testing/blob/main/src/application-spy.ts#L39)
49+
Defined in: [src/application-spy.ts:41](https://github.com/algorandfoundation/algorand-typescript-testing/blob/main/src/application-spy.ts#L41)
5050

5151
The `on` property is a proxy that allows you to register callbacks for specific method signatures.
5252
It dynamically creates methods based on the contract's methods.
@@ -57,7 +57,7 @@ It dynamically creates methods based on the contract's methods.
5757

5858
> **notify**(`itxn`): `void`
5959
60-
Defined in: [src/application-spy.ts:50](https://github.com/algorandfoundation/algorand-typescript-testing/blob/main/src/application-spy.ts#L50)
60+
Defined in: [src/application-spy.ts:52](https://github.com/algorandfoundation/algorand-typescript-testing/blob/main/src/application-spy.ts#L52)
6161

6262
#### Parameters
6363

@@ -73,44 +73,98 @@ Defined in: [src/application-spy.ts:50](https://github.com/algorandfoundation/al
7373

7474
### onAbiCall()
7575

76+
#### Call Signature
77+
7678
> **onAbiCall**(`methodSignature`, `callback`): `void`
7779
78-
Defined in: [src/application-spy.ts:69](https://github.com/algorandfoundation/algorand-typescript-testing/blob/main/src/application-spy.ts#L69)
80+
Defined in: [src/application-spy.ts:80](https://github.com/algorandfoundation/algorand-typescript-testing/blob/main/src/application-spy.ts#L80)
7981

8082
Registers a callback for a specific method signature.
8183

82-
#### Parameters
84+
##### Parameters
8385

84-
##### methodSignature
86+
###### methodSignature
8587

8688
`bytes`
8789

88-
##### callback
90+
###### callback
8991

9092
`AppSpyCb`
9193

92-
#### Returns
94+
##### Returns
95+
96+
`void`
97+
98+
#### Call Signature
99+
100+
> **onAbiCall**(`methodSignature`, `ocas`, `callback`): `void`
101+
102+
Defined in: [src/application-spy.ts:81](https://github.com/algorandfoundation/algorand-typescript-testing/blob/main/src/application-spy.ts#L81)
103+
104+
Registers a callback for a specific method signature.
105+
106+
##### Parameters
107+
108+
###### methodSignature
109+
110+
`bytes`
111+
112+
###### ocas
113+
114+
`OnCompleteAction`[]
115+
116+
###### callback
117+
118+
`AppSpyCb`
119+
120+
##### Returns
93121

94122
`void`
95123

96124
***
97125

98126
### onBareCall()
99127

128+
#### Call Signature
129+
100130
> **onBareCall**(`callback`): `void`
101131
102-
Defined in: [src/application-spy.ts:60](https://github.com/algorandfoundation/algorand-typescript-testing/blob/main/src/application-spy.ts#L60)
132+
Defined in: [src/application-spy.ts:62](https://github.com/algorandfoundation/algorand-typescript-testing/blob/main/src/application-spy.ts#L62)
103133

104134
Registers a callback for a bare call (no arguments).
105135

106-
#### Parameters
136+
##### Parameters
107137

108-
##### callback
138+
###### callback
109139

110140
`AppSpyCb`
111141

112142
The callback to be executed when a bare call is detected.
113143

114-
#### Returns
144+
##### Returns
145+
146+
`void`
147+
148+
#### Call Signature
149+
150+
> **onBareCall**(`ocas`, `callback`): `void`
151+
152+
Defined in: [src/application-spy.ts:63](https://github.com/algorandfoundation/algorand-typescript-testing/blob/main/src/application-spy.ts#L63)
153+
154+
Registers a callback for a bare call (no arguments).
155+
156+
##### Parameters
157+
158+
###### ocas
159+
160+
`OnCompleteAction`[]
161+
162+
###### callback
163+
164+
`AppSpyCb`
165+
166+
The callback to be executed when a bare call is detected.
167+
168+
##### Returns
115169

116170
`void`

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
@@ -66,8 +66,8 @@
6666
"vitest": "3.1.3"
6767
},
6868
"dependencies": {
69-
"@algorandfoundation/algorand-typescript": "1.0.0-alpha.54",
70-
"@algorandfoundation/puya-ts": "1.0.0-alpha.54",
69+
"@algorandfoundation/algorand-typescript": "1.0.0-alpha.55",
70+
"@algorandfoundation/puya-ts": "1.0.0-alpha.55",
7171
"elliptic": "^6.6.1",
7272
"js-sha256": "^0.11.0",
7373
"js-sha3": "^0.9.3",

src/impl/state.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,10 @@ export class BoxCls<TValue> {
236236
return lazyContext.ledger.getBox(this.#app, this.key).length
237237
}
238238

239+
get ref(): BoxRefCls {
240+
return new BoxRefCls(this.key)
241+
}
242+
239243
get(options: { default: TValue }): TValue {
240244
const [value, exists] = this.maybe()
241245
return exists ? value : options.default

tests/references/box-map.spec.ts

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import type { biguint, bytes, uint64 } from '@algorandfoundation/algorand-typescript'
22
import { BigUint, BoxMap, Bytes, op, Uint64 } from '@algorandfoundation/algorand-typescript'
33
import { TestExecutionContext } from '@algorandfoundation/algorand-typescript-testing'
4-
import type { Bool, DynamicBytes, Tuple } from '@algorandfoundation/algorand-typescript/arc4'
5-
import { ARC4Encoded, DynamicArray, interpretAsArc4, Str, Struct, UintN64 } from '@algorandfoundation/algorand-typescript/arc4'
4+
import type { Bool, DynamicBytes, StaticArray, Tuple, UintN16 } from '@algorandfoundation/algorand-typescript/arc4'
5+
import { ARC4Encoded, DynamicArray, interpretAsArc4, Str, Struct, UintN64, UintN8 } from '@algorandfoundation/algorand-typescript/arc4'
66
import { afterEach, describe, expect, it, test } from 'vitest'
77
import { MAX_UINT64 } from '../../src/constants'
88
import { toBytes } from '../../src/encoders'
@@ -259,4 +259,36 @@ describe('BoxMap', () => {
259259
expect(boxMap(key).value.at(-1).native).toEqual(400)
260260
})
261261
})
262+
263+
test('should be able to replace specific bytes values using ref', () => {
264+
ctx.txn.createScope([ctx.any.txn.applicationCall()]).execute(() => {
265+
const boxMap = BoxMap<uint64, StaticArray<UintN16, 4>>({ keyPrefix: 'a' })
266+
267+
const box1 = boxMap(1)
268+
box1.create()
269+
270+
const boxRefA = box1.ref
271+
boxRefA.replace(1, new UintN8(123).bytes)
272+
expect(box1.value[0].native).toEqual(123)
273+
expect(boxMap(1).value[0].native).toEqual(123)
274+
275+
const boxRefB = box1.ref
276+
boxRefB.replace(2, new UintN8(255).bytes)
277+
expect(box1.value[1].native).toEqual(65280)
278+
expect(boxMap(1).value[1].native).toEqual(65280)
279+
280+
const box2 = boxMap(2)
281+
box2.create()
282+
283+
const boxRefC = box2.ref
284+
boxRefC.replace(1, new UintN8(223).bytes)
285+
expect(box2.value[0].native).toEqual(223)
286+
expect(boxMap(2).value[0].native).toEqual(223)
287+
288+
const boxRefD = box2.ref
289+
boxRefD.replace(3, new UintN8(255).bytes)
290+
expect(box2.value[1].native).toEqual(255)
291+
expect(boxMap(2).value[1].native).toEqual(255)
292+
})
293+
})
262294
})

tests/references/box.spec.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { biguint, bytes, uint64 } from '@algorandfoundation/algorand-typescript'
22
import { BigUint, Box, Bytes, op, Uint64 } from '@algorandfoundation/algorand-typescript'
33
import { TestExecutionContext } from '@algorandfoundation/algorand-typescript-testing'
4-
import type { DynamicBytes } from '@algorandfoundation/algorand-typescript/arc4'
4+
import type { DynamicBytes, UintN16 } from '@algorandfoundation/algorand-typescript/arc4'
55
import {
66
ARC4Encoded,
77
Bool,
@@ -269,6 +269,21 @@ describe('Box', () => {
269269
})
270270
})
271271

272+
test('should be able to replace specific bytes values using ref', () => {
273+
ctx.txn.createScope([ctx.any.txn.applicationCall()]).execute(() => {
274+
const box = Box<StaticArray<UintN16, 4>>({ key: 'a' })
275+
box.create()
276+
277+
const boxRef1 = box.ref
278+
boxRef1.replace(1, new UintN8(123).bytes)
279+
expect(box.value[0].native).toEqual(123)
280+
281+
const boxRef2 = box.ref
282+
boxRef2.replace(2, new UintN8(255).bytes)
283+
expect(box.value[1].native).toEqual(65280)
284+
})
285+
})
286+
272287
describe('Box.create', () => {
273288
it('throw errors if size is not provided for dynamic value type', () => {
274289
ctx.txn.createScope([ctx.any.txn.applicationCall()]).execute(() => {

0 commit comments

Comments
 (0)