cake_wallet/lib/core/fiat_conversion_service.dart
Adegoke David b3d579c24a
CW-527-Add-Polygon-MATIC-Wallet (#1179)
* chore: Initial setup for polygon package

* feat: Add polygon node urls

* feat: Add Polygon(MATIC) wallet WIP

* feat: Add Polygon(MATIC) wallet WIP

* feat: Add Polygon MATIC wallet [skip ci]

* fix: Issue with create/restore wallet for polygon

* feat: Add erc20 tokens for polygon

* feat: Adding Polygon MATIC Wallet

* fix: Add build command for polygon to workflow file to fix failing action

* fix: Switch evm to not display additional balance

* chore: Sync with remote

* fix: Revert change to inject app script

* feat: Add polygon erc20 tokens

* feat: Increase migration version

* fix: Restore from QR address validator fix

* fix: Adjust wallet connect connection flow to adapt to wallet type

* fix: Make wallet fetch nfts based on the current wallet type

* fix: Make wallet fetch nfts based on the current wallet type

* fix: Try fetching transactions with moralis

* fix: Requested review changes

* fix: Error creating new wallet

* fix: Revert script

* fix: Exclude spam NFTs from nft listing API response

* Update default_erc20_tokens.dart

* replace matic with matic poly

* Add polygon wallet scheme to app links

* style: reformat default_settings_migration.dart 

* minor enhancement 

* fix using different wallet function for setting the transaction priorities 

* fix: Add chain to calls

* Add USDC.e to initial coins

* Add other default polygon node 

* Use Polygon scan
some UI fixes

* Add polygon scan api key to secrets generation code

---------

Co-authored-by: Omar Hatem <omarh.ismail1@gmail.com>
2023-12-02 04:26:43 +02:00

67 lines
1.9 KiB
Dart

import 'package:cw_core/crypto_currency.dart';
import 'package:cake_wallet/entities/fiat_currency.dart';
import 'dart:convert';
import 'package:flutter/foundation.dart';
import 'package:http/http.dart';
import 'package:cake_wallet/.secrets.g.dart' as secrets;
const _fiatApiClearNetAuthority = 'fiat-api.cakewallet.com';
const _fiatApiOnionAuthority = 'n4z7bdcmwk2oyddxvzaap3x2peqcplh3pzdy7tpkk5ejz5n4mhfvoxqd.onion';
const _fiatApiPath = '/v2/rates';
Future<double> _fetchPrice(Map<String, dynamic> args) async {
final crypto = args['crypto'] as String;
final fiat = args['fiat'] as String;
final torOnly = args['torOnly'] as bool;
final Map<String, String> queryParams = {
'interval_count': '1',
'base': crypto.split(".").first,
'quote': fiat,
'key': secrets.fiatApiKey,
};
num price = 0.0;
try {
late final Uri uri;
if (torOnly) {
uri = Uri.http(_fiatApiOnionAuthority, _fiatApiPath, queryParams);
} else {
uri = Uri.https(_fiatApiClearNetAuthority, _fiatApiPath, queryParams);
}
final response = await get(uri);
if (response.statusCode != 200) {
return 0.0;
}
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
final results = responseJSON['results'] as Map<String, dynamic>;
if (results.isNotEmpty) {
price = results.values.first as num;
}
return price.toDouble();
} catch (e) {
return price.toDouble();
}
}
Future<double> _fetchPriceAsync(CryptoCurrency crypto, FiatCurrency fiat, bool torOnly) async =>
compute(_fetchPrice, {
'fiat': fiat.toString(),
'crypto': crypto.toString(),
'torOnly': torOnly,
});
class FiatConversionService {
static Future<double> fetchPrice({
required CryptoCurrency crypto,
required FiatCurrency fiat,
required bool torOnly,
}) async =>
await _fetchPriceAsync(crypto, fiat, torOnly);
}