Published 11 February 2019

Using of Credits API in JS (demo).

Requirements

Requirements

Any OS (For example, Windows 10 is used)
Download CREDITS API files:https://github.com/CREDITSCOM/CS-API

Create a folder and copy the JS files there. Create an empty html file, for example, index.html

Credits API source code generation

Credits API source code generation

Open the folder where you downloaded CREDITS API from the GitHub, for example, in d:\credits\github\CS-API\

For example, thrift is downloaded (https://thrift.apache.org/download) in the folder: d:\thrift\ Execute commands:
D:\thrift\thrift.exe --gen js api.thrift
D:\thrift\thrift.exe --gen js general.thrift

The result of the work will be a folder with files: gen-js.

Generated files:
API.js
api_types.js
general_types.js

Changes in index html

Changes in index.html

Adding CREDITS API Script Files:

Add connection to Thrift and request the balance

<title>CREDITS API JS Demo</title>

  <script>

      function run()

      {

          var url = "http://localhost:8081";

          var transport = new Thrift.Transport(url);

          console.log("debug step 1");

          var protocol = new Thrift.Protocol(transport);

          console.log("debug step 2");

          var client = new APIClient(protocol);

          console.log("debug step 3");

          var PublicKeyFrom = "5B3YXqDTcWQFGAqEJQJP3Bg1ZK8FFtHtgCiFLT5VAxpe";

          var BytePublicKeyFrom = from_b58(PublicKeyFrom);

          console.log("debug step 4");

          var obj = client.WalletBalanceGet(BytePublicKeyFrom);

          console.log("debug step 5");

          console.log(obj);

          console.log("debug step 6");

          document.getElementById('pc-id').innerText = PublicKeyFrom;

          document.getElementById('bl-id').innerText = 'Integral: ' + obj.balance.integral + ', Fraction: ' + obj.balance.fraction;

      }

      function ready() {

          run();

      }

      document.addEventListener("DOMContentLoaded", ready);

  </script>

Checking the efficiency. Calling the WalletBalanceGet method

The result of the work will be the requested balance:

page view:

View via developer tool:

GitHub link

Votes 0, average rating 0

Contents