Bet on weather bot
Hello, everybody. Today we will learn how to create our first contract. We will do this on the example of a bot that accepts a bet on the weather.
We will need bot-example. Let’s install it.
git clone https://github.com/byteball/bot-example
cd bot-example
npm install
cp .env.testnet .envFirst, let’s create variables.
const request = require('request');
const correspondents = require('./correspondents');
let steps = {}; // store user steps
let assocDeviceAddressToCurrentCityAndTemp = {};
let assocDeviceAddressToAddress = {};
let bets = [];
let KEY_API = '50ace22603c84daba1780432180111'; // apixu.com
let my_address;Save our address for use in the contract
headlessWallet.readSingleWallet(walletId => {
db.query("SELECT address FROM my_addresses WHERE wallet=?", [walletId], function (rows) {
if (rows.length === 0)
throw Error("no addresses");
my_address = rows[0].address;
});
});Let’s add a text handler and analyze what happens:
Now let’s talk about contract creation. We need a separate function
Let’s analyze what happens arrSeenConditionPeer - watching if there was a payment in the contract with the amount same of the user. arrSeenConditionMyInput - сhecking, did we withdraw bytes? If yes, then allow the user to pick up their bytes.
arrDefinition - this is what our contract looks like, let’s look at it in details. When we need some address to subscribe we use [‘address’, address] When we need to check the publication data_feed we use [‘in data feed’, [[conf.oracle_address], name, operator, value]]
assocSignersByPath - in this object, we prescribe the path in the array of the contract before you need to sign and who should sign it. In our example, we sign all ‘address’. The path starts with r-this is the first element in the array, in our case ‘or’ construction. Other examples with signatures will be discussed in the following lessons.
walletDefinedByAddresses.createNewSharedAddress - we create a contract address headlessWallet.issueChangeAddressAndSendPayment - we add the address of the contract and make a payment to it
This is how we create a message that contains a contract and a payment request.
Create correspondents.js
We need to tell the oracle to check and publish the weather at the specified time, for this we use this feature.
We check whether there is an oracle in our correspondents and if it is not - we add.
That’s all. I also have a homework for you. Complete the oracle and our bot so that oracle informs us that he published and the publication has become stable. After oracle message, notify the user that he lost or won. And if he lost then take the money out of the contract. 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?