Blockchain Assignment, Exercises of Cryptography and System Security

Assignment i have Implemented on Blockchain

Typology: Exercises

2022/2023

Uploaded on 08/03/2023

mirza-mohd-junaid
mirza-mohd-junaid 🇮🇳

4 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Name : Mirza Mohammed Junaid
Roll No : 9459
Branch : AI&DS
Assignment 4
1. Q. Develop a smart contract with critical functions that should be paused in
certain scenarios (e.g., emergency). Implement a modifier to pause these
functions when needed.
Code :
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract MyContract {
address public owner;
bool public paused;
uint256 public criticalData;
modifier onlyOwner() {
require(msg.sender == owner, "Only contract owner can call this function.");
_;
}
modifier whenNotPaused() {
require(!paused, "Contract is paused.");
_;
}
constructor() {
owner = msg.sender;
paused = false;
}
function pauseContract() public onlyOwner {
paused = true;
}
function unpauseContract() public onlyOwner {
paused = false;
}
function criticalFunction(uint256 newValue) public whenNotPaused onlyOwner {
criticalData = newValue;
}
}
pf2

Partial preview of the text

Download Blockchain Assignment and more Exercises Cryptography and System Security in PDF only on Docsity!

Name : Mirza Mohammed Junaid

Roll No : 9459

Branch : AI&DS

Assignment 4

1. Q. Develop a smart contract with critical functions that should be paused in

certain scenarios (e.g., emergency). Implement a modifier to pause these

functions when needed.

Code :

// SPDX-License-Identifier: MIT pragma solidity ^0.8.0; contract MyContract { address public owner; bool public paused; uint256 public criticalData; modifier onlyOwner() { require(msg.sender == owner, "Only contract owner can call this function."); _; } modifier whenNotPaused() { require(!paused, "Contract is paused."); _; } constructor() { owner = msg.sender; paused = false; } function pauseContract() public onlyOwner { paused = true; } function unpauseContract() public onlyOwner { paused = false; } function criticalFunction(uint256 newValue) public whenNotPaused onlyOwner { criticalData = newValue; } }

Output :