Skip to content

Commit

Permalink
feat: fix issue with sync of accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
icfor committed Apr 8, 2024
1 parent 0166123 commit be38862
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 53 deletions.
3 changes: 3 additions & 0 deletions public/locales/en/staking.json
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,9 @@
"button": "Withdraw",
"fromWallet": "Use wallet:",
"selectAcc": "Select account:",
"success": {
"title": "You have successfully withdrawn your unstaked tokens"
},
"title": "Withdraw Tokens From Unstaked Accounts"
},
"your email": "Your Email",
Expand Down
3 changes: 3 additions & 0 deletions public/locales/zh-CN/staking.json
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,9 @@
"button": "Withdraw",
"fromWallet": "Use wallet:",
"selectAcc": "Select account:",
"success": {
"title": "You have successfully withdrawn your unstaked tokens"
},
"title": "Withdraw Tokens From Unstaked Accounts"
},
"your email": "你的邮箱地址",
Expand Down
3 changes: 3 additions & 0 deletions public/locales/zh-HK/staking.json
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,9 @@
"button": "Withdraw",
"fromWallet": "Use wallet:",
"selectAcc": "Select account:",
"success": {
"title": "You have successfully withdrawn your unstaked tokens"
},
"title": "Withdraw Tokens From Unstaked Accounts"
},
"your email": "你的電郵",
Expand Down
2 changes: 1 addition & 1 deletion scripts/lint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ if [ "$MISSING_TRANSLATIONS" -gt 0 ]; then
exit 1
fi

EMPTY_TRANSLATIONS="$(grep -r '""' public/locales | wc -l)"
EMPTY_TRANSLATIONS="$(grep -r ': "",' public/locales | wc -l)"

if [ "$EMPTY_TRANSLATIONS" -gt 0 ]; then
echo "Empty translations"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
import {
networkKeyToNetworkId,
networksWithRewards,
networksWithStakeAccounts,
networksWithStaking,
} from "@src/screens/staking/lib/staking_sdk/core";
import type {
Expand Down Expand Up @@ -115,9 +116,15 @@ const PopOver = ({
}
}, [stakingNetworkId, stakingRef]);

const { accounts, claimableRewards, inactiveStakeAccounts } = useMemo(() => {
const {
accounts,
activeStakeAccounts,
claimableRewards,
inactiveStakeAccounts,
} = useMemo(() => {
const result = {
accounts: null as Account[] | null,
activeStakeAccounts: null as null | StakeAccount[],
claimableRewards: null as NetworkClaimableRewards | null,
inactiveStakeAccounts: null as null | StakeAccount[],
};
Expand All @@ -129,10 +136,18 @@ const PopOver = ({
return result;
}

result.inactiveStakeAccounts = getStakeAccountsForNetwork(
const allStakeAccounts = getStakeAccountsForNetwork(
stakingRef.current.state,
stakingNetworkId,
)?.filter((stakeAccount) => stakeAccount.status === "inactive");
);

result.activeStakeAccounts = allStakeAccounts.filter(
(acc) => acc.status === "active",
);

result.inactiveStakeAccounts = allStakeAccounts.filter(
(acc) => acc.status === "inactive",
);

result.claimableRewards =
getClaimableRewardsForNetwork(
Expand Down Expand Up @@ -375,19 +390,22 @@ const PopOver = ({
{t("popover.withdrawUnstakeAccounts")}
</EmptyButton>
)}
{!!accountsWithDelegations?.length && (
<EmptyButton
onClick={() => {
setSelectedAccount(
stakingRef.current,
"unstake",
accountsWithDelegations[0],
);
}}
>
{t("popover.unstake")}
</EmptyButton>
)}
{!!accountsWithDelegations?.length &&
stakingNetworkId &&
(!networksWithStakeAccounts.has(stakingNetworkId) ||
!!activeStakeAccounts?.length) && (
<EmptyButton
onClick={() => {
setSelectedAccount(
stakingRef.current,
"unstake",
accountsWithDelegations[0],
);
}}
>
{t("popover.unstake")}
</EmptyButton>
)}
</>
)}
{canClickNetwork && (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,7 @@ const WithdrawUnstakedModal = () => {
}
}, [isOpen]);

const hasRewards = selectedAccount
? accountHasRewards(selectedAccount)
: null;

const stakeAccounts = (
const inactiveStakeAccounts = (
selectedAccount
? getStakeAccountsForNetwork(
stakingRef.current.state,
Expand All @@ -65,6 +61,10 @@ const WithdrawUnstakedModal = () => {
: []
).filter((acc) => acc.status === "inactive");

const hasRewards =
!!inactiveStakeAccounts?.length ||
(selectedAccount ? accountHasRewards(selectedAccount) : null);

const onClose = () => {
if (isLoading) return;

Expand Down Expand Up @@ -101,7 +101,7 @@ const WithdrawUnstakedModal = () => {
<div>
{selectedAccount && (
<StakeAccountsSelect
accounts={stakeAccounts}
accounts={inactiveStakeAccounts}
disabled={isLoading}
onChange={setSelectedStakeAccount}
selectedAccount={selectedStakeAccount}
Expand All @@ -112,17 +112,28 @@ const WithdrawUnstakedModal = () => {
<HighlightButton
disabled={!address || !networkId || isLoading || !hasRewards}
onClick={() => {
if (!selectedAccount?.address || !selectedStakeAccount || isLoading)
const usedSelectedStakeAccount =
selectedStakeAccount || inactiveStakeAccounts[0]?.address;

if (
!selectedAccount?.address ||
!usedSelectedStakeAccount ||
isLoading
)
return;

setIsLoading(true);

withdrawnUnstaked({
account: selectedAccount,
stakeAccountAddress: selectedStakeAccount,
stakeAccountAddress: usedSelectedStakeAccount,
})
.then(async (withdrawn) => {
if (withdrawn.success) {
await new Promise<void>((resolve) =>
setTimeout(resolve, 5000),
);

await syncAccountData(
stakingRef.current,
selectedAccount as NonNullable<typeof selectedAccount>,
Expand All @@ -131,8 +142,7 @@ const WithdrawUnstakedModal = () => {
setSelectedAccount(stakingRef.current, null, null);

toastSuccess({
subtitle: `${t("rewardsModal.success.sub")} 🎉`,
title: t("rewardsModal.success.title"),
title: t("withdrawUnstaked.success.title"),
});
} else if (withdrawn.error) {
const handlers: Record<ClaimRewardsError, () => void> = {
Expand Down
66 changes: 40 additions & 26 deletions src/screens/staking/lib/staking_sdk/context/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ export const syncAccountData = async (
context: TStakingContext,
account: Account,
) => {
const { address, networkId, wallet: walletId } = account;
const { address, networkId } = account;

const { info, rewards } = await fetchAccountData(
context,
Expand All @@ -224,34 +224,48 @@ export const syncAccountData = async (
false,
);

const newWallet: Wallet = {
...context.state.wallets[walletId],
networks: {
...context.state.wallets[walletId]?.networks,
[networkId]: {
accounts: [
...(
context.state.wallets[walletId]?.networks?.[networkId]?.accounts ||
[]
).filter((a: Account) => a.address !== address),
{
...account,
info,
rewards,
},
].sort(sortAccounts),
// This account may be in several wallets, so this updates all of them
Object.keys(context.state.wallets).forEach((walletId) => {
const wallet = walletId as WalletId;

const hasAccount = !!context.state.wallets[wallet]?.networks?.[
networkId
]?.accounts?.find((a) => a.address === address);

if (!hasAccount) return;

const newAccount: Account = {
...account,
info,
rewards,
wallet,
};

const newWallet: Wallet = {
...context.state.wallets[wallet],
networks: {
...context.state.wallets[wallet]?.networks,
[networkId]: {
accounts: [
...(
context.state.wallets[wallet]?.networks?.[networkId]?.accounts ||
[]
).filter((a: Account) => a.address !== address),
newAccount,
].sort(sortAccounts),
},
},
},
wallet: walletId,
};
wallet,
};

const newWallets = {
...context.state.wallets,
[walletId]: newWallet,
};
const newWallets = {
...context.state.wallets,
[walletId]: newWallet,
};

context.setState({
wallets: newWallets,
context.setState({
wallets: newWallets,
});
});
};

Expand Down
4 changes: 4 additions & 0 deletions src/screens/staking/lib/staking_sdk/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ export const networksWithRewards = new Set([
...Array.from(cosmosStakingNetworks),
]);

export const networksWithStakeAccounts = new Set([
...Array.from(solanaNetworks),
]);

export const walletsSupported = new Set([
...Array.from(cosmosWallets),
...Array.from(solanaWallets),
Expand Down

0 comments on commit be38862

Please sign in to comment.