Skip to content

Commit 98824ae

Browse files
committed
2 parents 5083c5b + 1a31a95 commit 98824ae

File tree

4 files changed

+91
-80
lines changed

4 files changed

+91
-80
lines changed

src/localServer/define.d.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,6 @@ declare type WorkerCommand =
259259
| "addMonitoredWallet"
260260
| "removeMonitoredWallet"
261261
| "getProfileAvailableCntpReward"
262-
| "claimChristmasReward" ;
263262

264263
type SINodesSortby = 'CUSTOMER_REVIEW'|'TOTAL_ONLINE_TIME'|
265264
'STORAGE_PRICE_LOW'|'STORAGE_PRICE_HIGH'|'OUTBOUND_PRICE_HIGH'|'OUTBOUND_PRICE_LOW'
@@ -362,6 +361,11 @@ interface conet_ticket {
362361
balance: string;
363362
}
364363

364+
interface historicBalance {
365+
timestamp: number
366+
balance: string
367+
}
368+
365369
interface profile extends keyPair {
366370
isPrimary?: boolean;
367371
pgpKey?: pgpKeyPair;
@@ -378,6 +382,7 @@ interface profile extends keyPair {
378382
nodeID?: number;
379383
nodeIP_address?: string;
380384
nodeRegion?: string;
385+
historicBalance?: historicBalance[]
381386
}
382387

383388
interface publicProfile {

src/localServer/workers/encrypt.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -829,14 +829,6 @@ const processCmd = async (cmd: worker_command) => {
829829
return removeMonitoredWallet(cmd);
830830
}
831831

832-
case "getProfileAvailableCntpReward": {
833-
return getProfileAvailableCntpReward(cmd);
834-
}
835-
836-
case "claimChristmasReward": {
837-
return claimChristmasReward(cmd);
838-
}
839-
840832
default: {
841833
cmd.err = "INVALID_COMMAND";
842834
responseChannel.postMessage(JSON.stringify(cmd));

src/localServer/workers/utilities/utilV2.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,3 +592,35 @@ const getRegionAllNodes = async (region: string, profile: profile) => {
592592
// curl -v -4 -x socks4://localhost:3003 "https://www.google.com"
593593
}
594594

595+
function generateDaysArrayForBalanceChart(): number[] {
596+
const currentDate = new Date(); // Current date (in local time)
597+
598+
// Get current day, month, and year in UTC
599+
const currentDay = currentDate.getUTCDate(); // Current day of the month (UTC)
600+
const currentMonth = currentDate.getUTCMonth() + 1; // Current month (1-indexed) (UTC)
601+
const currentYear = currentDate.getUTCFullYear(); // Current year (UTC)
602+
603+
// Calculate the start date (30 days before today) using UTC
604+
const startDate = new Date(
605+
Date.UTC(currentYear, currentMonth - 1, currentDay - 30)
606+
);
607+
608+
const daysArray: number[] = [];
609+
let tempDate = new Date(startDate);
610+
611+
// Generate days with 7-day intervals using UTC
612+
while (tempDate < currentDate) {
613+
// Set the time to 00:00:00 UTC (midnight) on the selected day
614+
tempDate.setUTCHours(0, 0, 0, 0); // Set to 00:00 UTC
615+
daysArray.push(tempDate.getTime()); // Push the timestamp (UTC)
616+
617+
// Add 7 days in UTC
618+
tempDate.setUTCDate(tempDate.getUTCDate() + 7);
619+
}
620+
621+
// Add today's timestamp in UTC (at 00:00 UTC)
622+
currentDate.setUTCHours(0, 0, 0, 0); // Set to 00:00 UTC
623+
daysArray.push(currentDate.getTime()); // Push today's timestamp (UTC)
624+
625+
return daysArray;
626+
}

src/localServer/workers/utilities/web3Util.ts

Lines changed: 53 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -976,7 +976,6 @@ let provideCONET
976976
let lesteningBlock = false
977977
let epoch = 0
978978
let needUpgradeVer = 0
979-
let claimedCntpInChristmas2024Event = 0
980979

981980
const listenProfileVer = async () => {
982981
epoch = await provideCONET.getBlockNumber()
@@ -999,13 +998,13 @@ const listenProfileVer = async () => {
999998

1000999
runningList.push(getBalanceOfMonitoredWallets())
10011000

1002-
runningList.push(getClaimedCntpReward())
1001+
runningList.push(getHistoricBalance())
10031002

10041003
await Promise.all(runningList)
10051004

10061005
const cmd = {
10071006
cmd: 'assets',
1008-
data: [profiles, RefereesList, leaderboardData, assetOracle, CoNET_Data?.monitoredWallets, claimedCntpInChristmas2024Event ]
1007+
data: [profiles, RefereesList, leaderboardData, assetOracle, CoNET_Data?.monitoredWallets ]
10091008
}
10101009

10111010
sendState('toFrontEnd', cmd)
@@ -1019,90 +1018,72 @@ const listenProfileVer = async () => {
10191018
selectLeaderboard(epoch)
10201019
}
10211020

1022-
function isSpecificTime() {
1023-
const targetDate = new Date("2024-12-20T00:00:00-08:00"); // PST is UTC-8
1024-
const currentDate = new Date();
1021+
const getHistoricBalance = async () => {
1022+
if (!CoNET_Data?.profiles) {
1023+
return logger(`getHistoricBalance Error! CoNET_Data.profiles empty Error!`)
1024+
}
10251025

1026-
const targetDatePST = new Date(
1027-
targetDate.toLocaleString("en-US", { timeZone: "America/Los_Angeles" })
1028-
);
1026+
const profiles = CoNET_Data.profiles
1027+
const daysInChart = generateDaysArrayForBalanceChart();
10291028

1030-
// Convert current date to PST
1031-
const currentDatePST = new Date(
1032-
currentDate.toLocaleString("en-US", { timeZone: "America/Los_Angeles" })
1033-
);
1029+
const runningList: any[] = []
10341030

1035-
return targetDatePST.getTime() <= currentDatePST.getTime();
1031+
for (let profile of profiles) {
1032+
const promise = getHistoricCntpBalance(profile, daysInChart)
1033+
runningList.push(promise)
1034+
}
1035+
1036+
await Promise.all(runningList)
10361037
}
10371038

1039+
const getHistoricCntpBalance = async (profile: profile, daysInChart: number[]) => {
1040+
const provideCONET = new ethers.JsonRpcProvider(conet_rpc);
1041+
const wallet = new ethers.Wallet(profile.privateKeyArmor, provideCONET)
1042+
const contract = new ethers.Contract(cCNTP_new_Addr, blast_CNTPAbi, wallet)
10381043

1039-
const getClaimedCntpReward = async () => {
1040-
const provider = new ethers.JsonRpcProvider(conet_rpc);
1041-
1042-
const christmas2024Contract = new ethers.Contract(
1043-
christmas2024ContractAddress,
1044-
christmas2024Abi,
1045-
provider
1046-
);
1044+
const runningList: any[] = []
10471045

1048-
try {
1049-
const claimedCntp = await christmas2024Contract.claimedCNTP()
1050-
claimedCntpInChristmas2024Event = Number(claimedCntp) / Math.pow(10, 18)
1051-
} catch (error) {
1052-
claimedCntpInChristmas2024Event = 0
1053-
}
1054-
}
1046+
for (let timestamp of daysInChart ) {
1047+
const promise = getBalanceByTimestamp(
1048+
contract,
1049+
wallet.address,
1050+
timestamp
1051+
);
10551052

1056-
const getProfileAvailableCntpReward = async (cmd) => {
1057-
if (!isSpecificTime()) return;
1053+
runningList.push(promise);
1054+
}
10581055

1059-
const walletAddress = cmd.data[0];
1056+
profile.historicBalance = [];
10601057

1061-
if (!CoNET_Data || !walletAddress) {
1062-
cmd.err = "FAILURE";
1063-
return returnUUIDChannel(cmd);
1064-
}
1058+
const balances = await Promise.all(runningList);
10651059

1066-
const provider = new ethers.JsonRpcProvider(conet_rpc);
1067-
1068-
const christmas2024Contract = new ethers.Contract(
1069-
christmas2024ContractAddress,
1070-
christmas2024Abi,
1071-
provider
1072-
);
1060+
balances.forEach((balance, index) => {
1061+
const balanceObj = {
1062+
timestamp: daysInChart[index],
1063+
balance: ethers.formatEther(balance),
1064+
};
10731065

1074-
try {
1075-
const claimableCntp = await christmas2024Contract.showClaimableCNTP(walletAddress)
1076-
cmd.data = [Number(claimableCntp)];
1077-
} catch (err) {
1078-
logger(`error getting available CNTP reward`, err);
1079-
cmd.err = "FAILURE";
1080-
}
1066+
if(!profile.historicBalance)
1067+
profile.historicBalance = [];
10811068

1082-
return returnUUIDChannel(cmd);
1069+
profile.historicBalance.push(balanceObj);
1070+
});
10831071
}
10841072

1085-
const claimChristmasReward = async (cmd: any) => {
1086-
if (!isSpecificTime()) return;
1073+
function isSpecificTime() {
1074+
const targetDate = new Date("2024-12-20T00:00:00-08:00"); // PST is UTC-8
1075+
const currentDate = new Date();
10871076

1088-
const walletAddressTmp = cmd.data[0];
1089-
const walletAddress = ethers.getAddress(walletAddressTmp);
1077+
const targetDatePST = new Date(
1078+
targetDate.toLocaleString("en-US", { timeZone: "America/Los_Angeles" })
1079+
);
10901080

1091-
try {
1092-
const url = `${apiv4_endpoint}christmas2024`;
1093-
const result: any = await postToEndpoint(url, true, { walletAddress });
1094-
1095-
if(!result){
1096-
cmd.err = "FAILURE"
1097-
}
1098-
else if (result.status === 200) {
1099-
cmd.data[0] = result;
1100-
}
1101-
} catch (err) {
1102-
cmd.err = "FAILURE"
1103-
}
1081+
// Convert current date to PST
1082+
const currentDatePST = new Date(
1083+
currentDate.toLocaleString("en-US", { timeZone: "America/Los_Angeles" })
1084+
);
11041085

1105-
return returnUUIDChannel(cmd);
1086+
return targetDatePST.getTime() <= currentDatePST.getTime();
11061087
}
11071088

11081089
const checkProfileVersion = async (wallet) => {
@@ -2398,6 +2379,7 @@ const getBalanceByTimestamp = async (erc20Contract, walletAddress: string, times
23982379
return balance
23992380
} catch (ex) {
24002381
console.log("historic balance error", ex);
2382+
return 0
24012383
}
24022384
}
24032385

@@ -2421,11 +2403,11 @@ const getBlockByTimestamp = async (
24212403
let earliestBlock = await provider.getBlock(0);
24222404

24232405
// Ensure the target timestamp is valid
2424-
if (targetTimestamp < earliestBlock.timestamp) {
2406+
if (targetTimestamp < earliestBlock.timestamp * 1000) {
24252407
throw new Error("Timestamp is earlier than the first block");
24262408
}
24272409

2428-
if (targetTimestamp > latestBlock.timestamp) {
2410+
if (targetTimestamp > latestBlock.timestamp * 1000) {
24292411
throw new Error("Timestamp is in the future");
24302412
}
24312413

0 commit comments

Comments
 (0)