Skip to content

Commit

Permalink
feat: add new rates controller for non-EVM chains (#4242)
Browse files Browse the repository at this point in the history
As part of the non-EVM initiative, there's a need to develop a new controller capable of fetching the rates for these blockchains. Currently, we can't rely on the `CurrencyRatesController` since it's dependent on the activity of the `NetworkController`, which is exclusively EVM oriented.

Co-authored-by: gantunesr <gantunesr@users.noreply.github.com>
Co-authored-by: Elliot Winkler <elliot.winkler@gmail.com>
Co-authored-by: Charly Chevalier <charly.chevalier@consensys.net>
  • Loading branch information
4 people authored May 22, 2024
1 parent 3adbaf1 commit 09b569b
Show file tree
Hide file tree
Showing 12 changed files with 985 additions and 88 deletions.
29 changes: 29 additions & 0 deletions packages/assets-controllers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,41 @@ This package features the following controllers:
- [**CollectibleDetectionController**](src/CollectibleDetectionController.ts) keeps a periodically updated list of ERC-721 tokens assigned to the currently selected address.
- [**CollectiblesController**](src/CollectiblesController.ts) tracks ERC-721 and ERC-1155 tokens assigned to the currently selected address, using OpenSea to retrieve token information.
- [**CurrencyRateController**](src/CurrencyRateController.ts) keeps a periodically updated value of the exchange rate from the currently selected "native" currency to another (handling testnet tokens specially).
- [**RatesController**](src/RatesController/RatesController.ts) keeps a periodically updated value for the exchange rates for different cryptocurrencies. The difference between the `RatesController` and `CurrencyRateController` is that the second one is coupled to the `NetworksController` and is EVM specific, whilst the first one can handle different blockchain currencies like BTC and SOL.
- [**TokenBalancesController**](src/TokenBalancesController.ts) keeps a periodically updated set of balances for the current set of ERC-20 tokens.
- [**TokenDetectionController**](src/TokenDetectionController.ts) keeps a periodically updated list of ERC-20 tokens assigned to the currently selected address.
- [**TokenListController**](src/TokenListController.ts) uses the MetaSwap API to keep a periodically updated list of known ERC-20 tokens along with their metadata.
- [**TokenRatesController**](src/TokenRatesController.ts) keeps a periodically updated list of exchange rates for known ERC-20 tokens relative to the currently selected native currency.
- [**TokensController**](src/TokensController.ts) stores the ERC-20 and ERC-721 tokens, along with their metadata, that are listed in the wallet under the currently selected address on the currently selected chain.

### `RatesController`

The `RatesController` is responsible for managing the state related to cryptocurrency exchange rates and periodically updating these rates by fetching new data from an external API.

```ts
// Initialize the RatesController
const ratesController = new RatesController({
interval: 180000,
includeUsdRate: true,
state: {
fiatCurrency: 'eur',
cryptocurrencies: [Cryptocurrency.Btc],
},
});

// Start the polling process
ratesController.start().then(() => {
console.log('Polling for exchange rates has started.');
});

// Stop the polling process after some time
setTimeout(() => {
ratesController.stop().then(() => {
console.log('Polling for exchange rates has stopped.');
});
}, 300000);
```

## Contributing

This package is part of a monorepo. Instructions for contributing can be found in the [monorepo README](https://github.com/MetaMask/core#readme).
2 changes: 1 addition & 1 deletion packages/assets-controllers/src/CurrencyRateController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import type {
import { StaticIntervalPollingController } from '@metamask/polling-controller';
import { Mutex } from 'async-mutex';

import { fetchExchangeRate as defaultFetchExchangeRate } from './crypto-compare';
import { fetchExchangeRate as defaultFetchExchangeRate } from './crypto-compare-service';

/**
* @type CurrencyRateState
Expand Down
Loading

0 comments on commit 09b569b

Please sign in to comment.