Skip to content

Commit bfc928b

Browse files
committed
chore: cleanup todos
1 parent fc04264 commit bfc928b

File tree

8 files changed

+47
-80
lines changed

8 files changed

+47
-80
lines changed

apps/namadillo/src/App/Swap/SwapCalculations.tsx

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,7 @@ import { SwapSource } from "./SwapSource";
2424
import { useSwapSimulation } from "./hooks/useSwapSimulation";
2525
import { useSwapValidation } from "./hooks/useSwapValidation";
2626
import { SwapQuote, SwapState, SwapStatus } from "./state";
27-
import {
28-
setInternalSwapStateAtom,
29-
swapQuoteAtom,
30-
swapStateAtom,
31-
swapStatusAtom,
32-
} from "./state/atoms";
27+
import { swapQuoteAtom, swapStateAtom, swapStatusAtom } from "./state/atoms";
3328

3429
const keplr = new KeplrWalletManager();
3530

@@ -45,7 +40,6 @@ export const SwapCalculations = (): JSX.Element => {
4540

4641
// Feature state
4742
const [swapState, setSwapState] = useAtom(swapStateAtom);
48-
const setInternalSwapState = useSetAtom(setInternalSwapStateAtom);
4943
const { data: quote } = useAtomValue(swapQuoteAtom);
5044
const setStatus = useSetAtom(swapStatusAtom);
5145

@@ -60,11 +54,7 @@ export const SwapCalculations = (): JSX.Element => {
6054
defaultShieldedAccountAtom
6155
)?.address;
6256

63-
useSwapSimulation({
64-
swapState,
65-
setInternalSwapState,
66-
quote,
67-
});
57+
useSwapSimulation();
6858

6959
const { walletAddress, connectToChainId, registry } = useWalletManager(keplr);
7060

apps/namadillo/src/App/Swap/hooks/usePerformOsmosisSwapTx.tsx

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import { AccountType } from "@namada/types";
33
import { allDefaultAccountsAtom } from "atoms/accounts";
44
import { chainAtom } from "atoms/chain";
55
import {
6-
createNotificationId,
76
dispatchToastNotificationAtom,
7+
getNotificationId,
88
} from "atoms/notifications";
99
import { createOsmosisSwapTxAtom } from "atoms/transfer/atoms";
1010
import {
@@ -20,34 +20,12 @@ import { useAtomValue, useSetAtom } from "jotai";
2020
import { broadcastTxWithEvents, signTx, TransactionPair } from "lib/query";
2121
import { useCallback, useState } from "react";
2222
import { NamadaAsset, OsmosisSwapTransactionData, TransferStep } from "types";
23+
import { TransactionError } from "types/errors";
2324
import { toBaseAmount } from "utils";
2425
import { SwapStatus } from "../state";
2526
import { swapQuoteAtom, swapStateAtom, swapStatusAtom } from "../state/atoms";
2627

27-
// TODO: reused - unify
28-
class TransactionError<T> extends Error {
29-
public cause: { originalError: unknown; context: TransactionPair<T> };
30-
constructor(
31-
public message: string,
32-
options: {
33-
cause: { originalError: unknown; context: TransactionPair<T> };
34-
}
35-
) {
36-
super(message);
37-
this.cause = options.cause;
38-
}
39-
}
40-
41-
// TODO: reused - unify;
42-
const getNotificationId = <T,>(tx: TransactionPair<T>): string => {
43-
const notificationId = createNotificationId(
44-
tx.encodedTxData.txs.map((tx) => tx.hash)
45-
);
46-
47-
return notificationId;
48-
};
49-
50-
// TODO: configurable
28+
// TODO: Should be a different address for housefire
5129
const SWAP_CONTRACT_ADDRESS =
5230
"osmo14q5zmg3fp774kpz2j8c52q7gqjn0dnm3vcj3guqpj4p9xylqpc7s2ezh0h";
5331

apps/namadillo/src/App/Swap/hooks/useSwapSimulation.tsx

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,20 @@
11
import invariant from "invariant";
2-
import { useEffect, useRef } from "react";
2+
import { useAtom, useAtomValue } from "jotai";
3+
import { useEffect } from "react";
34
import { toDisplayAmount } from "utils";
45
import { SwapQuote, SwapState } from "../state";
5-
6-
// TODO: no need to pass props, use atoms directly
7-
export const useSwapSimulation = ({
8-
swapState,
9-
setInternalSwapState,
10-
quote,
11-
}: {
12-
swapState: SwapState;
13-
setInternalSwapState: React.Dispatch<React.SetStateAction<SwapState>>;
14-
quote?: SwapQuote;
15-
}): void => {
16-
const swapStateRef = useRef(swapState);
17-
18-
useEffect(() => {
19-
swapStateRef.current = swapState;
20-
}, [swapState]);
6+
import {
7+
internalSwapStateAtom,
8+
swapQuoteAtom,
9+
swapStateAtom,
10+
} from "../state/atoms";
11+
12+
export const useSwapSimulation = (): void => {
13+
const { data: quote } = useAtomValue(swapQuoteAtom);
14+
const [internalSwapState, setInternalSwapState] = useAtom(
15+
internalSwapStateAtom
16+
);
17+
const swapState = useAtomValue(swapStateAtom);
2118

2219
useEffect(() => {
2320
const simulate = (quote: SwapQuote, swapState: SwapState): void => {
@@ -39,15 +36,15 @@ export const useSwapSimulation = ({
3936
const simulateBuy = swapState.mode === "buy";
4037

4138
if (simulateSell && sellAsset) {
42-
if (swapState.sellAmount === swapStateRef.current.sellAmount) {
39+
if (swapState.sellAmount === internalSwapState.sellAmount) {
4340
setInternalSwapState((s) => ({
4441
...s,
4542
buyAmount: toDisplayAmount(buyAsset, quote.amountOut),
4643
sellAmountPerOneBuy,
4744
}));
4845
}
4946
} else if (simulateBuy && buyAsset) {
50-
if (swapState.buyAmount === swapStateRef.current.buyAmount) {
47+
if (swapState.buyAmount === internalSwapState.buyAmount) {
5148
setInternalSwapState((s) => ({
5249
...s,
5350
sellAmount: toDisplayAmount(sellAsset, quote.amountIn),

apps/namadillo/src/App/Swap/state/atoms.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ export const swapQuoteAtom = atomWithQuery((get) => {
125125
const swapState = get(debouncedSwapStateAtom);
126126
const { sellAsset, buyAsset } = swapState;
127127

128-
// TODO: osmosis-1 should be dynamic
128+
// TODO: this should be configurable once we support osmosis testnet
129129
const osmosisAssets =
130130
getChainRegistryByChainId("osmosis-1")?.assets.assets || [];
131131

apps/namadillo/src/App/Swap/state/types.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ export const statusMessages: Record<
6666
},
6767
};
6868

69-
// TODO: make this type mroe specific
7069
export type SwapState = {
7170
mode: "sell" | "buy" | "none";
7271
sellAmount?: BigNumber;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
export const notificationIdSeparator = ";";
22
import { TxProps } from "@namada/types";
3+
import { TransactionPair } from "lib/query";
34

45
export const createNotificationId = (data?: TxProps["hash"][]): string => {
56
if (!data) return Date.now().toString();
67
if (Array.isArray(data)) return data.join(notificationIdSeparator);
78
return data;
89
};
10+
11+
export const getNotificationId = <T>(tx: TransactionPair<T>): string => {
12+
const notificationId = createNotificationId(
13+
tx.encodedTxData.txs.map((tx) => tx.hash)
14+
);
15+
16+
return notificationId;
17+
};

apps/namadillo/src/hooks/useTransaction.tsx

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { useMutation, UseMutationResult } from "@tanstack/react-query";
22
import { defaultAccountAtom } from "atoms/accounts";
33
import {
4-
createNotificationId,
54
dispatchToastNotificationAtom,
5+
getNotificationId,
66
} from "atoms/notifications";
77
import { getDisposableSigner } from "atoms/transfer/services";
88
import invariant from "invariant";
@@ -15,6 +15,7 @@ import {
1515
TransactionPair,
1616
} from "lib/query";
1717
import { BuildTxAtomParams, ToastNotification } from "types";
18+
import { TransactionError } from "types/errors";
1819
import { TransactionEventTypes } from "types/events";
1920
import { TransactionFeeProps, useTransactionFee } from "./useTransactionFee";
2021

@@ -61,14 +62,6 @@ export type UseTransactionOutput<T> = {
6162
unknown
6263
>;
6364

64-
const getNotificationId = <T,>(tx: TransactionPair<T>): string => {
65-
const notificationId = createNotificationId(
66-
tx.encodedTxData.txs.map((tx) => tx.hash)
67-
);
68-
69-
return notificationId;
70-
};
71-
7265
export const useTransaction = <T,>({
7366
params,
7467
createTxAtom,
@@ -133,19 +126,6 @@ export const useTransaction = <T,>({
133126
});
134127
};
135128

136-
class TransactionError<T> extends Error {
137-
public cause: { originalError: unknown; context: TransactionPair<T> };
138-
constructor(
139-
public message: string,
140-
options: {
141-
cause: { originalError: unknown; context: TransactionPair<T> };
142-
}
143-
) {
144-
super(message);
145-
this.cause = options.cause;
146-
}
147-
}
148-
149129
const transactionQuery = useMutation({
150130
mutationFn: async (
151131
additionalParams: Partial<BuildTxAtomParams<T>> = {}

apps/namadillo/src/types/errors.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { TransactionPair } from "lib/query";
2+
3+
export class TransactionError<T> extends Error {
4+
public cause: { originalError: unknown; context: TransactionPair<T> };
5+
constructor(
6+
public message: string,
7+
options: {
8+
cause: { originalError: unknown; context: TransactionPair<T> };
9+
}
10+
) {
11+
super(message);
12+
this.cause = options.cause;
13+
}
14+
}

0 commit comments

Comments
 (0)