Skip to content

Commit

Permalink
[Add] Extension -Check account alive condition
Browse files Browse the repository at this point in the history
  • Loading branch information
tunghp2002 committed Oct 8, 2024
1 parent 85d6a20 commit fbee181
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 5 deletions.
1 change: 1 addition & 0 deletions packages/extension-base/src/background/KoniTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,7 @@ export enum TransferTxErrorType {
INVALID_TOKEN = 'INVALID_TOKEN',
TRANSFER_ERROR = 'TRANSFER_ERROR',
RECEIVER_NOT_ENOUGH_EXISTENTIAL_DEPOSIT = 'RECEIVER_NOT_ENOUGH_EXISTENTIAL_DEPOSIT',
RECEIVER_ACCOUNT_INACTIVE = 'RECEIVER_ACCOUNT_INACTIVE'
}

export type TransactionErrorType = BasicTxErrorType | TransferTxErrorType | StakingTxErrorType | YieldValidationStatus | SwapErrorType
Expand Down
13 changes: 10 additions & 3 deletions packages/extension-base/src/core/logic-validation/transfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { TransactionError } from '@subwallet/extension-base/background/errors/Tr
import { _Address, AmountData, BasicTxErrorType, BasicTxWarningCode, ExtrinsicDataTypeMap, ExtrinsicType, FeeData, TransferTxErrorType } from '@subwallet/extension-base/background/KoniTypes';
import { TransactionWarning } from '@subwallet/extension-base/background/warnings/TransactionWarning';
import { XCM_MIN_AMOUNT_RATIO } from '@subwallet/extension-base/constants';
import { _canAccountBeReaped } from '@subwallet/extension-base/core/substrate/system-pallet';
import { _canAccountBeReaped, _isAccountActive } from '@subwallet/extension-base/core/substrate/system-pallet';
import { FrameSystemAccountInfo } from '@subwallet/extension-base/core/substrate/types';
import { _TRANSFER_CHAIN_GROUP } from '@subwallet/extension-base/services/chain-service/constants';
import { _EvmApi } from '@subwallet/extension-base/services/chain-service/types';
Expand Down Expand Up @@ -49,7 +49,7 @@ export function validateTransferRequest (tokenInfo: _ChainAsset, from: _Address,
return [errors, keypair, transferValue];
}

export function additionalValidateTransfer (tokenInfo: _ChainAsset, nativeTokenInfo: _ChainAsset, extrinsicType: ExtrinsicType, receiverTransferTokenTotalBalance: string, transferAmount: string, senderTransferTokenTransferable?: string, _receiverNativeTotal?: string): [TransactionWarning[], TransactionError[]] {
export function additionalValidateTransfer (tokenInfo: _ChainAsset, nativeTokenInfo: _ChainAsset, extrinsicType: ExtrinsicType, receiverTransferTokenTotalBalance: string, transferAmount: string, senderTransferTokenTransferable?: string, _receiverNativeTotal?: string, isReceiverActive?: unknown): [TransactionWarning[], TransactionError[]] {
const minAmount = _getTokenMinAmount(tokenInfo);
const nativeMinAmount = _getTokenMinAmount(nativeTokenInfo);
const warnings: TransactionWarning[] = [];
Expand All @@ -73,6 +73,13 @@ export function additionalValidateTransfer (tokenInfo: _ChainAsset, nativeTokenI
}
}

// Check if receiver's account is active
if (isReceiverActive && _isAccountActive(isReceiverActive as FrameSystemAccountInfo)) {
const error = new TransactionError(TransferTxErrorType.RECEIVER_ACCOUNT_INACTIVE, t('The recipient account may be inactive. Change recipient account and try again'));

errors.push(error);
}

// Check ed for receiver after sending
if (new BigN(receiverTransferTokenTotalBalance).plus(transferAmount).lt(minAmount)) {
const atLeast = new BigN(minAmount).minus(receiverTransferTokenTotalBalance).plus((tokenInfo.decimals || 0) === 0 ? 0 : 1);
Expand Down Expand Up @@ -419,7 +426,7 @@ export function checkBalanceWithTransactionFee (validationResponse: SWTransactio
}

// todo: only system.pallet has metadata, we should add for other pallets and mechanisms as well
const isNeedCheckRemainingBalance = !isTransferAll && extrinsicType === ExtrinsicType.TRANSFER_BALANCE && nativeTokenAvailable.metadata && _canAccountBeReaped(nativeTokenAvailable.metadata as FrameSystemAccountInfo);
const isNeedCheckRemainingBalance = !isTransferAll && extrinsicType === ExtrinsicType.TRANSFER_BALANCE && nativeTokenAvailable.metadata && _canAccountBeReaped(nativeTokenAvailable.metadata);
const isRemainingBalanceValid = bnNativeTokenAvailable.minus(bnNativeTokenTransferAmount).minus(bnFee).lt(_getTokenMinAmount(nativeTokenInfo));

if (isNeedCheckRemainingBalance && isRemainingBalanceValid) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export function _canAccountBeReaped (accountInfo: FrameSystemAccountInfo): boole
}

export function _isAccountActive (accountInfo: FrameSystemAccountInfo): boolean {
return accountInfo.providers === 0 && accountInfo.consumers === 0;
return accountInfo.consumers === 0 && accountInfo.providers === 0 && accountInfo.sufficients === 0;
}

export function _getSystemPalletTotalBalance (accountInfo: FrameSystemAccountInfo): bigint {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1826,6 +1826,7 @@ export default class KoniExtension {
const additionalValidator = async (inputTransaction: SWTransactionResponse): Promise<void> => {
let senderTransferTokenTransferable: string | undefined;
let receiverNativeTotal: string | undefined;
let isReceiverActive: unknown;

// Check ed for sender
if (!isTransferNativeToken) {
Expand All @@ -1836,11 +1837,12 @@ export default class KoniExtension {

senderTransferTokenTransferable = _senderTransferTokenTransferable.value;
receiverNativeTotal = _receiverNativeTotal.value;
isReceiverActive = _receiverNativeTotal.metadata;
}

const { value: receiverTransferTokenTransferable } = await this.getAddressTotalBalance({ address: to, networkKey, token: tokenSlug, extrinsicType }); // todo: shouldn't be just transferable, locked also counts

const [warnings, errors] = additionalValidateTransfer(transferTokenInfo, nativeTokenInfo, extrinsicType, receiverTransferTokenTransferable, transferAmount.value, senderTransferTokenTransferable, receiverNativeTotal);
const [warnings, errors] = additionalValidateTransfer(transferTokenInfo, nativeTokenInfo, extrinsicType, receiverTransferTokenTransferable, transferAmount.value, senderTransferTokenTransferable, receiverNativeTotal, isReceiverActive);

warnings.length && inputTransaction.warnings.push(...warnings);
errors.length && inputTransaction.errors.push(...errors);
Expand Down

0 comments on commit fbee181

Please sign in to comment.