Published 4 July 2019

Examples: JS: Get Balance

Project:

https://github.com/CREDITSCOM/examples.git

Branch:

https://github.com/CREDITSCOM/examples/tree/master/API/CreditsJSDemo

 

In this example all interactions with Credits API will be implemented in the class CreditsUtils

The example describes how to get balance of a wallet using the Credits API 

Connection parameters, wallet keys and destination address are shared in the constructor

class CreditsUtils {

   constructor(publicKey, privateKey, targetKey, url) {

       this.publicKey = publicKey;

       this.privateKey = privateKey;

       this.targetKey = targetKey;

       this.url = url;

       this.publicKeyByte = from_b58(this.publicKey);

       this.privateKeyByte = from_b58(this.privateKey);

       this.targetKeyByte = from_b58(this.targetKey);

   }

   walletGetBalance() {

       var balance = this.client().WalletBalanceGet(this.publicKeyByte).balance;

       return balance;

   }

   client() {

       var transport = new Thrift.Transport(this.url);

       var protocol = new Thrift.Protocol(transport);

       return new APIClient(protocol);

   }

}

Usage

   <script>

       function run()

       {

           var pubkey = "your public key";

           var prvkey = "your private key";

           var trgKey = "your target key";

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

           var client = new CreditsUtils(pubkey, prvkey, trgKey, url);

           console.log(client.walletGetBalance());

       }

   </script>

Votes 0, average rating 0

Contents