Tag Archives: Solidity

Defining a Smart Contract Lifecycle in Solidity

address owner;
bool public paused;
constructor() public {
  owner = msg.sender;
}
function setPaused(bool _paused) public {
  require(msg.sender == owner, "you are not authorized for this action");
  pause = _paused;
}

First on DEPLOYMENT you want to be sure that the owner is the only who interacts with the contract for concrete actions:

  • Address of deployer is the owner
  • constructor(): initializes contract class
  • setPaused(): pause control of contract
  • require(): basic owner authorization
function withdrawAll(address payable _to) public {
  require(msg.sender == owner, "you are not authorized for this action");
  require(!paused, "Contract is paused!");
  _to.transfer(address(this).balance);
}

Second during the LIFECYCLE, you can make any actions on contract balance:

  • require(sender): checks auth
  • require(paused): checks if contract is suspended
  • address(this): gets address of this contract
  function destroy (address payable _to) public {
    require(msg.sender == owner, "you are not authorized for this action");
    selfdestruct(_to);
  }

Last, you can disable a contract by selfdestructing and sending balance to any address the owner wants.

Ethereum addresses in Solidity

ETH addresses are hexadecimal like: 0x123AFD… The same addresses can be owned by a wallet or by Smart contract deployment. So you can send and get balance of both.

Basic functions are:

  • address.balance: total weis in account (1 ETH == 10^18 weis).
  • address.transfer(amount): send an amount in weis, raising an exception if error.
  • address.send(amount): returns true if success instead of raising an exception.

Transfer/send costs are 2300 in gas.

Global objects in a contract are:

  • msg.sender: Sending address
  • msg.value: weis sent
  • now: current timestamp

Functions or addresses should be marked as PAYABLE to receive ETH.