
Creating the createNewTransaction method
The next method that we are going to add to our blockchain constructor function is called createNewTransaction. This method will create a new transaction for us. Let's follow the below mentioned steps to create the method:
- Start building up this method by adding the following line of code after our getLastBlock method:
Blockchain.prototype.createNewTransaction = function () {
}
- The function () will take three parameters, such as the following:
Blockchain.prototype.createNewTransaction = function (amount, sender, recipient) {
}
What these three parameters will do is as follows:
- amount: This parameter will take in the amount of the transaction or how much is being sent in this transaction.
- sender: This will take in the sender's address.
- recipient: This will take in the recipient's address.
- The next thing that we want to do inside of our createNewTransaction method is create a transaction object. So, add the following line of code to our method:
const newTransaction = {
}
- This object will have three properties in it. It will have an amount, a sender, and the recipient. These are the same three parameters that we passed into our function(). So, type in the following:
Blockchain.prototype.createNewTransaction = function (amount, sender, recipient) {
const newTransaction = {
amount: amount,
sender: sender,
recipient: recipient,
};
}
This is what our transaction object will look like. All of the transactions that we record on our Blockchain are going to look just like this. They all are going to have an amount, a sender, and the recipient, which is pretty straightforward and simple.
- The next thing that we want to do now is push this newTransaction data into our newTransactions array. Let's do this by adding the following code after our newTransaction object:
this.newTransactions.push(newTransaction);
So, the new transaction that we just created will now be pushed into our newTransactions array.
Now, let's just try to understand what this newTransactions array actually is. Basically, what is happening here with this newTransactions array is that on our blockchain there are going to be a lot of people who will be making a lot of different transactions. They will be sending money from one person to another and this will be happening repetitively. Every time a new transaction is created, it's going to be pushed into our newTransactions array.
However, all of the transactions in this array are not really set in stone. They're not really recorded in our blockchain yet. They will get recorded in our blockchain when a new block is mined, which is when a new block is created. All of these new transactions are pretty much just pending transactions, and they have not been validated yet. They get validated, set in stone, and recorded in our blockchain when we create a new block with the help of the createNewBlock method.
In our createNewBlock method, you can observe in transactions: this.newTransactions that we set the transactions on a new block equal to the newTransactions or the pending transactions in our blockchain. You can think of this newTransactions property on our blockchain as a pending transactions property.
For easy reference, let's actually change all of the newTransactions properties in our code to pendingTransactions properties. Overall, when a new transaction is created, it is pushed into our pendingTransactions array. Then, when a new block is mined or when a new block is created, that's when all of our pending transactions become recorded on our blockchain, and they are then set in stone and can never be changed.
The point of all this is that before our method ends, we want to return in which block we will be able to find the new transaction because our new transaction will be in the next block when it is mined. Consequently, we'll simply type the following code:
this.newTransactions.push(newTransaction);
return.this.getlastBlock()['index'] + 1;
In the preceding code, this.getlastBlock() returns a block object for us. We want to get the index property of this block – adding ['index'] will provide us with the index of the last block in our chain, and adding + 1 will provide us with the number of the block our transaction was pushed to.
Let's have a quick recap, the createNewTransaction method simply creates a newTransaction object, and then we push that newTransaction into our pendingTransactions array. Finally, we return the number of the block that the newTransaction will be added to.