Skip to content

Instantly share code, notes, and snippets.

@phramz
Created September 20, 2017 08:38
Show Gist options
  • Select an option

  • Save phramz/3730cc79f8ab5f0e10aeb57fac425adf to your computer and use it in GitHub Desktop.

Select an option

Save phramz/3730cc79f8ab5f0e10aeb57fac425adf to your computer and use it in GitHub Desktop.
solutions.hamburg | web3 example | DO NOT USE IN PRODUCTION
web3.eth.accounts;
web3.personal.unlockAccount(web3.eth.accounts[0], "solutions.hamburg", 3600);
// abi def
var helloworldJsonDef = [{
"constant": true,
"inputs": [{"name": "a", "type": "uint256"}, {"name": "b", "type": "uint256"}],
"name": "add",
"outputs": [{"name": "result", "type": "uint256"}],
"payable": false,
"stateMutability": "view",
"type": "function"
}, {
"constant": false,
"inputs": [{"name": "_name", "type": "string"}],
"name": "hello",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
}, {"inputs": [], "payable": false, "stateMutability": "nonpayable", "type": "constructor"}, {
"anonymous": false,
"inputs": [{"indexed": true, "name": "from", "type": "address"}, {
"indexed": false,
"name": "name",
"type": "string"
}, {"indexed": false, "name": "message", "type": "string"}],
"name": "Greeting",
"type": "event"
}];
var helloworldContract = web3.eth.contract(helloworldJsonDef);
// deploy
var helloworldDeploy = helloworldContract.new(
{
from: web3.eth.accounts[0],
data: '0x6060604052341561000f57600080fd5b5b5b5b610197806100216000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063771602f714610049578063a777d0dc14610089575b600080fd5b341561005457600080fd5b61007360048080359060200190919080359060200190919050506100b7565b6040518082815260200191505060405180910390f35b341561009457600080fd5b6100b5600480803590602001908201803590602001919091929050506100c5565b005b600081830190505b92915050565b3373ffffffffffffffffffffffffffffffffffffffff167f5299067544afaba26734111f808ae9a1616812b98dfc63878cb027622728c6b8838360405180806020018060200183810383528585828181526020019250808284378201915050838103825260068152602001807f486921203a44000000000000000000000000000000000000000000000000000081525060200194505050505060405180910390a25b50505600a165627a7a7230582095b884933a0c5c556adcba8bfe0121641366ff0251a60e843ca0ad5cddb41fa00029',
gas: '4300000'
}, function (e, contract) {
console.log(e, contract);
if (typeof contract.address !== 'undefined') {
console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
}
}
);
var contractAddr = helloworldDeploy.address;
// try
var helloworld = web3.eth.contract(helloworldJsonDef).at(contractAddr);
var event = helloworld.Greeting({}, {fromBlock: 0, toBlock: 'latest'});
event.watch(function (error, result) {
if (!error) {
console.log("From " + result.address + " To " + result.args.name + "@" + result.args.from + ":");
console.log(result.args.message);
}
});
helloworld.hello("dude", {from: web3.eth.accounts[0]});
helloworld.add(1, 3, {from: web3.eth.accounts[0]});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment