Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use React context for SolanaConnection #284

Merged
merged 22 commits into from
Aug 18, 2022
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
979b097
Merge branch 'master' of github.com:swim-io/swim
swimdrew Aug 9, 2022
7cc0102
Merge branch 'master' of github.com:swim-io/swim
swimdrew Aug 10, 2022
9353c22
feat: use React context for SolanaConnection
swimdrew Aug 11, 2022
e6fa53b
fix: less naughty error msg
swimdrew Aug 11, 2022
1e24592
fix: update import
swimdrew Aug 11, 2022
bdc38c5
fix: fix some imports and remove dummy websocket connection
swimdrew Aug 11, 2022
72ae60e
fix: import
swimdrew Aug 11, 2022
a4cc973
Merge branch 'master' of github.com:swim-io/swim into ui/solanaContext
swimdrew Aug 12, 2022
84d8178
style: restore useSolanaConnection hook and original imports
swimdrew Aug 12, 2022
2474962
fix: revert TestPage to master
swimdrew Aug 12, 2022
a4ffa49
Merge branch 'master' of github.com:swim-io/swim into ui/solanaContext
swimdrew Aug 12, 2022
ce198a9
Merge branch 'master' of github.com:swim-io/swim into ui/solanaContext
swimdrew Aug 15, 2022
fd81699
fix: remove outdated file
swimdrew Aug 15, 2022
8e43757
fix: restore dummy WS subscription
swimdrew Aug 15, 2022
4579ea2
style: fix lint for imports
swimdrew Aug 15, 2022
48dcd84
fix: lint import
swimdrew Aug 15, 2022
b49e969
fix: import lint error
swimdrew Aug 15, 2022
43aec07
Merge branch 'master' of github.com:swim-io/swim into ui/solanaContext
swimdrew Aug 17, 2022
c7803cd
style: Update apps/ui/src/hooks/solana/useSolanaConnection.ts
swimdrew Aug 17, 2022
6b9ac35
Merge branch 'ui/solanaContext' of github.com:swim-io/swim into ui/so…
swimdrew Aug 17, 2022
3c2455f
fix: reply to PR comments
swimdrew Aug 17, 2022
1ea6027
Merge branch 'master' into ui/solanaContext
wormat Aug 18, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/ui/src/components/SolanaTpsWarning.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { Env } from "@swim-io/core";
import type { ReactElement } from "react";
import { useCallback, useEffect, useState } from "react";

import { useSolanaConnection } from "../contexts/SolanaConnection";
import { useEnvironment } from "../core/store";
import { useSolanaConnection } from "../hooks/solana";

const INTERVAL_FREQUENCY_MS = 60000; // 1 minute.
const SAMPLES_LIMIT = 5;
Expand Down
2 changes: 1 addition & 1 deletion apps/ui/src/components/SwapForm/SwapForm.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import type { UseQueryResult } from "react-query";
import { MemoryRouter, Route, Routes } from "react-router";

import { EcosystemId } from "../../config";
import { useSolanaConnection } from "../../contexts/SolanaConnection";
import { useEnvironment as environmentStore } from "../../core/store";
import {
useErc20BalanceQuery,
useGetSwapFormErrors,
useLiquidityQueries,
useSolanaConnection,
useSplTokenAccountsQuery,
useSplUserBalance,
useStartNewInteraction,
Expand Down
39 changes: 39 additions & 0 deletions apps/ui/src/contexts/SolanaConnection.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import type { ReactElement, ReactNode } from "react";
import { createContext, useContext, useMemo } from "react";
import shallow from "zustand/shallow.js";

import { Protocol } from "../config/ecosystem";
import { selectConfig } from "../core/selectors";
import { useEnvironment } from "../core/store";
import { SolanaConnection } from "../models";

const SolanaConnectionContext: React.Context<null | SolanaConnection> =
createContext<null | SolanaConnection>(null);

export const SolanaConnectionProvider = ({
children,
}: {
readonly children?: ReactNode;
}): ReactElement => {
const { chains } = useEnvironment(selectConfig, shallow);
const [{ endpoints }] = chains[Protocol.Solana];

const connection = useMemo(
() => new SolanaConnection(endpoints),
[endpoints],
);

return (
<SolanaConnectionContext.Provider value={connection}>
{children}
</SolanaConnectionContext.Provider>
);
};

export const useSolanaConnection = (): SolanaConnection => {
const context = useContext(SolanaConnectionContext);
if (!context) {
throw new Error("Missing Solana connection context");
}
return context;
};
5 changes: 4 additions & 1 deletion apps/ui/src/contexts/appContext.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import type React from "react";

import { SolanaConnectionProvider } from "./SolanaConnection";
import { QueryClientProvider } from "./queryClient";

export const AppContext: React.FC = ({ children }) => (
<QueryClientProvider>{children}</QueryClientProvider>
<SolanaConnectionProvider>
<QueryClientProvider>{children}</QueryClientProvider>
</SolanaConnectionProvider>
);
3 changes: 2 additions & 1 deletion apps/ui/src/hooks/interaction/useAddInteractionMutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useMutation } from "react-query";
import shallow from "zustand/shallow.js";

import { EcosystemId, isEvmEcosystemId } from "../../config";
import { useSolanaConnection } from "../../contexts/SolanaConnection";
import { selectConfig } from "../../core/selectors";
import { useEnvironment, useInteractionStateV2 } from "../../core/store";
import type { AddInteractionState } from "../../models";
Expand All @@ -13,7 +14,7 @@ import {
getTokensByPool,
} from "../../models";
import { useWallets } from "../crossEcosystem";
import { useSolanaConnection, useSplTokenAccountsQuery } from "../solana";
import { useSplTokenAccountsQuery } from "../solana";

export const useAddInteractionMutation = () => {
const { env } = useEnvironment();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
getSolanaTokenDetails,
getTokenDetailsForEcosystem,
} from "../../config";
import { useSolanaConnection } from "../../contexts/SolanaConnection";
import { selectConfig, selectGetInteractionState } from "../../core/selectors";
import { useEnvironment, useInteractionState } from "../../core/store";
import type { InteractionState, SolanaConnection, Tx } from "../../models";
Expand All @@ -31,11 +32,7 @@ import { DEFAULT_WORMHOLE_RETRIES } from "../../models/wormhole/constants";
import { getSignedVaaWithRetry } from "../../models/wormhole/guardiansRpc";
import { useWallets } from "../crossEcosystem";
import { useEvmConnections } from "../evm";
import {
useSolanaConnection,
useSolanaWallet,
useSplTokenAccountsQuery,
} from "../solana";
import { useSolanaWallet, useSplTokenAccountsQuery } from "../solana";

const getTransferredAmountsByTokenId = async (
interactionState: InteractionState,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ import type { AccountInfo as TokenAccount } from "@solana/spl-token";
import { act, renderHook } from "@testing-library/react-hooks";
import { useQueryClient } from "react-query";

import { useSolanaConnection } from "../../contexts/SolanaConnection";
import { selectGetInteractionState } from "../../core/selectors";
import { useInteractionState } from "../../core/store";
import { MOCK_SOL_WALLET } from "../../fixtures";
import { MOCK_INTERACTION_STATE } from "../../fixtures/swim/interactionState";
import type { SolanaWalletInterface } from "../../models";
import { createSplTokenAccount } from "../../models";
import { mockOf, renderHookWithAppContext } from "../../testUtils";
import { useSolanaConnection, useSolanaWallet } from "../solana";
import { useSolanaWallet } from "../solana";

import { usePrepareSplTokenAccountMutation } from "./usePrepareSplTokenAccountMutation";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { useMutation, useQueryClient } from "react-query";

import { useSolanaConnection } from "../../contexts/SolanaConnection";
import { selectGetInteractionState } from "../../core/selectors";
import { useInteractionState } from "../../core/store";
import { createSplTokenAccount, findTokenAccountForMint } from "../../models";
import {
getSplTokenAccountsQueryKey,
useSolanaConnection,
useSolanaWallet,
useSplTokenAccountsQuery,
} from "../solana";
Expand Down Expand Up @@ -61,7 +61,7 @@ export const usePrepareSplTokenAccountMutation = () => {
interaction.env,
solanaAddress,
);
await queryClient.refetchQueries(splTokenAccountsQueryKey);
await queryClient.refetchQueries(splTokenAccountsQueryKey, undefined, {});
}
});
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useMutation, useQueryClient } from "react-query";
import shallow from "zustand/shallow.js";

import { Protocol, getSolanaTokenDetails } from "../../config";
import { useSolanaConnection } from "../../contexts/SolanaConnection";
import { selectConfig, selectGetInteractionState } from "../../core/selectors";
import { useEnvironment, useInteractionState } from "../../core/store";
import {
Expand All @@ -21,11 +22,7 @@ import {
isUnlockEvmTx,
} from "../../models";
import { useEvmConnections, useEvmWallet } from "../evm";
import {
useSolanaConnection,
useSolanaWallet,
useSplTokenAccountsQuery,
} from "../solana";
import { useSolanaWallet, useSplTokenAccountsQuery } from "../solana";

export const useReloadInteractionStateMutation = () => {
const queryClient = useQueryClient();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useMutation } from "react-query";
import shallow from "zustand/shallow.js";

import { EcosystemId, isEvmEcosystemId } from "../../config";
import { useSolanaConnection } from "../../contexts/SolanaConnection";
import { selectConfig } from "../../core/selectors";
import { useEnvironment, useInteractionStateV2 } from "../../core/store";
import type { RemoveInteractionState } from "../../models";
Expand All @@ -13,7 +14,7 @@ import {
getTokensByPool,
} from "../../models";
import { useWallets } from "../crossEcosystem";
import { useSolanaConnection, useSplTokenAccountsQuery } from "../solana";
import { useSplTokenAccountsQuery } from "../solana";

export const useRemoveInteractionMutation = () => {
const { env } = useEnvironment();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { useMutation } from "react-query";
import shallow from "zustand/shallow.js";

import { EcosystemId } from "../../config";
import { useSolanaConnection } from "../../contexts/SolanaConnection";
import { selectConfig } from "../../core/selectors";
import { useEnvironment, useInteractionStateV2 } from "../../core/store";
import type {
Expand All @@ -19,7 +20,7 @@ import {
getTokensByPool,
} from "../../models";
import { useWallets } from "../crossEcosystem";
import { useSolanaConnection, useSplTokenAccountsQuery } from "../solana";
import { useSplTokenAccountsQuery } from "../solana";

export const useSingleChainSolanaSwapInteractionMutation = () => {
const { env } = useEnvironment();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,15 @@ import { useMutation } from "react-query";
import shallow from "zustand/shallow.js";

import { EcosystemId } from "../../config";
import { useSolanaConnection } from "../../contexts/SolanaConnection";
import { selectConfig, selectGetInteractionState } from "../../core/selectors";
import { useEnvironment, useInteractionState } from "../../core/store";
import {
doSingleSolanaPoolOperation,
getTokensByPool,
setOutputOperationInputAmount,
} from "../../models";
import {
useSolanaConnection,
useSolanaWallet,
useSplTokenAccountsQuery,
} from "../solana";
import { useSolanaWallet, useSplTokenAccountsQuery } from "../solana";

export const useSolanaPoolOperationsMutation = () => {
const { env } = useEnvironment();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { act, renderHook } from "@testing-library/react-hooks";
import { useQueryClient } from "react-query";

import { EcosystemId } from "../../config";
import { useSolanaConnection } from "../../contexts/SolanaConnection";
import { selectGetInteractionState } from "../../core/selectors";
import { useInteractionState } from "../../core/store";
import { MOCK_SOL_WALLET } from "../../fixtures";
Expand All @@ -13,7 +14,7 @@ import type { Wallets } from "../../models";
import { mockOf, renderHookWithAppContext } from "../../testUtils";
import { useWallets } from "../crossEcosystem";
import { useEvmConnections } from "../evm";
import { useSolanaConnection, useSplTokenAccountsQuery } from "../solana";
import { useSplTokenAccountsQuery } from "../solana";

import { useToSolanaTransferMutation } from "./useToSolanaTransferMutation";

Expand Down
3 changes: 2 additions & 1 deletion apps/ui/src/hooks/interaction/useToSolanaTransferMutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
getSolanaTokenDetails,
getTokenDetailsForEcosystem,
} from "../../config";
import { useSolanaConnection } from "../../contexts/SolanaConnection";
import { selectConfig, selectGetInteractionState } from "../../core/selectors";
import { useEnvironment, useInteractionState } from "../../core/store";
import type { EvmConnection, EvmTx } from "../../models";
Expand All @@ -27,7 +28,7 @@ import {
import { getFromEcosystemOfToSolanaTransfer } from "../../models/swim/transfer";
import { useWallets } from "../crossEcosystem";
import { useEvmConnections } from "../evm";
import { useSolanaConnection, useSplTokenAccountsQuery } from "../solana";
import { useSplTokenAccountsQuery } from "../solana";

const txResponseToTx = async (
interactionId: string,
Expand Down
1 change: 0 additions & 1 deletion apps/ui/src/hooks/solana/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ export * from "./useAccountNftsQuery";
export * from "./useAnchorProvider";
export * from "./useCreateSplTokenAccountsMutation";
export * from "./useLiquidityQuery";
export * from "./useSolanaConnection";
export * from "./useSolanaWallet";
export * from "./useSolBalanceQuery";
export * from "./useSplTokenAccountsQuery";
Expand Down
2 changes: 1 addition & 1 deletion apps/ui/src/hooks/solana/useAccountNftsQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import { useQuery } from "react-query";
import shallow from "zustand/shallow.js";

import { Protocol } from "../../config";
import { useSolanaConnection } from "../../contexts/SolanaConnection";
import { selectConfig } from "../../core/selectors";
import { useEnvironment } from "../../core/store";

import { useSolanaConnection } from "./useSolanaConnection";
import { useSolanaWallet } from "./useSolanaWallet";

const {
Expand Down
3 changes: 2 additions & 1 deletion apps/ui/src/hooks/solana/useAnchorProvider.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { AnchorProvider } from "@project-serum/anchor";

import { useSolanaConnection } from "./useSolanaConnection";
import { useSolanaConnection } from "../../contexts/SolanaConnection";

import { useSolanaWallet } from "./useSolanaWallet";

export const useAnchorProvider = (): AnchorProvider | null => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import type { AccountInfo as TokenAccount } from "@solana/spl-token";
import type { UseMutationResult } from "react-query";
import { useMutation, useQueryClient } from "react-query";

import { useSolanaConnection } from "../../contexts/SolanaConnection";
import { useEnvironment } from "../../core/store";
import { findOrCreateSplTokenAccount } from "../../models";

import { useSolanaConnection } from "./useSolanaConnection";
import { useSolanaWallet } from "./useSolanaWallet";
import { useSplTokenAccountsQuery } from "./useSplTokenAccountsQuery";

Expand Down
3 changes: 1 addition & 2 deletions apps/ui/src/hooks/solana/useLiquidityQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ import { PublicKey } from "@solana/web3.js";
import type { UseQueryResult } from "react-query";
import { useQueries, useQuery } from "react-query";

import { useSolanaConnection } from "../../contexts/SolanaConnection";
import { useEnvironment } from "../../core/store";
import {
deserializeTokenAccount,
getMultipleSolanaAccounts,
} from "../../models";

import { useSolanaConnection } from "./useSolanaConnection";

export const useLiquidityQuery = (
tokenAccountAddresses: readonly string[],
): UseQueryResult<readonly (TokenAccount | null)[], Error> => {
Expand Down
2 changes: 1 addition & 1 deletion apps/ui/src/hooks/solana/useSolBalanceQuery.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { LAMPORTS_PER_SOL } from "@solana/web3.js";
import Decimal from "decimal.js";
import { useQueryClient } from "react-query";

import { useSolanaConnection } from "../../contexts/SolanaConnection";
import { mockOf, renderHookWithAppContext } from "../../testUtils";

import { useSolBalanceQuery } from "./useSolBalanceQuery";
import { useSolanaConnection } from "./useSolanaConnection";
import { useSolanaWallet } from "./useSolanaWallet";

jest.mock("./useSolanaConnection", () => ({
Expand Down
2 changes: 1 addition & 1 deletion apps/ui/src/hooks/solana/useSolBalanceQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import Decimal from "decimal.js";
import type { UseQueryOptions, UseQueryResult } from "react-query";
import { useQuery } from "react-query";

import { useSolanaConnection } from "../../contexts/SolanaConnection";
import { useEnvironment } from "../../core/store";

import { useSolanaConnection } from "./useSolanaConnection";
import { useSolanaWallet } from "./useSolanaWallet";

// Returns user's Solana balance in SOL.
Expand Down
28 changes: 0 additions & 28 deletions apps/ui/src/hooks/solana/useSolanaConnection.ts

This file was deleted.

2 changes: 1 addition & 1 deletion apps/ui/src/hooks/solana/useSplTokenAccountsQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import type { Env } from "@swim-io/core";
import type { UseQueryOptions, UseQueryResult } from "react-query";
import { useQuery } from "react-query";

import { useSolanaConnection } from "../../contexts/SolanaConnection";
import { useEnvironment } from "../../core/store";
import { deserializeTokenAccount } from "../../models";

import { useSolanaConnection } from "./useSolanaConnection";
import { useSolanaWallet } from "./useSolanaWallet";

export const getSplTokenAccountsQueryKey = (
Expand Down
2 changes: 1 addition & 1 deletion apps/ui/src/hooks/swim/usePoolLpMint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import shallow from "zustand/shallow.js";

import type { PoolSpec } from "../../config";
import { EcosystemId, getSolanaTokenDetails } from "../../config";
import { useSolanaConnection } from "../../contexts/SolanaConnection";
import { selectConfig } from "../../core/selectors";
import { useEnvironment } from "../../core/store";
import { deserializeMint } from "../../models";
import { useSolanaConnection } from "../solana";

export const usePoolLpMints = (
poolSpecs: readonly PoolSpec[],
Expand Down
Loading