Skip to content

Commit c4cb520

Browse files
authored
feat: update create frontend page to use smart contract portal (#38)
<!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **Documentation** - Updated references from Polygon Mumbai testnet to Polygon Amoy testnet. - Changed installation instructions from Node.js to Bun. - Modified file paths and configurations for ERC-20 token contract deployment. - Adjusted environment variables and project IDs for WalletConnect and Smart Contract Portal Middleware. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent 43fdfd6 commit c4cb520

File tree

5 files changed

+64
-71
lines changed

5 files changed

+64
-71
lines changed

docs/developer-guides/connect-frontend.md

Lines changed: 64 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ sidebar_position: 2
55
keywords: [frontend, nextjs, web3auth, polygon, erc20]
66
---
77

8-
This guide will show you how to build a full stack application using SettleMint, Polygon, wagmi and, Next Js. If this is your first time using SettleMint, we recommend the Hello World Guide.
8+
This guide will show you how to build a full stack application using SettleMint, Polygon, wagmi and NextJS. If this is your first time using SettleMint, we recommend the Hello World Guide.
99

1010
In this guide, you will learn how to connect a custom front end to your blockchain application using:
1111

@@ -14,11 +14,11 @@ In this guide, you will learn how to connect a custom front end to your blockcha
1414
- [Web3Auth](https://web3auth.io/) to make the transfer and receive the tokens
1515
polygon
1616
To complete this guide, please have the following installed:
17-
- [Node JS](https://nodejs.org/en)
17+
- [Bun](https://bun.sh/docs/installation)
1818
- [The Metamask Wallet Extension](https://chrome.google.com/webstore/detail/metamask/nkbihfbeogaeaoehlefnkodbefgpgknn)
1919
- A code editor (ex:[VSCode](https://code.visualstudio.com)).
2020

21-
At the end of this guide, you will have deployed an ERC-20 token smart contract to the Polygon Mumbai Testnet and connected a front end to interact with this smart contract. The front end will allow us to send tokens that we created to any wallet address.
21+
At the end of this guide, you will have deployed an ERC-20 token smart contract to the Polygon Amoy Testnet and connected a front end to interact with this smart contract. The front end will allow us to send tokens that we created to any wallet address.
2222

2323
## Part 1: The Blockchain Application
2424

@@ -34,7 +34,7 @@ For this guide, we will name this application `TokenSender`.
3434

3535
After creating an application, we need to create a Blockchain Network and Node to deploy to. This is done by clicking on the `Add a blockchain network` from the Application Dashboard.
3636

37-
We will deploy our smart contract to the Polygon Mumbai Testnet like the image below:
37+
We will deploy our smart contract to the Polygon Amoy Testnet like the image below:
3838

3939
![Choose A Network](../../static/img/developer-guides/choose-network.png)
4040

@@ -49,7 +49,7 @@ It is now time to choose a deployment plan. Here is the configuration most suita
4949

5050
### Creating a Private Key
5151

52-
To deploy our smart contract and send tokens, we need funds to cover the transfer cost. To do this we will create a Private Key and then receive MATIC tokens, the native token of the Mumbai testnet, from the faucet. Faucets allow us to get free test tokens to spend on a testnet.
52+
To deploy our smart contract and send tokens, we need funds to cover the transfer cost. To do this we will create a Private Key and then receive MATIC tokens, the native token of the Amoy testnet, from the faucet. Faucets allow us to get free test tokens to spend on a testnet.
5353

5454
To make a Private Key, click on the `Private Key` link on the right side of the dashboard. Once there, select `Create a Private Key`.
5555

@@ -87,43 +87,35 @@ Once that is deployed, we can go to the IDE and start editing our contract.
8787

8888
![Open IDE](../../static/img/developer-guides/open-ide.png)
8989

90-
You can find the ERC20 contract on the left in the Explorer under `contracts/GenericToken.sol`
90+
You can find the ERC20 contract on the left in the Explorer under `contracts/GenericERC20.sol`
9191

9292
We don't need to change anything in this contract's code. The current default is to deploy a token called `GenericToken` with a symbol `GT`.
9393

94-
To change this to something less generic, we can go to the `Deploy` folder and open the `00_deploy_generic_token.ts` file. From there we can edit the below code block that starts on line 13:
94+
To change this to something less generic, we can go to the `gnition/modules` folder and open the `main.ts` file. From there we can edit the below code block that starts on line 4:
9595

9696
**Before:**
9797

9898
```typescript
99-
await deploy('GenericToken', {
100-
from: deployer,
101-
args: ['GenericToken', 'GT'],
102-
log: true,
103-
});
99+
const counter = m.contract("GenericERC20", ["GenericERC20", "GT"]);
104100
```
105101

106102
By editing this code block, you can create your token name and symbol:
107103

108104
**After:**
109105

110106
```typescript
111-
await deploy('GenericToken', {
112-
from: deployer,
113-
args: ['DocumentationToken', 'WRITE'],
114-
log: true,
115-
});
107+
const counter = m.contract("GenericERC20", ["DocumentationToken", "WRITE"]);
116108
```
117109

118110
### Compile and Deploy the Contract
119111

120-
After making the changes, we can compile our contract code. This is done by selecting the `Task Manager` option on the right side.
112+
After making the changes, we can compile our contract code. This is done by selecting the `Task Manager` option on the left side.
121113

122114
![Deploy Contract](../../static/img/developer-guides/deploy-contract.png)
123115

124-
1. Select the `Task Manager` on the right
125-
2. Compile the contract by selecting `hardhat/compile`
126-
3. Deploy the contract by selecting `npm/smartcontract:deploy`
116+
1. Select the `Task Manager` on the left
117+
2. Compile the contract by selecting `Hardhat - Build`
118+
3. Deploy the contract by selecting `Hardhat - Deploy to platform network`
127119

128120
If the deployment was successful, you will see a `deployed at 0x4251501F80cE773b594C0B6CEf1289b97b0`
129121

@@ -142,44 +134,19 @@ In addition to using NextJS, this template uses the:
142134
- `wagmi` - to interact with the contract
143135
- `web3auth` - to create a modal for connecting a wallet
144136
- `tailwind` - for styling and design
137+
- `connectkit` - connecting a wallet to your dApp
145138

146-
You can get all of these packages by running `npm install` in your terminal after cloning the repo.
139+
You can get all of these packages by running `bun install` in your terminal after cloning the repo.
147140

148141
After that is completed, open the folder in your favorite code editor.
149142

150-
### Adding the ABI and Address
143+
### Adding the ABI
151144

152145
The first thing we will do is add the ABI and address of our deployed contract. This allows our front end to know where to read and write data to it. This information can be found in the SettleMint IDE where you deployed your contract.
153146

154-
Go to the `Deployments` folder in the file explorer and the `GenericToken.json` file. Copy both the `address` and `abi` and paste it into the `contractData/data.js` file inside the template.
155-
156-
```javascript
157-
export const contractData = {
158-
"address": "0x...",
159-
"abi": [...]
160-
```
161-
162-
### Adding the RPC Provider
163-
164-
Now we will add the RPC Provider URL that will allow us to connect to the Polygon Mumbai network.
165-
166-
To get this URL go back to the SettleMint platform and select the `Blochain Nodes` option on the right. From there, select your deployed node `TokenSender` and choose the `Connect` tab.
167-
168-
Here you will find your `JSON-RPC` URL. Copy that URL and go to the `src/pages/_app.tsx` file.
169-
170-
Around line 21 you will find this code block:
147+
Go to the `ignition/deployments/chain-46622/artifacts` folder in the file explorer and copy the `GenericERC20Module#GenericERC20.json` file to the `contractData` directory.
171148

172-
```javascript
173-
[
174-
jsonRpcProvider({
175-
rpc: () => ({
176-
http: `[INSERT RPC URL HERE]`,
177-
}),
178-
}),
179-
];
180-
```
181-
182-
Add your `JSON-RPC` url between the dashes.
149+
![Copy ABI](../../static/img/developer-guides/copy-abi.png)
183150

184151
### Adding the API Key
185152

@@ -189,53 +156,79 @@ This is done by navigating to your profile in the top right next to the grid ico
189156

190157
![API Keys](../../static/img/developer-guides/api-keys.png)
191158

192-
From there, select the `Generate new API Key`. You can now create an API key name, expiration, and access. For this guide, selecting `All blockchain nodes in all organizations` will work.
159+
From there, select the `Generate new API Key`. You can now create an API key name, expiration, and access. For this guide, selecting `All blockchain nodes in all organizations` and `All middlewares in all organizations` will work.
193160

194-
You will now see your API key that was generated only once. Copy this and add it to the end of the `JSON-RPC` that you copied in the previous step. The format should be: `JSON-RPC URL/API-KEY`
161+
You will now see your API key that was generated only once. Copy this and add it as the `BTP_TOKEN` variable value in the `.env` file.
195162

196163
### Configuring web3auth
197164

198165
To use web3auth, you will need to make an account a project. To set up an account and a project, follow [this guide](https://web3auth.io/docs/dashboard-setup).
199166

200-
After creating an account, you can navigate to the `Projects` tab on the right and click on `Add Project`. From there you will see that the project has been created and you can access the Project Details page.
167+
After creating an account, you can create a new project. From there you will see that the project has been created and you can access the Project Details page.
201168

202-
On that page, you will find the `Client ID` like below:
169+
On that page, you will find the `Project ID` like below:
203170

204-
![Client ID](../../static/img/developer-guides/client-id.png)
171+
![Project ID](../../static/img/developer-guides/project-id.png)
205172

206-
Copy your ID and paste it into the `clientID` of the `webAuthInstance` configuration found around line 31:
173+
Copy your Project ID and add it as the `NEXT_PUBLIC_WALLETCONNECT_PROJECT_ID` variable value in the `.env` file.
207174

208-
```javascript
209-
const web3AuthInstance = new Web3Auth({
210-
clientId: "[INSERT ClientId]"
175+
### Adding environment variables
211176

212-
```
177+
Open the `.env` file and add the remaining environment variables `BLOCKCHAIN_NODE`, `NEXT_PUBLIC_CONTRACT_ADDRESS` and `PORTAL_URL`.
213178

214-
We will also need to add the `rpcTarget` to the `web3AuthInstance`. This is found in a few lines below under the `clientId`.
179+
```.env
180+
# Base url of the application, used for SSR
181+
NEXT_PUBLIC_SERVER_URL=http://localhost:3000
215182
216-
The `rpcTarget` is the same `JSON-RPC URL/API Key` that you pasted in a previous step.
183+
# API token
184+
BTP_TOKEN=sm_pat_...
185+
186+
# Web3Modal - WalletConnect
187+
NEXT_PUBLIC_WALLETCONNECT_PROJECT_ID=...
188+
189+
# Wagmi
190+
# To get this URL go to the SettleMint platform and select the `Blockhain Nodes` option on the right.
191+
# From there, select your deployed node and choose the `Connect` tab.
192+
# Here you will find your `JSON-RPC` URL.
193+
BLOCKCHAIN_NODE=https://blockchain-node.settlemint.com/
194+
195+
# Contracts
196+
# To get this open the IDE of the deployed Smart Contract Set on the SettleMint platform.
197+
# Open the `ignition/deployments/chain-46622/deployed_addresses.json` json file and copy the address.
198+
NEXT_PUBLIC_CONTRACT_ADDRESS=0x...
199+
200+
# Portal
201+
# To get this URL go to the SettleMint platform and select the `Middlewares` option on the right.
202+
# From there, select your deployed smart contract portal middleware and choose the `Connect` tab.
203+
# Copy the base url (without the `/graphql` or `/rest` part).
204+
PORTAL_URL=https://smart-contract-portal-middleware.settlemint.com
205+
```
217206

218207
## Part 3: Running the Application
219208

220209
Now it's time to see what we have made. To get your application running, run the following command in your terminal:
221210

222211
```bash
223-
npm run dev
212+
bun dev
224213
```
225214

226215
This will start your application locally on `localhost:3000`. Go to that address and you will now see the frontend using NextJS:
227216
![Front-End](../../static/img/developer-guides/front-end.png)
228217

229-
Using `wagmi` enables us to read different functions and values from our smart contract. In this template, we are reading the `symbol` function to display which token is connected to this smart contract.
218+
Using the `Smart Contract Portal Middleware` enables us to read different functions and values from our smart contract. In this template, we are reading the `symbol` function to display which token is connected to this smart contract.
230219

231-
This is done in this code block in `index.tsx`
220+
This is done in this code block in `page.tsx`
232221

233222
```javascript
234-
const { data } = useContractRead({
235-
address: contractData.address,
236-
abi: contractData.abi,
237-
functionName: 'symbol',
238-
watch: true,
223+
const { data } = useSuspenseQuery({
224+
queryKey: ["symbol"],
225+
queryFn: async () => {
226+
const response = await portal.GET("/api/generic-erc-20/{address}/symbol", {
227+
params: { path: { address } },
228+
parseAs: "text",
229+
});
230+
return response.data;
231+
},
239232
});
240233
```
241234

-375 KB
Binary file not shown.
144 KB
Loading
-602 KB
Loading
12.5 KB
Loading

0 commit comments

Comments
 (0)