- install dependencies
npm i -g ethereumjs-testrpc
npm i -g truffle
- init truffle project
truffle init
- write solidity
Helloworld.sol
contract
pragma solidity ^0.4.4;
contract Helloworld {
string _greeting;
function setGreeting(string greeting) {
_greeting = greeting;
}
function sayHello() returns(string) {
return _greeting;
}
}
- write
2_deploy_contracts.js
migration script
var Helloworld = artifacts.require("./Helloworld.sol");
module.exports = function(deployer) {
deployer.deploy(Helloworld);
};
- open up new terminal and run testrpc
testrpc
- build solidity smart contract
truffle build
- migrate
Helloworld
smart contract
truffle migrate
- test smart contract on truffle console
truffle console
/* console opens */
var hw;
Helloworld.deployed().then(function(d){hw = d;});
hw.sayHello.call() // ''
hw.setGreeting("hi github!");
hw.sayHello.call() // 'hi github!'
- change
sayHello
onHelloworld
contract
function sayHello() returns(string) {
return "im changed!";
}
- compile and migrate updated contract.. then run console
truffle compile
truffle migrate
/* deployed contract */
truffle console
- run sayHello
Notice sayHello output did not change. That is because contracts are cannot be changed once deployed to the blockchain.
var hw;
Helloworld.deployed().then(function(d){hw = d;});
hw.sayHello.call() // 'hi github!'