-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetchBalance.js
More file actions
126 lines (103 loc) · 3.55 KB
/
fetchBalance.js
File metadata and controls
126 lines (103 loc) · 3.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import { providers } from "ethers";
import { toEth } from "@colony/sdk";
import { ColonyNetwork, ColonyRpcEndpoint } from "@colony/sdk";
import dotenv from "dotenv";
import fs from "fs";
dotenv.config(); // Charger les variables d'environnement
const provider = new providers.JsonRpcProvider(ColonyRpcEndpoint.ArbitrumOne);
// Charger les adresses depuis le fichier .env
const colonyAddress = process.env.COLONY_ADDRESS
const ETH_TOKEN_ADDRESS = process.env.ETH_TOKEN_ADDRESS
const USDC_TOKEN_ADDRESS = process.env.USDC_TOKEN_ADDRESS
// Mapping des domaines
const domainNames = {
1: "General",
4: "🅿 Intuition",
3: "🅿 Eco",
5: "🅿 Jokerace",
};
const CACHE_FILE = "domainsData.json";
const CACHE_EXPIRATION = 5 * 60 * 1000; // 5 minutes
// Function to read the cache
function readCache() {
try {
if (fs.existsSync(CACHE_FILE)) {
const data = fs.readFileSync(CACHE_FILE, "utf-8");
// Handle empty file
if (!data.trim()) throw new Error("Cache file is empty");
const parsedData = JSON.parse(data);
// Ensure valid cache structure
if (!parsedData.timestamp || !Array.isArray(parsedData.funds)) {
throw new Error("Invalid cache format");
}
const now = Date.now();
if (now - parsedData.timestamp < CACHE_EXPIRATION) {
console.log("✅ Using cached data.");
return parsedData.funds;
}
}
} catch (error) {
console.error("⚠️ Error reading cache (will be refreshed):", error.message);
}
return null;
}
// Function to write to the cache
function writeCache(data) {
try {
if (!Array.isArray(data)) {
throw new Error("Invalid data format for cache");
}
fs.writeFileSync(CACHE_FILE, JSON.stringify({ timestamp: Date.now(), funds: data }, null, 2), "utf-8");
console.log("✅ Cache updated.");
} catch (error) {
console.error("⚠️ Error writing cache:", error.message);
}
}
// Fonction pour récupérer les fonds des domaines
export async function fetchDomainFunds() {
try {
if (!colonyAddress) {
throw new Error("❌ COLONY_ADDRESS is not defined in .env");
}
// Check cache first
const cachedData = readCache();
if (cachedData) return cachedData;
const colonyNetwork = new ColonyNetwork(provider);
const colony = await colonyNetwork.getColony(colonyAddress);
const domainFunds = await Promise.all(
Object.keys(domainNames).map(async (domainId) => {
domainId = Number(domainId);
try {
const balanceETHWei = await colony.getBalance(ETH_TOKEN_ADDRESS, domainId);
const balanceETH = toEth(balanceETHWei);
const balanceUSDCWei = await colony.getBalance(USDC_TOKEN_ADDRESS, domainId);
const balanceUSDC = toEth(balanceUSDCWei);
return {
domainId,
domainName: domainNames[domainId] || `Domain ${domainId}`,
funds: [
{ ticker: "ETH", amount: balanceETH },
{ ticker: "USDC", amount: balanceUSDC },
],
};
} catch (error) {
console.error(`⚠️ Error fetching funds for domain ${domainId}:`, error);
return {
domainId,
domainName: domainNames[domainId] || `Domain ${domainId}`,
funds: [
{ ticker: "ETH", amount: "0" },
{ ticker: "USDC", amount: "0" },
],
};
}
})
);
// Cache the new data
writeCache(domainFunds);
return domainFunds;
} catch (error) {
console.error("❌ Error fetching domain funds:", error);
return [];
}
}