Skip to content

[WIP] refactor: zerion api uses cache-first-datasource#2967

Draft
LucieFaire wants to merge 3 commits intomainfrom
refactor/wa-1651-zerion-api-datasource-get
Draft

[WIP] refactor: zerion api uses cache-first-datasource#2967
LucieFaire wants to merge 3 commits intomainfrom
refactor/wa-1651-zerion-api-datasource-get

Conversation

@LucieFaire
Copy link
Copy Markdown
Contributor

Summary

Refactor network calls in Zerion API services to use CacheFirstDataSource.get() instead of direct network calls with manual caching, aligning with the established pattern used throughout the codebase.

WA-1651

Changes

  • ZerionWalletPortfolioApi.getPortfolio() refactored to use dataSource.get()
  • ZerionBalancesApi.getBalances() refactored to use dataSource.get()
  • ZerionBalancesApi.getCollectibles() refactored to use dataSource.get()
  • ZerionBalancesApi._fetchChainMappingForNetwork() refactored to use dataSource.get()
  • Rate limiting preserved in ZerionBalancesApi
  • All existing tests pass
  • No change in external API behavior
  • Manual cache logging removed (handled by CacheFirstDataSource)

Signed-off-by: dsh <11198975+LucieFaire@users.noreply.github.com>
@LucieFaire LucieFaire requested a review from a team as a code owner March 6, 2026 14:49
@LucieFaire
Copy link
Copy Markdown
Contributor Author

@claude review

@LucieFaire LucieFaire marked this pull request as draft March 6, 2026 15:14
})
.then(({ data }) => ZerionBalancesSchema.parse(data));
await this.cacheService.hSet(
const data = await this.dataSource.get<ZerionBalances>({
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@PooyaRaki
The new path parses cache data as ZerionBalancesSchema ([{ data: [...] }], but the previous implementation wrote only the array ZerionBalanceSchema ([zerionBalances.data]) under the same zerion_balances_<address> cache key/field. So with the new implementation on cache hits it will deserialize to an array and throw ZodError until expiry, causing transient getBalances failures for already-cached wallets.
How critical it is? We can force clean cache for balances, or send a hook event to trigger it. Otherwise we need a fallback here to be able to work with both, but as cache is not long lived i wonder if we should invest in it. WDYT?

@LucieFaire LucieFaire changed the title refactor: zerion api uses cache-first-datasource [WIP] refactor: zerion api uses cache-first-datasource Mar 6, 2026
@LucieFaire LucieFaire added the wip Initial review, work in progress label Mar 6, 2026
@LucieFaire LucieFaire force-pushed the refactor/wa-1651-zerion-api-datasource-get branch from da0b57e to ab9313f Compare March 6, 2026 16:29
);
}

await this._checkRateLimit();
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to extract it above the get call, which now means the cache hits are also counted towards the RL. I can either do a cache pre-check here as it used to be (do double cache lookup), or i can extend datasource.get method to have a .onMiss callback function we can pass. The only thing its not a pattern anywhere else in code.
Not sure if I should just go back to how it was or implement additional stuff
cc @PooyaRaki

Signed-off-by: dsh <11198975+LucieFaire@users.noreply.github.com>
@LucieFaire LucieFaire force-pushed the refactor/wa-1651-zerion-api-datasource-get branch from ab9313f to df0c902 Compare March 6, 2026 16:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

wip Initial review, work in progress

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant