GithubHelp home page GithubHelp logo

pinkdiamond1 / eth Goto Github PK

View Code? Open in Web Editor NEW

This project forked from cowpig/eth

0.0 1.0 0.0 27 KB

A sandbox for playing with Ethereum smart contracts using testrpc, web3, and solc to compile solidity

HTML 19.30% JavaScript 80.70%

eth's Introduction

eth

sandbox for playing with etherereum

in particular, the testrpc / web3 / solc flow

installation

# install latest version of node, since ubuntu thinks 4.2 is up-to-date
curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
sudo apt-get install -y nodejs npm

# make sure you're in the root directory and
npm install

run the GiveAway example in a node repl

First, you need to run testrpc.

I recommend opening opening two windows-side-by-side so that you can see changes to the blockchain in real-time.

./node_modules/.bin/testrpc

Now, check out the Solidity code in giveaway/giveaway.sol. That's the contract we're playing with here.

All it does is add people to a list, and if they're on the list, they're allowed to claim a free_lunch amount of ether from the contract by calling ClaimEther.

Now, let's open a node repl

node

And get started..

// to mess around in node:
Solc = require('solc')
Web3 = require('web3')
// web3 instance to connect to our local blockchain
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));

web3 will return a massive interface, and I'm only using a small part of it here. Let's get the contract loaded up:

// compile our contract
contract_file = 'giveaway/giveaway.sol'
contract_name = ':GiveAway'
source_code = fs.readFileSync(contract_file).toString()
compiledContract = Solc.compile(source_code)

The compiledContract is kind of gnarly, but the important parts are the abi and the bytecode

// a Contract's interface is defined by the "Application Binary Interface"
abi = compiledContract.contracts[contract_name].interface
GiveAwayContract =  web3.eth.contract(JSON.parse(abi))

GiveAwayContract is the contract's class, not an instance. To get it onto the blockchain, we need to deploy it by instantiating it.

// let's deploy this contract to our blockchain
bytecode = compiledContract.contracts[contract_name].bytecode;
admin_account = web3.eth.accounts[0]
contract_init_data = {
    data: bytecode,
    from: admin_account,
    gas: 1000000,
}
people = ['Laura', 'Gary'] // Laura and Gary are our starting participants
deployed_contract = GiveAwayContract.new(people, contract_init_data)

Now we should see stuff show up in the testrpc window:

eth_sendTransaction
eth_accounts
eth_sendTransaction

  Transaction: 0x58a1186c2f22d30b0a6f5893771aa2be63458c2f4adf68113d1c150fcd532105
  Contract created: 0xaa25b3b881542d115e5a619c80af1f7d3b0c1478
  Gas usage: 408047
  Block Number: 1
  Block Time: Wed Aug 02 2017 16:38:38 GMT-0400 (EDT)

eth_newBlockFilter
eth_getFilterChanges
eth_getTransactionReceipt
eth_getCode
eth_uninstallFilter

We can now add some ether to our GiveAway:

// and add some ether to it
load_up = {
    from: admin_account, 
    to: deployed_contract.address, 
    value: web3.toWei(10, 'ether'),
}
deployed_contract.AddEth.sendTransaction(load_up)

Which, since it is a transaction, should also show up in testrpc. Now we can play around with this and make sure it works. Check out the rest of giveaway.js for some examples of things you can do.

eth's People

Contributors

cowpig avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.