Skip to content

Commit

Permalink
don't burn tokens until reduce
Browse files Browse the repository at this point in the history
  • Loading branch information
45930 committed Nov 19, 2023
1 parent 8edcb18 commit 68585f7
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
9 changes: 8 additions & 1 deletion src/TokenElection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,14 @@ describe("TokenElection", () => {
});

it("has the correct number of tokens remaining", async () => {
const remainingVoteBalance = await Mina.getBalance(sender, zkapp.token.id);
let remainingVoteBalance = await Mina.getBalance(sender, zkapp.token.id);
expect(remainingVoteBalance.toString()).toBe(String(50_000))
const tx2 = await Mina.transaction(sender, () => {
zkapp.reduceVotes();
})
await tx2.prove();
await tx2.sign([senderKey]).send();
remainingVoteBalance = await Mina.getBalance(sender, zkapp.token.id);
expect(remainingVoteBalance.toString()).toBe(String(50_000 - 110))
});

Expand Down
24 changes: 17 additions & 7 deletions src/TokenElection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,18 @@ export class Ballot extends Struct({
partial2: Field
}) { }

export class VoteAction extends Struct({
ballot: Ballot,
sender: PublicKey,
amount: UInt64
}) { }

export class TokenElection extends SmartContract {
@state(IpfsHash) electionDetailsIpfs = State<IpfsHash>();
@state(Ballot) ballot = State<Ballot>();
@state(Field) actionState = State<Field>();

reducer = Reducer({ actionType: Ballot });
reducer = Reducer({ actionType: VoteAction });

init() {
super.init();
Expand Down Expand Up @@ -54,11 +60,11 @@ export class TokenElection extends SmartContract {
voteSum = voteSum.add(unpackedVote2[i]);
}
voteSum.assertEquals(amount); // sum of votes must equal asserted amount (can vote for multiple options)
this.token.burn({
address: this.sender,
this.reducer.dispatch({
ballot: vote,
sender: this.sender,
amount: UInt64.from(amount)
});
this.reducer.dispatch(vote);
}

@method
Expand All @@ -74,17 +80,21 @@ export class TokenElection extends SmartContract {
this.reducer.reduce(
pendingActions,
Ballot,
(state: Ballot, _action: Ballot) => {
(state: Ballot, _action: VoteAction) => {
const unpackedState1 = PartialBallot.unpack(state.partial1);
const unpackedState2 = PartialBallot.unpack(state.partial2);
const unpackedAction1 = PartialBallot.unpack(_action.partial1);
const unpackedAction2 = PartialBallot.unpack(_action.partial2);
const unpackedAction1 = PartialBallot.unpack(_action.ballot.partial1);
const unpackedAction2 = PartialBallot.unpack(_action.ballot.partial2);
for (let i = 0; i < PartialBallot.l; i++) {
unpackedState1[i] = unpackedState1[i].add(unpackedAction1[i])
}
for (let i = 0; i < PartialBallot.l; i++) {
unpackedState2[i] = unpackedState2[i].add(unpackedAction2[i])
}
this.token.burn({
address: _action.sender,
amount: UInt64.from(_action.amount)
});
return {
partial1: PartialBallot.fromUInt32s(unpackedState1).packed,
partial2: PartialBallot.fromUInt32s(unpackedState2).packed
Expand Down
6 changes: 2 additions & 4 deletions src/examples/ZkIgnite/votes.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
{
"1": 10,
"4": 250,
"11": 2000,
"13": 2000
"0": 3000,
"7": 50
}

0 comments on commit 68585f7

Please sign in to comment.