Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
armaniferrante committed Jul 1, 2021
1 parent 4db7a62 commit 13d2e62
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 13 deletions.
45 changes: 33 additions & 12 deletions src/components/Swap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ import {
FEE_MULTIPLIER,
} from "../context/Dex";
import { useTokenMap } from "../context/TokenList";
import { useMint, useOwnedTokenAccount } from "../context/Token";
import {
useMint,
useOwnedTokenAccount,
useTokenContext,
} from "../context/Token";
import { useCanSwap, useReferral, useIsWrapSol } from "../context/Swap";
import TokenDialog from "./TokenDialog";
import { SettingsButton } from "./Settings";
Expand Down Expand Up @@ -335,7 +339,8 @@ export function SwapButton() {
isClosingNewAccounts,
isStrict,
} = useSwapContext();
const { swapClient } = useDexContext();
const { swapClient, isLoaded: isDexLoaded } = useDexContext();
const { isLoaded: isTokensLoaded } = useTokenContext();
const fromMintInfo = useMint(fromMint);
const toMintInfo = useMint(toMint);
const openOrders = useOpenOrders();
Expand Down Expand Up @@ -649,16 +654,32 @@ export function SwapButton() {

await swapClient.program.provider.sendAll(txs);
};
return disconnected ? (
<Button
variant="contained"
className={styles.swapButton}
onClick={sendCreateAccountsTransaction}
disabled={true}
>
Disconnected
</Button>
) : needsCreateAccounts ? (

if (disconnected) {
return (
<Button
variant="contained"
className={styles.swapButton}
onClick={sendCreateAccountsTransaction}
disabled={true}
>
Disconnected
</Button>
);
}
if (!isDexLoaded || !isTokensLoaded) {
return (
<Button
variant="contained"
className={styles.swapButton}
onClick={sendSwapTransaction}
disabled={true}
>
Swap
</Button>
);
}
return needsCreateAccounts ? (
<Button
variant="contained"
className={styles.swapButton}
Expand Down
5 changes: 5 additions & 0 deletions src/context/Dex.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,15 @@ type DexContext = {
openOrders: Map<string, Array<OpenOrders>>;
closeOpenOrders: (openOrder: OpenOrders) => void;
swapClient: SwapClient;
isLoaded: boolean;
};
const _DexContext = React.createContext<DexContext | null>(null);

export function DexContextProvider(props: any) {
const [ooAccounts, setOoAccounts] = useState<Map<string, Array<OpenOrders>>>(
new Map()
);
const [isLoaded, setIsLoaded] = useState(false);
const swapClient = props.swapClient;

// Removes the given open orders from the context.
Expand Down Expand Up @@ -154,6 +156,8 @@ export function DexContextProvider(props: any) {
new Promise<Market>((resolve) => resolve(m.account))
);
});

setIsLoaded(true);
});
}, [
swapClient.program.provider.connection,
Expand All @@ -166,6 +170,7 @@ export function DexContextProvider(props: any) {
openOrders: ooAccounts,
closeOpenOrders,
swapClient,
isLoaded,
}}
>
{props.children}
Expand Down
7 changes: 6 additions & 1 deletion src/context/Token.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ import { SOL_MINT } from "../utils/pubkeys";

export type TokenContext = {
provider: Provider;
isLoaded: boolean;
};
const _TokenContext = React.createContext<TokenContext | null>(null);

export function TokenContextProvider(props: any) {
const provider = props.provider;
const [, setRefresh] = useState(0);
const [isLoaded, setIsLoaded] = useState(false);

// Fetch all the owned token accounts for the wallet.
useEffect(() => {
Expand All @@ -35,6 +37,8 @@ export function TokenContextProvider(props: any) {
_OWNED_TOKEN_ACCOUNTS_CACHE.push(...accs);
setRefresh((r) => r + 1);
}
console.log("setting is loaded");
setIsLoaded(true);
}
);
// Fetch SOL balance.
Expand All @@ -59,14 +63,15 @@ export function TokenContextProvider(props: any) {
<_TokenContext.Provider
value={{
provider,
isLoaded,
}}
>
{props.children}
</_TokenContext.Provider>
);
}

function useTokenContext() {
export function useTokenContext() {
const ctx = useContext(_TokenContext);
if (ctx === null) {
throw new Error("Context not available");
Expand Down

0 comments on commit 13d2e62

Please sign in to comment.