Skip to content

Commit

Permalink
feat: add Solana to the polled exchange rates (#4914)
Browse files Browse the repository at this point in the history
## Explanation

As we aim to support more chains in the MetaMask extension we will need
the exchange rates of the native tokens of these chains to properly
value a user's account. Let's add Solana to the cryptocurrencies that we
need to keep track of price.

## References

Closes
[SOL-36](https://consensyssoftware.atlassian.net/jira/software/projects/SOL/boards/718/backlog?selectedIssue=SOL-36)

## Changelog

### `@metamask/assets-controller`

- feat: add Solana to the polled exchange rates

## 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 highlighted breaking changes using the "BREAKING" category
above as appropriate
- [x] I've prepared draft pull requests for clients and consumer
packages to resolve any breaking changes


[SOL-36]:
https://consensyssoftware.atlassian.net/browse/SOL-36?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ
  • Loading branch information
ulissesferreira authored Nov 13, 2024
1 parent 05bd4c7 commit fe985d2
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,14 @@ describe('RatesController', () => {
const { fiatCurrency, rates, cryptocurrencies } = ratesController.state;
expect(ratesController).toBeDefined();
expect(fiatCurrency).toBe('usd');
expect(Object.keys(rates)).toStrictEqual([Cryptocurrency.Btc]);
expect(cryptocurrencies).toStrictEqual([Cryptocurrency.Btc]);
expect(Object.keys(rates)).toStrictEqual([
Cryptocurrency.Btc,
Cryptocurrency.Solana,
]);
expect(cryptocurrencies).toStrictEqual([
Cryptocurrency.Btc,
Cryptocurrency.Solana,
]);
});
});

Expand All @@ -119,11 +125,16 @@ describe('RatesController', () => {
const publishActionSpy = jest.spyOn(messenger, 'publish');

jest.spyOn(global.Date, 'now').mockImplementation(() => getStubbedDate());
const mockRateValue = 57715.42;
const mockBtcRateValue = 57715.42;
const mockSolRateValue = 200.48;

const fetchExchangeRateStub = jest.fn(() => {
return Promise.resolve({
btc: {
eur: mockRateValue,
eur: mockBtcRateValue,
},
sol: {
eur: mockSolRateValue,
},
});
});
Expand All @@ -144,6 +155,10 @@ describe('RatesController', () => {
conversionDate: 0,
conversionRate: 0,
},
sol: {
conversionDate: 0,
conversionRate: 0,
},
});

await ratesController.start();
Expand All @@ -163,7 +178,11 @@ describe('RatesController', () => {
expect(ratesPosUpdate).toStrictEqual({
btc: {
conversionDate: MOCK_TIMESTAMP,
conversionRate: mockRateValue,
conversionRate: mockBtcRateValue,
},
sol: {
conversionDate: MOCK_TIMESTAMP,
conversionRate: mockSolRateValue,
},
});

Expand Down Expand Up @@ -326,7 +345,10 @@ describe('RatesController', () => {

const cryptocurrencyListPreUpdate =
ratesController.getCryptocurrencyList();
expect(cryptocurrencyListPreUpdate).toStrictEqual([Cryptocurrency.Btc]);
expect(cryptocurrencyListPreUpdate).toStrictEqual([
Cryptocurrency.Btc,
Cryptocurrency.Solana,
]);
// Just to make sure we're updating to something else than the default list
expect(cryptocurrencyListPreUpdate).not.toStrictEqual(
mockCryptocurrencyList,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,15 @@ import type {

export const name = 'RatesController';

/**
* Supported cryptocurrencies that can be used as a base currency. The value needs to be compatible
* with CryptoCompare's API which is the default source for the rates.
*
* See: https://min-api.cryptocompare.com/documentation?key=Price&cat=multipleSymbolsPriceEndpoint
*/
export enum Cryptocurrency {
Btc = 'btc',
Solana = 'sol',
}

const DEFAULT_INTERVAL = 180000;
Expand All @@ -31,8 +38,12 @@ const defaultState = {
conversionDate: 0,
conversionRate: 0,
},
[Cryptocurrency.Solana]: {
conversionDate: 0,
conversionRate: 0,
},
},
cryptocurrencies: [Cryptocurrency.Btc],
cryptocurrencies: [Cryptocurrency.Btc, Cryptocurrency.Solana],
};

export class RatesController extends BaseController<
Expand Down

0 comments on commit fe985d2

Please sign in to comment.