diff --git a/packages/milkomeda-wsc/src/Activity.ts b/packages/milkomeda-wsc/src/Activity.ts index cd07cf7..119a9d3 100644 --- a/packages/milkomeda-wsc/src/Activity.ts +++ b/packages/milkomeda-wsc/src/Activity.ts @@ -77,4 +77,68 @@ export class ActivityManager { return normalized; } + + static async getBridgeActivityV2( + network: MilkomedaNetworkName, + evmAddress: string + ): Promise { + const bridgeRequests = await MilkomedaNetwork.fetchBridgeRequests( + network, + 20, + `&evm_address=${evmAddress}` + ); + // leaving the rest as is, to make sure we return the same format to previous function + const userToL2 = bridgeRequests.filter( + (request) => request.to.toLowerCase() === evmAddress.toLowerCase() + ); + const userToL1 = bridgeRequests.filter( + (request) => request.from.toLowerCase() === evmAddress.toLowerCase() + ); + + const normalized = [...userToL1, ...userToL2].map((request) => { + const isCompleted = request.executed_timestamp; + const isFailed = request.invalidated; + + let status: ActivityStatus; + if (isCompleted) { + status = ActivityStatus.Completed; + } else if (isFailed) { + status = ActivityStatus.Failed; + } else { + status = ActivityStatus.Pending; + } + const txType = request.to === evmAddress ? PendingTxType.Wrap : PendingTxType.Unwrap; + + let values: { amount: string; decimals: number; token: string }[] = []; + if (txType !== PendingTxType.Unwrap) { + values = request.assets.map((asset) => { + return { + amount: asset.asset_value, + decimals: asset.mainchain_decimals, + token: asset.symbol !== null ? asset.symbol : "ADA", + }; + }); + } else { + values = request.assets.map((asset) => { + return { + amount: asset.asset_value, + decimals: asset.sidechain_decimals, + token: asset.symbol !== null ? asset.symbol : "ADA", + }; + }); + } + + return { + hash: request.transaction_id, + timestamp: request.timestamp, + explorer: MilkomedaConstants.getEVMExplorerUrl(network) + "/tx/" + request.transaction_id, + type: txType, + destinationAddress: request.to, + status, + values, + }; + }); + + return normalized; + } } diff --git a/packages/milkomeda-wsc/src/MilkomedaNetwork.ts b/packages/milkomeda-wsc/src/MilkomedaNetwork.ts index dac108e..5bbc90a 100644 --- a/packages/milkomeda-wsc/src/MilkomedaNetwork.ts +++ b/packages/milkomeda-wsc/src/MilkomedaNetwork.ts @@ -75,8 +75,14 @@ export class MilkomedaNetwork { } } - static async fetchBridgeRequests(network: string, totalItems = 100): Promise { - const url = MilkomedaConstants.getBridgeAPIUrl(network) + `/requests?sort=Desc&count=100`; + static async fetchBridgeRequests( + network: string, + totalItems = 100, + customPath?: string + ): Promise { + const url = + MilkomedaConstants.getBridgeAPIUrl(network) + + `/requests?sort=Desc&count=${totalItems}${customPath ?? ""}`; const results = await MilkomedaNetwork.bridgeFetchMultiplePages(url, totalItems); return results; } diff --git a/packages/milkomeda-wsc/src/wscLib.ts b/packages/milkomeda-wsc/src/wscLib.ts index aa804ab..735fae4 100644 --- a/packages/milkomeda-wsc/src/wscLib.ts +++ b/packages/milkomeda-wsc/src/wscLib.ts @@ -4,7 +4,7 @@ import BigNumber from "bignumber.js"; import { ethers } from "ethers"; import { Blockfrost, Lucid, WalletApi } from "lucid-cardano"; import type { MilkomedaProvider } from "milkomeda-wsc-provider"; -import { Activity, ActivityManager, ActivityStatus } from "./Activity"; +import { Activity, ActivityManager } from "./Activity"; import { AlgoPendingManager } from "./AlgoPendingManager"; import BridgeActions from "./BridgeActions"; import CardanoPendingManager, { @@ -605,21 +605,9 @@ export class WSCLib { // Show the latest activity of the user: L2 -> L2, L2 -> L1 and L1 -> L2. async latestActivity(): Promise { const targetAddress = await this.eth_getAccount(); - const bridgeActivity = await ActivityManager.getBridgeActivity(this.network, targetAddress); - const l2Activity = await this.getL2TransactionList(); - const l2normalized: Activity[] = l2Activity.map((transaction) => { - return { - hash: transaction.hash, - timestamp: parseInt(transaction.timeStamp), - explorer: MilkomedaConstants.getEVMExplorerUrl(this.network) + "/tx/" + transaction.hash, - type: PendingTxType.Normal, - destinationAddress: transaction.to, - status: ActivityStatus.Completed, - values: [], - }; - }); + const bridgeActivity = await ActivityManager.getBridgeActivityV2(this.network, targetAddress); - const grouped = [...bridgeActivity, ...l2normalized]; + const grouped = [...bridgeActivity]; // Remove duplicates based on hash and prioritize non-normal types const uniqueActivities = new Map(); grouped.forEach((activity) => {