Skip to content

Commit

Permalink
Deploying with Hardhat (#7)
Browse files Browse the repository at this point in the history
* First draft of deploying with Hardhat

* this is JS, not TS

* Use .env
  • Loading branch information
timothymcmackin authored Jan 24, 2024
1 parent aff2bf1 commit ae2bf94
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
79 changes: 79 additions & 0 deletions docs/get-started/deploy-with-hardhat.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
---
title: '👷 Deploy to Etherlink with Hardhat'
---

[Hardhat](https://hardhat.org/) is a suite of tools for working with Ethereum contracts and dApps.
You can use Hardhat to deploy contracts to Etherlink just like you use it on other networks:

1. Set up a new Hardhat project by running `npx hardhat init`.
1. Add Etherlink as a network by putting the Etherlink RPC node and your private key in the `hardhat.config.js` file, as in this example:

```javascript
import "@nomicfoundation/hardhat-toolbox";
import 'dotenv/config';

const config = {
solidity: "0.8.19",
networks: {
etherlinkTest: {
url: "https://node.ghostnet.etherlink.com",
accounts: [process.env.MY_PRIVATE_KEY],
}
}
};

export default config;
```

1. Create a contract or use the default contract in the `contracts` folder.
1. Compile the contract by running `npx hardhat compile`.
1. Update the default `scripts/deploy.js` file to deploy your contract.
For example, if your contract is named "MyContract.sol" and accepts a string value and an integer value in the constructor, the deployment script looks like this example:

```javascript
import { ethers } from "hardhat";

async function deployMyContract() {
const deployedContract = await ethers.deployContract("MyContract", ["Hello", 5]);
await deployedContract.waitForDeployment();
}

deployMyContract().catch((error) => {
console.error(error);
process.exitCode = 1;
});
```

1. Run the deployment script on the Etherlink network with this command:

```bash
npx hardhat run --network etherlinkTest scripts/deploy.js
```

This command refers to the `etherlinkTest` network in the Hardhat config file.

1. Copy the address of the deployed contract from the log.

Now you can interact with the contract from Hardhat.
For example, if the contract has a function named "DoSomething," you can call it with this code:

```javascript
import { ethers } from "hardhat";

async function callMyContract() {
const MyContract = await ethers.getContractFactory("MyContract");
const contract = MyContract.attach(
// The deployed contract address
"0xcDd461a8dD3d519111Fe7a4ac2b9563AcEbF5B7c"
);

await contract.DoSomething();
}

callMyContract().catch((error) => {
console.error(error);
process.exitCode = 1;
});
```

Then you can run this script on the Etherlink network with the command `npx hardhat run --network etherlinkTest scripts/myScript.js`.
1 change: 1 addition & 0 deletions sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const sidebars = {
items: [
'get-started/connect-your-wallet-to-etherlink',
'get-started/get-testnet-xtz-on-etherlink',
'get-started/deploy-with-hardhat',
],
},
{
Expand Down

0 comments on commit ae2bf94

Please sign in to comment.