|
| 1 | +import { BlockchainType, Contract, Fee, Interaction, Volume, Wallet } from '../types.ts'; |
| 2 | +import { getTransactions } from './transaction.ts'; |
| 3 | +import { Transaction } from './types.ts'; |
| 4 | +import { getTokens } from './token.ts'; |
| 5 | +import { assignTransfers, assignTransfersValue } from './transfer.ts'; |
| 6 | +import axios from 'axios'; |
| 7 | + |
| 8 | +const getInteraction = (address: string, transactions: Transaction[]): Interaction => { |
| 9 | + const interaction: Interaction = { |
| 10 | + change: 0, |
| 11 | + total: 0, |
| 12 | + }; |
| 13 | + |
| 14 | + for (const transaction of transactions) { |
| 15 | + const transactionDate = new Date(transaction.timeStamp * 1000); |
| 16 | + if (transaction.from.toLowerCase() === address.toLowerCase()) { |
| 17 | + interaction.total++; |
| 18 | + if (transactionDate > new Date(Date.now() - 1000 * 60 * 60 * 24 * 7)) { |
| 19 | + interaction.change++; |
| 20 | + } |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + return interaction; |
| 25 | +}; |
| 26 | + |
| 27 | +const getVolume = async (transactions: Transaction[], ethPrice: number): Promise<Volume> => { |
| 28 | + const volume: Volume = { |
| 29 | + change: 0, |
| 30 | + total: 0, |
| 31 | + }; |
| 32 | + |
| 33 | + for (const transaction of transactions) { |
| 34 | + const value = Number(transaction.value); |
| 35 | + volume.total += value * 10 ** -18 * ethPrice; |
| 36 | + if (transaction.transfers.length) volume.total += transaction.transfers[0].transferPrice; |
| 37 | + const transactionDate = new Date(transaction.timeStamp * 1000); |
| 38 | + if (transactionDate > new Date(Date.now() - 1000 * 60 * 60 * 24 * 7)) { |
| 39 | + volume.change += value; |
| 40 | + if (transaction.transfers.length) volume.total += transaction.transfers[0].transferPrice; |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + return volume; |
| 45 | +}; |
| 46 | + |
| 47 | +const getFee = async (address: string, transactions: Transaction[], ethPrice: number): Promise<Fee> => { |
| 48 | + const fee: Fee = { |
| 49 | + change: 0, |
| 50 | + total: 0, |
| 51 | + }; |
| 52 | + |
| 53 | + for (const transaction of transactions) { |
| 54 | + if (!transaction.gasPrice || !transaction.gasUsed || transaction.from.toLowerCase() !== address.toLowerCase()) |
| 55 | + continue; |
| 56 | + fee.total += transaction.gasPrice * transaction.gasUsed; |
| 57 | + |
| 58 | + const transactionDate = new Date(transaction.timeStamp * 1000); |
| 59 | + if (transactionDate > new Date(Date.now() - 1000 * 60 * 60 * 24 * 7)) { |
| 60 | + fee.change += transaction.gasPrice * transaction.gasUsed; |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + fee.total *= 10 ** -18 * ethPrice; |
| 65 | + fee.change *= 10 ** -18 * ethPrice; |
| 66 | + |
| 67 | + return fee; |
| 68 | +}; |
| 69 | + |
| 70 | +const getContract = (transactions: Transaction[]): Contract => { |
| 71 | + const interactedContracts: Set<string> = new Set(); |
| 72 | + const interactedContractsChange: Set<string> = new Set(); |
| 73 | + |
| 74 | + for (const transaction of transactions) { |
| 75 | + if (interactedContracts.has(transaction.to)) continue; |
| 76 | + interactedContracts.add(transaction.to); |
| 77 | + if (new Date(transaction.timeStamp).getTime() >= new Date().getTime() - 86400 * 7 * 1000) { |
| 78 | + interactedContractsChange.add(transaction.to); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + return { |
| 83 | + change: interactedContractsChange.size, |
| 84 | + total: interactedContracts.size, |
| 85 | + }; |
| 86 | +}; |
| 87 | + |
| 88 | +const getWallet = async (address: string, blockchain: BlockchainType): Promise<Wallet> => { |
| 89 | + const transactions: Transaction[] = await getTransactions(address, blockchain); |
| 90 | + const ethPrice = |
| 91 | + Number((await axios.get('https://api.etherscan.io/api?module=stats&action=ethprice')).data.result.ethusd) || 1900; |
| 92 | + |
| 93 | + await assignTransfers(transactions, address, blockchain); |
| 94 | + await assignTransfersValue(transactions, ethPrice); |
| 95 | + |
| 96 | + return { |
| 97 | + address, |
| 98 | + interaction: getInteraction(address, transactions), |
| 99 | + volume: await getVolume(transactions, ethPrice), |
| 100 | + fee: await getFee(address, transactions, ethPrice), |
| 101 | + contract: getContract(transactions), |
| 102 | + tokens: await getTokens(address, blockchain), |
| 103 | + additionalInfos: [], |
| 104 | + protocols: [], |
| 105 | + }; |
| 106 | +}; |
| 107 | + |
| 108 | +export { getWallet }; |
0 commit comments