Skip to content

Commit

Permalink
Merge pull request #253 from skip-mev/staging
Browse files Browse the repository at this point in the history
sync staging to main
  • Loading branch information
codingki authored Apr 16, 2024
2 parents 1342cf4 + e1a87a6 commit 73050ea
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 74 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/deploy-testnet-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
name: Checkout
uses: actions/checkout@v3
with:
ref: testnet
ref: testnet-dev

- id: pull-staging
name: Pull latest 'staging'
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/deploy-testnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:

- id: pull-staging
name: Pull latest 'staging'
run: git pull origin staging
run: git pull --ff-only origin staging

- id: diff-check
name: Check if 'testnet' is behind 'staging'
Expand Down
2 changes: 1 addition & 1 deletion chain-registry
Submodule chain-registry updated 155 files
6 changes: 4 additions & 2 deletions src/components/AssetInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,10 @@ function AssetInput({
}}
/>
<div className="flex h-8 items-center space-x-2 tabular-nums">
<p className="text-sm tabular-nums text-neutral-400">{amountUSD ? formatUSD(amountUSD) : null}</p>
{amountUSD !== undefined && diffPercentage !== 0 && context === "destination" ? (
<p className="text-sm tabular-nums text-neutral-400">
{amountUSD && Number(amountUSD) > 0 ? formatUSD(amountUSD) : null}
</p>
{amountUSD !== undefined && Number(amountUSD) > 0 && diffPercentage !== 0 && context === "destination" ? (
<p className={cn("text-sm tabular-nums", diffPercentage >= 0 ? "text-green-500" : "text-red-500")}>
({formatPercent(diffPercentage)})
</p>
Expand Down
6 changes: 1 addition & 5 deletions src/components/SwapWidget/SwapWidget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,9 @@ export function SwapWidget() {
sourceFeeAmount,
sourceFeeAsset,
swapPriceImpactPercent,
usdDiffPercent,
} = useSwapWidget();

let usdDiffPercent = 0.0;
if (route?.usdAmountIn && route?.usdAmountOut) {
usdDiffPercent = (parseFloat(route.usdAmountOut) - parseFloat(route.usdAmountIn)) / parseFloat(route.usdAmountIn);
}

const srcAccount = useAccount("source");
const destAccount = useAccount("destination");

Expand Down
63 changes: 35 additions & 28 deletions src/components/SwapWidget/useSwapWidget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import { useBalancesByChain } from "@/hooks/useBalancesByChain";
import { Chain, useChains } from "@/hooks/useChains";
import { useRoute, useSkipClient } from "@/solve";
import { getChainFeeAssets, getChainGasPrice } from "@/utils/chain.client";
import { formatPercent, formatUSD } from "@/utils/intl";
import { getAmountWei, parseAmountWei } from "@/utils/number";
import { gracefullyConnect } from "@/utils/wallet";

Expand Down Expand Up @@ -183,8 +182,12 @@ export function useSwapWidget() {
if (!route) {
return undefined;
}

if (!route.usdAmountIn || !route.usdAmountOut) {
if (
!route.usdAmountIn ||
!route.usdAmountOut ||
Number(route.usdAmountIn) === 0 ||
Number(route.usdAmountOut) === 0
) {
return undefined;
}

Expand All @@ -195,36 +198,20 @@ export function useSwapWidget() {
}, [route]);

const [routeWarningTitle, routeWarningMessage] = useMemo(() => {
if (!route) {
if (!route?.warning) {
return [undefined, undefined];
}

if (!route.swapPriceImpactPercent && (!route.usdAmountIn || !route.usdAmountOut)) {
return ["Low Information Trade", "We were unable to calculate the price impact of this route."];
}

if (usdDiffPercent && Math.abs(usdDiffPercent) > PRICE_IMPACT_THRESHOLD) {
const amountInUSD = formatUSD(parseFloat(route.usdAmountIn ?? "0"));

const amountOutUSD = formatUSD(parseFloat(route.usdAmountOut ?? "0"));

const formattedUsdDiffPercent = formatPercent(Math.abs(usdDiffPercent));
return [
"Bad Trade Warning",
`Your estimated output value (${amountOutUSD}) is ${formattedUsdDiffPercent} lower than your estimated input value (${amountInUSD}).`,
];
if (Number(route.usdAmountIn) === 0 || Number(route.usdAmountOut) === 0) {
return [undefined, undefined];
}

if (swapPriceImpactPercent && swapPriceImpactPercent > PRICE_IMPACT_THRESHOLD) {
const formattedPriceImpact = formatPercent(swapPriceImpactPercent);
return [
"Bad Trade Warning",
`Your swap is expected to execute at a ${formattedPriceImpact} worse price than the current estimated on-chain price. It's likely there's not much liquidity available for this swap.`,
];
if (route.warning.type === "BAD_PRICE_WARNING") {
return ["Bad Price Warning", route.warning.message];
}

return [undefined, undefined];
}, [route, swapPriceImpactPercent, usdDiffPercent]);
}, [route]);

// #endregion

Expand Down Expand Up @@ -252,7 +239,12 @@ export function useSwapWidget() {
if (!asset) {
const assets = assetsByChainID(chain.chainID);
if (chain.chainType === "evm") {
asset = assets.find((asset) => asset.denom.endsWith("-native"));
asset = assets.find(
(asset) =>
asset.denom.endsWith("-native") ||
asset.name?.toLowerCase() === chain.chainName.toLowerCase() ||
asset.symbol?.toLowerCase().includes("usdc"),
);
}
asset ??= assets[0];
}
Expand Down Expand Up @@ -297,9 +289,23 @@ export function useSwapWidget() {
const { destinationAsset: currentDstAsset } = useSwapWidgetStore.getState();
const assets = assetsByChainID(chain.chainID);

let asset = await getFeeAsset(chain.chainID);
let feeAsset: Asset | undefined = undefined;
if (chain.chainType === "cosmos") {
feeAsset = await getFeeAsset(chain.chainID);
}

let asset = feeAsset;
if (!asset) {
[asset] = assets;
const assets = assetsByChainID(chain.chainID);
if (chain.chainType === "evm") {
asset = assets.find(
(asset) =>
asset.denom.endsWith("-native") ||
asset.name?.toLowerCase() === chain.chainName.toLowerCase() ||
asset.symbol?.toLowerCase().includes("usdc"),
);
}
asset ??= assets[0];
}
if (currentDstAsset && userTouchedDstAsset) {
const equivalentAsset = findEquivalentAsset(currentDstAsset, assets);
Expand Down Expand Up @@ -831,6 +837,7 @@ export function useSwapWidget() {
sourceFeeAmount: gasRequired,
sourceFeeAsset: srcFeeAsset,
swapPriceImpactPercent,
usdDiffPercent,
};
}

Expand Down
2 changes: 1 addition & 1 deletion src/constants/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export const API_URL = process.env.NEXT_PUBLIC_API_URL || "https://api.skip.mone
export const APP_URL = process.env.APP_URL;

export const appUrl =
process.env.NEXT_PUBLIC_VERCEL_ENV === "preview"
process.env.NEXT_PUBLIC_VERCEL_ENV === "preview" || process.env.NEXT_PUBLIC_VERCEL_ENV === "staging"
? typeof window !== "undefined"
? `https://${window.location.hostname}`
: process.env.NEXT_PUBLIC_VERCEL_URL
Expand Down
80 changes: 46 additions & 34 deletions src/solve/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export function useRoute({
const skipClient = useSkipClient();

const [refetchCount, setRefetchCount] = useState(0);

const [isError, setIsError] = useState(false);
const { data: experimentalFeatures } = useExperimentalFeatures();

const queryKey = useMemo(
Expand Down Expand Up @@ -119,43 +119,54 @@ export function useRoute({
if (!sourceAsset || !sourceAssetChainID || !destinationAsset || !destinationAssetChainID) {
return;
}
try {
const route = await skipClient.route(
direction === "swap-in"
? {
amountIn: amount,
sourceAssetDenom: sourceAsset,
sourceAssetChainID: sourceAssetChainID,
destAssetDenom: destinationAsset,
destAssetChainID: destinationAssetChainID,
swapVenue,
allowMultiTx: true,
allowUnsafe: true,
experimentalFeatures,
smartRelay: true,
}
: {
amountOut: amount,
sourceAssetDenom: sourceAsset,
sourceAssetChainID: sourceAssetChainID,
destAssetDenom: destinationAsset,
destAssetChainID: destinationAssetChainID,
swapVenue,
allowMultiTx: true,
allowUnsafe: true,
experimentalFeatures,
smartRelay: true,
},
);

const route = await skipClient.route(
direction === "swap-in"
? {
amountIn: amount,
sourceAssetDenom: sourceAsset,
sourceAssetChainID: sourceAssetChainID,
destAssetDenom: destinationAsset,
destAssetChainID: destinationAssetChainID,
swapVenue,
allowMultiTx: true,
allowUnsafe: true,
experimentalFeatures,
smartRelay: true,
}
: {
amountOut: amount,
sourceAssetDenom: sourceAsset,
sourceAssetChainID: sourceAssetChainID,
destAssetDenom: destinationAsset,
destAssetChainID: destinationAssetChainID,
swapVenue,
allowMultiTx: true,
allowUnsafe: true,
experimentalFeatures,
smartRelay: true,
},
);
if (!route.operations) {
throw new Error("no routes found");
}

if (!route.operations) {
throw new Error("No route found");
return route;
} catch (error) {
if (
// @ts-expect-error - error
String(error?.message).toLowerCase().includes("no routes found") ||
// @ts-expect-error - error
String(error?.message).toLowerCase().includes("relay")
) {
setIsError(true);
}
throw error;
}

return route;
},
refetchInterval: refetchCount < 10 ? 1000 * 10 : false,
retry: 1,
refetchInterval: isError ? false : refetchCount < 10 ? 1000 * 10 : false,
retry: false,
enabled:
enabled &&
!!sourceAsset &&
Expand All @@ -172,6 +183,7 @@ export function useRoute({
}, [query.isRefetching]);

useEffect(() => {
setIsError(false);
setRefetchCount(0);
}, [queryKey]);

Expand Down
2 changes: 1 addition & 1 deletion vercel.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ declare namespace NodeJS {
readonly VERCEL_GIT_PREVIOUS_SHA?: string;
readonly VERCEL_GIT_PULL_REQUEST_ID?: string;

readonly NEXT_PUBLIC_VERCEL_ENV?: "production" | "preview" | "development";
readonly NEXT_PUBLIC_VERCEL_ENV?: "production" | "preview" | "development" | "staging";
readonly NEXT_PUBLIC_VERCEL_URL?: string;
readonly NEXT_PUBLIC_VERCEL_BRANCH_URL?: string;
readonly NEXT_PUBLIC_VERCEL_AUTOMATION_BYPASS_SECRET?: string;
Expand Down

0 comments on commit 73050ea

Please sign in to comment.