Tag Archives: Ethereum

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.

Quick basic introduction to Ethereum Development

You need to install Metamask as an extension in your Chrome browser. After that, create a wallet address and change to Ropsten Test Network.

Metamask

Now go to buy ETH and get it from a Test faucet. Then you can play with it by creating another account and sending ETH between them.

Here the big thing is the TRANSACTION SIGNATURE, how knows the Network that you are really the owner of this transaction? Because of Asymmetric signature, you will sign the transaction with your Private Key, so the node knows you are, because it decrypt with your Public Key. Metamask do all of it for you, also saves your private key.

Now you have ETH to test your wallet, you will code with SOLIDITY.

  • It is a language based on JavaScript designed to target Ethereum Virtual Machine (EVM).
  • Statically typed, supports inheritance, libraries and complex user-defined types. And compiled in Bytecode.
  • You can create SMART CONTRACTS with it.
  • You can find whole guide to this language at https://docs.soliditylang.org/
  • You can use Remix as web editor in your browser, or Truffle installed locally.
  • In order to test your contracts you have several options:
    • Injected Web3: testing over Ropsten Network directly.
    • JavascriptVM: more agile, but it is a Network simulation.
    • Web3 Provider: which mounts an ETH Blockchain in your Laptop, from Genesis Block. Ganache, which belongs to TruffleSuite, can do it really clean.

Enjoy your learning!!