Skip to content

Commit 60cc4a2

Browse files
authored
[FIX] - Line numbers in dapps tutorials (#780)
* Update code snippets lines in create-dapp-ethers-js.md for page.js * Update code snippets lines in create-dapp-viem.md for page.tsx * Fix snippet reference handling and improve line extraction logic in llms.txt and generate_llms.py
1 parent 83c9681 commit 60cc4a2

File tree

4 files changed

+54
-11
lines changed

4 files changed

+54
-11
lines changed

llms.txt

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37339,6 +37339,9 @@ This component handles connecting to the wallet, switching networks if necessary
3733937339
To integrate this component to your dApp, you need to overwrite the existing boilerplate in `app/page.js` with the following code:
3734037340

3734137341
```javascript title="app/page.js"
37342+
'use client';
37343+
import { useState } from 'react';
37344+
3734237345
import WalletConnect from './components/WalletConnect';
3734337346
export default function Home() {
3734437347
const [account, setAccount] = useState(null);
@@ -37438,6 +37441,10 @@ This component reads the `storedNumber` value from the contract and displays it
3743837441
To see this change in your dApp, you need to integrate this component into the `app/page.js` file:
3743937442

3744037443
```javascript title="app/page.js"
37444+
'use client';
37445+
import { useState } from 'react';
37446+
37447+
import WalletConnect from './components/WalletConnect';
3744137448
import ReadContract from './components/ReadContract';
3744237449
export default function Home() {
3744337450
const [account, setAccount] = useState(null);
@@ -38019,6 +38026,8 @@ This component handles connecting to the wallet, switching networks if necessary
3801938026
To use this component in your dApp, replace the existing boilerplate in `app/page.tsx` with the following code:
3802038027

3802138028
```typescript title="page.tsx"
38029+
"use client";
38030+
import { useState } from "react";
3802238031
import WalletConnect from "./components/WalletConnect";
3802338032
export default function Home() {
3802438033
const [account, setAccount] = useState<string | null>(null);
@@ -38124,6 +38133,9 @@ This component reads the `storedNumber` value from the contract and displays it
3812438133
To reflect this change in your dApp, incorporate this component into the `app/page.tsx` file.
3812538134

3812638135
```typescript title="page.tsx"
38136+
"use client";
38137+
import { useState } from "react";
38138+
import WalletConnect from "./components/WalletConnect";
3812738139
import ReadContract from "./components/ReadContract";
3812838140
export default function Home() {
3812938141
const [account, setAccount] = useState<string | null>(null);
@@ -38672,7 +38684,25 @@ Testing is a critical part of smart contract development. Hardhat makes it easy
3867238684
1. Create a folder for testing called `test`. Inside that directory, create a file named `Storage.js` and add the following code:
3867338685

3867438686
```javascript title="Storage.js"
38675-
38687+
const { expect } = require('chai');
38688+
const { ethers } = require('hardhat');
38689+
38690+
describe('Storage', function () {
38691+
let storage;
38692+
let owner;
38693+
let addr1;
38694+
38695+
beforeEach(async function () {
38696+
// Get signers
38697+
[owner, addr1] = await ethers.getSigners();
38698+
38699+
// Deploy the Storage contract
38700+
const Storage = await ethers.getContractFactory('Storage');
38701+
storage = await Storage.deploy();
38702+
await storage.waitForDeployment();
38703+
});
38704+
38705+
describe('Basic functionality', function () {
3867638706
// Add your logic here
3867738707
});
3867838708
});

scripts/generate_llms.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,16 +120,22 @@ def fetch_local_snippet(snippet_ref, snippet_directory):
120120
return snippet_content.strip()
121121

122122
def fetch_remote_snippet(snippet_ref, yaml_data, max_retries=3, backoff_factor=2):
123-
# Match URL with optional line range (start:end)
124-
match = re.match(r'^(https?://[^:]+)(?::(\d+))?(?::(\d+))?$', snippet_ref)
123+
# Match URL with optional line range. Cases:
124+
# - http://example.com:1:3 (this means extract lines 1 to 3)
125+
# - http://example.com::1 (this means extract until line 1)
126+
# - http://example.com (no line range specified, fetch entire content)
127+
match = re.match(r'^(https?://[^\s:]+)(?::(\d*))?(?::(\d*))?$', snippet_ref)
125128

126129
if not match:
127130
print(f"Invalid snippet reference format: {snippet_ref}")
128131
return f"Invalid snippet reference: {snippet_ref}"
129132

130133
url = match.group(1)
131-
line_start = int(match.group(2)) if match.group(2) else None
132-
line_end = int(match.group(3)) if match.group(3) else None
134+
line_start = match.group(2)
135+
line_end = match.group(3)
136+
137+
line_start = int(line_start) if line_start and line_start.isdigit() else None
138+
line_end = int(line_end) if line_end and line_end.isdigit() else None
133139

134140
# Resolve any template placeholders using the yaml_data
135141
url = resolve_placeholders(url, yaml_data)
@@ -151,9 +157,12 @@ def fetch_remote_snippet(snippet_ref, yaml_data, max_retries=3, backoff_factor=2
151157
snippet_content = response.text
152158

153159
# Extract specific lines if requested
154-
if line_start is not None and line_end is not None:
160+
if line_start is not None or line_end is not None:
155161
lines = snippet_content.split('\n')
156-
snippet_content = '\n'.join(lines[line_start - 1:line_end])
162+
# Python slice: start is inclusive, end is exclusive
163+
start = (line_start - 1) if line_start else 0
164+
end = line_end if line_end else len(lines)
165+
snippet_content = '\n'.join(lines[start:end])
157166

158167
return snippet_content.strip()
159168

tutorials/smart-contracts/launch-your-first-project/create-dapp-ethers-js.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ This component handles connecting to the wallet, switching networks if necessary
112112
To integrate this component to your dApp, you need to overwrite the existing boilerplate in `app/page.js` with the following code:
113113

114114
```javascript title="app/page.js"
115-
--8<-- "https://raw.githubusercontent.com/polkadot-developers/polkavm-storage-contract-dapps/refs/tags/v0.0.2/ethers-dapp/app/page.js:5:5"
115+
--8<-- "https://raw.githubusercontent.com/polkadot-developers/polkavm-storage-contract-dapps/refs/tags/v0.0.2/ethers-dapp/app/page.js::1"
116+
--8<-- "https://raw.githubusercontent.com/polkadot-developers/polkavm-storage-contract-dapps/refs/tags/v0.0.2/ethers-dapp/app/page.js:2:5"
116117
--8<-- "https://raw.githubusercontent.com/polkadot-developers/polkavm-storage-contract-dapps/refs/tags/v0.0.2/ethers-dapp/app/page.js:8:21"
117118
--8<-- "https://raw.githubusercontent.com/polkadot-developers/polkavm-storage-contract-dapps/refs/tags/v0.0.2/ethers-dapp/app/page.js:24:26"
118119
```
@@ -140,7 +141,8 @@ This component reads the `storedNumber` value from the contract and displays it
140141
To see this change in your dApp, you need to integrate this component into the `app/page.js` file:
141142

142143
```javascript title="app/page.js"
143-
--8<-- "https://raw.githubusercontent.com/polkadot-developers/polkavm-storage-contract-dapps/refs/tags/v0.0.2/ethers-dapp/app/page.js:6:6"
144+
--8<-- "https://raw.githubusercontent.com/polkadot-developers/polkavm-storage-contract-dapps/refs/tags/v0.0.2/ethers-dapp/app/page.js::1"
145+
--8<-- "https://raw.githubusercontent.com/polkadot-developers/polkavm-storage-contract-dapps/refs/tags/v0.0.2/ethers-dapp/app/page.js:2:6"
144146
--8<-- "https://raw.githubusercontent.com/polkadot-developers/polkavm-storage-contract-dapps/refs/tags/v0.0.2/ethers-dapp/app/page.js:8:22"
145147
--8<-- "https://raw.githubusercontent.com/polkadot-developers/polkavm-storage-contract-dapps/refs/tags/v0.0.2/ethers-dapp/app/page.js:24:26"
146148
```

tutorials/smart-contracts/launch-your-first-project/create-dapp-viem.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ This component handles connecting to the wallet, switching networks if necessary
113113
To use this component in your dApp, replace the existing boilerplate in `app/page.tsx` with the following code:
114114

115115
```typescript title="page.tsx"
116-
--8<-- "https://raw.githubusercontent.com/polkadot-developers/polkavm-storage-contract-dapps/refs/tags/v0.0.2/viem-dapp/app/page.tsx:4:4"
116+
--8<-- "https://raw.githubusercontent.com/polkadot-developers/polkavm-storage-contract-dapps/refs/tags/v0.0.2/viem-dapp/app/page.tsx::1"
117+
--8<-- "https://raw.githubusercontent.com/polkadot-developers/polkavm-storage-contract-dapps/refs/tags/v0.0.2/viem-dapp/app/page.tsx:2:4"
117118
--8<-- "https://raw.githubusercontent.com/polkadot-developers/polkavm-storage-contract-dapps/refs/tags/v0.0.2/viem-dapp/app/page.tsx:7:20"
118119
--8<-- "https://raw.githubusercontent.com/polkadot-developers/polkavm-storage-contract-dapps/refs/tags/v0.0.2/viem-dapp/app/page.tsx:23:25"
119120
```
@@ -141,7 +142,8 @@ This component reads the `storedNumber` value from the contract and displays it
141142
To reflect this change in your dApp, incorporate this component into the `app/page.tsx` file.
142143

143144
```typescript title="page.tsx"
144-
--8<-- "https://raw.githubusercontent.com/polkadot-developers/polkavm-storage-contract-dapps/refs/tags/v0.0.2/viem-dapp/app/page.tsx:5:5"
145+
--8<-- "https://raw.githubusercontent.com/polkadot-developers/polkavm-storage-contract-dapps/refs/tags/v0.0.2/viem-dapp/app/page.tsx::1"
146+
--8<-- "https://raw.githubusercontent.com/polkadot-developers/polkavm-storage-contract-dapps/refs/tags/v0.0.2/viem-dapp/app/page.tsx:2:5"
145147
--8<-- "https://raw.githubusercontent.com/polkadot-developers/polkavm-storage-contract-dapps/refs/tags/v0.0.2/viem-dapp/app/page.tsx:7:21"
146148
--8<-- "https://raw.githubusercontent.com/polkadot-developers/polkavm-storage-contract-dapps/refs/tags/v0.0.2/viem-dapp/app/page.tsx:23:25"
147149
```

0 commit comments

Comments
 (0)