Skip to content

Commit

Permalink
fix: 0 withdraw
Browse files Browse the repository at this point in the history
  • Loading branch information
tuul-wq committed Apr 10, 2024
1 parent 4d1d9b4 commit ff84290
Show file tree
Hide file tree
Showing 11 changed files with 63 additions and 72 deletions.
1 change: 1 addition & 0 deletions src/renderer/shared/api/translation/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -1094,6 +1094,7 @@
"notEnoughBalanceError": "Not enough funds to transfer specific amount",
"notEnoughBalanceForDepositError": "Insufficient balance to pay deposit and network fee",
"notEnoughBalanceForFeeError": "Insufficient balance to pay network fee",
"notZeroAmountError": "Value cannot be zero",
"onChainPlaceholder": "On-chain",
"pasteButton": "Paste",
"recipientLabel": "Recipient",
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/widgets/Staking/BondExtra/model/form-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ const $bondForm = createForm<FormParams>({
},
{
name: 'notZero',
errorText: 'transfer.requiredAmountError',
errorText: 'transfer.notZeroAmountError',
validator: (value) => value !== ZERO_BALANCE,
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ const $bondForm = createForm<FormParams>({
},
{
name: 'notZero',
errorText: 'transfer.requiredAmountError',
errorText: 'transfer.notZeroAmountError',
validator: (value) => value !== ZERO_BALANCE,
},
{
Expand Down
4 changes: 3 additions & 1 deletion src/renderer/widgets/Staking/Payee/model/form-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
isStringsMatchQuery,
stakeableAmount,
validateAddress,
toShortAddress,
} from '@shared/lib/utils';

type FormParams = {
Expand Down Expand Up @@ -371,7 +372,8 @@ sample({
fn: (networkStore, formData) => {
const signatory = formData.signatory.accountId ? formData.signatory : undefined;
// TODO: update after i18n effector integration
const defaultText = `Change reward destination to ${formData.destination || 'restake'}`;
const shortAddress = toShortAddress(formData.destination);
const defaultText = `Change reward destination to ${shortAddress || 'restake'}`;
const description = signatory ? formData.description || defaultText : '';

return { ...formData, signatory, description };
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/widgets/Staking/Restake/model/form-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ const $restakeForm = createForm<FormParams>({
},
{
name: 'notZero',
errorText: 'transfer.requiredAmountError',
errorText: 'transfer.notZeroAmountError',
validator: (value) => value !== ZERO_BALANCE,
},
{
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/widgets/Staking/Unstake/model/form-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ const $unstakeForm = createForm<FormParams>({
},
{
name: 'notZero',
errorText: 'transfer.requiredAmountError',
errorText: 'transfer.notZeroAmountError',
validator: (value) => value !== ZERO_BALANCE,
},
{
Expand Down
44 changes: 27 additions & 17 deletions src/renderer/widgets/Staking/Withdraw/model/form-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
getRelaychainAsset,
toAddress,
dictionary,
formatAmount,
ZERO_BALANCE,
redeemableAmount,
} from '@shared/lib/utils';
Expand All @@ -29,7 +28,7 @@ import {
DESCRIPTION_LENGTH,
} from '@entities/transaction';

type BalanceMap = { balance: string; redeemable: string };
type BalanceMap = { balance: string; withdraw: string };

type FormParams = {
shards: Account[];
Expand Down Expand Up @@ -74,8 +73,8 @@ const $isMultisig = createStore<boolean>(false);
const $isProxy = createStore<boolean>(false);

const $accountsBalances = createStore<BalanceMap[]>([]);
const $redeemableBalance = createStore<string>(ZERO_BALANCE);
const $signatoryBalance = createStore<string>(ZERO_BALANCE);
const $withdrawBalance = createStore<string>(ZERO_BALANCE);
const $proxyBalance = createStore<string>(ZERO_BALANCE);

const $fee = restore(feeChanged, ZERO_BALANCE);
Expand Down Expand Up @@ -138,18 +137,29 @@ const $withdrawForm = createForm<FormParams>({
amount: {
init: '',
rules: [
{
name: 'required',
errorText: 'transfer.requiredAmountError',
validator: Boolean,
},
{
name: 'notZero',
errorText: 'transfer.notZeroAmountError',
validator: (value) => value !== ZERO_BALANCE,
},
{
name: 'insufficientBalanceForFee',
errorText: 'transfer.notEnoughBalanceForFeeError',
source: combine({
network: $networkStore,
fee: $fee,
isMultisig: $isMultisig,
accountsBalances: $accountsBalances,
}),
validator: (value, form, { network, accountsBalances }) => {
const amountBN = new BN(formatAmount(value, network.asset.precision));
validator: (value, form, { fee, isMultisig, accountsBalances }) => {
if (isMultisig) return true;

return form.shards.every((_: Account, index: number) => {
return amountBN.lte(new BN(accountsBalances[index].balance));
return new BN(fee).lte(new BN(accountsBalances[index].balance));
});
},
},
Expand Down Expand Up @@ -275,11 +285,11 @@ const $accounts = combine(
return shards.map((shard) => {
const balance = balanceUtils.getBalance(balances, shard.accountId, chain.chainId, asset.assetId.toString());
const address = toAddress(shard.accountId, { prefix: chain.addressPrefix });
const redeemable = redeemableAmount(staking[address]?.unlocking, era || 0);
const withdraw = redeemableAmount(staking[address]?.unlocking, era || 0);

return {
account: shard,
balances: { balance: transferableAmount(balance), redeemable },
balances: { balance: transferableAmount(balance), withdraw },
};
});
},
Expand Down Expand Up @@ -455,15 +465,15 @@ sample({
fn: (balances) => {
if (balances.length === 0) return ZERO_BALANCE;

const totalRedeemable = balances.reduce<BN>((acc, { redeemable }) => {
if (!redeemable) return acc;
const totalWithdraw = balances.reduce<BN>((acc, { withdraw }) => {
if (!withdraw) return acc;

return new BN(redeemable).add(new BN(acc));
return new BN(withdraw).add(new BN(acc));
}, new BN(ZERO_BALANCE));

return totalRedeemable.toString();
return totalWithdraw.toString();
},
target: $redeemableBalance,
target: [$withdrawBalance, $withdrawForm.fields.amount.onChange],
});

sample({
Expand All @@ -479,7 +489,7 @@ sample({
shards: $withdrawForm.fields.shards.$value,
},
fn: ({ accounts, shards }) => {
return accounts.reduce<{ balance: string; redeemable: string }[]>((acc, { account, balances }) => {
return accounts.reduce<BalanceMap[]>((acc, { account, balances }) => {
if (shards.includes(account)) {
acc.push(balances);
}
Expand Down Expand Up @@ -558,7 +568,7 @@ sample({
sample({
clock: $withdrawForm.formValidated,
source: {
amount: $redeemableBalance,
amount: $withdrawBalance,
realAccounts: $realAccounts,
network: $networkStore,
transactions: $transactions,
Expand Down Expand Up @@ -626,7 +636,7 @@ export const formModel = {

$accounts,
$accountsBalances,
$redeemableBalance,
$withdrawBalance,
$proxyBalance,

$fee,
Expand Down
12 changes: 1 addition & 11 deletions src/renderer/widgets/Staking/Withdraw/ui/Confirmation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { cnTw } from '@shared/lib/utils';
import { AssetBalance } from '@entities/asset';
import { AssetFiatBalance } from '@entities/price/ui/AssetFiatBalance';
import { confirmModel } from '../model/confirm-model';
import { AccountsModal, StakingPopover, UnstakingDuration } from '@entities/staking';
import { AccountsModal } from '@entities/staking';
import { useToggle } from '@shared/lib/hooks';

type Props = {
Expand Down Expand Up @@ -182,16 +182,6 @@ export const Confirmation = ({ onGoBack }: Props) => {
</div>
</DetailRow>
)}

<StakingPopover labelText={t('staking.confirmation.hintTitle')}>
<StakingPopover.Item>
{t('staking.confirmation.hintUnstakePeriod')} {' ('}
<UnstakingDuration api={api} />
{')'}
</StakingPopover.Item>
<StakingPopover.Item>{t('staking.confirmation.hintNoRewards')}</StakingPopover.Item>
<StakingPopover.Item>{t('staking.confirmation.hintWithdraw')}</StakingPopover.Item>
</StakingPopover>
</dl>

<div className="flex w-full justify-between mt-3">
Expand Down
9 changes: 4 additions & 5 deletions src/renderer/widgets/Staking/Withdraw/ui/WithdrawForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ const AccountsSelector = () => {
name={isShard ? toShortAddress(address, 16) : account.name}
canCopy={false}
/>
<AssetBalance value={balances.redeemable} asset={network.asset} />
<AssetBalance value={balances.withdraw} asset={network.asset} />
</div>
),
};
Expand Down Expand Up @@ -152,7 +152,7 @@ const Amount = () => {
fields: { amount },
} = useForm(formModel.$withdrawForm);

const redeemableBalance = useUnit(formModel.$redeemableBalance);
const withdrawBalance = useUnit(formModel.$withdrawBalance);
const isStakingLoading = useUnit(formModel.$isStakingLoading);
const network = useUnit(formModel.$networkStore);

Expand All @@ -163,12 +163,11 @@ const Amount = () => {
<AmountInput
disabled
invalid={amount.hasError()}
value={formatBalance(redeemableBalance, network.asset.precision).value}
balance={isStakingLoading ? <Shimmering width={50} height={10} /> : redeemableBalance}
value={formatBalance(amount.value, network.asset.precision).value}
balance={isStakingLoading ? <Shimmering width={50} height={10} /> : withdrawBalance}
balancePlaceholder={t('general.input.availableLabel')}
placeholder={t('general.input.amountLabel')}
asset={network.asset}
onChange={amount.onChange}
/>
<InputHint active={amount.hasError()} variant="error">
{t(amount.errorText())}
Expand Down
51 changes: 22 additions & 29 deletions src/renderer/widgets/Transfer/model/form-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
toAccountId,
toAddress,
dictionary,
ZERO_BALANCE,
} from '@shared/lib/utils';

type BalanceMap = Record<'balance' | 'native', string>;
Expand Down Expand Up @@ -71,12 +72,12 @@ const $isProxy = createStore<boolean>(false);

const $isMyselfXcmOpened = createStore<boolean>(false).reset(xcmDestinationCancelled);

const $accountBalance = createStore<BalanceMap>({ balance: '0', native: '0' });
const $signatoryBalance = createStore<BalanceMap>({ balance: '0', native: '0' });
const $proxyBalance = createStore<BalanceMap>({ balance: '0', native: '0' });
const $accountBalance = createStore<BalanceMap>({ balance: ZERO_BALANCE, native: ZERO_BALANCE });
const $signatoryBalance = createStore<string>(ZERO_BALANCE);
const $proxyBalance = createStore<BalanceMap>({ balance: ZERO_BALANCE, native: ZERO_BALANCE });

const $fee = restore(feeChanged, '0');
const $multisigDeposit = restore(multisigDepositChanged, '0');
const $fee = restore(feeChanged, ZERO_BALANCE);
const $multisigDeposit = restore(multisigDepositChanged, ZERO_BALANCE);
const $isFeeLoading = restore(isFeeLoadingChanged, true);
const $isXcm = createStore<boolean>(false);

Expand Down Expand Up @@ -160,8 +161,8 @@ const $transferForm = createForm<FormParams>({
},
{
name: 'notZero',
errorText: 'transfer.requiredAmountError',
validator: (value) => value !== '0',
errorText: 'transfer.notZeroAmountError',
validator: (value) => value !== ZERO_BALANCE,
},
{
name: 'notEnoughBalance',
Expand Down Expand Up @@ -190,8 +191,8 @@ const $transferForm = createForm<FormParams>({
accountBalance: $accountBalance,
}),
validator: (value, _, { network, isNative, isProxy, isMultisig, isXcm, accountBalance, ...rest }) => {
const feeBN = new BN(isProxy || isMultisig ? '0' : rest.fee);
const xcmFeeBN = new BN(isXcm ? rest.xcmFee : '0');
const feeBN = new BN(isProxy || isMultisig ? ZERO_BALANCE : rest.fee);
const xcmFeeBN = new BN(isXcm ? rest.xcmFee : ZERO_BALANCE);
const amountBN = new BN(formatAmount(value, network.asset.precision));

return isNative
Expand Down Expand Up @@ -329,28 +330,20 @@ const $signatories = combine(
({ network, txWrappers, balances }) => {
if (!network) return [];

const { chain, asset } = network;
const { chain } = network;

return txWrappers.reduce<Array<{ signer: Account; balances: BalanceMap }[]>>((acc, wrapper) => {
return txWrappers.reduce<Array<{ signer: Account; balance: string }[]>>((acc, wrapper) => {
if (!transactionService.hasMultisig([wrapper])) return acc;

const balancedSignatories = (wrapper as MultisigTxWrapper).signatories.map((signatory) => {
const balance = balanceUtils.getBalance(balances, signatory.accountId, chain.chainId, asset.assetId.toString());

let nativeBalance = balance;
if (asset.assetId !== chain.assets[0].assetId) {
nativeBalance = balanceUtils.getBalance(
balances,
signatory.accountId,
chain.chainId,
chain.assets[0].assetId.toString(),
);
}

return {
signer: signatory,
balances: { balance: transferableAmount(balance), native: transferableAmount(nativeBalance) },
};
const balance = balanceUtils.getBalance(
balances,
signatory.accountId,
chain.chainId,
chain.assets[0].assetId.toString(),
);

return { signer: signatory, balance: transferableAmount(balance) };
});

acc.push(balancedSignatories);
Expand Down Expand Up @@ -553,7 +546,7 @@ sample({
fn: (accounts, account) => {
const match = accounts.find((a) => a.account.id === account.id);

return match?.balances || { balance: '0', native: '0' };
return match?.balances || { balance: ZERO_BALANCE, native: ZERO_BALANCE };
},
target: $accountBalance,
});
Expand Down Expand Up @@ -597,7 +590,7 @@ sample({
fn: (signatories, signatory) => {
const match = signatories[0].find(({ signer }) => signer.id === signatory.id);

return match?.balances || { balance: '0', native: '0' };
return match?.balance || ZERO_BALANCE;
},
target: $signatoryBalance,
});
Expand Down
6 changes: 1 addition & 5 deletions src/renderer/widgets/Transfer/ui/TransferForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,10 @@ const Signatories = () => {

if (!isMultisig || !network) return null;

const signatoriesWithBalances = signatories[0].map(({ signer, balances }) => {
return { signer, balance: balances.balance };
});

return (
<SignatorySelector
signatory={signatory.value}
signatories={signatoriesWithBalances}
signatories={signatories[0]}
asset={network.chain.assets[0]}
addressPrefix={network.chain.addressPrefix}
hasError={signatory.hasError()}
Expand Down

0 comments on commit ff84290

Please sign in to comment.