Skip to content

Commit

Permalink
Trimmed with working Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cryptphitraining committed Jun 26, 2024
1 parent f9fcf54 commit 8a4352b
Show file tree
Hide file tree
Showing 4 changed files with 236 additions and 183 deletions.
212 changes: 207 additions & 5 deletions src/ZKLottoGame.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AccountUpdate, Field, Mina, PrivateKey, PublicKey } from 'o1js';
import { Lotto, ZKLottoGame } from './ZKLottoGame';
import { AccountUpdate, Field, Mina, PrivateKey, PublicKey, Signature } from 'o1js';
import { LottoNumbers, GameBoard, ZKLottoGame } from './ZKLottoGame';

/*
* This file specifies how to test the `Add` example smart contract. It is safe to delete this file and replace
Expand Down Expand Up @@ -48,7 +48,7 @@ describe('ZKLottoGame', () => {
it('generates and deploys the `ZKLottoGame` smart contract', async () => {
await localDeploy();
const num = zkApp.lottogameWeek.get();
expect(num).toEqual(Field(1));
expect(num).toEqual(Field(0));
});

it('correctly start Lotto Week on the `ZKLottoGame` smart contract', async () => {
Expand All @@ -62,6 +62,208 @@ describe('ZKLottoGame', () => {
await txn.sign([senderKey]).send();

const updatedNum = zkApp.lottogameWeek.get();
expect(updatedNum).toEqual(Field(2));
expect(updatedNum).toEqual(Field(1));
});
});

it('Current game must end before new game starts in `ZKLottoGame` smart contract', async () => {
await localDeploy();

// update transaction
const txn1 = await Mina.transaction(senderAccount, async () => {
await zkApp.startLottoWeek();
});
await txn1.prove();
await txn1.sign([senderKey]).send();

const gameWeek = zkApp.lottogameWeek.get();
expect(gameWeek).toEqual(Field(1));

//-----------------------------
// second transaction
const winningNumbers = [Field(2), Field(15), Field(19), Field(28), Field(35), Field(42)]
const lottoWinInfo = LottoNumbers.from(gameWeek, winningNumbers);

const txn2 = await Mina.transaction(senderAccount, async () => {
await zkApp.endLottoWeek(lottoWinInfo);
});
await txn2.prove();
await txn2.sign([senderKey]).send();

const winningHash = lottoWinInfo.hash();
// const updatedNum2 = zkApp.LottoWinHash.get();
expect(winningHash).toEqual(zkApp.LottoWinHash.get());
});


it('Players can submit lotto numbers to play starts in `ZKLottoGame` smart contract', async () => {
await localDeploy();

// update transaction
const txn1 = await Mina.transaction(senderAccount, async () => {
await zkApp.startLottoWeek();
});
await txn1.prove();
await txn1.sign([senderKey]).send();

const gameWeek = zkApp.lottogameWeek.get();
expect(gameWeek).toEqual(Field(1));

// End Game
const chosenNumbers = [Field(2), Field(15), Field(19), Field(28), Field(35), Field(42)]
const PlayerEntry = LottoNumbers.from(gameWeek, chosenNumbers);
const PlayerEntryHash = PlayerEntry.hash();

const signature = Signature.create(
senderKey,
[gameWeek, PlayerEntryHash]);


const txn2 = await Mina.transaction(senderAccount, async () => {
await zkApp.play(senderAccount, signature, gameWeek, PlayerEntryHash);
});
await txn2.prove();
await txn2.sign([senderKey]).send();

// End Game
// const winningNumbers = [Field(2), Field(15), Field(19), Field(28), Field(35), Field(42)]
const lottoEntryInfo = LottoNumbers.from(gameWeek, chosenNumbers);

const txn3 = await Mina.transaction(senderAccount, async () => {
await zkApp.endLottoWeek(lottoEntryInfo);
});
await txn3.prove();
await txn3.sign([senderKey]).send();

const winningHash = lottoEntryInfo.hash();
// const updatedNum2 = zkApp.LottoWinHash.get();
expect(winningHash).toEqual(zkApp.LottoWinHash.get());
});


it('Winning Player can claim Winnings in `ZKLottoGame` smart contract', async () => {
await localDeploy();

// update transaction
const txn1 = await Mina.transaction(senderAccount, async () => {
await zkApp.startLottoWeek();
});
await txn1.prove();
await txn1.sign([senderKey]).send();

const gameWeek = zkApp.lottogameWeek.get();
expect(gameWeek).toEqual(Field(1));
// Play Game
const chosenNumbers = [Field(2), Field(15), Field(19), Field(28), Field(35), Field(42)]
const PlayerEntry = LottoNumbers.from(gameWeek, chosenNumbers);
const PlayerEntryHash = PlayerEntry.hash();

const signature1 = Signature.create(
senderKey,
[gameWeek, PlayerEntryHash]);


const txn2 = await Mina.transaction(senderAccount, async () => {
await zkApp.play(senderAccount, signature1, gameWeek, PlayerEntryHash);
});
await txn2.prove();
await txn2.sign([senderKey]).send();


//End Game
const lottoEntryInfo = LottoNumbers.from(gameWeek, chosenNumbers);
const txn3 = await Mina.transaction(senderAccount, async () => {
await zkApp.endLottoWeek(lottoEntryInfo);
});
await txn3.prove();
await txn3.sign([senderKey]).send();

const winningHash = lottoEntryInfo.hash();
// const updatedNum2 = zkApp.LottoWinHash.get();
expect(winningHash).toEqual(zkApp.LottoWinHash.get());


// Claim Wins

const signature2 = Signature.create(
senderKey,
[gameWeek, PlayerEntryHash]);

const txn4 = await Mina.transaction(senderAccount, async () => {
await zkApp.ClaimWinning(senderAccount, signature2, gameWeek, PlayerEntry);
});
await txn4.prove();
await txn4.sign([senderKey]).send();

expect(winningHash).toEqual(zkApp.LottoWinHash.get());
});



it('Player cannot claim for wrong lotto numbers in `ZKLottoGame` smart contract', async () => {
await localDeploy();

// Start Game
const txn1 = await Mina.transaction(senderAccount, async () => {
await zkApp.startLottoWeek();
});
await txn1.prove();
await txn1.sign([senderKey]).send();

const gameWeek = zkApp.lottogameWeek.get();
expect(gameWeek).toEqual(Field(1));

// Play Game
const chosenNumbers = [Field(2), Field(15), Field(19), Field(28), Field(35), Field(42)]
const PlayerEntry = LottoNumbers.from(gameWeek, chosenNumbers);
const PlayerEntryHash = PlayerEntry.hash();

const signature1 = Signature.create(
senderKey,
[gameWeek, PlayerEntryHash]);


const txn2 = await Mina.transaction(senderAccount, async () => {
await zkApp.play(senderAccount, signature1, gameWeek, PlayerEntryHash);
});
await txn2.prove();
await txn2.sign([senderKey]).send();


//End Game
const lottoEntryInfo = LottoNumbers.from(gameWeek, chosenNumbers);

const txn3 = await Mina.transaction(senderAccount, async () => {
await zkApp.endLottoWeek(lottoEntryInfo);
});
await txn3.prove();
await txn3.sign([senderKey]).send();

const winningHash = lottoEntryInfo.hash();
// const updatedNum2 = zkApp.LottoWinHash.get();
expect(winningHash).toEqual(zkApp.LottoWinHash.get());


// Claim Wins

const wrongNumbers = [Field(2), Field(15), Field(20), Field(28), Field(35), Field(43)]
const wrongEntry = LottoNumbers.from(gameWeek, wrongNumbers);
const wrongEntryHash = wrongEntry.hash();

const signature2 = Signature.create(
senderKey,
[gameWeek, wrongEntryHash]);

try {
const txn4 = await Mina.transaction(senderAccount, async () => {
await zkApp.ClaimWinning(senderAccount, signature2, gameWeek, wrongEntry);
});
await txn4.prove();
await txn4.sign([senderKey]).send();
}catch(e){
console.log(e)
}

});


});
Loading

0 comments on commit 8a4352b

Please sign in to comment.