Hey everyone, my name is Xavier
and in this video, we will continue working
on our little JavaScript blockchain implementation.
In the last two videos, we created our blockchain
and we secured it with the proof-of-work algorithm.
In this video, I'm going to expand it a little bit
and make it a simple cryptocurrency.
To do that, we're going to do two things.
Number one, we're gonna make it
so that a block can contain multiple transactions
and number two, we're gonna add rewards for miners.
Now why would we wanna do that?
Well when you start a cryptocurrency,
you need to have virtual money or coins.
And if you don't introduce them somewhere in the system,
no one can get any coins.
Mining rewards,
steadily introduce new coins into the system.
So, let's get going.
Here I've opened up the source code of the last two videos
in Visual Studio Code,
and I'm going to start by removing
all of our test code near the bottom.
We're gonna re-factor a bunch of this stuff
so this won't work anyway.
I'm just gonna keep the sajeevCoin instance.
Next, I'm going to modify the block class a bit,
so that it supports multiple transactions.
So instead of receiving a data field,
I'm gonna say that it receives transactions.
That makes a little bit more sense.
And we're gonna say this dot transactions
equals transactions.
I'm also going to remove the index argument right here
because I've added that in the first video
but it's actually not useful in a blockchain.
The order of blocks
is determined by their position in the area,
and not by the index that we can pass here.
So when I remove index from the constructor block here,
I also have to remove it from our genesis block down here.
All right, so this transaction argument
will get an array of transactions.
So, let's define what a transaction looks like
by creating a new class for a transaction.
And I'm gonna say that a transaction receives
a fromAddress, a toAddress and an amount.
This is pretty straightforward,
a transaction always comes from someone,
it goes to someone and it carries a certain amount of coins.
And then we're just going to assign each of these.
So fromAddress equals fromAddress,
this dot toAdress equals toAddress,
this dot amount equals amount.
With that out the way,
we can start working on our main blockchain class.
There are a few things that we have to do here.
We have to add a mining reward.
We need a place to store pending transactions
and we need a new method to mine a new block
for the pending transactions.
Now you might ask yourself,
what is this pending transaction stuff and why do I need it?
Well it's because we only create blocks
on a specific interval.
In Bitcoin's case, proof-of-work algorithm
makes sure that there's only one block created
every 10 minutes.
All the transactions that are made in between blocks
are temporarily stored in this pendingTransactionsarray,
so that they can be included in the next block.
So let's start with simple things first.
I'm going to add a new property to our blockchain class
for our pendingTransactions.
So I'm gonna call it this dot pendingTransactions
equals an empty array.
And them I'm also going to define a property
that will control how much coins the miners get as a reward.
So I'm gonna say, this dot miningReward equals,
let's say, 100 coins if you successfully mine a new block.
I'm also going to reduce the difficulty a bit
so that it's just faster to test
and then we can increase again when we're done
with our changes.
Now that we have these changes,
we're gonna work on our new mining method.
So, this was our old mining method,
the addBlock method, and I'm gonna replace that
by minePendingTransactions.
Now minePendingTransactions, this will receive an address.
It will receive a miningRewarAddress.
So when a miner calls this method
it will pass along it's wallet address,
and say, hey if I successfully mined this block
then send the reward to this address.
Now, inside this method,
we will start by creating a new block.
So I'm gonna say let block equals eight new Block.
That requires a timestamp, so I'm gonna say Date dot now.
And then it requires some transaction,
so we're gonna pass along this dot pendingTransactions.
We're gonna give it all of our pending transactions
that are currently stored in our blockchain.
Now in real world cryptocurrencies,
like again, for instance, Bitcoin,
adding all the pendingTransactionsto a block
isn't possible
because there are way too many pending transactions
and because the block size cannot increase one megabyte.
So instead,
miners get to choose which transactions they include
and which they don't.
But we're not going to worry about this
in our simple example,
but just be aware that in reality,
miners have to pick the transactions
that they want to include.
So after creating the block, let's mine it.
We're gonna say block dot minedBlock.
And we're going to mine it with the difficulty
that is currently set within our blockchain,
right now it's two.
And if that's done,
we're gonna to log something to the console.
We're gonna say block successfully mined.
And then we're going to add it to our chain.
So we're going to say this dot chain dot push our block.
And the last two things that we have to do
is reset the pendingTransactions array
and create a new transaction to give the miner his reward.
Now we can do this all in one go.
We can just say that this dot pendingTransactions
equals an empty array,
that will reset the pending transactions.
But we can also just define
an area which is one new element.
So we can say new Transaction.
The fromAddress, we're gonna say no.
Because there isn't really a fromAddress in a mine reward.
It just comes out of this system,
it's the algorithm that gives it to you.
We're going to send it to the miningRewardAddress,
and how much coins are we going to send
this dot miningRewards.
Now, in the previous video, some people pointed out,
that they can change this code right here
and give themselves more coins.
And that's true, you really can do that.
However, cryptocurrencies are powered
by a peer-to-peer network and other nodes in the network
won't accept your attempts of fooling them,
and instead, they will just ignore you.
So that's something that I wanted to address
from the previous videos.
Now to continue,
I'm also going to create a simple method
called createTransaction.
And this will receive a transaction and all this will do
is add this to the pendingTransactions area.
So we're going to push it on to that area.
All right, now the final thing that I'm going to do
is I'm going to create a method,
that checks the balance of an address.
Many people thing that if you send some bitcoins around,
they actually move away from your wallets balance
to someone else's balance.
But in reality, you don't really have a balance.
The transaction is just stored on the blockchain,
and if you ask for your balance,
you have to go through all the transactions
that involve your address and calculate it that way.
So let's create a method for this.
We gonna call this getBalanceOfAddress,
and it will of course receive an address
that we have to check its balance.
We're going to say that the balance starts at zero.
And then we're going to loop over all the blocks
in our blockchain.
So we're gonna say for constant block of this dot chain.
And because our blocks now contain multiple transactions,
we're going to say loop over each transaction
of the transactions in side this block.
And then we have to write a bit of logic.
If you are the fromAddress,
that means that you've transferred money
away from your wallet to someone else,
so we have to reduce your balance.
And if you are the toAddress,
then you were the receiver of a transaction
and we have to increase your balance.
So, we're going to say,
if transaction fromAddress equals the address
that was given here and its address,
then we're going to reduce your balance
with the amount of the transaction.
And if the toAddress equals the address that you gave us,
then we're going to increase your balance
with the amount of the transaction.
And when we're done looping over all the blocks
and all the transactions,
we're just gonna return the balance that you have.
Alright, so now it's finally time to test our code.
I'm gonna scroll down
and the first thing that I'm going to do
is I'm going to create some transactions.
I'm going to savjeeCoin dot createTransaction.
And we're going to make a new Transaction object here.
new Transaction.
This has a fromAddress,
I'm just going to give it random strings here address one,
to address two, and it will send address two,
let's say 100 coins.
And then we're going to repeat that.
And then we're going to say that address two, for instance,
gives address one 50 points back, something like that.
Now in reality address one and address two
would be the public key of someone's wallet.
After we've created these new transactions,
they will be pending.
They will be in the pendingTransactions area.
So we have to start the miner
to actually create a block for them
and store them on our blockchain.
So I'm going to say console dot log,
new line Starting the miner
and then I'm going to say savjeeCoin
dot minependingTransactions.
And this method expects a miningRewardAddress.
In other words, it wants to know,
where it should send the miningRewards to.
So, I'm just going to create another fake address.
I'm gonna say Xavier's address.
And after that is done,
we're going to check my balance.
So we're going to say console log Balance of Xavier is,
and then we're going to say, savjeeCoin dot
getBalanceOfAddress, xaviers address.
All right, so let's save the file.
And let's open the terminal and run it
to see if it actually works.
So I'm going to say node source main dot js.
And yeah, kind of works starting the miner, mined the block,
but then it says my balance still zero.
Well, actually, that's correct,
because in our mining method if you remember,
after a block has been mined,
we create a new Transaction to give you your miningReward,
but that one is added to the pendingTransactions array.
So the miningReward will only be sent
when the next block is mined.
So, lets mine another one then.
We say console dot log.
Uh oops, kind of put that...
Just copy it and put it there.
So storing the miner again,
and then we're gonna check my balance again, right there.
Save it.
Run it again.
And sure enough the second time,
it increased our balance to 100.
But in mining our second block,
I also get a new reward
which is again in the pendingTransaction state
and will again be included in the next block that is mined.
So, that concludes this video.
We've transformed our block chain
into a very simple cryptocurrency,
where a block can contain multiple transactions,
and where miners get 100 coins in miningRewards.
Now I want to point out that this is a simplified version
of a cryptocurrency and it's for education purposes.
Don't try to make your own cryptocurrency with this.
So that's it for now.
If you haven't subscribed already,
make sure you hit the subscribe button
and also like this video.
Thank you very much for watching,
and I'll see you in the next video.