Skip to content
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "defillama-extension",
"private": true,
"version": "0.0.8.2",
"version": "0.0.8.3",
"type": "module",
"description": "DefiLlama Extension",
"displayName": "DefiLlama",
Expand Down
55 changes: 41 additions & 14 deletions src/pages/background/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
DEFILLAMA_DIRECTORY_API,
PROTOCOL_TVL_THRESHOLD,
} from "../libs/constants";
import { getStorage } from "../libs/helpers";
import { getStorage, setStorage } from "../libs/helpers";
import { checkDomain } from "../libs/phishing-detector";

startupTasks();
Expand All @@ -28,7 +28,7 @@ async function getCurrentTab() {
async function handlePhishingCheck() {
const phishingDetector = await getStorage("local", "settings:phishingDetector", true);
if (!phishingDetector) {
await Browser.action.setIcon({ path: cute });
Browser.action.setIcon({ path: cute });
return;
}

Expand Down Expand Up @@ -91,6 +91,17 @@ async function handlePhishingCheck() {
}

export async function updateProtocolsDb() {
const lastUpdate = await getStorage("local", "lastUpdate:protocols", 0);
const now = Date.now();
const diff = now - lastUpdate;
if (diff < 4 * 60 * 60 * 1000) {
console.log("updateProtocolsDb", `last update was less than ${Math.ceil(diff / 1000 / 60)} minutes ago, skipping`);
return;
}

console.log("updateProtocolsDb", "start");
await setStorage("local", "lastUpdate:protocols", now);

const raw = await fetch(PROTOCOLS_API).then((res) => res.json());
const protocols = (raw["protocols"]?.map((x: any) => ({
name: x.name,
Expand All @@ -110,7 +121,17 @@ export async function updateProtocolsDb() {
}

export async function updateDomainDbs() {
const lastUpdate = await getStorage("local", "lastUpdate:domains", 0);
const now = Date.now();
const diff = now - lastUpdate;
if (diff < 4 * 60 * 60 * 1000) {
console.log("updateDomainDbs", `last update was less than ${Math.ceil(diff / 1000 / 60)} minutes ago, skipping`);
return;
}

console.log("updateDomainDbs", "start");
await setStorage("local", "lastUpdate:domains", now);

const rawProtocols = await fetch(PROTOCOLS_API).then((res) => res.json());
const protocols = (
(rawProtocols["protocols"]?.map((x: any) => ({
Expand Down Expand Up @@ -183,40 +204,46 @@ export async function updateDomainDbs() {
Browser.tabs.onUpdated.addListener(async (tabId, onUpdatedInfo, tab) => {
// console.log("onUpdated", onUpdatedInfo.status, onUpdatedInfo.url);
if (onUpdatedInfo.status === "complete" && tab.active) {
await Browser.tabs.sendMessage(tabId, { message: "TabUpdated" });
Browser.tabs.sendMessage(tabId, { message: "TabUpdated" });
}
await handlePhishingCheck();
});

// monitor tab activations, when the user switches to a different tab that was already open but not active
Browser.tabs.onActivated.addListener(async (onActivatedInfo) => {
// console.log("onActivated");
await Browser.tabs.sendMessage(onActivatedInfo.tabId, { message: "TabActivated" });
Browser.tabs.sendMessage(onActivatedInfo.tabId, { message: "TabActivated" });
await handlePhishingCheck();
});

async function setupUpdateProtocolsDb() {
console.log("setupUpdateProtocolsDb");
await Browser.alarms.clear("updateProtocolsDb");

console.log("setupUpdateProtocolsDb", "create");
await updateProtocolsDb();
Browser.alarms.create("updateProtocolsDb", { periodInMinutes: 4 * 60 }); // update once every 4 hours
Browser.alarms.get("updateProtocolsDb").then((a) => {
if (!a) {
console.log("setupUpdateProtocolsDb", "create");
updateProtocolsDb();
Browser.alarms.create("updateProtocolsDb", { periodInMinutes: 4 * 60 }); // update once every 4 hours
}
});
}

async function setupUpdateDomainDbs() {
console.log("setupUpdateDomainDbs");
await Browser.alarms.clear("updateDomainDbs");

console.log("setupUpdateDomainDbs", "create");
await updateDomainDbs();
Browser.alarms.create("updateDomainDbs", { periodInMinutes: 4 * 60 }); // update once every 4 hours
Browser.alarms.get("updateDomainDbs").then((a) => {
if (!a) {
console.log("setupUpdateDomainDbs", "create");
updateDomainDbs();
Browser.alarms.create("updateDomainDbs", { periodInMinutes: 4 * 60 }); // update once every 4 hours
}
});
}

function startupTasks() {
console.log("startupTasks", "start");
setupUpdateProtocolsDb();
setupUpdateDomainDbs();
updateProtocolsDb();
updateDomainDbs();
Browser.action.setIcon({ path: cute });
console.log("startupTasks", "done");
}
Expand Down