diff --git a/typology-11/src/rules/co-located-parties.ts b/typology-11/src/rules/co-located-parties.ts index 1edab972f..99396ccf6 100644 --- a/typology-11/src/rules/co-located-parties.ts +++ b/typology-11/src/rules/co-located-parties.ts @@ -8,13 +8,17 @@ const getPayeesFromILPsArray = async (outgoingTransfersClient: RedisClient, ILPS })) ); - return accountsTransfers.reduce((payeesAcc: any, payee: any) => { + if (accountsTransfers == undefined || accountsTransfers.length == 0) return undefined; + + const payeeNames = accountsTransfers.reduce((payeesAcc: any, payee: any) => { if (payee === null) return payeesAcc; const payeeTransfers = JSON.parse(payee); if (payeeTransfers.length < 1) return payeesAcc; const payeeNames = payeeTransfers.map((transfer: any) => transfer.ILPDestinationAccountAddress); return [...payeesAcc, ...payeeNames]; }, []); + + return { payeeNames, accountsTransfers }; } const removeDuplicates = (names: any) => names.filter((c: any, index: number) => { @@ -25,9 +29,30 @@ const removeValuesFromSecondArray = (firstArr: any, names: any) => names.filter( return firstArr.indexOf(c) === index; }); +const deg2rad = (num: number): number => (num * Math.PI) / 180; + +const distance = (location1: number[], location2: number[], useMiles = false): number => { + const coreRadius = 6371; // km + const dLat: number = deg2rad(location2[0] - location1[0]); + const dLon: number = deg2rad(location2[1] - location1[1]); + const a: number = Math.sin(dLat / 2) + * Math.sin(dLat / 2) + + Math.cos(deg2rad(location1[0])) + * Math.cos(deg2rad(location2[0])) + * Math.sin(dLon / 2) + * Math.sin(dLon / 2); + const c: number = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); + let d: number = Math.round(coreRadius * c); + if (useMiles) { // returns miles instead of kilometers + d = Math.round(d * 0.621371192); + } + return d; +}; + const handleCoLocatedParties = async (transfer: any, outgoingTransfersClient: RedisClient, historicalData: any) => { + if (historicalData == undefined || historicalData.length == 0) return false; const payeesList = historicalData.map((transfer: any) => transfer.ILPDestinationAccountAddress); payeesList.push(transfer.ILPDestinationAccountAddress); @@ -35,20 +60,26 @@ const handleCoLocatedParties = async (transfer: any, // get Payees names - tier 1 const tier1Payees = await getPayeesFromILPsArray(outgoingTransfersClient, uniqueNames); - payeesList.push(tier1Payees); - uniqueNames.push(removeDuplicates(tier1Payees)); + if (tier1Payees != undefined) { + payeesList.push(tier1Payees.payeeNames); + uniqueNames.push(removeDuplicates(tier1Payees.payeeNames)); + } // get Payees names - tier 2 const uniqueTier2Names = removeValuesFromSecondArray(uniqueNames, tier1Payees); const tier2Payees = await getPayeesFromILPsArray(outgoingTransfersClient, removeDuplicates(uniqueTier2Names)); - payeesList.push(tier2Payees); - uniqueNames.push(removeDuplicates(tier2Payees)); + if (tier2Payees != undefined) { + payeesList.push(tier2Payees.payeeNames); + uniqueNames.push(removeDuplicates(tier2Payees.payeeNames)); + } // get Payees names - tier 3 const uniqueTier3Names = removeValuesFromSecondArray(uniqueNames, tier2Payees); const tier3Payees = await getPayeesFromILPsArray(outgoingTransfersClient, removeDuplicates(uniqueTier3Names)); - payeesList.push(tier3Payees); - uniqueNames.push(removeDuplicates(tier3Payees)); + if (tier3Payees != undefined) { + payeesList.push(tier3Payees.payeeNames); + uniqueNames.push(removeDuplicates(tier3Payees.payeeNames)); + } if (payeesList.length < 8) return false; const uniqueEntitiesPercentage = uniqueNames.length / payeesList.length * 100; diff --git a/typology-11/src/rules/transaction-divergence.ts b/typology-11/src/rules/transaction-divergence.ts index a7aa37246..a779ef9c6 100644 --- a/typology-11/src/rules/transaction-divergence.ts +++ b/typology-11/src/rules/transaction-divergence.ts @@ -1,5 +1,5 @@ const handleTransactionDivergence = (transaction: any, payeeHistoricalSendData: any) => { - if (payeeHistoricalSendData.length < 10) return false; + if (payeeHistoricalSendData == undefined || payeeHistoricalSendData.length < 10) return false; const transactionDate = new Date(transaction.HTTPTransactionDate); // Count transaction over the last 8 hours diff --git a/typology-11/src/rules/transaction-mirroring.ts b/typology-11/src/rules/transaction-mirroring.ts index 50c1a7154..d69a438fe 100644 --- a/typology-11/src/rules/transaction-mirroring.ts +++ b/typology-11/src/rules/transaction-mirroring.ts @@ -27,7 +27,7 @@ const handleTransactionMirroring = ( payeeHistoricalSendData: any, payeeHistoricalReceiveData: any, ): boolean => { - if (payeeHistoricalSendData.length < 1) return false; + if (payeeHistoricalSendData == undefined || payeeHistoricalSendData.length < 1) return false; const { HTTPTransactionDate } = transfer; const currentTransferDate = new Date(HTTPTransactionDate); diff --git a/typology-11/src/rules/transactions-between-parties.ts b/typology-11/src/rules/transactions-between-parties.ts index 12e052570..541f6c613 100644 --- a/typology-11/src/rules/transactions-between-parties.ts +++ b/typology-11/src/rules/transactions-between-parties.ts @@ -7,6 +7,9 @@ const getTransfersFromILPsArray = async (outgoingTransfersClient: RedisClient, I resolve(get(outgoingTransfersClient, ILP)); })) ); + + if (accountsTransfers == undefined || accountsTransfers.length == 0) return undefined; + const payees = accountsTransfers.reduce((payeesAcc: any, payee: any) => { if (payee === null) return payeesAcc; const payeeTransfers = JSON.parse(payee); @@ -31,26 +34,34 @@ const handleTransactionsBetweenParties = async ( outgoingTransfersClient: RedisClient, historicalData: any ) => { + if (historicalData == undefined || historicalData.length == 0) return false; + const payeesList = historicalData.map((transfer: any) => transfer.ILPDestinationAccountAddress); payeesList.push(transfer.d); const uniqueNames = removeDuplicates(payeesList); // get Payess names - tier 1 const tier1Payees = await getTransfersFromILPsArray(outgoingTransfersClient, uniqueNames); - payeesList.push(tier1Payees.payees); - uniqueNames.push(removeDuplicates(tier1Payees.payees)); + if (tier1Payees != undefined) { + payeesList.push(tier1Payees.payees); + uniqueNames.push(removeDuplicates(tier1Payees.payees)); + } // get Payess names - tier 2 const uniqueTier2Names = removeValuesFromSecondArray(uniqueNames, tier1Payees.payees); const tier2Payees = await getTransfersFromILPsArray(outgoingTransfersClient, removeDuplicates(uniqueTier2Names)); - payeesList.push(tier2Payees.payees); - uniqueNames.push(removeDuplicates(tier2Payees.payees)); + if (tier2Payees != undefined) { + payeesList.push(tier2Payees.payees); + uniqueNames.push(removeDuplicates(tier2Payees.payees)); + } // get Payess names - tier 3 const uniqueTier3Names = removeValuesFromSecondArray(uniqueNames, tier2Payees.payees); const tier3Payees = await getTransfersFromILPsArray(outgoingTransfersClient, removeDuplicates(uniqueTier3Names)); - payeesList.push(tier3Payees.payees); - uniqueNames.push(removeDuplicates(tier3Payees.payees)); + if (tier3Payees != undefined) { + payeesList.push(tier3Payees.payees); + uniqueNames.push(removeDuplicates(tier3Payees.payees)); + } if (payeesList.length < 8) return false; const uniqueEntitiesPercentage = uniqueNames.length / payeesList.length * 100; diff --git a/typology-214/src/rules/account-dormancy.ts b/typology-214/src/rules/account-dormancy.ts index 621c28604..7100e8952 100644 --- a/typology-214/src/rules/account-dormancy.ts +++ b/typology-214/src/rules/account-dormancy.ts @@ -1,5 +1,5 @@ const handleAccountDormancy = (transfer: any, payeeHistoricalReceiveData: any, payeeHistoricalSendData: any): boolean => { - if (payeeHistoricalReceiveData.length < 1 && payeeHistoricalSendData.length < 1) return false; + if (payeeHistoricalReceiveData.length < 1 && payeeHistoricalReceiveData.length < 1) return false; const currentTransferDate = new Date(transfer.HTTPTransactionDate); const lastTransferReceivedDate = payeeHistoricalReceiveData.length >= 1 ? payeeHistoricalReceiveData.reduce((latestDate: any, transaction: any) => { if (!latestDate) { diff --git a/typology-214/src/rules/large-transaction-payer.ts b/typology-214/src/rules/large-transaction-payer.ts index e805b43b3..ddda2c8c7 100644 --- a/typology-214/src/rules/large-transaction-payer.ts +++ b/typology-214/src/rules/large-transaction-payer.ts @@ -8,8 +8,7 @@ const handleLargeTransactionPayer = ( transfer: any, historicalData: any, ): boolean => { - if (!historicalData || historicalData.length === 0) - return false; + if (historicalData == undefined || historicalData.length === 0) return false; const tranAmt = transfer.Amount; let amounts: number[] = []; diff --git a/typology-214/src/rules/newPayeeTransfer.ts b/typology-214/src/rules/newPayeeTransfer.ts index c58071a65..31aed5be7 100644 --- a/typology-214/src/rules/newPayeeTransfer.ts +++ b/typology-214/src/rules/newPayeeTransfer.ts @@ -2,6 +2,8 @@ const handleNewPayeeTransfer = ( message: any, ): boolean => { const { historicalData, transfer } = message; + if (historicalData == undefined || historicalData.length < 1) return false; + const ILPCount = historicalData .filter((transaction: any) => transfer.ILPDestinationAccountAddress !== transaction.ILPDestinationAccountAddress); return ILPCount.length <= 0; diff --git a/typology-214/src/rules/transaction-divergence.ts b/typology-214/src/rules/transaction-divergence.ts index 3ab93dfd7..9fd207f89 100644 --- a/typology-214/src/rules/transaction-divergence.ts +++ b/typology-214/src/rules/transaction-divergence.ts @@ -1,8 +1,8 @@ // move to typology 11 const handleTransactionDivergence = (transaction: any, payeeHistoricalSendData: any) => { - if (payeeHistoricalSendData.length < 10) return false; - + if (payeeHistoricalSendData == undefined || payeeHistoricalSendData.length < 10) return false; + const transactionDate = new Date(transaction.HTTPTransactionDate); // Count transaction over the last 8 hours const latestTransactions = payeeHistoricalSendData.filter((transfer: any) => { diff --git a/typology-214/src/rules/transaction-mirroring.ts b/typology-214/src/rules/transaction-mirroring.ts index 50c1a7154..26d9b2103 100644 --- a/typology-214/src/rules/transaction-mirroring.ts +++ b/typology-214/src/rules/transaction-mirroring.ts @@ -27,7 +27,8 @@ const handleTransactionMirroring = ( payeeHistoricalSendData: any, payeeHistoricalReceiveData: any, ): boolean => { - if (payeeHistoricalSendData.length < 1) return false; + if (payeeHistoricalSendData == undefined || payeeHistoricalSendData.length < 1) return false; + const { HTTPTransactionDate } = transfer; const currentTransferDate = new Date(HTTPTransactionDate); diff --git a/typology-214/src/rules/transactions-between-parties.ts b/typology-214/src/rules/transactions-between-parties.ts index 8a0123c98..cebc47f5a 100644 --- a/typology-214/src/rules/transactions-between-parties.ts +++ b/typology-214/src/rules/transactions-between-parties.ts @@ -10,6 +10,8 @@ const getPayeesFromILPsArray = async (outgoingTransfersClient: RedisClient, ILPS })) ); + if (accountsTransfers == undefined || accountsTransfers.length == 0) return undefined; + return accountsTransfers.reduce((payeesAcc: any, payee: any) => { if (payee === null) return payeesAcc; const payeeTransfers = JSON.parse(payee); @@ -33,26 +35,34 @@ const handleTransactionsBetweenParties = async ( outgoingTransfersClient: RedisClient, historicalData: any ) => { + if (historicalData == undefined || historicalData.length < 1) return false; + const payeesList = historicalData.map((transfer: any) => transfer.ILPDestinationAccountAddress); payeesList.push(transfer.ILPDestinationAccountAddress); const uniqueNames = removeDuplicates(payeesList); // get Payess names - tier 1 const tier1Payees = await getPayeesFromILPsArray(outgoingTransfersClient, uniqueNames); - payeesList.push(tier1Payees); - uniqueNames.push(removeDuplicates(tier1Payees)); + if (tier1Payees != undefined && tier1Payees.length > 0) { + payeesList.push(tier1Payees); + uniqueNames.push(removeDuplicates(tier1Payees)); + } // get Payess names - tier 2 const uniqueTier2Names = removeValuesFromSecondArray(uniqueNames, tier1Payees); const tier2Payees = await getPayeesFromILPsArray(outgoingTransfersClient, removeDuplicates(uniqueTier2Names)); - payeesList.push(tier2Payees); - uniqueNames.push(removeDuplicates(tier2Payees)); + if (tier2Payees != undefined && tier2Payees.length > 0) { + payeesList.push(tier2Payees); + uniqueNames.push(removeDuplicates(tier2Payees)); + } // get Payess names - tier 3 const uniqueTier3Names = removeValuesFromSecondArray(uniqueNames, tier2Payees); const tier3Payees = await getPayeesFromILPsArray(outgoingTransfersClient, removeDuplicates(uniqueTier3Names)); - payeesList.push(tier3Payees); - uniqueNames.push(removeDuplicates(tier3Payees)); + if (tier3Payees != undefined && tier3Payees.length > 0) { + payeesList.push(tier3Payees); + uniqueNames.push(removeDuplicates(tier3Payees)); + } if (payeesList.length < 8) return false; const uniqueEntitiesPercentage = uniqueNames.length / payeesList.length * 100; diff --git a/typology-27/src/rules/exceptionallyLargeTransfer.ts b/typology-27/src/rules/exceptionallyLargeTransfer.ts index 901f64310..746eab818 100644 --- a/typology-27/src/rules/exceptionallyLargeTransfer.ts +++ b/typology-27/src/rules/exceptionallyLargeTransfer.ts @@ -1,6 +1,8 @@ const handleExceptionallyLargeTransfer = ( message: any, ): boolean => { + if(message.historicalData == undefined || message.historicalData.length == 0) return false; + const { amount } = message.transfer.Amount; const biggestTransactionAmount = Math.max(message.historicalData .map((transaction: any) => transaction.Amount)); diff --git a/typology-27/src/rules/newPayeeTransfer.ts b/typology-27/src/rules/newPayeeTransfer.ts index c58071a65..404c0dece 100644 --- a/typology-27/src/rules/newPayeeTransfer.ts +++ b/typology-27/src/rules/newPayeeTransfer.ts @@ -2,6 +2,7 @@ const handleNewPayeeTransfer = ( message: any, ): boolean => { const { historicalData, transfer } = message; + if(historicalData == undefined || historicalData.length == 0) return false; const ILPCount = historicalData .filter((transaction: any) => transfer.ILPDestinationAccountAddress !== transaction.ILPDestinationAccountAddress); return ILPCount.length <= 0; diff --git a/typology-27/src/rules/recentSimSwap.ts b/typology-27/src/rules/recentSimSwap.ts index 3643c038f..8d1060e00 100644 --- a/typology-27/src/rules/recentSimSwap.ts +++ b/typology-27/src/rules/recentSimSwap.ts @@ -7,6 +7,7 @@ const handleSimSwap = ( const { historicalData } = message; if (historicalData == undefined + || historicalData.length == 0 || historicalData[0] == undefined) { return false; } diff --git a/typology-28/src/rules/benfordsLaw.ts b/typology-28/src/rules/benfordsLaw.ts index 3be00ea90..2354e9964 100644 --- a/typology-28/src/rules/benfordsLaw.ts +++ b/typology-28/src/rules/benfordsLaw.ts @@ -3,6 +3,7 @@ const handleBenfordsLaw = ( historicalData: any, ): boolean => { if (historicalData == undefined + || historicalData.length == 0 || historicalData[0] == undefined) { return false; } diff --git a/typology-28/src/rules/transaction-convergence.ts b/typology-28/src/rules/transaction-convergence.ts index e8449bb17..798c105fc 100644 --- a/typology-28/src/rules/transaction-convergence.ts +++ b/typology-28/src/rules/transaction-convergence.ts @@ -1,5 +1,5 @@ const handleTransactionConvergence = (transaction: any, payeeHistoricalSendData: any) => { - if (payeeHistoricalSendData.length < 10) return false; + if (payeeHistoricalSendData == undefined || payeeHistoricalSendData.length < 10) return false; const transactionDate = new Date(transaction.HTTPTransactionDate); // Count transaction over the last 8 hours diff --git a/typology-28/src/rules/transaction-divergence.ts b/typology-28/src/rules/transaction-divergence.ts index a7aa37246..a779ef9c6 100644 --- a/typology-28/src/rules/transaction-divergence.ts +++ b/typology-28/src/rules/transaction-divergence.ts @@ -1,5 +1,5 @@ const handleTransactionDivergence = (transaction: any, payeeHistoricalSendData: any) => { - if (payeeHistoricalSendData.length < 10) return false; + if (payeeHistoricalSendData == undefined || payeeHistoricalSendData.length < 10) return false; const transactionDate = new Date(transaction.HTTPTransactionDate); // Count transaction over the last 8 hours diff --git a/typology-28/src/rules/transaction-mirroring.ts b/typology-28/src/rules/transaction-mirroring.ts index 50c1a7154..397cd3544 100644 --- a/typology-28/src/rules/transaction-mirroring.ts +++ b/typology-28/src/rules/transaction-mirroring.ts @@ -27,7 +27,8 @@ const handleTransactionMirroring = ( payeeHistoricalSendData: any, payeeHistoricalReceiveData: any, ): boolean => { - if (payeeHistoricalSendData.length < 1) return false; + if (payeeHistoricalSendData == undefined || payeeHistoricalSendData.length < 1) return false; + if (payeeHistoricalReceiveData == undefined || payeeHistoricalReceiveData.length < 1) return false; const { HTTPTransactionDate } = transfer; const currentTransferDate = new Date(HTTPTransactionDate);