Weather oracle

Hi, everybody. Today we will create our own oracle. To do this, we need a bot-example. Let’s install it.

git clone https://github.com/byteball/bot-example
cd bot-example
npm install
cp .env.testnet .env

Our oracle will accept a message like City: timestamp, where timestamp is the time to publish. Let’s write a message handler

headlessWallet.readSingleAddress(address => {
  my_address = address;
  console.error('my_address', address)
});

eventBus.on('text', (from_address, text) => {
  text = text.trim();
  const device = require('ocore/device.js');
  const args = text.toLowerCase().split(':');
  if (args.length === 2){
     switch (args[0]) {
        case 'berlin':
        case 'moscow':
        case 'helsinki':
        case 'washington':
           if(parseInt(args[1]) <= Date.now()){
              console.error('dateNow', Date.now());
              device.sendMessageToDevice(from_address, 'text', "Incorrect time");
           }else {
              arrQueue.push({city: args[0], time: args[1], device_address: from_address});
              device.sendMessageToDevice(from_address, 'text', "ok");
           }
           break;
        default:
           device.sendMessageToDevice(from_address, 'text', "City not support");
           break;
     }
  }else {
     device.sendMessageToDevice(from_address, 'text', "Incorrect command");
  }
});

Add a variable to store the queue

Now add to the queue

Now we need to check the queue once a minute

Now we come to the main point. We need to publish data_feed

What is happening?

1) We create an object in data_feed and keep the temperature in it.

2) We send the payment to ourselves as we only need to publish data_feed

3) Create a message with data and signature

4) Publish

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 updated

Was this helpful?