Hello @filip can we map it like this:
Then
the function is equal on both sides, if I write it like this he will call the Destroyable.sol one right, like a mapping??
Hello @filip can we map it like this:
Then
the function is equal on both sides, if I write it like this he will call the Destroyable.sol one right, like a mapping??
hi guys!
import "./Ownable.sol"; // Ownable contract will validate the owner of the function executor
pragma solidity ^0.5.1;
contract Destroyable is Ownable {
function deleteContract() public onlyOwner {
selfdestruct(address(int160(owner))); // convert owner address to payable in order to received fund from destroyed contract
}
}
where Ownable.sol
as follows.
pragma solidity ^0.5.1;
contract Ownable {
address internal owner;
constructor() public {
owner = msg.sender;
}
modifier onlyOwner() {
require(msg.sender == owner, "You are not authorised to perform this operation!");
_;
}
}
Hey @Filipe_Venancio, hope you are ok.
Now there is no need to code the destroyCT
function in your mapping contract, when you inherit other contracts you will be able to use the public and external functions from the mapping contract.
If you have any more questions, please let us know so we can help you!
Carlos Z.
HelloWorld contract:
import “./Ownable.sol”;
import “./Destroyable.sol”;
pragma solidity 0.5.12;
contract HelloWorld is Ownable, Destroyable {
Destroyable.sol file:
import “./Ownable.sol”;
pragma solidity 0.5.12;
contract Destroyable is Ownable
{
function close() public onlyOwner
{
selfdestruct(msg.sender);
}
}
For the inheritance assignment I created a new Destroyable.sol file. Into this file I copied the Ownable code and then added a Destroyable contract as a child of the Ownable contract.
When I went to add a selfdestruct function passing owner as the argument I realized from the warning the owner address was not a payable address I therefore created a ownerPayable address so that this function would work.
The HelloWorld file was then modified to input the Destroyable.sol file and then the contract defined as “Contract HelloWorld is Destroyable”.
pragma solidity 0.5.12;
contract Ownable {
address owner;
modifier onlyOwner {
require(msg.sender == owner);
_;
}
constructor () public {
owner = msg.sender;
}
}
contract Destroyable is Ownable {
function close() public onlyOwner{
address payable ownerPayable = msg.sender;
selfdestruct(ownerPayable);
}
}
Hey @cryptophile, hope you are ok.
Now your solution works good, just to give you another point of view:
You can use convert a non-payable address to a payable address by using:
// convert owner address to payable in order to received fund from destroyed contract
selfdestruct(address(int160(owner)));
Also you can just modify your owner address to be a payable one by just adding payable
to its definition
contract Ownable {
address payable owner;
modifier onlyOwner {
require(msg.sender == owner);
_;
}
Although that your solution also works, you are missing to think also in terms of GAS fees, each line of code or action that your functions have, that will cost GAS at the end (the transaction). Just keep in mind to always try to code simply enough so the GAS cost of the transaction is not high for such simple operation. (but off course, code solid enough so you dont have weird bugs)
If you have any more questions, please let us know so we can help you!
Carlos Z.
import “./Ownable.sol”;
pragma solidity 0.5.12;
contract Destroyable is Ownable {
function destroy()public onlyOwner{
selfdestruct(msg.sender);
}
}
import “./Ownable.sol”;
import “./Destroyable.sol”;
pragma solidity 0.5.12;
contract HelloWorld is Ownable, Destroyable{
}
Destructable.sol:
import “./Ownable.sol”;
pragma solidity 0.5.12;
contract Destructable is Ownable{
function destroyContract() public onlyOwner {
address payable destroyer = address(uint160(owner));
selfdestruct(destroyer);
}
}
And then in HelloWorld.sol at first I did this:
import “./Ownable.sol”;
import “./Destructable.sol”;
pragma solidity 0.5.12;
contract HelloWorld is Ownable, Destructable{
…
}
But then realized that due to multiple inheritance this will be enough to make everything work:
import “./Destructable.sol”;
pragma solidity 0.5.12;
contract HelloWorld is Destructable{
…
}
My selfdestruct functionality implementation:
import "./Ownable.sol";
pragma solidity 0.5.12;
contract Destroyable is Ownable{
function destroy() public onlyOwner { //Only owner can destroy a contracts, inherits that functionality from Ownable.sol
selfdestruct(owner); //selfdestruct takes an address as an argument. When called, it sends all the current SC balance to the address provided and deletes the contract.
}
}
here is my contracts and works.
contract Destroyable
contract Struct( the same hello world)
// Ownable.sol
pragma solidity 0.5.12;
contract Ownable{
//address payable public owner ; // Approach/Method 1 ?
address public owner ; // owner = creator of contract
// Define Modifier(s)...
//-------------------------------------------------------
modifier onlyOwner(){
require(msg.sender == owner);
_;
}
// Contract Constructor
//-------------------------------------------------------
constructor() public{
owner = msg.sender;
}
// Public functions
//-------------------------------------------------------
function Destroy() public onlyOwner {
//selfdestruct(owner); // Approach/Method 1 ?
selfdestruct(address(uint160(owner)));
}
}
My solution would be:
import “./Ownable.sol”;
pragma solidity 0.5.12;
Contract Destroyable is Ownable{
function close() public onlyOwner {
selfdestruct(owner); // `owner` is the owners address
}
}
Hi @filip and everybody, thanks I enjoyed that and finding a use for the type cast to payable address was a nice easter egg
thank you
import “./Destroyable.sol”;
pragma solidity 0.5.12;
contract HelloWorld is Destroyable {
mapping(address => Person) people;
address public owner;
uint balance;
struct Person {
uint id;
string name;
uint age;
string height;
address walletAddress;
bool senior;
uint ethPayed;
}
event personCreated(string name, bool senior, uint ethPayed);
event personDeleted(string name, bool senior, address deletedBy);
address[] private creators;
modifier costs(uint cost){
require(msg.value >= cost, "message value needs to be meet cost");
balance += msg.value;
_;
}
function createPerson(string memory name, uint age, string memory height) public payable costs(1 ether) {
require(age <= 150, "Age needs to be below 150");
Person memory newPerson;
newPerson.id = creators.length;
newPerson.name = name;
newPerson.age = age;
newPerson.height = height;
newPerson.senior = isSenior(age);
newPerson.walletAddress = msg.sender;
newPerson.ethPayed = msg.value;
insertPerson(newPerson);
assert(
keccak256(
abi.encodePacked(
people[msg.sender].name,
people[msg.sender].age,
people[msg.sender].height,
people[msg.sender].senior,
people[msg.sender].ethPayed //<-- added funds to assertion
)
)
== keccak256(
abi.encodePacked(
newPerson.name,
newPerson.age,
newPerson.height,
newPerson.senior,
newPerson.ethPayed))
);
emit personCreated(newPerson.name, newPerson.senior, newPerson.ethPayed);
}
function insertPerson(Person memory newPerson) private {
creators.push(msg.sender);
people[msg.sender] = newPerson;
}
function getPerson() public view returns (string memory name, uint age, string memory height, bool isSenior){
return (people[msg.sender].name, people[msg.sender].age, people[msg.sender].height, people[msg.sender].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, msg.sender);
}
function isSenior(uint age) private pure returns (bool senior){
if (age > 65) {
return true;
} else {
return false;
}
}
function getCreator(uint index) public view onlyOwner returns (address creator) {
return creators[index];
}
function getContractBalance() public view returns (uint) {
return balance;
}
function withdrawAll() public onlyOwner returns(uint){
uint toTransfer = balance;
balance = 0;
msg.sender.transfer(toTransfer); //transfer reverts on error
return toTransfer;
}
}
pragma solidity 0.5.12;
import “./Ownable.sol”;
contract Destroyable is Ownable {
function destroy() public onlyOwner {
address payable destroyer = address(uint160(owner));
selfdestruct(destroyer);
}
}
pragma solidity 0.5.12;
contract Ownable {
address public owner;
modifier onlyOwner(){
require(msg.sender == owner, "Caller needs to be owner");
_;
}
constructor() public {
owner = msg.sender;
}
}
Updated solution so that Helloworld inherits both Ownable and Destroyable. That’s a little different from what im used to in Java
import “./Destroyable.sol”;
import “./Ownable.sol”;
pragma solidity 0.5.12;
contract HelloWorld is Ownable, Destroyable {
mapping(address => Person) people;
address public owner;
uint balance;
struct Person {
uint id;
string name;
uint age;
string height;
address walletAddress;
bool senior;
uint ethPayed;
}
event personCreated(string name, bool senior, uint ethPayed);
event personDeleted(string name, bool senior, address deletedBy);
address[] private creators;
modifier costs(uint cost){
require(msg.value >= cost, "message value needs to be meet cost");
balance += msg.value;
_;
}
function createPerson(string memory name, uint age, string memory height) public payable costs(1 ether) {
require(age <= 150, "Age needs to be below 150");
Person memory newPerson;
newPerson.id = creators.length;
newPerson.name = name;
newPerson.age = age;
newPerson.height = height;
newPerson.senior = isSenior(age);
newPerson.walletAddress = msg.sender;
newPerson.ethPayed = msg.value;
insertPerson(newPerson);
assert(
keccak256(
abi.encodePacked(
people[msg.sender].name,
people[msg.sender].age,
people[msg.sender].height,
people[msg.sender].senior,
people[msg.sender].ethPayed //<-- added funds to assertion
)
)
== keccak256(
abi.encodePacked(
newPerson.name,
newPerson.age,
newPerson.height,
newPerson.senior,
newPerson.ethPayed))
);
emit personCreated(newPerson.name, newPerson.senior, newPerson.ethPayed);
}
function insertPerson(Person memory newPerson) private {
creators.push(msg.sender);
people[msg.sender] = newPerson;
}
function getPerson() public view returns (string memory name, uint age, string memory height, bool isSenior){
return (people[msg.sender].name, people[msg.sender].age, people[msg.sender].height, people[msg.sender].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, msg.sender);
}
function isSenior(uint age) private pure returns (bool senior){
if (age > 65) {
return true;
} else {
return false;
}
}
function getCreator(uint index) public view onlyOwner returns (address) {
return creators[index];
}
function getContractBalance() public view returns (uint) {
return balance;
}
function withdrawAll() public onlyOwner returns(uint){
uint toTransfer = balance;
balance = 0;
msg.sender.transfer(toTransfer); //transfer reverts on error
return toTransfer;
}
}
Hi Guys!
I liked this assignment it was slightly challenging and mostly fun. I made a selfDestruct.sol with the following code:
pragma solidity 0.5.12;
import "./Ownable.sol";
contract Destroyable is Ownable {
function selfDestruct() internal onlyOwner {
address payable ownerPayable = address (uint160(owner));
selfdestruct(ownerPayable);
}
}
This then got imported in the helloWorld.sol and you can call it from there as a public function with onlyOwner modifier:
function deleteContract () public onlyOwner {
selfDestruct();
}
I tested it an it seems to be working. Please let me know if I messed up something or my solution is redundant.
Cheers!
import "./Ownable.sol";
pragma solidity 0.5.12;
contract Destroyable is Ownable{
function close() public onlyOwner payable{
selfdestruct(address(uint160(owner)));
}
}
Make the contract inherit from Destroyable
as follows:
contract HelloWorld is Ownable, Destroyable{
...
}
Here is my solution:
import “./Ownable.sol”;
pragma solidity 0.5.12;
contract Destroyable is Ownable{
address payable owner = msg.sender;
function destroy() public onlyOwner {
selfdestruct(owner);
}
}
pragma solidity 0.5.12;
contract Destroyable{
address public owner;
modifier onlyOwner(){
require(msg.sender == owner);
_;
}
constructor() public{
owner = msg.sender;
}
function close() public onlyOwner{
selfdestruct(msg.sender);
}
}
import “./Ownable.sol”;
pragma solidity 0.5.12;
contract Destroyable is Ownable{
function close() public onlyOwner { //onlyOwner is custom modifier
selfdestruct(address (uint160 (owner))); // `owner` is the owners address
}
}
For “Destroyable” part:
import “./OwnableC.sol”;
pragma solidity 0.5.12;
contract Destroyable is Ownable{
function close( ) public onlyOwner {
selfdestruct(msg.sender);
}
}
For “Hello World” part:
import “./OwnableC.sol”;
import “./DestroyableC.sol”;
pragma solidity 0.5.12;
contract CourtneyStructSC is Ownable, Detroyable{