Last active
January 29, 2025 10:30
-
-
Save Signor1/6d54db044bcdf30f8165fede2a2fa73e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // SPDX-License-Identifier: MIT | |
| pragma solidity ^0.8.27; | |
| /** | |
| * @title Ballot | |
| * @dev Implements voting process along with vote delegation | |
| */ | |
| contract Ballot { | |
| struct Voter { | |
| uint weight; // weight is accumulated by delegation | |
| bool voted; // if true, that person already voted | |
| address delegate; // person delegated to | |
| uint vote; // index of the voted proposal | |
| } | |
| struct Proposal { | |
| // If you can limit the length to a certain number of bytes, | |
| // always use one of bytes1 to bytes32 because they are much cheaper | |
| bytes32 name; // short name (up to 32 bytes) | |
| uint voteCount; // number of accumulated votes | |
| } | |
| address public chairperson; | |
| mapping(address => Voter) public voters; | |
| Proposal[] public proposals; | |
| /** | |
| * @dev Create a new ballot to choose one of 'proposalNames'. | |
| * @param proposalNames names of proposals | |
| */ | |
| constructor(bytes32[] memory proposalNames) { | |
| chairperson = msg.sender; | |
| voters[chairperson].weight = 1; | |
| for (uint i = 0; i < proposalNames.length; i++) { | |
| // 'Proposal({...})' creates a temporary | |
| // Proposal object and 'proposals.push(...)' | |
| // appends it to the end of 'proposals'. | |
| proposals.push(Proposal({ | |
| name: proposalNames[i], | |
| voteCount: 0 | |
| })); | |
| } | |
| } | |
| /** | |
| * @dev Give 'voter' the right to vote on this ballot. May only be called by 'chairperson'. | |
| * @param voter address of voter | |
| */ | |
| function giveRightToVote(address voter) public { | |
| require( | |
| msg.sender == chairperson, | |
| "Only chairperson can give right to vote." | |
| ); | |
| require( | |
| !voters[voter].voted, | |
| "The voter already voted." | |
| ); | |
| require(voters[voter].weight == 0); | |
| voters[voter].weight = 1; | |
| } | |
| /** | |
| * @dev Delegate your vote to the voter 'to'. | |
| * @param to address to which vote is delegated | |
| */ | |
| function delegate(address to) public { | |
| Voter storage sender = voters[msg.sender]; | |
| require(!sender.voted, "You already voted."); | |
| require(to != msg.sender, "Self-delegation is disallowed."); | |
| while (voters[to].delegate != address(0)) { | |
| to = voters[to].delegate; | |
| // We found a loop in the delegation, not allowed. | |
| require(to != msg.sender, "Found loop in delegation."); | |
| } | |
| sender.voted = true; | |
| sender.delegate = to; | |
| Voter storage delegate_ = voters[to]; | |
| if (delegate_.voted) { | |
| // If the delegate already voted, | |
| // directly add to the number of votes | |
| proposals[delegate_.vote].voteCount += sender.weight; | |
| } else { | |
| // If the delegate did not vote yet, | |
| // add to her weight. | |
| delegate_.weight += sender.weight; | |
| } | |
| } | |
| /** | |
| * @dev Give your vote (including votes delegated to you) to proposal 'proposals[proposal].name'. | |
| * @param proposal index of proposal in the proposals array | |
| */ | |
| function vote(uint proposal) public { | |
| Voter storage sender = voters[msg.sender]; | |
| require(sender.weight != 0, "Has no right to vote"); | |
| require(!sender.voted, "Already voted."); | |
| sender.voted = true; | |
| sender.vote = proposal; | |
| // If 'proposal' is out of the range of the array, | |
| // this will throw automatically and revert all | |
| // changes. | |
| proposals[proposal].voteCount += sender.weight; | |
| } | |
| /** | |
| * @dev Computes the winning proposal taking all previous votes into account. | |
| * @return winningProposal_ index of winning proposal in the proposals array | |
| */ | |
| function winningProposal() public view | |
| returns (uint winningProposal_) | |
| { | |
| uint winningVoteCount = 0; | |
| for (uint p = 0; p < proposals.length; p++) { | |
| if (proposals[p].voteCount > winningVoteCount) { | |
| winningVoteCount = proposals[p].voteCount; | |
| winningProposal_ = p; | |
| } | |
| } | |
| } | |
| /** | |
| * @dev Calls winningProposal() function to get the index of the winner contained in the proposals array and then | |
| * @return winnerName_ the name of the winner | |
| */ | |
| function winnerName() public view | |
| returns (bytes32 winnerName_) | |
| { | |
| winnerName_ = proposals[winningProposal()].name; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ### **1. What is the primary reason Ethereum uses an account-based model instead of UTXOs like Bitcoin?** | |
| A) Ethereum accounts store the state of smart contracts | |
| B) The UTXO model cannot handle token balances effectively | |
| C) Accounts enable sequential transaction processing | |
| D) All of the above | |
| **Answer: D) All of the above** | |
| --- | |
| ### **2. Which of the following statements about smart contract execution is true?** | |
| A) Smart contracts execute automatically without needing transaction fees | |
| B) Smart contracts modify blockchain state only if transactions are confirmed | |
| C) Once deployed, smart contracts can be upgraded freely | |
| D) None of the above | |
| **Answer: B) Smart contracts modify blockchain state only if transactions are confirmed** | |
| --- | |
| ### **3. How does a decentralized storage system like IPFS differ from traditional databases?** | |
| A) IPFS identifies files using content-based addressing instead of location-based addressing | |
| B) Files stored on IPFS are immutable and cannot be deleted | |
| C) IPFS requires all nodes to store every single file | |
| D) None of the above | |
| **Answer: A) IPFS identifies files using content-based addressing instead of location-based addressing** | |
| --- | |
| ### **4. What makes smart contracts immutable after deployment?** | |
| A) The contract code is stored permanently on the blockchain | |
| B) Miners control the execution and cannot modify deployed contracts | |
| C) The Ethereum Virtual Machine (EVM) prevents code changes | |
| D) All of the above | |
| **Answer: A) The contract code is stored permanently on the blockchain** | |
| --- | |
| ### **5. Which of these can lead to failed Ethereum transactions?** | |
| A) Running out of gas during execution | |
| B) Sending a transaction with an invalid nonce | |
| C) Trying to execute an invalid opcode in the EVM | |
| D) All of the above | |
| **Answer: D) All of the above** | |
| --- | |
| ### **6. What is the role of an RPC (Remote Procedure Call) provider in Web 3.0 applications?** | |
| A) It allows applications to interact with the blockchain without running a full node | |
| B) It validates smart contracts before they are deployed | |
| C) It enables off-chain computation of blockchain transactions | |
| D) None of the above | |
| **Answer: A) It allows applications to interact with the blockchain without running a full node** | |
| --- | |
| ### **7. Which of these factors influence Ethereum gas fees?** | |
| A) Network congestion | |
| B) The complexity of the smart contract being executed | |
| C) The base fee set by the Ethereum network | |
| D) All of the above | |
| **Answer: D) All of the above** | |
| --- | |
| ### **8. What is the purpose of the "view" function modifier in Solidity?** | |
| A) It prevents modification of the blockchain state | |
| B) It reduces gas fees for transactions that call the function | |
| C) It allows smart contracts to execute functions off-chain | |
| D) None of the above | |
| **Answer: A) It prevents modification of the blockchain state** | |
| --- | |
| ### **9. How does Ethereum achieve decentralization?** | |
| A) By distributing transaction validation among network participants | |
| B) By eliminating the need for third-party intermediaries | |
| C) By allowing nodes to store and verify blockchain data | |
| D) All of the above | |
| **Answer: D) All of the above** | |
| --- | |
| ### **10. Why are blockchain-based applications often slower than traditional web apps?** | |
| A) Every transaction must be validated and confirmed by multiple nodes | |
| B) Gas fees create delays in transaction execution | |
| C) Smart contract execution is limited by the EVM’s computational power | |
| D) All of the above | |
| **Answer: D) All of the above** | |
| --- | |
| ### **11. What happens if two Ethereum transactions from the same account have the same nonce?** | |
| A) Both transactions are processed, but only one modifies the blockchain state | |
| B) Only the transaction with the higher gas price gets executed | |
| C) The network rejects both transactions as invalid | |
| D) None of the above | |
| **Answer: C) The network rejects both transactions as invalid** | |
| --- | |
| ### **12. Why is the Merkle tree structure important in blockchain?** | |
| A) It enables efficient verification of large amounts of data | |
| B) It ensures the security and integrity of blockchain transactions | |
| C) It helps reduce the storage requirements for full nodes | |
| D) All of the above | |
| **Answer: D) All of the above** | |
| --- | |
| ### **13. Which of the following is true about smart contract wallets?** | |
| A) They allow users to set custom transaction validation rules | |
| B) They can enforce multi-signature approvals for security | |
| C) They are more expensive to use than externally owned accounts (EOAs) | |
| D) All of the above | |
| **Answer: D) All of the above** | |
| --- | |
| ### **14. How do Ethereum clients (such as Geth and Nethermind) differ from Ethereum nodes?** | |
| A) Ethereum clients provide APIs for interacting with the blockchain | |
| B) Nodes store and verify the blockchain state, while clients do not | |
| C) Clients and nodes are the same thing in blockchain architecture | |
| D) None of the above | |
| **Answer: A) Ethereum clients provide APIs for interacting with the blockchain** | |
| --- | |
| ### **15. What is a key security risk when interacting with smart contracts?** | |
| A) Reentrancy attacks can lead to the loss of funds | |
| B) Poorly written contracts can have logic vulnerabilities | |
| C) Malicious contracts can drain user wallets if given permission | |
| D) All of the above | |
| **Answer: D) All of the above** | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // SPDX-License-Identifier: MIT | |
| pragma solidity ^0.8.27; | |
| contract SaveEther{ | |
| address public owner; | |
| struct UserAccount{ | |
| uint256 amount; | |
| uint256 duration; | |
| } | |
| mapping (address user => UserAccount) userInfo; | |
| constructor(){ | |
| owner = msg.sender; | |
| } | |
| event Transfer(address indexed user, uint256 amount); | |
| function onlyOwner() private view{ | |
| require(msg.sender == owner, "User not allowed"); | |
| } | |
| function depositEther(uint256 _duration) public payable { | |
| require(msg.sender != address(0), "Not permitted"); | |
| require(msg.value >= 1 ether, "amount is too small"); | |
| UserAccount memory useracct; | |
| useracct.amount = msg.value; | |
| useracct.duration = block.timestamp + _duration; | |
| userInfo[msg.sender] = useracct; | |
| } | |
| function withdrawEther() public payable { | |
| require(msg.sender != address(0), "Not permitted"); | |
| UserAccount storage useracct = userInfo[msg.sender]; | |
| require(useracct.amount >= 1 ether, "Balance is not enough"); | |
| require(block.timestamp > useracct.duration, "Not yet due"); | |
| uint256 bal = useracct.amount; | |
| useracct.amount = 0; | |
| useracct.duration = 0; | |
| (bool sent, ) = payable(msg.sender).call{value: bal}(""); | |
| if(sent){ | |
| emit Transfer(msg.sender, bal); | |
| } | |
| } | |
| function getContractBalance() public view returns (uint256){ | |
| onlyOwner(); | |
| return address(this).balance; | |
| } | |
| function getDepositInfo() public view returns (uint256, uint256){ | |
| UserAccount storage userAcct = userInfo[msg.sender]; | |
| return(userAcct.amount, userAcct.duration); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment