Skip to content
This repository has been archived by the owner on Dec 21, 2023. It is now read-only.

Commit

Permalink
reassign wallet on 409 (#2423)
Browse files Browse the repository at this point in the history
  • Loading branch information
intergalacticspacehighway authored Sep 5, 2023
1 parent a19509b commit 9280cc7
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 22 deletions.
83 changes: 62 additions & 21 deletions packages/app/hooks/use-add-wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,20 @@ import { addWalletToBackend } from "app/lib/add-wallet/add-wallet";
import { Logger } from "app/lib/logger";
import { fetchNonce } from "app/lib/nonce";
import { MY_INFO_ENDPOINT } from "app/providers/user-provider";
import { WalletAddressesV2 } from "app/types";
import { isMobileWeb } from "app/utilities";

const getWalletDefalutNickname = (
walletName: string | undefined,
userWallets: WalletAddressesV2[] | undefined
) => {
const index = userWallets?.findIndex((item) => item.nickname === walletName);
if (!index || index === -1) {
return walletName;
}
return `${walletName} ${(userWallets?.length ?? 0) + 1}`;
};

const useAddWallet = () => {
const [status, setStatus] = useState<"idle" | "loading" | "error">("idle");
const Alert = useAlert();
Expand All @@ -27,6 +39,10 @@ const useAddWallet = () => {
const userWallets = user?.user?.data.profile.wallet_addresses_v2;

const addWallet = async () => {
let signature: string | undefined;
let address: string | undefined;
let walletName: string | undefined;

try {
setStatus("loading");

Expand All @@ -36,19 +52,10 @@ const useAddWallet = () => {

const res = await wallet.connect();

const getWalletDefalutNickname = () => {
const walletName = res?.walletName;
const index = userWallets?.findIndex(
(item) => item.nickname === walletName
);
if (!index || index === -1) {
return res?.walletName;
}
return `${res?.walletName} ${(userWallets?.length ?? 0) + 1}`;
};

if (res) {
const nonce = await fetchNonce(res.address);
address = res.address;
walletName = res.walletName;
const nonce = await fetchNonce(address);
const message = process.env.NEXT_PUBLIC_SIGNING_MESSAGE + " " + nonce;
if (isMobileWeb()) {
Alert.alert(
Expand All @@ -61,13 +68,17 @@ const useAddWallet = () => {
{
text: "Sign",
onPress: async () => {
const signature = await wallet.signMessageAsync({
signature = await wallet.signMessageAsync({
message,
});
if (signature) {
if (signature && address) {
const addedWallet = await addWalletToBackend({
address: res.address,
address,
signature,
nickname: getWalletDefalutNickname(
walletName,
userWallets
),
});

mutate(MY_INFO_ENDPOINT);
Expand All @@ -82,12 +93,12 @@ const useAddWallet = () => {
]
);
} else {
const signature = await wallet.signMessageAsync({ message });
if (signature) {
signature = await wallet.signMessageAsync({ message });
if (signature && address) {
const addedWallet = await addWalletToBackend({
address: res.address,
address,
signature,
nickname: getWalletDefalutNickname(),
nickname: getWalletDefalutNickname(walletName, userWallets),
});

mutate(MY_INFO_ENDPOINT);
Expand All @@ -103,8 +114,38 @@ const useAddWallet = () => {
setStatus("idle");
} catch (e: any) {
setStatus("error");
Alert.alert("Something went wrong", e.message);
Logger.error("failed adding wallet", e);
if (e?.response?.data?.error?.code === 409) {
Alert.alert(
`This wallet is already linked to another Showtime account`,
e.message,
[
{ text: "Cancel" },
{
text: "Confirm",
onPress: async () => {
if (signature && address) {
const addedWallet = await addWalletToBackend({
address,
signature,
nickname: getWalletDefalutNickname(walletName, userWallets),
reassignWallet: true,
});

mutate(MY_INFO_ENDPOINT);

// automatically set the primary wallet on add wallet if user doesn't have one
if (hasNoPrimaryWallet) {
setPrimaryWallet(addedWallet);
}
}
},
},
]
);
} else {
Logger.error("failed adding wallet", e);
Alert.alert("Something went wrong", e.message);
}
}
};

Expand Down
4 changes: 3 additions & 1 deletion packages/app/lib/add-wallet/add-wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@ export const addWalletToBackend = async ({
address,
signature,
nickname,
reassignWallet,
}: {
address: string;
signature: string;
nickname?: string;
reassignWallet?: boolean;
}) => {
return axios({
url: `/v2/wallet/${address}/add`,
method: "POST",
data: { address, signature, nickname },
data: { address, signature, nickname, reassign_wallet: reassignWallet },
});
};

Expand Down

0 comments on commit 9280cc7

Please sign in to comment.