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

Decode viewer #154

Closed
wants to merge 31 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
2a447cc
tmp
wirednkod Nov 2, 2023
e159da3
fix: scale-ts re-export (#145)
kratico Nov 1, 2023
708ad46
Alter based on latest updates
wirednkod Nov 2, 2023
7e9b442
feat: view-builder
josepot Nov 2, 2023
84b853d
view-builder: add `path` to all decoded types
josepot Nov 2, 2023
2395cda
view-builder: split `BytesDecoded`
josepot Nov 2, 2023
d805d4f
view-builder: use `HexString` for `Bytes\w*Decoded`
josepot Nov 2, 2023
b325b41
view-builder: a bit of cleanup
josepot Nov 2, 2023
d9238a4
view-builder: final improvements
josepot Nov 2, 2023
b520695
chore: leverage utils
josepot Nov 2, 2023
67b477f
feat: node polkadot provider (#137)
ryanleecode Nov 2, 2023
a4f301a
view-builder: and we are done
josepot Nov 3, 2023
1fde78b
checkpoint
wirednkod Nov 3, 2023
d3d3785
fix: txdescriptor event and error generics (#147)
ryanleecode Nov 3, 2023
020f212
feat: view-builder (#146)
josepot Nov 3, 2023
50d9e9a
substrate-codegen: improvements and fixes (#149)
josepot Nov 4, 2023
4d43954
chore: remove unnecessary file
josepot Nov 4, 2023
3fa5a59
fix: experiments not exiting (#148)
ryanleecode Nov 4, 2023
af505f3
feat: update descriptor result output (#138)
ryanleecode Nov 4, 2023
8d02115
feat-dynamic-builder: add missing builds (#150)
josepot Nov 5, 2023
a4e13c6
decode-viewer add css
wirednkod Nov 6, 2023
53ded21
fix based on latest types
wirednkod Nov 6, 2023
2c73e76
fix(cli): get look up entries (#151)
ryanleecode Nov 6, 2023
d5b0dea
fix(cli): descriptor data null issue (#152)
ryanleecode Nov 6, 2023
9104583
Replace types with new ones and organise a bit the code
wirednkod Nov 7, 2023
9c3f62d
cover more scenarios
wirednkod Nov 7, 2023
dca5d79
Decode-viewer: minor change on Bytes that was breaking the storybook …
wirednkod Nov 7, 2023
735fb11
Decode-Viewer: Simplify structure and pass components to storybook
wirednkod Nov 7, 2023
776a862
Add stories of storybook
wirednkod Nov 7, 2023
44b8687
Merge branch 'main' of github.com:paritytech/polkadot-api into nik-vi…
wirednkod Nov 7, 2023
f74a65a
comment out unused code
wirednkod Nov 7, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions examples/decode-viewer/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module.exports = {
root: true,
env: { browser: true, es2020: true },
extends: ['eslint:recommended', 'plugin:@typescript-eslint/recommended', 'plugin:react-hooks/recommended', 'plugin:storybook/recommended'],
ignorePatterns: ['dist', '.eslintrc.cjs'],
parser: '@typescript-eslint/parser',
plugins: ['react-refresh'],
rules: {
'react-refresh/only-export-components': [
'warn',
{ allowConstantExport: true },
],
},
}
24 changes: 24 additions & 0 deletions examples/decode-viewer/.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?
28 changes: 28 additions & 0 deletions examples/decode-viewer/.storybook/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import type { StorybookConfig } from "@storybook/react-vite"

import { join, dirname } from "path"

/**
* This function is used to resolve the absolute path of a package.
* It is needed in projects that use Yarn PnP or are set up within a monorepo.
*/
function getAbsolutePath(value: string): any {
return dirname(require.resolve(join(value, "package.json")))
}
const config: StorybookConfig = {
stories: ["../src/**/*.mdx", "../src/**/*.stories.@(js|jsx|mjs|ts|tsx)"],
addons: [
getAbsolutePath("@storybook/addon-links"),
getAbsolutePath("@storybook/addon-essentials"),
getAbsolutePath("@storybook/addon-onboarding"),
getAbsolutePath("@storybook/addon-interactions"),
],
framework: {
name: getAbsolutePath("@storybook/react-vite"),
options: {},
},
docs: {
autodocs: "tag",
},
}
export default config
15 changes: 15 additions & 0 deletions examples/decode-viewer/.storybook/preview.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import type { Preview } from "@storybook/react"

const preview: Preview = {
parameters: {
actions: { argTypesRegex: "^on[A-Z].*" },
controls: {
matchers: {
color: /(background|color)$/i,
date: /Date$/i,
},
},
},
}

export default preview
27 changes: 27 additions & 0 deletions examples/decode-viewer/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# 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
parserOptions: {
ecmaVersion: 'latest',
sourceType: 'module',
project: ['./tsconfig.json', './tsconfig.node.json'],
tsconfigRootDir: __dirname,
},
```

- Replace `plugin:@typescript-eslint/recommended` to `plugin:@typescript-eslint/recommended-type-checked` or `plugin:@typescript-eslint/strict-type-checked`
- Optionally add `plugin:@typescript-eslint/stylistic-type-checked`
- Install [eslint-plugin-react](https://github.com/jsx-eslint/eslint-plugin-react) and add `plugin:react/recommended` & `plugin:react/jsx-runtime` to the `extends` list
12 changes: 12 additions & 0 deletions examples/decode-viewer/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<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>
Binary file added examples/decode-viewer/ksm.scale
Binary file not shown.
46 changes: 46 additions & 0 deletions examples/decode-viewer/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"name": "decode-viewer",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "node prepare.mjs && vite",
"build": "tsc && vite build",
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
"preview": "vite preview",
"storybook": "storybook dev -p 6006",
"build-storybook": "storybook build"
},
"dependencies": {
"@polkadot-api/substrate-bindings": "workspace:*",
"@polkadot-api/substrate-codegen": "workspace:*",
"@polkadot-cloud/utils": "^0.0.25",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"devDependencies": {
"@polkadot-cloud/core": "^1.0.43",
"@polkadot-cloud/react": "^0.1.121",
"@storybook/addon-essentials": "^7.5.2",
"@storybook/addon-interactions": "^7.5.2",
"@storybook/addon-links": "^7.5.2",
"@storybook/addon-onboarding": "^1.0.8",
"@storybook/blocks": "^7.5.2",
"@storybook/react": "^7.5.2",
"@storybook/react-vite": "^7.5.2",
"@storybook/testing-library": "^0.2.2",
"@types/react": "^18.2.15",
"@types/react-dom": "^18.2.7",
"@typescript-eslint/eslint-plugin": "^6.0.0",
"@typescript-eslint/parser": "^6.0.0",
"@vitejs/plugin-react-swc": "^3.3.2",
"eslint": "^8.45.0",
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-react-refresh": "^0.4.3",
"eslint-plugin-storybook": "^0.6.15",
"sass": "^1.69.5",
"storybook": "^7.5.2",
"typescript": "^5.0.2",
"vite": "^4.4.5"
}
}
6 changes: 6 additions & 0 deletions examples/decode-viewer/prepare.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { readFile, writeFile } from "node:fs/promises"
import { v14 } from "@polkadot-api/substrate-bindings"

const metadataBytes = await readFile("./ksm.scale")
const metadata = v14.dec(metadataBytes)
await writeFile("./src/metadata.txt", JSON.stringify(metadata))
1 change: 1 addition & 0 deletions examples/decode-viewer/public/vite.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
69 changes: 69 additions & 0 deletions examples/decode-viewer/src/App.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#root {
max-width: 1280px;
margin: 0 auto;
padding: 2rem;
top: 10rem;
width: 80%;
}

.card {
padding: 2em;
}

.read-the-docs {
color: #888;
}

.struct_title {
font-weight: bold;
}

.hex-input {
padding: 0.5rem 1rem;
font-size: 1.2rem;
}

.hex-button {
margin-top: 1rem;
width: 13rem;
}

.help_tooltip {
cursor: pointer;
position: relative;
display: inline-block;
}

.help_tooltip .help_tooltiptext {
visibility: hidden;
width: 30vw;
background-color: black;
color: #fff;
text-align: left;
border-radius: 0.6rem;
padding: 0.5rem 0;
display: flex;
flex-direction: column;
padding: 1rem;
font-size: 0.8rem;

/* Position the tooltip */
position: absolute;
z-index: 1;
top: -0.5rem;
right: 105%;
}

.help_tooltip:hover .help_tooltiptext {
visibility: visible;
}

.error {
border: 0.2rem solid #db4437;
font-weight: bold;
padding: 0.5rem 2rem;
margin-top: 1rem;
color: #db4437;
font-size: 0.9rem;
font-weight: bold;
}
98 changes: 98 additions & 0 deletions examples/decode-viewer/src/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { useEffect, useState } from "react"
import { V14, getViewBuilder } from "@polkadot-api/substrate-codegen"

import { Button } from "@polkadot-cloud/react"
import { Input } from "./stories/Input"
import metadata from "./metadata.json"

import "@polkadot-cloud/core/accent/kusama-relay.css"
import "@polkadot-cloud/core/theme/default/index.css"

import "./App.css"
import { InitStruct, InitStructWithDocs } from "./types"
import { ArgsStructure } from "./stories/ArgStructure"
import { Structure } from "./stories/Structure"

const buttonStyle = {
background: "#424242",
padding: "0.5rem 1rem",
width: "15rem",
}

export const App = () => {
const [inputValue, setInputValue] = useState<string>(
"",
// "0x180008040700dc97b0271418c41f80d049826cfb1d6bd2e44e11ea39759addf6b01632ca973d0b00409452a30306050400dc97b0271418c41f80d049826cfb1d6bd2e44e11ea39759addf6b01632ca973d",
// "0x17002b0f01590100004901415050524f56455f52464328303030352c39636261626661383035393864323933353833306330396331386530613065346564383232376238633866373434663166346134316438353937626236643434290101000000",
)
const [err, setErr] = useState<string | null>()

const [pallet, setPallet] = useState<InitStruct>()
const [call, setCall] = useState<InitStructWithDocs>()
const [argsValue, setArgsValue] = useState<any>()

const decode = (hex: string) => {
try {
const { callDecoder } = getViewBuilder(metadata as V14)
const result = callDecoder(hex)

const {
pallet,
call,
args: { value },
} = result
setPallet(pallet)
setCall(call)
setArgsValue(value)
} catch (e: any) {
setErr(e.message)
}
}

useEffect(() => {
if (err) {
setPallet(undefined)
setCall(undefined)
setArgsValue(undefined)
}
}, [err])

return (
<>
<div className="top-stuff"></div>
<h3>Decode View</h3>
<div style={{ display: "flex", flexDirection: "column" }}>
<Input
isSimple
label={"hex-encoded call"}
value={inputValue}
onChange={(e: any) => setInputValue(e.target.value)}
/>
<Button
lg
style={buttonStyle}
text="Decode stuff"
onClick={() => {
setErr(null)
decode(inputValue)
}}
/>
</div>
<div className="bottom-stuff">
{err && <div className="error">{err}</div>}
{pallet && call ? <Structure pallet={pallet} call={call} /> : null}
{argsValue ? (
<ArgsStructure
value={argsValue.value}
codec={argsValue.codec}
innerDocs={argsValue.innerDocs}
input={argsValue.input}
/>
) : null}
</div>
</>
)
}

export default App
1 change: 1 addition & 0 deletions examples/decode-viewer/src/assets/react.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.