Skip to content

Commit

Permalink
refacto useTransactionHistory
Browse files Browse the repository at this point in the history
  • Loading branch information
doliG committed Aug 9, 2022
1 parent 3d52fe5 commit 987c6e6
Showing 1 changed file with 62 additions and 75 deletions.
137 changes: 62 additions & 75 deletions packages/react-app/src/hooks/TransactionHistory.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,92 +10,79 @@ export default function useTransactionHistory(vaultName, action, pollTime = 4000
const contracts = useContractLoader();
const { address, networkName, networkId } = useAuth();

useEffect(() => {
setTransactionHistories(undefined);
}, [vaultName, action]);
useEffect(() => setTransactionHistories(undefined), [vaultName, action]);

usePoller(
async () => {
if (contracts && vaultName && address) {
const vaultContract = contracts[vaultName];
if (vaultContract) {
const histories = [];

const events = [];

try {
if (action === TRANSACTION_ACTIONS.ALL || action === TRANSACTION_ACTIONS.LIQUIDATION) {
let liquidatorEvents;
if (networkId === CHAIN_IDS.FANTOM || CHAIN_NAME === CHAIN_NAMES.FANTOM) {
const filterLiquidator = contracts.FliquidatorFTM.filters.Liquidate(address);
liquidatorEvents = await contracts.FliquidatorFTM.queryFilter(filterLiquidator);
} else if (networkId === CHAIN_IDS.POLYGON || CHAIN_NAME === CHAIN_NAMES.POLYGON) {
const filterLiquidator = contracts.F2FliquidatorMATIC.filters.Liquidate(address);
liquidatorEvents = await contracts.F2FliquidatorMATIC.queryFilter(filterLiquidator);
} else {
const filterLiquidator = contracts.Fliquidator.filters.Liquidate(address);
liquidatorEvents = await contracts.Fliquidator.queryFilter(filterLiquidator);
}

events.push(...liquidatorEvents);
}

if (action === TRANSACTION_ACTIONS.ALL || action === TRANSACTION_ACTIONS.BORROW) {
const filterBorrows = vaultContract.filters.Borrow(address);
const borrowEvents = await vaultContract.queryFilter(filterBorrows);
events.push(...borrowEvents);
}

if (action === TRANSACTION_ACTIONS.ALL || action === TRANSACTION_ACTIONS.DEPOSIT) {
const filterDeposit = vaultContract.filters.Deposit(address);
const depositEvents = await vaultContract.queryFilter(filterDeposit);
events.push(...depositEvents);
}

if (action === TRANSACTION_ACTIONS.ALL || action === TRANSACTION_ACTIONS.WITHDRAW) {
const filterWithdraw = vaultContract.filters.Withdraw(address);
const withdrawEvents = await vaultContract.queryFilter(filterWithdraw);
events.push(...withdrawEvents);
}

if (action === TRANSACTION_ACTIONS.ALL || action === TRANSACTION_ACTIONS.PAYBACK) {
const filterPayback = vaultContract.filters.Payback(address);
const paybackEvents = await vaultContract.queryFilter(filterPayback);
events.push(...paybackEvents);
}

for (let j = 0; j < events.length; j += 1) {
const history = {};

const evt = events[j];
const block = await evt.getBlock(evt.blockNumber);

history.Date = new Date(block.timestamp * 1000);
history.Action = evt.event;
if (!contracts || !vaultName || !address) {
return;
}

history.Asset = Object.keys(ASSETS[networkName]).find(
key =>
ASSETS[networkName][key].address.toLowerCase() === evt.args.asset.toLowerCase(),
);
const vaultContract = contracts[vaultName];
if (!vaultContract) {
return;
}

if (history.Asset) {
history.Amount = Number(
formatUnits(evt.args.amount, ASSETS[networkName][history.Asset].decimals),
);
}
const events = [];
try {
if (action === TRANSACTION_ACTIONS.ALL || action === TRANSACTION_ACTIONS.LIQUIDATION) {
let liquidatorEvents;
if (networkId === CHAIN_IDS.FANTOM || CHAIN_NAME === CHAIN_NAMES.FANTOM) {
const filterLiquidator = contracts.FliquidatorFTM.filters.Liquidate(address);
liquidatorEvents = await contracts.FliquidatorFTM.queryFilter(filterLiquidator);
} else if (networkId === CHAIN_IDS.POLYGON || CHAIN_NAME === CHAIN_NAMES.POLYGON) {
const filterLiquidator = contracts.F2FliquidatorMATIC.filters.Liquidate(address);
liquidatorEvents = await contracts.F2FliquidatorMATIC.queryFilter(filterLiquidator);
} else {
const filterLiquidator = contracts.Fliquidator.filters.Liquidate(address);
liquidatorEvents = await contracts.Fliquidator.queryFilter(filterLiquidator);
}
events.push(...liquidatorEvents);
}

history.txHash = evt.transactionHash;
const vaultEvents = [
TRANSACTION_ACTIONS.BORROW,
TRANSACTION_ACTIONS.DEPOSIT,
TRANSACTION_ACTIONS.WITHDRAW,
TRANSACTION_ACTIONS.PAYBACK,
];
const toRetrieve =
action === TRANSACTION_ACTIONS.ALL
? vaultEvents
: vaultEvents.filter(eventName => eventName === action);

for (const eventName of toRetrieve) {
const filtered = vaultContract.filters[eventName](address);
const events = await vaultContract.queryFilter(filtered);
events.push(...events);
}

histories.push(history);
}
} catch (error) {
console.error('useTransactionHistory error:', { error });
const histories = [];
for (let j = 0; j < events.length; j += 1) {
const history = {};
const evt = events[j];
const block = await evt.getBlock(evt.blockNumber);

history.Date = new Date(block.timestamp * 1000);
history.Action = evt.event;
history.txHash = evt.transactionHash;
history.Asset = Object.keys(ASSETS[networkName]).find(
key => ASSETS[networkName][key].address.toLowerCase() === evt.args.asset.toLowerCase(),
);
if (history.Asset) {
history.Amount = Number(
formatUnits(evt.args.amount, ASSETS[networkName][history.Asset].decimals),
);
}

histories.sort((x, y) => y.Date.valueOf() - x.Date.valueOf());
setTransactionHistories(histories);
histories.push(history);
}
} catch (error) {
console.error('useTransactionHistory error:', { error });
}

histories.sort((x, y) => y.Date.valueOf() - x.Date.valueOf());
setTransactionHistories(histories);
},
pollTime,
vaultName,
Expand Down

0 comments on commit 987c6e6

Please sign in to comment.