-
Notifications
You must be signed in to change notification settings - Fork 415
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3681 from osmosis-labs/stage
Publish Stage
- Loading branch information
Showing
57 changed files
with
2,119 additions
and
663 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 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 +1,2 @@ | ||
export * from "./allocation"; | ||
export * from "./portfolio"; |
49 changes: 49 additions & 0 deletions
49
packages/server/src/queries/complex/portfolio/portfolio.ts
This file contains 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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import cachified, { CacheEntry } from "cachified"; | ||
import { LRUCache } from "lru-cache"; | ||
|
||
import { queryPortfolioOverTime } from "../../../queries/data-services"; | ||
import { DEFAULT_LRU_OPTIONS } from "../../../utils/cache"; | ||
|
||
export type Range = "1d" | "7d" | "1mo" | "1y" | "all"; | ||
|
||
const transactionsCache = new LRUCache<string, CacheEntry>(DEFAULT_LRU_OPTIONS); | ||
|
||
export interface ChartPortfolioOverTimeResponse { | ||
time: number; | ||
value: number; | ||
} | ||
|
||
export async function getPortfolioOverTime({ | ||
address, | ||
range, | ||
}: { | ||
address: string; | ||
range: Range; | ||
}): Promise<ChartPortfolioOverTimeResponse[]> { | ||
return await cachified({ | ||
cache: transactionsCache, | ||
ttl: 1000 * 60, // 60 seconds | ||
key: `portfolio-over-time-${address}-range-${range}`, | ||
getFreshValue: async () => { | ||
const data = await queryPortfolioOverTime({ | ||
address, | ||
range, | ||
}); | ||
|
||
// sort data by timestamp in ascending order for chart | ||
const sortedData = data?.sort( | ||
(a, b) => | ||
new Date(a.timestamp).getTime() - new Date(b.timestamp).getTime() | ||
); | ||
|
||
// map data to time and value for chart | ||
const mappedData = sortedData.map((d) => ({ | ||
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#get_the_number_of_seconds_since_the_ecmascript_epoch | ||
time: Math.floor(new Date(d.timestamp).getTime() / 1000), // convert to seconds | ||
value: d.usd, | ||
})); | ||
|
||
return mappedData; | ||
}, | ||
}); | ||
} |
This file contains 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
27 changes: 27 additions & 0 deletions
27
packages/server/src/queries/data-services/portfolio-over-time.ts
This file contains 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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { apiClient } from "@osmosis-labs/utils"; | ||
|
||
import { NUMIA_BASE_URL } from "../../env"; | ||
|
||
export interface PortfolioOverTimeResponse { | ||
timestamp: string; | ||
usd: number; | ||
} | ||
|
||
export async function queryPortfolioOverTime({ | ||
address, | ||
range, | ||
}: { | ||
address: string; | ||
range: string; | ||
}): Promise<PortfolioOverTimeResponse[]> { | ||
const url = new URL("/users/portfolio/over_time", NUMIA_BASE_URL); | ||
|
||
url.searchParams.append("address", address); | ||
url.searchParams.append("range", range); | ||
|
||
const headers = { | ||
Authorization: `Bearer ${process.env.NUMIA_API_KEY}`, | ||
}; | ||
|
||
return apiClient<PortfolioOverTimeResponse[]>(url.toString(), { headers }); | ||
} |
This file contains 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 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 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.