整版ubuntu上搭建私鏈,編寫智慧合約,實現智慧合約互動

NO IMAGE
1 Star2 Stars3 Stars4 Stars5 Stars 給文章打分!
Loading...
完整版ubuntu上搭建私鏈,編寫智慧合約,實現智慧合約互動
 搗鼓了幾天總算是把智慧合約部署到自己的ubuntu上,並用node.js進行呼叫協議,每筆交易用自己的token,可以進行必要時間進行追溯,完整使用區塊鏈,由於solidity更新太快,很多交易的程式碼都必須改一些才能進行編譯,好了,不好這麼多這個了。
  1、首先你需要擁有一臺ubuntu伺服器,我這裡買的是阿里雲的,配置問題,我提醒下,我買的8核16G記憶體的,挖礦還是比較牛的。
開始:
 

備選方案1 – 使用混編進行編譯,然後複製貼上進行部署

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

在這裡輸入影象描述

從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 a = web3.eth.contract(abi).at(aAddress);

//解鎖
web3.personal.unlockAccount(web3.eth.coinbase, “long3737257”, 15000);

//傳送代幣給自己
var myMonery = a.issue.sendTransaction(web3.eth.accounts[0], 10000, {from: web3.eth.accounts[0]});

//查詢餘額
var balancese = a.getBalance(web3.eth.accounts[0]);
console.log(balancese);
//呼叫協議轉賬
var sends = a.transfer(eth.accounts[1], 30, {from: eth.accounts[0]});

完成,你可以在本地上呼叫遠端或者是本地的智慧合約。是不是和簡單,但是要做的話,如果是自己做的話會遇到很多問題,如果遇到問題可以跟我聯絡[email protected]

程式語言 最新文章