How to Create Ethereum Smart Contracts

in #ethereum7 years ago (edited)

This is how to create Ethereum Smart Contracts



mkdir my-eth-chain
cd my-eth-chain

gedit myGenesis.json


{
    "config": {
        "chainId": 15,
        "homesteadBlock": 0,
        "eip155Block": 0,
        "eip158Block": 0
    },
    "difficulty": "0x400",
    "gasLimit": "0x2100000",
    "alloc": {
        "7a69b359e86893efa3d9732e4c65ced51567edd0":
         { "balance": "0x1337000000000000000000" }     
    }
}


chainid: this provides a way to send transactions that work on  ethereum without working on ETC (ethereum classic) or the Morden  testnet. EIP 155 suggests following chainid values for different  networks: ethereum mainnet (1), morden /expanse mainnet (2), ropsten  (3), rinkeby (4), rootstock mainnet (30), rootstock testnet (31), kovan  (42), ethereum classic mainnet (61), ethereum classic testnet (62), geth  private chains (1337 by default). In our example we have used 15, which  is not used by any of these networks.

    homesteadBlock: the  value 0 indicates, it is using ethereum homestead release. Homestead is  the 2nd major ethereum release — and couple of days back on 16th Oct,  2017, ethereum did a hard fork to move to the byzantium release.

     eip155Block: the value 0 indicates, this block supports EIP (ethereum  improvement proposal)155. EIPs describe standards for the ethereum  platform, including core protocol specifications, client APIs, and  contract standards.

    eip158Block: the value 0 indicates, this block supports EIP (ethereum improvement proposal)158.

     difficulty: a value corresponding to the difficulty level applied  during the nonce discovery of this block. In this blog I explain how the  difficulty is calculated in ethereum, in detail.

    gasLimit:  gas is the internal pricing for running a transaction or contract in  ethereum. Each instruction sent to the Ethereum Virtual Machine (EVM) to  process a transaction or smart contract costs a specific amount of gas.  If the required amount of gas is not provided to the transaction, it  will fail before completion. When you do any ethereum transaction, you  specify a gas limit — that is the maximum gas all the operations  corresponding to that transaction can consume. The gasLimit parameter in  the block specifies, the aggregated gasLimit from all the transactions  included in the block.

    alloc: this allows to pre-allocate  ether to one or more accounts from the genesis block. In the above  genesis block, the pre-allocation is done to the account we created at  the begining.

geth --mine --rpc --networkid 1999 --datadir <path-to-data-directory>


(CHANGE THE 1999 VALUE)


networkid: network identifier of this ethereum network. You pick a  value you want. For example: olympic (0), frontier (1), morden (2),  ropsten(3).

    mine: enables mining.

    rpc: enables an HTTP-RPC server. Wallet applications can connect to this mining node over http.

    rpcaddr: specifies the HTTP-RPC server listening interface (default: “localhost”)

    rpcport: specifies the HTTP-RPC server listening port (default: 8545)

    rpcapi: specifies the API’s offered over the HTTP-RPC interface (default: “eth,net,web3”)

--rpcapi "web3,eth"

--rpccorsdomain "*"

geth --mine --rpc --networkid 1999 --datadir /path/to/data/dir console

geth --datadir /path/to/data/dir attach ipc:/path/to/data/dir /geth.ipc

> eth.accounts
["0x7a69b359e86893efa3d9732e4c65ced51567edd0"]

> eth.getBalance("0x7a69b359e86893efa3d9732e4c65ced51567edd0")
1.295e+21

Download MetaMask
https://metamask.io/

> personal.unlockAccount( "0x7a69b359e86893efa3d9732e4c65ced51567edd0","password")

> var sender = "0x7a69b359e86893efa3d9732e4c65ced51567edd0";
> var receiver = "0xA9f28458eE1170F285440990c196c1592D3a73f5"
> var amount = web3.toWei(1, "ether")

> eth.sendTransaction({from:sender, to:receiver, value: amount})

> eth.getBalance("0xA9f28458eE1170F285440990c196c1592D3a73f5")
1000000000000000000

https://remix.ethereum.org/

geth --mine --rpc --rpccorsdomain "*" --networkid 1999 --datadir <path-to-data-directory>

To  connect, remix to our private network, we need to change the  Environment to Web3 Provider, under the tab Run. When you do this  change, remix will prompt you to specify the Web3 Provider  Endpoint — set the value http://localhost:8545. Unless you have changed the port explicitly, the default mining node will start on the port 8545.


pragma solidity ^0.4.11;
contract Hello  {
      // a string variable
      string public greeting;

    // the function with the same name as the class is a constructor
     function Hello(string _greeting) {
         greeting = _greeting;
     }
 
     // change the greeting message
     function setGreeting(string _greeting) {
         greeting = _greeting;
     }
 
     // get the greeting message
     function greet() constant returns (string _greeting) {
        _greeting = greeting;
     }
 }


Click the Details button

Now we can deploy our smart contract  to our private blockchain. Under the Run tab, make sure you have the  right ethereum account selected, and then the right gas limit is  specified. You can keep gas price and value as zero.

> personal.unlockAccount( "0x7a69b359e86893efa3d9732e4c65ced51567edd0","password")

Click "Create"


Then you have the beginning of your First Smart Contract.




Guide to create a Crypto Kitties type Game

https://medium.com/loom-network/how-to-code-your-own-cryptokitties-style-game-on-ethereum-7c8ac86a4eb3

Sort:  

I like this post.. Carry on brother mar. I am new in #steemit please #follow me back.

nice post :)
done to follow and upvote :)

i upvoted ur post now back to my post

I want to send 5000 Tokens for 1 ETH and for that reason I added that line: "unitsOneEthCanBuy = 5000".

But my problem is i have complately forgot about decimals! My decimal is 18 so i think i should have write 50 instead of 5000.

Does anyone know how to fix it? i have already deployed, verified and published my smart contract.