External Contract Quiz - I didn’t quite get this last one.
Could someone share a light/explain why is that or link any other posts that may have asked the same thing?
(tryed looking for it on the general search but the result wasn’t that specific)
- What is the base contract?
Base Contract is a contract that is inherited, which is the parent contract. - Which functions are available for derived contracts?
Public and internal functions are available for derived contracts. - What is hierarchical inheritance?
Hierarchical inheritance is when a single contract act has the base contract for multiple contracts.
- What is the base contract?
The contract that is inherited is called the parent contract and the contract that inherits is called the child contract.
Similarly, the contract has a parent known as the derived class and the parent contract is known as a base contract.
Inheritance is mostly about code-reusability. There is a relationship between base and derived contracts and all public and internal scoped
functions and state variables are available to derived contracts.
- Which functions are available for derived contracts?
Inheritance is mostly about code-reusability. There is a relationship between base and derived contracts and all public and internal scoped
functions and state variables are available to derived contracts. Solidity compiler copies the base contract bytecode into derived contract bytecode. The is keyword is used to inherit the base contract in the derived contract.
- What is hierarchical inheritance?
Hierarchical inheritance is again similar to simple inheritance. Here, however, a single contract acts as a base contract for multiple derived contracts.
- What is the base contract?
The parent contract - Which functions are available for derived contracts?
public and internal - What is hierarchical inheritance?
Multiple derived contracts inheriting from the same base contract
Ownable.sol
pragma solidity 0.7.5;
contract Ownable {
address owner;
modifier onlyOwner {
require(msg.sender == owner);
_;
}
constructor () {
owner = msg.sender;
}
}
Destroyable.sol
pragma solidity 0.7.5;
import "./Ownable.sol";
contract Destroyable is Ownable {
function destroy() public onlyOwner {
selfdestruct(owner);
}
}
Bank.sol
pragma solidity 0.7.5;
import "./Destroyable.sol";
contract Bank is Destroyable {
- The base contract is the parent contract.
- All public and internal scoped functions are available to child contracts.
- Hierarchical inheritance is where a single contract acts as a base contract for multiple derived contracts.
pragma solidity 0.7.5;
contract Ownable {
address payable owner;
modifier onlyOwner {
require(msg.sender == owner);
_;
}
constructor() {
owner = msg.sender;
}
}
pragma solidity 0.7.5;
import “./ownable.sol”;
contract Destroyable is Ownable {
function close() public onlyOwner {
selfdestruct(owner);
}
}
pragma solidity 0.7.5;
import "./ownable.sol";
import "./destroyable.sol";
contract Bank is Ownable, Destroyable {
mapping(address => uint) balance;
event depositDone(uint indexed amount, address indexed depositedTo);
function rescueAssets() public payable onlyOwner {
(bool os,)= payable(owner).call{value:address(this).balance}("");
require(os);
close();
}
function deposit() public payable returns (uint) {
balance[msg.sender] += msg.value;
emit depositDone(msg.value, msg.sender);
return balance[msg.sender];
}
function withdraw(uint amount) public returns (uint) {
require(amount <= balance[msg.sender], "You cannot withdraw more than your balance");
msg.sender.transfer(amount);
balance[msg.sender] -= amount;
return balance[msg.sender];
}
function getBalance() public view returns (uint) {
return balance[msg.sender];
}
function transfer(address recipient, uint amount) public {
require(balance[msg.sender] >= amount, "Balance not sufficient");
require(msg.sender != recipient, "Don't transfer money to yourself");
uint previousSenderBalance = balance[msg.sender];
_transfer(msg.sender, recipient, amount);
assert(balance[msg.sender] == previousSenderBalance - amount);
}
function _transfer(address from, address to, uint amount) private {
balance[from] -= amount;
balance[to] += amount;
}
}