- What is the base contract?
The parent contract is the base contract - Which functions are available for derived contracts?
All public and internal scoped functions - What is hierarchical inheritance?
A single contract acts as a base contract for multiple derived contracts.
- What is the base contract?
It is the parent contract in the context of inheritance.
- Which functions are available for derived contracts?
All public and internal scoped functions.
- What is hierarchical inheritance?
Hierarchical inheritance is the type of inheritance where multiple child contracts are derived from one parent contract.
- What is the base contract?
A base contract is a parent contract that is inherited.
- Which functions are available for derived contracts?
Public and internal functions are available for derived contracts.
- What is hierarchical inheritance?
A single contract acts as a base contract for different derived contracts.
- A base contract is a contract that passes properties onto a child contract.
- A child contract will have access to the base contractâs public and internal scoped functions and state variables.
- Hierarchical inheritance is exhibited when a single contract serves as a base contract for multiple inherited contracts.
Q1. 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. Parent contract is known as a base contract.
Q2. Which functions are available for derived contracts?
All public and internal scoped functions and state variables of the parent contract/s are available to derived contracts.
Q3. What is hierarchical inheritance?
In hierarchical inheritance a single contract acts as a base contract for multiple derived contracts.
contract B is A{
âŚ
}
contract C is A{
âŚ
}
-
What is the base contract?
This type of contract is called the parent contract, an inherited contract. -
Which functions are available for derived contracts?
All public and internal scope functions. -
What is hierarchical inheritance?
Hierarchical inheritance is a single contract that acts as a base contract
for multiple derived(child) contracts.
Hereâs my contribution for the âselfdestructâ opcode assignment with inheritance.
//SPDX-License-Identifier: GPL-3.0
//this is file destroyable.sol
pragma solidity ^0.7.5;
contract destroyable{
address payable owner;
constructor(){
owner = msg.sender;
}
modifier only_Owner{
require(msg.sender == owner);
_;
}
function destroy_contract() internal{
selfdestruct(owner);
}
}
//SPDX-License-Identifier: GPL-3.0
//file inherits_destroyable.sol
pragma solidity ^0.7.5;
import â./destroyable.solâ;
contract waiting_to_be_destroyed is destroyable{
function summon_the_end() public{
destroy_contract();
}
}
Hi, I cant seem to utilize the âValue Callsâ lesson correctly.
I have filipâs code exactly copied in my code, but here is the error message I receive.
Iâve added the âpayableâ component to my function header in both the external contract AND in the interface⌠Really confused
Hey @William, hope you are ok.
Could you please share your code?
You can use the âPreformatted Textâ Button to encapsulate any kind of code you want to show.
function formatText(){
let words = âIâm a preformatted Text box, Please use me wisely!â
}
Carlos Z.
Iâm trying to let my bank contract interact with the government contract. For some reason i get the following error message:
This pops up when I call âgetTransactionâ from the government contract. I think it has something to do with the msg.sender function that has to be payable. But I can not figure out what combination should be rightâŚ
pragma solidity 0.8.2;
import "./Ownable.sol";
interface GovermentInterface{
function addTransaction(address _from, address _to, uint _amount) external;
}
contract Bank is Ownable {
GovermentInterface GovermentInstance = GovermentInterface(0xa3A518Ba4e193Fb129aa379F5916d4660f15cE5D);
mapping(address => uint) balance;
event depositDone(uint amount, address indexed depositedTo);
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 onlyOwner returns (uint){
require(balance[msg.sender] >= amount);
payable(msg.sender).transfer(amount);
return balance[payable(msg.sender)];
}
function getBalance() public view returns (uint){
return balance[payable(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[payable(msg.sender)];
_transfer(payable(msg.sender), recipient, amount);
GovermentInstance.addTransaction(msg.sender, recipient, amount);
assert(balance[payable(msg.sender)] == previousSenderBalance - amount);
}
function _transfer(address from, address to, uint amount) private {
balance[from] -= amount;
balance[to] += amount;
}
}
Hey @thomascarl
function getBalance() public view returns (uint){
return balance[payable(msg.sender)];
}
There is not need to cast msg.sender
as payable here as you are not sending any ether to that address, thatâs just a âviewâ function.
Same thing for the âassertâ in transfer().
Please share the code of you GovermentInterface
contract so that I can deploy and take a look.
Cheers,
Dani
Okay so I only need to cast it as payable when ether is actually being send. Clear. I notice that it is difficult to follow the course with the newer version of solidity. So I think that I will just follow the rest of the course in the 0.7.5 version to avoid errors all the time.
Down below you will find my government contract:
pragma solidity 0.8.2;
contract government {
struct Transaction{
address from;
address to;
uint amount;
uint txId;
}
Transaction[] transactionLog;
function addTransaction(address _from, address _to, uint _amount) external payable {
transactionLog.push( Transaction(_from, _to, _amount, transactionLog.length) );
}
function getTransaction(uint _index) public view returns(address, address, uint) {
return (transactionLog[_index].from, transactionLog[_index].to, transactionLog[_index].amount);
}
}
pragma solidity 0.7.5;
//how to import a file correctly
import "./Destroyable.sol";
//INTERACT with an EXTERNAL contract, then inside main contract --> tell it where to find this external contract
interface GovernmentInterface{
//select specific function you want to use in external contract you are accessing...FULL FUNCTION HEADER
function addTransaction(address _from, address _to, uint _amount) external payable;
}
//example of inheriting 'Ownable' contract into our 'Bank' contract
contract Bank is Destroyable {
//Access external contract you are interacting with
//Type[GovernmentInterface] NameGiven[GovernmentInstance] = GovernmentInterface(address)
GovernmentInterface governmentInstance = GovernmentInterface(0xC4FA8Ef3914b2b09714Ebe35D1Fb101F98aAd13b);
mapping(address => uint) balance;
//EVENTS to be logged
event depositDone(uint amount, address indexed depositedTo);
//so we dont have to continue to write require() code in every function where executives only can access
//PAYABLE function that allows this "deposit" function to receive money when people call this function
function deposit() public payable returns (uint){
balance[msg.sender] += msg.value;
//call EVENT to be logged
emit depositDone(msg.value, msg.sender);
return balance[msg.sender];
}
function getOwner() public view returns (address) {
return owner;
}
//WITHDRAWING
function withdraw(uint amount) public returns (uint) {
//can also SUBSTITUTE msg.sender with
//address payable toSend = [desired address]; with msg.sender you dont need to include 'payable' because that's built into msg.sender
require(balance[msg.sender] >= amount);
balance[msg.sender] -= amount;
msg.sender.transfer(amount);
return balance[msg.sender];
}
function getBalance() public view returns (uint){
return balance[msg.sender];
}
//function that uses REQUIRE() function
function transfer(address recipient, uint amount) public onlyOwner{
//require what condition NEEDS to be true
//balance of the sender needs to be higher than the amount they are sending
//if this is true, then the rest of the function is run
require(balance[msg.sender] >= amount, "Nope you cannot do that douchebag!");
//can't send to yourself
require(msg.sender != recipient, "Nope cannot do that douche!");
//before we do transfer, save previous balance
uint previousSenderbalance = balance[msg.sender];
_transfer(msg.sender, recipient, amount);
//UTILIZE EXTERNAL contract --> governmentInstance.[function name(parameters)]
governmentInstance.addTransaction{value: 1000 wei}(msg.sender, recipient, amount);
//ASSERT that this should ALWAYS be the case. This should ALWAYS be true.
assert(balance[msg.sender] == previousSenderbalance - amount);
//event logs and further checks balance
}
//create private function that handles actual transfer (_transfer is what private functions use)
function _transfer(address fromPerson, address toPerson, uint amount) private {
//decrease balance from sender
balance[fromPerson] -= amount;
//increase balance of recipient by same amount
balance[toPerson] += amount;
}
}
Hi, I cant seem to utilize the âValue Callsâ lesson correctly.
I have filipâs code exactly copied in my code, but here is the error message I receive.
Iâve added the âpayableâ component to my function header in both the external contract AND in the interface⌠Really confused
@filip
I notice that you do not use Solidity 0.8.2, is it vital that I use the most updated version of Solidity or can I do everything using 0.7x. I notice that throughout the journey of coding, there has been minute difference among the 0.8.2 code and 0.7x.
For example, the withdraw function in the Bank contract looks like this in 0.8.2:
function withdraw(uint _amount) public view returns (uint) {
require(balance[msg.sender] >= _amount);
balance[msg.sender]-= _amount;
**payable(msg.sender).transfer(_amount);** vs msg.sender.transfer(amount);
return balance[msg.sender];
}
Or in the Destroy function:
address receiver = msg.sender;
} /*vs address payable receiver = msg.sender;
selfdestruct(receiver);*/
Thank you for your great instructing in this course, your feedback will be much appreciated.
Hi @Samuel1
I notice that you do not use Solidity 0.8.2, is it vital that I use the most updated version of Solidity or can I do everything using 0.7x.
I used for more than one year Solidity 0.5.12 I just now started using 0.8
It really does not matter which version you use (as long as it is higher than 0.5).
The way you code is the same, you just have some changes between a new version and the old one but if you know Solidity you will be able to switch version extremely easy.
For any question you have documentation.
Cheers,
Dani
1. What is the base contract?
The parent contract is known as the base contract. The base contracts are the ones from which other contracts inherit.
2. Which functions are available for derived contracts?
There is a is-a relationship between base and derived contracts and all public and internal scoped
functions and state variables are available to derived contracts.
3. What is hierarchical inheritance?
Hierarchical inheritance is when a single contract acts as a base contract for multiple derived contracts.
- What is the base contract?
The base contract is the parent contract from which other contracts can inherit. - Which functions are available for derived contracts?
All public and internal scoped functions and state variables of the base contract are available to derived contracts - What is hierarchical inheritance?
A single contract is base contract for multiple derived contracts
What is the base contract?
A parent contract that has derived classes
Which functions are available for derived contracts?
public, internal
What is hierarchical inheritance?
single contract that is the base for multiple contracts
-
The parent contract is the base contract from which other contracts can inherit.
-
All public and internal scoped functions and state variables are available to derived contracts.
-
Hierarchical inheritance has one parent contract with multiple child contracts that inherit the variables, functions, modifiers and events of the parent.
- What is the base contract?
- Which functions are available for derived contracts?
- What is hierarchical inheritance?
1.also know as the parent contract.
2all public and internal scoped
functions and state variables are available to derived contracts.
3.a single contract acts as a base contract for multiple derived contracts.