Skip to content

Commit

Permalink
refactor castTransferHistory function
Browse files Browse the repository at this point in the history
  • Loading branch information
gtg7784 committed Jun 12, 2024
1 parent 5de9b49 commit 65d2f6d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 63 deletions.
10 changes: 1 addition & 9 deletions src/components/assets/transfer/Information.vue
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,7 @@ import { getXvmAssetsTransferHistories } from 'src/modules/information/recent-hi
import { useStore } from 'src/store';
import { computed, defineComponent, PropType, ref, watchEffect, onUnmounted } from 'vue';
import { RecentLzHistory } from '../../../modules/information/index';
import {
getLzTxHistories,
getZkEVMTxHistories,
} from '../../../modules/information/recent-history/transfer/index';
import { getLzTxHistories } from '../../../modules/information/recent-history/transfer/index';
import { LOCAL_STORAGE } from '../../../config/localStorage';
import { endpointKey, providerEndpoints } from '../../../config/chainEndpoints';
Expand Down Expand Up @@ -161,11 +158,6 @@ export default defineComponent({
address: currentAccount.value,
network,
});
} else if (props.transferType === HistoryTxType.ZK_ETHEREUM_BRIDGE) {
txHistories.value = await getZkEVMTxHistories({
address: isH160.value ? currentAccount.value : senderSs58Account.value,
network,
});
} else {
txHistories.value = await getTxHistories({
address: isH160.value ? currentAccount.value : senderSs58Account.value,
Expand Down
64 changes: 10 additions & 54 deletions src/modules/information/recent-history/transfer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,26 +69,16 @@ export const castTransferHistory = ({
const timestamp = String(tx.timestamp);
const txType = HistoryTxType.Transfer;
const note = `To ${getShortenAddress(to)}`;
const networkIdx = localStorage.getItem(NETWORK_IDX);
const subscan = providerEndpoints[Number(networkIdx)].subscan;
const explorerUrl = `${subscan}/extrinsic/${hash}`;
return { timestamp, txType, amount, symbol, note, explorerUrl };
};

export const castZkEVMTransferHistory = ({
tx,
hash,
}: {
tx: TransferDetail;
hash: string;
}): RecentHistory => {
const { amount, symbol, to } = tx;
const timestamp = String(tx.timestamp);
const txType = HistoryTxType.Transfer;
const note = `To ${getShortenAddress(to)}`;
const networkIdx = localStorage.getItem(NETWORK_IDX);
const blockscount = providerEndpoints[Number(networkIdx)].blockscout;
const explorerUrl = `${blockscount}/tx/${hash}`;
const networkIdx = Number(localStorage.getItem(NETWORK_IDX));
let explorerUrl: string = '';

if (networkIdx === endpointKey.ASTAR_ZKEVM || networkIdx === endpointKey.ZKYOTO) {
const blockscount = providerEndpoints[networkIdx].blockscout;
explorerUrl = `${blockscount}/tx/${hash}`;
} else {
const subscan = providerEndpoints[networkIdx].subscan;
explorerUrl = `${subscan}/extrinsic/${hash}`;
}
return { timestamp, txType, amount, symbol, note, explorerUrl };
};

Expand Down Expand Up @@ -192,37 +182,3 @@ export const getLzTxHistories = async ({
);
return parsedTxs.filter((it) => it !== undefined) as RecentLzHistory[];
};

export const getZkEVMTxHistories = async ({
address,
network,
}: {
address: string;
network: string;
}): Promise<RecentHistory[]> => {
const txs: TxHistory[] = [];
const storageKey = TX_HISTORIES;

const transactions = getAccountHistories({
storageKey,
address,
network,
});
transactions.forEach((it) => txs.push(it));

const formattedTxs = txs.sort((a, b) => b.timestamp - a.timestamp).slice(0, NumberOfHistories);

const parsedTxs = await Promise.all(
formattedTxs.map(async (it) => {
const { hash } = it;
try {
const tx = await fetchTransferDetails({ hash, network });
return castZkEVMTransferHistory({ tx, hash });
} catch (error) {
console.error(error);
return undefined;
}
})
);
return parsedTxs.filter((it) => it !== undefined) as RecentHistory[];
};

0 comments on commit 65d2f6d

Please sign in to comment.