Skip to content
Open
Changes from 1 commit
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
121 changes: 85 additions & 36 deletions src/lib/tier.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { retryWithDelay } from "@/utils/retry.utils";
import { customAoInstance, ourAoInstance } from "./aoconnect";
import { redis } from "./redis";
import { isArweaveAddress } from "@/utils/address.utils";

type TierWallet = {
balance: string;
Expand Down Expand Up @@ -78,6 +79,8 @@ const Tiers = [
},
];

const TIER_PROCESS_ID = "rkAezEIgacJZ_dVuZHOKJR8WKpSDqLGfgPJrs_Es7CA";

function getTierThresholds(totalWallets: number) {
const tierThresholds = [];

Expand Down Expand Up @@ -135,53 +138,99 @@ function getWalletTier(walletRank: number, totalWallets: number): number {
}

async function getWalletsTierInfoFromAo() {
const { wallets, snapshotTimestamp } =
await retryWithDelay<WalletsTierInfoFromAo>(async (attempt) => {
const instance = attempt % 2 === 0 ? customAoInstance : ourAoInstance;
try {
// TODO: Update to actual process ID
const response = await fetch(
`http://forward.computer/[email protected]/now/wallets-tier-info/[email protected]/serialize/?bundle`
);
if (!response.ok) {
throw new Error("Failed to fetch wallets tier info from HB");
}
const data = (await response.json()) as Record<string, TierWallet>;

const result = await instance.dryrun({
process: "rkAezEIgacJZ_dVuZHOKJR8WKpSDqLGfgPJrs_Es7CA",
tags: [{ name: "Action", value: "Get-Wallets" }],
});
let firstWallet: TierWallet | null = null;
const walletsTierInfo: Record<string, TierWallet> = {};

const data = result?.Messages?.[0]?.Data;
if (!data) {
throw new Error("No data returned from AO");
for (const [addr, wallet] of Object.entries(data)) {
if (isArweaveAddress(addr)) {
if (!firstWallet) {
firstWallet = wallet;
}
walletsTierInfo[addr] = wallet;
}
}

const parsedData = JSON.parse(data);
if (!parsedData?.wallets || !parsedData?.snapshotTimestamp) {
throw new Error("Invalid response from AO");
}
if (!firstWallet) {
throw new Error("No valid wallet data found");
}

return parsedData;
});
const snapshotTimestamp = firstWallet.snapshotTimestamp;
const totalWallets = firstWallet.totalHolders;
const actualTotalWallets = Object.keys(walletsTierInfo).length;

const totalWallets = wallets.length;
if (!snapshotTimestamp || !totalWallets) {
throw new Error("No valid wallet data found");
}

const walletsTierInfo = wallets.reduce(
(acc: Record<string, TierWallet>, wallet, index) => {
const walletRank = index + 1;
const tier = getWalletTier(walletRank, totalWallets);
const progress = calculateTierProgressPercent(walletRank, totalWallets);
if (actualTotalWallets !== totalWallets) {
throw new Error("Total wallets mismatch");
}

const walletData = {
balance: wallet.balance,
rank: walletRank,
tier,
progress,
snapshotTimestamp,
totalHolders: totalWallets,
};
return {
walletsTierInfo,
snapshotTimestamp,
totalWallets,
};
} catch (error) {
console.error("Fallback to dryrun due to HB fetch error: ", error);
const { wallets, snapshotTimestamp } =
await retryWithDelay<WalletsTierInfoFromAo>(async (attempt) => {
const instance = attempt % 2 === 0 ? customAoInstance : ourAoInstance;

const result = await instance.dryrun({
process: TIER_PROCESS_ID,
tags: [{ name: "Action", value: "Get-Wallets" }],
});

const data = result?.Messages?.[0]?.Data;
if (!data) {
throw new Error("No data returned from AO");
}

const parsedData = JSON.parse(data);
if (!parsedData?.wallets || !parsedData?.snapshotTimestamp) {
throw new Error("Invalid response from AO");
}

return parsedData;
});

acc[wallet.address] = walletData;
const totalWallets = wallets.length;

return acc;
},
{}
);
const walletsTierInfo = wallets.reduce(
(acc: Record<string, TierWallet>, wallet, index) => {
const walletRank = index + 1;
const tier = getWalletTier(walletRank, totalWallets);
const progress = calculateTierProgressPercent(walletRank, totalWallets);

return { walletsTierInfo, snapshotTimestamp, totalWallets };
const walletData = {
balance: wallet.balance,
rank: walletRank,
tier,
progress,
snapshotTimestamp,
totalHolders: totalWallets,
};

acc[wallet.address] = walletData;

return acc;
},
{}
);

return { walletsTierInfo, snapshotTimestamp, totalWallets };
}
}

export async function getWalletsTierInfo(addresses: string[]) {
Expand Down