Oh that’s good, Atakan!
Congratulations on finishing this 101 course — enjoy 201
Oh that’s good, Atakan!
Congratulations on finishing this 101 course — enjoy 201
Inheritance Reading Assignment Answers.
What is the base contract?
The base contract is a copy of the derived class bytecode (The base contract is a copy of your contract’s bytecode) that is then copied into the derived contract bytecode.
Which functions are available for derived contracts?
All public and internal scoop functions are available to derived contracts.
What is hierarchical inheritance?
Hierarchical inheritances is when a single contract acts as the base contract for multiple
derived contracts
1.What is the base contract?
The base (or parent) contracts are the ones from which other contracts inherit
2.Which functions are available for derived contracts?
All functions from the parent contract
3.What is hierarchical inheritance?
where more than one class is inherited from a single parent or base class.
filip I copied your code word for word and got 7 error msgs not sure what I did wrong on my end
pragma solidity 0.7.5;
import "./ownable.sol";
contract Bank is ownable {
mapping(address => uint) balance;
address owner;
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 returns (uint){
require(balance[msg.sender] >= amount);
msg.sender.trasnfer(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 sufficeint");
rquire(msg.sender != recipient, "Dont transfer money to yourself");
uint previoussenderbalance = balance[msg.sender];
_transfer(msg.sender, recipient, amount);
asssert(balance[msg.sender] == previoussenderbalance - amount);
}
function _transfer(address from, address to, uint amount) private {
balance[from] -= amount;
balance [to] += amount;
}
What is the base contract?
A base contract is a contract in which another contract refers and derives its variable, functions, modifier or events from
Which functions are available for derived contracts?
Public, Internal, non-private state variable, events and modifiers
What is hierarchical inheritance?
Where one base contract has 2 or more child contracts deriving from the said base contract
Hi @ibn5x,
Q2 & Q3
I’ve tried reading this several times over, but it still doesn’t makes any sense to me. How can the base contract be a copy of the derived contract which is then copied into the derived contract?
The base contract is the parent contract in an inheritance relationship. This means that the code in the base contract that is inherited by the derived contract is effectively “copied into” the derived contract when the derived contract is deployed. Or put another way, the inherited code from the base contract is available in the deployed derived contract. Is that what you mean?
@jon_m below is my government and bank contract but my government is not getting report from the bank even while the whole code is correct and fully deploy we…pls help me check
pragma solidity 0.7.5;
contract CBN {
struct Banktransaction {
address from;
address to;
uint amount;
uint txId;
}
Banktransaction[] transactionlog;
function addTransaction(address _from, address _to, uint _amount) external {
transactionlog.push(Banktransaction(_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;
interface CBNsubmitRoute {
function addTransaction(address _from, address _to, uint _amount) external;
}
contract Bank {
CBNsubmitRoute CBNcontract = CBNsubmitRoute(0x5B38Da6a701c568545dCfcB03FcB875f56beddC4);
//event SKETCH
event balanceAdded(uint amount, address depostedto);
mapping(address => uint) balance;
address owner;
constructor() {
owner = msg.sender;
}
//(D) Modifier SKETCH
modifier onlyOwner {
require(msg.sender == owner, "this is not the owner");
_; // it means run the code.
}
function deposit() public payable returns(uint){
balance[msg.sender] += msg.value;
//event sktech
emit balanceAdded(msg.value, msg.sender);
return balance[msg.sender];
}
function getBalance() public view returns(uint){
return balance[msg.sender];
}
function withdraw(uint amount) public onlyOwner returns (uint){
require(balance[msg.sender] >= amount, “Balancee not sufficient”);
balance[msg.sender] -= amount;
msg.sender.transfer(amount);
return balance[msg.sender];
}
// this function sends a specific amount from the sender to the receiver by reducig the sender’s balance with an amount amd update the reciever’s balance by adding the particular amount reduced from the sender’s wallet to it.
function transfer(address recipient, uint amount) public {
require(balance[msg.sender] >= amount, “balance not sufficient”);
require(msg.sender != recipient, " this is your wallet you can’t send to your self");
// assert code implementation
// balance[msg.sender] -= amount;
// balance[recipient] += amount
uint previousSenderBalance = balance[msg.sender];
_transfer(msg.sender, recipient, amount);
CBNcontract.addTransaction(msg.sender, recipient, amount);
assert(balance[msg.sender] == previousSenderBalance - amount);
//_transfer(msg.sender, recipient, amount);
}
function _transfer(address from, address to, uint amount) private {
balance[from] -= amount;
balance[to] += amount;
//CBNcontract.addTransaction(msg.sender, recipient, amount);
}
}
Hi @Zeca,
Apologies for the delay in giving you some feedback on your answers to this assignment.
Q1
Q2
Not all functions in the parent contract are available.
Functions with public or internal visibility are available for derived contracts, but those with private or external visibility are not inherited.
Q3
It’s the other way round…
Hierarchical inheritance is where more than one derived contract (or class) inherits from the same, single base contract (or class).
Or put another way…
It’s where the same, single base contract is inherited by more than one derived contract.
Let me know if anything is unclear, or if you have any questions
Hey @jon_m
That’s Exaaaaactlly what I mean lol, I really had a hard time articulating myself with this one. I definitely believe I overstand the concept. I even kept saying to myself when reading the question “well the base contract IS the parent contract” for whatever reason I felt like it was deeper than that. Then attempted to express that miserably lol. Sorry about that, but yes put a better way “the inherited code from the base contract is available in the deployed derived contract”.
Thank you for your time,
-Mikal
Hi @Al86
The errors are mainly caused by:
See my comments in your code, below. The numbers refer to the order in which I suggest you address them, because when you resolve some errors, others will be flagged by the compiler.
pragma solidity 0.7.5;
import "./ownable.sol";
contract Bank is ownable {
mapping(address => uint) balance;
address owner; // (1) REMOVE
/* This is probably already defined in your parent contract Ownable.
If so it will be inherited by Bank. */
event depositdone(uint amount, address indexed depositedto);
} // (2) REMOVE FROM HERE AND PUT AT THE VERY END OF THE CONTRACT
// compiler reads this as the contract's closing curly bracket!
/* This is why you get all the errors in the functions below,
because they are not defined within the contract.
The error messages refer to these as "free" functions */
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(balance[msg.sender] >= amount);
msg.sender.trasnfer(amount);
// ^^ (5) SPELLING ERROR
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 sufficeint");
rquire(msg.sender != recipient, "Dont transfer money to yourself");
// ^^ (3) SPELLING ERROR
uint previoussenderbalance = balance[msg.sender];
_transfer(msg.sender, recipient, amount);
asssert(balance[msg.sender] == previoussenderbalance - amount);
// ^^^ (4) SPELLING ERROR
}
function _transfer(address from, address to, uint amount) private {
balance[from] -= amount;
balance [to] += amount;
}
// } (2) MISSING CLOSING CURLY BRACKET (see comment above)
Hey @ibn5x !
I’m glad we both understand each other At one point I thought maybe you’d discovered some deeper meaning to inheritance that I haven’t discovered yet
Yes… a lot of people do — including myself, sometimes I think the secret is to keep the definitions as simple as possible, otherwise they can easily become confusing instead of being clear.
…and that’s the main thing!
By the way, I haven’t forgotten your Inheritance Assignment. I’ve been a bit behind with my feedback this week, but I can assure you, I will get to it soon.
Hi @Phaxsam,
Both contracts are now correct and compile without any errors. Well done for fixing all of the issues I’ve raised in the various feedbacks I’ve given you
The problem you are still having could be a result of the following:
The CBN contract needs to be deployed before Bank, and the CBN contract’s address assigned to the CBNcontract
instance state variable. You do this by copying the contract address to the clipboard (click on the clipboard icon next to the deployed CBN contract in the Deploy & Run Transactions panel), and then pasting it into the correct place in Bank (replacing the old CBN contract address which is no longer valid).
If CBN is redeployed at any stage, then it will have a different contract address, and the CBN address previously assigned to the CBNcontract
instance will need to be updated to the new one before Bank is redeployed .
I have deployed both of your contracts using this method, and I am able to successfully retrieve the bank transaction data (for a transfer) from CBN’s transaction log, by calling getTransaction() with the appropriate index.
Let me know if this fixes the problem, and you manage to get it working
Once you have got it working for transfers, if you also want to add external function calls to addTransaction() for deposits and withdrawals (which is what I think you were trying to do before), then you will need to ensure that you pass a zero address for either the _from or _to parameters. This is because deposits and withdrawals only involve a single account holder, instead of two. The other way to do it would be to add separate functions for these transactions to your CBN contract, with just 2 instead of 3 parameters. You will then also need to think about how this information is correctly assigned to the different properties of each BankTransaction instance which is pushed to the transactionLog array. If you do want to have records for all 3 types of transaction, then I also suggest you add an extra parameter to the addTransaction function(s) which records the type of transaction (deposit, withdrawal or transfer), unless of course you decide to store each type of transaction in a different log, which is the other way to manage this.
Let me know if you have any questions about this
oh…I can get now…thanks for all the support…don’t mind my too many questions
Hi Jon
I see basic mistakes, I need to practice coding on more than 3 hours of sleep
Thanks for your feedback
That would be a good idea, Al
Hi there,
I don’t understand this concept of ‘contract balance’ that as @filip said we can always check with address(this).balance
So far we have worked on user account balance only, and we have created a specific mapping variable for this.
So what is a contract balance ?
Thanks for your help.