4

I had been watching a course online, where I learnt that we can compile & deploy any smart contract to blockchain and then with web3.js we can interact with the smart contract. So why do we need something like truffle or hardhat when we can actually build anything such as a Dapp? Please tell the advantages?

2
  • Hardhat/Truffle are used to compile and deploy the contracts
    – 0xSanson
    Dec 8, 2022 at 12:00
  • I think by using web3.js alone too, you can compile & deploy the smart contract.
    – MT 16
    Dec 9, 2022 at 14:02

2 Answers 2

5

I had used both Truffle, Brownie && Hardhat for SmartContract development. All of them are good, and they serve their purpose.

The reason you need these tools comes down to the fact that Blockchain development is very specific.

  • 1. You need a running blockchain to which you can deploy and test your code.

  • 2. You also need a way to manage your blockchain networks (to decide where you want to deploy your contracts)

  • 3. You want to estimate the cost/gas of your function easily

  • 4. In some tests, you might want to jump ahead in time on blockchain. (To test function that locks up funds for a year or something like that)

  • 5. There are likey hundreds more of specific cases for which regular development tools would not be of much use because they were not made for a blockchain development

Luckily... Truffle, Brownie, and Hardhat all have an inbuilt functionality that is ready for your use out of the box.

Which one to use is up to you.

  • 1. Truffle is JavaScript based tool, good overall

  • 2. Brownie is Python based (You write tests and deployment scripts in Python instead of JS)

  • 3. Hardhat is also JS based, but you can write your tests and scripts in TypeScript too.

However, as someone who had used (and is still using) all three of those extensively. I would go for a Hardhat (and I say that even though I love Brownie). But in my experience, the Hardhat is the most forgiving to newcomers. Plus, it has amazing libraries that make your life easier.

For example, I got this when I ran a test on one of my Smart Contracts. enter image description here

(This was just an example to showcase it here). But you can clearly see how the OrganizationManager contract has addOrganization() .

We also see its cost in terms of gas, + the deployment cost of the Contract itself. A rather helpful feature for any blockchain developer.

2
  • 1
    Hey, thanks for this descriptive answer. Btw I still don't get how truffle and hardhat actually help you estimate the gas? Can you please elaborate on that? Thanks
    – MT 16
    Dec 9, 2022 at 13:59
  • Yeah. When writing smartContracts you will also need to write the tests for all the functionality. Running tests with Hardhat/Truffle/Brownie allows you to get data on the gas cost of each function in your smart Contracts. I will post an example in my answer.
    – Sky
    Dec 9, 2022 at 14:07
2

Assuming the last line of your question is "when we can't actually build anything such as a Dapp"

So, in Blockchain development, testing on real environments can be costly. Since you mentioned smart contracts, you must be referring to Solidity. Now, on Ethereum Blockchain, performing any transaction or even deploying your contract requires gas. So, in cases where you don't know whether your smart contract is truly working well. It is stupid to post you code on the main blockchain as it cannot be erased, will take up space on the main chain and will also cost you money.

Hardhat and Truffle are like frameworks that allow you to test your blockchain code on a "test" network. You may say that we have Remix to test our code but that only runs on local environment and does not have Web3 functionality integrated on it. You cannot tell if nodes on other systems will be able to connect to your blockchain node. You also can't run JavaScript Tests.

Hardhat and Truffle allow us to go beyond these limitations.

Do let me know if you have further questions. We both could also explore and learn together

4
  • 1
    Hey Ishan, thanks for a great answer. This is what I was looking for. However, I have this another doubt of why do we actually write tests for the smart contract when we can actually check for those testing cases on remix. This is something I still do not get.
    – MT 16
    Dec 9, 2022 at 13:57
  • How about we make this as another question? I just realized the answer (PS: I had the same doubt too) To create another question so that others who have the same thought will be able to find the answer more efficiently? Or.. I could tell here too, In JS Tests, you can have 1(or more or all) test fail and still continue on with the rest of code execution. This is not possible in solidity. (With this feature you can pinpoint on what exactly went wrong or is missing)
    – Ishan
    Dec 11, 2022 at 7:36
  • Sure, I will add a separate question for this and you can answer there. Btw thanks for the answer.
    – MT 16
    Dec 11, 2022 at 9:15
  • Just realised that a very similar question has already been asked. Here's the link: ethereum.stackexchange.com/questions/119992/…
    – MT 16
    Dec 11, 2022 at 9:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.