Skip to content

Commit

Permalink
Create a Vite sample application (#1451)
Browse files Browse the repository at this point in the history
* feat: add vite blank project

* fix: update yarn.lock

* fix: add Hash page

* fix: add SDK network

* fix: complete vite SDK integration

* fix: add another example

* fix: update tsconfig for vite sample dApp

* fix: refactor code

* fix: refactor code

* fix: refactor code

* fix: add tests

* fix: fix vitest error

* fix: fix error tests

* fix: fix error tests

* fix: fix error when testing
  • Loading branch information
fabiorigam authored Nov 7, 2024
1 parent c29efb3 commit 2c65ef2
Show file tree
Hide file tree
Showing 31 changed files with 1,408 additions and 20 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/test-apps.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ jobs:

- name: Install packages
run: yarn install

- name: Install playwright dependencies (for sample Vite dApp tests)
run: yarn playwright install

- name: Build packages
run: yarn build
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/unit-integration-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ jobs:
- name: Install & Patch packages
run: yarn install

- name: Install playwright dependencies (for sample Vite dApp tests)
run: yarn playwright install

- name: Preliminary Stop of Thor solo node
id: preliminary-stop-solo
run: yarn stop-thor-solo
Expand Down
24 changes: 24 additions & 0 deletions apps/sdk-vite-integration/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
dist
dist-ssr
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
50 changes: 50 additions & 0 deletions apps/sdk-vite-integration/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# React + TypeScript + Vite

This template provides a minimal setup to get React working in Vite with HMR and some ESLint rules.

Currently, two official plugins are available:

- [@vitejs/plugin-react](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react/README.md) uses [Babel](https://babeljs.io/) for Fast Refresh
- [@vitejs/plugin-react-swc](https://github.com/vitejs/vite-plugin-react-swc) uses [SWC](https://swc.rs/) for Fast Refresh

## Expanding the ESLint configuration

If you are developing a production application, we recommend updating the configuration to enable type aware lint rules:

- Configure the top-level `parserOptions` property like this:

```js
export default tseslint.config({
languageOptions: {
// other options...
parserOptions: {
project: ['./tsconfig.node.json', './tsconfig.app.json'],
tsconfigRootDir: import.meta.dirname,
},
},
})
```

- Replace `tseslint.configs.recommended` to `tseslint.configs.recommendedTypeChecked` or `tseslint.configs.strictTypeChecked`
- Optionally add `...tseslint.configs.stylisticTypeChecked`
- Install [eslint-plugin-react](https://github.com/jsx-eslint/eslint-plugin-react) and update the config:

```js
// eslint.config.js
import react from 'eslint-plugin-react'

export default tseslint.config({
// Set the react version
settings: { react: { version: '18.3' } },
plugins: {
// Add the react plugin
react,
},
rules: {
// other rules...
// Enable its recommended rules
...react.configs.recommended.rules,
...react.configs['jsx-runtime'].rules,
},
})
```
28 changes: 28 additions & 0 deletions apps/sdk-vite-integration/eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import js from '@eslint/js'
import globals from 'globals'
import reactHooks from 'eslint-plugin-react-hooks'
import reactRefresh from 'eslint-plugin-react-refresh'
import tseslint from 'typescript-eslint'

export default tseslint.config(
{ ignores: ['dist'] },
{
extends: [js.configs.recommended, ...tseslint.configs.recommended],
files: ['**/*.{ts,tsx}'],
languageOptions: {
ecmaVersion: 2020,
globals: globals.browser,
},
plugins: {
'react-hooks': reactHooks,
'react-refresh': reactRefresh,
},
rules: {
...reactHooks.configs.recommended.rules,
'react-refresh/only-export-components': [
'warn',
{ allowConstantExport: true },
],
},
},
)
13 changes: 13 additions & 0 deletions apps/sdk-vite-integration/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + React + TS</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script>
</body>
</html>
37 changes: 37 additions & 0 deletions apps/sdk-vite-integration/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"name": "sdk-vite-integration",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "tsc -b && vite build",
"lint": "eslint .",
"preview": "vite preview",
"test": "vitest"
},
"dependencies": {
"@vechain/sdk-core": "^1.0.0-beta.32",
"@vechain/sdk-network": "^1.0.0-beta.32",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-router-dom": "^6.27.0"
},
"devDependencies": {
"@eslint/js": "^9.13.0",
"@types/react": "^18.3.11",
"@types/react-dom": "^18.3.1",
"@vitejs/plugin-react": "^4.3.3",
"@vitest/browser": "^2.1.4",
"eslint": "^9.13.0",
"eslint-plugin-react-hooks": "^5.0.0",
"eslint-plugin-react-refresh": "^0.4.13",
"globals": "^15.11.0",
"playwright": "^1.48.2",
"typescript": "~5.6.2",
"typescript-eslint": "^8.10.0",
"vite": "^5.4.10",
"vitest": "^2.1.4",
"vitest-browser-react": "^0.0.3"
}
}
1 change: 1 addition & 0 deletions apps/sdk-vite-integration/public/vite.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
42 changes: 42 additions & 0 deletions apps/sdk-vite-integration/src/App.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#root {
max-width: 1280px;
margin: 0 auto;
padding: 2rem;
text-align: center;
}

.logo {
height: 6em;
padding: 1.5em;
will-change: filter;
transition: filter 300ms;
}
.logo:hover {
filter: drop-shadow(0 0 2em #646cffaa);
}
.logo.react:hover {
filter: drop-shadow(0 0 2em #61dafbaa);
}

@keyframes logo-spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}

@media (prefers-reduced-motion: no-preference) {
a:nth-of-type(2) .logo {
animation: logo-spin infinite 20s linear;
}
}

.card {
padding: 2em;
}

.read-the-docs {
color: #888;
}
63 changes: 63 additions & 0 deletions apps/sdk-vite-integration/src/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { BrowserRouter as Router, Route, Routes, Link } from 'react-router-dom';
import reactLogo from './assets/react.svg';
import viteLogo from '/vite.svg';
import './App.css';
import Hash from './components/Hash';
import TransferLogs from './components/TransferLogs';
import GetLastBlock from './components/GetLastBlock';

function App() {
return (
<Router>
<div>
<div>
<a href="https://vite.dev" target="_blank" rel="noopener noreferrer">
<img src={viteLogo} className="logo" alt="Vite logo" />
</a>
<a href="https://react.dev" target="_blank" rel="noopener noreferrer">
<img src={reactLogo} className="logo react" alt="React logo" />
</a>
</div>
<h2>Welcome to the SDK Vite Integration!</h2>
<div className="card">
<p>
<b>@vechain/sdk-core</b> integration example:{' '}
<Link
className="text-blue-500 hover:underline"
to="/hash" // Change from href to to for React Router
>
Hash
</Link>
</p>
<p>
<b>@vechain/sdk-network</b> integration example:{' '}
<Link
className="text-blue-500 hover:underline"
to='/transfer-logs'
>
Transfer logs
</Link>
</p>
<p>
<b>@vechain/sdk-network</b> integration example:{' '}
<Link
className="text-blue-500 hover:underline"
to='/get-last-block'
>
Get Last Block
</Link>
</p>
</div>
</div>

{/* Define Routes */}
<Routes>
<Route path="/hash" element={<Hash />} />
<Route path="/transfer-logs" element={<TransferLogs />} />
<Route path="/get-last-block" element={<GetLastBlock />} />
</Routes>
</Router>
);
}

export default App;
1 change: 1 addition & 0 deletions apps/sdk-vite-integration/src/assets/react.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
38 changes: 38 additions & 0 deletions apps/sdk-vite-integration/src/components/GetLastBlock.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { type CompressedBlockDetail, ThorClient } from "@vechain/sdk-network";
import { useState } from "react";

const GetLastBlock = () => {
const [block, setBlock] = useState<CompressedBlockDetail | null>(null);

// URL of the VeChain mainnet
const mainnetUrl = 'https://mainnet.vechain.org';

// Thor client instance
const thorClient = ThorClient.fromUrl(mainnetUrl);

// Function to fetch the last block
const fetchLastBlock = async () => {
try {
const lastBlock = await thorClient.blocks.getBlockCompressed("best");
setBlock(lastBlock);
} catch (error) {
console.error("Error fetching the last block:", error);
}
};

return (
<div>
<button onClick={fetchLastBlock}>
Get Last Block
</button>
{block && (
<div>
<h3>Last Block Details:</h3>
<pre>{JSON.stringify(block, null, 2)}</pre>
</div>
)}
</div>
);
};

export default GetLastBlock;
Loading

1 comment on commit 2c65ef2

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test Coverage

Summary

Lines Statements Branches Functions
Coverage: 99%
98.99% (4334/4378) 97.61% (1390/1424) 99% (891/900)
Title Tests Skipped Failures Errors Time
core 810 0 πŸ’€ 0 ❌ 0 πŸ”₯ 2m 20s ⏱️
network 726 0 πŸ’€ 0 ❌ 0 πŸ”₯ 5m 1s ⏱️
errors 42 0 πŸ’€ 0 ❌ 0 πŸ”₯ 17.721s ⏱️
logging 3 0 πŸ’€ 0 ❌ 0 πŸ”₯ 18.369s ⏱️
hardhat-plugin 19 0 πŸ’€ 0 ❌ 0 πŸ”₯ 1m 1s ⏱️
aws-kms-adapter 23 0 πŸ’€ 0 ❌ 0 πŸ”₯ 1m 21s ⏱️
ethers-adapter 5 0 πŸ’€ 0 ❌ 0 πŸ”₯ 1m 12s ⏱️
rpc-proxy 37 0 πŸ’€ 0 ❌ 0 πŸ”₯ 1m 7s ⏱️

Please sign in to comment.