Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First Edit for colloboration #1

Merged
merged 1 commit into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 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.js';
import { Lotto, ZKLottoGame } from './ZKLottoGame';

Check failure on line 2 in src/ZKLottoGame.test.ts

View workflow job for this annotation

GitHub Actions / test (18, ubuntu-latest)

Module '"./ZKLottoGame"' has no exported member 'Lotto'.

Check failure on line 2 in src/ZKLottoGame.test.ts

View workflow job for this annotation

GitHub Actions / test (20, ubuntu-latest)

Module '"./ZKLottoGame"' has no exported member 'Lotto'.

Check failure on line 2 in src/ZKLottoGame.test.ts

View workflow job for this annotation

GitHub Actions / test (18, ubuntu-latest)

Module '"./ZKLottoGame"' has no exported member 'Lotto'.

Check failure on line 2 in src/ZKLottoGame.test.ts

View workflow job for this annotation

GitHub Actions / test (20, ubuntu-latest)

Module '"./ZKLottoGame"' has no exported member 'Lotto'.

/*
* This file specifies how to test the `Add` example smart contract. It is safe to delete this file and replace
Expand All @@ -10,7 +10,7 @@

let proofsEnabled = false;

describe('Add', () => {
describe('ZKLottoGame', () => {
let deployerAccount: Mina.TestPublicKey,
deployerKey: PrivateKey,
senderAccount: Mina.TestPublicKey,
Expand Down
143 changes: 105 additions & 38 deletions src/ZKLottoGame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
MerkleWitness,
} from 'o1js';

export { Lotto, ZKLottoGame };
export { LottoNumbers, GameBoard, ZKLottoGame };


export class MerkleWitness4 extends MerkleWitness(4) {}
Expand Down Expand Up @@ -88,33 +88,81 @@ export class MerkleWitness256 extends MerkleWitness(256) {}

// ==============================================================================



class Lotto {
lottogame: Field[];
class GameBoard extends Struct({
gmWeek: Field,
startTime: UInt64,
endTime: UInt64,
gameStatus: Bool,
}) {
static from(gmWeek: Field, startTime: UInt64, endTime: UInt64, gameStatus: Bool) {
return new GameBoard({ gmWeek: gmWeek, startTime: startTime, endTime: endTime, gameStatus: gameStatus});
}

}

constructor(serializedBoard: Field) {
const bits = serializedBoard.toBits(8);
let lottogame = [];
const row = Field(0);

lottogame.push(row);
this.lottogame = lottogame;
function Optional<T>(type: Provable<T>) {
return class Optional_ extends Struct({ isSome: Bool, value: type }) {
constructor(isSome: boolean | Bool, value: T) {
super({ isSome: Bool(isSome), value });
}

toFields() {
return Optional_.toFields(this);
}
};
}

class OptionalBool extends Optional(GameBoard) {}



serialize(): Field {
// class for creating the new Lotto Game Board
// class Lotto {
// lottogame: OptionalBool[];


// constructor() {
// let lottogame = [];

// const gmWeek = Field(0);
// const startTime = new UInt64(0);
// const endTime = new UInt64(0);
// const gameStatus = Bool(false);

return Field(0);
}

startNewLotto(week_: Field, weekStarted: Bool) {
for (let i = 0; i < 52; i++) {
}
}
// const gameBoard = GameBoard.from(gmWeek, startTime, endTime, gameStatus);
// const GameOption = (new OptionalBool(gameStatus, gameBoard));

// lottogame.push(GameOption);
// this.lottogame = lottogame;
// }


// startNewLotto(
// gmWeek: Field,
// startTime: UInt64,
// endTime: UInt64,
// gameStatus: Bool,
// ) {
// const gameBoard = GameBoard.from(gmWeek, startTime, endTime, gameStatus);
// for (let i = 1; i < 4; i++) {
// const toUpdate = gmWeek.equals(new Field(i));

// toUpdate.and(this.lottogame[i].isSome).assertEquals(false);

// // copy the game board (or update if new game is to start)
// this.lottogame[i] = Provable.if(
// toUpdate,
// new OptionalBool(true, gameBoard),
// this.lottogame[i]
// );
// }


// }


}
// }

class LottoNumbers extends Struct({
gmWeek: Field,
Expand All @@ -140,19 +188,19 @@ class LottoWinningHistory extends Struct({

class ZKLottoGame extends SmartContract {
// The board is serialized as a single field element
@state(Field) lottoboard = State<Field>();
@state(Field) lottoboard = State<GameBoard>();

//lotto game ended
//lotto game states
@state(Bool) lottoGameDone = State<Bool>();

@state(Field) lottogameWeek = State<Field>();
@state(Field) currentGameTimeStart = State<UInt64>();
@state(Field) currentGameTimeEnd = State<UInt64>();
@state(Field) gameduration = State<UInt64>();

//Lotto Winning numbers Hashed
@state(Field) LottoWinningNumbers = State<Field>();
@state(Field) LottoWinHistory = State<Field[]>();
//Lotto Winning numbers Details
@state(Field) LottoWeekWinningNumbers = State<LottoNumbers>();
@state(Field) LottoWinHistory = State<LottoNumbers[]>();
@state(Field) LottoWinHash = State<Field>();


@state(Field) storageTreeRoot = State<Field>();
Expand Down Expand Up @@ -196,9 +244,23 @@ class ZKLottoGame extends SmartContract {
/*Create New Lotto Week, start the new lotto for the week
This section to start the timer for the new Lotto Game week, should display the Week No. and Countdown
*/
// let lottoboard = new Lotto(this.lottoboard.get());
// lottoboard.startNewLotto(gameWeek, Bool(true));
// this.lottoboard.set(lottoboard.serialize());
this.lottoboard.requireEquals(this.lottoboard.get());
//this is for the demo. Production would require creating a new board each game week
const gameBoard = GameBoard.from(
gameWeek,
this.currentGameTimeStart.get(),
this.currentGameTimeEnd.get(),
this.lottoGameDone.get()
);
this.lottoboard.set(gameBoard);

/*let lottoboard = new Lotto(this.lottoboard.get());
lottoboard.startNewLotto(
gameWeek,
this.currentGameTimeStart,
this.currentGameTimeEnd, this.lottoGameDone) (gameWeek, Bool(true));
this.lottoboard.set(lottoboard.serialize());
*/

}

Expand All @@ -217,19 +279,24 @@ class ZKLottoGame extends SmartContract {
Ideally, we are to a more secure verifiable means to generate the winning numbers
possibly using VRF. But for this PoC, we manually set the winning numbers
*/

// verify the lotto week to end is same as current week
this.lottogameWeek.requireEquals(winningNums.gmWeek);
const winningHash = winningNums.hash();

//set winning details
this.LottoWeekWinningNumbers.set(winningNums);

//add to winning game lotto numbers array
//@notice MerkleMap might be a better option?
const winHistory = this.LottoWinHistory.get();
winHistory.push(winningHash);
this.LottoWinHistory.set(winHistory);

//set hash of winning details
this.LottoWinningNumbers.set(winningHash);
winHistory.push(winningNums);


// this.LottoWeekWinningNumbers.set(winningHash);

//@notice MerkleMap might be a better option?
//hash week winning numbers and set to LottoWinHash
this.LottoWinHash.requireEquals(this.LottoWinHash.get());
const winningHash = winningNums.hash();
this.LottoWinHash.set(winningHash);

}

Expand Down
Loading