Skip to content

Commit

Permalink
debug revert reason
Browse files Browse the repository at this point in the history
  • Loading branch information
darrenvechain committed Feb 28, 2024
0 parents commit c33fcd4
Show file tree
Hide file tree
Showing 18 changed files with 5,755 additions and 0 deletions.
18 changes: 18 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module.exports = {
root: true,
env: { browser: true, es2020: true },
extends: [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"plugin:react-hooks/recommended",
],
ignorePatterns: ["dist", ".eslintrc.cjs"],
parser: "@typescript-eslint/parser",
plugins: ["react-refresh"],
rules: {
"react-refresh/only-export-components": [
"warn",
{ allowConstantExport: true },
],
},
};
42 changes: 42 additions & 0 deletions .github/workflows/deploy-gh-pages.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: Deploy to GH pages

on:
push:
branches: [ main ]

# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages
permissions:
contents: read
pages: write
id-token: write
packages: write

jobs:
BuildAndDeploy:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4

- uses: actions/setup-node@v4
with:
node-version: '20'

- name: Install Repo Deps
run: yarn

- name: Build Website
run: yarn build

- name: Setup Pages
uses: actions/configure-pages@v2

- name: Upload artifact
uses: actions/upload-pages-artifact@v1
with:
path: './dist'

- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v1

24 changes: 24 additions & 0 deletions .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?
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# 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 {
// other rules...
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
13 changes: 13 additions & 0 deletions 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>
30 changes: 30 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "debug-revert-reason",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
"preview": "vite preview"
},
"dependencies": {
"@vechain/dapp-kit-react": "^0.0.4",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"devDependencies": {
"@types/react": "^18.2.43",
"@types/react-dom": "^18.2.17",
"@typescript-eslint/eslint-plugin": "^6.14.0",
"@typescript-eslint/parser": "^6.14.0",
"@vitejs/plugin-react": "^4.2.1",
"eslint": "^8.55.0",
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-react-refresh": "^0.4.5",
"typescript": "^5.2.2",
"vite": "^5.0.8",
"vite-plugin-node-polyfills": "^0.19.0"
}
}
1 change: 1 addition & 0 deletions 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 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;
}
143 changes: 143 additions & 0 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
import "./App.css";
import { useMemo, useState, useEffect } from "react";
import { DAppKit } from "@vechain/dapp-kit";
import axios from "axios";
import { abi, keccak256 } from "thor-devkit";

const mainNetUrl = "https://mainnet.vechain.org";
const testNetUrl = "https://testnet.vechain.org";

const mainnet = new DAppKit({
nodeUrl: mainNetUrl,
genesis: "main",
});

const testnet = new DAppKit({
nodeUrl: testNetUrl,
genesis: "test",
});

const errorSelector = keccak256("Error(string)").toString("hex").slice(0, 8);
const panicSelector = keccak256("Panic(uint256)").toString("hex").slice(0, 8);

console.log(errorSelector);
console.log(panicSelector);

export function decodeRevertReason(data: string): string {
try {
if (data.startsWith(errorSelector)) {
return abi.decodeParameter(
"string",
"0x" + data.slice(errorSelector.length),
) as string;
} else if (data.startsWith(panicSelector)) {
const decoded = abi.decodeParameter(
"uint256",
"0x" + data.slice(panicSelector.length),
) as string;
return `Panic(0x${parseInt(decoded).toString(16).padStart(2, "0")})`;
}
return "";
} catch {
return "";
}
}

const TX_HASH_REGEX = /^0x[0-9a-fA-F]{64}$/;

function App() {
const [txHash, setTxHash] = useState<string>(
"0xaa5efefc055af9b80e2fa8e97f006b252f6702ea4363c67afad92004c4bbc918",
);

const [isTestnet, setIsTestnet] = useState<boolean>(true);
const [revertReason, setRevertReason] = useState<string>("");

const txHashValid = useMemo(() => {
return TX_HASH_REGEX.test(txHash);
}, [txHash]);

const getDebugReason = async (
txHash: string,
dappKit: DAppKit,
url: string,
) => {
const tx = await dappKit.thor.transaction(txHash).get();

console.log(tx);

if (!tx) return;

const block = await dappKit.thor.block(tx.meta.blockID).get();

if (!block) return;

const txIndex = block.transactions.findIndex((tx) => tx === txHash);

for (let i = 0; i < tx.clauses.length; i++) {
const debugged = await axios.post(url + "/debug/tracers", {
target: `${block.id}/${txIndex}/${i}`,
});

const revertReason = decodeRevertReason(debugged.data.returnValue);

if (revertReason) {
setRevertReason(revertReason);
return;
}
}
};

useEffect(() => {
if (txHashValid) {
if (isTestnet) {
getDebugReason(txHash, testnet, testNetUrl);
} else {
getDebugReason(txHash, mainnet, mainNetUrl);
}
}
}, [txHashValid, txHash, isTestnet]);

const toggleNetwork = () => {
setRevertReason("");
setTxHash("");
setIsTestnet((prev) => !prev);
};

return (
<>
<div className="card">
<p>Enter TX Hash</p>
<input
value={txHash}
onChange={(e) => {
setTxHash(e.target.value);
setRevertReason("");
}}
></input>

<p>Revert Reason:</p>
<div>{revertReason}</div>
</div>

<div className="toggle">
<label>
Testnet
<input type="checkbox" checked={isTestnet} onChange={toggleNetwork} />
</label>
</div>
<div className="toggle">
<label>
Mainnet
<input
type="checkbox"
checked={!isTestnet}
onChange={toggleNetwork}
/>
</label>
</div>
</>
);
}

export default App;
1 change: 1 addition & 0 deletions 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.
Loading

0 comments on commit c33fcd4

Please sign in to comment.