Last active
March 21, 2017 16:01
-
-
Save bitgord/c812dd462a09841e3c0a14ca4f17b25b to your computer and use it in GitHub Desktop.
Introduction to Smart Contracts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Smart Contracts are a series of Ethereum Virtual Machine OP Codes | |
//Solidity it higher level and is compiled to EVM bytecode | |
// package.json | |
{ | |
"dependencies": { | |
"web3": "0.17.0-alpha", | |
"solc": "^0.4.4" | |
} | |
} | |
//install dependencies | |
npm install | |
//Add requirements and add new web3 instance | |
var Web3 = require("web3") | |
var web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545")) | |
var solc = require("solc") | |
//Make new .sol file for smart contract Introduction.sol | |
contract Introduction { | |
function displayMessage() constant returns (string) { | |
return "Congrats on your first smart contract"; | |
} | |
} | |
//Compile into bytecode in node console | |
var source = `[[contract syntax]]` | |
var compiled = solc.compile(source) | |
//Query contract | |
compiled.contracts.Introduction | |
//View bytecode | |
compiled.contracts.Introduction.bytecode | |
//View opcodes | |
compiled.contracts.Introduction.opcodes | |
//View ABI (public facing interface to show what methods are available to call) | |
compiled.contracts.Introduction.interface | |
//Set ABI variable | |
var abi = JSON.parse(compiled.contracts.Introduction.interface) | |
//Make contract object that is deployed to testrpc | |
var IntroductionContract = web3.eth.contract(abi) | |
//Make deployed variable | |
var deployed = IntroductionContract.new({ | |
from: web3.eth.accounts[0], | |
data: compiled.contracts.Introduction.bytecode, | |
gas: 4700000, | |
gasPrice: 5, | |
}, (error, contract) => { }) | |
//You can see in testrpc that it goes through; contract + tx address will be posted | |
web3.eth.getTransacton("[[transaction id]]") | |
//View contract address | |
deployed.address | |
//Call function on contract | |
deployed.displayMessage.call() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment