现在我们来开始编写第一个智能合约代码,solidity 代码如下:
p`js
ragma solidity ^0.4.2;
contract hello {
string greeting;
function hello(string _greeting) public {
greeting = _greeting;
}
系搭建唯:wwqqyy420
function say() constant public returns (string) {
return greeting;
}
}
在编写完成代码,IDE 进行编译时会出现错误,注意系统-176搭建-0206+可电可微-5616要选择正确的编译器版本,如图:
简单解释下,我们定义了一个名为 hello 的合约,在合约初始化时保存了一个字符串(我们会传入 hello world),每次调用 say 返回字符串。
把这段代码写 (拷贝) 到 Browser-Solidity,如果没有错误,点击 Details 获取部署代码,如:
部署合约
Browser-Solidity 生成的代码,拷贝到编辑器里修改后的代码如下:
var _greeting = "Hello World";
var helloContract = web3.eth.contract([{"constant":true,"inputs":[],"name":"say","outputs":[{"name":"","type":"string"}],"payable":false,"type":"function","stateMutability":"view"},{"inputs":[{"name":"_greeting","type":"string"}],"type":"constructor","payable":true,"stateMutability":"payable"}]);
var hello = helloContract.new(
_greeting,
企业新闻