Skip to content

Commit

Permalink
migrate truffle to hardhat
Browse files Browse the repository at this point in the history
Signed-off-by: Joshua Fernandes <joshua.fernandes@consensys.net>
  • Loading branch information
joshuafernandes committed Aug 28, 2023
1 parent 2654332 commit 8ab1957
Show file tree
Hide file tree
Showing 6 changed files with 181 additions and 216 deletions.
14 changes: 7 additions & 7 deletions docs/private-networks/how-to/configure/free-gas.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ When gas is free, limiting block and contract sizes is less important. In free g

### 1. Set the block size

If you want to remove gas from consideration and don't mind blocks potentially taking longer to create, in the genesis file set the block size limit (measured in gas) to the maximum accepted by Truffle (`0x1fffffffffffff`). In the genesis file, specify `gasLimit` following the `config` key.
If you want to remove gas from consideration and don't mind blocks potentially taking longer to create, in the genesis file set the block size limit (measured in gas) to the maximum accepted by Hardhat (`0x1fffffffffffff`). In the genesis file, specify `gasLimit` following the `config` key.

```json
{
Expand Down Expand Up @@ -107,21 +107,21 @@ If your network is configured to use the `londonBlock` or a later hard fork, the
}
```

## Configure free gas in Truffle
## Configure free gas in Hardhat

If using Truffle to develop on your free gas network, you also need to configure free gas in Truffle.
If using Hardhat to develop on your free gas network, you also need to configure free gas in Hardhat.

Like setting block and contract size limits to their maximum values for Besu, set the transaction gas limit in Truffle to the maximum possible.
Like setting block and contract size limits to their maximum values for Besu, set the transaction gas limit in Hardhat to the maximum possible.

:::info

Besu does not support private key management. To use Besu with Truffle, you must configure a [Truffle wallet](../../../public-networks/how-to/develop/truffle.md).
Besu does not support private key management. To use Besu with Hardhat, you must configure a [Hardhat wallet](../../../public-networks/how-to/develop/hardhat.md).

:::

### Update `truffle-config.js`
### Update `hardhat.config.js`

Update the `truffle-config.js` file:
Update the `hardhat.config.js` file:

1. Set the gas price to zero.

Expand Down
2 changes: 1 addition & 1 deletion docs/private-networks/how-to/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ The following features are shared with [public networks](../../public-networks/i
- [Manage JVM memory](../../public-networks/how-to/configure-jvm/manage-memory.md)
- [Use Java Flight Recorder](../../public-networks/how-to/configure-jvm/java-flight-recorder.md)
- Develop dapps:
- [Use Truffle](../../public-networks/how-to/develop/truffle.md)
- [Use Hardhat](../../public-networks/how-to/develop/hardhat.md)
- [Use client libraries](../../public-networks/how-to/develop/client-libraries.md)
- Troubleshoot:
- [Use EVM tool](../../public-networks/how-to/troubleshoot/evm-tool.md)
Expand Down
241 changes: 98 additions & 143 deletions docs/private-networks/tutorials/quickstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ This tutorial runs a private network suitable for education or demonstration pur
- Docker desktop configured to use the WSL2-based engine
- [Docker and Docker Compose](https://docs.docker.com/compose/install/)
- [Node.js](https://nodejs.org/en/download/) version 12 or higher
- [Truffle](https://www.trufflesuite.com/truffle)
- [Hardhat](https://hardhat.org/hardhat-runner/docs/getting-started#overview)
- [cURL command line](https://curl.haxx.se/download.html)
- [MetaMask](https://metamask.io/)

Expand Down Expand Up @@ -286,138 +286,96 @@ Besu doesn't incorporate [account management](../../public-networks/how-to/send-

## Smart contract and dapp usage

You can use a demo dapp called Pet Shop, provided by [Truffle](https://www.trufflesuite.com/tutorial).
You can use a demo dapp called QuorumToken which makes use of an ERC20 token that is deployed to the chain.

The dapp runs a local website using Docker, and uses smart contracts deployed on the network.
Behind the scenes uses [Hardhat](https://www.npmjs.com/package/hardhat), [Ethers](https://www.npmjs.com/package/ethers) and [MetaMask](https://metamask.io/) to interact with the chain. As such the process comprises two parts:

The directory created by `quorum-dev-quickstart` includes a `dapps` directory with a `pet-shop` subdirectory, which contains the source code for the dapp, including the smart contracts, website, and configurations to run this tutorial.
1. Deploy the contract to the chain and **save the contract's address**
2. Start the DApp and use the UI to read and transact on the chain with the token.

With the blockchain running and MetaMask connected to `Localhost 8545` via the browser, run the following command to start the Pet Shop dapp:
The `dapps/quorumToken` folder is this structured in this manner (only relevant paths shown):

```bash
cd dapps/pet-shop
./run_dapp.sh
quorumToken
├── hardhat.config.ts // hardhat network config
├── contracts // the QuorumToken.sol
├── scripts // handy scripts eg: to deploy to a chain
├── test // contract tests
└── frontend // DApp done in next.js
├── public
├── src
├── styles
├── tsconfig.json
```

The script:

1. Installs the dapp Node dependencies (you may see some warnings here, but it will not prevent the dapp from running).
1. Compiles the contracts.
1. Deploys the contracts to the blockchain.
1. Runs tests.
1. Builds and runs a Docker image to serve the dapp website.

```text './run_dapp.sh' example output
Compiling your contracts...
===========================
> Compiling ./contracts/Adoption.sol
> Compiling ./contracts/Migrations.sol
> Artifacts written to /Users/demo/quorum-test-network/dapps/pet-shop/pet-shop-box/build/contracts
> Compiled successfully using:
- solc: 0.5.16+commit.9c3226ce.Emscripten.clang
Starting migrations...
======================
> Network name: 'quickstartWallet'
> Network id: 1337
> Block gas limit: 16234336 (0xf7b760)
1_initial_migration.js
======================
Deploying 'Migrations'
----------------------
> transaction hash: 0xdd27f5bc5b0c4a42bb4f4d9ba00b4d33742de10ba8f03484cbf095ee824ba11a
> Blocks: 0 Seconds: 0
> contract address: 0xFB88dE099e13c3ED21F80a7a1E49f8CAEcF10df6
> block number: 2747
> block timestamp: 1618000437
> account: 0x627306090abaB3A6e1400e9345bC60c78a8BEf57
> balance: 89999.97435026
> gas used: 221555 (0x36173)
> gas price: 20 gwei
> value sent: 0 ETH
> total cost: 0.0044311 ETH
> Saving migration to chain.
> Saving artifacts
-------------------------------------
> Total cost: 0.0044311 ETH
2_deploy_contracts.js
=====================
Deploying 'Adoption'
--------------------
> transaction hash: 0xd6f5b11807a0727a92b6063c95b9101769d310592b0d3cf35d6df233d05d50e6
> Blocks: 0 Seconds: 0
> contract address: 0xf204a4Ef082f5c04bB89F7D5E6568B796096735a
> block number: 2749
> block timestamp: 1618000441
> account: 0x627306090abaB3A6e1400e9345bC60c78a8BEf57
> balance: 89999.968712
> gas used: 239915 (0x3a92b)
> gas price: 20 gwei
> value sent: 0 ETH
> total cost: 0.0047983 ETH
> Saving migration to chain.
> Saving artifacts
-------------------------------------
> Total cost: 0.0047983 ETH
Summary
=======
> Total deployments: 2
> Final cost: 0.0092294 ETH
Using network 'quickstartWallet'.
Compiling your contracts...
===========================
> Compiling ./test/TestAdoption.sol
TestAdoption
✓ testUserCanAdoptPet (2071ms)
✓ testGetAdopterAddressByPetId (6070ms)
✓ testGetAdopterAddressByPetIdInArray (6077ms)
3 passing (37s)
### Deploy the contract
Once the chain is up and running, enter the `quorumToken` folder and run the following:

```bash
# install dependencies
npm i
# compile the contract
npm run compile
npm run test
# deploy the contract to the quickstart network
npm run deploy-quorumtoken
```
with the output
```bash

After these tests are successful, the script builds a container for the Pet Shop dapp and deploys it, binding it to port 3001 on your system.
# compile
> quorumToken@1.0.0 compile
> npx hardhat compile

```text
Sending build context to Docker daemon 489.4MB
Step 1/5 : FROM node:12.14.1-stretch-slim
---> 2f7e25ad14ea
Step 2/5 : EXPOSE 3001
---> Using cache
---> 2ef0665a040a
Step 3/5 : WORKDIR /app
---> Using cache
---> e8e97cedb575
Step 4/5 : COPY . /app
---> f70e4265e598
Step 5/5 : CMD npm run dev
---> Running in 3c6e8bdb3f3b
Removing intermediate container 3c6e8bdb3f3b
---> ce2588e47ab0
Successfully built ce2588e47ab0
Successfully tagged quorum-dev-quickstart_pet_shop:latest
b1615ab765656bc027f63fc60019dba1ca572305766c820f41eaf113b7e14cf8
Generating typings for: 5 artifacts in dir: typechain-types for target: ethers-v6
Successfully generated 24 typings!
Compiled 5 Solidity files successfully

# test
> quorumToken@1.0.0 test
> npx hardhat test

QuorumToken
Deployment
✔ Should have the correct initial supply (1075ms)
✔ Should token transfer with correct balance (78ms)


2 passing (1s)

# deploy
Contract deploy at: 0x5FbDB2315678afecb367f032d93F642f64180aa3
```
This will deploy the contract to the network and return the address. **Please save this address for the next step.**

### Run the DApp

The dapp runs a local website using next.js, and uses the contract in the previous step deployed on the network.

With the blockchain running, and MetaMask connected to **Localhost 8545**, import one of [our test accounts via private key](../../reference/accounts-for-testing.md). Then run the following command:

```bash
cd frontend
npm i
npm run dev
```
This starts the DApp, binding it to port 3001 on your system.

In the browser where you have MetaMask enabled and one of the test accounts loaded, open a new tab and navigate to [the Pet Shop dapp](http://localhost:3001) where you can adopt lovely pets (sorry, not for real, it's a demo).
```text
When you select **Adopt**, a MetaMask window pops up and requests your permission to continue with the transaction.
> webapp@0.1.0 dev
> next dev -p 3001
After the transaction is complete and successful, the status of the pet you adopted shows **Success**.
- ready started server on [::]:3001, url: http://localhost:3001
- event compiled client and server successfully in 270 ms (18 modules)
- wait compiling...
- event compiled client and server successfully in 173 ms (18 modules)
```

![Dapp UI](../../assets/images/dapp-ui.png)
In the browser where you have MetaMask enabled and one of the test accounts loaded, open a new tab and navigate to [the QuorumToken dapp](http://localhost:3001).
Connect to MetaMask and input the address from the previous step. Fox example our contract above deployed to `0x5FbDB2315678afecb367f032d93F642f64180aa3`.

The DApp will then read the balance of the account from MetaMask and get details of the contract. You can then send funds to another address (any of the other test accounts) on the network and MetaMask will sign and send the transaction.

You can also search for the transaction and view its details in the [Block Explorer](http://localhost:25000/).

Expand All @@ -429,42 +387,39 @@ The MetMask UI also keeps a record of the transaction.

### Deploy your own dapp

You can deploy your own dapp to the Quorum Developer Quickstart, by configuring your dapp to point to the Quickstart network.
You can deploy your own dapp to the Quorum Developer Quickstart by configuring your dapp to point to the Quickstart network.

If you're using [Truffle](https://trufflesuite.com/truffle/), update the `networks` object in the [Truffle configuration file](https://trufflesuite.com/docs/truffle/reference/configuration#networks) to specify which networks to connect to for deployments and testing. The Quickstart RPC service endpoint is `http://localhost:8545`.
We recommend using [Hardhat](https://hardhat.org/hardhat-runner/docs/guides/project-setup), and you can use the sample `hardhat.config.js` to configure the networks object in the [Hardhat configuration file](https://hardhat.org/hardhat-network/docs/reference#config) to specify which networks to connect to for deployments and testing. The Quickstart's RPC service endpoint is `http://localhost:8545`.

For example, the following is the Truffle configuration file for the Pet Shop dapp used in the Quickstart Besu network:
For example, the following is the Hardhat configuration file for the QuorumToken dapp used in the Quickstart GoQuorum network:

```js
const PrivateKeyProvider = require("@truffle/hdwallet-provider");

// insert the private key of the account used in MetaMask, e.g. Account 1 (Miner Coinbase Account)
const privateKey =
"c87509a1c067bbde78beb793e6fa76530b6382a4c0241e5e4a9ec0a0f44dc0d3";

module.exports = {
networks: {
development: {
host: "127.0.0.1",
port: 7545,
network_id: "*", // Match any network id
},
develop: {
port: 8545,
},
quickstartWallet: {
provider: () =>
new PrivateKeyProvider(privateKey, "http://localhost:8545"),
network_id: "*",
// in built test network to use when developing contracts
hardhat: {
chainId: 1337
},
},
};
quickstart: {
url: "http://127.0.0.1:8545",
chainId: 1337,
// test accounts only, all good ;)
accounts: [
"0x8f2a55949038a9610f50fb23b5883af3b4ecb3c3bb792cbcefbd1542c692be63",
"0xc87509a1c067bbde78beb793e6fa76530b6382a4c0241e5e4a9ec0a0f44dc0d3",
"0xae6ae8e5ccbfb04590405997ee2d52d2b330726137b875053c36d94e974d162f"
]
}
},
defaultNetwork: "hardhat",
...
...
```
Deploy the dapp using:
Deploy the contract using:
```bash
truffle migrate --network quickstartWallet
npx hardhat run ./scripts/deploy_quorumtoken.ts --network quickstart
```
## Stop and restart the private network without removing containers
Expand Down
64 changes: 64 additions & 0 deletions docs/public-networks/how-to/develop/hardhat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
---
title: Use Hardhat
sidebar_position: 1
description: Using Hyperledger Besu with Hardhat
tags:
- public networks
- private networks
---

# Use Hardhat

Developing for Hyperledger Besu using Hardhat is the same as developing for public Ethereum networks using Hardhat. Hardhat supports Besu with the only difference being Besu does not support private key management. To use Besu with Hardhat, you must configure a wallet.

## Use a HD wallet

To add the wallet provider, update the `hardhat.config.ts` file in the project directory. Replace:

- `<JSON-RPC-http-endpoint>` with the JSON-RPC endpoint (IP address and port) of a Besu node.
- `<MY-ACCOUNT-MNEUMONIC>` with the list of words that make up your account's mneumonic.
- `<MY-PASSWORD`> your password if used
- `<MY-ACCOUNT-PRIVATE-KEY>` your account's private key

```js
module.exports = {
// See <https://hardhat.org/hardhat-runner/docs/config#hd-wallet-config>
// for more about customizing your Hardhat configuration!
networks: {
besuWallet: {
url: "<JSON-RPC-http-endpoint>",
accounts: {
mnemonic: "<ACCOUNT-MNEUMONIC>",
path: "m/44'/60'/0'/0",
initialIndex: 0,
count: 1,
passphrase: "",
},
},
},
};
```

## Alternatively, via a private key in code

Please make sure you do not commit and private keys to source control like Github, always inject your keys at runtime as env vars, or use the likes of a vault.

```js
const provider = new ethers.JsonRpcApiProvider(<JSON-RPC-http-endpoint>);
const wallet = new ethers.Wallet(<MY-ACCOUNT-PRIVATE-KEY>);
// connect the wallet to the provider
const signer = wallet.connect(provider);

```

## Start a Besu node

Start a Besu node with JSON-RPC enabled on the endpoint specified in the Hardhat configuration file.

## Deploy a contract

To deploy a contract onto the Besu network:

```bash
npx hardhat scripts run ./scripts/deploy_my_contract.ts --network besuWallet
```
Loading

0 comments on commit 8ab1957

Please sign in to comment.