you are missing the file format, which is .sol
at the end.
Try it and let us know.
Carlos Z
you are missing the file format, which is .sol
at the end.
Try it and let us know.
Carlos Z
hey Filip, I have a question⌠Dont know what this function means
govermentInstance.addTransaction(msg.sender, recipient, amount);
it shows me error that its undefinedâŚ
browser/Bank.sol:35:9: DeclarationError: Undeclared identifier. govermentInstance.addTransaction(msg.sender, recipient, amount); ^---------------^
pragma solidity 0.7.5;
contract Destroyable{
address public owner;
modifier onlyOwner{
require(msg.sender==owner, "not owner");
_;
}
constructor(){
owner=msg.sender;
}
event lastGoodbye(string lastMessage);
function kaboom() public onlyOwner {
emit lastGoodbye("Bye cruel world!");
selfdestruct(address(uint160(owner)));
}
}
In the task it is mentioned that we should create a new contract. That is why I put all in this one contract. Is this ok? Or should have I reused existing files like I saw in the solution.
(I know that is good pratcice to use imports, but just asking for this task)
pragma solidity 0.5.12;
import â./Ownable.solâ;
contract Destroyable is Ownable{
//address public owner;
function destroy() public onlyOwner {
address payable receiver = msg.sender;
selfdestruct(receiver);
}
}
Thank you
For the bank contract I did:
import "./Destroyable.sol"
contract Bank is Destoryable {
}
Hey @Gorana, hope you are ok.
there could be an issue in your smart contract, code line # 35 or 9. You might want to share your code so we can help you find the solution
If you have any more questions, please let us know so we can help you!
Carlos Z.
Hi @inahan, hope you are ok.
Your solution can work with no big issue, but has you realize, it will be better to inherit functionalities from other contracts (like ownable), so i could suggest to keep the good practice of importing other contracts into a new one in order to re-use that code properly.
I mean, if your Ownable contract already have all the functionalities for the onlyOwner
modifier and conditions, it is better practice to just import it to the Destroyable
contract instead of re-writing the same functions.
If you have any more questions, please let us know so we can help you!
Carlos Z.
@thecil Thank you Carlose again, it is Filipâs solution for assignment that shows me that:
pragma solidity 0.7.5;
import â./Ownable.solâ;
import â./Destroyable.solâ;
contract Bank is Ownable, Destroyable {
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);
msg.sender.transfer(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);
**govermentInstance.addTransaction(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;
}
}
I wached next video about external contracts, and its there, it is a function from external contract that I havent made yet, I tried to finish assignment first, and than checked for solution that wasnât working for me.
thank you, Iâll do that!
import "./Ownable.sol";
pragma solidity 0.7.5;
contract Destroyable is Ownable {
function close() public onlyOwner {
selfdestruct(msg.sender);
}
}
Indeed, the bank
contract works good if you delete a code line that should not be there for this assignment, at least not for now.
**govermentInstance.addTransaction(msg.sender, recipient, amount);**
If you delete or comment that line, the entire contract works as expected.
What does code lines do?: (if you are wonderingâŚ)
govermentInstance
should be a variable that bind the interface of a contract (government
contract i assume) so you can use that variable as instance for a contract interface.
govermentInstance.addTransaction(msg.sender, recipient, amount);
means, from thegovermentInstance
variable, invoke the functionaddTransaction
.
Problem is:
- In the bank contract we do not import nor define the contract neither its interface.
Easy Fix:- Remove that code line, contract works perfect.
This could be just a mistype that we made, and thank you for notifying it .
We will be fixing it these next days.
@jon_m just tagging you to made you aware of this partner
If you have any more questions, please let us know so we can help you!
Carlos Z.
This is my parent contract Ownable ( âownable.solâ) that controls access. Itâs located within the same directory as the child.
contract Ownable {
address payable public owner;
modifier onlyOwner {
require(msg.sender == owner);
_;
}
constructor () {
owner = msg.sender;
}
}
This is my âDestroyableâ contract, as you can see it inherits âOwnableâ and contains the function âremoveContract()â which permits the self destruction, callable only by the owner address.
import "./ownable.sol";
contract Destroyable is Ownable{
function removeContract() public onlyOwner {
selfdestruct(owner);
}
}
pragma solidity 0.7.5;
import './ownable.sol';
contract SelfDestructable is Ownable {
function close() public onlyOwner {
selfdestruct(owner);
}
}
contract Ownable {
address payable public owner;
constructor() {
owner = msg.sender;
}
modifier onlyOwner {
require(msg.sender == owner);
_;//Proceed to function code.
}
}
contract Bank is Ownable {
mapping(address => uint) balance;
function close() public onlyOwner {
selfdestruct(owner);
}
}
contract Ownable {
address owner;
modifier onlyOwner { //here we restrict access
require(msg.sender == owner); // this function is available only to the contract owner
_;
}
constructor (){
owner = msg.sender;
}
}
contract Destroyable is Ownable {
function close () public onlyOwner {
selfdestruct (msg.sender);
}
}
pragma solidity 0.7.5;
import "./Ownable.sol";
contract Destroyable is Ownable {
function close() public onlyOwner {
selfdestruct(owner);
}
}
Here it is, with a child contract as a test :
pragma solidity 0.7.5;
contract Destroyable {
address owner;
modifier onlyOwner{
require(msg.sender == owner);
_;
}
constructor(){
owner = payable(msg.sender);
}
function DestroyContract() public onlyOwner returns (bool isDestroyed){
selfdestruct(payable(owner));
return true;
}
}
contract MyContract is Destroyable{
// used to test the disposition of balance after the destruction of contract
function Deposit() public payable returns (uint balance){
return address(this).balance;
}
}
pragma solidity 0.7.5;
contract Destroyable {
address payable owner;
constructor(){
owner = msg.sender;
}
function destroy() public {
require(msg.sender == owner);
selfdestruct(owner);
}
}