How to start development

Published 30 January 2019

Credits JavaScript Utils

Introduction

Introduction

The purpose of this article is to describe the implementation of a JavaScript class that uses Credits API with application functions that are understandable to the end user. Features will be added over time and at the request of users.

This code works with versions 3.4 and above.

Link to GitHub: https://github.com/pvl1175/CreditsUtils

Structure

Structure

Common files (API files, utilities)

API_3_0.js

api_types_3_0.js

general_types_3_0.js

nacl.js

Signature_3_0.js

thrift.js

The class itself that implements custom methods

Util.js

Implemented methods

Class builder.

constructor(publicKey, privateKey, url)

publicKey - public key of the wallet

privateKey - private key of the wallet (optional)

url - link to HTTP Credits API

 

Get wallet balance

Only the public key of the wallet is required

walletGetBalance()

 

Execute the specified smart contract method

Public key of the wallet is required

Private key of the wallet is required

executeSmartContractMethod(smartContract, method, result)

smartContract - smart-contract address

method - method of the smart-contract

result - result of the method execution

 

Deploying a smart-contract

Public key of the wallet is required

Private key of the wallet is required

deploySmartContract(smartContractCode)

smartContractCode - smart-contract code on Java

Code. GitHub

Code. GitHub

Example of use

      function csSamples() {
           var publicKey = "H5ptdUUfjJBGiK2X3gN2EzNYxituCUUnXv2tiMdQKP3b";
           var privateKey = "3Ki86Y3dy8enEgM1LXL97oQ6zLnhVbjJPpWAdqhgkAh7uFab37ergRWJxyDDsa46ra3UiQXqe2rW7JrJPkekBWMs";

           var smartContractAddress = "36eHSndcfHeUH33P3Rq85V3aNUPAjXuMonimA6kQZ9QS"; //"36eHSndcfHeUH33P3Rq85V3aNUPAjXuMonimA6kQZ9QS";
           var smartContractMethod = "getString";

           var cu = new CreditsUtils(publicKey, privateKey, "http://169.50.169.11:8081/thrift/service/Api");
           var balance = cu.walletGetBalance();
           document.getElementById('pc-id').innerText = publicKey;
           document.getElementById('bl-id').innerText = 'Integral: ' + balance.integral + ', Fraction: ' + balance.fraction;

           document.getElementById('sm-id').innerText = "SmartContract Address" + smartContractAddress + "/" + smartContractMethod;
           cu.executeSmartContractMethod(smartContractAddress, smartContractMethod, function(result) {
               document.getElementById('res-id').innerText = result;
           });

           // cu.deploySmartContract("");
       }

Result

Result

Votes 0, average rating 0

Contents