Skip to content

Commit

Permalink
add getDefaultTokenBalances
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick-1979 committed Dec 30, 2023
1 parent 2c3b43d commit 13b70b5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 20 deletions.
6 changes: 2 additions & 4 deletions packages/snap/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ import { getAddress, signJSON, signRaw } from './rpc';
import { getMetadataList, setMetadata } from './rpc/metadata';
import { getKeyPair } from './util/getKeyPair';
import { DEFAULT_CHAIN_NAME } from './defaults';
import { getBalances } from './util/getBalance';
import { getGenesisHash } from './chains';
import { accountDemo } from './ui/accountDemo';
import { getDefaultTokenBalances } from './util/getDefaultTokenBalances';

export const onRpcRequest: OnRpcRequestHandler = async ({
origin,
Expand Down Expand Up @@ -43,8 +42,7 @@ export const onRpcRequest: OnRpcRequestHandler = async ({
*/
export const onHomePage: OnHomePageHandler = async () => {
const { address } = await getKeyPair(DEFAULT_CHAIN_NAME);
const genesisHash = getGenesisHash('westend'); // For testing purposes
const balances = await getBalances(genesisHash, address);
const balances = await getDefaultTokenBalances(address);

return {
content: accountDemo(address, balances),
Expand Down
18 changes: 2 additions & 16 deletions packages/snap/src/rpc/getAddress.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { DEFAULT_CHAIN_NAME } from '../defaults';
import { getKeyPair } from '../util/getKeyPair';
import { getBalances } from '../util/getBalance';
import { getGenesisHash } from '../chains';
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);
Expand All @@ -24,20 +23,7 @@ export const getAddress = async (chainName?: string): Promise<string> => {
* @param address - The any chain address.
*/
async function showAccount(address: string) {
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,
};
const balances = await getDefaultTokenBalances(address);

/** to show the address to user */
snap.request({
Expand Down
24 changes: 24 additions & 0 deletions packages/snap/src/util/getDefaultTokenBalances.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { getGenesisHash } from '../chains';
import { getBalances } from './getBalance';

/**
* To get balances on the tree default chains, Polkadot, Kusama, and Westend.
*
* @param address - The any chain address.
*/
export async function getDefaultTokenBalances(address: string) {
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);

return {
westendBalances,
polkadotBalances,
kusamaBalances,
};
}

0 comments on commit 13b70b5

Please sign in to comment.