在线编辑器:
http://remix.ethereum.org
编写第一个智能合约
在这个合约代码里面,我们定义了一个名为 "Hello" 的合约。在合约初始化时,保存了一个字符串。例如,传入 "hello world",每次调用 say() 函数时将返回该字符串
pragma solidity ^0.4.20;contract hello{ string greeting;
function hello(string _greeting) public{ greeting = _greeting; }
function say() constant public returns (string){ return greeting; }}
选择这个版本是 0.4.21+commit
点击那个 Details,来到 WEB3DEPLOY
把第一行的 var_greeting = /* var of type string her e*/;
改成 var_greeting = "hello world";
第 6 行的 from: web3.eth.account[0]
改为 from: web3.eth.account[2]
修改部署账户
就是下面这样:
var _greeting = "hello world";var helloContract = web3.eth.contract([{"constant":true,"inputs":[],"name":"say","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"inputs":[{"name":"_greeting","type":"string"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"}]);var hello = helloContract.new( _greeting, { from: web3.eth.accounts[2], data: '0x6060604052341561000f57600080fd5b6040516102a33803806102a3833981016040528080519091019050600081805161003d929160200190610044565b50506100df565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061008557805160ff19168380011785556100b2565b828001600101855582156100b2579182015b828111156100b2578251825591602001919060010190610097565b506100be9291506100c2565b5090565b6100dc91905b808211156100be57600081556001016100c8565b90565b6101b5806100ee6000396000f3006060604052600436106100405763ffffffff7c0100000000000000000000000000000000000000000000000000000000600035041663954ab4b28114610045575b600080fd5b341561005057600080fd5b6100586100cf565b60405160208082528190810183818151815260200191508051906020019080838360005b8381101561009457808201518382015260200161007c565b50505050905090810190601f1680156100c15780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6100d7610177565b60008054600181600116156101000203166002900480601f01602080910402602001604051908101604052809291908181526020018280546001816001161561010002031660029004801561016d5780601f106101425761010080835404028352916020019161016d565b820191906000526020600020905b81548152906001019060200180831161015057829003601f168201915b5050505050905090565b602060405190810160405260008152905600a165627a7a7230582016579f8b662064fd3fdbd4215682d7ebefe7998115729b69b443d5ad1572b2a70029', gas: '4700000' }, function (e, contract){ console.log(e, contract); if (typeof contract.address !== 'undefined') { console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash); } })
先看一下现在的账户
将上面这一段复制到 geth 的控制台(先开始挖矿⛏)
如果提示这样,是因为 u2 没有解锁账户
解锁
personal.unlockAccount(u2,'123456')
部署成功
看一下,u2 的账户余额变少了,部署合约用掉了
运行合约,调用 hello.say(),可以看到输出了 hello world