Skip to content

Commit

Permalink
feat: Replace AccountResolver with ResolverService
Browse files Browse the repository at this point in the history
replace resolver functionally with better codebase and handling
  • Loading branch information
N3TC4T committed Nov 26, 2024
1 parent 9faa680 commit b67a12d
Show file tree
Hide file tree
Showing 24 changed files with 486 additions and 375 deletions.
109 changes: 109 additions & 0 deletions src/common/helpers/advisory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/**
Advisory helper
*/

import BigNumber from 'bignumber.js';

import LedgerService from '@services/LedgerService';

import { AccountRoot } from '@common/libs/ledger/types/ledger';
import { AccountInfoAccountFlags } from '@common/libs/ledger/types/methods/accountInfo';

/* Constants ==================================================================== */
const BLACK_HOLE_KEYS = ['rrrrrrrrrrrrrrrrrrrrrhoLvTp', 'rrrrrrrrrrrrrrrrrrrrBZbvji'];
const EXCHANGE_BALANCE_THRESHOLD = 1000000000000;
const MIN_TRANSACTION_TAG = 9999;
const HIGH_SENDER_COUNT = 10;
const HIGH_PERCENTAGE_TAGGED_TX = 50;

/* Helper Functions ==================================================================== */
/**
* The Advisory object provides methods to fetch account advisory information, account details,
* and perform various checks on accounts based on their data and flags.
*/
const Advisory = {
/**
* Determines whether a possible exchange can take place based on the account balance.
*
* The function evaluates if the account balance is defined and greater than a specified threshold.
*
* @param {AccountRoot} accountData - The account data containing balance information.
* @returns {boolean} - Returns true if the account balance exceeds the exchange threshold; otherwise, false.
*/
checkPossibleExchange: (accountData?: AccountRoot): boolean => {
return !!accountData?.Balance && new BigNumber(accountData.Balance).isGreaterThan(EXCHANGE_BALANCE_THRESHOLD);
},

/**
* Checks if a given account is a "black hole" account.
*
* A black hole account is determined by checking if:
* 1. The account has a RegularKey set.
* 2. The master key is disabled.
* 3. The RegularKey is one of the predefined black hole keys.
*
* @param {AccountRoot} accountData - The account data object containing details of the account.
* @param {AccountInfoAccountFlags} accountFlags - The flags indicating account settings.
* @returns {boolean} - True if the account is a black hole account, otherwise false.
*/
checkBlackHoleAccount: (accountData?: AccountRoot, accountFlags?: AccountInfoAccountFlags): boolean => {
return (
!!accountData?.RegularKey &&
!!accountFlags?.disableMasterKey &&
BLACK_HOLE_KEYS.includes(accountData.RegularKey)
);
},

/**
* Determines if incoming XRP is disallowed for an account based on its flags.
*
* @param {AccountInfoAccountFlags} accountFlags - The flags associated with the account.
* @returns {boolean} - Returns `true` if the account disallows incoming XRP, otherwise `false`.
*/
checkDisallowIncomingXRP: (accountFlags?: AccountInfoAccountFlags): boolean => {
return accountFlags?.disallowIncomingXRP ?? false;
},

/**
* Checks if a destination tag is required for transactions to a specific account.
*
* This function evaluates multiple conditions to determine if a destination tag
* should be enforced for incoming transactions to the specified account address.
* It first checks if the destination tag requirement is already specified in the
* provided advisory or account flags. If not, it retrieves recent transactions
* for the account and analyzes the percentage of incoming transactions that
* already use a destination tag, as well as the number of unique senders.
*
* @param {string} address - The account address to check for destination tag requirement.
* @param {XamanBackend.AccountAdvisoryResponse} advisory - Advisory response with force destination tag info.
* @param {AccountInfoAccountFlags} accountFlags - Account flags indicating if destination tag is required.
* @returns {Promise<boolean>} - Returns true if a destination tag is required, false otherwise.
*/
checkRequireDestinationTag: async (
address: string,
advisory: XamanBackend.AccountAdvisoryResponse,
accountFlags?: AccountInfoAccountFlags,
): Promise<boolean> => {
// already indicates on advisory or account info ?
if (advisory.force_dtag || accountFlags?.requireDestinationTag) {
return true;
}

const transactionsResp = await LedgerService.getTransactions(address, undefined, 200);

if (!('error' in transactionsResp) && transactionsResp.transactions?.length > 0) {
const incomingTXS = transactionsResp.transactions.filter((tx) => tx.tx.Destination === address);
const incomingTxCountWithTag = incomingTXS.filter(
(tx) => Number(tx.tx.DestinationTag) > MIN_TRANSACTION_TAG,
).length;
const uniqueSenders = new Set(transactionsResp.transactions.map((tx) => tx.tx.Account || '')).size;
const percentageTag = (incomingTxCountWithTag / incomingTXS.length) * 100;

return uniqueSenders >= HIGH_SENDER_COUNT && percentageTag > HIGH_PERCENTAGE_TAGGED_TX;
}

return false;
},
};

export default Advisory;
274 changes: 0 additions & 274 deletions src/common/helpers/resolver.ts

This file was deleted.

4 changes: 4 additions & 0 deletions src/common/utils/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ class LRUCache<K, V> {
}
this.cache.set(key, value);
}

delete(key: K): void {
this.cache.delete(key);
}
}

export default LRUCache;
Loading

0 comments on commit b67a12d

Please sign in to comment.