Introduction To Truffle Ep5
EatTheBlocks/ Garrick Jocelyn
5 followers
2 Video Views·Mar 16, 2023
Hi guys, welcome to the fifth episode of EatTheBlocks. Today we're going to introduce
Truffle, which is the most popular framework for building smart contracts on Ethereum.
So the first thing we're going to do is to install Truffle, which is the framework for
developing smart contracts. So here I'm on the web page of Truffle. I will put the link
in the comments. And as you can see, there is a command to run in order to install the npm package.
So we're going to open a console terminal and I'm going to execute the command to install
Truffle. So it's better to install it globally so we can use its command line tool.
So I'm going to set the gflag install g truffle. So once this is done, you can check that it's
installed properly. If you type truffle version, you should see that at the time of this recording,
we are at version 4. And it also gives you the version of the Solidity compiler that it will
provide. The next thing we're going to do is to run the init command of Truffle to set up our
folder. So here I will type truffle init. And basically, it's going to set up the default
structure of a Truffle project. So here, let me clean up my screen. Let me show you the folder
that we have now. So we have three important folders. The first one is called contract.
It's where you're going to write your smart contract, your Solidity file. The second is
called migrations. So migrations are how you deploy your contract to the network. And finally,
you have the test folder where you will be able to test your Solidity contract. So you can test
them with Solidity or with JavaScript. And finally, we have two configuration files, truffleconfig.js
and truffle.js. So for this episode, we're not going to talk about them.
Okay, let's first inspect what we have in the contract folder. So if I ls what we have inside,
we already have a Solidity file. So that's a file which is required by the Truffle framework. So we're
not going to edit it. So now we're going to create our first smart contract with Truffle.
So for this, we're going to use the command line utility of Truffle. So since you may not
know by heart how to use it, we can use the help. So we just type truffle and you will see all the
commands that you can use with Truffle. So the one we already used is init here with the description
here. And the one we're going to use now to create a smart contract is the create command here. So it
says it's a helper to create new contracts, migration and test. Cool. Okay, so let's clean up
the screen. So we're going to use it like this. So truffle create contract hello world. And I'm
going to press enter. And it's going to create a new Solidity file in the contract directory. So
now if I ls what we have in contracts, then I can see that we do have a new file hello world.so.
Okay, so now I'm going to switch over my code editor and I'm going to open this new file. So
hello world.so. Okay, so that's basically a sort of template that Truffle provided for me.
So I'm not going to use that. So I'm just going to delete everything. And now what I'm going to do,
I'm going to switch to GitHub on the repo of EatTheBlock and I'm going to copy the content
of a hello world contract that we used in the previous episode. So here it's already highlighted.
So I copied it. By the way, I will put all the links in the comments. So don't worry if you
don't know what is the URL. Okay, so back to the code editor and we're going to copy this code.
I'm not going to spend time explaining what this contract does. It's very simple and I already
explained in a previous episode. And I'm going to do some very small changes. So for example,
this method accepts a variable of type byte 32, but I'm going to change this to a string
because it's easier to manipulate a string variable. And basically I'm going to replace
this byte 32 by string everywhere else. Okay, so now it's been replaced. And another change I'm
going to do in this get message method, which is basically a getter. It doesn't modify the
data on the blockchain. I'm going to add the constant keyword so that Solidity knows that
this method will not consume and will just read data. So back to the terminal and now we need to
write a migration file. So for this we're going to first inspect what we have in the migration
folder. So lsmigration. So we already have a migration that was created when we run the
truffle init command. So let's cat this to see what's inside. So migration, initial migrations.
Okay, so that's a very small file. So here it's highlighted. So basically what truffle
is going to do here, it uses a variable here called artifacts. So artifact is something
provided by truffle that is an abstraction for contracts that allow you to manipulate,
easily contract. If you remember in previous episodes, there were some concepts such as
ABR, which basically define the functions that can be called in each smart contract.
And it can be a little bit troublesome to manipulate this manually. So truffle provides
this abstraction called artifacts and with artifacts we can require a smart contract like this.
So artifact require and then we need to put the name of our smart contract and we can access
all those nice abstractions in the migrations variable here. And each migration file
needs to export a function. So here we have module.exports and this function takes a first
argument which is a deployer. And a deployer here in the following line has a method called deploy
where you can deploy your smart contract. So this is the most simple migration file possible.
And for what we need to do, we can just copy this and it will work perfectly to migrate
our hello world contract. In any case, make sure that you do not modify this first migration file
because truffle needs it to use its migration feature. So like previously, we're going to use
a command of truffle to create a migration file. So in this case, it's going to be truffle
create migration and we're going to call it hello world. And what truffle will do is that,
let me clean up my screen first. So if we inspect what we have in the migration folder,
we can see that now we have a new migration file here. So basically truffle took the name that we
passed as argument, hello world, and it adds some underscore and it's prefixed our file name with
some random numbers. And those numbers are useful because when you run the migration command,
basically truffle is going to use those prefix here to know in which order you need to run the
migration. Okay, so now we have our two migration files. Okay, so now let's switch back to the text
editor and we're going to modify our new migration file. So let's find it. So migration and we see
that this is our migration file here. Okay, so truffle created a small template, but it's not
enough. So we're going to use the other migration file to know what we need to put inside. So in
the other migration file, we have this. Okay, let's copy it and let's switch to the other file.
Okay, so here, first line, we require our smart contract. So in our case, it's not
migration.sol, but it's hello world.sol world. And here we're going to call this variable
hello world. And here inside this function, so we have our deployer, which called the method deploy
and is going to deploy hello world. It's not going to deploy migration. Okay, that's correct.
And we can just delete this. And there's still something we need to change. So here in our deploy
method, we need to add another argument. So basically, when we're going to instantiate
our smart contract on the blockchain, our smart contract takes the constructor takes an argument.
So we need to specify this. So here, we're going to add a comma. And here we're going to put a
random string. And for example, it can be this is my message. Okay, so the smart contract is supposed
to save this in its data after it's deployed. So later, we should be able to read this.
So back to the terminal. Now, we need to build our smart contract. So for this,
there is another truffle command. So we're going to run truffle.
Oh, no, sorry. No, it's not the build command. It's something else. No, what we need to do is
to compile. So this is the compile command. So basically, it's going to take our Solidity
contract and compile them into bytecode that the Ethereum virtual machine is going to understand.
It's also going to produce the API that we need to interact with our smart contract. So let's run
this command. And it's going to compile both our smart contracts. So the one we just created,
and the one that truffle uses, the migration one. So here, here we go, compiling contract,
and second compilation, compiling migration, that's all. We also got some warning messages
of the Solidity compiler. But we can just ignore this. Okay, so now I'm going to clean up my screen.
And if I ls what I have in my directory, I will see that there is a new directory,
which is called build. So what do we have here? Okay, ls build. Okay, well, we have another
directory, which is called contract. So let's see what's inside. Okay, so we have two JSON
files that have been produced by this compile step. So basically, when I told you before about
this artifact abstraction provided by truffle, the artifact needs those JSON files that are
produced after the compilation step. Okay, so now we are ready for the final step, which is to start
our testnet, deploy our contract, and interact with them. So for this, we're going to use the
truffle develop command. And this command is going to do two things. First, it's going to start a
local testnet. So here we can see it's available at localhost port 9545. And we have 10 accounts
that are unlocked. So those who followed the previous episode must be familiar with this step.
And second, at the bottom of my screen, you can see that we have an interactive prompt.
And we can use this to interact with our smart contract. So the first thing that we need to do
is we need to run the migration that we prepared just before. So for this, we can run the migration
command. So since we are already in the truffle console, we don't need to add the truffle keyword,
we can just directly type the name of the command. So the name of the command is just migrate.
So let's run this. Cool. Okay, so it has run the migration. So here we have the first migration
that we use internally by truffle. And finally, the second migration is the one that we created
ourselves. And here you can see that it has deployed the hello world contract and you have
its address on the blockchain. Okay, so now our smart contract is live on our local test net.
So we can interact with it. So in order to interact with it very easily,
the truffle framework already loads some variable in the console. So for example,
there is the famous library web suite that you can use directly. And there is already the
abstraction of all our contract. So one of them is hello world. So if I type hello world like this,
I should see something. So let me see. Yes. Okay, so now I'm going to create a variable
that's going to point to our smart contract on the blockchain. So the name would be var
inst. And now we're going to use this hello world abstraction and we need to execute the deploy
method and it's going to return a promise with the variable pointing to our smart contract on
the blockchain. So here I call the then method and this then method is going to be passed this
variable instance and I'm going to assign this instance variable to my int variable instance
like this. Okay and I'm going to add a bracket because otherwise the command line is going to
complain. Oops, it's a little bit annoying to retype everything. Anyway, it's no big deal.
Okay, so we close the parentheses and we're going to execute this and hopefully I didn't make any
syntax error. Let's see. Oops, syntax error. Okay, let's try again. Hello world deployed then
instance
and inst for instance.
Close the parentheses and we add a semicolon and I cross my finger. Yes, this time it has worked.
Okay, so now if I just type the name of my variable I should see that it points to my
smart contract. Yes, so here we can see that we have an address here so it's working. Okay, so
now the last thing we're going to do is we're going to call the get message method of this
smart contract and we're going to verify that we see the string that we passed in the constructor.
So for this I'm going to use this inst variable and then I'm going to
type the name of the method which is get message. I'm going to execute it and it's going to return
a promise and for this promise we're just going to console.log the result and we should see
the string that we passed in the constructor. Okay, so let's execute this and yes it does work.
Here we can see that what we have returned is this is my message. So we've succeeded. Congrats,
you've made it to the end. So let's quickly go through what we have learned. So first we saw
that truffle is a framework for developing smart contracts and it can be installed as an npm package.
Then in truffle there are four folders. First you have the contracts folder it's where you put your
solidity smart contract code. Then you have the migration folder where you put your migration file
that defines how to deploy your smart contract to the network. Then you have the test folder
where you can test your solidity contract. Then you have the build folder where you have a json
file that represents your compile contract. Then you have truffle.js which is the configuration
file. So the general procedure is like this. First you write your smart contract in a contract
directory. Then you write a migration file for your smart contract in a migrations directory.
Then you compile your contracts. Then you start a local test net. Then you run the migrations.
Finally once you've run the migrations you can interact with your smart contract in the truffle
console. The last thing I wanted to remind you is that truffle has several commands that you can run
from your terminal. So you just need to type truffle on your terminal and it will list you all
the available commands. So now you probably have a good idea of what is the general workflow with
truffle. So if you followed the previous episode you probably realized how helpful truffle can be.
So remember that without truffle you had a different library to manage and you had to do all
the plumbing yourself. So I'm talking about web3, the solidity compiler and things such as ABI
manipulation. So if you don't really see what I'm talking about make sure to watch the previous
episodes and I can guarantee you that after you will really appreciate truffle. So in the next
episode we're going to study contract inheritance. If you like this channel make sure to subscribe
or please give me a like. Bye-bye!
