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

[FRE-522] resolve wallet fallacy #140

Merged
merged 4 commits into from
Jan 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 src/components/AssetInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ function AssetInput({

const showChainInfo = chain ? false : true;

const account = useAccount(chain?.chainID);
const account = useAccount(context === "src" ? "source" : "destination");

const { data: balances } = useBalancesByChain(
account?.address,
Expand Down
19 changes: 17 additions & 2 deletions src/components/SwapWidget/SwapWidget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {} from "typed-query-selector";

import { disclosure } from "@/context/disclosures";
import { useSettingsStore } from "@/context/settings";
import { trackWallet } from "@/context/track-wallet";
import { useAccount } from "@/hooks/useAccount";
import { useChains as useSkipChains } from "@/hooks/useChains";

Expand Down Expand Up @@ -65,8 +66,8 @@ export function SwapWidget() {
parseFloat(route.usdAmountIn);
}

const srcAccount = useAccount(sourceChain?.chainID);
const destAccount = useAccount(destinationChain?.chainID);
const srcAccount = useAccount("source");
const destAccount = useAccount("destination");

const isWalletConnected =
srcAccount?.isWalletConnected && destAccount?.isWalletConnected;
Expand Down Expand Up @@ -170,6 +171,20 @@ export function SwapWidget() {
amountOut: amountIn,
direction: direction === "swap-in" ? "swap-out" : "swap-in",
});
if (destAccount?.wallet?.walletName) {
trackWallet.track(
"source",
destinationChain.chainID,
destAccount.wallet.walletName,
);
}
if (sourceChain?.chainID && srcAccount?.wallet?.walletName) {
trackWallet.track(
"destination",
sourceChain.chainID,
srcAccount?.wallet?.walletName,
);
}
}}
data-testid="swap-button"
ref={invertButtonRef}
Expand Down
2 changes: 1 addition & 1 deletion src/components/SwapWidget/useSwapWidget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ export function useSwapWidget() {
destinationAsset?.decimals,
]);

const account = useAccount(sourceChain?.chainID);
const account = useAccount("source");

const { assetsByChainID } = useAssets();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ function TransactionDialogContent({

const { getWalletRepo } = useManager();

const srcAccount = useAccount(route.sourceAssetChainID);
const dstAccount = useAccount(route.destAssetChainID);
const srcAccount = useAccount("source");
const dstAccount = useAccount("destination");

async function onSubmit() {
setTransacting(true);
Expand Down
12 changes: 8 additions & 4 deletions src/components/WalletModal/WalletModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { useAccount, useConnect, useDisconnect } from "wagmi";
import { chainIdToName } from "@/chains/types";
import { DialogContent } from "@/components/Dialog";
import { EVM_WALLET_LOGOS, INJECTED_EVM_WALLET_LOGOS } from "@/constants/wagmi";
import { trackAccount } from "@/context/account";
import { trackWallet } from "@/context/track-wallet";
import { useChainByID } from "@/hooks/useChains";

import { AdaptiveLink } from "../AdaptiveLink";
Expand Down Expand Up @@ -156,7 +156,7 @@ export function WalletModal({ chainType, onClose, wallets }: Props) {

function WalletModalWithContext() {
const { connector: currentConnector } = useAccount();
const { chainID } = useWalletModal();
const { chainID, context } = useWalletModal();
const { disconnect } = useDisconnect();
const { connectors, connect } = useConnect();
const { getWalletRepo } = useManager();
Expand Down Expand Up @@ -203,11 +203,15 @@ function WalletModalWithContext() {
assetList: w.assetList,
});
await w.connect();
trackAccount.track(chainID, w.walletName);
trackWallet.track(
context === "src" ? "source" : "destination",
chainID,
w.walletName,
);
},
disconnect: async () => {
await w.disconnect();
trackAccount.untrack(chainID);
trackWallet.untrack(context === "src" ? "source" : "destination");
},
isWalletConnected: w.isWalletConnected,
}));
Expand Down
32 changes: 0 additions & 32 deletions src/context/account.ts

This file was deleted.

53 changes: 53 additions & 0 deletions src/context/track-wallet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { create } from "zustand";
import { createJSONStorage, persist } from "zustand/middleware";

export type TrackWalletCtx = "source" | "destination";

interface TrackWalletStore {
source?: {
chainID: string;
walletName: string;
};
destination?: {
chainID: string;
walletName: string;
};
}

const defaultValues: TrackWalletStore = {
source: undefined,
destination: undefined,
};

const store = create(
persist(() => defaultValues, {
name: "TrackWalletState",
version: 1,
storage: createJSONStorage(() => window.sessionStorage),
}),
);

export const trackWallet = {
track: (ctx: TrackWalletCtx, chainID: string, walletName: string) => {
store.setState({
[ctx]: { chainID, walletName },
});
},
untrack: (ctx: TrackWalletCtx) => {
store.setState({
[ctx]: undefined,
});
},
};

export function useTrackWalletByCtx(ctx: TrackWalletCtx) {
return store((state) => state[ctx]);
}

export function useTrackWallet() {
return store((state) => state);
}

export function getTrackWallet() {
return store.getState();
}
13 changes: 7 additions & 6 deletions src/hooks/useAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ import { useMemo } from "react";
import { useAccount as useWagmiAccount } from "wagmi";

import { EVM_WALLET_LOGOS, INJECTED_EVM_WALLET_LOGOS } from "@/constants/wagmi";
import { useTrackAccount } from "@/context/account";
import { TrackWalletCtx, useTrackWalletByCtx } from "@/context/track-wallet";
import { useChainByID } from "@/hooks/useChains";
export function useAccount(context: TrackWalletCtx) {
const trackedWallet = useTrackWalletByCtx(context);

export function useAccount(chainID?: string) {
const { data: chain } = useChainByID(chainID);
const { data: chain } = useChainByID(trackedWallet?.chainID);

const { walletRepo } = useCosmosChain(
chain?.chainType === "cosmos" ? chain.chainName : "cosmoshub",
true,
);

const walletName = useTrackAccount(chainID);
const cosmosWallet = walletRepo.wallets.find((w) => {
return w.walletName === walletName;
return w.walletName === trackedWallet?.walletName;
});

const wagmiAccount = useWagmiAccount();
Expand All @@ -26,7 +26,8 @@ export function useAccount(chainID?: string) {
if (chain.chainType === "cosmos" && cosmosWallet) {
return {
address: cosmosWallet.address,
isWalletConnected: cosmosWallet.isWalletConnected,
isWalletConnected:
cosmosWallet.isWalletConnected && !cosmosWallet.isWalletDisconnected,
wallet: cosmosWallet
? {
walletName: cosmosWallet.walletInfo.name,
Expand Down
4 changes: 2 additions & 2 deletions src/solve/context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { useNetwork as useWagmiNetwork } from "wagmi";

import { chainIdToName } from "@/chains";
import { API_URL } from "@/constants/api";
import { getTrackAccount } from "@/context/account";
import { getTrackWallet } from "@/context/track-wallet";
import { getNodeProxyEndpoint } from "@/utils/api";
import { isWalletClientUsingLedger } from "@/utils/wallet";

Expand All @@ -26,7 +26,7 @@ export function SkipProvider({ children }: { children: ReactNode }) {
throw new Error(`getCosmosSigner error: unknown chainID '${chainID}'`);
}

const walletName = getTrackAccount(chainID);
const walletName = getTrackWallet().source?.walletName;
const wallet = getWalletRepo(chainName).wallets.find((w) => {
return w.walletName === walletName;
});
Expand Down