以太坊私有链智能合约创建及调用
不点
阅读:584
2021-04-01 11:15:15
评论:0
1.通过在线IDE并使用Solidity语言进行一个简单智能合约的编写与编译
https://chriseth.github.io/browser-solidity
pragma solidity ^0.4.18;
contract helloworld {
uint data;
function set(uint x) {
data = x;
}
function get() constant returns (uint) {
return data;
}}
2.选择版本
3.点击create复制web3 deploy
4.去除文件名,data添加0x前缀为16进制
var helloworldContract = web3.eth.contract([{"constant":false,"inputs":[{"name":"x","type":"uint256"}],"name":"set","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"get","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"}]);
var helloworld = helloworldContract.new(
{
from: web3.eth.accounts[0],
data: '0x6060604052341561000f57600080fd5b60bb8061001d6000396000f30060606040526004361060485763ffffffff7c010000000000000000000000000000000000000000000000000000000060003504166360fe47b18114604d5780636d4ce63c146062575b600080fd5b3415605757600080fd5b60606004356084565b005b3415606c57600080fd5b60726089565b60405190815260200160405180910390f35b600055565b600054905600a165627a7a72305820cfa9843a61b33ba0a349c61e62924c94da4038579955f3771976f71874a2a97b0029',
gas: 3000000
}, function(e, contract){
console.log(e, contract);
if (typeof contract.address != 'undefined') {
console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
}
})
7.粘贴到输入命令行
需要解锁账号再次执行
personal.unlockAccount("0x0bb81ec0920fed869f6b5f1bf46d4b923058af18","123456")//账号,密码
成功
8.将合约写入区块链,挖矿成功关闭挖矿
9.调用合约set方法,需要花费费用,从0x0bb81ec0920fed869f6b5f1bf46d4b923058af18账户出费用,账户必须解除锁定,开启挖矿成功后停止挖矿
10.调用合约get()方法
11.get()方法不花费费用是因为constant关键字
constant说明此方法不会改变任何值、任何状态,因此也不会有任何花费
声明
1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。