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

new hardhat guides + collapsed all #618

Merged
merged 7 commits into from
Sep 7, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/guides/browser-extension/_category_.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
label: '🧩 Browser Extension'
collapsed: false
collapsed: true
position: 2
3 changes: 3 additions & 0 deletions docs/guides/hardhat-walkthrough/_category_.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
label: '✏️ Hardhat Walkthrough'
kalote marked this conversation as resolved.
Show resolved Hide resolved
collapsed: true
position: 3
37 changes: 37 additions & 0 deletions docs/guides/hardhat-walkthrough/create-custom-lsp7.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
sidebar_label: Create a custom LSP7 contract
sidebar_position: 2
---

# Create a custom LSP7 contract

Following the previous guide on ["How to setup a hardhat base setup"](./hardhat-base-setup.md), we will now create a custom [LSP7 contract](../../standards/nft-2.0/LSP7-Digital-Asset.md).

This contract will extend LSP7Mintable & [LSP7Burnable](../../contracts/contracts/LSP7DigitalAsset/extensions/LSP7Burnable.md) (to allow burning tokens). We will also premint 20k tokens to the owner of the contract (the deployer).
To do that, delete the `Lock.sol` contract in the `contracts/` folder, then create a new file named `MyCustomToken.sol` with the following content:

```solidity title="contracts/MyCustomToken.sol"
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.9;

import "@lukso/lsp-smart-contracts/contracts/LSP7DigitalAsset/presets/LSP7Mintable.sol";
import "@lukso/lsp-smart-contracts/contracts/LSP7DigitalAsset/extensions/LSP7Burnable.sol";

contract CustomToken is LSP7Mintable, LSP7Burnable {
// parameters for LSP7Mintable constructor are:
// token name,
// token symbol,
// token owner,
// boolean isNonDivisible
// for more informations, check https://github.com/lukso-network/LIPs/blob/main/LSPs/LSP-7-DigitalAsset.md
constructor() LSP7Mintable("My Custom Token", "MCT", msg.sender, false) {
mint(msg.sender, 20000 * 10**decimals(), true, '0x' );
}
}
```

We are now ready to build it using the command:

```bash
npm run build
```
172 changes: 172 additions & 0 deletions docs/guides/hardhat-walkthrough/deploy-custom-lsp7.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
---
sidebar_label: Deploy our custom LSP7 contract
sidebar_position: 3
---

# Deploy your contract

Following the previous guide (["Create a custom LSP7 contract"](./create-custom-lsp7.md)), we are now ready to deploy it on the LUKSO Testnet network!

## Deploy the contract on LUKSO Testnet

In order to deploy the contract, we will have to update the `hardhat.config.ts` and create a deploy script. Let's go!

### Update hardhat config

Jump in the `hardhat.config.ts` and update the file with this:

```ts title="hardhat.config.ts"
import { HardhatUserConfig } from 'hardhat/config';
import { config as LoadEnv } from 'dotenv';
import '@nomicfoundation/hardhat-toolbox';

LoadEnv();

const config: HardhatUserConfig = {
solidity: '0.8.9',
networks: {
luksoTestnet: {
url: 'https://rpc.testnet.lukso.network',
chainId: 4201,
accounts: [process.env.PRIVATE_KEY as string],
},
},
};

export default config;
```

### Create a deploy script

We will create a script to deploy the smart contract to the LUKSO Testnet network. You can either use a regular EOA (Externally Owned Account) or a Universal Profile. Let's see those 2 possibilities below.

#### Deploy using a Universal Profile (Recommended)

In this chapter, we are going to deploy our contract using our Universal Profile. First thing is to [Install the UP browser extension](../../guides/browser-extension/install-browser-extension.md). Once installed, we will retrieve the information we need:

- Click on the extension
- Click on the cogwheel ⚙️ at the top right corner, then select "reveal private keys"
- Enter your password
- Scroll down and copy the `privateKey` field to your `.env` file in `PRIVATE_KEY`
- Copy the `address` field to your `.env` file in `UP_ADDR`

:::note

The `privateKey` coming from your UP extension is the private key of the EOA that controls your UP (more information about controllers can be found in the [Key Manager](../../standards/universal-profile/lsp6-key-manager.md) page). You can find the associated address in the extension if you click on the controller tab > UP Extension. This address will need to be funded using the [Testnet Faucet](https://faucet.testnet.lukso.network/).

:::

Now that we are all set up, we will create the script that will deploy the contract as your Universal Profile. In order to do so, we will:

1. Create a wallet instance with our private key (the `signer`)
2. Load the associated UP
3. Get the bytecode of our contract
4. use `staticCall` method to get the address of the contract
5. deploy the contract

Go in the `scripts/` folder and create a file named `deployUP.ts` with the following content:
Hugoo marked this conversation as resolved.
Show resolved Hide resolved

```ts title="scripts/deployUP.ts"
import hre from 'hardhat';
import { ethers } from 'hardhat';
import * as dotenv from 'dotenv';
import * as LSP0ABI from '@lukso/lsp-smart-contracts/artifacts/LSP0ERC725Account.json';

// load env vars
dotenv.config();

async function main() {
// setup provider
const provider = new ethers.JsonRpcProvider(
'https://rpc.testnet.lukso.network',
);
// setup signer (the browser extension controller)
const signer = new ethers.Wallet(process.env.PRIVATE_KEY as string, provider);
console.log('Deploying contracts with EOA: ', signer.address);

// load the associated UP
const UP = new ethers.Contract(
process.env.UP_ADDR as string,
LSP0ABI.abi,
signer,
);

/**
* Custom LSP7 Token
*/
const CustomTokenBytecode =
hre.artifacts.readArtifactSync('CustomToken').bytecode;

// get the address of the contract that will be created
const CustomTokenAddress = await UP.connect(signer)
.getFunction('execute')
.staticCall(1, ethers.ZeroAddress, 0, CustomTokenBytecode);

// deploy CustomLSP7 as the UP (signed by the browser extension controller)
const tx1 = await UP.connect(signer).getFunction('execute')(
1,
ethers.ZeroAddress,
0,
CustomTokenBytecode,
);

await tx1.wait();

console.log('Custom token address: ', CustomTokenAddress);
}

main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});
```

Now, you can deploy the contract using:

```bash
npx hardhat --network luksoTestnet run scripts/deployUP.ts
```

#### Deploy using an EOA

Deploying with an EOA is definitively more intuitive and straight forward, but you miss on the Universal Profile features. To do so, we will need:

- an EOA (MetaMask, Coinbase wallet, ...)
- the private key (to be copied in your `.env` file in `PRIVATE_KEY`)

Then, create a file named `deployEOA.ts` in the `scripts/` folder with this:

```ts title="scripts/deployEOA.ts"
import { ethers } from 'hardhat';
import * as dotenv from 'dotenv';

dotenv.config();

async function main() {
const customToken = await ethers.getContractFactory('CustomToken');

const Token = await customToken.deploy();
const token = await Token.waitForDeployment();
const CustomTokenAddress = await token.getAddress();
console.log(`Token address: ${CustomTokenAddress}`);
}

main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});
```

You can deploy the contract using the command:

```bash
npx hardhat --network luksoTestnet run scripts/deployEOA.ts
```

## Congratulations 🥳

You have deployed your first LSP7 token contract on LUKSO testnet through your Universal Profile :)
71 changes: 71 additions & 0 deletions docs/guides/hardhat-walkthrough/hardhat-base-setup.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
---
sidebar_label: Hardhat basic setup
sidebar_position: 1
---

# Setup your Hardhat project

In this article will guide you through the process of:

- setting up an [Hardhat](https://hardhat.org/) installation (using TypeScript)
- adding the [`@lukso/lsp-smart-contracts`](https://www.npmjs.com/package/@lukso/lsp-smart-contracts) package (using version 0.11.0-rc.1)
- creating a basic LSP7 contract
- and deploying it on [LUKSO Testnet](../../networks/testnet/parameters).

## Create Hardhat project

The first thing to do is to create a new Hardhat project that will use TypeScript:

```bash title="Setup new hardhat project"
mkdir lukso-app
cd lukso-app
npx hardhat
# select 'Create a TypeScript project' and
# use the default value for the rest of the setup
```

Once finished, you have a working Hardhat setup!

## Install packages & setup tools

To work in the best condition possible, we will install libraries that includes tools, helpers and the [`@lukso/lsp-smart-contracts`](https://www.npmjs.com/package/@lukso/lsp-smart-contracts) package.

### Install dependencies

```bash
npm i -D dotenv
npm i -s @lukso/lsp-smart-contracts@0.11.0-rc.1
```

### Add a build script in your package.json

Update your `package.json` with the following:

```json title="package.json"
"scripts": {
"build": "hardhat compile --force --show-stack-traces"
},
```

### Create a .env file

Create a new file at the root of your project called `.env` with the following content:

:::warning

The `.env` file contains sensitive values such as PRIVATE_KEY. Do not commit it to your source code repository!

:::

:::note

We will populate the values of the `.env` file later.

:::

```text title=".env"
PRIVATE_KEY=
UP_ADDR=
```

We now have a base Hardhat setup that we can use to develop and deploy our smart contracts.
2 changes: 1 addition & 1 deletion docs/guides/network/_category_.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
label: '⛓️ Network'
collapsed: false
collapsed: true
position: 1