Skip to content

Commit

Permalink
stop asking blockscout for lastactivity information
Browse files Browse the repository at this point in the history
  • Loading branch information
bartfish committed Dec 20, 2023
1 parent fe0ba6c commit 980ffe4
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 17 deletions.
64 changes: 64 additions & 0 deletions packages/milkomeda-wsc/src/Activity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,68 @@ export class ActivityManager {

return normalized;
}

static async getBridgeActivityV2(
network: MilkomedaNetworkName,
evmAddress: string
): Promise<Activity[]> {
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;
}
}
10 changes: 8 additions & 2 deletions packages/milkomeda-wsc/src/MilkomedaNetwork.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,14 @@ export class MilkomedaNetwork {
}
}

static async fetchBridgeRequests(network: string, totalItems = 100): Promise<BridgeRequest[]> {
const url = MilkomedaConstants.getBridgeAPIUrl(network) + `/requests?sort=Desc&count=100`;
static async fetchBridgeRequests(
network: string,
totalItems = 100,
customPath?: string
): Promise<BridgeRequest[]> {
const url =
MilkomedaConstants.getBridgeAPIUrl(network) +
`/requests?sort=Desc&count=${totalItems}${customPath ?? ""}`;
const results = await MilkomedaNetwork.bridgeFetchMultiplePages(url, totalItems);
return results;
}
Expand Down
18 changes: 3 additions & 15 deletions packages/milkomeda-wsc/src/wscLib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand Down Expand Up @@ -605,21 +605,9 @@ export class WSCLib {
// Show the latest activity of the user: L2 -> L2, L2 -> L1 and L1 -> L2.
async latestActivity(): Promise<Activity[]> {
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<string, Activity>();
grouped.forEach((activity) => {
Expand Down

0 comments on commit 980ffe4

Please sign in to comment.