Ping-pong paymets
Last updated
Last updated
Hello. I’m starting a series of tutorials about creating projects on Obyte. Today we will consider the creation of ping-pong payment bot. First we will need Linux or MacOS, node.js, Obyte wallet for testnet and bot-example. So, let's clone it and run
We run cp .env.testnet .env
for changing it configuration to testnet network. When you starting bot at first, he will ask you passphrase(don’t forget it, since you need to enter each time you start). When starting bot in console you will see “my pairing code”. It looks like this: Copy this pairing code.
Now add our bot, open the Obyte wallet and go to Chat (below) > Add a new device > Accept invitation from the other device then insert your pairing code (instead of an asterisk any word, for example, test) and click "PAIR”
After that you will have a chat with your bot, write to him and he will answer you with your message. All right, now let's teach the bot to give us our money back.
Opening the start.js file we see 5 events:
headless_wallet_ready - Triggered when the bot is running and ready to work
paired - Triggered when someone adds our bot
text - Triggered when we receive a message
new_my_transactions - Triggered when we receive a transaction(but it is not stable)
my_transactions_became_stable - Triggered when the transaction has become stable
First, let's create variables where the associations for addresses will be stored. We will learn how to work with the database in the following lessons.
Now, lets teach the bot to send a request to send us your address:
Here we see the function sendMessageToDevice, we will use it whenever we want to send a message to the user.
The function takes 3 parameters
device address
subject - for now we will use only ‘text’
our text message
In order to send bot our address we need to go to our chat and click "Insert my address”:
Great, now we will teach our bot to check and save the user's address, and also create an address for accepting payments and request 5000 bytes from the user.
This time we used issueNextMainAddress, this function creates a new wallet address. We need it to distinguish between payments.
Now the most important thing! First, we need to inform the user that we have received the payment and waiting until it becomes stable.
After that, we send the user his payment minus the payment fee:
Here, we use sendAllBytesFromAddress, as the name implies, we just send all the funds(minus the payment fee) from one address to another.
That's it. Now let's test it. To do this you need to add faucet bot: AxBxXDnPOzE/AxLHmidAjwLPFtQ6dK3k70zM0yKVeDzC@byteball.org/bb-test#0000
And send him your address, wait until it becomes stable(this can be seen on the main screen), that's all. Send payments to the bot and get them back.
All code you can find on github. In the next lessons we will continue, but for now I recommend to read the official wiki. 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 chat in telegram - @obytedev.