Skip to content

Instantly share code, notes, and snippets.

@oneamitj
Last active February 26, 2020 16:42
Show Gist options
  • Save oneamitj/20a5dc3635dc1ae54205a3b17ff589f1 to your computer and use it in GitHub Desktop.
Save oneamitj/20a5dc3635dc1ae54205a3b17ff589f1 to your computer and use it in GitHub Desktop.
Demo Smart Contract for currency simulation
pragma solidity 0.4.11;
contract owned {
address public owner;
function owned() {
owner = msg.sender;
}
modifier onlyOwner {
if (msg.sender != owner) throw;
_;
}
function transferOwnership(address newOwner) onlyOwner {
owner = newOwner;
}
}
contract currencyRecipient {
function receiveApproval(address _from, uint256 _value, address _currency, bytes _extraData);
}
contract currency is owned {
/* Public variables of the currency */
string public name;
string public symbol;
uint8 public decimals;
uint256 public totalSupply;
uint public usd2nep = 104;
/* This creates an array with all balances */
mapping (address => uint256) public balanceOf;
mapping (address => uint256) public npr;
mapping (address => uint256) public usd;
mapping (address => mapping (address => uint256)) public allowance;
/* This generates a public event on the blockchain that will notify clients */
event Transfer(address indexed from, address indexed to, uint256 value);
/* set usd to nep rate */
function setRate(uint rate) onlyOwner {
usd2nep = rate;
}
/* Initializes contract with initial supply currency to the creator of the contract */
function currency(
uint256 initialSupply,
string currencyName,
uint8 decimalUnits,
string currencySymbol
) {
balanceOf[msg.sender] = initialSupply; // Give the creator all initial currency
npr[msg.sender] = initialSupply; // Give the creator all initial currency
usd[msg.sender] = initialSupply/usd2nep; // Give the creator all initial currency
totalSupply = initialSupply; // Update total supply
name = currencyName; // Set the name for display purposes
symbol = currencySymbol; // Set the symbol for display purposes
decimals = decimalUnits; // Amount of decimals for display purposes
}
/* Send coins */
function transfer(address _to, uint256 _value) {
if (balanceOf[msg.sender] < _value) throw; // Check if the sender has enough
if (balanceOf[_to] + _value < balanceOf[_to]) throw; // Check for overflows
balanceOf[msg.sender] -= _value; // Subtract from the sender
balanceOf[_to] += _value; // Add the same to the recipient
npr[msg.sender] = balanceOf[msg.sender];
npr[_to] = balanceOf[_to];
usd[msg.sender] = balanceOf[msg.sender]/usd2nep;
usd[_to] = balanceOf[_to]/usd2nep;
Transfer(msg.sender, _to, _value); // Notify anyone listening that this transfer took place
}
/* Allow another contract to spend some currency in your behalf */
function approve(address _spender, uint256 _value)
returns (bool success) {
allowance[msg.sender][_spender] = _value;
return true;
}
/* Approve and then communicate the approved contract in a single tx */
function approveAndCall(address _spender, uint256 _value, bytes _extraData)
returns (bool success) {
currencyRecipient spender = currencyRecipient(_spender);
if (approve(_spender, _value)) {
spender.receiveApproval(msg.sender, _value, this, _extraData);
return true;
}
}
/* A contract attempts to get the coins */
function transferFrom(address _from, address _to, uint256 _value) returns (bool success) {
if (balanceOf[_from] < _value) throw; // Check if the sender has enough
if (balanceOf[_to] + _value < balanceOf[_to]) throw; // Check for overflows
if (_value > allowance[_from][msg.sender]) throw; // Check allowance
balanceOf[_from] -= _value; // Subtract from the sender
balanceOf[_to] += _value; // Add the same to the recipient
allowance[_from][msg.sender] -= _value;
Transfer(_from, _to, _value);
return true;
}
/* This unnamed function is called whenever someone tries to send ether to it */
function () {
throw; // Prevents accidental sending of ether
}
}

Helful Bits & Bytes for Geth (Ethereum)

Installation

Mist & Wallet via Git

Geth CLI

Linux

  sudo apt-get install software-properties-common
  sudo add-apt-repository -y ppa:ethereum/ethereum
  sudo apt-get update
  sudo apt-get install ethereum

Mac

  brew tap ethereum/ethereum
  brew install ethereum

Genesis state BlockChain

  {
    "nonce": "0x0000000000000042",
    "timestamp": "0x00",
    "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
    "extraData": "0x00",
    "gasLimit": "0x8000000",
    "difficulty": "0x4000",
    "mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
    "coinbase": "0x3333333333333333333333333333333333333333",
    "alloc": {     },
    "config": {
      "chainId": 15,
      "homesteadBlock": 0,
      "eip155Block": 0,
      "eip158Block": 0
        
    }
  }

Initialize Private Chain

  geth --identity "ivoip" --rpc --rpcport "8080" --rpccorsdomain "*" --datadir "/home/one-1/ether/testchain" --port "9779" --nodiscover --rpcapi "db,eth,net,web3" --networkid 7037 init /home/one-1/ether/genesis.json      

Start Private Chain

  geth --fast --cache 512 --ipcpath /home/one-1/ether/testgeth.ipc --identity "ivoip" --rpc --rpcport "8080" --rpccorsdomain "*" --datadir "/home/one-1/ether/testchain" --port "9779" --rpcapi "db,eth,net,web3" --networkid 7037 console 2>> /dev/pts/4

  
  geth --fast --cache 512 --ipcpath /home/one-1/ether/testgeth.ipc --identity "ivoip" --rpc --rpcport "8080" --rpccorsdomain "*" --datadir "/home/one-1/ether/testchain" --port "9779" --nodiscover --rpcapi "db,eth,net,web3" --networkid 7037 console

Connecting new session to Private Chain

  geth attach ipc:/home/one-1/ether/testgeth.ipc

Connecting Mist to Private Chain

  mist --rpc testgeth.ipc

Useful commands

Accounts

  personal.newAccount("passwd")
  personal.listAccounts
  primary = personal.listAccounts[1]
  personal.unlockAccount(primary, "2")

Mining

  miner.setEtherbase(primary)
  miner.start()
  miner.stop()

Transaction

  tx = {from: primary, to: "0x59429d6106e98dc914622315e408b7aa4998d8f3", value: web3.toWei(1.23, "ether")}

  personal.sendTransaction(tx, "1")

  web3.fromWei(eth.getBalance(eth.coinbase), "ether")
  web3.fromWei(eth.getBalance(primary), "ether")

Connect to contract in Ethereum BlockChain

  var nep = eth.contract([{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"string"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"decimals","outputs":[{"name":"","type":"uint8"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"balanceOf","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"symbol","outputs":[{"name":"","type":"string"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transfer","outputs":[],"payable":false,"type":"function"},{"inputs":[{"name":"initialSupply","type":"uint256"},{"name":"tokenName","type":"string"},{"name":"decimalUnits","type":"uint8"},{"name":"tokenSymbol","type":"string"}],"payable":false,"type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"name":"from","type":"address"},{"indexed":true,"name":"to","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Transfer","type":"event"}]).at("0x5e52D089dA24Db3B9c4623AfE35734b06aa754FA");

Transaction via contract

  nep.balanceOf(primary)
  nep.transfer.sendTransaction( primary, 54, {from:personal.listAccounts[0]})

Loading JS functions from file

loadScript("/home/one-1/ether/chkbalance.js");
checkAllBalances();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment