-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
77 changed files
with
5,932 additions
and
7,799 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,7 +42,9 @@ | |
"eslint-plugin-prettier": "^4.2.1", | ||
"prettier": "^2.2.1", | ||
"prettier-plugin-packagejson": "^2.2.18", | ||
"typescript": "^4.7.4" | ||
"typescript": "^4.7.4", | ||
"webpack-cli": "^5.1.4", | ||
"webpack-node-externals": "^3.0.0" | ||
}, | ||
"packageManager": "[email protected]", | ||
"engines": { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# Getting Started with Create React App | ||
|
||
This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). | ||
|
||
## Available Scripts | ||
|
||
In the project directory, you can run: | ||
|
||
### `npm start` | ||
|
||
Runs the app in the development mode.\ | ||
Open [http://localhost:3000](http://localhost:3000) to view it in the browser. | ||
|
||
The page will reload if you make edits.\ | ||
You will also see any lint errors in the console. | ||
|
||
### `npm test` | ||
|
||
Launches the test runner in the interactive watch mode.\ | ||
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information. | ||
|
||
### `npm run build` | ||
|
||
Builds the app for production to the `build` folder.\ | ||
It correctly bundles React in production mode and optimizes the build for the best performance. | ||
|
||
The build is minified and the filenames include the hashes.\ | ||
Your app is ready to be deployed! | ||
|
||
See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information. | ||
|
||
### `npm run eject` | ||
|
||
**Note: this is a one-way operation. Once you `eject`, you can’t go back!** | ||
|
||
If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project. | ||
|
||
Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own. | ||
|
||
You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it. | ||
|
||
## Learn More | ||
|
||
You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started). | ||
|
||
To learn React, check out the [React documentation](https://reactjs.org/). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
{ | ||
"name": "dapp", | ||
"version": "0.1.0", | ||
"private": true, | ||
"dependencies": { | ||
"@emotion/react": "latest", | ||
"@emotion/styled": "latest", | ||
"@metamask/providers": "latest", | ||
"@mui/icons-material": "latest", | ||
"@mui/lab": "latest", | ||
"@mui/material": "latest", | ||
"@polkadot/api": "latest", | ||
"@polkadot/api-derive": "latest", | ||
"@polkadot/apps-config": "latest", | ||
"@polkadot/extension-dapp": "../../../polkadot-js-extension/packages/extension-dapp/build", | ||
"@polkadot/extension-inject": "latest", | ||
"@testing-library/jest-dom": "^5.17.0", | ||
"@testing-library/react": "^13.4.0", | ||
"@testing-library/user-event": "^13.5.0", | ||
"@types/jest": "^27.5.2", | ||
"@types/node": "^16.18.66", | ||
"@types/react": "^18.2.40", | ||
"@types/react-dom": "^18.2.17", | ||
"react": "^18.2.0", | ||
"react-dom": "^18.2.0", | ||
"react-i18next": "latest", | ||
"react-scripts": "5.0.1", | ||
"typescript": "^4.9.5", | ||
"web-vitals": "^2.1.4" | ||
}, | ||
"scripts": { | ||
"start": "PORT=8000 react-scripts start", | ||
"build": "react-scripts build", | ||
"test": "react-scripts test", | ||
"eject": "react-scripts eject", | ||
"clean": "yarn cache clean --all" | ||
}, | ||
"eslintConfig": { | ||
"extends": [ | ||
"react-app", | ||
"react-app/jest" | ||
] | ||
}, | ||
"browserslist": { | ||
"production": [ | ||
">0.2%", | ||
"not dead", | ||
"not op_mini all" | ||
], | ||
"development": [ | ||
"last 1 chrome version", | ||
"last 1 firefox version", | ||
"last 1 safari version" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
.App { | ||
text-align: center; | ||
} | ||
|
||
.App-logo { | ||
height: 40vmin; | ||
pointer-events: none; | ||
} | ||
|
||
@media (prefers-reduced-motion: no-preference) { | ||
.App-logo { | ||
animation: App-logo-spin infinite 20s linear; | ||
} | ||
} | ||
|
||
.App-header { | ||
background-color: #282c34; | ||
min-height: 100vh; | ||
display: flex; | ||
flex-direction: column; | ||
align-items: center; | ||
justify-content: center; | ||
font-size: calc(10px + 2vmin); | ||
color: white; | ||
} | ||
|
||
.App-link { | ||
color: #61dafb; | ||
} | ||
|
||
@keyframes App-logo-spin { | ||
from { | ||
transform: rotate(0deg); | ||
} | ||
to { | ||
transform: rotate(360deg); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import React from 'react'; | ||
import { render, screen } from '@testing-library/react'; | ||
import App from './App'; | ||
|
||
test('renders learn react link', () => { | ||
render(<App />); | ||
const linkElement = screen.getByText(/learn react/i); | ||
expect(linkElement).toBeInTheDocument(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,206 @@ | ||
import TransferFund from './TransferFund'; | ||
import React, { useCallback, useEffect, useState } from 'react'; | ||
import type { InjectedAccountWithMeta, InjectedExtension } from '@polkadot/extension-inject/types'; | ||
import './App.css'; | ||
import { web3Accounts, web3Enable } from '@polkadot/extension-dapp'; | ||
import { Box, Tabs, Tab, Button, Grid, Typography } from '@mui/material'; | ||
import { ApiPromise, WsProvider } from '@polkadot/api'; | ||
import useEndpoint from './hooks/useEndpoint'; | ||
import type { DeriveBalancesAll } from '@polkadot/api-derive/types'; | ||
import { DEFAULT_SNAP_ORIGIN, POLKAMASK_ACCOUNT_META_SOURCE } from './util/consts'; | ||
import { getFormatted } from './util/getFormatted'; | ||
import { getChain } from './util/getChain'; | ||
import { installPolkaMask } from './util/installPolkaMask'; | ||
import logo from './assets/logo.svg'; | ||
import SignMessage from './SignMessage'; | ||
|
||
interface TabPanelProps { | ||
children?: React.ReactNode; | ||
index: number; | ||
value: number; | ||
} | ||
|
||
const currentChainName = 'westend'; | ||
|
||
function TabPanel(props: TabPanelProps) { | ||
const { children, value, index, ...other } = props; | ||
|
||
return ( | ||
<div | ||
role="tabpanel" | ||
hidden={value !== index} | ||
id={`vertical-tabpanel-${index}`} | ||
aria-labelledby={`vertical-tab-${index}`} | ||
{...other} | ||
> | ||
{value === index && ( | ||
<Box sx={{ p: 3 }}> | ||
<Typography>{children}</Typography> | ||
</Box> | ||
)} | ||
</div> | ||
); | ||
} | ||
|
||
function a11yProps(index: number) { | ||
return { | ||
id: `vertical-tab-${index}`, | ||
'aria-controls': `vertical-tabpanel-${index}`, | ||
}; | ||
} | ||
|
||
export default function App() { | ||
const [value, setValue] = React.useState(0); | ||
const endpoint = useEndpoint(currentChainName) | ||
const [extensions, setExtensions] = useState<InjectedExtension[] | undefined>(); | ||
const [account, setAccount] = useState<InjectedAccountWithMeta>(); | ||
const [api, setApi] = useState<ApiPromise>(); | ||
const [formatted, setFormatted] = useState<string>(); | ||
const [isPolkaMaskInstalled, setIsSnapInstalled] = useState<boolean>(); | ||
const [balances, setBalances] = useState<DeriveBalancesAll>(); | ||
const [token, setToken] = useState<string>(); | ||
|
||
useEffect(() => { | ||
if (!api) { | ||
return; | ||
} | ||
setToken(api.registry.chainTokens[0]); | ||
}, [api]); | ||
|
||
useEffect(() => { | ||
if (!endpoint) { | ||
return; | ||
} | ||
const wsProvider = new WsProvider(endpoint); | ||
|
||
ApiPromise.create({ provider: wsProvider }).then(setApi); | ||
}, [endpoint]); | ||
|
||
useEffect(() => { | ||
isPolkaMaskInstalled && web3Enable('PolkaMask-dapp').then((ext: InjectedExtension[] | undefined) => { | ||
console.log('all injected extensions:', ext); | ||
setExtensions(ext); | ||
}); | ||
}, [isPolkaMaskInstalled]); | ||
|
||
useEffect(() => { | ||
/** To getPolkaMask account */ | ||
extensions?.length && | ||
web3Accounts().then((accounts: any) => { | ||
const maybePolkamaskAccount = accounts.find((account: InjectedAccountWithMeta) => account.meta.source === POLKAMASK_ACCOUNT_META_SOURCE) | ||
setAccount(maybePolkamaskAccount); | ||
console.info('PolkamaskAccount:', maybePolkamaskAccount); | ||
}); | ||
}, [extensions, isPolkaMaskInstalled]); | ||
|
||
useEffect(() => { | ||
/** To format account address based on the dApp chain */ | ||
const chain = getChain(currentChainName); | ||
const maybeChainLatestGenesisHash = chain?.genesisHash?.[0]; | ||
if (maybeChainLatestGenesisHash && account?.address) { | ||
const formatted = getFormatted(String(maybeChainLatestGenesisHash), account.address) | ||
setFormatted(formatted); | ||
} | ||
}, [account]); | ||
|
||
const handleInstallClick = useCallback(() => { | ||
installPolkaMask().then((installedSnap) => { | ||
installedSnap && setIsSnapInstalled(installedSnap[DEFAULT_SNAP_ORIGIN]?.enabled) | ||
}); | ||
}, []); | ||
|
||
useEffect(() => { | ||
handleInstallClick() | ||
}, [handleInstallClick]); | ||
|
||
useEffect(() => { | ||
// getSnaps().then((snaps) => { | ||
// if (snaps?.length) { | ||
// const isDefaultSnapInstalled = !!Object.keys(snaps).find((id) => POLKAMASK_SNAP_IDS.includes(id)); | ||
// setIsSnapInstalled(isDefaultSnapInstalled); | ||
// console.log('Installed snaps:', snaps) | ||
// } | ||
// }) | ||
}, []); | ||
|
||
useEffect(() => { | ||
api && formatted && api.derive.balances?.all(formatted).then(setBalances); | ||
}, [api, formatted]); | ||
|
||
|
||
const handleChange = (event: React.SyntheticEvent, newValue: number) => { | ||
setValue(newValue); | ||
}; | ||
|
||
return ( | ||
<> | ||
<Grid container item justifyContent="space-between" mb='50px' p='10px' sx={{ borderBottom: 1 }}> | ||
<Grid container item alignItems='center' xs={4}> | ||
<Box | ||
component="img" | ||
alt="polkamask logo" | ||
src={logo} | ||
sx={{ width: '30px', height: '30px', objectFit: 'cover' }} | ||
/> | ||
<Typography variant="h5" sx={{ fontWeight: '700', display: 'inline-block', ml: '5px' }}> | ||
PolkaMask | ||
</Typography> | ||
</Grid> | ||
<Grid item xs textAlign='right'> | ||
<Button | ||
variant="contained" | ||
color='secondary' | ||
onClick={handleInstallClick} | ||
disabled={isPolkaMaskInstalled} | ||
sx={{ fontSize: '16px', width: 'fit-content' }} | ||
> | ||
Install PolkaMask Snap | ||
</Button> | ||
</Grid> | ||
</Grid> | ||
<Grid container | ||
sx={{ flexGrow: 1, bgcolor: 'background.paper', display: 'flex', height: 224 }} | ||
> | ||
<Grid item sx={{ width: '20%' }}> | ||
<Tabs | ||
orientation="vertical" | ||
variant="scrollable" | ||
value={value} | ||
onChange={handleChange} | ||
aria-label="Vertical tabs example" | ||
sx={{ borderRight: 1, borderColor: 'divider' }} | ||
> | ||
<Tab label="Transfer Fund" {...a11yProps(0)} /> | ||
<Tab label="Sign Message" {...a11yProps(1)} /> | ||
<Tab label="Other" {...a11yProps(2)} /> | ||
</Tabs> | ||
</Grid> | ||
<Grid item sx={{ width: '79%' }}> | ||
|
||
<TabPanel value={value} index={0}> | ||
<TransferFund | ||
api={api} | ||
account={account} | ||
isPolkaMaskInstalled={isPolkaMaskInstalled} | ||
formatted={formatted} | ||
balances={balances} | ||
currentChainName={currentChainName} | ||
token={token} | ||
/> | ||
</TabPanel> | ||
<TabPanel value={value} index={1}> | ||
<SignMessage | ||
api={api} | ||
account={account} | ||
isPolkaMaskInstalled={isPolkaMaskInstalled} | ||
formatted={formatted} | ||
balances={balances} | ||
currentChainName={currentChainName} | ||
token={token} | ||
/> | ||
</TabPanel> | ||
</Grid> | ||
</Grid> | ||
</> | ||
); | ||
} |
Oops, something went wrong.