Skip to content

Commit

Permalink
Added script to deploy and setup quiz in one task
Browse files Browse the repository at this point in the history
  • Loading branch information
ZigaMr committed Jul 16, 2024
1 parent 3a389f0 commit f0d71e2
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,13 @@ Checklist after deploying a production-ready quiz:
npx hardhat getCoupons 0x385cAE1F3afFC50097Ca33f639184f00856928Ff --network sapphire-testnet
```

### Deploy and setup quiz in with a single task

You can also run the entire quiz with ```deployAndSetupQuiz```. This script handles all the above steps, you only need to provide the PRIVATE_KEY environment variable.
```shell
npx hardhat deployAndSetupQuiz --gaslessaddress <gaslessAddress> --gaslesssecret <gaslessSecret> --network sapphire-testnet
```

Check out other hardhat tasks that will help you manage the quiz:

```shell
Expand Down
85 changes: 85 additions & 0 deletions backend/hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,91 @@ task('setGaslessKeyPair')
console.log(`Successfully set gasless keypair to ${args.payerAddress}, secret ${args.payerSecret} and nonce ${nonce}. Transaction hash: ${receipt!.hash}`);
});

// Deploy and setup Quiz contract
task('deployAndSetupQuiz', 'Deploys Quiz contract and performs setup tasks')
// .addOptionalParam('network', 'Specify the network', 'sapphire-testnet')
.addOptionalParam('questionsfile', 'File containing questions in JSON format', 'test-questions.json')
.addOptionalParam('couponsfile', 'File containing coupons, one per line', 'test-coupons.txt')
.addOptionalParam('reward', 'Reward in ROSE', '2.0')
.addOptionalParam('gaslessaddress', 'Payer address for gasless transactions')
.addOptionalParam('gaslesssecret', 'Payer secret key for gasless transactions')
.addOptionalParam('fundamount', 'Amount in ROSE to fund the contract', '100')
.addOptionalParam('fundgaslessamount', 'Amount in ROSE to fund the gasless account', '10')
.addOptionalParam('contractaddress', 'Contract address for status check')
.setAction(async (args, hre) => {
// Run compilation
await hre.run('compile');

// For deployment unwrap the provider to enable contract verification.
const uwProvider = new JsonRpcProvider(hre.network.config.url);
// const accounts = await hre.ethers.getSigners();

// Deploy Quiz contract
const Quiz = await hre.ethers.getContractFactory('Quiz', new hre.ethers.Wallet(accounts[0], uwProvider));
const quiz = await Quiz.deploy();
await quiz.waitForDeployment();
console.log(`Quiz deployed at address: ${await quiz.getAddress()}`);

// Add questions
const questionsFilePath = path.resolve(args.questionsfile);
const questions = JSON.parse(await fs.readFile(questionsFilePath, 'utf8'));
for (const question of questions) {
const tx = await quiz.addQuestion(question.question, question.choices);
const receipt = await tx.wait();
console.log(`Added question: ${question.question}. Transaction hash: ${receipt!.hash}`);
}

// Add coupons
const couponsFilePath = path.resolve(args.couponsfile);
const coupons = (await fs.readFile(couponsFilePath, 'utf8')).split('\n').filter(Boolean);
for (let i = 0; i < coupons.length; i += 20) {
const chunk = coupons.slice(i, i + 20);
const tx = await quiz.addCoupons(chunk);
const receipt = await tx.wait();
console.log(`Added coupons: ${chunk}. Transaction hash: ${receipt!.hash}`);
}

// Set reward
const txReward = await quiz.setReward(hre.ethers.parseEther(args.reward));
const receiptReward = await txReward.wait();
console.log(`Set reward to ${args.reward} ROSE. Transaction hash: ${receiptReward!.hash}`);

// Set gasless keypair
const nonce = await hre.ethers.provider.getTransactionCount(args.gaslessaddress);
const txGasless = await quiz.setGaslessKeyPair(args.gaslessaddress, args.gaslesssecret, nonce);
const receiptGasless = await txGasless.wait();
// console.log(`Set gasless keypair. Transaction hash: ${receiptGasless!.hash}`);
console.log(`Successfully set gasless keypair to ${args.payerAddress}, secret ${args.payerSecret} and nonce ${nonce}. Transaction hash: ${receiptGasless!.hash}`);

// Fund contract
const txFund = await (await hre.ethers.getSigners())[0].sendTransaction({
to: await quiz.getAddress(),
value: hre.ethers.parseEther(args.fundamount),
});
const receiptFund = await txFund.wait();
console.log(`Funded contract with ${args.fundamount} ROSE. Transaction hash: ${receiptFund!.hash}`);

// Fund gasless account
const txFundGasless = await (await hre.ethers.getSigners())[0].sendTransaction({
to: args.gaslessAddress,
value: hre.ethers.parseEther(args.fundgaslessamount),
});
const receiptFundGasless = await txFundGasless.wait();
console.log(`Funded gasless account with ${args.fundgaslessamount} ROSE. Transaction hash: ${receiptFundGasless!.hash}`);

// Check status if contract address provided
if (args.contractaddress) {
await hre.run('status', { address: args.contractaddress });
} else {
await hre.run('status', { address: await quiz.getAddress() });
// console.log('Provide --contractAddress to check quiz contract status.');
}
});





// Hardhat Node and sapphire-dev test mnemonic.
const TEST_HDWALLET = {
mnemonic: "test test test test test test test test test test test junk",
Expand Down

0 comments on commit f0d71e2

Please sign in to comment.