Skip to content

Commit c200aa2

Browse files
committed
only support evm tokens
1 parent ac4f855 commit c200aa2

File tree

2 files changed

+122
-183
lines changed

2 files changed

+122
-183
lines changed

packages/assets-controllers/src/token-prices-service/abstract-token-prices-service.ts

Lines changed: 103 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import type { ServicePolicy } from '@metamask/controller-utils';
2-
import type { Hex } from '@metamask/utils';
2+
import type { CaipAssetType, Hex } from '@metamask/utils';
33

44
/**
55
* Represents the price of a token in a currency.
66
*/
7-
export type TokenPrice<TokenAddress extends Hex, Currency extends string> = {
8-
tokenAddress: TokenAddress;
7+
export type TokenPrice<Currency extends string> = {
8+
tokenAddress: Hex;
99
currency: Currency;
1010
allTimeHigh: number;
1111
allTimeLow: number;
@@ -41,11 +41,8 @@ export type ExchangeRate = {
4141
/**
4242
* A map of token address to its price.
4343
*/
44-
export type TokenPricesByTokenAddress<
45-
TokenAddress extends Hex,
46-
Currency extends string,
47-
> = {
48-
[A in TokenAddress]: TokenPrice<A, Currency>;
44+
export type TokenPricesByTokenAddress<Currency extends string> = {
45+
[A in Hex]: TokenPrice<Currency>;
4946
};
5047

5148
/**
@@ -55,66 +52,128 @@ export type ExchangeRatesByCurrency<Currency extends string> = {
5552
[C in Currency]: ExchangeRate;
5653
};
5754

55+
export type EvmAssetAddressWithChain<ChainId extends Hex> = {
56+
address: Hex;
57+
chainId: ChainId;
58+
};
59+
60+
export type EvmAssetWithId<ChainId extends Hex> =
61+
EvmAssetAddressWithChain<ChainId> & {
62+
assetId: CaipAssetType;
63+
};
64+
65+
export type EvmAssetWithMarketData<
66+
ChainId extends Hex,
67+
Currency extends string,
68+
> = EvmAssetWithId<ChainId> & MarketData & { currency: Currency };
69+
70+
/**
71+
* The shape of the data that the /spot-prices endpoint returns.
72+
*/
73+
export type MarketData = {
74+
/**
75+
* The all-time highest price of the token.
76+
*/
77+
allTimeHigh: number;
78+
/**
79+
* The all-time lowest price of the token.
80+
*/
81+
allTimeLow: number;
82+
/**
83+
* The number of tokens currently in circulation.
84+
*/
85+
circulatingSupply: number;
86+
/**
87+
* The market cap calculated using the diluted supply.
88+
*/
89+
dilutedMarketCap: number;
90+
/**
91+
* The highest price of the token in the last 24 hours.
92+
*/
93+
high1d: number;
94+
/**
95+
* The lowest price of the token in the last 24 hours.
96+
*/
97+
low1d: number;
98+
/**
99+
* The current market capitalization of the token.
100+
*/
101+
marketCap: number;
102+
/**
103+
* The percentage change in market capitalization over the last 24 hours.
104+
*/
105+
marketCapPercentChange1d: number;
106+
/**
107+
* The current price of the token.
108+
*/
109+
price: number;
110+
/**
111+
* The absolute change in price over the last 24 hours.
112+
*/
113+
priceChange1d: number;
114+
/**
115+
* The percentage change in price over the last 24 hours.
116+
*/
117+
pricePercentChange1d: number;
118+
/**
119+
* The percentage change in price over the last hour.
120+
*/
121+
pricePercentChange1h: number;
122+
/**
123+
* The percentage change in price over the last year.
124+
*/
125+
pricePercentChange1y: number;
126+
/**
127+
* The percentage change in price over the last 7 days.
128+
*/
129+
pricePercentChange7d: number;
130+
/**
131+
* The percentage change in price over the last 14 days.
132+
*/
133+
pricePercentChange14d: number;
134+
/**
135+
* The percentage change in price over the last 30 days.
136+
*/
137+
pricePercentChange30d: number;
138+
/**
139+
* The percentage change in price over the last 200 days.
140+
*/
141+
pricePercentChange200d: number;
142+
/**
143+
* The total trading volume of the token in the last 24 hours.
144+
*/
145+
totalVolume: number;
146+
};
147+
58148
/**
59149
* An ideal token prices service. All implementations must confirm to this
60150
* interface.
61151
*
62152
* @template ChainId - A type union of valid arguments for the `chainId`
63153
* argument to `fetchTokenPrices`.
64-
* @template TokenAddress - A type union of all token addresses. The reason this
65-
* type parameter exists is so that we can guarantee that same addresses that
66-
* `fetchTokenPrices` receives are the same addresses that shown up in the
67-
* return value.
68154
* @template Currency - A type union of valid arguments for the `currency`
69155
* argument to `fetchTokenPrices`.
70156
*/
71157
export type AbstractTokenPricesService<
72158
ChainId extends Hex = Hex,
73-
TokenAddress extends Hex = Hex,
74159
Currency extends string = string,
75160
> = Partial<Pick<ServicePolicy, 'onBreak' | 'onDegraded'>> & {
76161
/**
77162
* Retrieves prices in the given currency for the tokens identified by the
78163
* given addresses which are expected to live on the given chain.
79164
*
80165
* @param args - The arguments to this function.
81-
* @param args.chainId - An EIP-155 chain ID.
82-
* @param args.tokenAddresses - Addresses for tokens that live on the chain.
166+
* @param args.assets - The assets to get prices for.
83167
* @param args.currency - The desired currency of the token prices.
84168
* @returns The prices for the requested tokens.
85169
*/
86-
fetchTokenPrices({
87-
chainId,
88-
tokenAddresses,
170+
fetchTokenPricesV3({
171+
assets,
89172
currency,
90173
}: {
91-
chainId: ChainId;
92-
tokenAddresses: TokenAddress[];
174+
assets: EvmAssetAddressWithChain<ChainId>[];
93175
currency: Currency;
94-
}): Promise<Partial<TokenPricesByTokenAddress<TokenAddress, Currency>>>;
95-
96-
// fetchTokenPricesV3({
97-
// assets,
98-
// currency,
99-
// }: {
100-
// assets: (
101-
// | { address: Hex; chainId: Hex }
102-
// | { address: CaipAssetType; chainId: CaipChainId }
103-
// )[];
104-
// currency: SupportedCurrency;
105-
// }): (MarketData & {
106-
// assetId: CaipAssetType;
107-
// currency: SupportedCurrency;
108-
// } & (
109-
// | {
110-
// address: Hex;
111-
// chainId: Hex;
112-
// }
113-
// | {
114-
// address: CaipAssetType;
115-
// chainId: CaipChainId;
116-
// }
117-
// ))[];
176+
}): Promise<EvmAssetWithMarketData<ChainId, Currency>[]>;
118177

119178
/**
120179
* Retrieves exchange rates in the given currency.

0 commit comments

Comments
 (0)