Skip to content

Commit

Permalink
Merge branch 'jose/fe-575-fetch-bridge-providers-quote' into jon/fe-5…
Browse files Browse the repository at this point in the history
…40-ui-confirmation-screen
  • Loading branch information
jonator committed Jul 1, 2024
2 parents 68321b2 + 1f01db5 commit 0ac0ab1
Show file tree
Hide file tree
Showing 3 changed files with 213 additions and 99 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
import { CoinPretty } from "@keplr-wallet/unit";
import { BridgeChain } from "@osmosis-labs/bridge";
import { MinimalAsset } from "@osmosis-labs/types";
import { isNil } from "@osmosis-labs/utils";
import { observer } from "mobx-react-lite";
import { useState } from "react";

import { AmountScreen } from "~/components/bridge/immersive/amount-screen";
import { ImmersiveBridgeScreens } from "~/components/bridge/immersive/immersive-bridge";
import { useBridgeQuote } from "~/components/bridge/immersive/use-bridge-quote";
import { useBridgesSupportedAssets } from "~/components/bridge/immersive/use-bridges-supported-assets";
import { Screen } from "~/components/screen-manager";
import { Button } from "~/components/ui/button";
import { useEvmWalletAccount } from "~/hooks/evm-wallet";
import { useStore } from "~/stores";

export type SupportedAsset = ReturnType<
typeof useBridgesSupportedAssets
>["supportedAssetsByChainId"][string][number];

export type SupportedAssetWithAmount = SupportedAsset & { amount: CoinPretty };

interface AmountAndConfirmationScreenProps {
direction: "deposit" | "withdraw";
selectedAssetDenom: string | undefined;
onClose: () => void;
}

export const AmountAndConfirmationScreen = observer(
({
direction,
selectedAssetDenom,
onClose,
}: AmountAndConfirmationScreenProps) => {
const { accountStore } = useStore();

const [sourceAsset, setSourceAsset] = useState<SupportedAssetWithAmount>();
const [destinationAsset, setDestinationAsset] = useState<MinimalAsset>();
const [fromChain, setFromChain] = useState<BridgeChain>();
const [toChain, setToChain] = useState<BridgeChain>();

const [cryptoAmount, setCryptoAmount] = useState<string>("0");
const [fiatAmount, setFiatAmount] = useState<string>("0");

// Wallets
const destinationAccount = accountStore.getWallet(
accountStore.osmosisChainId
);
const { address: evmAddress } = useEvmWalletAccount();

const sourceChain = direction === "deposit" ? fromChain : toChain;
const destinationChain = direction === "deposit" ? toChain : fromChain;

const cosmosCounterpartyAccount =
sourceChain?.chainType === "evm" || isNil(sourceChain)
? undefined
: accountStore.getWallet(sourceChain.chainId);

const sourceAddress =
sourceChain?.chainType === "evm"
? evmAddress
: cosmosCounterpartyAccount?.address;

const quote = useBridgeQuote({
destinationAddress: destinationAccount?.address,
destinationChain,
destinationAsset: destinationAsset
? {
address: destinationAsset.coinMinimalDenom,
decimals: destinationAsset.coinDecimals,
denom: destinationAsset.coinDenom,
}
: undefined,
sourceAddress,
sourceChain,
sourceAsset,
direction,
onRequestClose: onClose,
inputAmount: cryptoAmount,
bridges: sourceAsset?.supportedProviders,
onTransfer: () => {
setCryptoAmount("0");
setFiatAmount("0");
},
});

if (!selectedAssetDenom) return;

return (
<>
<Screen screenName={ImmersiveBridgeScreens.Amount}>
{() => (
<AmountScreen
direction={direction}
selectedDenom={selectedAssetDenom!}
sourceChain={sourceChain}
destinationChain={destinationChain}
fromChain={fromChain}
setFromChain={setFromChain}
toChain={toChain}
setToChain={setToChain}
sourceAsset={sourceAsset}
setSourceAsset={setSourceAsset}
destinationAsset={destinationAsset}
setDestinationAsset={setDestinationAsset}
cryptoAmount={cryptoAmount}
setCryptoAmount={setCryptoAmount}
fiatAmount={fiatAmount}
setFiatAmount={setFiatAmount}
quote={quote}
/>
)}
</Screen>
<Screen screenName={ImmersiveBridgeScreens.Review}>
{({ goBack }) => (
<div>
<h6>Step 3: Review</h6>
<Button onClick={goBack}>Back</Button>
<Button onClick={onClose}>Close</Button>
</div>
)}
</Screen>
</>
);
}
);
143 changes: 79 additions & 64 deletions packages/web/components/bridge/immersive/amount-screen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
} from "react";

import { Icon } from "~/components/assets";
import { SupportedAssetWithAmount } from "~/components/bridge/immersive/amount-and-confirmation-screen";
import { BridgeNetworkSelectModal } from "~/components/bridge/immersive/bridge-network-select-modal";
import { BridgeProviderDropdown } from "~/components/bridge/immersive/bridge-provider-dropdown";
import { BridgeQuoteRemainingTime } from "~/components/bridge/immersive/bridge-quote-remaining-time";
Expand Down Expand Up @@ -56,24 +57,75 @@ interface AmountScreenProps {
selectedDenom: string;

/**
* Includes both the canonical asset and its variants.
* Chain taking into account the direction.
*/
assetsInOsmosis: MinimalAsset[] | undefined;
sourceChain: BridgeChain | undefined;
destinationChain: BridgeChain | undefined;

onClose: () => void;
fromChain: BridgeChain | undefined;
setFromChain: (chain: BridgeChain) => void;
toChain: BridgeChain | undefined;
setToChain: (chain: BridgeChain) => void;

sourceAsset: SupportedAssetWithAmount | undefined;
setSourceAsset: (asset: SupportedAssetWithAmount | undefined) => void;
destinationAsset: MinimalAsset | undefined;
setDestinationAsset: (asset: MinimalAsset | undefined) => void;

cryptoAmount: string;
fiatAmount: string;
setCryptoAmount: (amount: string) => void;
setFiatAmount: (amount: string) => void;

quote: ReturnType<typeof useBridgeQuote>;
}

export const AmountScreen = observer(
({
direction,
assetsInOsmosis,
selectedDenom,
onClose,

sourceChain,

fromChain,
setFromChain,
toChain,
setToChain,

sourceAsset,
setSourceAsset,

destinationAsset,
setDestinationAsset,

cryptoAmount,
setCryptoAmount,
fiatAmount,
setFiatAmount,

quote,
}: AmountScreenProps) => {
const { accountStore } = useStore();
const { onOpenWalletSelect } = useWalletSelect();
const { t } = useTranslation();

const {
selectedQuote,
successfulQuotes,
setSelectedBridgeProvider,
buttonErrorMessage,
buttonText,
isLoadingBridgeQuote,
isLoadingBridgeTransaction,
isRefetchingQuote,
selectedQuoteUpdatedAt,
refetchInterval,
isInsufficientBal,
isInsufficientFee,
warnUserOfPriceImpact,
warnUserOfSlippage,
} = quote;

const { accountActionButton: connectWalletButton, walletConnected } =
useConnectWalletModalRedirect(
{
Expand All @@ -82,18 +134,9 @@ export const AmountScreen = observer(
noop
);

const [sourceAsset, setSourceAsset] = useState<
SupportedAsset & { amount: CoinPretty }
>();
const [destinationAsset, setDestinationAsset] = useState<MinimalAsset>();
const [fromChain, setFromChain] = useState<BridgeChain>();
const [toChain, setToChain] = useState<BridgeChain>();

const [areMoreOptionsVisible, setAreMoreOptionsVisible] = useState(false);

const [inputUnit, setInputUnit] = useState<"crypto" | "fiat">("fiat");
const [cryptoAmount, setCryptoAmount] = useState<string>("0");
const [fiatAmount, setFiatAmount] = useState<string>("0");
const {
isOpen: isBridgeWalletSelectOpen,
onClose: onCloseBridgeWalletSelect,
Expand All @@ -110,9 +153,6 @@ export const AmountScreen = observer(
isConnected: isEvmWalletConnected,
} = useEvmWalletAccount();

const sourceChain = direction === "deposit" ? fromChain : toChain;
const destinationChain = direction === "deposit" ? toChain : fromChain;

const cosmosCounterpartyAccountRepo =
sourceChain?.chainType === "evm" || isNil(sourceChain)
? undefined
Expand All @@ -127,6 +167,18 @@ export const AmountScreen = observer(
? evmAddress
: cosmosCounterpartyAccount?.address;

const { data: assetsInOsmosis } =
api.edge.assets.getCanonicalAssetWithVariants.useQuery(
{
findMinDenomOrSymbol: selectedDenom!,
},
{
enabled: !isNil(selectedDenom),
cacheTime: 10 * 60 * 1000, // 10 minutes
staleTime: 10 * 60 * 1000, // 10 minutes
}
);

const { data: osmosisChain } = api.edge.chains.getChain.useQuery({
findChainNameOrId: accountStore.osmosisChainId,
});
Expand Down Expand Up @@ -259,7 +311,7 @@ export const AmountScreen = observer(

setDestinationAsset(destinationAsset);
}
}, [assetsInOsmosis, selectedDenom, sourceAsset]);
}, [assetsInOsmosis, setDestinationAsset, sourceAsset]);

/**
* Set the osmosis chain based on the direction
Expand All @@ -274,13 +326,7 @@ export const AmountScreen = observer(
chainType: "cosmos",
});
}
}, [
accountStore.osmosisChainId,
direction,
fromChain,
osmosisChain,
toChain,
]);
}, [direction, fromChain, osmosisChain, setFromChain, setToChain, toChain]);

/**
* Set the initial chain based on the direction.
Expand All @@ -302,7 +348,14 @@ export const AmountScreen = observer(
chainType: firstChain.chainType,
} as BridgeChain);
}
}, [direction, fromChain, supportedChains, toChain]);
}, [
direction,
fromChain,
setFromChain,
setToChain,
supportedChains,
toChain,
]);

/**
* Connect cosmos wallet to the counterparty chain
Expand Down Expand Up @@ -376,44 +429,6 @@ export const AmountScreen = observer(
toChain,
]);

const {
selectedQuote,
successfulQuotes,
setSelectedBridgeProvider,
buttonErrorMessage,
buttonText,
isLoadingBridgeQuote,
isLoadingBridgeTransaction,
isRefetchingQuote,
selectedQuoteUpdatedAt,
refetchInterval,
isInsufficientBal,
isInsufficientFee,
warnUserOfPriceImpact,
warnUserOfSlippage,
} = useBridgeQuote({
destinationAddress: destinationAccount?.address,
destinationChain,
destinationAsset: destinationAsset
? {
address: destinationAsset.coinMinimalDenom,
decimals: destinationAsset.coinDecimals,
denom: destinationAsset.coinDenom,
}
: undefined,
sourceAddress,
sourceChain,
sourceAsset,
direction,
onRequestClose: onClose,
inputAmount: cryptoAmount,
bridges: sourceAsset?.supportedProviders,
onTransfer: () => {
setCryptoAmount("0");
setFiatAmount("0");
},
});

if (
isLoadingCanonicalAssetPrice ||
isNil(supportedAssets) ||
Expand Down
Loading

0 comments on commit 0ac0ab1

Please sign in to comment.