Skip to content

Commit

Permalink
Merge pull request #12 from skip-mev/staging
Browse files Browse the repository at this point in the history
Deploy max fee button to prod
  • Loading branch information
thal0x authored Jul 25, 2023
2 parents 4584407 + 1ca9190 commit d2409f7
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 9 deletions.
26 changes: 21 additions & 5 deletions src/components/AssetInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Toast from "@/elements/Toast";
import { useChain } from "@cosmos-kit/react";
import { ethers } from "ethers";
import { useAssets } from "@/context/assets";
import { getFee } from "@/utils/utils";

interface Props {
amount: string;
Expand All @@ -31,7 +32,7 @@ const AssetInput: FC<Props> = ({
}) => {
const [isError, setIsError] = useState(false);

const { assetsByChainID, getNativeAssets } = useAssets();
const { assetsByChainID, getNativeAssets, getFeeDenom } = useAssets();

const assets = useMemo(() => {
if (!chain) {
Expand All @@ -41,8 +42,6 @@ const AssetInput: FC<Props> = ({
return assetsByChainID(chain.chain_id);
}, [assetsByChainID, chain, getNativeAssets]);

// console.log(assets);

const showChainInfo = chain ? false : true;

const { address } = useChain(chain?.record?.name ?? "cosmoshub");
Expand Down Expand Up @@ -134,11 +133,28 @@ const AssetInput: FC<Props> = ({
className="font-extrabold text-xs bg-neutral-400 text-white px-3 py-1 rounded-md transition-transform enabled:hover:scale-110 enabled:hover:rotate-2 disabled:cursor-not-allowed"
disabled={maxButtonDisabled}
onClick={() => {
if (!selectedAssetBalance) {
if (!selectedAssetBalance || !chain || !asset) {
return;
}

onAmountChange?.(selectedAssetBalance);
const feeDenom = getFeeDenom(chain.chain_id);

let amount = selectedAssetBalance;

// if selected asset is the fee denom, subtract the fee
if (feeDenom && feeDenom.denom === asset.denom) {
const fee = getFee(chain.chain_id);

const feeInt = parseFloat(
ethers.formatUnits(fee, asset.decimals)
).toFixed(asset.decimals);

amount = (
parseFloat(selectedAssetBalance) - parseFloat(feeInt)
).toFixed(asset.decimals);
}

onAmountChange?.(amount);
}}
>
MAX
Expand Down
2 changes: 1 addition & 1 deletion src/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Fragment } from "react";
import va from "@vercel/analytics";
import { useChains } from "@/context/chains";
import AssetInput from "@/components/AssetInput";
import { ArrowsUpDownIcon } from "@heroicons/react/20/solid";
Expand All @@ -9,7 +10,6 @@ import TransactionDialog from "@/components/TransactionDialog";
import { queryClient } from "@/utils/query";
import { getBalancesByChain } from "@/cosmos";
import { useInterval } from "@/utils/hooks";
import va from "@vercel/analytics";

export default function Home() {
const { chains } = useChains();
Expand Down
2 changes: 1 addition & 1 deletion src/solve/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ interface GetChainsResponse {
chains: Chain[];
}

const IGNORE_CHAINS = ["agoric", "8ball"];
const IGNORE_CHAINS = ["agoric", "8ball", "akashnet-2"];

export async function getChains() {
const response = await axios.get<GetChainsResponse>(`${API_URL}/info/chains`);
Expand Down
3 changes: 1 addition & 2 deletions src/solve/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -516,11 +516,10 @@ export async function executeRoute(
),
}
);

const tx = await client.signAndBroadcast(msgJSON.sender, [msg], "auto");
}


while (true) {
console.log("polling...");

Expand Down
19 changes: 19 additions & 0 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,3 +376,22 @@ export async function getOfflineSignerOnlyAmino(

throw new Error("unsupported wallet");
}

export function getFee(chainID: string) {
const chain = getChainByID(chainID);

const feeInfo = chain.fees?.fee_tokens[0];

if (!feeInfo) {
throw new Error("No fee info found");
}

let averageGasPrice = 0;
if (feeInfo.average_gas_price) {
averageGasPrice = feeInfo.average_gas_price;
}

const amountNeeded = averageGasPrice * 200000;

return amountNeeded;
}

1 comment on commit d2409f7

@vercel
Copy link

@vercel vercel bot commented on d2409f7 Jul 25, 2023

Choose a reason for hiding this comment

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

Please sign in to comment.