1.因?yàn)閟olc@0.5.1出現(xiàn)調(diào)用簡(jiǎn)單運(yùn)算合約出現(xiàn)返回0的問題,所以把solc降到了0.4.22。
2.安裝truffle后,npm install -g truffle,版本有所不同:
PS C:\> truffle version
Truffle v5.0.24 (core: 5.0.24)
Solidity v0.5.0 (solc-js)
Node v10.16.0
Web3.js v1.0.0-beta.37
3.建立簡(jiǎn)單合約Greeter.sol后,利用truffle compile后,出現(xiàn):
Error: CompileError: ParsedContract.sol:1:1: ParserError: Source file requires different compiler version (current compiler is 0.5.8+commit.23d335f2.Emscripten.clang - note that nightly builds are considered to be strictly less than the released version
pragma solidity ^0.4.24;
^----------------------^
Compilation failed. See above.
4.修改truffle-config.js文件:
module.exports = {
// Uncommenting the defaults below
// provides for an easier quick-start with Ganache.
// You can also follow this format for other networks;
// see <http://truffleframework.com/docs/advanced/configuration>
// for more details on how to specify configuration options!
/*
networks: {
development: {
host: "127.0.0.1",
port: 7545,
network_id: "*"
},
test: {
host: "127.0.0.1",
port: 7545,
network_id: "*"
}
}
*/
compilers: {
solc: {
version: "0.4.24"
}
}
};
5.再次編譯,出現(xiàn)
/C/users/administrator/webstormprojects/testtruffle/contracts/Migrations.sol:1:1: SyntaxError: Source file requires different compiler version (current compiler is 0.4.24+commit.e67f0147.Emscripten.clang - note that nightly builds are considered to be strictly less than the released version
pragma solidity >=0.4.25 <0.6.0;
^------------------------------^
6.打開Migrations.sol文件,
把pragma solidity >=0.4.25 <0.6.0;
修改為:pragma solidity >=0.4.24 <0.6.0;
編譯通過。
本文章的項(xiàng)目基于春哥的博客教程
【IPFS + 區(qū)塊鏈 系列】 入門篇 - IPFS + Ethereum (下篇)-ipfs + Ethereum 大圖片存儲(chǔ)
我個(gè)人只是作為記錄學(xué)習(xí)心得所借鑒
項(xiàng)目流程
首先調(diào)用代碼創(chuàng)建truffle項(xiàng)目
truffle unbox react
其次,要引入ipfs的api,用作圖片存儲(chǔ)的相關(guān)功能,我們是將圖片存儲(chǔ)到ipfs當(dāng)中,而將所獲得圖片的hash區(qū)塊鏈之中,區(qū)塊鏈大數(shù)據(jù)成本的問題
npm install –save ipfs-api
安裝完畢調(diào)用complie編譯合約代碼,,以便使用web3調(diào)用合約存儲(chǔ)區(qū)塊鏈
compile
替換合約地址,這個(gè)需要將合約在以太坊部署并取得對(duì)應(yīng)地址
然后運(yùn)行ipfs節(jié)點(diǎn)
ipfs daemon
啟動(dòng)項(xiàng)目
npm start
就可以看到項(xiàng)目成功
代碼解讀分析
import React, {Component} from 'react'
import SimpleStorageContract from '../build/contracts/SimpleStorage.json'
import getWeb3 from './utils/getWeb3'
import './css/oswald.css'
import './css/open-sans.css'
import './css/pure-min.css'
import './App.css'
const ipfsAPI = require('ipfs-api');
const ipfs = ipfsAPI({host: 'localhost', port: '5001', protocol: 'http'});
const contract = require('truffle-contract')
const simpleStorage = contract(SimpleStorageContract)
let account;
/** Declaring this for later so we can chain functions on SimpleStorage.**/
let contractInstance;
//ipfs保存圖片方法//
let saveImageOnIpfs = (reader) => {
return new Promise(function(resolve, reject) {
const buffer = Buffer.from(reader.result);
ipfs.add(buffer).then((response) => {
console.log(response)
resolve(response[0].hash);
}).catch((err) => {
console.error(err)
reject(err);
})
})
}
//創(chuàng)建構(gòu)造函數(shù),添加狀態(tài)機(jī)變量//
class App extends Component {
constructor(props) {
super(props)
this.state = {
blockChainHash: null,
web3: null,
address: null,
imgHash: null,
isWriteSuccess: false
}
}
//程序啟動(dòng)默認(rèn)調(diào)用方法//
componentWillMount() {
//打印項(xiàng)目中網(wǎng)絡(luò)節(jié)點(diǎn)//
ipfs.swarm.peers(function(err, res) {
if (err) {
console.error(err);
} else {
/** var numPeers = res.Peers === null ? 0 : res.Peers.length;**/
/** console.log("IPFS - connected to " + numPeers + " peers");**/
console.log(res);
}
});
//web3設(shè)置,同時(shí)調(diào)用初始化方法//
getWeb3.then(results => {
this.setState({web3: results.web3})
// Instantiate contract once web3 provided.
this.instantiateContract()
}).catch(() => {
console.log('Error finding web3.')
})
}
//初始化合約實(shí)例、web3獲取合約賬號(hào)以及合約實(shí)例//
instantiateContract = () => {
simpleStorage.setProvider(this.state.web3.currentProvider);
this.state.web3.eth.getAccounts((error, accounts) => {
account = accounts[0];
simpleStorage.at('0xf6a7e96860f05f21ecb4eb588fe8a8a83981af03').then((contract) => {
console.log(contract.address);
contractInstance = contract;
this.setState({address: contractInstance.address});
return;
});
})
}
render() {
return (<div className="App">
{
this.state.address
? <h1>合約地址:{this.state.address}</h1>
: <div/>
}
<h2>上傳圖片到IPFS:</h2>
/**這一部分用于上傳文件到ipfs**/
<div>
<label id="file">Choose file to upload</label>
<input type="file" ref="file" id="file" name="file" multiple="multiple"/>
</div>
<div>
<button onClick={() => {
var file = this.refs.file.files[0];
var reader = new FileReader();
// reader.readAsDataURL(file);
reader.readAsArrayBuffer(file)
reader.onloadend = function(e) {
console.log(reader);
saveImageOnIpfs(reader).then((hash) => {
console.log(hash);
this.setState({imgHash: hash})
});
}.bind(this);
}}>將圖片上傳到IPFS并返回圖片HASH</button>
</div>
/**這一部分用于上傳hash到區(qū)塊鏈**/
{
this.state.imgHash
? <div>
<h2>imgHash:{this.state.imgHash}</h2>
<button onClick={() => {
contractInstance.set(this.state.imgHash, {from: account}).then(() => {
console.log('圖片的hash已經(jīng)寫入到區(qū)塊鏈!');
this.setState({isWriteSuccess: true});
})
}}>將圖片hash寫到區(qū)塊鏈:contractInstance.set(imgHash)</button>
</div>
: <div/>
}
{
this.state.isWriteSuccess
? <div>
<h1>圖片的hash已經(jīng)寫入到區(qū)塊鏈!</h1>
<button onClick={() => {
contractInstance.get({from: account}).then((data) => {
console.log(data);
this.setState({blockChainHash: data});
})
}}>從區(qū)塊鏈讀取圖片hash:contractInstance.get()</button>
</div>
: <div/>
}
{
this.state.blockChainHash
? <div>
<h3>從區(qū)塊鏈讀取到的hash值:{this.state.blockChainHash}</h3>
</div>
: <div/>
}
{
this.state.blockChainHash
? <div>
<h2>瀏覽器訪問:{"http://localhost:8080/ipfs/" + this.state.imgHash}</h2>
<img alt="" style={{width:200}} src={"http://localhost:8080/ipfs/" + this.state.imgHash}/>
</div>
: <img alt=""/>
}
</div>);
}
}
export default App
該項(xiàng)目算是truffle和ipfs結(jié)合以太坊一起使用的綜合案例,用與梳理知識(shí)點(diǎn)
---------------------
作者:czZ__czZ
來源:CSDN
原文:https://blog.csdn.net/czZ__czZ/article/details/79036567
版權(quán)聲明:本文為博主原創(chuàng)文章,轉(zhuǎn)載請(qǐng)附上博文鏈接!
背景介紹
本文主要介紹如何使用Ganache,在本地搭建以太坊私有網(wǎng)絡(luò),并進(jìn)行簡(jiǎn)單的測(cè)試。
所需軟件
Ganache
Ganache用于搭建私有網(wǎng)絡(luò)。在開發(fā)和測(cè)試環(huán)境下,Ganache提供了非常簡(jiǎn)便的以太坊私有網(wǎng)絡(luò)搭建方法,通過可視化界面可以直觀地設(shè)置各種參數(shù)、瀏覽查看賬戶和交易等數(shù)據(jù)。
下載地址為:https://truffleframework.com/ganache/
MetaMask
MetaMask用于測(cè)試私有網(wǎng)絡(luò)。MetaMask是一個(gè)輕量級(jí)的以太坊錢包,由于它是一個(gè)Chrome插件,因此使用MetaMask可以非常方便地在瀏覽器中完成以太坊轉(zhuǎn)賬等操作。
下載地址為:https://www.metamask.io
操作步驟
安裝、啟動(dòng)Ganache
1. 使用安裝包安裝即可。
2. 打開程序后,會(huì)顯示以下界面,用戶可以查看賬戶(默認(rèn)創(chuàng)建10個(gè)賬戶)、區(qū)塊、交易和日志。
3. 點(diǎn)擊“設(shè)置”,如下圖所示,用戶還可以設(shè)置綁定的ip和端口(設(shè)置為8545即可,稍后MetaMask會(huì)用這個(gè)端口)、賬戶數(shù)量以及gas限制等,點(diǎn)擊“restart”后設(shè)置生效。
此時(shí),Ganache已經(jīng)在本機(jī)運(yùn)行了一個(gè)以太坊私有網(wǎng)絡(luò),并綁定了8545端口。
安裝、啟動(dòng)MetaMask
1. 把插件添加到chrome擴(kuò)展程序即可
2. 點(diǎn)擊Chrome中的MetaMask圖標(biāo),按照每一步提示啟動(dòng)MetaMask
3. 如下圖所示,設(shè)置MetaMask連接到本地的以太坊私有網(wǎng)絡(luò)
此時(shí),MetaMask就可以和本地的以太坊私有網(wǎng)絡(luò)進(jìn)行交互了。
用MetaMask測(cè)試私有網(wǎng)絡(luò)
1. 從Ganache創(chuàng)建的賬戶中選擇一個(gè)導(dǎo)入到MetaMask中
a. 在Ganache賬戶頁面選定一個(gè)賬戶,點(diǎn)擊最右邊的小鑰匙圖標(biāo),復(fù)制其私鑰(private key)
b. 在MetaMask中點(diǎn)擊頭像,選擇 “import account”,彈出對(duì)話框
c. 把復(fù)制的賬戶私鑰填入文本框中,并點(diǎn)擊“import”
此時(shí),MetaMask就可以操作這個(gè)新賬戶了。
2. 用新導(dǎo)入的賬戶進(jìn)行轉(zhuǎn)賬
a. 點(diǎn)擊“send”按鈕,彈出轉(zhuǎn)賬對(duì)話框
b. 從Ganache賬戶頁面中,再選定一個(gè)其他的賬戶,復(fù)制其地址
c. 把復(fù)制的地址填入到 “to” 文本框中,并在“amount”文本框中填入一個(gè)數(shù)值,表示要轉(zhuǎn)賬的金額(如 “10”);其它文本框默認(rèn)值即可
d. 點(diǎn)擊next,彈出轉(zhuǎn)賬確認(rèn)框,點(diǎn)擊“confirm”確認(rèn)交易
e. 提醒轉(zhuǎn)賬成功后,可以看到賬戶余額發(fā)生了變化,此時(shí)再轉(zhuǎn)到Ganache賬戶頁面,也可看到兩個(gè)賬戶的余額也都發(fā)生了變化。
注意
由于Ganache的交易數(shù)據(jù)是在內(nèi)存中操作的,并沒有持久化到本地硬盤中,因此每次Ganache重啟后,其上一次的交易記錄就沒有了,都是重新開始的。重啟Ganache后,再在MetaMask中轉(zhuǎn)賬就會(huì)發(fā)生錯(cuò)誤,解決辦法是在MetaMask設(shè)置中“restart account”,然后再操作就ok了。
如果想保留Ganache每一次運(yùn)行時(shí)的交易數(shù)據(jù),以便下一次繼續(xù)使用,可以使用命令行的形式ganache-cli啟動(dòng)Ganache,并指定數(shù)據(jù)存儲(chǔ)目錄
---------------------
作者:BigCuttie
來源:CSDN
原文:https://blog.csdn.net/starleelzx/article/details/82943530
版權(quán)聲明:本文為博主原創(chuàng)文章,轉(zhuǎn)載請(qǐng)附上博文鏈接!