Skip to content

Commit f105195

Browse files
joaosantos15zone-liveBigshmow
authored
chore: add imageUrl (#8448)
## Explanation Add tokenImageUrl to Position and avgHoldMinutes to TraderStats types and superstruct schemas so the extension can consume the new API fields. Also fixes fetchClosedPositions hitting a v2 URL that doesn't exist (the closed positions endpoint is v1-only), which was causing 404s. <!-- Thanks for your contribution! Take a moment to answer these questions so that reviewers have the information they need to properly understand your changes: * What is the current state of things and why does it need to change? * What is the solution your changes offer and how does it work? * Are there any changes whose purpose might not obvious to those unfamiliar with the domain? * If your primary goal was to update one package but you found you had to update another one along the way, why did you do so? * If you had to upgrade a dependency, why did you do so? --> ## References <!-- Are there any issues that this pull request is tied to? Are there other links that reviewers should consult to understand these changes better? Are there client or consumer pull requests to adopt any breaking changes? For example: * Fixes #12345 * Related to #67890 --> ## Checklist - [x] I've updated the test suite for new or updated code as appropriate - [x] I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate - [x] I've communicated my changes to consumers by [updating changelogs for packages I've changed](https://github.com/MetaMask/core/tree/main/docs/processes/updating-changelogs.md) - [ ] I've introduced [breaking changes](https://github.com/MetaMask/core/tree/main/docs/processes/breaking-changes.md) in this PR and have prepared draft pull requests for clients and consumer packages to resolve them <!-- CURSOR_SUMMARY --> --- > [!NOTE] > **Medium Risk** > Touches social API response validation/types and changes the `fetchClosedPositions` URL selection, which could affect consumers if the API payloads or endpoints differ from assumptions. Scope is limited to `@metamask/social-controllers` with updated tests covering the new fields and endpoint. > > **Overview** > Adds support for new social-api response fields by extending `Position`/`PositionStruct` with optional `tokenImageUrl` and `TraderStats`/`TraderStatsStruct` with optional `medianHoldMinutes`. > > Fixes `fetchClosedPositions` to call the **v1** closed-positions endpoint (while keeping open positions on v2) to avoid 404s, and updates tests/changelog accordingly. > > <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit b1fc8cb. Bugbot is set up for automated code reviews on this repo. Configure [here](https://www.cursor.com/dashboard/bugbot).</sup> <!-- /CURSOR_SUMMARY --> --------- Co-authored-by: Antonio Regadas <antonio.regadas@consensys.net> Co-authored-by: António Regadas <apregadas@gmail.com> Co-authored-by: Devin Stewart <49423028+Bigshmow@users.noreply.github.com>
1 parent 32d78e3 commit f105195

4 files changed

Lines changed: 17 additions & 2 deletions

File tree

packages/social-controllers/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Added
1111

12+
- Add optional `tokenImageUrl` field to `Position` type and `PositionStruct` validation schema ([#8448](https://github.com/MetaMask/core/pull/8448))
13+
- Add optional `medianHoldMinutes` field to `TraderStats` type and `TraderStatsStruct` validation schema ([#8448](https://github.com/MetaMask/core/pull/8448))
1214
- Add `intent` and optional `category` fields to `Trade` type ([#8410](https://github.com/MetaMask/core/pull/8410))
1315
- Export `TradeStruct` superstruct schema; derive `Trade` type via `Infer` ([#8410](https://github.com/MetaMask/core/pull/8410))
1416
- Narrow `direction` to `'buy' | 'sell'` and `intent` to `'enter' | 'exit'` on `Trade` type ([#8410](https://github.com/MetaMask/core/pull/8410))
@@ -19,6 +21,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1921
- Bump `@metamask/messenger` from `^1.1.0` to `^1.1.1` ([#8373](https://github.com/MetaMask/core/pull/8373))
2022
- Bump `@metamask/base-controller` from `^9.0.1` to `^9.1.0` ([#8457](https://github.com/MetaMask/core/pull/8457))
2123

24+
### Fixed
25+
26+
- Fix `fetchClosedPositions` using v2 URL instead of v1, which caused 404 errors since the closed positions endpoint only exists on v1 ([#8448](https://github.com/MetaMask/core/pull/8448))
27+
2228
## [0.1.0]
2329

2430
### Added

packages/social-controllers/src/SocialService.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ const mockPosition = {
4444
costBasis: 3000,
4545
trades: [mockTrade],
4646
lastTradeAt: 1700000000,
47+
tokenImageUrl: 'https://assets.daylight.xyz/images/token-eth.png',
4748
};
4849

4950
function createMessenger(): SocialServiceMessenger {
@@ -239,6 +240,7 @@ describe('SocialService', () => {
239240
winRate7d: 0.7,
240241
roiPercent7d: 1.2,
241242
tradeCount7d: 15,
243+
medianHoldMinutes: 120,
242244
},
243245
perChainBreakdown: {
244246
perChainPnl: { base: 30000 },
@@ -477,7 +479,7 @@ describe('SocialService', () => {
477479

478480
expect(result).toStrictEqual(mockPositionsResponse);
479481
expect(mockFetch).toHaveBeenCalledWith(
480-
`${V2_URL}/traders/0x1234/positions/closed`,
482+
`${V1_URL}/traders/0x1234/positions/closed`,
481483
);
482484
});
483485

packages/social-controllers/src/SocialService.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ const PositionStruct = structType({
6969
costBasis: number(),
7070
trades: array(TradeStruct),
7171
lastTradeAt: number(),
72+
tokenImageUrl: optional(nullable(string())),
7273
currentValueUSD: optional(nullable(number())),
7374
pnlValueUsd: optional(nullable(number())),
7475
pnlPercent: optional(nullable(number())),
@@ -119,6 +120,7 @@ const TraderStatsStruct = structType({
119120
winRate7d: optional(nullable(number())),
120121
roiPercent7d: optional(nullable(number())),
121122
tradeCount7d: optional(nullable(number())),
123+
medianHoldMinutes: optional(nullable(number())),
122124
});
123125

124126
const PerChainBreakdownStruct = structType({
@@ -546,8 +548,9 @@ export class SocialService extends BaseDataService<
546548
page ?? null,
547549
],
548550
queryFn: async () => {
551+
const baseUrl = status === 'open' ? this.#v2Url : this.#v1Url;
549552
const url = new URL(
550-
`${this.#v2Url}/traders/${encodeURIComponent(addressOrId)}/positions/${status}`,
553+
`${baseUrl}/traders/${encodeURIComponent(addressOrId)}/positions/${status}`,
551554
);
552555
if (chain) {
553556
url.searchParams.append('chain', chain);

packages/social-controllers/src/social-types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ export type TraderStats = {
105105
winRate7d?: number | null;
106106
roiPercent7d?: number | null;
107107
tradeCount7d?: number | null;
108+
/** Median holding time in minutes. */
109+
medianHoldMinutes?: number | null;
108110
};
109111

110112
export type PerChainBreakdown = {
@@ -142,6 +144,8 @@ export type Position = {
142144
costBasis: number;
143145
trades: Trade[];
144146
lastTradeAt: number;
147+
/** Daylight-hosted token image URL. */
148+
tokenImageUrl?: string | null;
145149
/** Current USD value of the remaining position (open positions only). */
146150
currentValueUSD?: number | null;
147151
/** Unrealized + realized PnL in USD. */

0 commit comments

Comments
 (0)