-
Notifications
You must be signed in to change notification settings - Fork 0
feat: hb integration #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
pawanpaudel93
wants to merge
5
commits into
main
Choose a base branch
from
feat/hb-integration
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7e386a2
feat: use hb node in for tier
pawanpaudel93 14a9053
fix: update TIER_PROCESS_ID and add snapshot age validation for walle…
pawanpaudel93 fe7206c
fix: update TIER_PROCESS_ID to new production value
pawanpaudel93 ab3caac
refactor: introduce TierWalletBase interface and transform wallet dat…
pawanpaudel93 f4402b5
refactor: remove customAoInstance
pawanpaudel93 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
|
|
@@ -78,6 +79,8 @@ const Tiers = [ | |
| }, | ||
| ]; | ||
|
|
||
| const TIER_PROCESS_ID = "rkAezEIgacJZ_dVuZHOKJR8WKpSDqLGfgPJrs_Es7CA"; | ||
|
|
||
| function getTierThresholds(totalWallets: number) { | ||
| const tierThresholds = []; | ||
|
|
||
|
|
@@ -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[]) { | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.