Skip to content

Commit

Permalink
updating tests for whitelist token election
Browse files Browse the repository at this point in the history
  • Loading branch information
45930 committed Nov 30, 2023
1 parent d2ca6d4 commit 876a90b
Showing 1 changed file with 31 additions and 13 deletions.
44 changes: 31 additions & 13 deletions src/TokenElection/WhitelistTokenElection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ describe("WhitelistTokenElection", () => {
let senderKey: PrivateKey;
let initialBalance = 10_000_000_000;
let zkapp: WhitelistTokenElection;
let Local = Mina.LocalBlockchain({ proofsEnabled: true });

beforeAll(async () => {
console.time('compile');
Expand All @@ -17,7 +18,6 @@ describe("WhitelistTokenElection", () => {
});

beforeEach(async () => {
let Local = Mina.LocalBlockchain({ proofsEnabled: true });
zkappKey = PrivateKey.random();
zkappAddress = zkappKey.toPublicKey();
zkapp = new WhitelistTokenElection(zkappAddress);
Expand All @@ -29,7 +29,6 @@ describe("WhitelistTokenElection", () => {

describe("Votes in the WhitelistTokenElection", () => {
beforeEach(async () => {
let Local = Mina.LocalBlockchain({ proofsEnabled: true });
zkappKey = PrivateKey.random();
zkappAddress = zkappKey.toPublicKey();
zkapp = new WhitelistTokenElection(zkappAddress);
Expand Down Expand Up @@ -94,7 +93,6 @@ describe("WhitelistTokenElection", () => {
});

it("Updates the State", async () => {
let Local = Mina.LocalBlockchain({ proofsEnabled: true });
const wlPks = [
Local.testAccounts[1].privateKey,
Local.testAccounts[2].privateKey,
Expand All @@ -106,11 +104,11 @@ describe("WhitelistTokenElection", () => {
wlPks[1].toPublicKey(),
wlPks[2].toPublicKey()
]
const tx2 = await Mina.transaction(sender, () => {
const tx2 = await Mina.transaction(wl[0], () => {
zkapp.reduceVotes();
})
await tx2.prove();
await tx2.sign([senderKey, wlPks[0]]).send();
await tx2.sign([wlPks[0]]).send();
const zkappState = zkapp.ballot.get();
const pb1 = PartialBallot.unpack(zkappState.partial1);
const pb2 = PartialBallot.unpack(zkappState.partial2);
Expand All @@ -119,19 +117,39 @@ describe("WhitelistTokenElection", () => {
});

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

it("updates a second time", async () => {
let tx = await Mina.transaction(sender, () => {
const wlPks = [
Local.testAccounts[1].privateKey,
Local.testAccounts[2].privateKey,
Local.testAccounts[3].privateKey
]
const wl = [
wlPks[0].toPublicKey(),
wlPks[1].toPublicKey(),
wlPks[2].toPublicKey()
]
let tx = await Mina.transaction(wl[0], () => {
const partialBallot1 = PartialBallot.fromBigInts([101n, 0n, 0n, 0n, 0n, 0n, 0n]);
const partialBallot2 = PartialBallot.fromBigInts([0n, 0n, 0n, 0n, 20n, 0n, 40n]);
const myVote = new Ballot({
Expand All @@ -141,13 +159,13 @@ describe("WhitelistTokenElection", () => {
zkapp.castVote(myVote, UInt32.from(161));
});
await tx.prove();
await tx.sign([senderKey]).send();
await tx.sign([wlPks[0]]).send();

const tx2 = await Mina.transaction(sender, () => {
const tx2 = await Mina.transaction(wl[0], () => {
zkapp.reduceVotes();
})
await tx2.prove();
await tx2.sign([senderKey]).send();
await tx2.sign([wlPks[0]]).send();
const zkappState = zkapp.ballot.get();
const pb1 = PartialBallot.unpack(zkappState.partial1);
const pb2 = PartialBallot.unpack(zkappState.partial2);
Expand Down

0 comments on commit 876a90b

Please sign in to comment.