Developing and programming on the hive blockchain and hive-engine with javascript/JQuery

in HK UNIVERSITY2 years ago (edited)

hiveLogo.png

hiveEngineLogo.png

As every experienced developer will know, blockchain programming is a concept that requires training and learning. You can know how to program in any language but have no idea how to do it in a certain blockchain, this mini experience will enter us and give us the necessary resources to program specifically in the HIVE and Hive-Engine blockchain under javascript/JQuery language.

We thank Hashkings for the initiation of the course through HK UNIVERSITY that has made this post possible.

TOKENS, NFTs and Transactions
These are the basic concepts with which we will have to deal

For the most experienced users here we will have the resources of the endpoints, methods and tables that we can call with Hive-Engine

Balances
When we refer to balances, we will talk about tokens, these are the number of tokens that a user or account of a specific asset has. For example: BUDS

There are several ways to make this query using directly the sscjs library or by making an ajax call with JQuery to the find/findOne function to the tokens contract and balances table. In this example we will use the sscjs library:

<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/sscjs@latest/dist/ssc.min.js"></script>
<script>
   const ssc = new SSC('https://api.hive-engine.com/rpc');
   // Obtemos los BUDs que tiene la cuenta de apzyx....
   var BUDSinStk = parseFloat(0);
   await ssc.findOne(
      'tokens',
      'balances',
      {
         account: 'apzyx',
         symbol: "BUDS"
      }, (err, result) => {

      console.log(err, result);            
      BUDSinStk = parseFloat(result.balance);
      alert(BUDSinStk);
});
</script>

In this query the findOne method is used so it will only retrieve one row, we can change the BUDS token for the HKWATER token or any other that is inside the Hive-Engine node,

Transactions
The transactions or transaction history of an account will require making several calls to the same method that returns a limit of 1000 or 500 transactions per call, we will use the offset attribute as a pagination.

<script>

   function sleep(ms)
   {
      var start = new Date().getTime(), expire = start + ms;
      while (new Date().getTime() < expire) { }
      return;
   }

   var startV = BigInt(0);
   var completarDatos = true;
   var arrayHKNVAULT = [];
   while (completarDatos) {
       await  $.get({
           url: "https://history.hive-engine.com/accountHistory",
           data: {
               account: accountV,
               symbol: "BUDS",
               offset: startV,
               limit: 500
           },
           async: false
       },
           function(data, status) {

               console.log(data);
               console.log(status);

               for (var i = 0; i < data.length; i++) {
                   arrayHKNVAULT.push(data[i]);

                   var dataS = JSON.stringify(data[i])
                   var dataJs = JSON.parse(dataS);

                   if (dataJs.symbol == 'BUDS') {
                       if (dataJs.operation == 'tokens_transfer') {
                           if (dataJs.to == 'null') {
                               completarDatos = false;
                           }
                       }
                   }
               }

               if (data.length < 500) {
                   completarDatos = false;
               }

                startV = startV + BigInt(500);
            });
        sleep(333);
    }
   console.log(arrayHKNVAULT);
</script>

These would be the parameters that this historical endpoint accepts:
GET Parameters
account: a HIVE account or multiple HIVE accounts separated by a comma (required)
symbol: a Hive-Engine token symbol (optional)
ops: a comma separated list of operations e.g. tokens_transfer (optional)
timestampStart: a unix timestamp that represents the start of the dataset (optional)
timestampEnd: a unix timestamp that represents the end of the dataset (optional)
limit: number of records for the dataset (max is 500) (optional)
offset: offset for the dataset (required to browse a dataset that is bigger than 500 records) (optional)

NFTs
For NFTs search method will be find, the contract will be nft and the table the id of the NFT + Instances, in our case HKFARMInstances

<script>
    
    async function nfts() {        
        $.ajaxSetup({ async: false });
        var offsetV = BigInt(0);
        var completarDatos = true;
        var usuariosUnicos = [];
        while (completarDatos) {
                        
            await $.ajax({
                url: "https://engine.rishipanthee.com/contracts",
                method: "POST",
                contentType: "application/json",
                async: false,
                data: '{"jsonrpc":"2.0", "method":"find", "params":{"contract":"nft", "table":"HKFARMinstances","query":{"properties.TYPE": "plot"},"offset":'+ offsetV + ', "limit":1000, "indexes":[]}, "id": 1}'
                ,

                success: function(response) {
                    console.log(response.result);
                    
                    if (response.result.length < 1000) {
                        completarDatos = false;
                    }
                    
                    offsetV = offsetV + BigInt(1000);
                },
                error: function(error) {
                    console.log(error);
                    alert("error");
                    completarDatos = false;
                }
            });
        }

    }

</script>

I hope that all this will serve as a guide or help to start, we will update the post as we learn new strategies and concepts within the HIVE and HIVE-ENGINE universe by the hand of HK UNIVERSITY ❤️

They can access my current project and fiddle in the direction
https://hashkings.azurewebsites.net