Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick-1979 committed Dec 28, 2023
1 parent d304b12 commit 75df7a6
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 43 deletions.
2 changes: 1 addition & 1 deletion packages/dapp/src/util/consts.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down
2 changes: 1 addition & 1 deletion packages/snap/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion packages/snap/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
46 changes: 16 additions & 30 deletions packages/snap/src/rpc/getAddress.ts
Original file line number Diff line number Diff line change
@@ -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<string> => {
const account = await getKeyPair(chainName || DEFAULT_CHAIN_NAME);
Expand All @@ -50,8 +24,20 @@ export const getAddress = async (chainName?: string): Promise<string> => {
* @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({
Expand Down
4 changes: 2 additions & 2 deletions packages/snap/src/rpc/index.ts
Original file line number Diff line number Diff line change
@@ -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';
53 changes: 53 additions & 0 deletions packages/snap/src/ui/accountDemo.ts
Original file line number Diff line number Diff line change
@@ -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<string, Balances>;

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)}`,
),
]),
]);
};
Original file line number Diff line number Diff line change
Expand Up @@ -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 = `<svg width="100" height="100">
<circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 5 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -7323,7 +7323,7 @@ __metadata:
"@polkadot/types": "*"
"@polkadot/util": "*"
"@polkadot/util-crypto": "*"
checksum: 0cdb566a82025961deddb1c311dc6adc29d837fd2c473816f30d60f586294526c95dd311b82c9566024227005fa7f2510311edef4f83dc0f128fc2fc94c174c4
checksum: e7865323aeef55ed797cbc12e65160eb779822359bd2c86771588889cd6b10ebd4a4a18915faad8113ac53efd64c09c73cc968cb1189ae465dc8b96213df815f
languageName: node
linkType: hard

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 75df7a6

Please sign in to comment.