Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Learn guides to use Hardhat #258

Merged
merged 7 commits into from
Nov 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ In this guide, we will use our beloved xref:developing-smart-contracts.adoc#box-

Remember that deploying to a public test network is a necessary step when developing an Ethereum project. They provide a safe environment for testing that closely mimics the main network - you don't want to take out your project for a test drive in a network where mistakes will cost you and your users money!

include::learn::partial$buidler-truffle-toggle.adoc[]
include::learn::partial$hardhat-truffle-toggle.adoc[]

[[testnet-list]]
== Available testnets
Expand Down Expand Up @@ -111,11 +111,11 @@ We need to update our configuration file with a new network connection to the te
NOTE: See the `HDWalletProvider` https://github.com/trufflesuite/truffle/tree/master/packages/hdwallet-provider[documentation] for information on configuration options.
--

[.buidler]
[.hardhat]
--
[source,diff]
----
// buidler.config.js
// hardhat.config.js
+ const { alchemyApiKey, mnemonic } = require('./secrets.json');
...
module.exports = {
Expand All @@ -129,7 +129,7 @@ NOTE: See the `HDWalletProvider` https://github.com/trufflesuite/truffle/tree/ma
};
----

NOTE: See the Buidler https://buidler.dev/config/#json-rpc-based-networks[networks configuration] documentation for information on configuration options.
NOTE: See the Hardhat https://hardhat.org/config/#json-rpc-based-networks[networks configuration] documentation for information on configuration options.
--
Note in the first line that we are loading the project id and mnemonic from a `secrets.json` file, which should look like the following, but using your own values. Make sure to `.gitignore` it to ensure you don't commit secrets to version control!

Expand Down Expand Up @@ -157,12 +157,11 @@ truffle(rinkeby)> accounts
----
--

[.buidler]
[.hardhat]
--
[source,console]
----
$ npx buidler console --network rinkeby
All contracts have already been compiled, skipping compilation.
$ npx hardhat console --network rinkeby
> accounts = await ethers.provider.listAccounts()
[ '0xEce6999C6c5BDA71d673090144b6d3bCD21d13d4',
'0xC1310ade58A75E6d4fCb8238f9559188Ea3808f9',
Expand All @@ -179,7 +178,7 @@ truffle(rinkeby)> await web3.eth.getBalance(accounts[0])
----
--

[.buidler]
[.hardhat]
--
[source,console]
----
Expand Down Expand Up @@ -220,14 +219,13 @@ $ npx truffle migrate --network rinkeby
----
--

[.buidler]
[.hardhat]
--
[source,console]
----
$ npx buidler run --network rinkeby scripts/deploy.js
All contracts have already been compiled, skipping compilation.
$ npx hardhat run --network rinkeby scripts/deploy.js
Deploying Box...
Box deployed to: 0xc4B718c07971C96641478D146aA1f411b714b366
Box deployed to: 0x7e25FaDC15EBc59c7b4EFEd313A95A2955bA73E2
----
--

Expand All @@ -240,9 +238,9 @@ You can see your contract on a block explorer such as https://etherscan.io/[Ethe
TIP: You can check out the contract we deployed in the example above, along with all transactions sent to it, https://rinkeby.etherscan.io/address/0xA4D767f2Fba05242502ECEcb2Ae97232F7611353[here].
--

[.buidler]
[.hardhat]
--
TIP: You can check out the contract we deployed in the example above, along with all transactions sent to it, https://rinkeby.etherscan.io/address/0xc4B718c07971C96641478D146aA1f411b714b366[here].
TIP: You can check out the contract we deployed in the example above, along with all transactions sent to it, https://rinkeby.etherscan.io/address/0x7e25FaDC15EBc59c7b4EFEd313A95A2955bA73E2[here].
--

You can also interact with your instance as you regularly would, either using the xref::deploying-and-interacting.adoc#interacting-from-the-console[console], or xref::deploying-and-interacting.adoc#interacting-programatically[programmatically].
Expand All @@ -262,18 +260,18 @@ truffle(rinkeby)> (await box.retrieve()).toString()
```
--

[.buidler]
[.hardhat]
--
```console
$ npx buidler console --network rinkeby
$ npx hardhat console --network rinkeby
All contracts have already been compiled, skipping compilation.
> const Box = await ethers.getContractFactory("Box")
undefined
> const box = await Box.attach("0xc4B718c07971C96641478D146aA1f411b714b366")
> const box = await Box.attach("0x7e25FaDC15EBc59c7b4EFEd313A95A2955bA73E2")
undefined
> await box.store(42)
{ hash:
'0x7dcc0ae058c88f1490f4f9dbf918fb6ae8f710676fb9a15e2e04e72223d042df',
'0xc2b34ff678f71df463856d2183ec5644dac8d57806461fcf9417b72824e4de06',
...
> (await box.retrieve()).toString()
'42'
Expand Down
100 changes: 48 additions & 52 deletions components/learn/modules/ROOT/pages/deploying-and-interacting.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ This guide will cover all you need to know to get you started using your contrac
* <<interacting-from-the-console, Interacting from the Console>>
* <<interacting-programmatically, Interacting Programmatically>>

include::learn::partial$buidler-truffle-toggle.adoc[]
include::learn::partial$hardhat-truffle-toggle.adoc[]

[[local-blockchain]]
== Setting up a Local Blockchain
Expand Down Expand Up @@ -43,21 +43,21 @@ Keep in mind that every time you run Ganache, it will create a brand new local b
TIP: **Truffle** has a graphical version of `ganache-cli`, also called https://www.trufflesuite.com/ganache[Ganache].
--

[.buidler]
[.hardhat]
--
Buidler comes with a local blockchain built-in, the https://buidler.dev/buidler-evm/[Buidler EVM].
Hardhat comes with a local blockchain built-in, the https://hardhat.org/hardhat-network/[Hardhat Network].

Upon startup, Buidler EVM will create a set of unlocked accounts and give them Ether.
Upon startup, Hardhat Network will create a set of unlocked accounts and give them Ether.

```console
$ npx buidler node
$ npx hardhat node
```

Buidler EVM will print out a list of available accounts and their private keys, along with its address, `http://127.0.0.1:8545`.
Hardhat Network will print out its address, `http://127.0.0.1:8545`, along with a list of available accounts and their private keys.

Keep in mind that every time you run Buidler EVM, it will create a brand new local blockchain - the state of previous runs is **not** preserved. This is fine for short-lived experiments, but it means that you will need to have a window open running Buidler EVM for the duration of these guides.
Keep in mind that every time you run Hardhat Network, it will create a brand new local blockchain - the state of previous runs is **not** preserved. This is fine for short-lived experiments, but it means that you will need to have a window open running Hardhat Network for the duration of these guides.

TIP: Buidler will always spin up an instance of **Buidler EVM** when no network is specified and there is no default network configured or the default network is set to `buidlerevm`.
TIP: Hardhat will always spin up an instance of **Hardhat Network** when no network is specified and there is no default network configured or the default network is set to `hardhat`.
--

NOTE: You can also run an actual Ethereum node in _https://geth.ethereum.org/getting-started/dev-mode[development] https://wiki.parity.io/Private-development-chain[mode]_. These are a bit more complex to set up, and not as flexible for testing and development, but are more representative of the real network.
Expand Down Expand Up @@ -87,9 +87,9 @@ module.exports = async function (deployer) {
```
--

[.buidler]
[.hardhat]
--
Buidler doesn't currently have a native deployment system, instead we use https://buidler.dev/guides/deploying.html[scripts] to deploy contracts.
Hardhat doesn't currently have a native deployment system, instead we use https://hardhat.org/guides/deploying.html[scripts] to deploy contracts.

We will create a script to deploy our Box contract. We will save this file as `scripts/deploy.js`.

Expand All @@ -112,10 +112,10 @@ main()
});
```

We use https://github.com/ethers-io/ethers.js[ethers] in our script, so we need to install it and the https://buidler.dev/plugins/nomiclabs-buidler-ethers.html[buidler-ethers plugin].
We use https://github.com/ethers-io/ethers.js[ethers] in our script, so we need to install it and the https://hardhat.org/plugins/nomiclabs-hardhat-ethers.html[@nomiclabs/hardhat-ethers plugin].

```console
$ npm install --save-dev @nomiclabs/buidler-ethers ethers
$ npm install --save-dev @nomiclabs/hardhat-ethers ethers
```
--

Expand All @@ -138,13 +138,13 @@ module.exports = {
```
--

[.buidler]
[.hardhat]
--
We need to add in our https://buidler.dev/config[configuration] that we are using the `buidler-ethers` plugin.
We need to add in our https://hardhat.org/config/[configuration] that we are using the `@nomiclabs/hardhat-ethers` plugin.

```js
// buidler.config.js
usePlugin('@nomiclabs/buidler-ethers');
// hardhat.config.js
require('@nomiclabs/hardhat-ethers');

module.exports = {
...
Expand Down Expand Up @@ -192,18 +192,17 @@ Starting migrations...
NOTE: Truffle will keep track of your deployed contracts, but it also displays their addresses when deploying (in our example, `0xCfEB869F69431e42cdB54A4F4f105C19C080A601`). These values will be useful when interacting with them programmatically.
--

[.buidler]
[.hardhat]
--
Using the `run` command, we can deploy the `Box` contract to the local network (<<local-blockchain, Buidler EVM>>):
Using the `run` command, we can deploy the `Box` contract to the local network (<<local-blockchain, Hardhat Network>>):

```console
$ npx buidler run --network localhost scripts/deploy.js
All contracts have already been compiled, skipping compilation.
$ npx hardhat run --network localhost scripts/deploy.js
Deploying Box...
Box deployed to: 0x7c2C195CD6D34B8F845992d380aADB2730bB9C6F
Box deployed to: 0x5FbDB2315678afecb367f032d93F642f64180aa3
```

NOTE: Buidler doesn't keep track of your deployed contracts. We displayed the deployed address in our script (in our example, `0x7c2C195CD6D34B8F845992d380aADB2730bB9C6F`). This will be useful when interacting with them programmatically.
NOTE: Hardhat doesn't keep track of your deployed contracts. We displayed the deployed address in our script (in our example, `0x7c2C195CD6D34B8F845992d380aADB2730bB9C6F`). This will be useful when interacting with them programmatically.
--

All done! On a real network this process would've taken a couple of seconds, but it is near instant on local blockchains.
Expand All @@ -228,18 +227,17 @@ undefined
```
--

[.buidler]
[.hardhat]
--
We will use the https://buidler.dev/guides/buidler-console.html[Buidler console] to interact with our deployed `Box` contract on our localhost network.
We will use the https://hardhat.org/guides/hardhat-console.html[Hardhat console] to interact with our deployed `Box` contract on our localhost network.

NOTE: We need to specify the address of our `Box` contract we displayed in our deploy script.

```console
$ npx buidler console --network localhost
All contracts have already been compiled, skipping compilation.
$ npx hardhat console --network localhost
> const Box = await ethers.getContractFactory("Box")
undefined
> const box = await Box.attach("0x7c2C195CD6D34B8F845992d380aADB2730bB9C6F")
> const box = await Box.attach("0x5FbDB2315678afecb367f032d93F642f64180aa3")
undefined
```
--
Expand All @@ -265,12 +263,12 @@ truffle(development)> await box.store(42)
Notice how the transaction receipt also shows that `Box` emitted a `ValueChanged` event.
--

[.buidler]
[.hardhat]
--
```console
> await box.store(42)
{ hash:
'0xb1d8a8e227110b3a808cb393fff5010344a8b328f16f5994b838e6394af46bd9',
'0xa61ed89d6bd21a6182df28e557c241a3dd5aa932d1689f9c5bf8c49039d2334a',
...
```
--
Expand All @@ -287,7 +285,7 @@ truffle(development)> await box.retrieve()
```
--

[.buidler]
[.hardhat]
--
```console
> await box.retrieve()
Expand All @@ -307,7 +305,7 @@ truffle(development)> (await box.retrieve()).toString()
```
--

[.buidler]
[.hardhat]
--
```console
> (await box.retrieve()).toString()
Expand All @@ -322,9 +320,9 @@ truffle(development)> (await box.retrieve()).toString()
TIP: To learn more about using the console, check out the https://www.trufflesuite.com/docs/truffle/getting-started/using-truffle-develop-and-the-console[Truffle documentation].
--

[.buidler]
[.hardhat]
--
TIP: To learn more about using the console, check out the https://buidler.dev/guides/buidler-console.html[Buidler documentation].
TIP: To learn more about using the console, check out the https://hardhat.org/guides/hardhat-console.html[Hardhat documentation].
--

[[interacting-programmatically]]
Expand All @@ -337,9 +335,9 @@ The console is useful for prototyping and running one-off queries or transaction
In this section, we'll see how to interact with our contracts from JavaScript, and use https://www.trufflesuite.com/docs/truffle/getting-started/writing-external-scripts[Truffle to execute our script] with our Truffle configuration.
--

[.buidler]
[.hardhat]
--
In this section, we'll see how to interact with our contracts from JavaScript, and use https://buidler.dev/guides/scripts.html[Buidler to run our script] with our Buidler configuration.
In this section, we'll see how to interact with our contracts from JavaScript, and use https://hardhat.org/guides/scripts.html[Hardhat to run our script] with our Hardhat configuration.
--

TIP: Keep in mind that there are many other JavaScript libraries available, and you can use whichever you like the most. Once a contract is deployed, you can interact with it through any library!
Expand Down Expand Up @@ -367,7 +365,7 @@ module.exports = async function main(callback) {
----
--

[.buidler]
[.hardhat]
--
[source,js]
----
Expand Down Expand Up @@ -398,7 +396,7 @@ console.log(accounts)
----
--

[.buidler]
[.hardhat]
--
[source,js]
----
Expand All @@ -425,17 +423,16 @@ Using network 'development'.
----
--

[.buidler]
[.hardhat]
--
Run the code above using `buidler run`, and check that you are getting a list of available accounts in response.
Run the code above using `hardhat run`, and check that you are getting a list of available accounts in response.

[source,console]
----
$ npx buidler run --network localhost ./scripts/index.js
All contracts have already been compiled, skipping compilation.
[ '0xc783df8a850f42e7F7e57013759C285caa701eB6',
'0xeAD9C93b79Ae7C1591b1FB5323BD777E86e150d4',
... ]
$ npx hardhat run --network localhost ./scripts/index.js
[ '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266',
'0x70997970C51812dc3A010C7d01b50e0d17dc79C8',
... ]
----
--

Expand All @@ -456,7 +453,7 @@ const box = await Box.deployed();
----
--

[.buidler]
[.hardhat]
--
In order to interact with the <<box-contract,`Box`>> contract we deployed, we'll use an https://docs.ethers.io/v5/api/contract/contract/[ethers contract instance].

Expand Down Expand Up @@ -492,7 +489,7 @@ console.log("Box value is", value.toString());
----
--

[.buidler]
[.hardhat]
--
We'll need to call the read only `retrieve()` public method of the contract, and https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await[await] the response:

Expand All @@ -516,12 +513,11 @@ Box value is 42
----
--

[.buidler]
[.hardhat]
--
[source,console]
----
$ npx buidler run --network localhost ./scripts/index.js
All contracts have already been compiled, skipping compilation.
$ npx hardhat run --network localhost ./scripts/index.js
Box value is 42
----
--
Expand Down Expand Up @@ -555,7 +551,7 @@ console.log("Box value is", value.toString());
NOTE: In a real-world application, you may want to https://web3js.readthedocs.io/en/1.0/web3-eth-contract.html#methods-mymethod-estimategas[estimate the gas] of your transactions, and check a https://ethgasstation.info/[gas price oracle] to know the optimal values to use on every transaction.
--

[.buidler]
[.hardhat]
--

[source,js]
Expand Down Expand Up @@ -584,11 +580,11 @@ Box value is 23
----
--

[.buidler]
[.hardhat]
--
[source,console]
----
$ npx truffle exec --network development ./scripts/index.js
$ npx hardhat run --network localhost ./scripts/index.js
Using network 'development'.

Box value is 23
Expand Down