UNPKG

l1-lottery-contracts

Version:

This repo contains smart contracts and related scripts for ZkNoid L1 Lottery proposed [here](https://forums.minaprotocol.com/t/zknoid-l1-lottery/6269)

96 lines 4.43 kB
import { AccountUpdate, fetchAccount, Field, Mina, PrivateKey, PublicKey, } from 'o1js'; import { PlotteryFactory } from '../src/Factory.js'; import { configDefaultInstance, getFedFactoryManager } from './utils.js'; import * as fs from 'fs'; let { transactionFee } = configDefaultInstance(); let deployerKey = PrivateKey.fromBase58(process.env.DEPLOYER_KEY); let deployer = deployerKey.toPublicKey(); console.log(`Using deployer ${deployer.toBase58()}`); let from = process.argv[2]; let to = process.argv[3]; if (!from || !to) { throw Error(`You should provide from round and to round`); } console.log(`Compiling PlotteryFactory`); const networkId = Mina.activeInstance.getNetworkId().toString(); let { verificationKey } = await PlotteryFactory.compile(); console.log(`Factory verification key: ${verificationKey.hash.toString()}`); let factoryAddress; if (!fs.existsSync(`./deployV2/${networkId}/${verificationKey.hash.toString()}`)) { throw Error(`No factory deployment found. Deploy it first`); } const factoryDataPath = `./deployV2/${networkId}/${verificationKey.hash.toString()}/factory.json`; if (fs.existsSync(factoryDataPath)) { let factoryData = fs.readFileSync(factoryDataPath); factoryAddress = PublicKey.fromBase58(JSON.parse(factoryData.toString()).address); } else { throw Error(`No factory deployment found. Deploy it first`); } let factory = new PlotteryFactory(factoryAddress); let deployments; const deploymentsPath = `./deployV2/${networkId}/${verificationKey.hash.toString()}/deployments.json`; if (fs.existsSync(deploymentsPath)) { let deploymentsBuffer = fs.readFileSync(deploymentsPath); deployments = JSON.parse(deploymentsBuffer.toString()); } else { deployments = {}; } const factoryManager = await getFedFactoryManager(factory); const txPromises = []; await fetchAccount({ publicKey: deployer }); await fetchAccount({ publicKey: factory.address }); let currentNonce = Mina.getAccount(deployer).nonce; console.log(factory); throw Error(`Not implemented`); for (let round = +from; round <= +to; round++) { console.log(`Nonce: ${currentNonce.toString()}`); if (factoryManager.roundsMap.get(Field(round)).greaterThan(0).toBoolean()) { console.log(`Plottery for round ${round} have been deployed`); continue; } let witness = factoryManager.roundsMap.getWitness(Field(round)); let plotteryPrivateKey = PrivateKey.random(); let plotteryAddress = plotteryPrivateKey.toPublicKey(); let randomManagerPrivateKey = PrivateKey.random(); let randomManagerAddress = randomManagerPrivateKey.toPublicKey(); console.log(`Deploying plottery: ${plotteryAddress.toBase58()} and random manager: ${randomManagerAddress.toBase58()} for round ${round}`); console.log(`Contract root: ${factory.roundsRoot.get()}`); console.log(`Manager root: ${factoryManager.roundsMap.getRoot()}`); let tx = await Mina.transaction({ sender: deployer, fee: 10 * transactionFee }, async () => { AccountUpdate.fundNewAccount(deployer); AccountUpdate.fundNewAccount(deployer); await factory.deployRound(witness, randomManagerAddress, plotteryAddress); }); tx.transaction.feePayer.body.nonce = currentNonce; currentNonce = currentNonce.add(1); await tx.prove(); let txInfo = await tx .sign([deployerKey, randomManagerPrivateKey, plotteryPrivateKey]) .send(); txPromises.push(txInfo); // const txResult = await txInfo.safeWait(); // if (txResult.status === 'rejected') { // console.log(`Transaction failed due to following reason`); // console.log(txResult.errors); // break; // } factoryManager.addDeploy(round, randomManagerAddress, plotteryAddress); console.log(`Before root change: ${factory.roundsRoot.get()}`); factory.roundsRoot.set(factoryManager.roundsMap.getRoot()); console.log(`After root change: ${factory.roundsRoot.get()}`); deployments[round] = { randomManager: randomManagerAddress.toBase58(), plottery: plotteryAddress.toBase58(), }; } Promise.all(txPromises); // Write result to file if (!fs.existsSync(`./deployV2/${networkId}/${verificationKey.hash.toString()}`)) { fs.mkdirSync(`./deployV2/${networkId}/${verificationKey.hash.toString()}`, { recursive: true, }); } fs.writeFileSync(deploymentsPath, JSON.stringify(deployments, null, 2)); //# sourceMappingURL=pdeploy_1rnd_pottery.js.map