-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Replace AccountResolver with ResolverService
replace resolver functionally with better codebase and handling
- Loading branch information
Showing
24 changed files
with
486 additions
and
375 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.