05 Petshop - Development and Deployment

Learn Ethereum by Examples Part 2 Pet-Shop - Your first Dapp
21 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
This is a free item
$0.00
د.إ0.00
Kz0.00
ARS$0.00
A$0.00
৳0.00
Лв0.00
Bs0.00
B$0.00
P0.00
CA$0.00
CHF 0.00
CLP$0.00
CN¥0.00
COP$0.00
₡0.00
Kč0.00
DKK kr0.00
RD$0.00
DA0.00
E£0.00
ብር0.00
€0.00
FJ$0.00
£0.00
Q0.00
GY$0.00
HK$0.00
L0.00
Ft0.00
₪0.00
₹0.00
ISK kr0.00
¥0.00
KSh0.00
₩0.00
DH0.00
L0.00
ден0.00
MOP$0.00
MX$0.00
RM0.00
N$0.00
₦0.00
C$0.00
NOK kr0.00
रु0.00
NZ$0.00
S/0.00
K0.00
₱0.00
₨0.00
zł0.00
₲0.00
L0.00
QR0.00
SAR0.00
SEK kr0.00
S$0.00
฿0.00
₺0.00
$U0.00
R0.00
ZK0.00
Already have an account? Log In

Transcript

Hello everyone. So in today's session we are going to get started with our first part in our paid shop tutorial of developing the tab decentralized application. And in this part we are going to talk about the development and deployment of the tab. Okay, so the things we are going to cover in this part is we are going to create our paint shop project. Then instead of creating the project from scratch, what we are going to do is we are going to use something called as truffle box. Okay, so truffle box basically are you know, the projects which are available on truffle, so that you know, we can use use those projects.

Okay. So, truffle provided us with a jumpstart application. For our pet shop, which contains you know a lot of out of the box things okay so instead of me creating the whole dap from scratch, we are going to capitalize on truffle box for the shop tutorial. Okay, so so boxes are nothing but you know truffle provided projects which are publicly available to get more information about truffle box. We can get more details over here, we can get more details about truffle box if I go the search box Okay, so truffle boxes, truffle box boxes are helpful boilerplates that allow you to focus on you know What makes you unique that allows you to focus on you know, the the development you want to do and you know it, it will give you a lot of boilerplate functionality. So in these boxes fill out the boxes you will see is spectrum.

Okay, so this petshop dap is what we are going to develop, but, you know, this box provided us the boilerplate for this okay. So let's get started. Okay, before we start, after we unbox, we are going to develop a new contract in that we will put some logic Okay. And then the new thing which we are going to discuss today is ganache. ganache is basically the test RPC which we talked about in our hello world. demo.

Okay, so this is the latest, or the newer version of desktop PC with, you know, a user interface and a lot of other features which you won't get it in test RPC. Okay. And finally, we are going to migrate our contract into the blockchain, the test RPC blockchain which we can actually will be running on. Okay, so let's get started. First of all you need to go to also get your development get started. So in my case, I have my tedium development kit and it is up and running.

Next thing I do is I will remotely connect to the development kit. Okay, so now that I have connected over here, we are going to our demo directory so I'll open a terminal. Let me maximize this CD to stop. The tables are here, I'm going to do into creating this project directory, I will CD to that directory. And here again you know I just showed you the picture box. So we will use the truffle command truffle unbox command and we are going to give the name of the box Okay, so this command will phage the pet shop box which is available in truffle box and it will fetch it from internet and give us the boilerplate for it.

The box is downloaded and we unboxed it. So let's see, you know how the folder directory structure look like. Okay, so for that I'm going to open or project in visual code studios for that audio code and the current directory, it will open up the Visual Studio. And we are just going to look at the directory structure. The directory structure will be very similar to the one which we have seen in our hello world example wherein it will consist of one directory for contracts where we can have all the contracts on there so let's look at that. So this is our pet shop.

Here. You can have all your contracts by default You will see one contract appearing okay which you know the truffle box provide you migration script script it will have the initial migration script. So this is the default migration script which will deploy the contract on blockchain and keep track of the last deployment. Okay. It has some node modules We'll talk about it later. src also, we'll talk about it later.

So these node modules in src folder they contain the source code for the user interface of our application. Then the test directory is for all the test cases. Okay. And then we have our truffle.gs. Here, the blockchain details are provided where the blockchain is running. So in our case, it is going to run on localhost 7545 right here you will see the difference right so last time when we Then our hello world example it was running on test RPC which starts on 848454.

Okay, now this time we are going to use ganache. So in that case the port is 7.45. Okay, so this is the directory structure we have. So the next thing we're going to do is we're going to start with our own contracts. For that I go to the contract directory, click on new file. And here I'm going to provide the name as an options.

So this this contract basically will have a logic of, you know, allowing somebody to adopt the pet. Okay. So let's put the code here and then we'll discuss What what that code is meant to do. Now, with this develop development kit, you also get the solution for the demos. So if you go to our tedium surrounding demos Now you will see a demo solution folder in that will go fetch up tutorial in contracts to hear you already have the code which is which is to be you know, created okay. So this is the solution for the ultimate solution which we are going to have.

So, what I'm going to do is I'll copy the entire code from solution Okay, into our new contract with which we just created okay. And now let me go to go through the code. So here what we are doing is this is the pragma to tell that you know which version of solidity ever to use. After that, again, we are creating a new contract. Core adoption just like we had that contract in our hello world example of mate of mine. Next thing is we are declaring a variable just like in other programming languages, here also in solidity we have variables we the variables are strongly typed.

So here we are creating an array of size 16 of type address okay. So, this add eight can hold addresses of the blockchain okay. So, each address on the blockchain is you know has a specific format and that type of variable or the value can be stored in a variable of type address. Okay. And then accessibility of this variable is public so it is publicly available the name of the variable we are calling it as adopters, okay? So there can be maximum 16 or you know pets we cannot talk.

Okay, so this variable will hold the details about all the pets. That instance is going to care about, okay, the address is on the blockchain. Okay. The next thing over here is the method, a dog. So this method all it does is it takes an integer the pet ID as input the method is again public and it returns an integer as an output. And then we have one assumption or here that it requires the pet ID to be between zero and 50.

This requirement is there because What logic we are going to do is put is any pet owner can have up to 16 pets they can adopt maximum 16 pets. So, for that reason we declared the adopters of size 16 and in our adoption method we are putting this restriction okay that the pet ID has to be between zero and 15 So, total 16 pets can be unlocked. The next thing over here is if this requirement is fulfilled we are going to then add that bit ID into our adopters list okay. And this MSG dot sender So, this MSG is again reserved you can say a variable okay which will give you the address of The instance which is calling this function okay. So, when we are going to deploy this contract on the blockchain we will have an instance of adoption right that instance then can call this method okay.

And when this method is getting called this MSG variable dot sender will have the address okay the address on the blockchain of that instance which is invoking this method. So this way our adapters array will hold the address of the adapter and finally, it is going to return the pet ID okay. So in all we are going to have maximum 16 pets right. So it will return you know the pet ID Okay, next thing is the get adopters method. Now here it will simply return the entire array. Okay.

So one thing with solidity language is that we have different types of variables. And if you make the variable public then by default you can do a get and set on the variable. But if you are having an add a of a variable in that case you don't have any data and setup for the entire array. So, in this case, to get the entire array we created our own method which will return the whole array. Okay, so this is our contract which consists of one variable to hold the list of pets. another variable another one function to get you the logic of adding pets.

To your list of adopted pets and the function to return the entire place we are going to compile this contract so quickly. So, truffle compile should compile our contract Okay, well this is done the next thing is we need to create the migration script okay. So let's go to our project, maximize this. So here we have the migration okay. So in the initial migration dot j s you will see that there is a logic to deploy or migration pattern okay. So by default whenever you create a truffle project you import or unbox a truffle box, you will see that there is one contract by default which is migration.org.

Okay. So this contract itself is this contract is there to deploy the entire project on the blockchain and keep track of the last deployment, okay. So any new contract when you add then it is going to deploy only the newly added contract and not a previously deployed contract. Okay. So this keeps track of that and the migration script for this. Basically just get the contract, okay?

And use this function deployed or deploy. And this function will deploy it on the blockchain. So in our case, we added a new contract called adoption. And that contract has to be migrated similar to the migration of this contract. coding migrations. Okay, so here, what I'll do is I'll just create a new file.

Okay, let's call this file s to underscore deployed contracts.ts. And in this file again, you know you have the solution, okay, so we can go to the solution, copy the migration script from there. Okay, and it is very similar to the one with the initial migration, all we need to do is import the contract. Okay. So we import that in a variable, and then we'll call this module dot exe. call this function called deployed or deployed with that variable.

So this will deploy contract on the blockchain. Okay, now, last time if you remember, we talked about the test RPC. Right we use test RPC to deploy our contracts. Okay. Now the test RPC is a command line test blockchain right. And, you know, it doesn't give you that much visibility into the blockchain.

Okay. So the enhanced version of our test RPC is what we are going to use this time. Okay. And we have that installed. So if you go to the application and here an application so we'll go to the text. So I have the shortcut for the ganja.

Available on the text top. So this is the similar test RPC but with a user interface, okay. So on the desktop if you go double click on this and it will open up the application ID. So it will come up with this Welcome to sprint here I will disable the analytics will continue Okay. Now here you will see some interesting things. Okay.

So, current block, the count is zero. Okay. So, here the blockchain artists blockchain is not it has not mined any blocks yet. Okay. and here if you go in the settings, this is the details of the server, the test RPC server, okay where we have the test blockchain running, okay and if you see here the port is 7545 Which is the same port which is mentioned in our truffle.js file. And just like our test RPC here, we have some default addresses, which comes along for our testing purpose.

And also one more thing you will see is we have 180. So 100 ethers are there for our testing purpose. And then you can go to the blog. So here it will show how many blocks we have currently, we don't have any no gas used, there is no transaction, okay? And you can check the logs. Okay.

And here are all the information where the blockchain is tracking and whatnot. Okay. So now what we're going to do is we are going to migrate our contract on to this blockchain. For that, I will go to the terminal My grid let's see right on the blockchain now, if we go back to our user interface for the blockchain you will see here there is that hundred ethers are now reduced to 99 by line four because there is some gas used to deploy or to migrate or contract okay. If you go to the blog, you will see there are four blocks which are mined, if you go to the transaction, he will see the transaction okay. So, there are two contracts created and you know, some gets utilized for deploying the contracts.

So, this is how we can migrate our contracts to the blockchain okay, right, okay. So this completes this part of Session

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.