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

Pulling refs/heads/staging into test-staging #2084

Merged
merged 2 commits into from
Dec 10, 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
1 change: 0 additions & 1 deletion components/portfolio/TransferButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ const TransferModal = ({
address: string;
}) => {
const { data: assetMetadata, isSuccess } = useAllAssetMetadata();
const { data: chainConstants } = useChainConstants();
const notifications = useNotifications();

const options = useMemo<AssetOption[]>(() => {
Expand Down
27 changes: 19 additions & 8 deletions components/portfolio/WithdrawButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Dialog } from "@headlessui/react";
import type { ApiPromise } from "@polkadot/api";
import { isRpcSdk } from "@zeitgeistpm/sdk";
import FormTransactionButton from "components/ui/FormTransactionButton";
import Input from "components/ui/Input";
import Modal from "components/ui/Modal";
import SecondaryButton from "components/ui/SecondaryButton";
import Decimal from "decimal.js";
Expand All @@ -13,13 +14,13 @@ import { useSdkv2 } from "lib/hooks/useSdkv2";
import { useChain } from "lib/state/cross-chain";
import { useNotifications } from "lib/state/notifications";
import { useWallet } from "lib/state/wallet";
import { assetsAreEqual } from "lib/util/assets-are-equal";
import { convertDecimals } from "lib/util/convert-decimals";
import { countDecimals } from "lib/util/count-decimals";
import { formatNumberCompact } from "lib/util/format-compact";
import { useEffect, useState } from "react";
import { Controller, useForm } from "react-hook-form";
import Transfer from "./Transfer";
import Input from "components/ui/Input";
import { convertDecimals } from "lib/util/convert-decimals";
import { formatNumberCompact } from "lib/util/format-compact";

const WithdrawButton = ({
toChain,
Expand Down Expand Up @@ -136,6 +137,12 @@ const WithdrawModal = ({
? convertDecimals(new Decimal(amount), 0, assetDecimals)
: new Decimal(0);

const maxSendAmount = assetsAreEqual(fee?.assetId, {
ForeignAsset: foreignAssetId,
})
? balance.minus(fee?.amount ?? 0)
: balance;

const { send: transfer, isLoading } = useCrossChainExtrinsic(
() => {
if (isRpcSdk(sdk) && wallet.realAddress) {
Expand Down Expand Up @@ -181,18 +188,22 @@ const WithdrawModal = ({
if (name === "percentage") {
setValue(
"amount",
balance.mul(value.percentage).div(100).div(ZTG).toNumber(),
maxSendAmount.mul(value.percentage).div(100).div(ZTG).toNumber(),
);
} else if (name === "amount" && value.amount !== "") {
setValue(
"percentage",
new Decimal(value.amount).mul(ZTG).div(balance).mul(100).toString(),
new Decimal(value.amount)
.mul(ZTG)
.div(maxSendAmount)
.mul(100)
.toString(),
);
}
trigger("amount");
});
return () => subscription.unsubscribe();
}, [watch]);
}, [watch, maxSendAmount]);

const onSubmit = () => {
transfer();
Expand Down Expand Up @@ -233,8 +244,8 @@ const WithdrawModal = ({
},
//todo: validate transfer where fee is paid in same asset as the one being transferred
validate: (value) => {
if (balance.div(ZTG).lessThan(value)) {
return `Insufficient balance. Current balance: ${balance
if (maxSendAmount.div(ZTG).lessThan(value)) {
return `Insufficient balance. Current balance: ${maxSendAmount
.div(ZTG)
.toFixed(3)}`;
} else if (value <= 0) {
Expand Down