Skip to content

Commit

Permalink
WIP: remove and stash before rebasing
Browse files Browse the repository at this point in the history
  • Loading branch information
jaeaster committed May 2, 2023
1 parent c80927a commit 53786a9
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 50 deletions.
60 changes: 34 additions & 26 deletions src/components/Signin.tsx
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;
52 changes: 28 additions & 24 deletions src/hooks/useSwitchChain.tsx
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],
);
}

0 comments on commit 53786a9

Please sign in to comment.