The javascript function is returning a promise and the promise is used in the solidity function which gives the error of invalid arguments

I am trying to make a Dapp which maintains the various versions of a trade(new versions are created when we update the data of trade). I am using javascript we3.js api to call my solidity functions through javascript. I have a function which displays the data when we search using a field called 'client'. When I am clicking the button to search on basis of client, it returns an error. I am using promises in javascript, but it seems to me that the javascript function is returning a promise which is undefined to solidity function. But after the error the javascript function is returning the expected values(I checked using console.log).

I am trying to make a Dapp which maintains the various versions of a trade(new versions are created when we update the data of trade). I am using javascript we3.js api to call my solidity functions through javascript. I have a function which displays the data when we search using a field called 'client'. When I am clicking the button to search on basis of client, it returns an error. I am using promises in javascript, but it seems to me that the javascript function is returning a promise which is undefined to solidity function. But after the error the javascript function is returning the expected values(I checked using console.log).

I have tried using async/await but that doesnt seems to do the trick.

My Javascript code:-

getVersion:async function(i){

var returnedLatestVersion;

App.contracts.Trades.deployed().then(async function(instance){

await instance.getLatestVersionOfTrade.call(i).then(function(a){      

  returnedLatestVersion = a;
  console.log("test:"+returnedLatestVersion+",a:"+a);

}).catch(function(err){
console.log(err)
})
console.log("test2="+returnedLatestVersion);
return await returnedLatestVersion;    

})
},

searchByClient: function(){

var tradesInstance;

var client = $('#clientSearch').val();

var tradeResults = $("#tradesResult");
tradeResults.empty();

App.contracts.Trades.deployed().then(function(instance){
tradesInstance = instance;

return tradesInstance.getClientData.call(client).then(async function(returnValues){

console.log("returnValues Array = "+returnValues);

var returnValuesLength = returnValues.length;   

for(var i=0;i<returnValuesLength;i++){     

  var a = returnValues[i];
  var c = returnValues[i];    
  var j;

  j = await App.getVersion(a);

  console.log("version ="+j);
  console.log("tradeId="+a);

  tradesInstance.trades(c,j).then(function(trade){

    var secId = trade[0];
    var notional = trade [1];
    var price = trade[2];
    var client = trade[3];

    var tradeTemplate = "<tr><td>" + secId + "</td><td>" + notional + "</td><td>" + price + "</td><td>" + client +"</td></tr>" 
    tradeResults.append(tradeTemplate);
  })
}

}).catch(function(err){
console.log(err);
})
}).catch(function(err){
console.warn(err);
})

}

};

My Smart Contract :-

pragma solidity 0.5.0;

contract Trades {
struct Trade {
string secId;
uint notional;
uint price;
string client;
uint version;
uint index;
uint tradeId;
}

mapping(uint =>mapping(uint => Trade)) public trades;

uint public tradeId;
uint[] tradeIndex;
uint[] tradeID;
uint[] public totalNum;

function updateTrade(uint _tradeId, string memory _secId,uint _notional,uint _price,string memory _client) public{

uint j;
j= (getLatestVersionOfTrade(_tradeId)) + 1;
trades[_tradeId][j].secId = _secId;
trades[_tradeId][j].notional = _notional;
trades[_tradeId][j].price = _price;
trades[_tradeId][j].client = _client;
trades[_tradeId][j].version = j;
trades[_tradeId][j].index = tradeIndex.push(tradeIndex[tradeIndex.length-1]+1);

}

function setTrade(string memory _secId,uint _notional,uint _price,string memory _client) public {

uint  version  = 0;
tradeId++;

trades[tradeId][version].secId = _secId;
trades[tradeId][version].notional = _notional;
trades[tradeId][version].price = _price;
trades[tradeId][version].client = _client;
trades[tradeId][version].version = version;
trades[tradeId][version].index = tradeIndex.push(tradeId);
tradeID.push(tradeId);

}

function getAllTradeData()view public returns(uint[] memory){
return tradeIndex;
}

function getAllTradeDataId()view public returns(uint[] memory){
return tradeID;
}

function getTradeById(uint _tradeId,uint version)view public returns(string memory, uint, uint, string memory, uint, uint){
return (trades[_tradeId][version].secId, trades[_tradeId][version].notional, trades[_tradeId][version].price,
trades[_tradeId][version].client,trades[_tradeId][version].version, trades[_tradeId][version].index);
}

function getLatestVersionOfTrade(uint _tradeId) view public returns (uint) {
uint max = 0;

for (uint i = 0; i < tradeIndex.length; i++) {
    uint ver = trades[_tradeId][i].version;
    if (ver > max) {
        max = ver;
    }
}

return max;
}

function getClientData(string memory _client) public returns (uint[] memory) {

if (totalNum.length > 0){
    delete totalNum;
}
    for(uint i=1; i <= tradeID.length;i++){
    uint j;
    j= (getLatestVersionOfTrade(i));
    if(uint(keccak256(abi.encodePacked(trades[i][j].client))) == uint(keccak256(abi.encodePacked(_client)))){
        totalNum.push(i);
    }             
}  
return totalNum;

}

function getTotalNumLength() public returns (uint){
return totalNum.length;
}
}

The error shown on brower console : -

returnValues Array = 3,4,5
app.js:113 version =undefined
app.js:114 tradeId=3
app.js:113 version =undefined
app.js:114 tradeId=4
app.js:113 version =undefined
app.js:114 tradeId=5

3inpage.js:1 Uncaught (in promise) Error: Invalid number of arguments to
Solidity function
at Object.InvalidNumberOfSolidityArgs (inpage.js:1)
at u.validateArgs (inpage.js:1)
at u.toPayload (inpage.js:1)
at u.call (inpage.js:1)
at u.execute (inpage.js:1)
at truffle-contract.js:136
at new Promise (<anonymous>)
at truffle-contract.js:127

app.js:77 test:0,a:0
app.js:82 test2=0
app.js:77 test:0,a:0
app.js:82 test2=0
app.js:77 test:1,a:1
app.js:82 test2=1

The variable 'j' in searchByClient is returning an undefined value when it is passed in 'trades(c,j)'. But in the function getVersion the value returning seems to be fine. Please help me out as I have no other options left. Also I am extremly new to javascript, so please do tell me if I had make any mistakes. I was able to write this code with help of a tutorial.

How to Build a Blockchain in JavaScript

How to Build a Blockchain in JavaScript

For the purpose of this blog, let’s build a blockchain written in JavaScript. Hopefully, this doesn’t just serve as a simple code example but also gives a very basic understanding of how a blockchain actually works.

Blockchain is all the buzz now. And for good reason! A distributed public ledger that is extremely secure through encryption?! Imagine the millions of applicable use cases. A blockchain can be created and maintained using any modern language.

For the purpose of this blog, let’s dive into a blockchain written in JavaScript. Hopefully, this doesn’t just serve as a simple code example but also gives a very basic understanding of how a blockchain actually works.

Creating A Block

Let’s first create a block and give it some attributes.

  • BlockIndex – the position of the block on the blockchain.
  • BlockCreatedWhen – when the block was created.
  • BlockTransaction – a type of data to be stored on the block. This could be any exchange of value really. Perhaps an array of sorts.
  • BlockPreviousHash – hash of the previous block. This ties one block to another.
  • Hash – calculated hash function using the properties of the block. This is the block identifier on the blockchain. There is currently no built-in functionality for hash functions outside of the WebCryptoAPI. But there are plenty of other external libraries such as object-hash, crypto-hs, SJCL, forge SHA-256, etc.
  • Nonce – Proof of work
  • Merkle Root – A Merkle Root hash that houses all hashed transactions tied to a particular block.
const hash = require('crypto-js/sha256'); // could also use object-hash, Stanford Javascript Crypto Library (SJCL), forge SHA-256, jsSHA, etc
 
class VincentsBlock{
         
    constructor(blockIndex, blockCreatedWhen, blockTransactions, blockPreviousHash) {
        this.blockIndex = blockIndex;
        this.blockCreatedWhen = blockCreatedWhen;
        this.blockTransactions = blockTransactions;  // an array of transactions
        this.blockPreviousHash = blockPreviousHash;
        this.hash = this.calculateBlockHash();
        this.nonce = 0;  // needed for proof of work
        this.merkleRoot = this.computeMerkleRoot()  // used to validate transactions within block
    } 
 
    calculateBlockHash() { 
        return hash(this.blockIndex + JSON.stringify(this.blockTransaction) + this.blockPreviousHash + this.blockCreatedWhen + this.nonce + this.merkleTreeRoot).toString();
    }
     
    computeMerkleRoot() {
        let treeList = this.generateMerkleTreeRoot();
         
        return treeList[treeList.length-1];
    }
 
    generateMerkleTreeRoot() {
        let tree = [];
        let transactionCount = this.blockTransactions.length;
 
        for (var i=0; i<transactionCount; i++) { tree.push(this.blockTransactions[i].hash); } let levelOffset = 0; for (let levelSize = transactionCount; levelSize > 1; levelSize = (levelSize + 1) / 2) {          
            for (let left = 0; left < levelSize; left += 2) {            
                let right = Math.min(left + 1, levelSize - 1);
                let tleft = tree[levelOffset + left];
                let tright = tree[levelOffset + right];
                tree.push(hash(tleft + tright));
            }            
            levelOffset += levelSize;
        }
 
        return tree;
    }
}
 
module.exports = VincentsBlock;

Notice the Merkle Root property on the block. Given that a transaction has its own hash generated, we can create a Merkle Tree out of the transaction list. This tree can then be used to validate the block transactions. Now that a block is generated, let’s go back and look at a transaction object. This should illustrate that a transaction can be anything really. This is where the many applications of blockchain come into play.

These particular transactions consist of a

  • transactionId (id of transaction),
  • inputAccount (sender),
  • outputAccount (receiver),
  • fee (perhaps in BTC or any other currency),
  • the amount (processed amount),
  • type,
  • misc (notes)
  • timestamp (timestamp of transaction)
  • hash (hash of transaction)

Creating a Block Transaction Object

I created a block transaction object as follows:

const hash = require('crypto-js/sha256');
 
class VincentsTransaction{
         
    constructor(transactionId, inputAccount, outputAccount, fee, amount, type, misc, timestamp) {
        this.transactionId = transactionId,   // could be hex address
        this.inputAccount = inputAccount;    // sender
        this.outputAccount = outputAccount;   // receiver
        this.fee = fee;    // fee of transaction
        this.amount = amount;    // amount of transaction processed
        this.type = type;      // type of transaction
        this.misc = misc;     // misc note text
        this.timestamp = timestamp    // timestamp of block
        this.hash = this.calculateTransactionHash();  // hash of the transaction
    } 
 
    calculateTransactionHash() { 
        return hash(this.transactionId + this.inputAccount + this.outputAccount + this.fee + this.amount + this.type + this.misc + this.timestamp).toString();
    }
}
 
module.exports = VincentsTransaction;
The Chain

Moving on to the good part, let’s take a look at a basic immutable blockchain using the above blocks.

Below, in the constructor, you will notice a manual creation of the first block. This is needed to get the ball rolling. Every chain needs a first link!

Let’s manually create it and give it an index of 0 to show its placement on the chain. Many functions can be added to this class to assist on tasks such as adding blocks, checking for an empty chain, grabbing the latest block, validation, etc.

The process of creating additional blocks can be a complex one involving tons of validation if needed. We must keep in mind to regenerate the hash if any other property changes on a block.

In regards to validation of the blockchain, for now, we will focus on blocks linked together. Since index 0 corresponds to the manually created first block, we can skip it and check the hash of corresponding linked blocks.

let VincentsBlock = require('./VincentsBlock');
 
class VincentsBlockChain {
     
    constructor() {
        this.chain = [this.createBrandNewBlock()];
 
        //this.current_transactions = [];
    }
 
    createBrandNewBlock() {
        return new VincentsBlock(0, "01/01/2019", "Initial Block", ""); //no hash needed here since this is a manual creation of the first block.
    }
 
    createAdditionalBlock(newBlock) {
        ///very simplistic... could have more validation in place here.  
        let previousBlock = this.getLatestBlock();
        newBlock.index = previousBlock.index + 1; 
        newBlock.blockPreviousHash = this.getLatestBlock().hash;
        newBlock.hash = newBlock.calculateBlockHash(); //need to regenerate hash if any properties in our block changes
        this.chain.push(newBlock);
    }
 
    getLatestBlock() {
        return this.chain.slice(-1)[0];        
    }
 
    isEmpty() {
        return this.chain.length == 0;
    } 
 
    isBlockChainValid() { 
        for(let i = 1;i < this.chain.length; i++) {
            const currentBlock = this.chain[i];
            const previousBlock = this.chain[i - 1];
 
            if (currentBlock.blockPreviousHash != previousBlock.hash) { // does our block match up to correct previous hash property? 
                return false;
            }            
 
            if (currentBlock.hash != currentBlock.calculateBlockHash()) { // does the actual hash of the block match up to it's current hash property?
                return false;
            }     
              
            if (previousBlock.index + 1 !== currentBlock.index) { // do the indexes line up in order?
                return false;
            }  
        }
 
        return true;
    }
};
 
module.exports = VincentsBlockChain;
Adding to the Chain

For this simplistic example, let’s create 10 blocks, each with multiple transactions, and add them to the chain.

let VincentsBlockChain = require( "./blockchain/VincentsBlockChain");
let VincentsBlock = require( "./blockchain/VincentsBlock");
let VincentsTransaction = require( "./blockchain/VincentsTransaction");
 
let blockChain = new VincentsBlockChain();
blockChain.createAdditionalBlock(new VincentsBlock(1, "01/02/2019", [new VincentsTransaction(123, "abcxyz", "xyzabc", 30, "btc"), new VincentsTransaction(999, "uyyuyy", "xxdzkj", 550, "btc"), new VincentsTransaction(653, "drerea", "kioolk", 70, "eth")]));
blockChain.createAdditionalBlock(new VincentsBlock(2, "01/03/2019", [new VincentsTransaction(234, "sfgfsd", "rrttyu", 50, "eth"), new VincentsTransaction(555, "iuiuii", "rrttyu", 50, "eth"), new VincentsTransaction(553, "hghghg", "ffgfff", 80, "eth")]));
blockChain.createAdditionalBlock(new VincentsBlock(3, "01/04/2019", [new VincentsTransaction(345, "srthhf", "oouyuu", 65, "btc"), new VincentsTransaction(452, "jhghgg", "rrttyu", 11, "eth"), new VincentsTransaction(230, "wewfgf", "ghjgtt", 230, "eth")]));
blockChain.createAdditionalBlock(new VincentsBlock(4, "01/05/2019", [new VincentsTransaction(456, "sfgfss", "xzsdee", 4, "btc"), new VincentsTransaction(780, "fgtfdd", "rrttyu", 22, "eth"), new VincentsTransaction(112, "qwewue", "hgyuyu", 280, "eth")]));
blockChain.createAdditionalBlock(new VincentsBlock(5, "01/06/2019", [new VincentsTransaction(567, "xxcbbf", "nbvbcx", 545, "eth"), new VincentsTransaction(677, "fdfsaa", "uytttt", 33, "eth"), new VincentsTransaction(111, "hjhjh", "huiuii", 5480, "eth")]));
blockChain.createAdditionalBlock(new VincentsBlock(6, "01/07/2019", [new VincentsTransaction(678, "uiyuyy", "dfgree", 5, "btc"), new VincentsTransaction(553, "ooouio", "yuyuuu", 55, "eth"), new VincentsTransaction(467, "eeeewq", "jgftft", 880, "eth")]));
blockChain.createAdditionalBlock(new VincentsBlock(7, "01/08/2019", [new VincentsTransaction(789, "iughff", "opoiii", 125, "btc"), new VincentsTransaction(988, "aqQQQQ", "trewew", 01, "eth"), new VincentsTransaction(432, "eeeee", "khuuuu", 40, "eth")]));
blockChain.createAdditionalBlock(new VincentsBlock(8, "01/09/2019", [new VincentsTransaction(890, "sdsrer", "sdfser", 99, "eth"), new VincentsTransaction(886, "gfgfdg", "qqqqqq", 88, "eth"), new VincentsTransaction(444, "gfgfd", "llllkl", 70, "eth")]));
blockChain.createAdditionalBlock(new VincentsBlock(9, "01/10/2019", [new VincentsTransaction(901, "mnghjy", "dfgxbg", 65, "btc"), new VincentsTransaction(644, "dddsds", "wwwwww", 55, "eth"), new VincentsTransaction(333, "sdsaqq", "ererew", 50, "eth")]));
blockChain.createAdditionalBlock(new VincentsBlock(10, "01/11/2019", [new VincentsTransaction(012, "vccvff", "uiyfvv", 74, "btc"), new VincentsTransaction(994, "mnmnmm", "qqqqhh", 66, "eth"), new VincentsTransaction(323, "qwqwqq", "wreret", 30, "eth")]));
 
console.log(JSON.stringify(blockChain, null, 2)); // let's view entire blockchain
 
console.log('Validate entire blockchain: ' + blockChain.isBlockChainValid()); // let's validate the blockchain is secure
 
blockChain.chain[5].hash = blockChain.chain[5].calculateBlockHash();  // let's try to recalculate hash for block 5
 
console.log('Is blockchain valid? ' + blockChain.isBlockChainValid()); 

Alright, let’s run this bad boy and see the blockchain we created.

{
  "chain": [
    {
      "blockIndex": 0,
      "blockCreatedWhen": "01/01/2019",
      "blockTransactions": "Initial Block",
      "blockPreviousHash": "",
      "hash": "46d4d81e6047cccb6e856a4bc800ed344fbe4b78df5f5a7fe5862e8773cd022a",
      "nonce": 0,
      "merkleTreeRoot": {
        "words": [
          -474954686,
          -1728308204,
          -1694763832,
          -1720731356,
          665731556,
          1687917388,
          -1533699813,
          2018687061
        ],
        "sigBytes": 32
      }
    },
    {
      "blockIndex": 1,
      "blockCreatedWhen": "01/02/2019",
      "blockTransactions": [
        {
          "transactionId": 123,
          "inputAccount": "abcxyz",
          "outputAccount": "xyzabc",
          "fee": 30,
          "amount": "btc",
          "hash": "324cc491e4436b69299a905847124308c6dfc272642d9dbb3808bd79048bc110"
        },
        {
          "transactionId": 999,
          "inputAccount": "uyyuyy",
          "outputAccount": "xxdzkj",
          "fee": 550,
          "amount": "btc",
          "hash": "3beef24348997907eaa0263d3eb1393b1e74cb365e730649c60a910ecbbee0d5"
        },
        {
          "transactionId": 653,
          "inputAccount": "drerea",
          "outputAccount": "kioolk",
          "fee": 70,
          "amount": "eth",
          "hash": "a30e34266150f7b616aa4ebe88461406308280c9821425dbe0bfbc1fa7e6843a"
        }
      ],
      "blockPreviousHash": "46d4d81e6047cccb6e856a4bc800ed344fbe4b78df5f5a7fe5862e8773cd022a",
      "hash": "54ee7657f991b1341ecfa7d2ecfe7cf0501751f147ecc7b9ca08b2354068eeab",
      "nonce": 0,
      "merkleTreeRoot": {
        "words": [
          -474954686,
          -1728308204,
          -1694763832,
          -1720731356,
          665731556,
          1687917388,
          -1533699813,
          2018687061
        ],
        "sigBytes": 32
      },
      "index": null
    },
    {
      "blockIndex": 2,
      "blockCreatedWhen": "01/03/2019",
      "blockTransactions": [
        {
          "transactionId": 234,
          "inputAccount": "sfgfsd",
          "outputAccount": "rrttyu",
          "fee": 50,
          "amount": "eth",
          "hash": "f2378fced4de261dc5c531441d867b385f5726b76eb76d6ece8b739de3f6d2f7"
        },
        {
          "transactionId": 555,
          "inputAccount": "iuiuii",
          "outputAccount": "rrttyu",
          "fee": 50,
          "amount": "eth",
          "hash": "cb42ae591f275f7d265f749489c0830ef985252d256ad176430a9acee0b7c525"
        },
        {
          "transactionId": 553,
          "inputAccount": "hghghg",
          "outputAccount": "ffgfff",
          "fee": 80,
          "amount": "eth",
          "hash": "edb71da69f72d01fdefe7939665d8fc59489bb16e5f3d7e662a187b1d437ef8f"
        }
      ],
      "blockPreviousHash": "54ee7657f991b1341ecfa7d2ecfe7cf0501751f147ecc7b9ca08b2354068eeab",
      "hash": "f58b63c0fbe00f69b6b1bd04560ffdd72ebe7b71e70a16224bf45aad01308e0f",
      "nonce": 0,
      "merkleTreeRoot": {
        "words": [
          -474954686,
          -1728308204,
          -1694763832,
          -1720731356,
          665731556,
          1687917388,
          -1533699813,
          2018687061
        ],
        "sigBytes": 32
      },
      "index": null
    },
    {
      "blockIndex": 3,
      "blockCreatedWhen": "01/04/2019",
      "blockTransactions": [
        {
          "transactionId": 345,
          "inputAccount": "srthhf",
          "outputAccount": "oouyuu",
          "fee": 65,
          "amount": "btc",
          "hash": "eaa995a8b3a64237c3133fbb744ed1761ba716781b7026f7f956043173c452f6"
        },
        {
          "transactionId": 452,
          "inputAccount": "jhghgg",
          "outputAccount": "rrttyu",
          "fee": 11,
          "amount": "eth",
          "hash": "e867bcef38e0272776fc3fbeff0aac19cb366ca513945ddfa1491adc2f3257b2"
        },
        {
          "transactionId": 230,
          "inputAccount": "wewfgf",
          "outputAccount": "ghjgtt",
          "fee": 230,
          "amount": "eth",
          "hash": "d25b62651513ee902e063970b5b7baed30de4f9832b7a78db3247a09414a1495"
        }
      ],
      "blockPreviousHash": "f58b63c0fbe00f69b6b1bd04560ffdd72ebe7b71e70a16224bf45aad01308e0f",
      "hash": "288b9f37d503b22a5f4535e086ab84fdbff2e22f8880fb2f24baa29b5151235d",
      "nonce": 0,
      "merkleTreeRoot": {
        "words": [
          -474954686,
          -1728308204,
          -1694763832,
          -1720731356,
          665731556,
          1687917388,
          -1533699813,
          2018687061
        ],
        "sigBytes": 32
      },
      "index": null
    },
    {
      "blockIndex": 4,
      "blockCreatedWhen": "01/05/2019",
      "blockTransactions": [
        {
          "transactionId": 456,
          "inputAccount": "sfgfss",
          "outputAccount": "xzsdee",
          "fee": 4,
          "amount": "btc",
          "hash": "af15f83e9c599347351019f62309fb1b9077447a4e0fc8b48a6b5c487b1e144e"
        },
        {
          "transactionId": 780,
          "inputAccount": "fgtfdd",
          "outputAccount": "rrttyu",
          "fee": 22,
          "amount": "eth",
          "hash": "8984871129b02f0b09f83abd5c1161f6a0dee64f84c336a54f5c34e5454f02a7"
        },
        {
          "transactionId": 112,
          "inputAccount": "qwewue",
          "outputAccount": "hgyuyu",
          "fee": 280,
          "amount": "eth",
          "hash": "2631aa030dfbc44fe084d86f69c1d24ca64a154c59d13b42718211ed02e3b1dc"
        }
      ],
      "blockPreviousHash": "288b9f37d503b22a5f4535e086ab84fdbff2e22f8880fb2f24baa29b5151235d",
      "hash": "07205c43afea9e53d02dedce994c891babccd8397d082b431f986045cbc18e44",
      "nonce": 0,
      "merkleTreeRoot": {
        "words": [
          -474954686,
          -1728308204,
          -1694763832,
          -1720731356,
          665731556,
          1687917388,
          -1533699813,
          2018687061
        ],
        "sigBytes": 32
      },
      "index": null
    },
    {
      "blockIndex": 5,
      "blockCreatedWhen": "01/06/2019",
      "blockTransactions": [
        {
          "transactionId": 567,
          "inputAccount": "xxcbbf",
          "outputAccount": "nbvbcx",
          "fee": 545,
          "amount": "eth",
          "hash": "8030161e3fdb7038fb7e751236063996152897f9647c654fb47334da7f7295bb"
        },
        {
          "transactionId": 677,
          "inputAccount": "fdfsaa",
          "outputAccount": "uytttt",
          "fee": 33,
          "amount": "eth",
          "hash": "b327dfa8769ec04bc447313af98feeb783c95c198e926737ea916700f4b71994"
        },
        {
          "transactionId": 111,
          "inputAccount": "hjhjh",
          "outputAccount": "huiuii",
          "fee": 5480,
          "amount": "eth",
          "hash": "aae165434e054c42e7da129b08a7c99f6c544351b7383459e322ef253570f4a4"
        }
      ],
      "blockPreviousHash": "07205c43afea9e53d02dedce994c891babccd8397d082b431f986045cbc18e44",
      "hash": "55c88c98e497a9f300ca3ec8bec0129652a8e5e9b49e6ecaf4ab0c63b42ebf4f",
      "nonce": 0,
      "merkleTreeRoot": {
        "words": [
          -474954686,
          -1728308204,
          -1694763832,
          -1720731356,
          665731556,
          1687917388,
          -1533699813,
          2018687061
        ],
        "sigBytes": 32
      },
      "index": null
    },
    {
      "blockIndex": 6,
      "blockCreatedWhen": "01/07/2019",
      "blockTransactions": [
        {
          "transactionId": 678,
          "inputAccount": "uiyuyy",
          "outputAccount": "dfgree",
          "fee": 5,
          "amount": "btc",
          "hash": "134269524a221afb1f5ef218584b063ab4bea3aaa5dc2cdbfa66f439f2d1e36b"
        },
        {
          "transactionId": 553,
          "inputAccount": "ooouio",
          "outputAccount": "yuyuuu",
          "fee": 55,
          "amount": "eth",
          "hash": "19076e1cb35d8357b16f92794c5bd1ff020c8cb9e8ce5fcf03b560256a363c16"
        },
        {
          "transactionId": 467,
          "inputAccount": "eeeewq",
          "outputAccount": "jgftft",
          "fee": 880,
          "amount": "eth",
          "hash": "461ad65962d2d1cc1f767c1b9c0f569d19ef48be60ef2fe8c08898cc01d34492"
        }
      ],
      "blockPreviousHash": "55c88c98e497a9f300ca3ec8bec0129652a8e5e9b49e6ecaf4ab0c63b42ebf4f",
      "hash": "e1087468c2719f967584cab0a22938b81e9e956762f2c5a2d2dd91da2c92cfbd",
      "nonce": 0,
      "merkleTreeRoot": {
        "words": [
          -474954686,
          -1728308204,
          -1694763832,
          -1720731356,
          665731556,
          1687917388,
          -1533699813,
          2018687061
        ],
        "sigBytes": 32
      },
      "index": null
    },
    {
      "blockIndex": 7,
      "blockCreatedWhen": "01/08/2019",
      "blockTransactions": [
        {
          "transactionId": 789,
          "inputAccount": "iughff",
          "outputAccount": "opoiii",
          "fee": 125,
          "amount": "btc",
          "hash": "7149930eae17b35f22c02acb0765b829d79e5539c6e2e80b837045f89d0422c2"
        },
        {
          "transactionId": 988,
          "inputAccount": "aqQQQQ",
          "outputAccount": "trewew",
          "fee": 1,
          "amount": "eth",
          "hash": "d752f91eb8010b5c9d6066afb92a5fa6c4667f9fc18b6f8d4a7c1a32b104919c"
        },
        {
          "transactionId": 432,
          "inputAccount": "eeeee",
          "outputAccount": "khuuuu",
          "fee": 40,
          "amount": "eth",
          "hash": "c5c20ac19253cd6782f0a95eb628c719cafeac22d4c8bacbf2863b06e0e366db"
        }
      ],
      "blockPreviousHash": "e1087468c2719f967584cab0a22938b81e9e956762f2c5a2d2dd91da2c92cfbd",
      "hash": "63302ff671cee7c659e1bf9eb1cd659f054d4615c4692d5b425ad04b2f82deb6",
      "nonce": 0,
      "merkleTreeRoot": {
        "words": [
          -474954686,
          -1728308204,
          -1694763832,
          -1720731356,
          665731556,
          1687917388,
          -1533699813,
          2018687061
        ],
        "sigBytes": 32
      },
      "index": null
    },
    {
      "blockIndex": 8,
      "blockCreatedWhen": "01/09/2019",
      "blockTransactions": [
        {
          "transactionId": 890,
          "inputAccount": "sdsrer",
          "outputAccount": "sdfser",
          "fee": 99,
          "amount": "eth",
          "hash": "c4abc25fcc9ea1e78be488aac5ee9a3ee1f16b0915e23700ff6bb11e1df02c51"
        },
        {
          "transactionId": 886,
          "inputAccount": "gfgfdg",
          "outputAccount": "qqqqqq",
          "fee": 88,
          "amount": "eth",
          "hash": "fd454dbdfb263db527ed335116aab73a7dab8468d3d443aa0fd8e4c7e438f465"
        },
        {
          "transactionId": 444,
          "inputAccount": "gfgfd",
          "outputAccount": "llllkl",
          "fee": 70,
          "amount": "eth",
          "hash": "c9b9ebdcb469f0f5ae8c6653c10f45d88a59b807c87b9febe1be986844d558f4"
        }
      ],
      "blockPreviousHash": "63302ff671cee7c659e1bf9eb1cd659f054d4615c4692d5b425ad04b2f82deb6",
      "hash": "fec255eb642f1dd02ab7c5185bfd78f9574b50c0188a888b4227b8cef5497e0e",
      "nonce": 0,
      "merkleTreeRoot": {
        "words": [
          -474954686,
          -1728308204,
          -1694763832,
          -1720731356,
          665731556,
          1687917388,
          -1533699813,
          2018687061
        ],
        "sigBytes": 32
      },
      "index": null
    },
    {
      "blockIndex": 9,
      "blockCreatedWhen": "01/10/2019",
      "blockTransactions": [
        {
          "transactionId": 901,
          "inputAccount": "mnghjy",
          "outputAccount": "dfgxbg",
          "fee": 65,
          "amount": "btc",
          "hash": "6e94502eb38d14410141d87bd27a2369e14e62bf8efece8adbde9873a8e88777"
        },
        {
          "transactionId": 644,
          "inputAccount": "dddsds",
          "outputAccount": "wwwwww",
          "fee": 55,
          "amount": "eth",
          "hash": "7072d7ec51bf41a1f36744f7abf8e9e4f70d195b708420f77b5141bcb7567f45"
        },
        {
          "transactionId": 333,
          "inputAccount": "sdsaqq",
          "outputAccount": "ererew",
          "fee": 50,
          "amount": "eth",
          "hash": "199c710224da1e16f597c2eae088acd5c3d4b6fd9a12d84c0b76a735eed541a2"
        }
      ],
      "blockPreviousHash": "fec255eb642f1dd02ab7c5185bfd78f9574b50c0188a888b4227b8cef5497e0e",
      "hash": "fcec05a7f1afb3c91ddbf90f36e04d17e4eabd7e47545a9f08173767cd4d74c8",
      "nonce": 0,
      "merkleTreeRoot": {
        "words": [
          -474954686,
          -1728308204,
          -1694763832,
          -1720731356,
          665731556,
          1687917388,
          -1533699813,
          2018687061
        ],
        "sigBytes": 32
      },
      "index": null
    },
    {
      "blockIndex": 10,
      "blockCreatedWhen": "01/11/2019",
      "blockTransactions": [
        {
          "transactionId": 10,
          "inputAccount": "vccvff",
          "outputAccount": "uiyfvv",
          "fee": 74,
          "amount": "btc",
          "hash": "8f2abd539ef073b7d4053fe6090196f682f6361a619329e45a3a0df10919623c"
        },
        {
          "transactionId": 994,
          "inputAccount": "mnmnmm",
          "outputAccount": "qqqqhh",
          "fee": 66,
          "amount": "eth",
          "hash": "040dda42beee5673e882884e3e963292015253348b1db5c3feb5d54af4e4b728"
        },
        {
          "transactionId": 323,
          "inputAccount": "qwqwqq",
          "outputAccount": "wreret",
          "fee": 30,
          "amount": "eth",
          "hash": "af54b136d57904e80823c53649d4149092fb6ebb4bf1e8578facaae8aca2cf1c"
        }
      ],
      "blockPreviousHash": "fcec05a7f1afb3c91ddbf90f36e04d17e4eabd7e47545a9f08173767cd4d74c8",
      "hash": "9074846c6a4153802d86cd19b6d95266c56d082329c6f0f52ded270fae269c48",
      "nonce": 0,
      "merkleTreeRoot": {
        "words": [
          -474954686,
          -1728308204,
          -1694763832,
          -1720731356,
          665731556,
          1687917388,
          -1533699813,
          2018687061
        ],
        "sigBytes": 32
      },
      "index": null
    }
  ]
}

NOTE: Within the merkleTreeRoot property, crypto-js utilizes word arrays encoded as UTF-8 when given a string.

Above, notice the hash and blockPreviousHash property of each block. This serves as the link for the chain.

But what happens if one were to modify a block somehow? Perhaps change the fee property or attempt to recalculate a block hash?

console.log('Validate entire blockchain: ' + blockChain.isBlockChainValid()); // let's validate the blockchain is secure
 
blockChain.chain[5].hash = blockChain.chain[5].generateBlockHash();  // let's try to recalculate hash for block 5
 
console.log('Is blockchain valid? ' + blockChain.isBlockChainValid()); 
 
Validate entire blockchain: true
Is blockchain valid? false

It appears that the blockchain validation function isBlockChainValid() caught an attempt to recalculate its hash. Of course, this is pretty basic validation, but it serves its purpose to illustrate the secure nature of blockchain.

Final Thoughts

In summary, a blockchain can be written in various languages. I hope this basic JavaScript version not only illustrates that but gives a little more overview of how blockchains work.

Thank for reading ! Please share if you liked it!

How do I build an HTML solution in Solidity

There is a code for a simple conversion (Farenheit to Celcius) in HTML. I am working to recreate the same thing in Solidity. I would need some pointers in making it work. The solidity Code is as follows:&nbsp;

There is a code for a simple conversion (Farenheit to Celcius) in HTML. I am working to recreate the same thing in Solidity. I would need some pointers in making it work. The solidity Code is as follows: 


contract TemperatureSolution{
    uint16 input,
function convertTemp(uint16 _input) public{
    return (document.getElementById("outputCelcius").innerHTML=(valNum-32)/1.8)
}
convertTemp(_input);

}

For reference, the HTML that it is based upon is as follows (this works, but I intend to create it on a blockchain):

<html>
<body>
<p>Type a value in the Fahrenheit field to convert the value to Celsius:</p>
<p>
<label>Fahrenheit</label>
<input id="inputFahrenheit" type="number" placeholder="Fahrenheit" oninput="temperatureConverter(this.value)" onchange="temperatureConverter(this.value)">
</p>
<p>Celcius: <span id="outputCelcius"></span></p>

&lt;script&gt;
function temperatureConverter(valNum) {

valNum = parseFloat(valNum);
document.getElementById("outputCelcius").innerHTML=(valNum-32)/1.8;
}
</script>
</body>


Top 10 Best Blockchain Programming Language for Blockchain Programmer

Top 10 Best Blockchain Programming Language for Blockchain Programmer

We aim at equipping you with every necessary knowledge of the best programming languages for blockchain and you'll learn more 10 Best Blockchain Programming Language for Programmer: C++, Java, Python, Ruby, Solidity, Go, JavaScript...

We aim at equipping you with every necessary knowledge of the best programming languages for blockchain and you'll learn more 10 Best Blockchain Programming Language for Programmer: C++, Java, Python, Ruby, Solidity, Go, JavaScript...

If you’re a tech-oriented person like me, chances are you’ve wondered at least once about all those latest fusses regarding blockchain and cryptocurrencies. So, what is this blockchain and why’d you be tempted to learn more about it? Blockchain, as the name suggests, is a chain of blocks; connected sequentially using complex cryptographic logic.

This technology was implemented first by Satoshi Nakamoto and was first used in the implementation of the popular BitCoin cryptocurrency. The blockchain technology is being used heavily in the industry, thanks to the high-level of security it provides in business transactions. From corporate firms to industrial banks, blockchain developers are sought everywhere equally. So, wielding this modern-day skill by learning the best blockchain programming language guarantee you an edge over your fellow developers.

Which are the best programming languages for blockchain? Developers are presently utilizing prevalent programming languages like C++ and Java to manufacture custom blockchain programs. What’s more, digital money specialists have made languages like Simplicity and Solidity that are explicitly intended for blockchain improvement.

The worldwide Blockchain market is right now worth an expected $1.2 billion and specialists foresee that it will arrive at a $57 billion valuation by 2025, developing at over 69% every year.

Significant enterprises and investors are teaming up with Blockchain counseling organizations to grow new digital currency innovation, savvy contracts, conveyed records for customary banks, gaming tokens, and inventory network the executives frameworks.

What Is Blockchain?

Customary financial uses a bank as the record and mediator. To move cash to a companion, an individual should initially contact their own bank and request that they move cash to a particular record number. The bank checks the sender’s record for assets, moves those assets to the goal, and records the exchange on the sender’s record. The accepting bank must accomplish something very similar.

In any case, the issue with this customary financial framework is that records are put away inside and are defenseless against hacking and control.

Blockchain disposes of this hazard by putting away all records online in a decentralized, unknown record that can be gotten to by anybody. Blockchain uses squares, or accumulations of information, like spreadsheet lines and segments, to store information. Squares are added to the “chain” in successive request.

In contrast to conventional bank records, which are put away inside, each blockchain client has a total record of the whole blockchain on their PC. This implies they can rapidly discover any exchange that has ever happened in the event that they have the comparing hash code. Since that information is put away freely, it can never be changed or erased — giving clients genuine feelings of serenity and trust in the framework.

Organizations keen on exploiting the blockchain upset should scan for up-and-comers with skill in the accompanying programming languages.

Here are the best programming languages for Blockchain

1. C++

C++ keeps on being one of the most famous programming languages in the tech world and is a prevailing power in the blockchain business also. The article arranged language is ideal for blockchain improvement, since it utilizes similar standards, for example, epitome, deliberation, polymorphism, and information covering up, as blockchain to avoid incidental alters to information.

Engineers additionally prize C++ in view of its memory control abilities. The language helps keep squares secure and deal with an enormous number of asset demands by enabling each system hub to acknowledge or dismiss individual squares.

C++ is additionally utilized broadly by blockchain advancement administrations due to the manner in which it handles parallel undertakings and stringing. The language is equipped for taking care of both parallel and non-parallel assignments, notwithstanding improving single-string execution.

EOS is an awesome case of a blockchain program worked with C++. The open source programming was discharged by Square in 2018 and is intended to process exchanges more rapidly than choices by restricting the product to only 21 square creating hubs. This enables the product to affirm an exchange in under a second and settle it in only two minutes.

2. JavaScript

GitHub as of late positioned JavaScript as the most mainstream language for developers — with a fantastic 95% of sites utilizing it here and there. Be that as it may, JavaScript isn’t just the lord of web advancement; the adaptable programming language is additionally utilized broadly for blockchain improvement.

One reason why blockchain designers prize JavaScript is a direct result of the manner in which it handles offbeat code. This is significant in blockchain, as thousands or even a great many exchanges might be started simultaneously. Offbeat, parallel programming empowers a program to finish numerous activities all the while. Standard, synchronous programming just can’t deal with that volume.

By running numerous activities on the double, offbeat code can improve programming responsiveness and application execution. This empowers blockchain projects to deal with the enormous volume of activities without hindering execution and disappointing clients.

You may also like: How to Build a Blockchain in JavaScript.

3. Java

The only language that can challenge the reign of C++ in the industry is Java, and for good reasons so. Java is in many ways similar to C++ regarding its object-oriented approach and a vast community of third-party applications and platforms. The main reason to use Java as the de-facto blockchain programming language in the industry is, however, its highly-capable portability.

Programs written in Java are portable across any computational device, as they don’t rely on system-specific architecture, instead uses the universal JVM(Java Virtual Machine) for execution. This makes Java one of the best programming languages for blockchain.

4. Python

Python is probably THE most trending programming language you can learn these days!

It's very popular because it's easy to learn and use, runs on all operating systems and allows you to build a broad variety of programs: Be that web applications, desktop applications, utility scripts or using it for data science and machine learning.

You'll do so whilst building your own Blockchain and Cryptocurrency. These are of course also highly trending topics and not a lot of people understand what a Blockchain really is, you'll learn a lot about the core concepts of the Blockchain and you'll see how Python can be used for the many aspects that make up a Blockchain and Cryptocurrency.

You may also like: Building a Blockchain with Python.

5. Solidity

Solidity is a savvy contract and blockchain improvement language that is utilized broadly by Ethereum designers. The area explicit language utilizes a significant number of indistinguishable standards and punctuation from JavaScript to make high-caliber, decentralized applications.

Engineers lean toward the language since it enables them to compose elevated level code for the Ethereum blockchain arrange, the second-most famous blockchain digital currency, which can be assembled into low-level machine code. It additionally enables people to use the Ethereum advanced exchange record to make brilliant agreements between organizations.

The agreement situated language utilizes invariants, preconditions, and post-conditions to streamline the advancement procedure and to make the agreement age process easier for clients.

Solidity is at present accessible on a scope of blockchain stages, including Ethereum, Ethereum Great, Tendermint, and Counterparty. It’s utilized for a scope of utilizations, including business contracts, barters, crowdfunding, and that’s just the beginning.

6. Ruby

Although quite old and tested by the industry, Ruby gained momentum as a blockchain programming language in the last couple of years or so. Ruby, an interpreted high-level language with object-oriented features, much like Python, can be a viable blockchain coding language for uncountable reasons. It offers developers the ability to prototype their vision rapidly using open source third-party APIs and plugins.

The Ruby ecosystem is thriving with loyal contributors since its inception as the de-facto web language starting from the first half of this millennium. It’s especially prevalent within the Asian developers, the most substantial fraction of open source blockchain developers.

7. Simplicity

Simplicity is a fresh out of the plastic new programming language that was discharged in November 2017 and planned explicitly for shrewd agreements and blockchain improvement. The language conceals low-level consistent parts from architects so as to expand efficiency and stay away from engineer interruptions, which is one motivation behind why it is quickly winding up well known in the network.

Like C++, Effortlessness is an item arranged language that uses indistinguishable standards from blockchain to forestall blunders and changes to information. It additionally utilizes Merklized Theoretical Sentence structure Trees to sort out the projects into trees — along these lines taking into account littler exchange sizes and lessening square space prerequisites.

The language’s makers, Blockstream, are as yet extending the language and its abilities. Designers can hope to see Simplicity being utilized in more applications towards mid-2020 once the language is incorporated into Bitcoin and its highlights are concluded.

8. Go

The brainchild of Robert Griesemer, Rob Pike, and Ken Thompson, pioneer of modern programming languages, Go is the best blockchain programming language for building hyper ledger fabric. The statically-typed yet compiled language is on par the performance level needed by a blockchain coding language. Go comes with every advanced feature you’d need when building your first blockchain, namely classes and inheritance, generics, annotations, constructors, and exceptions.

Go offers top-notch concurrency support in blockchain applications, thanks to its smart implementation of channels and interfaces. So, it’s one of the best programming languages for blockchain when it comes to developing a system that is not only efficient but also lightning fast.

9. Rust

The newest blockchain programming language on the block currently, Rust aims at providing open source devs the capability to build fast and efficient blockchain systems. We found Rust to be exceptionally good when it comes to CPU-bound tasks. You can take either a functional approach or an imperative one with Rust for developing your blockchain.

It’s one of the best programming languages for blockchain due to its highly-capable mechanism of handling mutable states. The Rust compiler provides awe-inspiring optimization of your blockchain. The fast, memory safe, and exclusively concurrent nature of this blockchain coding language makes it most suitable for developing real-world blockchains.

10. PHP

Although dimmed not suitable for modern web anymore, PHP still covers the majority of web systems. It can be utilized to build simple to complex blockchain systems as well, thanks to its object-oriented features and a vast active open source community.

If you’re a new programmer looking for getting your hands dirty at blockchain coding, PHP might turn out to be the best option for you. A considerable number of PHP developers will guarantee a ready workforce in case you develop something highly capable and intend on going corporate.

Summary

Blockchain is here to stay. The popular record-keeping technology is what makes cryptocurrency exchanges possible and is widely used by corporations, individuals, and blockchain consulting services for software development.

Developers can easily use popular programming languages like C++ and Java for blockchain development. Alternatively, the community has recently created blockchain-specific languages like Solidity and Simplicity which make cryptocurrency development a smooth process.

Expect to see more original languages spring up over the next several years, as the blockchain market continues to grow rapidly and cryptocurrency begins to be used by ever-larger numbers of people.

Thank for reading!