整版ubuntu上搭建私链,编写智能合约,实现智能合约交互

完整版ubuntu上搭建私链,编写智能合约,实现智能合约交互
 捣鼓了几天总算是把智能合约部署到自己的ubuntu上,并用node.js进行调用协议,每笔交易用自己的token,可以进行必要时间进行追溯,完整使用区块链,由于solidity更新太快,很多交易的代码都必须改一些才能进行编译,好了,不好这么多这个了。
  1、首先你需要拥有一台ubuntu服务器,我这里买的是阿里云的,配置问题,我提醒下,我买的8核16G内存的,挖矿还是比较牛的。
开始:
 

备选方案1 - 使用混编进行编译,然后复制粘贴进行部署

使用https://ethereum.github.io/browser-solidity中的混音

整版ubuntu上搭建私链,编写智能合约,实现智能合约交互_第1张图片

从Remix页面复制Web3部署说明:

var untitled_testContract = web3.eth.contract([{"constant":true,"inputs":[],"name":"value","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"inputs":[],"payable":false,"type":"constructor"}]);var untitled_test = untitled_testContract.new( { from: web3.eth.accounts[0],data: '0x60606040523415600b57fe5b5b607b6000819055505b5b608f806100246000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680633fa4f24514603a575bfe5b3415604157fe5b6047605d565b6040518082815260200191505060405180910390f35b600054815600a165627a7a72305820a4fac284b98d43538f802082b0db8c67ddd6d72df4e8d9fbccb4cec0e52ea0df0029',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 console解锁帐户中:

> personal.unlockAccount(eth.accounts[0], "{top secret password}");true> var untitled_testContract = web3.eth.contract([{"constant":true,"inputs":[],"name":"value","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"inputs":[],"payable":false,"type":"constructor"}]);"0xe22dc29e3e05d3206d9636ae7cba87a2827e7b3ef28c6e8e6d2ad49a14ba3ce9"var untitled_test = untitled_testContract.new( { from: web3.eth.accounts[0], data: '0x60606040523415600b57fe5b5b607b6000819055505b5b608f806100246000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680633fa4f24514603a575bfe5b3415604157fe5b6047605d565b6040518082815260200191505060405180910390f35b600054815600a165627a7a72305820a4fac284b98d43538f802082b0db8c67ddd6d72df4e8d9fbccb4cec0e52ea0df0029',gas: '4700000' }, function (e, contract){ console.log(e, contract); if (typeof contract.address !== 'undefined') {console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash); } })

方案2 - 使用Solidity编译器(Linux和OS / X,也许Windows使用Cygwin)

$ more Test.sol pragma solidity ^0.4.8;contract Test { uint256 public value; function Test() { value = 123; }}

以.json格式编译Test.sol,将数据分配给JavaScript变量并将输出发送到文件中:

$ echo "var testOutput=`solc --optimize --combined-json abi,bin,interface Test.sol`" > test.js$ cat test.jsvar testOutput={"contracts":{"Test.sol:Test":{"abi":"[{\"constant\":true,\"inputs\":[],\"name\":\"value\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"inputs\":[],\"payable\":false,\"type\":\"constructor\"}]","bin":"60606040523415600b57fe5b5b607b6000819055505b5b608f806100246000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680633fa4f24514603a575bfe5b3415604157fe5b6047605d565b6040518082815260200191505060405180910390f35b600054815600a165627a7a72305820d0e71d151634ac6ae7626860a17881104022e5cd6d3a088eb8f941d9aa8e3bd20029"}},"version":"0.4.9+commit.364da425.Darwin.appleclang"}

geth,加载test.js的内容:

$ geth console...> loadScript("test.js")true> testOutput{ contracts: { Test.sol:Test: { abi: "[{\"constant\":true,\"inputs\":[],\"name\":\"value\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"inputs\":[],\"payable\":false,\"type\":\"constructor\"}]", bin: "60606040523415600b57fe5b5b607b6000819055505b5b608f806100246000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680633fa4f24514603a575bfe5b3415604157fe5b6047605d565b6040518082815260200191505060405180910390f35b600054815600a165627a7a72305820d0e71d151634ac6ae7626860a17881104022e5cd6d3a088eb8f941d9aa8e3bd20029" }}, version: "0.4.9+commit.364da425.Darwin.appleclang"}> testOutput.contracts{ abi: "[{\"constant\":true,\"inputs\":[],\"name\":\"value\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"inputs\":[],\"payable\":false,\"type\":\"constructor\"}]",...> var testContract = web3.eth.contract(JSON.parse(testOutput.contracts["Test.sol:Test"].abi));undefined> personal.unlockAccount(eth.accounts[0], "{top secret password}");true> var test = testContract.new({ from: eth.accounts[0], data: "0x" + testOutput.contracts["Test.sol:Test"].bin, gas: 4700000}, function (e, contract) {console.log(e, contract); if (typeof contract.address !== 'undefined') { console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash); } });...


3、编译完成后,
与合约进行交互可以直接在命令符里直接命令操作:
充值

personal.unlockAccount(eth.accounts[0])
token.issue.sendTransaction(eth.accounts[0], 100, {from: eth.accounts[0]});
miner.start(1)
miner.stop()

发送 token

token.transfer(eth.accounts[1], 30, {from: eth.accounts[0]})
miner.start(1)
miner.stop()

查看余额

token.getBalance()

 如果是在node.js上使用web3调用的话,请看下面方法:
var  Web3  require ( 'web3' ) var  web3Admin require( 'web3admin')
var  web3 new  Web3()
setTimeout( function(){
web3Admin. extend( web3)
},  1000)
//web3Admin.extend(web3);//扩展web3模块
web3. setProvider( new  web3. providers. HttpProvider( "http://localhost:8545"));

//调用智能合约
var  abi = [
{
"constant"false,
"inputs": [
{
"name""account",
"type""address"
},
{
"name""amount",
"type""uint256"
}
],
"name""issue",
"outputs": [],
"payable"false,
"stateMutability""nonpayable",
"type""function"
},
{
"constant"false,
"inputs": [
{
"name""to",
"type""address"
},
{
"name""amount",
"type""uint256"
}
],
"name""transfer",
"outputs": [],
"payable"false,
"stateMutability""nonpayable",
"type""function"
},
{
"constant"true,
"inputs": [
{
"name""account",
"type""address"
}
],
"name""getBalance",
"outputs": [
{
"name""",
"type""uint256"
}
],
"payable"false,
"stateMutability""view",
"type""function"
},
{
"inputs": [],
"payable"false,
"stateMutability""nonpayable",
"type""constructor"
},
{
"anonymous"false,
"inputs": [
{
"indexed"false,
"name""account",
"type""address"
},
{
"indexed"false,
"name""amount",
"type""uint256"
}
],
"name""Issue",
"type""event"
},
{
"anonymous"false,
"inputs": [
{
"indexed"false,
"name""from",
"type""address"
},
{
"indexed"false,
"name""to",
"type""address"
},
{
"indexed"false,
"name""amount",
"type""uint256"
}
],
"name""Transfer",
"type""event"
}
];
var  aAddress "0x317b132862f95a8eaf44fb87059e1c5bd085e0ab";

var  web3. eth. contract( abi). at( aAddress);

//解锁
web3. personal.unlockAccount( web3. eth.coinbase,  "long3737257"15000);

//发送代币给自己
var  myMonery a.issue. sendTransaction( web3. eth.accounts[ 0],  10000, { fromweb3. eth.accounts[ 0]});

//查询余额
var  balancese a.getBalance( web3. eth.accounts[ 0]);
console. log( balancese);
//调用协议转账
var  sends a.transfer( eth.accounts[ 1],  30, { frometh.accounts[ 0]});

完成,你可以在本地上调用远程或者是本地的智能合约。是不是和简单,但是要做的话,如果是自己做的话会遇到很多问题,如果遇到问题可以跟我联系[email protected]

你可能感兴趣的:(整版ubuntu上搭建私链,编写智能合约,实现智能合约交互)