Skip to content

udumulanaveen/Smart-Contract-with-Truffle

Repository files navigation

Truffle is used it to develop your smart contracts, lot of people have used remix in the past to quickly build smart contracts and deploy them and test them and truffle is of the next step if you want to go a little bit more advanced tool set of smart contracts working together interacting with external contract. then we really need a tool like truffle. Installing truffle installing truffle and building and deploying our first smart contract using truffle just have a quick look at their homepage trufflesuite.com here we go sweet tools for smart contracts the travel suite gets developers from idea to dab as comfortably as possible and this is true in a sense but truffle is primarily used for building smart contracts

i would say that if we're going to build an entire adapt you need travel but we also need better tools for building the actual dap the front and the actual application we use it to be able to compile our smart contracts locally to be able to deploy them to either a network just local on our machine or testnet or a mainnet we use it for testing our smart contracts to write unit tests to make sure that the logic in our smart contract works the way we want it to before we deploy any of it to the mainnet so that's what we're going to do in. we're going to start by installing it so copy this here npm install travel dash g and then go into your you know

editor of choice i'm going to go into visual studio code and here i've created a new folder called truffle i'm going to open up the terminal here and we're going to install this using npm and of course to do this you need the node package manager installed you can get it if we install node you will get known package manager so that we can use that in your terminal and here we're going to paste npm install trouble just g i already have this installed so the output for me might look a little bit different from the output for you i'm also going to make this bigger can i do that here yes there we go and this can take a little while for it to install.

your directory here with the basic stuff we to set up a truffle work environment right so we have a folder called contract we have a folder called migrations we have a folder called test and then we have travel config file how we build and compile contracts using truffle and you will already have one contract as standard here and this is the contract and this is a contract that truffle creates for you in order to keep track of the last migration you did because if you're building out a you know large ecosystem of contrast then this will keep track you know if you're continuing to extend it you might deploy multiple contracts after each other and this will keep track of which contract you deployed um you know in your in your last migration.

which one they should run now and so this is just standard stuff that goes together with this first migration file that just deploys the migration contract i'll get more into detail of this later but for now you just need to know that this is a standard contract you don't need to mess with it in any way you don't even need to understand it at this point all you need to know is that it's there from the beginning created by truffle in order to create a new smart contract in our travel contracts folder we click new file and we create let's say hello world dot sol whatever you want right your contract should be created in here and then here um your code just like normal primal solidity let's see what version i'm on currently probably ..

i'll create my contact here and in order to get this nice syntax highlighting if you haven't done that already you can go into plugins here and you can install the solidity plugin by one blanco and you will get this nice syntax highlighting you'll get snippets and a bunch of stuff so that's nice if you want to do that in visual studio code if you have any other editor i'm sure there are plugins to give you solidity linters and syntax highlighting and so on but here nothing is different from remix or any other dev tools for solidity just build your smart contract so will function hello can be public and let's see it is view or rather pure and it returns let's say string memory this will return hello world

In order to compile this you write travel compile this will compile your contract and see so that all of your code is correct and now and it will compile all of the contracts that are in your contract folder and now you can see that it compiled successfully using this compiler version solidity call was processed down into helloworld.json file inside this new folder that was created build.contracts and here we have all of the code that is needed for truffle to deploy our contract to any ethereum blockchain. there's an initial migration file as i imports the migrations solidity contract they call it migrations and then they just run deploy.deploy migrations and we need to have this structure to our file or we have module.

exports and this is just a node thing and inside here so we do our actual deploy logic so we'll start by cloning this file hello world migration and the is actually important because the number at the beginning of this file determines of what order truffle will deploy this contract and we always want it to be deployed the initial migration should always be deployed as number one and then now since we only have another contract we want that to be number two the only thing we need to change here is that we want to import instead of the migrations contract we want to import the hello world contract we can rename this as well to hello world this variable this constant and this name here doesn't come from the file name of the contract it comes from this contract name right here that's what you need to specify okay and then we just change here so that we want to deploy this contract instance hello world

or rather this and then we just need to change here so that we want to deploy hello world which is the imported contract here and this will actually deploy this contract and now if we just run this in its most basic mode it will deploy to a local dev chain a local ethereum you know virtual machines or off that we have um just on our computer so it won't be to any blockchain that is available for anyone else we can use it for testing and we do that by writing truffle migrate that will run all of our migration files and it will first compile all of our contracts and then it will migrate it .

we'll actually connect to this local instance and then deploy the contracts to it and there you can see it just running it first runs initial migration deploying the migrations contract and you can see a bunch of details about it transaction hash contract address block number which is one because this is a local chain block timestamp account balance gas used all of the stuff same thing goes for hello world we just deployed afterwards called our cost you see here uh total deployments too and now we're ready to interact and test out our contracts

we interact with our contract here in the console because you're used to just doing it in remix maybe clicking a button and then you run the function here it's a little bit more complicated so first of all we need to get an a contract instance meaning a javascript object that represents our contract so we'll set we'll create a variable here in the console called uh hello uh or just call it instance maybe is equal to await hello hello world capital h same thing as we specified in the migrations file here or rather here hello world dot deployed and then a function call okay like that and now we can run instance dot hello which was our function that will return the string

we set in our contract like that and that's all the interaction we can do now in our most basic contributions we just have one function and that's a pure function so it doesn't set anything doesn't change anything it just returns a string and that's how we interact with contract right we just do this to get the contract instance and then we call the function

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published