Skip to content

Commit ded0b45

Browse files
authored
Merge pull request #49 from algorandfoundation/refactor/docs
refactor: simplify sample contract and tests by avoiding arc4 types
2 parents 67ce56c + 7eb3eee commit ded0b45

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

readme.md

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -87,27 +87,27 @@ export default class VotingContract extends arc4.Contract {
8787
voted = LocalState<uint64>({ key: Bytes('voted') })
8888

8989
@arc4.abimethod()
90-
public setTopic(topic: arc4.Str): void {
91-
this.topic.value = topic.bytes
90+
public setTopic(topic: string): void {
91+
this.topic.value = Bytes(topic)
9292
}
9393
@arc4.abimethod()
94-
public vote(pay: gtxn.PaymentTxn): arc4.Bool {
94+
public vote(pay: gtxn.PaymentTxn): boolean {
9595
assert(op.Global.groupSize === 2, 'Expected 2 transactions')
9696
assert(pay.amount === 10_000, 'Incorrect payment amount')
9797
assert(pay.sender === Txn.sender, 'Payment sender must match transaction sender')
9898

9999
if (this.voted(Txn.sender).hasValue) {
100-
return new arc4.Bool(false) // Already voted
100+
return false // Already voted
101101
}
102102

103103
this.votes.value = this.votes.value + 1
104104
this.voted(Txn.sender).value = 1
105-
return new arc4.Bool(true)
105+
return true
106106
}
107107

108108
@arc4.abimethod({ readonly: true })
109-
public getVotes(): arc4.UintN64 {
110-
return new arc4.UintN64(this.votes.value)
109+
public getVotes(): uint64 {
110+
return this.votes.value
111111
}
112112

113113
public clearStateProgram(): boolean {
@@ -141,7 +141,7 @@ describe('Voting contract', () => {
141141
})
142142

143143
const result = contract.vote(payment)
144-
expect(result.native).toEqual(true)
144+
expect(result).toEqual(true)
145145
expect(contract.votes.value).toEqual(1)
146146
expect(contract.voted(voter).value).toEqual(1)
147147
})
@@ -150,18 +150,18 @@ describe('Voting contract', () => {
150150
// Initialize the contract within the testing context
151151
const contract = ctx.contract.create(VotingContract)
152152

153-
const newTopic = ctx.any.arc4.str(10)
153+
const newTopic = ctx.any.string(10)
154154
contract.setTopic(newTopic)
155-
expect(contract.topic.value).toEqual(newTopic.bytes)
155+
expect(contract.topic.value).toEqual(newTopic)
156156
})
157157

158158
test('getVotes function', () => {
159159
// Initialize the contract within the testing context
160160
const contract = ctx.contract.create(VotingContract)
161161

162-
contract.votes.value = Uint64(5)
162+
contract.votes.value = 5
163163
const votes = contract.getVotes()
164-
expect(votes.native).toEqual(5)
164+
expect(votes).toEqual(5)
165165
})
166166
})
167167
```
@@ -172,13 +172,12 @@ This example demonstrates key aspects of testing with `algorand-typescript-testi
172172

173173
- Use of `arc4.Contract` as the base class for the contract.
174174
- ABI methods defined using the `@arc4.abimethod` decorator.
175-
- Use of ARC4-specific types like `arc4.Str`, `arc4.Bool`, and `arc4.UintN64`.
176175
- Readonly method annotation with `@arc4.abimethod({readonly: true})`.
177176

178177
2. Testing ARC4 Contracts:
179178

180179
- Creation of an `arc4.Contract` instance within the test context.
181-
- Use of `ctx.any.arc4` for generating ARC4-specific random test data.
180+
- Use of `ctx.any` for generating random test data.
182181
- Direct invocation of ABI methods on the contract instance.
183182

184183
3. Transaction Handling:
@@ -188,7 +187,7 @@ This example demonstrates key aspects of testing with `algorand-typescript-testi
188187

189188
4. State Verification:
190189
- Checking global and local state changes after method execution.
191-
- Verifying return values from ABI methods using ARC4-specific types.
190+
- Verifying return values from ABI methods.
192191

193192
> **NOTE**: Thorough testing is crucial in smart contract development due to their immutable nature post-deployment. Comprehensive unit and integration tests ensure contract validity and reliability. Optimizing for efficiency can significantly improve user experience by reducing transaction fees and simplifying interactions. Investing in robust testing and optimization practices is crucial and offers many benefits in the long run.
194193

0 commit comments

Comments
 (0)