Skip to content

Commit ab50295

Browse files
authored
Merge pull request #64 from algorandfoundation/fix/box-struct-arc4
fix: change stub implementations of box, struct to match latest algo-ts
2 parents a981614 + 7f1f5fb commit ab50295

31 files changed

+1409
-1475
lines changed

examples/marketplace/contract.algo.ts

Lines changed: 50 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -84,22 +84,19 @@ export default class DigitalMarketplace extends arc4.Contract {
8484
asset: new arc4.UintN64(xfer.xferAsset.id),
8585
nonce: nonce,
8686
})
87-
assert(!this.listings.has(key))
87+
assert(!this.listings(key).exists)
8888

8989
assert(xfer.sender === Txn.sender)
9090
assert(xfer.assetReceiver === Global.currentApplicationAddress)
9191
assert(xfer.assetAmount > 0)
9292

93-
this.listings.set(
94-
key,
95-
new ListingValue({
96-
deposited: new arc4.UintN64(xfer.assetAmount),
97-
unitaryPrice: unitaryPrice,
98-
bidder: new arc4.Address(),
99-
bid: new arc4.UintN64(),
100-
bidUnitaryPrice: new arc4.UintN64(),
101-
}),
102-
)
93+
this.listings(key).value = new ListingValue({
94+
deposited: new arc4.UintN64(xfer.assetAmount),
95+
unitaryPrice: unitaryPrice,
96+
bidder: new arc4.Address(),
97+
bid: new arc4.UintN64(),
98+
bidUnitaryPrice: new arc4.UintN64(),
99+
})
103100
}
104101

105102
@arc4.abimethod()
@@ -114,17 +111,14 @@ export default class DigitalMarketplace extends arc4.Contract {
114111
assert(xfer.assetReceiver === Global.currentApplicationAddress)
115112
assert(xfer.assetAmount > 0)
116113

117-
const existing = this.listings.get(key)
118-
this.listings.set(
119-
key,
120-
new ListingValue({
121-
bid: existing.bid,
122-
bidUnitaryPrice: existing.bidUnitaryPrice,
123-
bidder: existing.bidder,
124-
unitaryPrice: existing.unitaryPrice,
125-
deposited: new arc4.UintN64(existing.deposited.native + xfer.assetAmount),
126-
}),
127-
)
114+
const existing = this.listings(key).value
115+
this.listings(key).value = new ListingValue({
116+
bid: existing.bid,
117+
bidUnitaryPrice: existing.bidUnitaryPrice,
118+
bidder: existing.bidder,
119+
unitaryPrice: existing.unitaryPrice,
120+
deposited: new arc4.UintN64(existing.deposited.native + xfer.assetAmount),
121+
})
128122
}
129123

130124
@arc4.abimethod()
@@ -135,17 +129,14 @@ export default class DigitalMarketplace extends arc4.Contract {
135129
nonce: nonce,
136130
})
137131

138-
const existing = this.listings.get(key)
139-
this.listings.set(
140-
key,
141-
new ListingValue({
142-
bid: existing.bid,
143-
bidUnitaryPrice: existing.bidUnitaryPrice,
144-
bidder: existing.bidder,
145-
deposited: existing.deposited,
146-
unitaryPrice: unitaryPrice,
147-
}),
148-
)
132+
const existing = this.listings(key).value
133+
this.listings(key).value = new ListingValue({
134+
bid: existing.bid,
135+
bidUnitaryPrice: existing.bidUnitaryPrice,
136+
bidder: existing.bidder,
137+
deposited: existing.deposited,
138+
unitaryPrice: unitaryPrice,
139+
})
149140
}
150141

151142
@arc4.abimethod()
@@ -156,24 +147,21 @@ export default class DigitalMarketplace extends arc4.Contract {
156147
nonce: nonce,
157148
})
158149

159-
const listing = this.listings.get(key)
150+
const listing = this.listings(key).value
160151

161152
const amountToBePaid = this.quantityPrice(quantity, listing.unitaryPrice.native, asset.decimals)
162153

163154
assert(buyPay.sender === Txn.sender)
164155
assert(buyPay.receiver.bytes === owner.bytes)
165156
assert(buyPay.amount === amountToBePaid)
166157

167-
this.listings.set(
168-
key,
169-
new ListingValue({
170-
bid: listing.bid,
171-
bidUnitaryPrice: listing.bidUnitaryPrice,
172-
bidder: listing.bidder,
173-
unitaryPrice: listing.unitaryPrice,
174-
deposited: new arc4.UintN64(listing.deposited.native - quantity),
175-
}),
176-
)
158+
this.listings(key).value = new ListingValue({
159+
bid: listing.bid,
160+
bidUnitaryPrice: listing.bidUnitaryPrice,
161+
bidder: listing.bidder,
162+
unitaryPrice: listing.unitaryPrice,
163+
deposited: new arc4.UintN64(listing.deposited.native - quantity),
164+
})
177165

178166
itxn
179167
.assetTransfer({
@@ -192,13 +180,13 @@ export default class DigitalMarketplace extends arc4.Contract {
192180
nonce: nonce,
193181
})
194182

195-
const listing = this.listings.get(key)
183+
const listing = this.listings(key).value
196184
if (listing.bidder !== new arc4.Address()) {
197185
const currentBidDeposit = this.quantityPrice(listing.bid.native, listing.bidUnitaryPrice.native, asset.decimals)
198186
itxn.payment({ receiver: listing.bidder.native, amount: currentBidDeposit }).submit()
199187
}
200188

201-
this.listings.delete(key)
189+
this.listings(key).delete()
202190

203191
itxn.payment({ receiver: Txn.sender, amount: this.listingsBoxMbr() }).submit()
204192

@@ -215,7 +203,7 @@ export default class DigitalMarketplace extends arc4.Contract {
215203
bid(owner: arc4.Address, asset: Asset, nonce: arc4.UintN64, bidPay: gtxn.PaymentTxn, quantity: arc4.UintN64, unitaryPrice: arc4.UintN64) {
216204
const key = new ListingKey({ owner, asset: new arc4.UintN64(asset.id), nonce })
217205

218-
const listing = this.listings.get(key)
206+
const listing = this.listings(key).value
219207
if (listing.bidder !== new arc4.Address()) {
220208
assert(unitaryPrice.native > listing.bidUnitaryPrice.native)
221209

@@ -230,23 +218,20 @@ export default class DigitalMarketplace extends arc4.Contract {
230218
assert(bidPay.receiver === Global.currentApplicationAddress)
231219
assert(bidPay.amount === amountToBeBid)
232220

233-
this.listings.set(
234-
key,
235-
new ListingValue({
236-
deposited: listing.deposited,
237-
unitaryPrice: listing.unitaryPrice,
238-
bidder: new arc4.Address(Txn.sender),
239-
bid: quantity,
240-
bidUnitaryPrice: unitaryPrice,
241-
}),
242-
)
221+
this.listings(key).value = new ListingValue({
222+
deposited: listing.deposited,
223+
unitaryPrice: listing.unitaryPrice,
224+
bidder: new arc4.Address(Txn.sender),
225+
bid: quantity,
226+
bidUnitaryPrice: unitaryPrice,
227+
})
243228
}
244229

245230
@arc4.abimethod()
246231
acceptBid(asset: Asset, nonce: arc4.UintN64) {
247232
const key = new ListingKey({ owner: new arc4.Address(Txn.sender), asset: new arc4.UintN64(asset.id), nonce })
248233

249-
const listing = this.listings.get(key)
234+
const listing = this.listings(key).value
250235
assert(listing.bidder !== new arc4.Address())
251236

252237
const minQuantity = listing.deposited.native < listing.bid.native ? listing.deposited.native : listing.bid.native
@@ -263,15 +248,12 @@ export default class DigitalMarketplace extends arc4.Contract {
263248
})
264249
.submit()
265250

266-
this.listings.set(
267-
key,
268-
new ListingValue({
269-
bidder: listing.bidder,
270-
bidUnitaryPrice: listing.bidUnitaryPrice,
271-
unitaryPrice: listing.unitaryPrice,
272-
deposited: new arc4.UintN64(listing.deposited.native - minQuantity),
273-
bid: new arc4.UintN64(listing.bid.native - minQuantity),
274-
}),
275-
)
251+
this.listings(key).value = new ListingValue({
252+
bidder: listing.bidder,
253+
bidUnitaryPrice: listing.bidUnitaryPrice,
254+
unitaryPrice: listing.unitaryPrice,
255+
deposited: new arc4.UintN64(listing.deposited.native - minQuantity),
256+
bid: new arc4.UintN64(listing.bid.native - minQuantity),
257+
})
276258
}
277259
}

examples/marketplace/contract.spec.ts

Lines changed: 44 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,13 @@ describe('DigitalMarketplace', () => {
5555
asset: new arc4.UintN64(testAsset.id),
5656
nonce: testNonce,
5757
})
58-
contract.listings.set(
59-
listingKey,
60-
new ListingValue({
61-
deposited: new arc4.UintN64(10),
62-
unitaryPrice: new arc4.UintN64(10),
63-
bidder: new arc4.Address(ctx.defaultSender),
64-
bid: new arc4.UintN64(10),
65-
bidUnitaryPrice: new arc4.UintN64(10),
66-
}),
67-
)
58+
contract.listings(listingKey).value = new ListingValue({
59+
deposited: new arc4.UintN64(10),
60+
unitaryPrice: new arc4.UintN64(10),
61+
bidder: new arc4.Address(ctx.defaultSender),
62+
bid: new arc4.UintN64(10),
63+
bidUnitaryPrice: new arc4.UintN64(10),
64+
})
6865

6966
// Act
7067
contract.deposit(
@@ -91,16 +88,13 @@ describe('DigitalMarketplace', () => {
9188
asset: new arc4.UintN64(testAsset.id),
9289
nonce: testNonce,
9390
})
94-
contract.listings.set(
95-
listingKey,
96-
new ListingValue({
97-
deposited: new arc4.UintN64(10),
98-
unitaryPrice: new arc4.UintN64(10),
99-
bidder: new arc4.Address(ctx.defaultSender),
100-
bid: new arc4.UintN64(10),
101-
bidUnitaryPrice: new arc4.UintN64(10),
102-
}),
103-
)
91+
contract.listings(listingKey).value = new ListingValue({
92+
deposited: new arc4.UintN64(10),
93+
unitaryPrice: new arc4.UintN64(10),
94+
bidder: new arc4.Address(ctx.defaultSender),
95+
bid: new arc4.UintN64(10),
96+
bidUnitaryPrice: new arc4.UintN64(10),
97+
})
10498

10599
// Act
106100
contract.setPrice(testAsset, testNonce, testUnitaryPrice)
@@ -121,16 +115,13 @@ describe('DigitalMarketplace', () => {
121115
const testBuyQuantity = ctx.any.arc4.uintN64(0, 1000000n)
122116

123117
const listingKey = new ListingKey({ owner: testOwner, asset: new arc4.UintN64(testAsset.id), nonce: testNonce })
124-
contract.listings.set(
125-
listingKey,
126-
new ListingValue({
127-
deposited: initialDeposit,
128-
unitaryPrice: testUnitaryPrice,
129-
bidder: new arc4.Address(),
130-
bid: new arc4.UintN64(0),
131-
bidUnitaryPrice: new arc4.UintN64(0),
132-
}),
133-
)
118+
contract.listings(listingKey).value = new ListingValue({
119+
deposited: initialDeposit,
120+
unitaryPrice: testUnitaryPrice,
121+
bidder: new arc4.Address(),
122+
bid: new arc4.UintN64(0),
123+
bidUnitaryPrice: new arc4.UintN64(0),
124+
})
134125

135126
// Act
136127
contract.buy(
@@ -163,16 +154,13 @@ describe('DigitalMarketplace', () => {
163154
const listingsBoxMbr = contract.listingsBoxMbr()
164155

165156
const listingKey = new ListingKey({ owner: testOwner, asset: new arc4.UintN64(testAsset.id), nonce: testNonce })
166-
contract.listings.set(
167-
listingKey,
168-
new ListingValue({
169-
deposited: initialDeposit,
170-
unitaryPrice: testUnitaryPrice,
171-
bidder: new arc4.Address(),
172-
bid: new arc4.UintN64(0),
173-
bidUnitaryPrice: new arc4.UintN64(0),
174-
}),
175-
)
157+
contract.listings(listingKey).value = new ListingValue({
158+
deposited: initialDeposit,
159+
unitaryPrice: testUnitaryPrice,
160+
bidder: new arc4.Address(),
161+
bid: new arc4.UintN64(0),
162+
bidUnitaryPrice: new arc4.UintN64(0),
163+
})
176164

177165
// Act
178166
contract.withdraw(testAsset, testNonce)
@@ -203,16 +191,13 @@ describe('DigitalMarketplace', () => {
203191
const initialDeposit = ctx.any.arc4.uintN64(0, 1000000n)
204192

205193
const listingKey = new ListingKey({ owner, asset: new arc4.UintN64(testAsset.id), nonce: testNonce })
206-
contract.listings.set(
207-
listingKey,
208-
new ListingValue({
209-
deposited: initialDeposit,
210-
unitaryPrice: initialPrice,
211-
bidder: new arc4.Address(),
212-
bid: new arc4.UintN64(0),
213-
bidUnitaryPrice: new arc4.UintN64(0),
214-
}),
215-
)
194+
contract.listings(listingKey).value = new ListingValue({
195+
deposited: initialDeposit,
196+
unitaryPrice: initialPrice,
197+
bidder: new arc4.Address(),
198+
bid: new arc4.UintN64(0),
199+
bidUnitaryPrice: new arc4.UintN64(0),
200+
})
216201

217202
const bidder = ctx.any.account()
218203
const bidQuantity = ctx.any.arc4.uintN64(0, BigInt(initialDeposit.native))
@@ -236,7 +221,7 @@ describe('DigitalMarketplace', () => {
236221
})
237222

238223
// Assert
239-
const updatedListing = contract.listings.get(listingKey)
224+
const updatedListing = contract.listings(listingKey).value
240225
expect(updatedListing.bidder.native).toEqual(bidder)
241226
expect(updatedListing.bid.native).toEqual(bidQuantity.native)
242227
expect(updatedListing.bidUnitaryPrice.native).toEqual(bidPrice.native)
@@ -258,16 +243,13 @@ describe('DigitalMarketplace', () => {
258243
asset: new arc4.UintN64(testAsset.id),
259244
nonce: testNonce,
260245
})
261-
contract.listings.set(
262-
listingKey,
263-
new ListingValue({
264-
deposited: initialDeposit,
265-
unitaryPrice: ctx.any.arc4.uintN64(),
266-
bidder: new arc4.Address(bidder),
267-
bid: bidQuantity,
268-
bidUnitaryPrice: bidPrice,
269-
}),
270-
)
246+
contract.listings(listingKey).value = new ListingValue({
247+
deposited: initialDeposit,
248+
unitaryPrice: ctx.any.arc4.uintN64(),
249+
bidder: new arc4.Address(bidder),
250+
bid: bidQuantity,
251+
bidUnitaryPrice: bidPrice,
252+
})
271253

272254
const minQuantity = initialDeposit.native < bidQuantity.native ? initialDeposit.native : bidQuantity.native
273255
const expectedPayment = contract.quantityPrice(minQuantity, bidPrice.native, testAsset.decimals)
@@ -276,7 +258,7 @@ describe('DigitalMarketplace', () => {
276258
contract.acceptBid(testAsset, testNonce)
277259

278260
// Assert
279-
const updatedListing = contract.listings.get(listingKey)
261+
const updatedListing = contract.listings(listingKey).value
280262
expect(updatedListing.deposited.native).toEqual(initialDeposit.native - minQuantity)
281263

282264
expect(ctx.txn.lastGroup.itxnGroups.length).toEqual(2)

examples/precompiled/contract.algo.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { bytes, uint64 } from '@algorandfoundation/algorand-typescript'
2-
import { assert, compile, Contract, itxn } from '@algorandfoundation/algorand-typescript'
3-
import { decodeArc4, encodeArc4, methodSelector, OnCompleteAction } from '@algorandfoundation/algorand-typescript/arc4'
2+
import { assert, compile, Contract, itxn, OnCompleteAction } from '@algorandfoundation/algorand-typescript'
3+
import { decodeArc4, encodeArc4, methodSelector } from '@algorandfoundation/algorand-typescript/arc4'
44
import { Hello, HelloTemplate, HelloTemplateCustomPrefix, LargeProgram, TerribleCustodialAccount } from './precompiled-apps.algo'
55

66
export class HelloFactory extends Contract {

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,10 @@ export default class ProofOfAttendance extends arc4.Contract {
7676
const mintedAsset = this.mintPoa(Txn.sender)
7777
this.totalAttendees.value += 1
7878

79-
const hasClaimed = this.boxMap.has(Txn.sender.bytes)
79+
const hasClaimed = this.boxMap(Txn.sender.bytes).exists
8080
assert(!hasClaimed, 'Already claimed POA')
8181

82-
this.boxMap.set(Txn.sender.bytes, mintedAsset.id)
82+
this.boxMap(Txn.sender.bytes).value = mintedAsset.id
8383
}
8484
@arc4.abimethod({ readonly: true })
8585
getPoaId(): uint64 {
@@ -106,7 +106,7 @@ export default class ProofOfAttendance extends arc4.Contract {
106106

107107
@arc4.abimethod({ readonly: true })
108108
getPoaIdWithBoxMap(): uint64 {
109-
const [poaId, exists] = this.boxMap.maybe(Txn.sender.bytes)
109+
const [poaId, exists] = this.boxMap(Txn.sender.bytes).maybe()
110110
assert(exists, 'POA not found')
111111
return poaId
112112
}
@@ -172,7 +172,7 @@ export default class ProofOfAttendance extends arc4.Contract {
172172

173173
@arc4.abimethod()
174174
claimPoaWithBoxMap(optInTxn: gtxn.AssetTransferTxn) {
175-
const [poaId, exists] = this.boxMap.maybe(Txn.sender.bytes)
175+
const [poaId, exists] = this.boxMap(Txn.sender.bytes).maybe()
176176
assert(exists, 'POA not found, attendance validation failed!')
177177
assert(optInTxn.xferAsset.id === poaId, 'POA ID mismatch')
178178
assert(optInTxn.fee === 0, 'We got you covered for free!')

0 commit comments

Comments
 (0)