日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Truffle合约交互 - WEB端对以太坊数据的读写

發(fā)布時(shí)間:2025/3/15 编程问答 15 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Truffle合约交互 - WEB端对以太坊数据的读写 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1. 初始化truffle

truffle init webpack
  • 1
  • 2

可以參考:here

2. 寫一個(gè)合約

這里給出一個(gè)簡(jiǎn)單的合約。

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文件

這里假設(shè)已經(jīng)有了web基礎(chǔ)。

寫3個(gè)input, 分別是身份、分類以及價(jià)格。注意它們的id

以及2個(gè)按鈕:

  • 提交:會(huì)調(diào)用saveMessage函數(shù),把數(shù)據(jù)寫入以太坊
  • 獲取信息:調(diào)用getMessage函數(shù),從以太坊讀取數(shù)據(jù)
  • <!DOCTYPE html> <html> <head><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1"><title>MetaCoin - Truffle Webpack Demo w/ Frontend</title><link href='https://fonts.googleapis.com/css?family=Open+Sans:400,700' rel='stylesheet' type='text/css'><script src="./app.js"></script> </head> <body><h1>區(qū)塊鏈征信</h1><br><label for="identity">身份ID</label><input type="text" id="identity" ></input><br><label for="category">分類</label><input type="text" id="category" ></input><br><label for="price">價(jià)格</label><input type="text" id="price" ></input><br><br><button type = "submit" id="send" onclick="App.saveMessage()">提交</button><br><br><button type = "submit" id="get" onclick="App.getMessage()">獲取信息</button><br><label for="amount" id = "status" >信息</label></body> </html>
    • 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);//展示與合約的互動(dòng) 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();});},//設(shè)置頁面文字信息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.");});},//寫數(shù)據(jù)到以太坊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");});} };//下面這一段應(yīng)該是不用管照抄就行了。web頁面加載會(huì)調(diào)用。但實(shí)際上我還沒理解,待解決。 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); 調(diào)用時(shí):createRecord(identity, category, price);
    • 1
    • 2
    • 3

    得到的identity不是輸入的字符串而是一串類似于下面這樣的數(shù)據(jù)

    0x4f6054c80000000000000000000000000000000000000000
    • 1
    • 2

    解決:

    event createRecord(address indexed _address, string identity, uint category, uint price);調(diào)用時(shí):createRecord(msg.sender, identity, category, price);
    • 1
    • 2
    • 3
    • 4

    可以看到加了一個(gè)地址,這個(gè)事件是用來廣播的,第一個(gè)參數(shù)是用來部署合約的地址。

    版權(quán)聲明:本文為博主原創(chuàng)文章,未經(jīng)博主允許不得轉(zhuǎn)載。 http://blog.csdn.net/loy_184548/article/details/78041350

    總結(jié)

    以上是生活随笔為你收集整理的Truffle合约交互 - WEB端对以太坊数据的读写的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。