Solidity实现智能合约
Posted ZhangJiQun.
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Solidity实现智能合约相关的知识,希望对你有一定的参考价值。
目录
Object has no number say: hello.say()不能调用
出现Contract mined ! hello.say()
将生成代码在geth 客户端运行;
Remix网页版仅仅是我们使用Solidity语言编写的程序进行编译,生成对应的代码能在geth中运行而已。
合约代码:
pragma solidity ^0.4.18;
contract hello
string greeting;
function hello(string _greeting) public
greeting = _greeting;
function say() constant public returns (string)
return greeting;
生成代码:
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[1],
data: '0x6060604052341561000f57600080fd5b6040516102b83803806102b8833981016040528080518201919050508060009080519060200190610041929190610048565b50506100ed565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061008957805160ff19168380011785556100b7565b828001600101855582156100b7579182015b828111156100b657825182559160200191906001019061009b565b5b5090506100c491906100c8565b5090565b6100ea91905b808211156100e65760008160009055506001016100ce565b5090565b90565b6101bc806100fc6000396000f300606060405260043610610041576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063954ab4b214610046575b600080fd5b341561005157600080fd5b6100596100d4565b6040518080602001828103825283818151815260200191508051906020019080838360005b8381101561009957808201518184015260208101905061007e565b50505050905090810190601f1680156100c65780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6100dc61017c565b60008054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156101725780601f1061014757610100808354040283529160200191610172565b820191906000526020600020905b81548152906001019060200180831161015557829003601f168201915b5050505050905090565b6020604051908101604052806000815250905600a165627a7a723058204a5577bb3ad30e02f7a3bdd90eedcc682700d67fc8ed6604d38bb739c0655df90029',
gas: '4700000'
, function (e, contract)
console.log(e, contract);
if (typeof contract.address !== 'undefined')
console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
);
err="authentication needed: password or unlock"
Error: authentication needed: password or unlock undefined
解锁下用户:personal.unlockAccount(eth.accounts[1])
Object has no number say: hello.say()不能调用
这时候需要miner 去挖下矿(记账)
miner.start();admin.sleepBlocks(1);miner.stop()
出现Contract mined ! hello.say()
Solidity实现简单的智能合约
Remix网页版仅仅是我们使用Solidity语言编写的程序进行编译,生成对应的代码能在geth中运行而已。
合约代码:
pragma solidity ^0.4.18;
contract hello
string greeting;
function hello(string _greeting) public
greeting = _greeting;
function say() constant public returns (string)
return greeting;
生成代码:
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[1],
data: '0x6060604052341561000f57600080fd5b6040516102b83803806102b8833981016040528080518201919050508060009080519060200190610041929190610048565b50506100ed565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061008957805160ff19168380011785556100b7565b828001600101855582156100b7579182015b828111156100b657825182559160200191906001019061009b565b5b5090506100c491906100c8565b5090565b6100ea91905b808211156100e65760008160009055506001016100ce565b5090565b90565b6101bc806100fc6000396000f300606060405260043610610041576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063954ab4b214610046575b600080fd5b341561005157600080fd5b6100596100d4565b6040518080602001828103825283818151815260200191508051906020019080838360005b8381101561009957808201518184015260208101905061007e565b50505050905090810190601f1680156100c65780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6100dc61017c565b60008054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156101725780601f1061014757610100808354040283529160200191610172565b820191906000526020600020905b81548152906001019060200180831161015557829003601f168201915b5050505050905090565b6020604051908101604052806000815250905600a165627a7a723058204a5577bb3ad30e02f7a3bdd90eedcc682700d67fc8ed6604d38bb739c0655df90029',
gas: '4700000'
, function (e, contract)
console.log(e, contract);
if (typeof contract.address !== 'undefined')
console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
);
打开之后首页会是这样的。接下来我向大家演示一下如何实现helloworld
接下来我们先创建文件夹以及.sol文件
接下来开始编写一个简单的合约
编写完之后我们进行编译,部署
我们在println输入helloworld时,就会发现能够成功输出了。
以上是关于Solidity实现智能合约的主要内容,如果未能解决你的问题,请参考以下文章