ARC4 Simple Voting Contract
Description
Section titled “Description”Example source from examples/arc4-simple-voting/contract.algo.ts.
Prerequisites
Section titled “Prerequisites”LocalNet running (algokit localnet start)
Run This Example
Section titled “Run This Example”From the repository’s examples directory:
cd examples
npx tsx arc4-simple-voting/contract.algo.ts
import type { gtxn, uint64 } from '@algorandfoundation/algorand-typescript'
import { arc4, assert, Bytes, GlobalState, LocalState, op, readonly, Txn, Uint64 } from '@algorandfoundation/algorand-typescript'
export default class VotingContract extends arc4.Contract {
topic = GlobalState({ initialValue: Bytes('default_topic'), key: Bytes('topic') })
votes = GlobalState({ initialValue: Uint64(0), key: Bytes('votes') })
voted = LocalState<uint64>({ key: Bytes('voted') })
@arc4.abimethod()
public setTopic(topic: arc4.Str): void {
this.topic.value = topic.bytes
}
@arc4.abimethod()
public vote(pay: gtxn.PaymentTxn): arc4.Bool {
assert(op.Global.groupSize === 2, 'Expected 2 transactions')
assert(pay.amount === 10_000, 'Incorrect payment amount')
assert(pay.sender === Txn.sender, 'Payment sender must match transaction sender')
if (this.voted(Txn.sender).hasValue) {
return new arc4.Bool(false) // Already voted
}
this.votes.value = this.votes.value + 1
this.voted(Txn.sender).value = 1
return new arc4.Bool(true)
}
@readonly
public getVotes(): arc4.Uint64 {
return new arc4.Uint64(this.votes.value)
}
public clearStateProgram(): boolean {
return true
}
}
Other examples
Section titled “Other examples”- ARC4 Simple Voting Contract
- Auction
- Calculator Contract
- Hello World Contract
- Hello World ABI Contract
- Htlc Logicsig Signature
- Local Storage Contract
- Marketplace Contract
- Precompiled Precompiled Apps
- Precompiled Precompiled Factory
- Precompiled Precompiled Typed
- Proof Of Attendance Contract
- Scratch Storage Contract
- Simple Voting
- Tealscript Example
- Tealscript Teal Script Base
- Voting Contract
- ZK Whitelist