Skip to content

Commit

Permalink
Merge pull request #21 from archethic-foundation/20-add-btc-in-fetch-…
Browse files Browse the repository at this point in the history
…price-method

feat: ✨ Add Bitcoin in ucids_tokens config file
  • Loading branch information
redDwarf03 authored Aug 14, 2024
2 parents 3c55228 + 8c6e8d3 commit fa305ba
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 31 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
Changelog
=========

#### Version 1.2.13
* Add Bitcoin in ucids_tokens config file
*
#### Version 1.2.12
* Update dependencies

Expand Down
33 changes: 15 additions & 18 deletions lib/src/application/coin_price.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:async';
import 'dart:convert';
import 'package:archethic_dapp_framework_flutter/src/application/ucids_tokens.dart';
import 'package:archethic_dapp_framework_flutter/src/domain/models/crypto_price.dart';
import 'package:archethic_dapp_framework_flutter/src/domain/models/ucid.dart';
import 'package:http/http.dart' as http;
import 'package:riverpod_annotation/riverpod_annotation.dart';

Expand Down Expand Up @@ -30,15 +31,8 @@ class _CoinPriceNotifier extends Notifier<CryptoPrice> {
}

Future<CryptoPrice?> fetchPrices() async {
// UCIDs
// 3890 : MATIC
// 1027 : Ethereum
// 1839 : BNB
// 3408 : USDC
// 20920 : Monerium EURe

const url =
'https://fas.archethic.net/api/v1/quotes/latest?ucids=1027,3890,1839,3408,20920';
'https://fas.archethic.net/api/v1/quotes/latest?ucids=1027,3890,1839,3408,20920,1';
final headers = {
'Content-type': 'application/json',
'Accept': 'application/json',
Expand Down Expand Up @@ -69,11 +63,12 @@ class _CoinPriceNotifier extends Notifier<CryptoPrice> {
Map<String, double> _extractPriceMethods(String responseBody) {
final jsonData = json.decode(responseBody) as Map<String, dynamic>;
return {
'matic': jsonData['3890'],
'ethereum': jsonData['1027'],
'bnb': jsonData['1839'],
'usdc': jsonData['3408'],
'eure': jsonData['20920'],
'bitcoin': jsonData[UCID.bitcoin.toString()],
'matic': jsonData[UCID.matic.toString()],
'ethereum': jsonData[UCID.ethereum.toString()],
'bnb': jsonData[UCID.bnb.toString()],
'usdc': jsonData[UCID.usdc.toString()],
'eure': jsonData[UCID.eure.toString()],
};
}
}
Expand All @@ -91,15 +86,17 @@ double _coinPriceFromAddress(
final ucid = ucidsList[address.toUpperCase()] ?? 0;
if (ucid != 0) {
switch (ucid) {
case 1027:
case UCID.bitcoin:
return coinPrice.bitcoin;
case UCID.ethereum:
return coinPrice.ethereum;
case 1839:
case UCID.bnb:
return coinPrice.bnb;
case 3890:
case UCID.matic:
return coinPrice.matic;
case 3408:
case UCID.usdc:
return coinPrice.usdc;
case 20920:
case UCID.eure:
return coinPrice.eure;
default:
return 0;
Expand Down
4 changes: 2 additions & 2 deletions lib/src/application/coin_price.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions lib/src/domain/models/crypto_price.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ part 'crypto_price.g.dart';
class CryptoPrice with _$CryptoPrice {
factory CryptoPrice({
int? timestamp,
// Used UCIDs
@Default(0.0) double matic, // 3890
@Default(0.0) double ethereum, // 1027
@Default(0.0) double bnb, // 1839
@Default(0.0) double usdc, // 3408
@Default(0.0) double eure, // 20920
@Default(0.0) double bitcoin,
@Default(0.0) double matic,
@Default(0.0) double ethereum,
@Default(0.0) double bnb,
@Default(0.0) double usdc,
@Default(0.0) double eure,
}) = _CryptoPrice;

factory CryptoPrice.fromJson(Map<String, dynamic> json) =>
Expand Down
28 changes: 25 additions & 3 deletions lib/src/domain/models/crypto_price.freezed.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ CryptoPrice _$CryptoPriceFromJson(Map<String, dynamic> json) {
/// @nodoc
mixin _$CryptoPrice {
int? get timestamp => throw _privateConstructorUsedError; // Used UCIDs
double get bitcoin => throw _privateConstructorUsedError; // 1
double get matic => throw _privateConstructorUsedError; // 3890
double get ethereum => throw _privateConstructorUsedError; // 1027
double get bnb => throw _privateConstructorUsedError; // 1839
Expand All @@ -41,6 +42,7 @@ abstract class $CryptoPriceCopyWith<$Res> {
@useResult
$Res call(
{int? timestamp,
double bitcoin,
double matic,
double ethereum,
double bnb,
Expand All @@ -62,6 +64,7 @@ class _$CryptoPriceCopyWithImpl<$Res, $Val extends CryptoPrice>
@override
$Res call({
Object? timestamp = freezed,
Object? bitcoin = null,
Object? matic = null,
Object? ethereum = null,
Object? bnb = null,
Expand All @@ -73,6 +76,10 @@ class _$CryptoPriceCopyWithImpl<$Res, $Val extends CryptoPrice>
? _value.timestamp
: timestamp // ignore: cast_nullable_to_non_nullable
as int?,
bitcoin: null == bitcoin
? _value.bitcoin
: bitcoin // ignore: cast_nullable_to_non_nullable
as double,
matic: null == matic
? _value.matic
: matic // ignore: cast_nullable_to_non_nullable
Expand Down Expand Up @@ -107,6 +114,7 @@ abstract class _$$CryptoPriceImplCopyWith<$Res>
@useResult
$Res call(
{int? timestamp,
double bitcoin,
double matic,
double ethereum,
double bnb,
Expand All @@ -126,6 +134,7 @@ class __$$CryptoPriceImplCopyWithImpl<$Res>
@override
$Res call({
Object? timestamp = freezed,
Object? bitcoin = null,
Object? matic = null,
Object? ethereum = null,
Object? bnb = null,
Expand All @@ -137,6 +146,10 @@ class __$$CryptoPriceImplCopyWithImpl<$Res>
? _value.timestamp
: timestamp // ignore: cast_nullable_to_non_nullable
as int?,
bitcoin: null == bitcoin
? _value.bitcoin
: bitcoin // ignore: cast_nullable_to_non_nullable
as double,
matic: null == matic
? _value.matic
: matic // ignore: cast_nullable_to_non_nullable
Expand Down Expand Up @@ -166,6 +179,7 @@ class __$$CryptoPriceImplCopyWithImpl<$Res>
class _$CryptoPriceImpl implements _CryptoPrice {
_$CryptoPriceImpl(
{this.timestamp,
this.bitcoin = 0.0,
this.matic = 0.0,
this.ethereum = 0.0,
this.bnb = 0.0,
Expand All @@ -178,6 +192,10 @@ class _$CryptoPriceImpl implements _CryptoPrice {
@override
final int? timestamp;
// Used UCIDs
@override
@JsonKey()
final double bitcoin;
// 1
@override
@JsonKey()
final double matic;
Expand All @@ -200,7 +218,7 @@ class _$CryptoPriceImpl implements _CryptoPrice {

@override
String toString() {
return 'CryptoPrice(timestamp: $timestamp, matic: $matic, ethereum: $ethereum, bnb: $bnb, usdc: $usdc, eure: $eure)';
return 'CryptoPrice(timestamp: $timestamp, bitcoin: $bitcoin, matic: $matic, ethereum: $ethereum, bnb: $bnb, usdc: $usdc, eure: $eure)';
}

@override
Expand All @@ -210,6 +228,7 @@ class _$CryptoPriceImpl implements _CryptoPrice {
other is _$CryptoPriceImpl &&
(identical(other.timestamp, timestamp) ||
other.timestamp == timestamp) &&
(identical(other.bitcoin, bitcoin) || other.bitcoin == bitcoin) &&
(identical(other.matic, matic) || other.matic == matic) &&
(identical(other.ethereum, ethereum) ||
other.ethereum == ethereum) &&
Expand All @@ -220,8 +239,8 @@ class _$CryptoPriceImpl implements _CryptoPrice {

@JsonKey(ignore: true)
@override
int get hashCode =>
Object.hash(runtimeType, timestamp, matic, ethereum, bnb, usdc, eure);
int get hashCode => Object.hash(
runtimeType, timestamp, bitcoin, matic, ethereum, bnb, usdc, eure);

@JsonKey(ignore: true)
@override
Expand All @@ -240,6 +259,7 @@ class _$CryptoPriceImpl implements _CryptoPrice {
abstract class _CryptoPrice implements CryptoPrice {
factory _CryptoPrice(
{final int? timestamp,
final double bitcoin,
final double matic,
final double ethereum,
final double bnb,
Expand All @@ -252,6 +272,8 @@ abstract class _CryptoPrice implements CryptoPrice {
@override
int? get timestamp;
@override // Used UCIDs
double get bitcoin;
@override // 1
double get matic;
@override // 3890
double get ethereum;
Expand Down
2 changes: 2 additions & 0 deletions lib/src/domain/models/crypto_price.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions lib/src/domain/models/ucid.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
abstract class UCID {
static const bitcoin = 1;
static const matic = 3890;
static const ethereum = 1027;
static const bnb = 1839;
static const usdc = 3408;
static const eure = 20920;
}
3 changes: 2 additions & 1 deletion lib/src/domain/repositories/tokens/ucids_tokens.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"mainnet": {
"0000457EACA7FBAA96DB4A8D506A0B69684F546166FBF3C55391B1461907EFA58EAF": 1027,
"00005751A05BA007E7E2518DEA171DBBD67B0527C637232F923830C39BFF9E8F159A": 20920
"00005751A05BA007E7E2518DEA171DBBD67B0527C637232F923830C39BFF9E8F159A": 20920,
"00002CEC79D588D5CDD24331968BEF0A9CFE8B1B03B8AEFC4454726DEF79AA10C125": 1
},
"testnet": {
"00001A4AB7AD0CE2B494C965C66FF2962692A5FE5ECB71B345ABB53BAD88A83A01F1": 3890,
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: archethic_dapp_framework_flutter
description: An internal framework for archethic flutter development
homepage: https://github.com/archethic-foundation/archethic-dapp-framework-flutter

version: 1.2.12
version: 1.2.13

environment:
sdk: ">=3.3.0 <4.0.0"
Expand Down

0 comments on commit fa305ba

Please sign in to comment.