Blockchain Essentials | Assignment – 2
QUIZ – 2 | BLOCKCHAIN ESSENTIALS AND DEVELOPMENT OF DApps
1. Blockchain is a type of ____.
Database
Table
View
Object
2. Are Decentralized Blockchains immutable
TRUE
False
3. The types of Blockchain networks are
public
permissioned
consortium
All of the above
4. As Solidity does not support the Switch statement, what else can you do to replace the same behavior?
if-else
if else if else
for loop
while loop
5. What happens if the execution of a Smart Contract consumes more than the specified gas?
The contract still gets executed
The contract does not get executed
The user gets a refund
None of the above
6. In the Etherscan website, the transaction fee of each transaction is quoted in which of the following?
BTC
gwei
USD
INR
7. A pure function in solidity can access
state variables
local variables
both
none
8. ________ can be used to restrict access in solidity
modifiers
functions
constructors
all of the above
9. 1 gwei is _________ ETH
10^-9
10^-18
10^9
10^18
10. solidity is also known as
curly bracket language
access restriction language
both
none
11. The symbol _; in modifiers is used to
shift to function definition
shift to another modifier
terminate the process
All of the above
12. msg.sender fetches __________
20 bytes accounts address
40 bytes account address
ethers in an account
transaction hash
13. the data type address payable can
hold 20 bytes address
transfer ethers to another account
cannot transfer ethers to another account
only receive ethers
14. The data types in Ethereum are
uint256
fixedMxN
bool
All of the above
15. in fixedMxN
M should be divisible by 8
M should be divisible by 16
M should be divisible by 32
M should be divisible by 64
Assignment 2
1. PiggyBank is a concept we all know from childhood where we collect money till a particular
time period in a container called PiggyBank and will break it at some time to withdraw all the
money we collected.
Create a Smart Contract that implements the concept of piggybank.
a) Set the owner to the deployer of the contract using the constructor.
b) Deposit money to the contract (piggyBank) through receive() function.
c) Emit an event for the Deposit function.
d) Write the modifier onlyOwner() so that only the owner can call the withdraw function.
e) Write the withdraw() function to kill the contract and transfer all the contract balance
to the owner’s account.
f) Emit an event for withdrawal function.
pragma solidity ^0.8.0;
contract PiggyBank {
address payable owner;
event Deposit(address depositor, uint amount);
event Withdrawal(address withdrawer, uint amount);
constructor() public {
owner = msg.sender;
}
function receive() public payable {
emit Deposit(msg.sender, msg.value);
}
modifier onlyOwner() {
require(msg.sender == owner);
_;
}
function withdraw() public onlyOwner {
emit Withdrawal(msg.sender, address(this).balance);
owner.transfer(address(this).balance);
selfdestruct(owner);
}
}
Note that the above contract uses the selfdestruct
function, which is a way to destroy a smart contract and transfer its remaining balance to a specified address. The selfdestruct
function can only be called by the contract’s owner and it is important to use it with caution, because once called, the contract cannot be reused again.
2. The Ownable smart contract keeps the track of ownership. At the moment who has the
ownership of the contract or maybe tokens like NFTs. Ownership is transferrable so we can
transfer it from one user to the other and get the respective benefits.
Create a smart contract that implement the following functionalities:
a) Set the owner to the deployer of the contract using the constructor.
b) Write the modifier onlyOwner() so that only the owner can call certain functions like
transferring the current ownership of the contract.
c) Write the function for getting the current owner of the contract
d) Write the function for transferring the ownership of the contract from the current owner
to the owner.
Here is an example of a smart contract that implements the functionalities you described:
pragma solidity ^0.8.0;
contract Ownable {
address public owner;
constructor() public {
owner = msg.sender;
}
modifier onlyOwner() {
require(msg.sender == owner);
_;
}
function getOwner() public view returns (address) {
return owner;
}
function transferOwnership(address newOwner) public onlyOwner {
require(newOwner != address(0));
owner = newOwner;
}
}
Here, the constructor()
function is used to set the initial owner of the contract to the deployer. The onlyOwner()
modifier is used to restrict access to certain functions to only the current owner of the contract. The getOwner()
function returns the current owner of the contract, and the transferOwnership()
function allows the current owner to transfer ownership of the contract to a new address. The require(newOwner != address(0))
check is added to make sure that new owner address is not a zero address.
3. Explain Solidity and its features. What types of applications can be developed using
Solidity.
Solidity is a programming language for writing smart contracts on the Ethereum blockchain. It is similar to JavaScript and is designed to be used for creating decentralized applications (dApps) on the Ethereum platform.
Some features of Solidity include:
- Support for inheritance and complex user-defined types
- Support for libraries
- Support for complex user-defined types
- Support for events and logging
- Support for unit testing
Using Solidity, developers can create a wide range of decentralized applications such as:
- Decentralized exchanges (DEXs)
- Non-fungible tokens (NFTs)
- Gaming platforms
- Supply chain management systems
- Voting systems
- Crowdfunding platforms
- And many more
It is important to note that Solidity is specific to the Ethereum blockchain and cannot be used for other blockchain platforms.
* The material and content uploaded on this website are for general information and reference purposes only and don’t copy the answers of this website to any other domain without any permission or else copyright abuse will be in action.
Please do it by your own first!