Contract-Design
智能合約函式呼叫不會在使用“Truffle”框架開發的 dapp 中執行
我正在使用“testrpc”和“truffle”開發一個 dapp。我還為 Oraclize 使用了 ethereum-bridge。我編寫了一個智能合約函式,它使用“OraclizeQuery”呼叫一個 url。我在 app.js 文件中添加了程式碼,但查詢不會被執行。我嘗試使用 truffle 控制台執行這些功能,效果很好。
以下是合約程式碼:-
pragma solidity ^0.4.0; import "./usingOraclize.sol"; contract WeatherApiCall is usingOraclize { string public weathercondition; function WeatherApiCall() { OAR = OraclizeAddrResolverI(0x3df0db5bda9d685e41cb4a8834c44f8028957417); } function __callback(bytes32 myid, string result) { if (msg.sender != oraclize_cbAddress()) throw; weathercondition=result; } function update(string to,string datetime) payable returns(bool sufficient) { oraclize_query("URL", strConcat("json(http://api.openweathermap.org/data/2.5/forecast?q='", to ,"'&mode=json&APPID=d2e8279188c8649c17540f798c9cc972).list[?(@.dt_txt='", datetime, "')].weather[0].main")); return true; } }
以下是 index.html 文件:-
<!DOCTYPE html> <html> <head> <title>Sample Truffle App</title> <link href='https://fonts.googleapis.com/css?family=Open+Sans:400,700' rel='stylesheet' type='text/css'> <link href="./app.css" rel='stylesheet' type='text/css'> <script src="./app.js"></script> </head> <body> <h1>Airline</h1> <h2>Example Truffle Dapp</h2> <br> <h1>FORM</h1> <br><label for="from">From:</label><input type="text" id="from" placeholder="--Residing Place--"></input> <br><label for="to">To:</label><input type="text" id="to" placeholder="--Travelling Place--"></input> <br><label for="persons">No. of Persons:</label><input type="text" id="persons" placeholder="--No. of Persons--"></input> <br><label for="date">Travel Date:</label><input type="text" id="date" placeholder="--Date of travel yyyy-MM-dd--"></input> <br><label for="time">Destination Time:</label><input type="text" id="time" placeholder="--HH:mm:ss--"></input> <br><br><button id="send" onclick="sendRequest()">Generate Quote</button> <br><br> </body> </html>
以下是對應智能合約程式碼的 app.js 文件:-
var accounts; var account; function sendRequest() { var weatherapicall = WeatherApiCall.deployed(); var fromdata = document.getElementById("from").value; var to = document.getElementById("to").value; var persons = parseInt(document.getElementById("persons").value); var date = document.getElementById("date").value; var time = document.getElementById("time").value; //verifying at javascript console console.log(fromdata); console.log(to); console.log(persons); console.log(date); console.log(time); var timeperiod; if ((time >= '00:00:00') && (time < '03:00:00')) { timeperiod = '00:00:00'; } else if ((time >= '03:00:00') && (time < '06:00:00')) { timeperiod = '03:00:00'; } else if ((time >= '06:00:00') && (time < '09:00:00')) { timeperiod = '06:00:00';[![enter image description here][1]][1] } else if ((time >= '09:00:00') && (time < '12:00:00')) { timeperiod = '09:00:00'; } else if ((time >= '12:00:00') && (time < '15:00:00')) { timeperiod = '12:00:00'; } else if ((time >= '15:00:00') && (time < '18:00:00')) { timeperiod = '15:00:00'; } else if ((time >= '18:00:00') && (time < '21:00:00')) { timeperiod = '18:00:00'; } else if ((time >= '21:00:00') && (time < '24:00:00')) { timeperiod = '21:00:00'; } datetime = date + ' ' + timeperiod; console.log(datetime); weatherapicall.update(to ,datetime, {from: account}).then(function(value) { console.log("Testing"); console.log(value); console.log(value.valueOf()); }).catch(function(e) { console.log(e); }); }; window.onload = function() { 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]; }); }
我無法與您的案例交談,但我認為我看到了一個缺失的步驟。
這條最重要的線:
weatherapicall.update(to ,datetime, {from: account}).then(function(value) {
您將獲得一個交易雜湊,表明交易已送出到區塊鏈。如果我沒記錯的話,這就是控制台日誌中出現的內容。它不包含交易的結果,因為在交易被探勘之前該結果是不可知的。
應該看起來大概是這樣的:
weatherapicall.update(to ,datetime, {from: account}).then(function(txn) { console.log("transaction submited", txn); return getTransactionReceiptMined(txn); // wait for mined result }) .then(function(receipt) { console.log("transaction mined", receipt); // explore the receipt to find your data, or return a call() to get it
我在這裡的 getTransactionReceiptMined() 運氣不錯:
https://gist.github.com/xavierlepretre/88682e871f4ad07be4534ae560692ee6
希望能幫助到你。