Estoy desarrollando un dapp usando 'testrpc' y 'truffle'. También he usado ethereum-bridge para Oraclize. He escrito una función de contrato inteligente que llama a una URL usando 'OraclizeQuery'. He agregado el código en el archivo app.js , pero la consulta no se ejecutará. Intenté ejecutar las funciones usando la consola truffle y funcionó bien.
El siguiente es el código del contrato: -
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;
}
}
El siguiente es el archivo 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>
El siguiente es el archivo app.js para el código de contrato inteligente correspondiente: -
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];
});
}
Cargué el dapp y proporcioné las entradas e imprimí los valores correctamente en la consola de javascript. Pero la llamada a la función de actualización no se ejecutará.
No puedo hablar de su caso de uso, pero creo que veo un paso faltante.
Esta línea tan importante:
weatherapicall.update(to ,datetime, {from: account}).then(function(value) {
Obtendrá un hash de transacción que indica que la transacción se envió a la cadena de bloques. Si no me equivoco, esto es lo que aparece en el registro de la consola. No contiene el resultado de la transacción porque ese resultado no se puede conocer hasta que se extrae la transacción.
Debería verse aproximadamente así:
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
He tenido buena suerte con getTransactionReceiptMined() aquí:
https://gist.github.com/xavierlepretre/88682e871f4ad07be4534ae560692ee6
Espero eso ayude.
crissi mariam robert
Rob Hitchens
crissi mariam robert
crissi mariam robert
Rob Hitchens
crissi mariam robert