Skip to content

Commit 4cfc405

Browse files
authored
Merge pull request #92 from algorandfoundation/feat/native-funcs
feat: replace .native property with .as_uint64() and .as_biguint() methods in arc4.Uint
2 parents 7d8f820 + f4c737d commit 4cfc405

30 files changed

+310
-208
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,6 @@ describe('Voting contract', () => {
4040

4141
contract.votes.value = Uint64(5)
4242
const votes = contract.getVotes()
43-
expect(votes.native).toEqual(5)
43+
expect(votes.asUint64()).toEqual(5)
4444
})
4545
})

examples/marketplace/contract.algo.spec.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ describe('DigitalMarketplace', () => {
4040
nonce: testNonce,
4141
})
4242
const listingValue = interpretAsArc4<ListingValue>(Bytes(ctx.ledger.getBox(contract, Bytes('listings').concat(listingKey.bytes))))
43-
expect(listingValue.deposited.native).toEqual(10)
43+
expect(listingValue.deposited.asUint64()).toEqual(10)
4444
})
4545

4646
test('deposit', () => {
@@ -101,7 +101,7 @@ describe('DigitalMarketplace', () => {
101101

102102
// Assert
103103
const updatedListing = interpretAsArc4<ListingValue>(Bytes(ctx.ledger.getBox(contract, Bytes('listings').concat(listingKey.bytes))))
104-
expect(updatedListing.unitaryPrice.native).toEqual(testUnitaryPrice.native)
104+
expect(updatedListing.unitaryPrice.asUint64()).toEqual(testUnitaryPrice.asUint64())
105105
})
106106

107107
test('buy', () => {
@@ -130,14 +130,14 @@ describe('DigitalMarketplace', () => {
130130
testNonce,
131131
ctx.any.txn.payment({
132132
receiver: ctx.defaultSender,
133-
amount: contract.quantityPrice(testBuyQuantity.native, testUnitaryPrice.native, testAsset.decimals),
133+
amount: contract.quantityPrice(testBuyQuantity.asUint64(), testUnitaryPrice.asUint64(), testAsset.decimals),
134134
}),
135-
testBuyQuantity.native,
135+
testBuyQuantity.asUint64(),
136136
)
137137

138138
// Assert
139139
const updatedListing = interpretAsArc4<ListingValue>(Bytes(ctx.ledger.getBox(contract, Bytes('listings').concat(listingKey.bytes))))
140-
expect(updatedListing.deposited.native).toEqual(initialDeposit.native - testBuyQuantity.native)
140+
expect(updatedListing.deposited.asUint64()).toEqual(initialDeposit.asUint64() - testBuyQuantity.asUint64())
141141
expect(ctx.txn.lastGroup.getItxnGroup(0).getAssetTransferInnerTxn(0).assetReceiver).toEqual(ctx.defaultSender)
142142
})
143143

@@ -176,7 +176,7 @@ describe('DigitalMarketplace', () => {
176176
const assetTransferTxn = ctx.txn.lastGroup.getItxnGroup(1).getAssetTransferInnerTxn(0)
177177
expect(assetTransferTxn.xferAsset).toEqual(testAsset)
178178
expect(assetTransferTxn.assetReceiver).toEqual(testOwner.native)
179-
expect(assetTransferTxn.assetAmount).toEqual(initialDeposit.native)
179+
expect(assetTransferTxn.assetAmount).toEqual(initialDeposit.asUint64())
180180
})
181181

182182
test('bid', () => {
@@ -200,9 +200,9 @@ describe('DigitalMarketplace', () => {
200200
})
201201

202202
const bidder = ctx.any.account()
203-
const bidQuantity = ctx.any.arc4.uint64(0, BigInt(initialDeposit.native))
204-
const bidPrice = ctx.any.arc4.uint64(initialPrice.native + 1, 10000000n)
205-
const bidAmount = contract.quantityPrice(bidQuantity.native, bidPrice.native, testAsset.decimals)
203+
const bidQuantity = ctx.any.arc4.uint64(0, BigInt(initialDeposit.asUint64()))
204+
const bidPrice = ctx.any.arc4.uint64(initialPrice.asUint64() + 1, 10000000n)
205+
const bidAmount = contract.quantityPrice(bidQuantity.asUint64(), bidPrice.asUint64(), testAsset.decimals)
206206

207207
// Act
208208
ctx.txn.createScope([ctx.any.txn.applicationCall({ appId: app, sender: bidder })]).execute(() => {
@@ -223,8 +223,8 @@ describe('DigitalMarketplace', () => {
223223
// Assert
224224
const updatedListing = contract.listings(listingKey).value
225225
expect(updatedListing.bidder.native).toEqual(bidder)
226-
expect(updatedListing.bid.native).toEqual(bidQuantity.native)
227-
expect(updatedListing.bidUnitaryPrice.native).toEqual(bidPrice.native)
226+
expect(updatedListing.bid.asUint64()).toEqual(bidQuantity.asUint64())
227+
expect(updatedListing.bidUnitaryPrice.asUint64()).toEqual(bidPrice.asUint64())
228228
})
229229

230230
test('acceptBid', () => {
@@ -234,7 +234,7 @@ describe('DigitalMarketplace', () => {
234234
// Arrange
235235
const owner = ctx.defaultSender
236236
const initialDeposit = ctx.any.arc4.uint64(1, 10000000n)
237-
const bidQuantity = ctx.any.arc4.uint64(0, BigInt(initialDeposit.native))
237+
const bidQuantity = ctx.any.arc4.uint64(0, BigInt(initialDeposit.asUint64()))
238238
const bidPrice = ctx.any.arc4.uint64(0, 10000000n)
239239
const bidder = ctx.any.account()
240240

@@ -251,15 +251,15 @@ describe('DigitalMarketplace', () => {
251251
bidUnitaryPrice: bidPrice,
252252
})
253253

254-
const minQuantity = initialDeposit.native < bidQuantity.native ? initialDeposit.native : bidQuantity.native
255-
const expectedPayment = contract.quantityPrice(minQuantity, bidPrice.native, testAsset.decimals)
254+
const minQuantity = initialDeposit.asUint64() < bidQuantity.asUint64() ? initialDeposit.asUint64() : bidQuantity.asUint64()
255+
const expectedPayment = contract.quantityPrice(minQuantity, bidPrice.asUint64(), testAsset.decimals)
256256

257257
// Act
258258
contract.acceptBid(testAsset, testNonce)
259259

260260
// Assert
261261
const updatedListing = contract.listings(listingKey).value
262-
expect(updatedListing.deposited.native).toEqual(initialDeposit.native - minQuantity)
262+
expect(updatedListing.deposited.asUint64()).toEqual(initialDeposit.asUint64() - minQuantity)
263263

264264
expect(ctx.txn.lastGroup.itxnGroups.length).toEqual(2)
265265

examples/marketplace/contract.algo.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ export default class DigitalMarketplace extends arc4.Contract {
117117
bidUnitaryPrice: existing.bidUnitaryPrice,
118118
bidder: existing.bidder,
119119
unitaryPrice: existing.unitaryPrice,
120-
deposited: new arc4.Uint64(existing.deposited.native + xfer.assetAmount),
120+
deposited: new arc4.Uint64(existing.deposited.asUint64() + xfer.assetAmount),
121121
})
122122
}
123123

@@ -149,7 +149,7 @@ export default class DigitalMarketplace extends arc4.Contract {
149149

150150
const listing = clone(this.listings(key).value)
151151

152-
const amountToBePaid = this.quantityPrice(quantity, listing.unitaryPrice.native, asset.decimals)
152+
const amountToBePaid = this.quantityPrice(quantity, listing.unitaryPrice.asUint64(), asset.decimals)
153153

154154
assert(buyPay.sender === Txn.sender)
155155
assert(buyPay.receiver.bytes === owner.bytes)
@@ -160,7 +160,7 @@ export default class DigitalMarketplace extends arc4.Contract {
160160
bidUnitaryPrice: listing.bidUnitaryPrice,
161161
bidder: listing.bidder,
162162
unitaryPrice: listing.unitaryPrice,
163-
deposited: new arc4.Uint64(listing.deposited.native - quantity),
163+
deposited: new arc4.Uint64(listing.deposited.asUint64() - quantity),
164164
})
165165

166166
itxn
@@ -182,7 +182,7 @@ export default class DigitalMarketplace extends arc4.Contract {
182182

183183
const listing = clone(this.listings(key).value)
184184
if (listing.bidder !== new arc4.Address()) {
185-
const currentBidDeposit = this.quantityPrice(listing.bid.native, listing.bidUnitaryPrice.native, asset.decimals)
185+
const currentBidDeposit = this.quantityPrice(listing.bid.asUint64(), listing.bidUnitaryPrice.asUint64(), asset.decimals)
186186
itxn.payment({ receiver: listing.bidder.native, amount: currentBidDeposit }).submit()
187187
}
188188

@@ -194,7 +194,7 @@ export default class DigitalMarketplace extends arc4.Contract {
194194
.assetTransfer({
195195
xferAsset: asset,
196196
assetReceiver: Txn.sender,
197-
assetAmount: listing.deposited.native,
197+
assetAmount: listing.deposited.asUint64(),
198198
})
199199
.submit()
200200
}
@@ -205,14 +205,14 @@ export default class DigitalMarketplace extends arc4.Contract {
205205

206206
const listing = clone(this.listings(key).value)
207207
if (listing.bidder !== new arc4.Address()) {
208-
assert(unitaryPrice.native > listing.bidUnitaryPrice.native)
208+
assert(unitaryPrice.asUint64() > listing.bidUnitaryPrice.asUint64())
209209

210-
const currentBidAmount = this.quantityPrice(listing.bid.native, listing.bidUnitaryPrice.native, asset.decimals)
210+
const currentBidAmount = this.quantityPrice(listing.bid.asUint64(), listing.bidUnitaryPrice.asUint64(), asset.decimals)
211211

212212
itxn.payment({ receiver: listing.bidder.native, amount: currentBidAmount }).submit()
213213
}
214214

215-
const amountToBeBid = this.quantityPrice(quantity.native, unitaryPrice.native, asset.decimals)
215+
const amountToBeBid = this.quantityPrice(quantity.asUint64(), unitaryPrice.asUint64(), asset.decimals)
216216

217217
assert(bidPay.sender === Txn.sender)
218218
assert(bidPay.receiver === Global.currentApplicationAddress)
@@ -234,9 +234,9 @@ export default class DigitalMarketplace extends arc4.Contract {
234234
const listing = clone(this.listings(key).value)
235235
assert(listing.bidder !== new arc4.Address())
236236

237-
const minQuantity = listing.deposited.native < listing.bid.native ? listing.deposited.native : listing.bid.native
237+
const minQuantity = listing.deposited.asUint64() < listing.bid.asUint64() ? listing.deposited.asUint64() : listing.bid.asUint64()
238238

239-
const bestBidAmount = this.quantityPrice(minQuantity, listing.bidUnitaryPrice.native, asset.decimals)
239+
const bestBidAmount = this.quantityPrice(minQuantity, listing.bidUnitaryPrice.asUint64(), asset.decimals)
240240

241241
itxn.payment({ receiver: Txn.sender, amount: bestBidAmount }).submit()
242242

@@ -252,8 +252,8 @@ export default class DigitalMarketplace extends arc4.Contract {
252252
bidder: listing.bidder,
253253
bidUnitaryPrice: listing.bidUnitaryPrice,
254254
unitaryPrice: listing.unitaryPrice,
255-
deposited: new arc4.Uint64(listing.deposited.native - minQuantity),
256-
bid: new arc4.Uint64(listing.bid.native - minQuantity),
255+
deposited: new arc4.Uint64(listing.deposited.asUint64() - minQuantity),
256+
bid: new arc4.Uint64(listing.bid.asUint64() - minQuantity),
257257
})
258258
}
259259
}

examples/voting/contract.algo.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,9 @@ export class VotingRoundApp extends arc4.Contract {
131131
if (questionIndex > 0) {
132132
note += ','
133133
}
134-
if (questionOptions.native > 0) {
134+
if (questionOptions.asUint64() > 0) {
135135
note += '['
136-
for (let optionIndex = Uint64(0); optionIndex <= questionOptions.native; optionIndex++) {
136+
for (let optionIndex = Uint64(0); optionIndex <= questionOptions.asUint64(); optionIndex++) {
137137
if (optionIndex > 0) {
138138
note += ','
139139
}
@@ -204,8 +204,8 @@ export class VotingRoundApp extends arc4.Contract {
204204
)
205205
let cumulativeOffset = Uint64(0)
206206
for (const questionIndex of urange(questionsCount)) {
207-
const answerOptionIndex = answerIds.at(questionIndex).native
208-
const optionsCount = this.optionCounts.value.at(questionIndex).native
207+
const answerOptionIndex = answerIds.at(questionIndex).asUint64()
208+
const optionsCount = this.optionCounts.value.at(questionIndex).asUint64()
209209
assert(answerOptionIndex < optionsCount, 'Answer option index invalid')
210210
this.incrementVoteInBox(cumulativeOffset + answerOptionIndex)
211211
cumulativeOffset += optionsCount
@@ -228,7 +228,7 @@ export class VotingRoundApp extends arc4.Contract {
228228

229229
let totalOptions = Uint64(0)
230230
for (const item of optionCounts) {
231-
totalOptions += item.native
231+
totalOptions += item.asUint64()
232232
}
233233
this.optionCounts.value = clone(optionCounts)
234234
this.totalOptions.value = totalOptions

package-lock.json

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

0 commit comments

Comments
 (0)