Skip to content
Draft
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { clearPersistedBridgeFormValues } from "./bridge-form-storage"
import { LocalStorageKey } from "./constants"

describe("clearPersistedBridgeFormValues", () => {
const storage = new Map<string, string>()

beforeEach(() => {
storage.clear()
vi.stubGlobal("localStorage", {
getItem: (key: string) => storage.get(key) ?? null,
setItem: (key: string, value: string) => {
storage.set(key, value)
},
removeItem: (key: string) => {
storage.delete(key)
},
clear: () => {
storage.clear()
},
})
})

afterEach(() => {
vi.unstubAllGlobals()
})

it("removes persisted bridge form values including recipient", () => {
localStorage.setItem(LocalStorageKey.BRIDGE_SRC_CHAIN_ID, "interwoven-1")
localStorage.setItem(LocalStorageKey.BRIDGE_SRC_DENOM, "uinit")
localStorage.setItem(LocalStorageKey.BRIDGE_DST_CHAIN_ID, "initiation-2")
localStorage.setItem(LocalStorageKey.BRIDGE_DST_DENOM, "uusdc")
localStorage.setItem(LocalStorageKey.BRIDGE_QUANTITY, "1")
localStorage.setItem(LocalStorageKey.BRIDGE_SLIPPAGE_PERCENT, "0.5")
localStorage.setItem(LocalStorageKey.BRIDGE_RECIPIENT, "init1recipient")
localStorage.setItem(LocalStorageKey.PUBLIC_KEY, "pubkey")

clearPersistedBridgeFormValues()

expect(localStorage.getItem(LocalStorageKey.BRIDGE_SRC_CHAIN_ID)).toBeNull()
expect(localStorage.getItem(LocalStorageKey.BRIDGE_SRC_DENOM)).toBeNull()
expect(localStorage.getItem(LocalStorageKey.BRIDGE_DST_CHAIN_ID)).toBeNull()
expect(localStorage.getItem(LocalStorageKey.BRIDGE_DST_DENOM)).toBeNull()
expect(localStorage.getItem(LocalStorageKey.BRIDGE_QUANTITY)).toBeNull()
expect(localStorage.getItem(LocalStorageKey.BRIDGE_SLIPPAGE_PERCENT)).toBeNull()
expect(localStorage.getItem(LocalStorageKey.BRIDGE_RECIPIENT)).toBeNull()
expect(localStorage.getItem(LocalStorageKey.PUBLIC_KEY)).toBe("pubkey")
})
})
17 changes: 17 additions & 0 deletions packages/interwovenkit-react/src/data/bridge-form-storage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { LocalStorageKey } from "./constants"

const BRIDGE_FORM_LOCAL_STORAGE_KEYS = [
LocalStorageKey.BRIDGE_SRC_CHAIN_ID,
LocalStorageKey.BRIDGE_SRC_DENOM,
LocalStorageKey.BRIDGE_DST_CHAIN_ID,
LocalStorageKey.BRIDGE_DST_DENOM,
LocalStorageKey.BRIDGE_QUANTITY,
LocalStorageKey.BRIDGE_SLIPPAGE_PERCENT,
LocalStorageKey.BRIDGE_RECIPIENT,
]

export function clearPersistedBridgeFormValues() {
for (const key of BRIDGE_FORM_LOCAL_STORAGE_KEYS) {
localStorage.removeItem(key)
}
}
1 change: 1 addition & 0 deletions packages/interwovenkit-react/src/data/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export const LocalStorageKey = {
BRIDGE_DST_DENOM: `${NAMESPACE}:bridge:dst-denom`,
BRIDGE_QUANTITY: `${NAMESPACE}:bridge:quantity`,
BRIDGE_SLIPPAGE_PERCENT: `${NAMESPACE}:bridge:slippage-percent`,
BRIDGE_RECIPIENT: `${NAMESPACE}:bridge:recipient`,
BRIDGE_ROUTE_TYPE: `${NAMESPACE}:bridge:route-type`,
BRIDGE_HISTORY: `${NAMESPACE}:bridge:history`,

Expand Down
10 changes: 2 additions & 8 deletions packages/interwovenkit-react/src/data/ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { atom, useAtom, useSetAtom } from "jotai"
import { useAnalyticsTrack } from "@/data/analytics"
import { useNavigate, useReset } from "@/lib/router"
import { useDeriveWallet } from "@/pages/autosign/data/wallet"
import { LocalStorageKey } from "./constants"
import { clearPersistedBridgeFormValues } from "./bridge-form-storage"

const isDrawerOpenAtom = atom<boolean>(false)
const isModalOpenAtom = atom<boolean>(false)
Expand Down Expand Up @@ -73,12 +73,6 @@ export function useDisconnect() {

clearAllWallets()

// Clear bridge form values on disconnect
localStorage.removeItem(LocalStorageKey.BRIDGE_SRC_CHAIN_ID)
localStorage.removeItem(LocalStorageKey.BRIDGE_SRC_DENOM)
localStorage.removeItem(LocalStorageKey.BRIDGE_DST_CHAIN_ID)
localStorage.removeItem(LocalStorageKey.BRIDGE_DST_DENOM)
localStorage.removeItem(LocalStorageKey.BRIDGE_QUANTITY)
localStorage.removeItem(LocalStorageKey.BRIDGE_SLIPPAGE_PERCENT)
clearPersistedBridgeFormValues()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,15 @@ const BridgeForm = () => {
localStorage.setItem(LocalStorageKey.BRIDGE_DST_DENOM, dstDenom)
localStorage.setItem(LocalStorageKey.BRIDGE_QUANTITY, quantity)
localStorage.setItem(LocalStorageKey.BRIDGE_SLIPPAGE_PERCENT, slippagePercent)
localStorage.setItem(LocalStorageKey.BRIDGE_RECIPIENT, recipient)
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Comment thread
cursor[bot] marked this conversation as resolved.
}, [
srcChainId,
srcDenom,
dstChainId,
dstDenom,
quantity,
slippagePercent,
recipient,
isSrcDenomValid,
isDstDenomValid,
])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export function useDefaultValues(): Partial<FormValues> {
dstDenom: localStorage.getItem(LocalStorageKey.BRIDGE_DST_DENOM),
quantity: localStorage.getItem(LocalStorageKey.BRIDGE_QUANTITY),
slippagePercent: localStorage.getItem(LocalStorageKey.BRIDGE_SLIPPAGE_PERCENT),
recipient: localStorage.getItem(LocalStorageKey.BRIDGE_RECIPIENT),
})

return {
Expand Down
Loading