hi, here is my take on this assignment - cheers
https://github.com/lyonslj/solidity/tree/main/Inheritance
the beginning of âMyContractâ file
- add import for the âDestroyableâ contract
- add inheritance Destroyable
pragma solidity 0.5.12;
import "./ownable.sol";
import "./destroyable.sol";
contract MyContract is Ownable, Destroyable {
File destroyable.sol
pragma solidity 0.5.12;
import "./ownable.sol";
contract Destroyable is Ownable {
function destroyContract() public onlyOwner {
selfdestruct(address(uint160(owner)));
}
}
Hehy @Johnl, hope you are great.
Now i dont think your solution on the destroyable contract is the best approach, if you are looking to fix the issue with the âpayableâ address, it is better to just change your ownable contract address to payable
, or just convert it on your destroyable contract.
Although your solution could work, it takes more computing resources to calculate the same therefore, more gas fees at the end.
You also can define your owner
address as payable at the begining of your Ownable
contract in order to avoir the conversion (address(uint160(owner)))
.
You can just use address payable public owner
, then in your destroy
function does not need the conversion.
Carlos Z
import â./Ownable.solâ;
pragma solidity 0.5.12;
contract Destroyable is Ownable {
function torol() public onlyOwner{
address payable receiver = msg.sender;
selfdestruct(receiver);
}
}
Here is my solution.
pragma solidity 0.5.12;
contract Destroyable{
address public owner;
modifier onlyOwner(){
require(msg.sender == owner);
_;
}
constructor() public{
owner = msg.sender;
}
function destroyContract() public onlyOwner {
selfdestruct(msg.sender);
}
}
When I wrote selfdestruct(owner) it threw an error but it didnât when I wrote msg.sender. Why is this? I am fairly certain I got it right anyway but please let me know if the code is faulty or that I need to improve it somehow.
I am a bit lostâŚ
Adding to the main program sendTrans:
Creating the contract kill.sol
But somehow I lost the ability to add person etcâŚ
How come reduced code does not decrease compilation time?
Does soilidity recognize functionality so even if I run rhe same function several times it doesnât matter if the function is embedded in a child contract or not? Thus reducing the gas?
Compilation time will take the same, instead of having 1 big contract with all the logic, you divided into many, using the inheritance, but for the compiler, it will be ârun/compile this amount of code linesâ, in that term, the compiler does not care if you have 1 big contract or multiple contracts.
For gas cost is the same, is not the inheritance who reduce the gas cost, its the way you code the logic (fewer instructions on a function logic will reduce gas costs).
Carlos Z.
Could you show me the error that shows up when you try run the createPerson function? I need the code to help you
Carlos Z
import â./kill.solâ;
import â./Ownable.solâ;
pragma solidity 0.5.12;
contract sendTrans is Ownable, kill{
struct Person {
uint id;
string name;
uint age;
uint height;
bool senior;
}
event personCreated(string name, bool senior);
event personDeleted(string name, bool senior, address deletedBy);
uint public balance;
modifier costs(uint cost){
require(msg.value >= cost);
_;
}
mapping (address => Person) private people;
address[] private creators;
function createPerson(string memory name, uint age, uint height) public payable costs(100){
require(age < 150, "Age needs to be below 150");
require(msg.value >= 1 ether);
balance += msg.value;
//This creates a person
Person memory newPerson;
newPerson.name = name;
newPerson.age = age;
newPerson.height = height;
if(age >= 65){
newPerson.senior = true;
}
else{
newPerson.senior = false;
}
insertPerson(newPerson);
creators.push(msg.sender);
assert(
keccak256(
abi.encodePacked(
people[msg.sender].name,
people[msg.sender].age,
people[msg.sender].height,
people[msg.sender].senior
)
)
==
keccak256(
abi.encodePacked(
newPerson.name,
newPerson.age,
newPerson.height,
newPerson.senior
)
)
);
emit personCreated(newPerson.name, newPerson.senior);
}
function insertPerson(Person memory newPerson) private {
address creator = msg.sender;
people[creator] = newPerson;
}
function getPerson() public view returns(string memory name, uint age, uint height, bool senior){
address creator = msg.sender;
return (people[creator].name, people[creator].age, people[creator].height, people[creator].senior);
}
function deletePerson(address creator) public onlyOwner {
string memory name = people[creator].name;
bool senior = people[creator].senior;
delete people[creator];
assert(people[creator].age == 0);
emit personDeleted(name, senior, owner);
}
function getCreator(uint index) public view onlyOwner returns(address){
return creators[index];
}
function withdrawAll() public onlyOwner returns(uint) {
uint toTransfer = balance;
balance = 0;
msg.sender.transfer(toTransfer);
return toTransfer;
}
}
At the end of the function, your cost is 100 of what ???, I mean it should be costs(100 wei)
or costs(100 ether)
âŚmaybe thats the only error.
Carlos Z
I did the assumption wei was default. Iâll change and try again.
Perhaps I just did the misstake @filip highlighted later: select the correct contract to deployâŚ
It seems to work fine.
AndâŚthe value must adjusted.
import â./Ownable.solâ;
pragma solidity 0.5.12;
contract Destroyable is Ownable {
function close() public onlyOwner {
selfdestruct(owner);
}
}
pragma solidity 0.5.12;
contract Ownable{
address payable public owner;
modifier onlyOwner() {
require(msg.sender == owner);
_; //Continue execution
}
constructor() public {
owner = msg.sender;
}
}
import â./Destroyable.solâ;
pragma solidity 0.5.12;
contract HelloWorld is Destroyable {
struct Person {
uint id;
string name;
uint age;
uint height;
bool senior;
}
uint public balance;
modifier onlyOwner() {
require(msg.sender == owner);
_; //Continue execution
}
modifier costs(uint cost) {
require(msg.value >= cost);
_;
}
mapping(address => Person) private people;
address[] private creators;
function createPerson(string memory name, uint age, uint height) public payable costs(1 ether) {
require(age <= 150, "Age must be below or 150");
require(msg.value >= 1 ether);
Person memory newPerson;
newPerson.name = name;
newPerson.age = age;
newPerson.height = height;
if (age >= 65) {
newPerson.senior = true;
} else {
newPerson.senior = false;
}
insertPerson(newPerson);
creators.push(msg.sender);
balance += msg.value;
assert(
keccak256(abi.encodePacked(
people[msg.sender].name,
people[msg.sender].age,
people[msg.sender].height,
people[msg.sender].senior))
==
keccak256(abi.encodePacked(
newPerson.name,
newPerson.age,
newPerson.height,
newPerson.senior)));
}
function insertPerson(Person memory newPerson) private {
address creator = msg.sender;
// address payable test = address(uint160(creator)); // how to cast an address to a payable address; the other way is straight forward
people[creator] = newPerson;
}
function getPerson() public view returns(string memory name, uint age, uint height, bool senior) {
address creator = msg.sender;
return (people[creator].name, people[creator].age, people[creator].height, people[creator].senior);
}
function deletePerson(address creator) public onlyOwner {
delete people[creator];
assert(people[creator].age == 0);
}
function getAddress(uint index) public view onlyOwner returns(address) {
return creators[index];
}
function withdrawAll() public onlyOwner returns(uint) {
uint toTransfer = balance;
balance = 0;
msg.sender.transfer(toTransfer); //REVERT
// if (msg.sender.send(toTransfer)) {
// return toTransfer;
// } else {
// balance = toTransfer;
// return 0;
// }
return toTransfer;
}
}
Destroyable.sol:
import "./Ownable.sol";
pragma solidity 0.5.12;
contract Destroyable is Ownable {
function close() public onlyOwner {
selfdestruct(address(uint160(owner)));
}
}
HelloWorld.sol:
import "./Destroyable.sol";
pragma solidity 0.5.12;
contract HelloWorld is Destroyable {
...
}
Destroyable Contract
import â./Ownable.solâ;
pragma solidity 0.5.12;
contract Destroyable is Ownable {
//use the is keyword to inherit the Ownable contract
function close() public onlyOwner {
selfdestruct(msg.sender);
}
}
HelloWorld Contract
import â./Ownable.solâ;
import â./Destroyable.solâ;
pragma solidity 0.5.12;
contract HelloWorld is Ownable, Destroyable{
_;
Created destroyable contract, which inherits from Ownable.
import "./Ownable.sol";
pragma solidity 0.5.12;
contract Destroyable is Ownable {
function destroy() public onlyOwner {
selfdestruct(msg.sender);
}
}
And then included the Destroyable in HelloWorld:
import "./Ownable.sol";
import "./Destroyable.sol";
pragma solidity 0.5.12;
contract HelloWorld is Ownable, Destroyable {
...
- I created a Destroyable.sol contract, inheriting from Ownable.sol
import "./Ownable.sol";
pragma solidity 0.5.12;
contract Destroyable is Ownable {
function close() public onlyOwner {
selfdestruct(owner);
}
}
- Helloworld.sol contract then inherits from Destroyable.sol (as well as Ownable.sol through multi-level inheritance)
import "./Destroyable.sol";
pragma solidity 0.5.12;
contract HelloWorld is Destroyable { // and Destroyable is Ownable :)
...
From my understanding, the contracts are not yet deployed on the blockchain so it possible to add the delete function(selfdestruct) to the Ownable Contract. Creating a .sol file for just one function seem unecessary. We can then make the function internal so it can be run from the HelloWorld contract.
pragma solidity 0.5.12;
contract Ownable(){
address public owner;
modifier onlyOwner(){
require(owner == msg.sender);
}
function close() internal onlyOwner{
selftdestruct(owner);
}
}