Skip to content

Commit

Permalink
replaced tokens with fortis coins
Browse files Browse the repository at this point in the history
  • Loading branch information
Tikhon committed Dec 3, 2023
1 parent ddea8c8 commit eee3448
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 758 deletions.
2 changes: 1 addition & 1 deletion demo/app/src/components/trade-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const TradeButton = ({ pair }: { pair: Pair }) => {
{isConnected ? (
<DialogTrigger asChild>
<Button
disabled={!prices[pair]}
// disabled={!prices[pair]}
className="w-[156px] bg-[#375BD2] py-3 text-base font-black leading-4 hover:bg-[#375BD2]/90"
>
Trade
Expand Down
71 changes: 17 additions & 54 deletions demo/app/src/components/trade-dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,39 +20,30 @@ import { Input } from "@/components/ui/input";
import { Form, FormField, FormItem, FormLabel } from "@/components//ui/form";
import { symbols } from "@/config/trade";
import { useDatafeed } from "@/app/datafeed-provider";
import { Pair, chainlinkPairToFeedId } from "@/_types";
import { Pair } from "@/_types";
import { useState } from "react";
import {
wethConfig,
swapAppConfig,
usdcConfig,
avaxConfig, oracleConfig,
usdcConfig, oracleConfig,
} from "@/config/contracts";
import { Check } from "lucide-react";

const formSchema = z.object({
from: z.coerce.number().gt(0),
to: z.coerce.number().gt(0),
to: z.coerce.number(),
});

const TradeDialog = ({ pair }: { pair: Pair }) => {
const [isLoading, setIsLoading] = useState(false);
const [txHash, setTxHash] = useState<Address | undefined>();
const { address } = useAccount();
const { prices } = useDatafeed();
const [tokenA, setTokenA] = useState<Address | undefined>(
pair === Pair.AVAX_USD ? avaxConfig.address : wethConfig.address,
);
const [tokenB, setTokenB] = useState<Address | undefined>(usdcConfig.address);
const [tokenA] = useState<Address | undefined>(wethConfig.address);
const [tokenB] = useState<Address | undefined>(usdcConfig.address);
const { data: tokenABalance } = useBalance({ address, token: tokenA });
const { data: tokenBBalance } = useBalance({ address, token: tokenB });

const [feedId, setFeedId] = useState(
pair === Pair.AVAX_USD
? chainlinkPairToFeedId[Pair.AVAX_USD]
: chainlinkPairToFeedId[Pair.ETH_USD],
);

const form = useForm<z.infer<typeof formSchema>>({
resolver: zodResolver(formSchema),
defaultValues: {
Expand Down Expand Up @@ -87,23 +78,6 @@ const TradeDialog = ({ pair }: { pair: Pair }) => {
},
});

const { writeAsync: approveAvax } = useContractWrite({
...avaxConfig,
functionName: "approve",
onError(error) {
toast({
variant: "destructive",
title: error.name,
description: error.message,
});
},
onSuccess() {
toast({
title: "Approve transaction has been sent",
});
},
});

const { writeAsync: approveUsdc } = useContractWrite({
...usdcConfig,
functionName: "approve",
Expand Down Expand Up @@ -139,14 +113,9 @@ const TradeDialog = ({ pair }: { pair: Pair }) => {
});

async function onSubmit(values: z.infer<typeof formSchema>) {
console.log('submitting');
setIsLoading(true);
setFeedId(
tokenA === wethConfig.address
? chainlinkPairToFeedId[Pair.ETH_USD]
: chainlinkPairToFeedId[Pair.AVAX_USD],
);
const amountA = parseUnits(`${values.from}`, tokenABalance?.decimals ?? 0);
const amountB = parseUnits(`${values.to}`, tokenBBalance?.decimals ?? 0);

if (amountA > (tokenABalance?.value ?? BigInt(0))) {
toast({
Expand All @@ -168,33 +137,27 @@ const TradeDialog = ({ pair }: { pair: Pair }) => {
});
}

if (tokenA == avaxConfig.address) {
await approveAvax({
args: [swapAppConfig.address, parseEther(`${fromAmount}`)],
});
}

if (tokenA == usdcConfig.address) {
await approveUsdc({
args: [swapAppConfig.address, parseEther(`${fromAmount}`)],
});
}

const nonce = BigInt(new Date().getTime());
const tradeArgs = [tokenA!, tokenB!, amountA, feedId] as const;
const tradeArgs = {recipient: address!,tokenIn: tokenA!,tokenOut: tokenB!,amountIn: amountA} as const;

const result = await trade({
args: [...tradeArgs, nonce],
});
const { refetch} = useContractRead({
...oracleConfig,
functionName: "previewFallbackCall",
args: [swapAppConfig.address, tradeArgs, nonce, address],
args: [tradeArgs, nonce],
});
setInterval(async() => {
const result = await refetch();
console.log(result);
}, 1000);
// const { refetch} = useContractRead({
// ...oracleConfig,
// functionName: "previewFallbackCall",
// args: [swapAppConfig.address, tradeArgs, nonce, address] as unknown,
// });
// setInterval(async() => {
// const result = await refetch();
// console.log(result);
// }, 1000);
toast({
title: "Swap completed:",
description: `${values.from} ${tokenABalance?.symbol} for ${values.to} ${tokenBBalance?.symbol}`,
Expand Down
Loading

0 comments on commit eee3448

Please sign in to comment.