Skip to content

Commit b49ecd2

Browse files
committed
feat: implement clone function
1 parent 21d8601 commit b49ecd2

File tree

21 files changed

+190
-49
lines changed

21 files changed

+190
-49
lines changed

examples/marketplace/contract.algo.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Asset, gtxn, uint64 } from '@algorandfoundation/algorand-typescript'
2-
import { arc4, assert, BoxMap, Global, itxn, op, Txn } from '@algorandfoundation/algorand-typescript'
2+
import { arc4, assert, BoxMap, clone, Global, itxn, op, Txn } from '@algorandfoundation/algorand-typescript'
33

44
export class ListingKey extends arc4.Struct<{
55
owner: arc4.Address
@@ -111,7 +111,7 @@ export default class DigitalMarketplace extends arc4.Contract {
111111
assert(xfer.assetReceiver === Global.currentApplicationAddress)
112112
assert(xfer.assetAmount > 0)
113113

114-
const existing = this.listings(key).value.copy()
114+
const existing = clone(this.listings(key).value)
115115
this.listings(key).value = new ListingValue({
116116
bid: existing.bid,
117117
bidUnitaryPrice: existing.bidUnitaryPrice,
@@ -129,7 +129,7 @@ export default class DigitalMarketplace extends arc4.Contract {
129129
nonce: nonce,
130130
})
131131

132-
const existing = this.listings(key).value.copy()
132+
const existing = clone(this.listings(key).value)
133133
this.listings(key).value = new ListingValue({
134134
bid: existing.bid,
135135
bidUnitaryPrice: existing.bidUnitaryPrice,
@@ -147,7 +147,7 @@ export default class DigitalMarketplace extends arc4.Contract {
147147
nonce: nonce,
148148
})
149149

150-
const listing = this.listings(key).value.copy()
150+
const listing = clone(this.listings(key).value)
151151

152152
const amountToBePaid = this.quantityPrice(quantity, listing.unitaryPrice.native, asset.decimals)
153153

@@ -180,7 +180,7 @@ export default class DigitalMarketplace extends arc4.Contract {
180180
nonce: nonce,
181181
})
182182

183-
const listing = this.listings(key).value.copy()
183+
const listing = clone(this.listings(key).value)
184184
if (listing.bidder !== new arc4.Address()) {
185185
const currentBidDeposit = this.quantityPrice(listing.bid.native, listing.bidUnitaryPrice.native, asset.decimals)
186186
itxn.payment({ receiver: listing.bidder.native, amount: currentBidDeposit }).submit()
@@ -203,7 +203,7 @@ export default class DigitalMarketplace extends arc4.Contract {
203203
bid(owner: arc4.Address, asset: Asset, nonce: arc4.UintN64, bidPay: gtxn.PaymentTxn, quantity: arc4.UintN64, unitaryPrice: arc4.UintN64) {
204204
const key = new ListingKey({ owner, asset: new arc4.UintN64(asset.id), nonce })
205205

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

@@ -231,7 +231,7 @@ export default class DigitalMarketplace extends arc4.Contract {
231231
acceptBid(asset: Asset, nonce: arc4.UintN64) {
232232
const key = new ListingKey({ owner: new arc4.Address(Txn.sender), asset: new arc4.UintN64(asset.id), nonce })
233233

234-
const listing = this.listings(key).value.copy()
234+
const listing = clone(this.listings(key).value)
235235
assert(listing.bidder !== new arc4.Address())
236236

237237
const minQuantity = listing.deposited.native < listing.bid.native ? listing.deposited.native : listing.bid.native

examples/voting/contract.algo.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
BoxMap,
88
BoxRef,
99
Bytes,
10+
clone,
1011
ensureBudget,
1112
Global,
1213
GlobalState,
@@ -207,7 +208,7 @@ export class VotingRoundApp extends arc4.Contract {
207208
assert(answerOptionIndex < optionsCount, 'Answer option index invalid')
208209
this.incrementVoteInBox(cumulativeOffset + answerOptionIndex)
209210
cumulativeOffset += optionsCount
210-
this.votesByAccount(Txn.sender).value = answerIds.copy()
211+
this.votesByAccount(Txn.sender).value = clone(answerIds)
211212
this.voterCount.value += 1
212213
}
213214
}
@@ -228,7 +229,7 @@ export class VotingRoundApp extends arc4.Contract {
228229
for (const item of optionCounts) {
229230
totalOptions += item.native
230231
}
231-
this.optionCounts.value = optionCounts.copy()
232+
this.optionCounts.value = clone(optionCounts)
232233
this.totalOptions.value = totalOptions
233234
}
234235

examples/zk-whitelist/contract.algo.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
assert,
77
BigUint,
88
Bytes,
9+
clone,
910
ensureBudget,
1011
Global,
1112
GlobalState,
@@ -88,7 +89,7 @@ export default class ZkWhitelistContract extends arc4.Contract {
8889
.applicationCall({
8990
appId: appId,
9091
fee: 0,
91-
appArgs: [arc4.methodSelector('verify(byte[32][],byte[32][])bool'), proof.copy(), publicInputs.copy()],
92+
appArgs: [arc4.methodSelector('verify(byte[32][],byte[32][])bool'), clone(proof), clone(publicInputs)],
9293
onCompletion: OnCompleteAction.NoOp,
9394
})
9495
.submit().lastLog

src/impl/clone.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { getEncoder, toBytes } from './encoded-types'
2+
3+
export function cloneImpl<T>(typeInfoString: string, value: T): T {
4+
if (value && typeof value === 'object' && 'copy' in value && typeof value.copy === 'function') {
5+
return value.copy() as T
6+
}
7+
const bytes = toBytes(value)
8+
const typeInfo = JSON.parse(typeInfoString)
9+
const encoder = getEncoder(typeInfo)
10+
return encoder(bytes, typeInfo) as T
11+
}

src/impl/encoded/arc4-impl.ts

Whitespace-only changes.

src/impl/encoded/array-proxy.ts

Whitespace-only changes.

src/impl/encoded/containers.ts

Whitespace-only changes.

src/impl/encoded/encoders.ts

Whitespace-only changes.

src/impl/encoded/helpers.ts

Whitespace-only changes.

src/impl/encoded/primitives.ts

Whitespace-only changes.

0 commit comments

Comments
 (0)