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

228 Transaction review #350

Merged
merged 9 commits into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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: 1 addition & 0 deletions src/app/types/enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export enum ActionType {
SET_ASSET_LOADING = "SET_ASSET_LOADING",
SET_TOKEN_CAN_NOT_CREATE_WARNING_POOLS = "SET_TOKEN_CAN_NOT_CREATE_WARNING_POOLS",
SET_TOKEN_CAN_NOT_CREATE_WARNING_SWAP = "SET_TOKEN_CAN_NOT_CREATE_WARNING_SWAP",
SET_BLOCK_HASH_FINALIZED = "SET_BLOCK_HASH_FINALIZED",
}

export enum TokenSelection {
Expand Down
4 changes: 4 additions & 0 deletions src/assets/img/open-link-arrow.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file removed src/components/molecule/.gitkeep
Empty file.
31 changes: 30 additions & 1 deletion src/components/organism/AddPoolLiquidity/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import TokenAmountInput from "../../molecule/TokenAmountInput";
import CreatePool from "../CreatePool";
import PoolSelectTokenModal from "../PoolSelectTokenModal";
import SwapAndPoolSuccessModal from "../SwapAndPoolSuccessModal";
import ReviewTransactionModal from "../ReviewTransactionModal";

type AssetTokenProps = {
tokenSymbol: string;
Expand Down Expand Up @@ -94,6 +95,7 @@ const AddPoolLiquidity = ({ tokenBId }: AddPoolLiquidityProps) => {
const [inputEdited, setInputEdited] = useState<InputEditedProps>({ inputType: InputEditedType.exactIn });
const [isModalOpen, setIsModalOpen] = useState<boolean>(false);
const [poolExists, setPoolExists] = useState<boolean>(false);
const [reviewModalOpen, setReviewModalOpen] = useState<boolean>(false);
const [tooManyDecimalsError, setTooManyDecimalsError] = useState<TokenDecimalsErrorProps>({
tokenSymbol: "",
isError: false,
Expand Down Expand Up @@ -137,6 +139,7 @@ const AddPoolLiquidity = ({ tokenBId }: AddPoolLiquidityProps) => {
};

const handlePool = async () => {
setReviewModalOpen(false);
if (waitingForTransaction) {
clearTimeout(waitingForTransaction);
}
Expand Down Expand Up @@ -643,12 +646,38 @@ const AddPoolLiquidity = ({ tokenBId }: AddPoolLiquidityProps) => {
</>
)}
<Button
onClick={() => (getButtonProperties.disabled ? null : handlePool())}
onClick={() => (getButtonProperties.disabled ? null : setReviewModalOpen(true))}
variant={ButtonVariants.btnInteractivePink}
disabled={getButtonProperties.disabled || addLiquidityLoading}
>
{addLiquidityLoading ? <LottieMedium /> : getButtonProperties.label}
</Button>
<ReviewTransactionModal
open={reviewModalOpen}
title="Review adding liquidity"
priceImpact={priceImpact}
youPay={selectedTokenA.tokenBalance}
youReceive={selectedTokenB.assetTokenBalance}
tokenValueA={
inputEdited.inputType === InputEditedType.exactIn
? selectedTokenAssetValue?.tokenValue
: selectedTokenNativeValue?.tokenValue
}
tokenValueB={
inputEdited.inputType === InputEditedType.exactIn
? formatDecimalsFromToken(assetTokenWithSlippage?.tokenValue, selectedTokenB.decimals)
: formatDecimalsFromToken(nativeTokenWithSlippage?.tokenValue, selectedTokenA.nativeTokenDecimals)
}
tokenSymbolA={selectedTokenA.nativeTokenSymbol}
tokenSymbolB={selectedTokenB.tokenSymbol}
onClose={() => {
setReviewModalOpen(false);
}}
inputType={inputEdited.inputType}
onConfirmTransaction={() => {
handlePool();
}}
/>
<PoolSelectTokenModal
onSelect={setSelectedTokenB}
onClose={() => setIsModalOpen(false)}
Expand Down
86 changes: 86 additions & 0 deletions src/components/organism/ReviewTransactionModal/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { FC } from "react";
import Modal from "../../atom/Modal";
import Button from "../../atom/Button";
import { ButtonVariants, InputEditedType } from "../../../app/types/enum";
import { ReactComponent as DotToken } from "../../../assets/img/dot-token.svg";

interface SwapSelectTokenModalProps {
open: boolean;
title: string;
youPay: string;
youReceive: string;
priceImpact: string;
tokenValueA?: string;
tokenValueB?: string;
tokenSymbolA: string;
tokenSymbolB: string;
inputType: string;
onClose: () => void;
onConfirmTransaction: () => void;
}

const ReviewTransactionModal: FC<SwapSelectTokenModalProps> = ({
open,
title,
youPay,
youReceive,
priceImpact,
tokenValueA,
tokenValueB,
tokenSymbolA,
tokenSymbolB,
inputType,
onClose,
onConfirmTransaction,
}) => {
return (
<Modal isOpen={open} onClose={onClose} title={title}>
<div className="flex w-[360px] flex-col gap-5">
<div className="flex flex-col items-start">
<span className="font-inter text-small text-gray-200">You pay</span>
<span className="flex w-full items-center justify-between font-unbounded-variable text-heading-4 font-bold text-gray-400">
{youPay}
<DotToken />
</span>
</div>
<div className="flex flex-col items-start">
<span className="font-inter text-small text-gray-200">You receive</span>
<span className="flex w-full items-center justify-between font-unbounded-variable text-heading-4 font-bold text-gray-400">
{youReceive}
<DotToken />
</span>
</div>
<hr className="mb-0.5 mt-1 w-full border-[0.7px] border-gray-50" />
<div className="flex flex-col">
<div className="flex justify-between">
<span className="font-inter text-medium text-gray-300">Price impact</span>
<span className="font-inter text-medium text-gray-400">{priceImpact}%</span>
</div>
<div className="flex justify-between">
<span className="font-inter text-medium text-gray-300">
{inputType == InputEditedType.exactIn ? "Expected output" : "Expected input"}
</span>
<span className="font-inter text-medium text-gray-400">
{tokenValueA} {tokenSymbolA}
</span>
</div>
<div className="flex justify-between">
<span className="font-inter text-medium text-gray-300">
{inputType === InputEditedType.exactIn ? "Minimum output" : "Maximum input"}t
</span>
<span className="font-inter text-medium text-gray-400">
{tokenValueB} {tokenSymbolB}
</span>
</div>
</div>
<div className="flex flex-col">
<Button onClick={onConfirmTransaction} variant={ButtonVariants.btnInteractivePink}>
Confirm Swap
</Button>
</div>
</div>
</Modal>
);
};

export default ReviewTransactionModal;
10 changes: 10 additions & 0 deletions src/components/organism/SwapAndPoolSuccessModal/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { FC } from "react";
import { ReactComponent as ArrowLeft } from "../../../assets/img/arrow-left.svg";
import { ReactComponent as ArrowRight } from "../../../assets/img/arrow-right.svg";
import { ReactComponent as OpenLinkArrow } from "../../../assets/img/open-link-arrow.svg";
import Modal from "../../atom/Modal";
import { useAppContext } from "../../../state";

interface SwapAndPoolSuccessModalProps {
open: boolean;
Expand All @@ -28,6 +30,8 @@ const SwapAndPoolSuccessModal: FC<SwapAndPoolSuccessModalProps> = ({
tokenB,
onClose,
}) => {
const { state } = useAppContext();
const { blockHashFinalized } = state;
return (
<div>
<Modal isOpen={open} onClose={onClose}>
Expand All @@ -47,6 +51,12 @@ const SwapAndPoolSuccessModal: FC<SwapAndPoolSuccessModalProps> = ({
{tokenA.icon} {tokenA.value} {tokenA.symbol} <ArrowRight /> {tokenB.icon} {tokenB.value} {tokenB.symbol}
</div>
</div>
<div className="flex flex-row items-center justify-center gap-1 font-unbounded-variable text-medium underline">
<a href={`https://assethub-rococo.subscan.io/block/${blockHashFinalized}`} target="_blank" rel="noreferrer">
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be variable based on network

View in block explorer
</a>
<OpenLinkArrow />
</div>
</div>
</Modal>
</div>
Expand Down
37 changes: 36 additions & 1 deletion src/components/organism/SwapTokens/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import WarningMessage from "../../atom/WarningMessage";
import TokenAmountInput from "../../molecule/TokenAmountInput";
import SwapAndPoolSuccessModal from "../SwapAndPoolSuccessModal";
import SwapSelectTokenModal from "../SwapSelectTokenModal";
import ReviewTransactionModal from "../ReviewTransactionModal";

type SwapTokenProps = {
tokenA: TokenProps;
Expand Down Expand Up @@ -124,6 +125,7 @@ const SwapTokens = () => {
});

const [isTransactionTimeout, setIsTransactionTimeout] = useState<boolean>(false);
const [reviewModalOpen, setReviewModalOpen] = useState<boolean>(false);
const [waitingForTransaction, setWaitingForTransaction] = useState<NodeJS.Timeout>();
const [priceImpact, setPriceImpact] = useState<string>("");
const [assetBPriceOfOneAssetA, setAssetBPriceOfOneAssetA] = useState<string>("");
Expand Down Expand Up @@ -573,6 +575,7 @@ const SwapTokens = () => {
};

const handleSwap = async () => {
setReviewModalOpen(false);
if (waitingForTransaction) {
clearTimeout(waitingForTransaction);
}
Expand Down Expand Up @@ -1317,7 +1320,7 @@ const SwapTokens = () => {
/>

<Button
onClick={() => (getSwapButtonProperties.disabled ? null : handleSwap())}
onClick={() => (getSwapButtonProperties.disabled ? null : setReviewModalOpen(true))}
variant={ButtonVariants.btnInteractivePink}
disabled={getSwapButtonProperties.disabled || swapLoading}
>
Expand Down Expand Up @@ -1350,6 +1353,38 @@ const SwapTokens = () => {
}}
actionLabel="Swapped"
/>
<ReviewTransactionModal
open={reviewModalOpen}
title="Review Swap"
priceImpact={priceImpact}
youPay={selectedTokenAValue.tokenValue}
youReceive={selectedTokenBValue.tokenValue}
tokenValueA={
inputEdited.inputType === InputEditedType.exactIn
? selectedTokenBValue.tokenValue
: selectedTokenAValue.tokenValue
}
tokenValueB={
inputEdited.inputType === InputEditedType.exactIn
? formatDecimalsFromToken(
formatInputTokenValue(tokenBValueForSwap.tokenValue, selectedTokens.tokenB.decimals),
selectedTokens.tokenB.decimals
)
: formatDecimalsFromToken(
formatInputTokenValue(tokenAValueForSwap.tokenValue, selectedTokens.tokenA.decimals),
selectedTokens.tokenA.decimals
)
}
tokenSymbolA={selectedTokens.tokenA.tokenSymbol}
tokenSymbolB={selectedTokens.tokenB.tokenSymbol}
onClose={() => {
setReviewModalOpen(false);
}}
inputType={inputEdited.inputType}
onConfirmTransaction={() => {
handleSwap();
}}
/>
</div>
<WarningMessage show={lowTradingMinimum} message={t("pageError.tradingMinimum")} />
<WarningMessage
Expand Down
32 changes: 30 additions & 2 deletions src/components/organism/WithdrawPoolLiquidity/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { useLocation, useNavigate, useParams } from "react-router-dom";
import useGetNetwork from "../../../app/hooks/useGetNetwork";
import { POOLS_PAGE } from "../../../app/router/routes";
import { LpTokenAsset } from "../../../app/types";
import { ActionType, ButtonVariants, LiquidityPageType } from "../../../app/types/enum";
import { ActionType, ButtonVariants, InputEditedType, LiquidityPageType } from "../../../app/types/enum";
import {
calculateSlippageReduce,
formatDecimalsFromToken,
Expand All @@ -28,6 +28,7 @@ import AmountPercentage from "../../molecule/AmountPercentage";
import TokenAmountInput from "../../molecule/TokenAmountInput";
import PoolSelectTokenModal from "../PoolSelectTokenModal";
import SwapAndPoolSuccessModal from "../SwapAndPoolSuccessModal";
import ReviewTransactionModal from "../ReviewTransactionModal";

type AssetTokenProps = {
tokenSymbol: string;
Expand Down Expand Up @@ -86,6 +87,7 @@ const WithdrawPoolLiquidity = () => {
const [minimumTokenAmountExceeded, setMinimumTokenAmountExceeded] = useState<boolean>(false);
const [withdrawAmountPercentage, setWithdrawAmountPercentage] = useState<number>(100);
const [maxPercentage, setMaxPercentage] = useState<number>(100);
const [reviewModalOpen, setReviewModalOpen] = useState<boolean>(false);
const [isTransactionTimeout, setIsTransactionTimeout] = useState<boolean>(false);
const [waitingForTransaction, setWaitingForTransaction] = useState<NodeJS.Timeout>();
const [assetBPriceOfOneAssetA, setAssetBPriceOfOneAssetA] = useState<string>("");
Expand Down Expand Up @@ -118,6 +120,7 @@ const WithdrawPoolLiquidity = () => {
};

const handlePool = async () => {
setReviewModalOpen(false);
const lpToken = Math.floor(Number(lpTokensAmountToBurn) * (withdrawAmountPercentage / 100)).toString();
if (waitingForTransaction) {
clearTimeout(waitingForTransaction);
Expand Down Expand Up @@ -586,7 +589,7 @@ const WithdrawPoolLiquidity = () => {
</>
)}
<Button
onClick={() => (getWithdrawButtonProperties.disabled ? null : handlePool())}
onClick={() => (getWithdrawButtonProperties.disabled ? null : setReviewModalOpen(true))}
variant={ButtonVariants.btnInteractivePink}
disabled={getWithdrawButtonProperties.disabled || withdrawLiquidityLoading}
>
Expand All @@ -598,6 +601,31 @@ const WithdrawPoolLiquidity = () => {
open={isModalOpen}
title={t("button.selectToken")}
/>
<ReviewTransactionModal
open={reviewModalOpen}
title="Review remove liquidity"
priceImpact={priceImpact}
youPay={
selectedTokenNativeValue?.tokenValue
? new Decimal(selectedTokenNativeValue?.tokenValue).mul(withdrawAmountPercentage).div(100).toFixed()
: ""
}
youReceive={formattedTokenBValue()}
tokenValueA={
selectedTokenAssetValue?.tokenValue &&
new Decimal(selectedTokenAssetValue?.tokenValue).times(withdrawAmountPercentage / 100).toString()
}
tokenValueB={formatDecimalsFromToken(assetTokenWithSlippage.tokenValue, selectedTokenB.decimals)}
tokenSymbolA={selectedTokenA.nativeTokenSymbol}
tokenSymbolB={selectedTokenB.tokenSymbol}
onClose={() => {
setReviewModalOpen(false);
}}
inputType={InputEditedType.exactIn}
onConfirmTransaction={() => {
handlePool();
}}
/>
<SwapAndPoolSuccessModal
open={successModalOpen}
onClose={closeSuccessModal}
Expand Down
9 changes: 6 additions & 3 deletions src/services/poolServices/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import dotAcpToast from "../../app/util/toast";
import NativeTokenIcon from "../../assets/img/dot-token.svg";
import AssetTokenIcon from "../../assets/img/test-token.svg";
import { PoolAction } from "../../store/pools/interface";
import { WalletAction } from "../../store/wallet/interface";

const { parents, nativeTokenSymbol } = useGetNetwork();

Expand Down Expand Up @@ -113,7 +114,7 @@ export const createPool = async (
minAssetTokenValue: string,
nativeTokenDecimals: string,
assetTokenDecimals: string,
dispatch: Dispatch<PoolAction>
dispatch: Dispatch<PoolAction | WalletAction>
) => {
const firstArg = api
.createType("MultiLocation", {
Expand Down Expand Up @@ -199,7 +200,7 @@ export const addLiquidity = async (
minAssetTokenValue: string,
nativeTokenDecimals: string,
assetTokenDecimals: string,
dispatch: Dispatch<PoolAction>
dispatch: Dispatch<PoolAction | WalletAction>
) => {
const firstArg = api
.createType("MultiLocation", {
Expand Down Expand Up @@ -271,6 +272,7 @@ export const addLiquidity = async (
if (response.status.type === ServiceResponseStatus.Finalized && !response.dispatchError) {
exactAddedLiquidityInPool(response.toHuman(), nativeTokenDecimals, assetTokenDecimals, dispatch);

dispatch({ type: ActionType.SET_BLOCK_HASH_FINALIZED, payload: response.status.asFinalized.toString() });
dispatch({ type: ActionType.SET_SUCCESS_MODAL_OPEN, payload: true });
dispatch({ type: ActionType.SET_ADD_LIQUIDITY_LOADING, payload: false });
const allPools = await getAllPools(api);
Expand All @@ -297,7 +299,7 @@ export const removeLiquidity = async (
minAssetTokenValue: string,
nativeTokenDecimals: string,
assetTokenDecimals: string,
dispatch: Dispatch<PoolAction>
dispatch: Dispatch<PoolAction | WalletAction>
) => {
const firstArg = api
.createType("MultiLocation", {
Expand Down Expand Up @@ -361,6 +363,7 @@ export const removeLiquidity = async (
if (response.status.type === ServiceResponseStatus.Finalized && !response.dispatchError) {
exactWithdrawnLiquidityFromPool(response.toHuman(), nativeTokenDecimals, assetTokenDecimals, dispatch);

dispatch({ type: ActionType.SET_BLOCK_HASH_FINALIZED, payload: response.status.asFinalized.toString() });
dispatch({ type: ActionType.SET_SUCCESS_MODAL_OPEN, payload: true });
dispatch({ type: ActionType.SET_WITHDRAW_LIQUIDITY_LOADING, payload: false });
const allPools = await getAllPools(api);
Expand Down
Loading