From 75df7a6ea9ba19c86c84e7d003c338cf2738aa32 Mon Sep 17 00:00:00 2001 From: Kami Date: Fri, 29 Dec 2023 02:37:36 +0330 Subject: [PATCH] refactor --- packages/dapp/src/util/consts.ts | 2 +- packages/snap/snap.manifest.json | 2 +- packages/snap/src/index.ts | 2 +- packages/snap/src/rpc/getAddress.ts | 46 ++++++---------- packages/snap/src/rpc/index.ts | 4 +- packages/snap/src/ui/accountDemo.ts | 53 +++++++++++++++++++ .../snap/src/{rpc => ui}/showConfirmTx.ts | 2 +- packages/snap/src/{rpc => ui}/txContent.ts | 2 +- .../snap/src/{rpc => util}/decodeTxMethod.ts | 2 +- yarn.lock | 10 ++-- 10 files changed, 82 insertions(+), 43 deletions(-) create mode 100644 packages/snap/src/ui/accountDemo.ts rename packages/snap/src/{rpc => ui}/showConfirmTx.ts (98%) rename packages/snap/src/{rpc => ui}/txContent.ts (98%) rename packages/snap/src/{rpc => util}/decodeTxMethod.ts (98%) diff --git a/packages/dapp/src/util/consts.ts b/packages/dapp/src/util/consts.ts index ce57e61..8c92659 100644 --- a/packages/dapp/src/util/consts.ts +++ b/packages/dapp/src/util/consts.ts @@ -1,4 +1,4 @@ -const SNAP_VERSION = '>=0.1.5'; +const SNAP_VERSION = '>=0.1.11'; const LOCAL_SNAP_ID = 'local:http://localhost:8080'; const NPM_SNAP_ID = 'npm:@polkagate/snap'; diff --git a/packages/snap/snap.manifest.json b/packages/snap/snap.manifest.json index a948e2f..1b12b5b 100644 --- a/packages/snap/snap.manifest.json +++ b/packages/snap/snap.manifest.json @@ -7,7 +7,7 @@ "url": "https://github.com/polkagate/polkamask.git" }, "source": { - "shasum": "ZFoWEuasiMv0vYHNobbL41b+OXLvI4geOjN1ZsZu7+I=", + "shasum": "9lPirWwIGYRLHHZKuVwdU75S7udO/tSOsdhR39CXJhM=", "location": { "npm": { "filePath": "dist/bundle.js", diff --git a/packages/snap/src/index.ts b/packages/snap/src/index.ts index 9e8fe70..2eba05c 100644 --- a/packages/snap/src/index.ts +++ b/packages/snap/src/index.ts @@ -6,7 +6,7 @@ import { getKeyPair } from './util/getKeyPair'; import { DEFAULT_CHAIN_NAME } from './defaults'; import { getBalances } from './util/getBalance'; import { getGenesisHash } from './chains'; -import { accountDemo } from './rpc/getAddress'; +import { accountDemo } from './ui/accountDemo'; export const onRpcRequest: OnRpcRequestHandler = async ({ origin, diff --git a/packages/snap/src/rpc/getAddress.ts b/packages/snap/src/rpc/getAddress.ts index 94d4180..cdfc27d 100644 --- a/packages/snap/src/rpc/getAddress.ts +++ b/packages/snap/src/rpc/getAddress.ts @@ -1,34 +1,8 @@ -import { copyable, divider, heading, panel, text } from '@metamask/snaps-sdk'; import { DEFAULT_CHAIN_NAME } from '../defaults'; import { getKeyPair } from '../util/getKeyPair'; -import { Balances, getBalances } from '../util/getBalance'; +import { getBalances } from '../util/getBalance'; import { getGenesisHash } from '../chains'; -import { getFormatted } from '../util/getFormatted'; - -export const accountDemo = (address: string, balances: Balances) => { - const polkadotGenesishash = getGenesisHash('polkadot'); - const addressOnPolkadot = getFormatted(polkadotGenesishash, address); - - const kusamaGenesishash = getGenesisHash('kusama'); - const addressOnPKusama = getFormatted(kusamaGenesishash, address); - - return panel([ - heading('Your Account on Different Chains'), - divider(), - panel([text('**Polkadot**'), copyable(addressOnPolkadot), divider()]), - panel([text('**Kusama**'), copyable(addressOnPKusama), divider()]), - panel([ - text('**Westend**'), - copyable(address), - text( - `Transferable: **${balances.transferable - .toHuman() - .replace(balances.token, '') - .trim()}** / ${balances.total.toHuman(true)}`, - ), - ]), - ]); -}; +import { accountDemo } from '../ui/accountDemo'; export const getAddress = async (chainName?: string): Promise => { const account = await getKeyPair(chainName || DEFAULT_CHAIN_NAME); @@ -50,8 +24,20 @@ export const getAddress = async (chainName?: string): Promise => { * @param address - The any chain address. */ async function showAccount(address: string) { - const genesisHash = getGenesisHash('westend'); // For testing purposes - const balances = await getBalances(genesisHash, address); + const westendGenesisHash = getGenesisHash('westend'); // These will be changed when dropdown component will be available + const westendBalances = await getBalances(westendGenesisHash, address); + + const polkadotGenesisHash = getGenesisHash('polkadot'); // These will be changed when dropdown component will be available + const polkadotBalances = await getBalances(polkadotGenesisHash, address); + + const kusamaGenesisHash = getGenesisHash('kusama'); // These will be changed when dropdown component will be available + const kusamaBalances = await getBalances(kusamaGenesisHash, address); + + const balances = { + westendBalances, + polkadotBalances, + kusamaBalances, + }; /** to show the address to user */ snap.request({ diff --git a/packages/snap/src/rpc/index.ts b/packages/snap/src/rpc/index.ts index 273c889..29e96b4 100644 --- a/packages/snap/src/rpc/index.ts +++ b/packages/snap/src/rpc/index.ts @@ -1,6 +1,6 @@ -export * from './showConfirmTx'; +export * from '../ui/showConfirmTx'; export * from './signRaw'; export * from './signJSON'; export * from './getAddress'; export * from './metadata'; -export * from './decodeTxMethod'; +export * from '../util/decodeTxMethod'; diff --git a/packages/snap/src/ui/accountDemo.ts b/packages/snap/src/ui/accountDemo.ts new file mode 100644 index 0000000..2df1ed5 --- /dev/null +++ b/packages/snap/src/ui/accountDemo.ts @@ -0,0 +1,53 @@ +import { copyable, divider, heading, panel, text } from '@metamask/snaps-sdk'; +import { Balances } from '../util/getBalance'; +import { getGenesisHash } from '../chains'; +import { getFormatted } from '../util/getFormatted'; + +type tokenBalance = Record; + +export const accountDemo = (address: string, balances: tokenBalance) => { + const polkadotGenesishash = getGenesisHash('polkadot'); + const addressOnPolkadot = getFormatted(polkadotGenesishash, address); + + const kusamaGenesishash = getGenesisHash('kusama'); + const addressOnPKusama = getFormatted(kusamaGenesishash, address); + + const { polkadotBalances, kusamaBalances, westendBalances } = balances; + + return panel([ + heading('Your Account on Different Chains'), + divider(), + panel([ + text('**Polkadot**'), + copyable(addressOnPolkadot), + text( + `Transferable: **${polkadotBalances.transferable + .toHuman() + .replace(polkadotBalances.token, '') + .trim()}** / ${polkadotBalances.total.toHuman()}`, + ), + divider(), + ]), + panel([ + text('**Kusama**'), + copyable(addressOnPKusama), + text( + `Transferable: **${kusamaBalances.transferable + .toHuman() + .replace(kusamaBalances.token, '') + .trim()}** / ${kusamaBalances.total.toHuman(true)}`, + ), + divider(), + ]), + panel([ + text('**Westend**'), + copyable(address), + text( + `Transferable: **${westendBalances.transferable + .toHuman() + .replace(westendBalances.token, '') + .trim()}** / ${westendBalances.total.toHuman(true)}`, + ), + ]), + ]); +}; diff --git a/packages/snap/src/rpc/showConfirmTx.ts b/packages/snap/src/ui/showConfirmTx.ts similarity index 98% rename from packages/snap/src/rpc/showConfirmTx.ts rename to packages/snap/src/ui/showConfirmTx.ts index 457336c..7316910 100644 --- a/packages/snap/src/rpc/showConfirmTx.ts +++ b/packages/snap/src/ui/showConfirmTx.ts @@ -10,7 +10,7 @@ import getChainName from '../util/getChainName'; import { formatCamelCase } from '../util/formatCamelCase'; import { getIdentity } from '../util/getIdentity'; import { txContent } from './txContent'; -import { Decoded, getDecoded } from '.'; +import { Decoded, getDecoded } from '../rpc'; const EMPTY_LOGO = ` diff --git a/packages/snap/src/rpc/txContent.ts b/packages/snap/src/ui/txContent.ts similarity index 98% rename from packages/snap/src/rpc/txContent.ts rename to packages/snap/src/ui/txContent.ts index 6ddd09a..c07414f 100644 --- a/packages/snap/src/rpc/txContent.ts +++ b/packages/snap/src/ui/txContent.ts @@ -4,7 +4,7 @@ import { copyable, divider, panel, text } from '@metamask/snaps-sdk'; import { ApiPromise } from '@polkadot/api'; import { AnyTuple } from '@polkadot/types/types'; import { amountToHuman } from '../util/amountToHuman'; -import { Decoded } from './decodeTxMethod'; +import { Decoded } from '../util/decodeTxMethod'; export const txContent = ( api: ApiPromise, diff --git a/packages/snap/src/rpc/decodeTxMethod.ts b/packages/snap/src/util/decodeTxMethod.ts similarity index 98% rename from packages/snap/src/rpc/decodeTxMethod.ts rename to packages/snap/src/util/decodeTxMethod.ts index 08bd505..953aff9 100644 --- a/packages/snap/src/rpc/decodeTxMethod.ts +++ b/packages/snap/src/util/decodeTxMethod.ts @@ -6,7 +6,7 @@ import { BN } from '@polkadot/util'; import type { AnyJson } from '@polkadot/types/types'; import { Metadata, TypeRegistry } from '@polkadot/types'; import { base64Decode } from '@polkadot/util-crypto'; -import { getSavedMeta } from '.'; +import { getSavedMeta } from '../rpc'; export type Decoded = { args: AnyJson | null; diff --git a/yarn.lock b/yarn.lock index 2d67180..1d76b23 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7310,9 +7310,9 @@ __metadata: languageName: node linkType: hard -"@polkagate/extension-dapp@npm:latest": - version: 0.46.6-18 - resolution: "@polkagate/extension-dapp@npm:0.46.6-18" +"@polkagate/extension-dapp@file:../../../polkadot-js-extension/packages/extension-dapp/build::locator=dapp%40workspace%3Apackages%2Fdapp": + version: 0.46.6 + resolution: "@polkagate/extension-dapp@file:../../../polkadot-js-extension/packages/extension-dapp/build#../../../polkadot-js-extension/packages/extension-dapp/build::hash=66b8e4&locator=dapp%40workspace%3Apackages%2Fdapp" dependencies: "@polkadot/extension-inject": 0.46.6 "@polkadot/util": ^12.6.1 @@ -7323,7 +7323,7 @@ __metadata: "@polkadot/types": "*" "@polkadot/util": "*" "@polkadot/util-crypto": "*" - checksum: 0cdb566a82025961deddb1c311dc6adc29d837fd2c473816f30d60f586294526c95dd311b82c9566024227005fa7f2510311edef4f83dc0f128fc2fc94c174c4 + checksum: e7865323aeef55ed797cbc12e65160eb779822359bd2c86771588889cd6b10ebd4a4a18915faad8113ac53efd64c09c73cc968cb1189ae465dc8b96213df815f languageName: node linkType: hard @@ -12228,7 +12228,7 @@ __metadata: "@polkadot/api-derive": latest "@polkadot/apps-config": latest "@polkadot/extension-inject": latest - "@polkagate/extension-dapp": latest + "@polkagate/extension-dapp": ../../../polkadot-js-extension/packages/extension-dapp/build "@testing-library/jest-dom": ^5.17.0 "@testing-library/react": ^13.4.0 "@testing-library/user-event": ^13.5.0