Truffle合约交互 - WEB端对以太坊数据的读写
生活随笔
收集整理的這篇文章主要介紹了
Truffle合约交互 - WEB端对以太坊数据的读写
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1. 初始化truffle
truffle init webpack- 1
- 2
可以參考:here
2. 寫一個合約
這里給出一個簡單的合約。
pragma solidity ^0.4.2;contract Credit {event createRecord(address indexed _address, string identity, uint category, uint price);string a;uint b;uint c;function create(string identity, uint category, uint256 price){a = identity;b = category;c = price;createRecord(msg.sender, identity, category, price);}function all() returns (string, uint, uint){return(a, b, c);}}- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
3. 部署合約
前文有提到:here
4. 修改HTML文件
這里假設已經有了web基礎。
寫3個input, 分別是身份、分類以及價格。注意它們的id
以及2個按鈕:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
5. 修改app.js文件
注意引入的文件。
// Import the page's CSS. Webpack will know what to do with it. import "../stylesheets/app.css";// Import libraries we need. import { default as Web3} from 'web3'; import { default as contract } from 'truffle-contract'// Import our contract artifacts and turn them into usable abstractions. import credit_artifacts from '../../build/contracts/Credit.json'// MetaCoin is our usable abstraction, which we'll use through the code below. var Credit = contract(credit_artifacts);//展示與合約的互動 var accounts; var account;window.App = {start: function() {var self = this;// Bootstrap the Credit abstraction for Use.Credit.setProvider(web3.currentProvider);//獲取初始賬戶web3.eth.getAccounts(function(err, accs) {if (err != null) {alert("There was an error fetching your accounts.");return;}if (accs.length == 0) {alert("Couldn't get any accounts! Make sure your Ethereum client is configured correctly.");return;}accounts = accs;account = accounts[0];// self.refreshBalance();});},//設置頁面文字信息setStatus: function(message) {var status = document.getElementById("status");status.innerHTML = message;},//從以太坊獲取信息getMessage: function(){var self = this;var meta;var a,b,c;Credit.deployed().then(function(instance) {meta = instance;return meta.all.call({from: account});}).then(function(value){self.setStatus(value);}).catch(function(e) {console.log(e);self.setStatus("Error getting message. see log.");});},//寫數據到以太坊saveMessage: function(){var self = this;var meta;var identity = document.getElementById("identity").value;var category = parseInt(document.getElementById("category").value);var price = parseInt(document.getElementById("price").value);Credit.deployed().then(function(instance) {meta = instance;return meta.create(identity, category, price, {from: account});}).then(function(){self.setStatus(identity);}).catch(function(e){console.log(e);self.setStatus("Error");});} };//下面這一段應該是不用管照抄就行了。web頁面加載會調用。但實際上我還沒理解,待解決。 window.addEventListener('load', function() {if (typeof web3 !== 'undefined') {console.warn("Using web3 detected from external source. If you find that your accounts don't appear or you have 0 MetaCoin, ensure you've configured that source properly. If using MetaMask, see the following link. Feel free to delete this warning. :) http://truffleframework.com/tutorials/truffle-and-metamask")// Use Mist/MetaMask's providerwindow.web3 = new Web3(web3.currentProvider);} else {console.warn("No web3 detected. Falling back to http://localhost:8545. You should remove this fallback when you deploy live, as it's inherently insecure. Consider switching to Metamask for development. More info here: http://truffleframework.com/tutorials/truffle-and-metamask");// fallback - use your fallback strategy (local node / hosted node + in-dapp id mgmt / fail)window.web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));}App.start(); });- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
參考:here
記錄一下自己遇到的問題:
剛開始寫法:
event createRecord(string identity, uint category, uint price); 調用時:createRecord(identity, category, price);- 1
- 2
- 3
得到的identity不是輸入的字符串而是一串類似于下面這樣的數據
0x4f6054c80000000000000000000000000000000000000000- 1
- 2
解決:
event createRecord(address indexed _address, string identity, uint category, uint price);調用時:createRecord(msg.sender, identity, category, price);- 1
- 2
- 3
- 4
可以看到加了一個地址,這個事件是用來廣播的,第一個參數是用來部署合約的地址。
版權聲明:本文為博主原創文章,未經博主允許不得轉載。 http://blog.csdn.net/loy_184548/article/details/78041350總結
以上是生活随笔為你收集整理的Truffle合约交互 - WEB端对以太坊数据的读写的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: EVM反编译软件Porosity的使用-
- 下一篇: 使用web3.js进行开发