Skip to content

Commit

Permalink
finish balances typing and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
piyalbasu committed Sep 11, 2024
1 parent 82c3bb0 commit 13f8089
Show file tree
Hide file tree
Showing 16 changed files with 142 additions and 50 deletions.
1 change: 0 additions & 1 deletion @shared/api/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,6 @@ export const getAccountBalancesStandalone = async ({
liquidityPoolId: v.liquidityPoolId,
reserves: lp.reserves,
};
delete balances[k].liquidityPoolId;
}
}
isFunded = true;
Expand Down
4 changes: 3 additions & 1 deletion @shared/api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,10 @@ export interface NativeBalance extends Balance {
}

export interface TokenBalance extends AssetBalance {
decimals: number;
name: string;
symbol: string;
decimals: number;
total: BigNumber;
}

export interface BalanceMap {
Expand Down
13 changes: 11 additions & 2 deletions @shared/helpers/stellar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ import BigNumber from "bignumber.js";
import * as StellarSdk from "stellar-sdk";
import * as StellarSdkNext from "stellar-sdk-next";

import { BalanceMap, AssetBalance } from "@shared/api/types";
import {
BalanceMap,
AssetBalance,
BlockAidScanAssetResult,
} from "@shared/api/types";
import {
BASE_RESERVE,
BASE_RESERVE_MIN_COUNT,
NetworkDetails,
} from "@shared/constants/stellar";
import { defaultBlockaidScanAssetResult } from "../../extension/src/popup/helpers/blockaid";

export const CUSTOM_NETWORK = "STANDALONE";

Expand Down Expand Up @@ -43,6 +46,12 @@ export function getBalanceIdentifier(
}
}

export const defaultBlockaidScanAssetResult: BlockAidScanAssetResult = {
// eslint-disable-next-line @typescript-eslint/naming-convention
result_type: "Benign",
features: [{ description: "" }],
};

export function makeDisplayableBalances(
accountDetails: StellarSdk.Horizon.ServerApi.AccountRecord,
) {
Expand Down
29 changes: 29 additions & 0 deletions extension/e2e-tests/loadAccount.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { test, expect, expectPageToHaveScreenshot } from "./test-fixtures";
import { loginToTestAccount } from "./helpers/login";

test("Load accounts on standalone network", async ({ page, extensionId }) => {
test.slow();
await loginToTestAccount({ page, extensionId });
await page.getByTestId("network-selector-open").click();
await page.getByText("Add custom network").click();
await expect(page.getByText("Add Custom Network")).toBeVisible();
await expectPageToHaveScreenshot({
page,
screenshot: "network-form-page.png",
});
await page.getByTestId("NetworkForm__networkName").fill("test standalone");
await page
.getByTestId("NetworkForm__networkUrl")
.fill("https://horizon-testnet.stellar.org");
await page
.getByTestId("NetworkForm__sorobanRpcUrl")
.fill("https://soroban-testnet.stellar.org/");
await page
.getByTestId("NetworkForm__networkPassphrase")
.fill("Test SDF Network ; September 2015");
await page.getByTestId("NetworkForm__add").click();
await expect(page.getByTestId("account-view")).toBeVisible({
timeout: 30000,
});
await expect(page.getByTestId("account-assets")).toContainText("XLM");
});
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion extension/src/popup/__testHelpers__/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { Balances } from "@shared/api/types";

import { reducer as auth } from "popup/ducks/accountServices";
import { reducer as settings } from "popup/ducks/settings";
import { defaultBlockaidScanAssetResult } from "popup/helpers/blockaid";
import { defaultBlockaidScanAssetResult } from "@shared/helpers/stellar";
import {
reducer as transactionSubmission,
initialState as transactionSubmissionInitialState,
Expand Down
38 changes: 18 additions & 20 deletions extension/src/popup/components/account/AccountAssets/index.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import React, { useEffect, useState } from "react";
import { useSelector } from "react-redux";
import { BigNumber } from "bignumber.js";
import isEmpty from "lodash/isEmpty";
import { Asset, Horizon } from "stellar-sdk";

import { AssetIcons, BlockAidScanAssetResult } from "@shared/api/types";
import { AssetIcons, AssetType } from "@shared/api/types";
import { retryAssetIcon } from "@shared/api/internal";

import { getCanonicalFromAsset } from "helpers/stellar";
Expand Down Expand Up @@ -153,7 +152,7 @@ export const AssetIcon = ({

interface AccountAssetsProps {
assetIcons: AssetIcons;
sortedBalances: any[];
sortedBalances: AssetType[];
setSelectedAsset?: (selectedAsset: string) => void;
}

Expand Down Expand Up @@ -221,40 +220,39 @@ export const AccountAssets = ({

return (
<>
{sortedBalances.map((rb: any) => {
let issuer;
{sortedBalances.map((rb) => {
let isLP = false;
let issuer = {
key: "",
};
let code = "";
let amountUnit;
if (rb.liquidityPoolId) {
issuer = "lp";
isLP = true;
code = getLPShareCode(rb.reserves as Horizon.HorizonApi.Reserve[]);
amountUnit = "shares";
} else if (rb.contractId) {
} else if (rb.contractId && "symbol" in rb) {
issuer = {
key: rb.contractId,
};
code = rb.symbol;
amountUnit = rb.symbol;
} else {
issuer = rb.token.issuer;
if ("issuer" in rb.token && rb.token) {
issuer = rb.token.issuer;
}
code = rb.token.code;
amountUnit = rb.token.code;
}

const isLP = issuer === "lp";
const canonicalAsset = getCanonicalFromAsset(
code,
issuer?.key as string,
);
const canonicalAsset = getCanonicalFromAsset(code, issuer?.key);

const isSuspicious = isAssetSuspicious(
rb.blockaidData as BlockAidScanAssetResult,
);
const isSuspicious = isAssetSuspicious(rb.blockaidData);

const bigTotal = new BigNumber(rb.total as string);
const amountVal = rb.contractId
? formatTokenAmount(bigTotal, rb.decimals as number)
: bigTotal.toFixed();
const amountVal =
rb.contractId && "decimals" in rb
? formatTokenAmount(rb.total, rb.decimals)
: rb.total.toFixed();

return (
<div
Expand Down
6 changes: 2 additions & 4 deletions extension/src/popup/components/account/AssetDetail/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { IconButton, Icon } from "@stellar/design-system";
import { HorizonOperation, AssetType } from "@shared/api/types";
import { NetworkDetails } from "@shared/constants/stellar";
import { stellarSdkServer } from "@shared/api/helpers/stellarSdkServer";
import { defaultBlockaidScanAssetResult } from "@shared/helpers/stellar";
import {
getAvailableBalance,
getIsPayment,
Expand Down Expand Up @@ -47,10 +48,7 @@ import {
import { AppDispatch } from "popup/App";
import StellarLogo from "popup/assets/stellar-logo.png";
import { formatAmount } from "popup/helpers/formatters";
import {
isAssetSuspicious,
defaultBlockaidScanAssetResult,
} from "popup/helpers/blockaid";
import { isAssetSuspicious } from "popup/helpers/blockaid";
import { Loading } from "popup/components/Loading";
import { BlockaidAssetWarning } from "popup/components/WarningMessages";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { getAssetDomain } from "popup/helpers/getAssetDomain";
import { getNativeContractDetails } from "popup/helpers/searchAsset";
import { isAssetSuspicious } from "popup/helpers/blockaid";

import { Balances, BlockAidScanAssetResult } from "@shared/api/types";
import { Balances } from "@shared/api/types";

import { ManageAssetCurrency, ManageAssetRows } from "../ManageAssetRows";
import { SelectAssetRows } from "../SelectAssetRows";
Expand Down Expand Up @@ -103,9 +103,7 @@ export const ChooseAsset = ({ balances }: ChooseAssetProps) => {
image: assetIcons[getCanonicalFromAsset(code, issuer.key)],
domain,
contract: contractId,
isSuspicious: isAssetSuspicious(
blockaidData as BlockAidScanAssetResult,
),
isSuspicious: isAssetSuspicious(blockaidData),
});
// include native asset for asset dropdown selection
} else if (!isManagingAssets) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
} from "helpers/stellar";
import { isContractId } from "popup/helpers/soroban";
import { useNetworkFees } from "popup/helpers/useNetworkFees";
import { defaultBlockaidScanAssetResult } from "popup/helpers/blockaid";
import { defaultBlockaidScanAssetResult } from "@shared/helpers/stellar";

import { LoadingBackground } from "popup/basics/LoadingBackground";
import { ROUTES } from "popup/constants/routes";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ export const NetworkForm = ({ isEditing }: NetworkFormProps) => {
) : null}

<Input
data-testid="NetworkForm__networkName"
fieldSize="md"
disabled={isEditingDefaultNetworks}
id="networkName"
Expand All @@ -350,6 +351,7 @@ export const NetworkForm = ({ isEditing }: NetworkFormProps) => {
placeholder={t("Enter network name")}
/>
<Input
data-testid="NetworkForm__networkUrl"
fieldSize="md"
disabled={isEditingDefaultNetworks}
id="networkUrl"
Expand All @@ -367,6 +369,7 @@ export const NetworkForm = ({ isEditing }: NetworkFormProps) => {
{!isEditingDefaultNetworks ||
networkDetailsToEdit.network !== NETWORKS.PUBLIC ? (
<Input
data-testid="NetworkForm__sorobanRpcUrl"
fieldSize="md"
disabled={isEditingDefaultNetworks}
id="sorobanRpcUrl"
Expand All @@ -383,6 +386,7 @@ export const NetworkForm = ({ isEditing }: NetworkFormProps) => {
/>
) : null}
<Input
data-testid="NetworkForm__networkPassphrase"
fieldSize="md"
disabled={isEditingDefaultNetworks}
id="networkPassphrase"
Expand Down Expand Up @@ -478,6 +482,7 @@ export const NetworkForm = ({ isEditing }: NetworkFormProps) => {
) : (
<div className="NetworkForm__add-button">
<Button
data-testid="NetworkForm__add"
size="md"
variant="primary"
disabled={!(isValid && dirty)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@ import { navigateTo } from "popup/helpers/navigate";
import { useNetworkFees } from "popup/helpers/useNetworkFees";
import { useIsSwap, useIsSoroswapEnabled } from "popup/helpers/useIsSwap";
import { LP_IDENTIFIER } from "popup/helpers/account";
import {
isAssetSuspicious,
defaultBlockaidScanAssetResult,
} from "popup/helpers/blockaid";
import { isAssetSuspicious } from "popup/helpers/blockaid";
import { emitMetric } from "helpers/metrics";
import { useRunAfterUpdate } from "popup/helpers/useRunAfterUpdate";
import { getAssetDecimals, getTokenBalance } from "popup/helpers/soroban";
Expand Down Expand Up @@ -55,6 +52,7 @@ import {
import { ScamAssetWarning } from "popup/components/WarningMessages";
import { TX_SEND_MAX } from "popup/constants/transaction";
import { BASE_RESERVE } from "@shared/constants/stellar";
import { defaultBlockaidScanAssetResult } from "@shared/helpers/stellar";

import { BalanceMap, SorobanBalance } from "@shared/api/types";
import "../styles.scss";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ import {
} from "helpers/stellar";
import { getStellarExpertUrl } from "popup/helpers/account";
import { stellarSdkServer } from "@shared/api/helpers/stellarSdkServer";
import { AssetIcons, ActionStatus } from "@shared/api/types";
import { AssetBalance, AssetIcons, ActionStatus } from "@shared/api/types";
import { getIconUrlFromIssuer } from "@shared/api/helpers/getIconUrlFromIssuer";
import { isCustomNetwork } from "@shared/helpers/stellar";
import {
defaultBlockaidScanAssetResult,
isCustomNetwork,
} from "@shared/helpers/stellar";
import {
isAssetSuspicious,
useScanAsset,
useScanTx,
Expand Down Expand Up @@ -598,10 +600,10 @@ export const TransactionDetails = ({ goBack }: { goBack: () => void }) => {
token: {
issuer: { key: sourceAsset.issuer },
code: sourceAsset.code,
type: "credit_alphanum4",
},
total: amount || "0",
isMalicious: isSourceAssetSuspicious,
},
total: new BigNumber(amount),
} as AssetBalance,
]}
/>
</Card>
Expand Down
4 changes: 3 additions & 1 deletion extension/src/popup/helpers/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ export const sortOperationsByAsset = ({
assetOperationMap[getCanonicalFromAsset(code, issuer)] = [];
}
if ("contractId" in bal && "symbol" in bal) {
assetOperationMap[getCanonicalFromAsset(bal.symbol, bal.contractId)] = [];
assetOperationMap[
getCanonicalFromAsset(bal.symbol, bal.contractId || "")
] = [];
}
});

Expand Down
6 changes: 0 additions & 6 deletions extension/src/popup/helpers/blockaid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,3 @@ export const isAssetSuspicious = (blockaidData?: BlockAidScanAssetResult) => {
}
return blockaidData.result_type !== "Benign";
};

export const defaultBlockaidScanAssetResult: BlockAidScanAssetResult = {
// eslint-disable-next-line @typescript-eslint/naming-convention
result_type: "Benign",
features: [{ description: "" }],
};
Loading

0 comments on commit 13f8089

Please sign in to comment.