-
Notifications
You must be signed in to change notification settings - Fork 29
Feature / Trending tokens #2505
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
sonytooo
wants to merge
10
commits into
v2
Choose a base branch
from
feature/trending-tokens
base: v2
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 2 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
b83717a
impl trending tokens
sonytooo 0ee2c23
remove redundnat comments
sonytooo 52fdd1d
fix: conflicts
sonytooo a75a77b
fix: conflicts
sonytooo 99fe2e6
fixes
sonytooo 1ef0747
revert temp url
sonytooo 60427b8
refacotr(trending): remove duplicate properties
JIOjosBG 6f9abfa
fix(trending): update test url
JIOjosBG a6c96bf
refactor(trending): image field
JIOjosBG 984637b
Merge pull request #2529 from AmbireTech/feature/trending-tokens-trim…
sonytooo 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
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
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 |
|---|---|---|
|
|
@@ -17,6 +17,10 @@ import { | |
| featuredDapps, | ||
| predefinedDapps | ||
| } from '../../consts/dapps/dapps' | ||
| import { | ||
| TRENDING_TOKENS_FAILED_UPDATE_INTERVAL, | ||
| TRENDING_TOKENS_UPDATE_INTERVAL | ||
| } from '../../consts/intervals' | ||
| import { | ||
| ConnectionSource, | ||
| Dapp, | ||
|
|
@@ -27,7 +31,9 @@ import { | |
| GetCurrentDappRes, | ||
| HasUnverifiedDappsRes, | ||
| IDappsController, | ||
| RecentDappEntry | ||
| RawTrendingToken, | ||
| RecentDappEntry, | ||
| TrendingToken | ||
| } from '../../interfaces/dapp' | ||
| import { IEventEmitterRegistryController } from '../../interfaces/eventEmitter' | ||
| import { Fetch } from '../../interfaces/fetch' | ||
|
|
@@ -45,13 +51,16 @@ import { | |
| getDomainFromUrl, | ||
| modifyDappPropsIfNeeded, | ||
| normalizeDappConnection, | ||
| normalizeTrendingTokens, | ||
| sortDapps, | ||
| unifyDefiLlamaDappUrl | ||
| } from '../../libs/dapps/helpers' | ||
| import { networkChainIdToHex } from '../../libs/networks/networks' | ||
| import { fetchWithTimeout } from '../../utils/fetch' | ||
| import EventEmitter from '../eventEmitter/eventEmitter' | ||
|
|
||
| const TRENDING_TOKENS_URL = 'https://cena.ambire.com/api/v3/trending/' | ||
|
|
||
| const mergeSource = ( | ||
| existing: ConnectionSource[] | undefined, | ||
| source: ConnectionSource | ||
|
|
@@ -102,6 +111,22 @@ export class DappsController extends EventEmitter implements IDappsController { | |
|
|
||
| #selectedAccount: ISelectedAccountController | ||
|
|
||
| #trendingTokens: TrendingToken[] = [] | ||
|
|
||
| #trendingTokensUpdatedAt: number | null = null | ||
|
|
||
| #updateTrendingTokensInterval: IRecurringTimeout | ||
|
|
||
| #continuouslyUpdateTrendingTokensPromise?: Promise<void> | ||
|
|
||
| get trendingTokens(): TrendingToken[] { | ||
| return this.#trendingTokens | ||
| } | ||
|
|
||
| get updateTrendingTokensInterval() { | ||
| return this.#updateTrendingTokensInterval | ||
| } | ||
|
|
||
| get shouldRetryFetchAndUpdate() { | ||
| return this.#shouldRetryFetchAndUpdate | ||
| } | ||
|
|
@@ -158,6 +183,13 @@ export class DappsController extends EventEmitter implements IDappsController { | |
| 5 * 60 * 1000 // 5min. | ||
| ) | ||
|
|
||
| // Trending tokens are refreshed server-side every 10 minutes; poll at the same cadence. | ||
| this.#updateTrendingTokensInterval = new RecurringTimeout( | ||
| () => this.continuouslyUpdateTrendingTokens(), | ||
| TRENDING_TOKENS_UPDATE_INTERVAL, | ||
| this.emitError.bind(this) | ||
| ) | ||
|
Comment on lines
+186
to
+191
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we move it to continuous updates? |
||
|
|
||
| this.#ui.uiEvent.on('addView', () => { | ||
| if (this.#retryFetchAndUpdateInterval.running) this.#retryFetchAndUpdateInterval.stop() | ||
| }) | ||
|
|
@@ -247,14 +279,19 @@ export class DappsController extends EventEmitter implements IDappsController { | |
| await this.#networks.initialLoadPromise | ||
| await this.#selectedAccount.initialLoadPromise | ||
|
|
||
| const [storedDapps, storedRecentDapps] = await Promise.all([ | ||
| const [storedDapps, storedRecentDapps, storedTrending] = await Promise.all([ | ||
| this.#storage.get('dappsV2', predefinedDapps), | ||
| this.#storage.get('recentDapps', [] as RecentDappEntry[]) | ||
| this.#storage.get('recentDapps', [] as RecentDappEntry[]), | ||
| this.#storage.get('trending', { updatedAt: 0, tokens: [] as TrendingToken[] }) | ||
| ]) | ||
| // Normalize on read so a drifted record (e.g. isConnected: true but connectedSources: []) | ||
| // can't show a dapp as connected in the UI while permission checks force a reconnect. | ||
| this.#dapps = new Map(storedDapps.map((d) => [d.id, normalizeDappConnection(d)])) | ||
| this.#recentDapps = storedRecentDapps | ||
| this.#trendingTokens = storedTrending.tokens | ||
| this.#trendingTokensUpdatedAt = storedTrending.updatedAt || null | ||
|
|
||
| this.#updateTrendingTokensInterval.start({ runImmediately: true }) | ||
|
|
||
| void this.fetchAndUpdateDapps() | ||
| } | ||
|
|
@@ -509,6 +546,72 @@ export class DappsController extends EventEmitter implements IDappsController { | |
| void this.#storage.set('dappsV2', Array.from(this.#dapps.values())) | ||
| } | ||
|
|
||
| /** | ||
| * Wrapper around #continuouslyUpdateTrendingTokens that: | ||
| * 1) deduplicates concurrent triggers via a shared promise | ||
| * 2) switches to the failed-retry interval when the fetch/update flow throws | ||
| */ | ||
| async continuouslyUpdateTrendingTokens() { | ||
| if (this.#continuouslyUpdateTrendingTokensPromise) { | ||
| await this.#continuouslyUpdateTrendingTokensPromise | ||
|
|
||
| return | ||
| } | ||
|
|
||
| this.#continuouslyUpdateTrendingTokensPromise = this.#continuouslyUpdateTrendingTokens() | ||
| .catch((err) => { | ||
| this.#updateTrendingTokensInterval.updateTimeout({ | ||
| timeout: TRENDING_TOKENS_FAILED_UPDATE_INTERVAL | ||
| }) | ||
| throw err | ||
| }) | ||
|
Comment on lines
+563
to
+569
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we rethrow the error here and not emit an error? |
||
| .finally(() => { | ||
| this.#continuouslyUpdateTrendingTokensPromise = undefined | ||
| }) | ||
|
|
||
| await this.#continuouslyUpdateTrendingTokensPromise | ||
| } | ||
|
|
||
| async #continuouslyUpdateTrendingTokens() { | ||
| // Skip if the last successful update is still fresh — prevents redundant requests when the | ||
| // background reloads multiple times within a short period (e.g. service worker wake-ups). | ||
| const timeSinceLastUpdate = this.#trendingTokensUpdatedAt | ||
| ? Date.now() - this.#trendingTokensUpdatedAt | ||
| : null | ||
| if ( | ||
| this.#trendingTokensUpdatedAt && | ||
| timeSinceLastUpdate !== null && | ||
| timeSinceLastUpdate < this.#updateTrendingTokensInterval.currentTimeout | ||
| ) { | ||
| return | ||
| } | ||
|
|
||
| const res = await fetchWithTimeout(this.#fetch, TRENDING_TOKENS_URL, {}, 30000) | ||
|
|
||
| if (!res.ok || res.status !== 200) { | ||
| throw new Error(`Failed to update trending tokens (status: ${res.status}, url: ${res.url})`) | ||
| } | ||
|
|
||
| const raw: RawTrendingToken[] = await res.json() | ||
| if (!Array.isArray(raw)) { | ||
| throw new Error('Trending tokens response is not an array') | ||
| } | ||
|
|
||
| this.#trendingTokens = normalizeTrendingTokens(raw) | ||
| const updatedAt = Date.now() | ||
| this.#trendingTokensUpdatedAt = updatedAt | ||
| this.emitUpdate() | ||
|
|
||
| await this.#storage.set('trending', { updatedAt, tokens: this.#trendingTokens }) | ||
|
|
||
| // Recover the normal cadence after a previously failed fetch bumped it down. | ||
| if ( | ||
| this.#updateTrendingTokensInterval.currentTimeout === TRENDING_TOKENS_FAILED_UPDATE_INTERVAL | ||
| ) { | ||
| this.#updateTrendingTokensInterval.updateTimeout({ timeout: TRENDING_TOKENS_UPDATE_INTERVAL }) | ||
| } | ||
| } | ||
|
|
||
| async #createDappSession(initProps: SessionInitProps) { | ||
| await this.initialLoadPromise | ||
| const dappSession = new Session(initProps) | ||
|
|
@@ -1325,9 +1428,11 @@ export class DappsController extends EventEmitter implements IDappsController { | |
| recentDapps: this.recentDapps, | ||
| categories: this.categories, | ||
| isReady: this.isReady, | ||
| trendingTokens: this.trendingTokens, | ||
| shouldRetryFetchAndUpdate: this.shouldRetryFetchAndUpdate, | ||
| retryFetchAndUpdateInterval: this.retryFetchAndUpdateInterval, | ||
| retryFetchAndUpdateAttempts: this.retryFetchAndUpdateAttempts | ||
| retryFetchAndUpdateAttempts: this.retryFetchAndUpdateAttempts, | ||
| updateTrendingTokensInterval: this.updateTrendingTokensInterval | ||
| } | ||
| } | ||
| } | ||
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't this way too often? There are 30 tokens, yes, but we are updating the tokens regardless of whether the extension is active or not. Imo, we shoud run the interval while the extension is active and simply update on popup open if the last update is more than X minutes ago. If the interval runs less often then it will be okay to run it always (e.g., every 4+ hours)