Skip to content

Commit

Permalink
feat-277 Network validation
Browse files Browse the repository at this point in the history
feat: Check connection network
  • Loading branch information
adrianvrj authored Nov 29, 2024
2 parents 94e1915 + f6e1c3f commit 4e55586
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 24 deletions.
36 changes: 24 additions & 12 deletions frontend/gostarkme-web/components/modules/Fund/FundDonate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import ProgressBar from "@/components/ui/ProgressBar";
import { provider } from "@/constants";
import { strkAbi } from "@/contracts/abis/strk";
import { addrSTRK } from "@/contracts/addresses";
import { walletStarknetkitLatestAtom } from "@/state/connectedWallet";
import {
networkAtom,
networkEnvironmentAtom,
walletStarknetkitLatestAtom,
} from "@/state/connectedWallet";
import { useAtomValue } from "jotai";
import Image, { StaticImageData } from "next/image";
import { useState } from "react";
Expand All @@ -24,6 +28,8 @@ const FundDonate = ({ currentBalance, goal, addr, icon }: FundDonateProps) => {
const [isLoading, setIsLoading] = useState<boolean>(false);
const [localBalance, setLocalBalance] = useState<number>(currentBalance);
const wallet = useAtomValue(walletStarknetkitLatestAtom);
const network = useAtomValue(networkAtom);
const networkEnvironment = useAtomValue(networkEnvironmentAtom);
const progress = calculatePorcentage(localBalance, goal);

const handleAmountChange = (e: React.ChangeEvent<HTMLInputElement>) => {
Expand Down Expand Up @@ -126,17 +132,23 @@ const FundDonate = ({ currentBalance, goal, addr, icon }: FundDonateProps) => {
{error}
</p>
)}
<button
onClick={handleDonateClick}
disabled={isLoading}
className={`self-center py-2 px-6 md:py-3 md:px-10 rounded-md text-xs md:text-sm shadow-xl ease-in-out duration-500 active:duration-0 shadow-gray-400
${isLoading
? 'bg-gray-400 cursor-not-allowed'
: 'bg-darkblue text-white hover:bg-starkorange active:bg-darkblue'
}`}
>
{isLoading ? "Processing..." : "Donate"}
</button>
<div className="text-center">
<button
disabled={!network}
onClick={handleDonateClick}
className={`self-center bg-darkblue text-white py-2 px-6 md:py-3 md:px-10 rounded-md
text-xs md:text-sm shadow-xl hover:bg-starkorange active:bg-darkblue ease-in-out
duration-500 active:duration-0 shadow-gray-400 ${!network ? "opacity-50 cursor-not-allowed" : ""}`}
>
Donate
</button>
{wallet && !network && (
<p className="text-sm text-gray-500 mt-2">
Your wallet is currently connected to the wrong network. Please
switch to {networkEnvironment[1]} to continue.
</p>
)}
</div>
</div>
);
};
Expand Down
31 changes: 24 additions & 7 deletions frontend/gostarkme-web/components/modules/Fund/FundVote.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import { calculatePorcentage } from "@/app/utils";
import { Button } from "@/components/ui/Button";
import ProgressBar from "@/components/ui/ProgressBar";
import { fundAbi } from "@/contracts/abis/fund";
import { walletStarknetkitLatestAtom } from "@/state/connectedWallet";
import {
networkAtom,
networkEnvironmentAtom,
walletStarknetkitLatestAtom,
} from "@/state/connectedWallet";
import { latestTxAtom } from "@/state/latestTx";
import { useAtomValue, useSetAtom } from "jotai";
import { Contract, InvokeFunctionResponse } from "starknet";
Expand All @@ -20,6 +24,8 @@ interface FundVoteProps {
export const FundVote = ({ upVotes, upVotesNeeded, addr, setLoading}: FundVoteProps) => {

const wallet = useAtomValue(walletStarknetkitLatestAtom);
const network = useAtomValue(networkAtom);
const networkEnvironment = useAtomValue(networkEnvironmentAtom);

const progress = calculatePorcentage(upVotes, upVotesNeeded);

Expand Down Expand Up @@ -110,12 +116,23 @@ export const FundVote = ({ upVotes, upVotesNeeded, addr, setLoading}: FundVotePr
<p className="text-sm text-gray-500 mt-2">You have already voted</p>
</div>
) : (
<Button
label={isVoting ? "Voting..." : "Vote"}
onClick={handleVote}
disabled={isVoting}
className={isVoting ? "opacity-50 cursor-not-allowed" : ""}
/> // If the wallet is connected, and voteStatus is false render a button that allows voting
<div className="text-center">
<Button
label={isVoting ? "Voting..." : "Vote"}
onClick={handleVote}
disabled={isVoting || !network}
className={
isVoting || !network ? "opacity-50 cursor-not-allowed" : ""
}
/>
{/* // If the wallet is connected, and voteStatus is false render a button that allows voting */}
{!network && (
<p className="text-sm text-gray-500 mt-2">
Your wallet is currently connected to the wrong network. Please
switch to {networkEnvironment[1]} to continue.
</p>
)}
</div>
)
) : ( // If the wallet is not connected, render a disabled vote button with instructions
<div className="text-center">
Expand Down
7 changes: 5 additions & 2 deletions frontend/gostarkme-web/components/ui/ConnectWalletButton.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
"use client";
import { ARGENT_WEBWALLET_URL, CHAIN_ID, provider } from "@/constants";
import { walletStarknetkitLatestAtom } from "@/state/connectedWallet";
import { useAtom } from "jotai";
import { networkAtom, walletStarknetkitLatestAtom } from "@/state/connectedWallet";
import { useAtom, useSetAtom } from "jotai";
import { connect, disconnect } from "starknetkit";

export default function WalletConnector() {
const [wallet, setWallet] = useAtom(walletStarknetkitLatestAtom)
const setNetworkAtom = useSetAtom(networkAtom)

const handleConnect = async (event:any) => {
try {
Expand All @@ -21,6 +22,8 @@ export default function WalletConnector() {
},
})

wallet && setNetworkAtom(wallet?.chainId === process.env.NEXT_PUBLIC_NETWORK)

setWallet(wallet)
} catch (e) {
console.error(e)
Expand Down
10 changes: 7 additions & 3 deletions frontend/gostarkme-web/state/connectedWallet.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { atomWithReset } from "jotai/utils"
import { StarknetWindowObject } from "starknetkit"
import { atom } from "jotai";
import { atomWithReset } from "jotai/utils";
import { StarknetWindowObject } from "starknetkit";

export const walletStarknetkitLatestAtom = atomWithReset<
StarknetWindowObject | null | undefined
>(undefined)
>(undefined);

export const networkAtom = atom(false);
export const networkEnvironmentAtom = atom(["Mainnet", "Sepolia", "Devnet"]);

0 comments on commit 4e55586

Please sign in to comment.