Skip to content

Commit

Permalink
fixes comments
Browse files Browse the repository at this point in the history
  • Loading branch information
matthew-garrett committed Feb 13, 2025
1 parent ff0e7f0 commit 3d336c7
Show file tree
Hide file tree
Showing 22 changed files with 127 additions and 282 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
import { useEffect, useState } from "react";
import { CloseIcon } from "@repo/ui/icons";
import { ActionButton } from "@repo/ui/components";
import { TXN_STATUS } from "../../constants";
import { TXN_STATUS } from "@repo/flame-types";

interface ConfirmationModalProps {
onSubmitCallback: () => void;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ import { GearIcon } from "@repo/ui/icons";
import { InfoTooltip } from "@repo/ui/components";
import { useState } from "react";
import { getFromLocalStorage, setInLocalStorage } from "utils/utils";
import { defaultSlippageTolerance } from "../../constants";
import { useConfig } from "config";

export const SettingsPopover = () => {
const { SwapDefaultSlippageTolerance } = useConfig();
const currentSettings = getFromLocalStorage("settings") || {};
const [customSlippage, setCustomSlippage] = useState<string>(
currentSettings?.slippageTolerance?.toString() ||
defaultSlippageTolerance.toString(),
SwapDefaultSlippageTolerance.toString(),
);
const [expertMode, setExpertMode] = useState(
currentSettings?.expertMode || false,
Expand Down
39 changes: 39 additions & 0 deletions apps/flame-defi/app/config/contexts/ConfigContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,42 @@ export const ConfigContextProvider: React.FC<ConfigContextProps> = ({
const poolURL = getEnvVariable("NEXT_PUBLIC_POOL_URL");
const earnAPIURL = getEnvVariable("NEXT_PUBLIC_EARN_API_URL");

const TokenDefaultApprovalAmount =
"115792089237316195423570985008687907853269984665640564039457";

const SwapDefaultSlippageTolerance = 0.1;

const feeData = [
{
id: 0,
feePercent: "0.3%",
text: "Best for most pairs.",
tvl: "100M",
selectPercent: "0.3%",
},
{
id: 1,
feePercent: "0.5%",
text: "Best for stable pairs.",
tvl: "100M",
selectPercent: "0.5%",
},
{
id: 2,
feePercent: "1%",
text: "Best for high-volatility pairs.",
tvl: "100M",
selectPercent: "1%",
},
{
id: 3,
feePercent: "1%",
text: "Best for high-volatility pairs.",
tvl: "100M",
selectPercent: "1%",
},
];

let feedbackFormURL: string | null;
try {
feedbackFormURL = getEnvVariable("NEXT_PUBLIC_FEEDBACK_FORM_URL");
Expand Down Expand Up @@ -68,6 +104,9 @@ export const ConfigContextProvider: React.FC<ConfigContextProps> = ({
earnAPIURL,
feedbackFormURL,
networksList,
TokenDefaultApprovalAmount,
SwapDefaultSlippageTolerance,
feeData,
}}
>
{children}
Expand Down
19 changes: 1 addition & 18 deletions apps/flame-defi/app/config/hooks/useConfig.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
"use client";

import { useContext } from "react";

import { ConfigContext } from "../contexts/ConfigContext";
import { EvmChainInfo } from "@repo/flame-types";
import { Chain } from "viem";

/**
* Hook to use the config context.
Expand All @@ -22,20 +20,5 @@ export const useEvmChainData = () => {
const evmChainsData = Object.values(evmChains);
const selectedChain = evmChainsData[0] as EvmChainInfo;

const evmChainConfig: Chain = {
id: selectedChain?.chainId,
name: selectedChain?.chainName,
nativeCurrency: {
name: selectedChain?.currencies[0]?.title,
symbol: selectedChain?.currencies[0]?.coinDenom,
decimals: selectedChain?.currencies[0]?.coinDecimals || 18,
},
rpcUrls: {
default: {
http: selectedChain?.rpcUrls,
},
},
};

return { selectedChain, evmChainConfig };
return { selectedChain };
};
12 changes: 12 additions & 0 deletions apps/flame-defi/app/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@ export interface AppConfig {
feedbackFormURL: string | null;
// List of networks to display in the network selector.
networksList: FlameNetwork[];
// The default approval amount for tokens.
TokenDefaultApprovalAmount: string;
// The default slippage tolerance for swaps.
SwapDefaultSlippageTolerance: number;
// The data for the fee options.
feeData: {
id: number;
feePercent: string;
text: string;
tvl: string;
selectPercent: string;
}[];
}

export {
Expand Down
54 changes: 0 additions & 54 deletions apps/flame-defi/app/constants.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
AccordionItem,
AccordionTrigger,
} from "@repo/ui/shadcn-primitives";
import { flameExplorerUrl } from "../../../../constants";
import { useEvmChainData } from "config";

interface ConnectEvmWalletButtonProps {
// Label to show before the user is connected to a wallet.
Expand All @@ -25,6 +25,7 @@ interface ConnectEvmWalletButtonProps {
export default function ConnectEvmWalletButton({
labelBeforeConnected,
}: ConnectEvmWalletButtonProps) {
const { selectedChain } = useEvmChainData();
const {
connectEvmWallet,
disconnectEvmWallet,
Expand Down Expand Up @@ -63,7 +64,7 @@ export default function ConnectEvmWalletButton({
<div className="flex items-center gap-3">
<CopyToClipboardButton textToCopy={userAccount.address} />
<a
href={`${flameExplorerUrl}/address/${userAccount.address}`}
href={`${selectedChain.blockExplorerUrl}/address/${userAccount.address}`}
target="_blank"
rel="noreferrer"
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import {
PopoverContent,
PopoverTrigger,
} from "@repo/ui/shadcn-primitives";
import { flameExplorerUrl } from "../../../../constants";

import { useEvmChainData } from "config";
export function SingleWalletContent({
address,
handleClose,
Expand All @@ -19,6 +18,7 @@ export function SingleWalletContent({
handleClose?: () => void;
}) {
// const [showTransactions, setShowTransactions] = useState(false);
const { selectedChain } = useEvmChainData();
const {
disconnectEvmWallet,
evmNativeTokenBalance,
Expand All @@ -45,7 +45,7 @@ export function SingleWalletContent({
<div className="flex items-center gap-3 text-grey-light">
<CopyToClipboardButton textToCopy={address} />
<a
href={`${flameExplorerUrl}/address/${address}`}
href={`${selectedChain.blockExplorerUrl}/address/${address}`}
target="_blank"
rel="noreferrer"
>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useConnectModal } from "@rainbow-me/rainbowkit";
import React, { useCallback, useEffect, useMemo, useState } from "react";
import { erc20Abi, formatUnits } from "viem";
import { Chain, erc20Abi, formatUnits } from "viem";
import {
useAccount,
useBalance,
Expand All @@ -26,11 +26,11 @@ import {
import { formatBalance } from "../../../utils/utils";
import {
EvmChainInfo,
evmChainToRainbowKitChain,
EvmCurrency,
evmCurrencyBelongsToChain,
} from "@repo/flame-types";
import JSBI from "jsbi";
import { defaultApprovalAmount } from "../../../constants";

export interface EvmWalletContextProps {
connectEvmWallet: () => void;
Expand Down Expand Up @@ -69,15 +69,19 @@ interface EvmWalletProviderProps {
export const EvmWalletProvider: React.FC<EvmWalletProviderProps> = ({
children,
}) => {
const { evmChains, selectedFlameNetwork, selectFlameNetwork } =
useAppConfig();
const {
evmChains,
selectedFlameNetwork,
selectFlameNetwork,
TokenDefaultApprovalAmount,
} = useAppConfig();

const { openConnectModal } = useConnectModal();
const { disconnect } = useDisconnect();
const wagmiConfig = useConfig();
const userAccount = useAccount();
const { switchChain } = useSwitchChain();
const { selectedChain, evmChainConfig } = useEvmChainData();
const { selectedChain } = useEvmChainData();
const publicClient = getPublicClient(wagmiConfig, {
chainId: selectedChain?.chainId,
});
Expand Down Expand Up @@ -315,7 +319,7 @@ export const EvmWalletProvider: React.FC<EvmWalletProviderProps> = ({
) {
return null;
}
const amountAsBigInt = BigInt(defaultApprovalAmount);
const amountAsBigInt = BigInt(TokenDefaultApprovalAmount);
// NOTE: Reset this to 0 whenever we want to reset the approval
// const amountAsBigInt = BigInt('0');

Expand All @@ -325,15 +329,15 @@ export const EvmWalletProvider: React.FC<EvmWalletProviderProps> = ({
abi: erc20Abi,
functionName: "approve",
args: [contracts.swapRouter.address, amountAsBigInt],
chain: evmChainConfig,
chain: evmChainToRainbowKitChain(selectedChain) as Chain,
account: userAccount?.address as `0x${string}`,
});

const newTokenAllowance = tokenAllowances.map((data) => {
if (data.symbol === token?.coinDenom) {
return {
symbol: token?.coinDenom,
allowance: JSBI.BigInt(defaultApprovalAmount),
allowance: JSBI.BigInt(TokenDefaultApprovalAmount),
};
}
return data;
Expand All @@ -350,8 +354,9 @@ export const EvmWalletProvider: React.FC<EvmWalletProviderProps> = ({
userAccount.address,
walletClient,
contracts?.swapRouter?.address,
evmChainConfig,
tokenAllowances,
TokenDefaultApprovalAmount,
selectedChain,
],
);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import JSBI from "jsbi";
import { Chain, encodeFunctionData, PublicClient, WalletClient } from "viem";

import { GetQuoteResult } from "@repo/flame-types";
import ERC_20_ABI from "./contracts/erc20.json";
import SWAP_ROUTER_ABI from "./contracts/swaprouter02.json";
import {
ExactInputParams,
Expand Down Expand Up @@ -32,24 +30,12 @@ export class SwapRouter {
outputs: Array<{ name: string; type: string }>;
stateMutability: string;
}>;
private readonly erc20Abi: Array<{
name: string;
type: string;
inputs: Array<{
name: string;
type: string;
components?: Array<{ name: string; type: string }>;
}>;
outputs: Array<{ name: string; type: string }>;
stateMutability: string;
}>;

constructor(routerAddress: string, chainConfig: Chain) {
this.routerAddress = routerAddress as `0x${string}`;
this.chainConfig = chainConfig;
// Minimal ABI for the swap functions.
this.routerAbi = SWAP_ROUTER_ABI;
this.erc20Abi = ERC_20_ABI;
}

private calculateMinimumOut(
Expand Down
Loading

0 comments on commit 3d336c7

Please sign in to comment.