Weather oracle
Hi, everybody. Today we will create our own oracle. To do this, we need a bot-example. Let’s install it.
1
git clone https://github.com/byteball/bot-example
2
cd bot-example
3
npm install
4
cp .env.testnet .env
Copied!
Our oracle will accept a message like City: timestamp, where timestamp is the time to publish. Let’s write a message handler
1
headlessWallet.readSingleAddress(address => {
2
my_address = address;
3
console.error('my_address', address)
4
});
5
6
eventBus.on('text', (from_address, text) => {
7
text = text.trim();
8
const device = require('ocore/device.js');
9
const args = text.toLowerCase().split(':');
10
if (args.length === 2){
11
switch (args[0]) {
12
case 'berlin':
13
case 'moscow':
14
case 'helsinki':
15
case 'washington':
16
if(parseInt(args[1]) <= Date.now()){
17
console.error('dateNow', Date.now());
18
device.sendMessageToDevice(from_address, 'text', "Incorrect time");
19
}else {
20
arrQueue.push({city: args[0], time: args[1], device_address: from_address});
21
device.sendMessageToDevice(from_address, 'text', "ok");
22
}
23
break;
24
default:
25
device.sendMessageToDevice(from_address, 'text', "City not support");
26
break;
27
}
28
}else {
29
device.sendMessageToDevice(from_address, 'text', "Incorrect command");
30
}
31
});
Copied!
Add a variable to store the queue
1
let arrQueue = [];
Copied!
Now add to the queue
1
if(parseInt(args[1]) <= Date.now()){
2
device.sendMessageToDevice(from_address, 'text', "Incorrect time");
3
}else {
4
arrQueue.push({city: args[0], time: args[1], device_address: from_address});
5
device.sendMessageToDevice(from_address, 'text', "ok");
6
}
Copied!
Now we need to check the queue once a minute
1
function checkQueue(){
2
arrQueue.forEach((el, index) => {
3
if(el.time <= Date.now()){
4
let result = JSON.parse(body);
5
postDataFeed(el.city, el.time, result.current.temp_c);
6
}
7
});
8
}
9
setTimeout(checkQueue, 60000);
Copied!
Now we come to the main point. We need to publish data_feed
1
function postDataFeed(city, time, temp){
2
const network = require('ocore/network.js');
3
const composer = require('ocore/composer.js');
4
5
let data_feed = {};
6
data_feed[city + '_' + time] = temp;
7
let params = {
8
paying_addresses: [my_address],
9
outputs: [{address: my_address, amount: 0}],
10
signer: headlessWallet.signer,
11
callbacks: composer.getSavingCallbacks({
12
ifNotEnoughFunds: console.error,
13
ifError: console.error,
14
ifOk: function(objJoint){
15
network.broadcastJoint(objJoint);
16
}
17
})
18
};
19
let objMessage = {
20
app: "data_feed",
21
payload_location: "inline",
22
payload: data_feed
23
};
24
params.messages = [objMessage];
25
composer.composeJoint(params);
26
}
Copied!
What is happening?
1) We create an object in data_feed and keep the temperature in it.
1
let data_feed = {};
2
data_feed[city + '_' + time] = temp;
Copied!
2) We send the payment to ourselves as we only need to publish data_feed
1
paying_addresses: [my_address],
2
outputs: [{address: my_address, amount: 0}]
Copied!
3) Create a message with data and signature
1
let objMessage = {
2
app: "data_feed",
3
payload_location: "inline",
4
payload: data_feed
5
};
Copied!
4) Publish
1
composer.composeJoint(params);
Copied!
That’s all. Your first oracle is ready. For the experiment, you can add more cities or make paid access. All code is available on github.Did you like the lesson? Any questions? What topics have caused you difficulties and it is worth talking about them? Write in the comments and I will help you. Also join my telegram chat - @obytedev.
Last modified 1yr ago
Export as PDF
Copy link