Using Ethereum With Truffle

Follow

In the course of interacting with Ethereum, you may find yourself wishing to do more than just exchanging currency.  Smart contracts for every kind of transaction and the privacy and efficiency they provide is poised to become the part of the blockchain movement with the biggest potential impact for world-wide change.  It can be complex, but that is rapidly changing and evolving as well.  

 

One of the ways to quickly create, test and manage contracts with Ethereum is by using Truffle

What is Truffle?

A development environment, testing framework and asset pipeline for blockchains using the Ethereum Virtual Machine (EVM).

With Truffle, you get:

  • Built-in smart contract compilation, linking, deployment and binary management.
  • Automated contract testing for rapid development.
  • Scriptable, extensible deployment & migrations framework.
  • Network management for deploying to any number of public & private networks.
  • Package management with EthPM & NPM, using the ERC190 standard.
  • Interactive console for direct contract communication.
  • Configurable build pipeline with support for tight integration.
  • External script runner that executes scripts within a Truffle environment.

It's designed to simplify and automate some of the more clunky aspects of developing and deploying contracts.  They have excellent documentation on every phase of the process, here.

Some of the processes covered:

  • Creating a Project
  • Compiling Contracts
  • Running Migrations
  • Interacting with Your Contract 
  • Debugging Your Contracts
  • Using Truffle Develop and The Console
  • Writing External Scripts
  • Using The Build Pipeline

 

TESTING 

  • Testing Your Contracts
  • Writing Tests in JavaScript
  • Writing Tests in Solidity

 

ADVANCED

  • Networks and App Deployment
  • Build Processes

 

You can then run and monitor your contracts right from your Mist browser, the same place you can manage your Ethereum wallet account(s).  Once you've launched Mist and connected to your node you can click on the <Contracts> button on the Mist application UI.

 

A Sample __Hello World__ greeter smart contract

Your first citizen: the Greeter

The Greeter is an intelligent digital entity that lives on the blockchain and is able to have conversations with anyone who interacts with it, based on its input.

Here is its code:

contract Mortal {
    /* Define variable owner of the type address */
    address owner;

    /* This function is executed at initialization and sets the owner of the contract */
    function Mortal() { owner = msg.sender; }

    /* Function to recover the funds on the contract */
    function kill() { if (msg.sender == owner) selfdestruct(owner); }
}

contract Greeter is Mortal {
    /* Define variable greeting of the type string */
    string greeting;

    /* This runs when the contract is executed */
    function Greeter(string _greeting) public {
        greeting = _greeting;
    }

    /* Main function */
    function greet() constant returns (string) {
        return greeting;
    }
}

You'll notice that there are two different contracts in this code: "mortal" and "greeter". This is because Solidity (the high level contract language we are using) has inheritance, meaning that one contract can inherit characteristics of another. This is very useful to simplify coding as common traits of contracts don't need to be rewritten every time, and all contracts can be written in smaller, more readable chunks. So by just declaring that greeter is mortal you inherited all characteristics from the "mortal" contract and kept the greeter code simple and easy to read.

The inherited characteristic "mortal" simply means that the greeter contract can be killed by its owner, to clean up the blockchain and recover funds locked into it when the contract is no longer needed. Contracts in ethereum are, by default, immortal and have no owner, meaning that once deployed the author has no special privileges anymore. Consider this before deploying.

USING REMIX

As of 2018, the most convenient way to develop contracts is using Remix, an online IDE.

To access the compiled code, ensure that the dropdown menu on the right pane has greeter selected. Then click on the Details button directly to the right of the dropdown. In the popup, scroll down and copy all the code in the WEB3DEPLOY textbox.

Create a temporary text file on your computer and paste that code. Make sure to change the first line to look like the following:

var _greeting = "Hello World!"

Now you can paste the resulting text on your geth window, or import the file with loadScript("yourFilename.js"). Wait up to thirty seconds and you'll see a message like this:

Contract mined! address: 0xdaa24d02bad7e9d6a80106db164bad9399a0423e

You may have to "unlock" the account that is sending the transaction using the password you picked in the beginning because you need to pay for the gas costs to deploying your contract: 

Within less than a minute, you should have a log with the contract address, this means you've successfully deployed your contract. You can verify the deployed code (which will be compiled) by using this command:

eth.getCode(greeter.address)

If it returns anything other than "0x" then congratulations! Your little Greeter is live! If the contract is created again (by performing another eth.sendTransaction), it will be published to a new address.

Run the Greeter

In order to call your bot, just type the following command in your terminal:

greeter.greet();

Since this call changes nothing on the blockchain, it returns instantly and without any gas cost. You should see it return your greeting:

'Hello World!'

 

 

 

0 out of 0 found this helpful

Comments

0 comments

Please sign in to leave a comment.