Ethereum Smart Contracts Transactions, and Use Cases

Bernard Lin
4 min readFeb 9, 2018

Ethereum Smart Contract & Use Cases

Smart Contract Transaction Summary

Account Concept

  • There is no difference between a contract account and externally owned account (EOA)
  • Contract account and EOA balance can be updated only through signed transactions
  • EOA can send money to contract account only if contract contractor is “payable” or there is a defined fallback “payable” function
  • The contact address is the last 160 bit hash of the sender address and its nonce (see example below)

Transaction Concept

  • A contract method call is translated into a transaction that needs to be signed by the calling EOA
  • Contract account is always in “to” except when it calls another contract. See transaction creation in the picture as follows.

(v, r, s) is the transaction signature.

Use Case Scenarios

Simple Endowment Contract

pragma solidity ^0.4.0;

contract EndowmentRetriever {
address creator;
address beneficiary;

event LogCoinsSent(address deliveredTo, uint amount);
event LogAddMoreFund (uint amount);

function EndowmentRetriever() public payable {
creator = msg.sender;
}

function addMorefund() external payable {

LogAddMoreFund(msg.value);
}

function getContractBalance() public constant returns(uint256 bal) {
return (this.balance);
}

function setBeneficiary (address to) external {
beneficiary = to;
}

function sendOneEtherHome() external {
creator.transfer(1000000000000000000);
}

function sendOneEtherToBeneficiary() external {
if (beneficiary != address(0)) {
beneficiary.transfer(1000000000000000000);
LogCoinsSent (beneficiary, 1000000000000000000);
}
}

/**********
Standard kill() function to recover funds
**********/
function kill() public {
if (msg.sender == creator) {
selfdestruct(creator);
}
}

}

Contract Transactions

Token Contract and Token Transfer

/**
* Set allowance for other address and notify
*
* Allows `_spender` to spend no more than `_value` tokens on your behalf, and then ping the contract about it
*
* @param _spender The address authorized to spend
* @param _value the max amount they can spend
* @param _extraData some extra information to send to the approved contract
*/
function approveAndCall(address _spender, uint256 _value, bytes _extraData)
public
returns (bool success) {
tokenRecipient spender = tokenRecipient(_spender);
if (approve(_spender, _value)) {
spender.receiveApproval(msg.sender, _value, this, _extraData);
return true;
}
}

/**
* Destroy tokens
*
* Remove `_value` tokens from the system irreversibly
*
* @param _value the amount of money to burn
*/
function burn(uint256 _value) public returns (bool success) {
require(balanceOf[msg.sender] >= _value); // Check if the sender has enough
balanceOf[msg.sender] -= _value; // Subtract from the sender
totalSupply -= _value; // Updates totalSupply
Burn(msg.sender, _value);
return true;
}

/**
* Destroy tokens from other account
*
* Remove `_value` tokens from the system irreversibly on behalf of `_from`.
*
* @param _from the address of the sender
* @param _value the amount of money to burn
*/
function burnFrom(address _from, uint256 _value) public returns (bool success) {
require(balanceOf[_from] >= _value); // Check if the targeted balance is enough
require(_value <= allowance[_from][msg.sender]); // Check allowance
balanceOf[_from] -= _value; // Subtract from the targeted balance
allowance[_from][msg.sender] -= _value; // Subtract from the sender’s allowance
totalSupply -= _value; // Update totalSupply
Burn(_from, _value);
return true;
}
}

Token Contract Transactions

Kraken Bitcoin Price Update Feed (Oraclize)

pragma solidity ^0.4.0;
import “github.com/oraclize/ethereum-api/oraclizeAPI.sol”;

contract KrakenPriceTicker is usingOraclize {

string public ETHXBT;

event newOraclizeQuery(string description);
event newKrakenPriceTicker(string price);

function KrakenPriceTicker() public {
oraclize_setProof(proofType_TLSNotary | proofStorage_IPFS);
update();
}

function __callback(bytes32 myid, string result, bytes proof) public {
if (msg.sender == oraclize_cbAddress()) {
ETHXBT = result;
newKrakenPriceTicker(ETHXBT);
update();
}
}

function update() payable public {
if (oraclize.getPrice(“URL”) > this.balance) {
newOraclizeQuery(“Oraclize query was NOT sent, \
please add some ETH to cover for the query fee”);
} else {
newOraclizeQuery(“Oraclize query was sent, standing by for the answer..”);

oraclize_query(60, “URL”,”json(https://api.kraken.com/0/public/Ticker?pair=ETHXBT)\.result.XETHXXBT.c.0");
}
}
}

Contract Transaction — External Feed

--

--