Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #41

Merged
merged 8 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"build:chrome": "BROWSER_TARGET=CHROME vite build",
"build:firefox": "BROWSER_TARGET=FIREFOX vite build",
"build": "NODE_ENV=production tsc --noEmit && npm run build:chrome && npm run build:firefox",
"dev": "NODE_ENV=develop BROWSER_TARGET=CHROME nodemon",
"dev": "NODE_ENV=develop BROWSER_TARGET=CHROME vite build --watch",
"dev:firefox": "NODE_ENV=develop BROWSER_TARGET=FIREFOX nodemon",
"test": "vitest --reporter=verbose",
"test:cov": "vitest --reporter=verbose run --coverage",
Expand Down
6 changes: 6 additions & 0 deletions src/pages/manageAssets/ManageAssets.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,17 @@ describe("ManageAssets", () => {
state: {
selectedChain: CHAINS[0].chains[3],
type: AccountType.EVM,
api: null
},
}),
useThemeContext: () => ({
color: "red",
}),
useAccountContext: () => ({
state: {
selectedAccount: {}
}
})
}));

vi.mock("react-router-dom", () => ({
Expand Down
11 changes: 8 additions & 3 deletions src/pages/manageAssets/ManageAssets.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { useNavigate } from "react-router-dom";
import { useToast } from "@src/hooks";
import Extension from "@src/Extension";
import { BALANCE } from "@src/routes/paths";
import { useAssetContext, useNetworkContext } from "@src/providers";
import { useAccountContext, useAssetContext, useNetworkContext } from "@src/providers";
import { number, object, string } from "yup";
import { isHex, u8aToHex } from "@polkadot/util";
import { useForm } from "react-hook-form";
Expand All @@ -29,8 +29,9 @@ export const ManageAssets = () => {
const { t } = useTranslation("manage_assets");
const { t: tCommon } = useTranslation("common");
const {
state: { selectedChain, type },
state: { selectedChain, type, api },
} = useNetworkContext();
const { state: { selectedAccount } } = useAccountContext()
const { loadAssets } = useAssetContext();

const navigate = useNavigate();
Expand Down Expand Up @@ -71,7 +72,11 @@ export const ManageAssets = () => {
const onSubmit = handleSubmit(async (data) => {
try {
await Extension.addAsset(selectedChain.name, data);
loadAssets();
loadAssets({
api,
selectedChain,
selectedAccount
});
navigate(BALANCE);
} catch (error) {
showErrorToast(error);
Expand Down
20 changes: 2 additions & 18 deletions src/pages/send/Send.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ import { useAccountContext, useNetworkContext } from "@src/providers";
import { yupResolver } from "@hookform/resolvers/yup";
import { number, object, string } from "yup";
import { useLoading, useToast } from "@src/hooks";
import { decodeAddress, encodeAddress } from "@polkadot/keyring";
import { useNavigate } from "react-router-dom";
import { isHex } from "@polkadot/util";
import { isAddress } from "ethers/lib/utils";
import { AccountType } from "@src/accounts/types";
import { ConfirmTx, WasmForm, EvmForm } from "./components";
import { BALANCE } from "@src/routes/paths";
Expand All @@ -19,6 +16,7 @@ import { BigNumber, Contract } from "ethers";
import { getWebAPI } from "@src/utils/env";
import { XCM_MAPPING } from "@src/xcm/extrinsics";
import { MapResponseEVM } from "@src/xcm/interfaces";
import { isValidAddress } from "@src/utils/account-utils";

const WebAPI = getWebAPI();

Expand Down Expand Up @@ -52,21 +50,7 @@ export const Send = () => {
.test(
"valid address",
tCommon("invalid_address") as string,
(address) => {
try {
if (!address) return false;

if (isHex(address)) {
return isAddress(address);
} else {
encodeAddress(decodeAddress(address));
}

return true;
} catch (error) {
return false;
}
}
(address) => isValidAddress(address)
)
.required(t("required") as string),
amount: number().required(t("required") as string),
Expand Down
16 changes: 11 additions & 5 deletions src/pages/send/components/EvmForm.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ describe("EvmForm", () => {

if (value === "to") {
return {
address: "0x123",
address: "0xa0a58b72969DF1904Bf2315f2D041AD639737429",
};
}

Expand All @@ -88,7 +88,7 @@ describe("EvmForm", () => {
decimals: 18,
};
case "destinationAccount":
return "0x123";
return "0xa0a58b72969DF1904Bf2315f2D041AD639737429";
default:
return "";
}
Expand All @@ -113,7 +113,9 @@ describe("EvmForm", () => {
Wallet: class Wallet { },
Contract: class Contract {
estimateGas = {
transfer: vi.fn().mockReturnValue(BigNumber.from("21000")),
transfer: vi.fn().mockReturnValue(
Promise.resolve(BigNumber.from("21000"))
),
};
},
},
Expand Down Expand Up @@ -152,7 +154,7 @@ describe("EvmForm", () => {

if (value === "to") {
return {
address: "0x123",
address: "0xa0a58b72969DF1904Bf2315f2D041AD639737429",
};
}

Expand All @@ -177,7 +179,7 @@ describe("EvmForm", () => {
balance: BigNumber.from("1000000000000000000"),
};
case "destinationAccount":
return "0x123";
return "0xa0a58b72969DF1904Bf2315f2D041AD639737429";
default:
return "";
}
Expand All @@ -199,4 +201,8 @@ describe("EvmForm", () => {
});
expect(confirmTx).toHaveBeenCalled();
});

it("should return error calculating gas fee", async () => {

})
});
26 changes: 23 additions & 3 deletions src/pages/send/components/EvmForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { captureError } from "@src/utils/error-handling";
import { XCM_MAPPING } from "@src/xcm/extrinsics";
import { MapResponseEVM } from "@src/xcm/interfaces";
import { ShowBalance } from "./ShowBalance";
import { isValidAddress } from "@src/utils/account-utils";

interface EvmFormProps {
confirmTx: confirmTx;
Expand Down Expand Up @@ -84,8 +85,10 @@ export const EvmForm: FC<EvmFormProps> = ({ confirmTx }) => {
}
}, []);


useEffect(() => {
if (destinationIsInvalid || !destinationAccount || amount <= 0) return;
const isXcm = getValues("isXcm");
if (destinationIsInvalid || !isValidAddress(destinationAccount, isXcm ? undefined : "evm") || amount <= 0) return;
(async () => {
setIsLoadingFee(true);

Expand All @@ -94,11 +97,12 @@ export const EvmForm: FC<EvmFormProps> = ({ confirmTx }) => {
? Number(amount) * currencyUnits
: Number(amount) * 10 ** asset.decimals;

if (isNaN(_amount)) return;

const bnAmount = ethers.BigNumber.from(
_amount.toLocaleString("fullwide", { useGrouping: false })
);

const isXcm = getValues("isXcm");
const to = getValues("to");

if (isXcm) {
Expand Down Expand Up @@ -204,7 +208,7 @@ export const EvmForm: FC<EvmFormProps> = ({ confirmTx }) => {
const gasLimit = await contract.estimateGas.transfer(
destinationAccount,
bnAmount
);
).catch(() => BigNumber.from("21000"));

const _gasLimit = gasLimit;
const _maxFeePerGas = feeData.maxFeePerGas as ethers.BigNumber;
Expand All @@ -228,6 +232,16 @@ export const EvmForm: FC<EvmFormProps> = ({ confirmTx }) => {
setEvmTx(contract);
}
} catch (error) {
console.log('fee', {
error
})
setFee({
"gas limit": BigNumber0,
"max fee per gas": BigNumber0,
"max priority fee per gas": BigNumber0,
"estimated fee": BigNumber0,
"estimated total": BigNumber0,
})
captureError(error);
showErrorToast(tCommon("failed_to_get_fees"));
} finally {
Expand Down Expand Up @@ -255,6 +269,9 @@ export const EvmForm: FC<EvmFormProps> = ({ confirmTx }) => {
? Number(amount) * currencyUnits
: Number(amount) * 10 ** asset.decimals;


if (isNaN(_amount)) return false;

const bnAmount = BigNumber.from(
_amount.toLocaleString("fullwide", { useGrouping: false })
);
Expand All @@ -273,6 +290,9 @@ export const EvmForm: FC<EvmFormProps> = ({ confirmTx }) => {
);
}
} catch (error) {
console.log('isEnoughToPay', {
error
})
captureError(error);
return false;
}
Expand Down
4 changes: 4 additions & 0 deletions src/pages/send/components/WasmForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ export const WasmForm: FC<WasmFormProps> = ({ confirmTx }) => {
? amount * currencyUnits
: amount * 10 ** asset?.decimals || 0;

if (isNaN(_amount)) return;

const bnAmount = new BN(
String(_amount.toLocaleString("fullwide", { useGrouping: false }))
);
Expand Down Expand Up @@ -265,6 +267,8 @@ export const WasmForm: FC<WasmFormProps> = ({ confirmTx }) => {
? amount * currencyUnits
: amount * 10 ** asset.decimals;

if (isNaN(_amount)) return;

const bnAmount = new BN(
_amount.toLocaleString("fullwide", { useGrouping: false })
);
Expand Down
Loading