Skip to content

Commit 0142790

Browse files
committed
Add basic price-api package to fetch historical prices
This PR demonstrates what a data service class to hit the Price API would look like. It's designed to fetch historical prices to support the graph when viewing a token in the extension.
1 parent 109b22e commit 0142790

File tree

16 files changed

+634
-0
lines changed

16 files changed

+634
-0
lines changed

.github/CODEOWNERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
/packages/rate-limit-controller @MetaMask/core-platform
9494
/packages/react-data-query @MetaMask/core-platform
9595
/packages/profile-metrics-controller @MetaMask/core-platform
96+
/packages/price-api @MetaMask/core-platform
9697

9798
## Web3Auth Team
9899
/packages/seedless-onboarding-controller @MetaMask/web3auth

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ Each package in this repository has its own README where you can find installati
8080
- [`@metamask/phishing-controller`](packages/phishing-controller)
8181
- [`@metamask/polling-controller`](packages/polling-controller)
8282
- [`@metamask/preferences-controller`](packages/preferences-controller)
83+
- [`@metamask/price-api`](packages/price-api)
8384
- [`@metamask/profile-metrics-controller`](packages/profile-metrics-controller)
8485
- [`@metamask/profile-sync-controller`](packages/profile-sync-controller)
8586
- [`@metamask/ramps-controller`](packages/ramps-controller)
@@ -166,6 +167,7 @@ linkStyle default opacity:0.5
166167
phishing_controller(["@metamask/phishing-controller"]);
167168
polling_controller(["@metamask/polling-controller"]);
168169
preferences_controller(["@metamask/preferences-controller"]);
170+
price_api(["@metamask/price-api"]);
169171
profile_metrics_controller(["@metamask/profile-metrics-controller"]);
170172
profile_sync_controller(["@metamask/profile-sync-controller"]);
171173
ramps_controller(["@metamask/ramps-controller"]);
@@ -427,6 +429,9 @@ linkStyle default opacity:0.5
427429
polling_controller --> messenger;
428430
preferences_controller --> base_controller;
429431
preferences_controller --> messenger;
432+
price_api --> base_data_service;
433+
price_api --> controller_utils;
434+
price_api --> messenger;
430435
profile_metrics_controller --> accounts_controller;
431436
profile_metrics_controller --> base_controller;
432437
profile_metrics_controller --> controller_utils;

packages/price-api/CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Changelog
2+
3+
All notable changes to this project will be documented in this file.
4+
5+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7+
8+
## [Unreleased]
9+
10+
[Unreleased]: https://github.com/MetaMask/core/

packages/price-api/LICENSE

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
MIT License
2+
3+
Copyright (c) 2026 MetaMask
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE

packages/price-api/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# `@metamask/price-api`
2+
3+
Wraps the Price API.
4+
5+
## Installation
6+
7+
`yarn add @metamask/price-api`
8+
9+
or
10+
11+
`npm install @metamask/price-api`
12+
13+
## Contributing
14+
15+
This package is part of a monorepo. Instructions for contributing can be found in the [monorepo README](https://github.com/MetaMask/core#readme).

packages/price-api/jest.config.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* For a detailed explanation regarding each configuration property and type check, visit:
3+
* https://jestjs.io/docs/configuration
4+
*/
5+
6+
const merge = require('deepmerge');
7+
const path = require('path');
8+
9+
const baseConfig = require('../../jest.config.packages');
10+
11+
const displayName = path.basename(__dirname);
12+
13+
module.exports = merge(baseConfig, {
14+
// The display name when running multiple projects
15+
displayName,
16+
17+
// An object that configures minimum threshold enforcement for coverage results
18+
coverageThreshold: {
19+
global: {
20+
branches: 100,
21+
functions: 100,
22+
lines: 100,
23+
statements: 100,
24+
},
25+
},
26+
});

packages/price-api/package.json

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
{
2+
"name": "@metamask/price-api",
3+
"version": "0.0.0",
4+
"description": "Wraps the Price API",
5+
"keywords": [
6+
"MetaMask",
7+
"Ethereum"
8+
],
9+
"homepage": "https://github.com/MetaMask/core/tree/main/packages/price-api#readme",
10+
"bugs": {
11+
"url": "https://github.com/MetaMask/core/issues"
12+
},
13+
"repository": {
14+
"type": "git",
15+
"url": "https://github.com/MetaMask/core.git"
16+
},
17+
"license": "MIT",
18+
"sideEffects": false,
19+
"exports": {
20+
".": {
21+
"import": {
22+
"types": "./dist/index.d.mts",
23+
"default": "./dist/index.mjs"
24+
},
25+
"require": {
26+
"types": "./dist/index.d.cts",
27+
"default": "./dist/index.cjs"
28+
}
29+
},
30+
"./package.json": "./package.json"
31+
},
32+
"main": "./dist/index.cjs",
33+
"types": "./dist/index.d.cts",
34+
"files": [
35+
"dist/"
36+
],
37+
"scripts": {
38+
"build": "ts-bridge --project tsconfig.build.json --verbose --clean --no-references",
39+
"build:all": "ts-bridge --project tsconfig.build.json --verbose --clean",
40+
"build:docs": "typedoc",
41+
"changelog:update": "../../scripts/update-changelog.sh @metamask/price-api",
42+
"changelog:validate": "../../scripts/validate-changelog.sh @metamask/price-api",
43+
"messenger-action-types:check": "tsx ../../packages/messenger-cli/src/cli.ts --check",
44+
"messenger-action-types:generate": "tsx ../../packages/messenger-cli/src/cli.ts --generate",
45+
"since-latest-release": "../../scripts/since-latest-release.sh",
46+
"test": "NODE_OPTIONS=--experimental-vm-modules jest --reporters=jest-silent-reporter",
47+
"test:clean": "NODE_OPTIONS=--experimental-vm-modules jest --clearCache",
48+
"test:verbose": "NODE_OPTIONS=--experimental-vm-modules jest --verbose",
49+
"test:watch": "NODE_OPTIONS=--experimental-vm-modules jest --watch"
50+
},
51+
"dependencies": {
52+
"@metamask/base-data-service": "^0.1.1",
53+
"@metamask/controller-utils": "^11.20.0",
54+
"@metamask/messenger": "^1.1.1",
55+
"@metamask/superstruct": "^3.1.0",
56+
"@metamask/utils": "^11.9.0",
57+
"@tanstack/query-core": "^4.43.0"
58+
},
59+
"devDependencies": {
60+
"@metamask/auto-changelog": "^3.4.4",
61+
"@ts-bridge/cli": "^0.6.4",
62+
"@types/jest": "^29.5.14",
63+
"deepmerge": "^4.2.2",
64+
"jest": "^29.7.0",
65+
"ts-jest": "^29.2.5",
66+
"tsx": "^4.20.5",
67+
"typedoc": "^0.25.13",
68+
"typedoc-plugin-missing-exports": "^2.0.0",
69+
"typescript": "~5.3.3"
70+
},
71+
"engines": {
72+
"node": "^18.18 || >=20"
73+
},
74+
"publishConfig": {
75+
"access": "public",
76+
"registry": "https://registry.npmjs.org/"
77+
}
78+
}

packages/price-api/src/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export type {
2+
PriceApiServiceActions,
3+
PriceApiServiceCacheUpdatedEvent,
4+
PriceApiServiceEvents,
5+
PriceApiServiceGranularCacheUpdatedEvent,
6+
PriceApiServiceInvalidateQueriesAction,
7+
PriceApiServiceMessenger,
8+
} from './price-api-service/price-api-service';
9+
export type { PriceApiServiceFetchHistoricalPricesV3Action } from './price-api-service/price-api-service-method-action-types';
10+
export { PriceApiService } from './price-api-service/price-api-service';
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* This file is auto generated.
3+
* Do not edit manually.
4+
*/
5+
6+
import type { PriceApiService } from './price-api-service';
7+
8+
/**
9+
* Get historical prices by CAIP-19 asset ID (v3 endpoint).
10+
*
11+
* @param args - The arguments to this function.
12+
* @param args.params - Essential request parameters. Usually `{ chainId,
13+
* assetType }` where `chainId` is the CAIP-2 chain ID and `assetType` is the
14+
* asset type portion of CAIP-19. May also be `null` to "disable" the query.
15+
* @param args.options - Optional request parameters.
16+
* @param args.options.currency - The currency for prices.
17+
* @param args.options.timePeriod - The time period.
18+
* @param args.options.from - Start timestamp.
19+
* @param args.options.to - End timestamp.
20+
* @param args.options.interval - Data interval.
21+
* @returns The historical prices response.
22+
*/
23+
export type PriceApiServiceFetchHistoricalPricesV3Action = {
24+
type: `PriceApiService:fetchHistoricalPricesV3`;
25+
handler: PriceApiService['fetchHistoricalPricesV3'];
26+
};
27+
28+
/**
29+
* Union of all PriceApiService action types.
30+
*/
31+
export type PriceApiServiceMethodActions =
32+
PriceApiServiceFetchHistoricalPricesV3Action;

0 commit comments

Comments
 (0)