Blockchain Smart Contracts and dApps Questions and Answers — Questions and Answers
Question 1: A development team is building a decentralized application (dApp) and needs to choose a JavaScript library to facilitate interaction with the Ethereum blockchain. They prioritize a smaller library size for faster loading times, a more modern API design using Promises and async/await, and strong TypeScript support. Which library would be the most suitable choice?
- Web3.js
- Ethers.js (Correct answer)
- Truffle.js
- Ganache.js
Correct answer: Ethers.js
Ethers.js is known for being more lightweight and having a smaller bundle size compared to Web3.js. It was designed with a modern JavaScript approach, natively using Promises and async/await, which many developers find more intuitive. Furthermore, it has excellent built-in TypeScript support, which aligns with the team's requirements.
Question 2: A smart contract requires real-world data, such as the current price of a commodity, to execute its logic. Blockchains cannot natively access off-chain information. What is the standard solution for securely feeding external data into a smart contract?
- A centralized API call from within the smart contract.
- A hardcoded data feed updated by the contract owner.
- A decentralized oracle network. (Correct answer)
- An inter-blockchain communication protocol.
Correct answer: A decentralized oracle network.
The 'oracle problem' refers to the inability of blockchains to access external, off-chain data. A decentralized oracle network solves this by acting as a secure bridge, fetching, verifying, and delivering external data to the smart contract. Relying on a single, centralized source would reintroduce a single point of failure, undermining the trustless nature of the blockchain.
Question 3: A developer deploys a smart contract but soon discovers a critical bug. Due to the immutability of the blockchain, the contract code cannot be changed. Which design pattern allows developers to update contract logic while preserving the contract's address and state?
- The Singleton Pattern
- The Factory Pattern
- The Observer Pattern
- The Proxy Pattern (Correct answer)
Correct answer: The Proxy Pattern
The Proxy Pattern is the most common method for creating upgradeable smart contracts. It works by separating the contract's logic and its data storage into two separate contracts: an implementation contract (logic) and a proxy contract (storage). Users interact with the proxy contract, which delegates calls to the implementation contract. To upgrade, the proxy is simply pointed to a new implementation contract, allowing the logic to be updated while the state and address remain unchanged.
Question 4: Which of the following best describes the core function of a Decentralized Autonomous Organization (DAO)?
- A legal framework for registering blockchain-based companies.
- A marketing collective for promoting dApps and crypto projects.
- An organization governed by rules encoded as smart contracts on a blockchain. (Correct answer)
- A centralized company that uses cryptocurrency for payroll.
Correct answer: An organization governed by rules encoded as smart contracts on a blockchain.
A DAO is an organization where the rules and operational logic are encoded in smart contracts and executed on a blockchain. This allows for decentralized governance, where members typically vote on proposals, and the outcomes are automatically enforced by the code, removing the need for a central authority or traditional management structure.
Question 5: A DeFi protocol's smart contract has a function that (1) checks a user's balance, (2) sends them their requested funds, and then (3) updates their balance. An attacker exploits this by creating a malicious contract that, upon receiving funds, immediately calls the withdrawal function again before the original call can update the balance. This vulnerability is known as a:
- Integer Overflow Attack
- Reentrancy Attack (Correct answer)
- Timestamp Dependence Attack
- Denial of Service Attack
Correct answer: Reentrancy Attack
A Reentrancy Attack occurs when an external call is made to an untrusted contract before the original function resolves its state changes. The malicious contract can then 're-enter' the original function, repeatedly executing a part of it (like a withdrawal) before the state (like the user's balance) is updated. The famous DAO hack of 2016 was a reentrancy attack.
Question 6: In the context of Ethereum, what is the primary purpose of 'gas'?
- To measure the physical energy consumption of the network.
- A token used for voting in network governance proposals.
- To act as a unit of measurement for the computational work required to execute transactions and smart contracts. (Correct answer)
- A protocol for compressing transaction data to save block space.
Correct answer: To act as a unit of measurement for the computational work required to execute transactions and smart contracts.
Gas is the unit used to measure the amount of computational effort required to execute specific operations on the Ethereum network. Every transaction or smart contract execution requires a certain amount of gas, and users must pay a fee (the gas fee) in ETH to compensate miners/validators for this computational work and to prevent network spam.
A development team is building a decentralized application (dApp) and needs to choose a JavaScript library to facilitate interaction with the Ethereum blockchain.
They prioritize a smaller library size for faster loading times, a more modern API design using Promises and async/await, and strong TypeScript support.
Which library would be the most suitable choice?