-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
233 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 0 additions & 6 deletions
6
components/app/atoms/Swap/TokenSelectionModal/TokenSelectionCloseButton.tsx
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/** | ||
* Safely updates a nested property within a state object, ensuring type safety. | ||
* | ||
* @param currentState - The current state object. | ||
* @param topLevelKey - The key of the top-level property within the state object to update. | ||
* @param nestedKey - The key of the nested property within the top-level property to update. | ||
* @param newValue - The new value to set for the nested property. | ||
* @returns A new state object with the nested property updated. | ||
*/ | ||
function updateState< | ||
CurrentStateType extends Record<string, any>, | ||
TopLevelKey extends keyof CurrentStateType, | ||
NestedKey extends keyof CurrentStateType[TopLevelKey] | ||
>( | ||
currentState: CurrentStateType, | ||
topLevelKey: TopLevelKey, | ||
nestedKey: NestedKey, | ||
newValue: CurrentStateType[TopLevelKey][NestedKey] | Record<string, any> | ||
): CurrentStateType { | ||
// Determine if newValue is a plain object for a potential merge operation. | ||
const isObject = (obj: any): obj is Record<string, any> => | ||
obj && typeof obj === "object" && !Array.isArray(obj); | ||
|
||
// Perform the update operation. | ||
const updatedState = { | ||
...currentState, | ||
[topLevelKey]: { | ||
...currentState[topLevelKey], | ||
[nestedKey]: | ||
isObject(newValue) && isObject(currentState[topLevelKey][nestedKey]) | ||
? { ...currentState[topLevelKey][nestedKey], ...newValue } | ||
: newValue, | ||
}, | ||
}; | ||
|
||
return updatedState; | ||
} | ||
|
||
export default updateState; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { SecretString } from "../SecretString"; | ||
|
||
export interface WalletState { | ||
address: SecretString | null; | ||
SCRTBalance: string; | ||
ADMTBalance: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// hooks/useKeplrConnection.ts | ||
import { useEffect } from "react"; | ||
import { useStore } from "@/store/swapStore"; | ||
|
||
type CustomWindow = typeof window & { | ||
keplr: any; | ||
}; | ||
|
||
const useKeplrConnect = (manual: boolean) => { | ||
useEffect(() => { | ||
// check if user already rejected connection | ||
let attemptedConnection = false; | ||
|
||
const attemptConnection = async () => { | ||
const { keplr }: CustomWindow = window as CustomWindow; | ||
const connectionRefused = useStore.getState().connectionRefused; | ||
|
||
// if not manually connecting and user already rejected connection, do not attempt connection | ||
if (keplr && !manual && connectionRefused) { | ||
attemptedConnection = true; | ||
try { | ||
const chainId = "secret-4"; | ||
await keplr.enable(chainId); | ||
const offlineSigner = keplr.getOfflineSigner(chainId); | ||
const accounts = await offlineSigner.getAccounts(); | ||
|
||
if (accounts && accounts.length > 0) { | ||
const { address } = accounts[0]; | ||
// Update the store with the user's address | ||
useStore.getState().connectWallet(address); | ||
|
||
// Here, you might also want to fetch and update the SCRT and ADMT balances | ||
// For example: | ||
// useStore.getState().updateBalance('SCRT', '100'); // Fetch and use actual balance | ||
// useStore.getState().updateBalance('ADMT', '200'); // Fetch and use actual balance | ||
} | ||
} catch (error) { | ||
console.error("Error connecting to Keplr:", error); | ||
// set connection refused to true so we only connect again if the user clicks the connect button | ||
useStore.getState().setConnectionRefused(true); | ||
} | ||
} | ||
}; | ||
|
||
if (manual) { | ||
attemptConnection(); | ||
} | ||
|
||
// Optional: Listen for account change | ||
window.addEventListener("keplr_keystorechange", attemptConnection); | ||
|
||
return () => { | ||
window.removeEventListener("keplr_keystorechange", attemptConnection); | ||
}; | ||
}, [manual]); // Ensure this runs whenever the manual param changes | ||
}; | ||
|
||
export default useKeplrConnect; |
Oops, something went wrong.