Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: broken tx history on polygon #455

Draft
wants to merge 2 commits into
base: alpha
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 62 additions & 72 deletions packages/react-app/src/hooks/TransactionHistory.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,89 +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 {
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