Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
remove unused

comment unused

remove unused

Update consts.ts

refactor
  • Loading branch information
Nick-1979 committed Jun 22, 2024
1 parent 02f1430 commit 572dd82
Show file tree
Hide file tree
Showing 12 changed files with 34 additions and 146 deletions.
4 changes: 2 additions & 2 deletions packages/dapp/src/util/consts.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const SNAP_VERSION = '>=0.1.14';
const LOCAL_SNAP_ID = 'local:http://localhost:8080';
// const NPM_SNAP_ID = 'npm:@polkagate/snap';
const NPM_SNAP_ID = 'npm:@polkagate/snap';

export const DEFAULT_SNAP_ORIGIN = LOCAL_SNAP_ID;
export const DEFAULT_SNAP_ORIGIN = process.env.POLKAGATE_NODE_ENV === 'development' ? LOCAL_SNAP_ID : NPM_SNAP_ID;

export const SUPPORTED_SNAPS = {
[DEFAULT_SNAP_ORIGIN]: { version: SNAP_VERSION },
Expand Down
17 changes: 0 additions & 17 deletions packages/snap/.depcheckrc.json

This file was deleted.

16 changes: 0 additions & 16 deletions packages/snap/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,5 @@
module.exports = {
extends: ['../../.eslintrc.js'],
parserOptions: {
tsconfigRootDir: __dirname,
},

ignorePatterns: ['!.eslintrc.js', 'dist/'],
overrides: [
{
files: ['*.ts', '*.tsx'],
rules: {
'@typescript-eslint/no-shadow': [
'error',
{
allow: ['Text'],
},
],
},
},
],
};
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/snap.git"
},
"source": {
"shasum": "58jiQ5tJoKgBrAlIj1zkwP91F59PV28otmy97y4F3Kk=",
"shasum": "X6m+PNFdGlFmegHJ2E6iNMeJTtbPLSW+m8HlkjU2DdM=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
26 changes: 12 additions & 14 deletions packages/snap/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,11 @@ import {
accountDemo,
accountInfo,
showDappList,
transferReview,
exportAccount,
showJsonContent,
getNextChain,
} from './ui';
import { getBalances2, getCurrentChain, getKeyPair } from './util';
import { getBalances, getCurrentChain, getKeyPair } from './util';

export const onRpcRequest: OnRpcRequestHandler = async ({
origin,
Expand Down Expand Up @@ -69,7 +68,7 @@ export const onHomePage: OnHomePageHandler = async () => {
const { address } = await getKeyPair(currentChainName);

const genesisHash = getGenesisHash(currentChainName); // These will be changed when dropdown component will be available
const balances = await getBalances2(genesisHash, address);
const balances = await getBalances(genesisHash, address);

return {
content: accountDemo(address, currentChainName, balances),
Expand Down Expand Up @@ -133,20 +132,19 @@ export const onUserInput: OnUserInputHandler = async ({ id, event }) => {
const { value } = event;

switch (event.name) {
case 'transferInput':
if (!event?.value?.amount) {
break;
}
await showSpinner(id);
await transferReview(id, value);
break;

case 'saveExportedAccount': {
// case 'transferInput': // will be uncommented when JSX components will be released
// if (!event?.value?.amount) {
// break;
// }
// await showSpinner(id);
// await transferReview(id, value);
// break;

case 'saveExportedAccount':
await showSpinner(id, 'Exporting the account ...');

await showJsonContent(id, value?.password);
break;
}

default:
break;
}
Expand Down
26 changes: 2 additions & 24 deletions packages/snap/src/rpc/getAddress.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,14 @@
import { DEFAULT_CHAIN_NAME } from '../defaults';
import { getKeyPair } from '../util/getKeyPair';
import { accountDemo } from '../ui/accountDemo';
import { getDefaultTokenBalances } from '../util/getDefaultTokenBalances';

export const getAddress = async (chainName?: string): Promise<string> => {
const account = await getKeyPair(chainName || DEFAULT_CHAIN_NAME);
const account = await getKeyPair(chainName ?? DEFAULT_CHAIN_NAME);

if (!account) {
throw new Error('account not found');
}

const { address } = account;

showAccount(address); // This can be removed in favour of onHomePage or onInstall

return address;
};

/**
* To show address(es) in some main chains format to users for a short while.
*
* @param address - The any chain address.
*/
async function showAccount(address: string) {
const balances = await getDefaultTokenBalances(address);

/** to show the address to user */
snap.request({
method: 'snap_dialog',
params: {
type: 'alert',
content: accountDemo(address, balances), // TODO: fix parameters
},
});
}
};
2 changes: 1 addition & 1 deletion packages/snap/src/ui/accountDemo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const accountDemo = (
// }),
button({
variant: 'primary',
value: 'dApp list',
value: 'DApps list',
name: 'dapp',
}),
button({
Expand Down
4 changes: 2 additions & 2 deletions packages/snap/src/ui/accountInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { getGenesisHash } from '../chains';
import { DEFAULT_CHAIN_NAME, CHAIN_NAMES } from '../defaults';
import { getState, updateState } from '../rpc';
import { getCurrentChain } from '../util';
import { getBalances2 } from '../util/getBalance';
import { getBalances } from '../util/getBalance';
import { getKeyPair } from '../util/getKeyPair';

/**
Expand Down Expand Up @@ -47,7 +47,7 @@ export async function accountInfo(id: string, chainName?: string) {

const genesisHash = getGenesisHash(_chainName);

const balances = await getBalances2(genesisHash, address);
const balances = await getBalances(genesisHash, address);

await snap.request({
method: 'snap_updateInterface',
Expand Down
2 changes: 1 addition & 1 deletion packages/snap/src/ui/dappList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export async function showDappList(id: string) {
params: {
id,
ui: panel([
heading('dApp List'),
heading('DApps List'),
divider(),
text(
'Explore these dApps to streamline your daily tasks and engage with the Polkadot ecosystem.',
Expand Down
6 changes: 3 additions & 3 deletions packages/snap/src/util/buildPayload.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ApiPromise } from '@polkadot/api';
import { SignerPayloadJSON } from '@polkadot/types/types';
import type { ApiPromise } from '@polkadot/api';
import type { SignerPayloadJSON } from '@polkadot/types/types';

export const buildPayload = async (
api: ApiPromise,
Expand Down Expand Up @@ -49,6 +49,6 @@ export const buildPayload = async (
version: payload.version,
});

return raw.toPayload() as SignerPayloadJSON;
return raw.toPayload();
}
};
51 changes: 10 additions & 41 deletions packages/snap/src/util/getBalance.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Balance } from '@polkadot/types/interfaces';
import type { Balance } from '@polkadot/types/interfaces';
import type { AccountData } from '@polkadot/types/interfaces/balances/types';

import { getApi } from './getApi';
import { getFormatted } from './getFormatted';

Expand All @@ -10,68 +11,36 @@ export type Balances = {
};

/**
* To get the balance of an address.
* To get the balances including locked one of an address.
*
* @param genesisHash - The genesisHash of the chain will be used to find an endpoint to use.
* @param address - An address to get its balances.
* @returns The total, transferable balance.
* @returns The total, transferable, and locked balances.
*/
export async function getBalances(
genesisHash: string,
address: string,
): Promise<Balances> {
const api = await getApi(genesisHash);
const formatted = getFormatted(genesisHash, address);
const token = api.registry.chainTokens[0];

const balances = (await api.query.system.account(formatted)) as unknown as {
data: AccountData;
};

const transferable = api.createType(
'Balance',
balances.data.free.sub(balances.data.frozen),
) as Balance;
const total = api.createType(
'Balance',
balances.data.free.add(balances.data.reserved),
) as Balance;

return { total, transferable, token };
}

/**
*
* @param genesisHash
* @param address
*/
export async function getBalances2(
genesisHash: string,
address: string,
): Promise<Balances> {
const api = await getApi(genesisHash);
console.log('api ', api);

const formatted = getFormatted(genesisHash, address);
console.log('formatted ', formatted);

const balances = (await api.query.system.account(formatted)) as unknown as {
data: AccountData;
};

console.log('balances ', balances);

const transferable = api.createType(
'Balance',
balances.data.free.sub(balances.data.frozen),
) as Balance;
) as unknown as Balance;

const total = api.createType(
'Balance',
balances.data.free.add(balances.data.reserved),
) as Balance;
) as unknown as Balance;

const locked = api.createType('Balance', balances.data.frozen) as Balance;
const locked = api.createType(
'Balance',
balances.data.frozen,
) as unknown as Balance;

return { total, transferable, locked };
}
24 changes: 0 additions & 24 deletions packages/snap/src/util/getDefaultTokenBalances.ts

This file was deleted.

0 comments on commit 572dd82

Please sign in to comment.