generated from m1guelpf/dapp-starter
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: remove and stash before rebasing
- Loading branch information
Showing
2 changed files
with
62 additions
and
50 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,45 @@ | ||
import { FC } from 'react' | ||
import useGlobalStore from '@/stores/globalStore' | ||
import { Medusa } from '@medusa-network/medusa-sdk' | ||
import { CHAIN_CONFIG } from '@/lib/consts' | ||
import { useNetwork, useSigner } from 'wagmi' | ||
import { FC } from "react"; | ||
import useGlobalStore from "@/stores/globalStore"; | ||
import { Medusa } from "@medusa-network/medusa-sdk"; | ||
import { CHAIN_CONFIG } from "@/lib/consts"; | ||
import { useNetwork, useSigner } from "wagmi"; | ||
|
||
const Signin: FC = () => { | ||
const { chain } = useNetwork() | ||
const medusa = useGlobalStore((state) => state.medusa) | ||
const updateMedusa = useGlobalStore((state) => state.updateMedusa) | ||
const { data: signer } = useSigner() | ||
const { chain } = useNetwork(); | ||
const medusa = useGlobalStore((state) => state.medusa); | ||
const updateMedusa = useGlobalStore((state) => state.updateMedusa); | ||
const { data: signer } = useSigner(); | ||
|
||
const signMessage = async () => { | ||
if (!signer) return | ||
const medusa = await Medusa.init(CHAIN_CONFIG[chain?.id]?.oracleContractAddress, signer) | ||
await medusa.signForKeypair() | ||
updateMedusa(medusa) | ||
} | ||
if (!signer) return; | ||
const medusa = await Medusa.init( | ||
CHAIN_CONFIG[chain?.id]?.oracleContractAddress, | ||
signer, | ||
"localhost", | ||
); | ||
await medusa.signForKeypair(); | ||
updateMedusa(medusa); | ||
}; | ||
|
||
if (medusa?.keypair) { | ||
return <button | ||
return ( | ||
<button | ||
className="bg-gray-700 hover:bg-gray-500 hover:cursor-pointer text-gray-50 py-2 px-4 rounded" | ||
onClick={() => medusa.setKeypair(null)} | ||
> | ||
Sign out | ||
</button> | ||
); | ||
} | ||
|
||
return ( | ||
<button | ||
className="bg-gray-700 hover:bg-gray-500 hover:cursor-pointer text-gray-50 py-2 px-4 rounded" | ||
onClick={() => medusa.setKeypair(null)} | ||
onClick={() => signMessage()} | ||
> | ||
Sign out | ||
Sign in | ||
</button> | ||
} | ||
|
||
return <button | ||
className="bg-gray-700 hover:bg-gray-500 hover:cursor-pointer text-gray-50 py-2 px-4 rounded" | ||
onClick={() => signMessage()} | ||
> | ||
Sign in | ||
</button> | ||
} | ||
); | ||
}; | ||
|
||
export default Signin; |
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 |
---|---|---|
@@ -1,38 +1,42 @@ | ||
import { useSwitchNetwork } from 'wagmi'; | ||
import { getProvider } from '@wagmi/core'; | ||
import { VoidSigner } from 'ethers'; | ||
import { Medusa } from '@medusa-network/medusa-sdk'; | ||
import { useSwitchNetwork } from "wagmi"; | ||
import { getProvider } from "@wagmi/core"; | ||
import { VoidSigner } from "ethers"; | ||
import { Medusa } from "@medusa-network/medusa-sdk"; | ||
|
||
import useGlobalStore from '@/stores/globalStore' | ||
import { CHAIN_CONFIG } from '@/lib/consts'; | ||
import { useCallback } from 'react'; | ||
import useGlobalStore from "@/stores/globalStore"; | ||
import { CHAIN_CONFIG } from "@/lib/consts"; | ||
import { useCallback } from "react"; | ||
|
||
export default function useSwitchChain() { | ||
const { switchNetworkAsync } = useSwitchNetwork() | ||
const { switchNetworkAsync } = useSwitchNetwork(); | ||
|
||
const medusa = useGlobalStore((state) => state.medusa) | ||
const updateMedusa = useGlobalStore((state) => state.updateMedusa) | ||
const updateListings = useGlobalStore((state) => state.updateListings) | ||
const updateSales = useGlobalStore((state) => state.updateSales) | ||
const updateDecryptions = useGlobalStore((state) => state.updateDecryptions) | ||
const medusa = useGlobalStore((state) => state.medusa); | ||
const updateMedusa = useGlobalStore((state) => state.updateMedusa); | ||
const updateListings = useGlobalStore((state) => state.updateListings); | ||
const updateSales = useGlobalStore((state) => state.updateSales); | ||
const updateDecryptions = useGlobalStore((state) => state.updateDecryptions); | ||
|
||
return useCallback( | ||
async (chainId: number) => { | ||
await switchNetworkAsync?.(chainId) | ||
await switchNetworkAsync?.(chainId); | ||
|
||
updateListings([]) | ||
updateSales([]) | ||
updateDecryptions([]) | ||
updateListings([]); | ||
updateSales([]); | ||
updateDecryptions([]); | ||
|
||
if (medusa) { | ||
const newMedusa = await Medusa.init( | ||
CHAIN_CONFIG[chainId].oracleContractAddress, | ||
new VoidSigner(await medusa.signer.getAddress(), getProvider({ chainId })) | ||
) | ||
newMedusa.setKeypair(medusa.keypair) | ||
updateMedusa(newMedusa) | ||
new VoidSigner( | ||
await medusa.signer.getAddress(), | ||
getProvider({ chainId }), | ||
), | ||
"localhost", | ||
); | ||
newMedusa.setKeypair(medusa.keypair); | ||
updateMedusa(newMedusa); | ||
} | ||
}, [switchNetworkAsync] | ||
) | ||
}, | ||
[switchNetworkAsync], | ||
); | ||
} | ||
|