Instructions to build games on the Lightning network

Tram Ho

In this article, I will guide you to make a betting game on the Lightning network

Prepare :

Clone source code here

  • Polar (app to create test environment Installation instructions here )
  • Joule (Lightning wallet installation instructions here )
  • Reactjs
  • Nodejs

Environment setup:

I chose to use polar to create an environment with just a few clicks

First we will create a network consisting of 1 bitcoind node and 3 LND nodes

Then create a channel between the nodes.

So we have temporarily set up the environment to test already.

Building the app

Target

First, we still have to solve the problem of growing what to raise first. In this article I will guide how to make simple betting games. The rule is very simple, that is, the player chooses a range of values, for example, predicts the next random number is greater than 50 and the result is 78 , we will play. However, there will be a difference in the ratio we guess for example, as in the case above we guess greater than 75 , it will gain more money than we guess is greater than 50.

The app has 2 parts Backend and Frontend. The Back End of the game will be a node LND we assume that Alice node on the environment setup on Polar. As a player we can choose Bob or Carol here we choose Bob

Wallet

After downloading joule, we only need to enter 2 values admin.macaroon and readonly.macaroon Select Local node

Enter the REST Host value of the node we choose here we choose Bob

Drag and drop the admin.macaroon and readonly.macaroon files into it

That’s it then

Backend

My Backend uses Nodejs

The backend we will use is Alice node. First we create a .env file

We will fill in the parameters Polar provides for Alice Node

  • LND_URL: GRPC Host
  • LND_MACAROON_PATH: Admin Macaroon FIle path
  • LND_CERT_PATH: TLS Cert File path

Then create an RPC.js file to run commands that communicate with LND through RPC

Note: must download 1 file ./rpc.proto (download here ) of LND and put the same path vs file rpc.js

Then you can see the RPC api https://api.lightning.community/#lnd-grpc-api-reference example I wrote the function to send Payment

Then you can write APIs that run these functions

To solve the problem of trust in gambling games like this, the Bitcoin community has come up with a solution for a random number selection technique that Provably fair I also shared about it in this article .

Flow description

Frontend

Webln

To interact with Joule we use the Webln library to interact with the user.

The first is webln.sendPayment and webln.makeInvoice

  • webln.sendPayment : This function to show the user a popup screen to confirm the payment when starting to play.

  • webln.makeInvoice : this function will show the user a popup screen confirming the amount the player will receive when winning then create an invoice and send the token to the server

Although there are many parts that need elaborate, but basically this is a direction to develop Dapp on the Lightning network.

You can refer to my repo: https://github.com/vinhyenvodoi98/Lightning_maxbet

Share the news now

Source : Viblo