Skip to content

Commit

Permalink
Merge pull request #136 from polkawallet-io/develop
Browse files Browse the repository at this point in the history
add KSM, DOT market price
  • Loading branch information
RomeroYang authored Aug 15, 2020
2 parents f61413f + d47bf2b commit c2d8f9f
Show file tree
Hide file tree
Showing 14 changed files with 204 additions and 72 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
# 0.9.4
- add KSM/DOT prices

# 0.9.3
- bug fix

# 0.9.1
- add support for opening dApps in webview

# 0.9.0
- add Governance proposals
- add Council motions
Expand Down
2 changes: 1 addition & 1 deletion ios/Flutter/.last_build_id
Original file line number Diff line number Diff line change
@@ -1 +1 @@
21550ad233f9eb1230b6dc6301eb3cf0
dd15065b86c2890a4fee04c8e16046f0
6 changes: 3 additions & 3 deletions ios/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@
"$(inherited)",
"$(PROJECT_DIR)/Flutter",
);
MARKETING_VERSION = 0.9.3;
MARKETING_VERSION = 0.9.4;
PRODUCT_BUNDLE_IDENTIFIER = io.polkawallet.polkawallet;
PRODUCT_NAME = Runner;
SWIFT_OBJC_BRIDGING_HEADER = "Runner/Runner-Bridging-Header.h";
Expand Down Expand Up @@ -531,7 +531,7 @@
"$(inherited)",
"$(PROJECT_DIR)/Flutter",
);
MARKETING_VERSION = 0.9.3;
MARKETING_VERSION = 0.9.4;
PRODUCT_BUNDLE_IDENTIFIER = io.polkawallet.polkawallet;
PRODUCT_NAME = Runner;
SWIFT_OBJC_BRIDGING_HEADER = "Runner/Runner-Bridging-Header.h";
Expand Down Expand Up @@ -561,7 +561,7 @@
"$(inherited)",
"$(PROJECT_DIR)/Flutter",
);
MARKETING_VERSION = 0.9.3;
MARKETING_VERSION = 0.9.4;
PRODUCT_BUNDLE_IDENTIFIER = io.polkawallet.polkawallet;
PRODUCT_NAME = Runner;
SWIFT_OBJC_BRIDGING_HEADER = "Runner/Runner-Bridging-Header.h";
Expand Down
2 changes: 1 addition & 1 deletion lib/common/consts/settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ const String cross_chain_transfer_address_laminar =
'5CLaminarAUSDCrossChainTransferxxxxxxxxxxxxxwisu';

/// app versions
const String app_beta_version = '0.9.3-beta.1';
const String app_beta_version = '0.9.4-beta.1';

/// js code versions
const Map<String, int> js_code_version_map = {
Expand Down
109 changes: 64 additions & 45 deletions lib/page/assets/asset/assetPage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,17 @@ class _AssetPageState extends State<AssetPage>
});
}

String tokenPrice;
if ((store.settings.endpoint.info == network_name_polkadot ||
store.settings.endpoint.info == network_name_kusama) &&
store.assets.marketPrices[symbol] != null &&
balancesInfo != null) {
tokenPrice = (store.assets.marketPrices[symbol] *
Fmt.bigIntToDouble(balancesInfo.total,
decimals: decimals))
.toStringAsFixed(4);
}

return Column(
children: <Widget>[
Container(
Expand All @@ -231,7 +242,8 @@ class _AssetPageState extends State<AssetPage>
child: Column(
children: <Widget>[
Padding(
padding: EdgeInsets.only(bottom: 16),
padding: EdgeInsets.only(
bottom: tokenPrice != null ? 4 : 16),
child: Text(
Fmt.token(isBaseToken ? balancesInfo.total : balance,
decimals: decimals, length: 8),
Expand All @@ -242,53 +254,60 @@ class _AssetPageState extends State<AssetPage>
),
),
),
tokenPrice != null
? Padding(
padding: EdgeInsets.only(bottom: 16),
child: Text(
'≈ \$ ${tokenPrice ?? '--.--'}',
style: TextStyle(
color: Theme.of(context).cardColor,
),
),
)
: Container(),
isBaseToken
? Builder(
builder: (_) {
return Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
margin: EdgeInsets.only(right: 12),
child: Row(
children: <Widget>[
lockedInfo.length > 2
? TapTooltip(
message: lockedInfo,
child: Padding(
padding: EdgeInsets.only(
right: 6),
child: Icon(
Icons.info,
size: 16,
color: titleColor,
),
),
waitDuration:
Duration(seconds: 0),
)
: Container(),
Text(
'${dic['locked']}: ${Fmt.token(balancesInfo.lockedBalance, decimals: decimals)}',
style: TextStyle(color: titleColor),
),
],
),
),
Container(
margin: EdgeInsets.only(right: 12),
child: Text(
'${dic['available']}: ${Fmt.token(balancesInfo.transferable, decimals: decimals)}',
? Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
margin: EdgeInsets.only(right: 12),
child: Row(
children: <Widget>[
lockedInfo.length > 2
? TapTooltip(
message: lockedInfo,
child: Padding(
padding:
EdgeInsets.only(right: 6),
child: Icon(
Icons.info,
size: 16,
color: titleColor,
),
),
waitDuration:
Duration(seconds: 0),
)
: Container(),
Text(
'${dic['locked']}: ${Fmt.token(balancesInfo.lockedBalance, decimals: decimals)}',
style: TextStyle(color: titleColor),
),
),
Text(
'${dic['reserved']}: ${Fmt.token(balancesInfo.reserved, decimals: decimals)}',
style: TextStyle(color: titleColor),
),
],
);
},
],
),
),
Container(
margin: EdgeInsets.only(right: 12),
child: Text(
'${dic['available']}: ${Fmt.token(balancesInfo.transferable, decimals: decimals)}',
style: TextStyle(color: titleColor),
),
),
Text(
'${dic['reserved']}: ${Fmt.token(balancesInfo.reserved, decimals: decimals)}',
style: TextStyle(color: titleColor),
),
],
)
: Container(),
],
Expand Down
43 changes: 32 additions & 11 deletions lib/page/assets/index.dart
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,7 @@ class _AssetsState extends State<Assets> {
bool isLaminar =
store.settings.endpoint.info == networkEndpointLaminar.info;
bool isPolkadot = store.settings.endpoint.info == network_name_polkadot;
bool isKusama = store.settings.endpoint.info == network_name_kusama;

List<String> currencyIds = [];
if ((isAcala || isLaminar) && networkName != null) {
Expand All @@ -460,6 +461,12 @@ class _AssetsState extends State<Assets> {
style: TextStyle(fontStyle: FontStyle.italic),
);
}
String tokenPrice;
if (store.assets.marketPrices[symbol] != null && balancesInfo != null) {
tokenPrice = (store.assets.marketPrices[symbol] *
Fmt.bigIntToDouble(balancesInfo.total, decimals: decimals))
.toStringAsFixed(2);
}

return RefreshIndicator(
key: globalBalanceRefreshKey,
Expand Down Expand Up @@ -548,17 +555,31 @@ class _AssetsState extends State<Assets> {
'assets/images/assets/${symbol.isNotEmpty ? symbol : 'DOT'}.png'),
),
title: tokenViewTitle,
trailing: Text(
Fmt.priceFloorBigInt(
balancesInfo != null
? balancesInfo.total
: BigInt.zero,
decimals: decimals,
lengthFixed: 3),
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20,
color: Colors.black54),
trailing: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Text(
Fmt.priceFloorBigInt(
balancesInfo != null
? balancesInfo.total
: BigInt.zero,
decimals: decimals,
lengthFixed: 3),
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20,
color: Colors.black54),
),
isPolkadot || isKusama
? Text(
'≈ \$ ${tokenPrice ?? '--.--'}',
style: TextStyle(
color: Theme.of(context).disabledColor,
),
)
: Container(width: 16),
],
),
onTap: () {
Navigator.pushNamed(context, AssetPage.route,
Expand Down
9 changes: 2 additions & 7 deletions lib/page/homePage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,6 @@ class _HomePageState extends State<HomePage> {
return _tabList.asMap().keys.map((i) {
if (i == 0) {
// return assets page
final bool accountsReady =
store.account.pubKeyAddressMap[store.settings.endpoint.ss58] !=
null;
return BackgroundWrapper(
AssetImage("assets/images/assets/top_bg_$imageColor.png"),
Scaffold(
Expand All @@ -112,10 +109,8 @@ class _HomePageState extends State<HomePage> {
actions: <Widget>[
IconButton(
icon: Icon(Icons.menu),
onPressed: accountsReady
? () => Navigator.of(context)
.pushNamed(NetworkSelectPage.route)
: null,
onPressed: () =>
Navigator.of(context).pushNamed(NetworkSelectPage.route),
),
],
),
Expand Down
7 changes: 5 additions & 2 deletions lib/page/networkSelectPage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,11 @@ class _NetworkSelectPageState extends State<NetworkSelectPage> {
accounts.addAll(store.account.optionalAccounts);

res.addAll(accounts.map((i) {
String address =
store.account.pubKeyAddressMap[_selectedNetwork.ss58][i.pubKey];
String address = i.address;
if (store.account.pubKeyAddressMap[_selectedNetwork.ss58] != null) {
address =
store.account.pubKeyAddressMap[_selectedNetwork.ss58][i.pubKey];
}
return RoundedCard(
border: address == store.account.currentAddress
? Border.all(color: Theme.of(context).primaryColorLight)
Expand Down
2 changes: 1 addition & 1 deletion lib/service/faucet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'dart:convert';
import 'package:http/http.dart';

class FaucetApi {
static const String _endpoint = 'https://apps.acala.network/faucet';
static const String _endpoint = 'https://api.polkawallet.io/faucet';

static Future<String> getAcalaTokens(String address, String deviceId) async {
Map<String, String> headers = {"Content-type": "application/json"};
Expand Down
35 changes: 35 additions & 0 deletions lib/service/subscan.dart
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,39 @@ class SubScanApi {
}
return {};
}

Future<Map> fetchTokenPriceAsync(String network) async {
Completer completer = new Completer<Map>();
ReceivePort receivePort = ReceivePort();
Isolate isolateIns = await Isolate.spawn(
SubScanApi.fetchTokenPrice,
SubScanRequestParams(
sendPort: receivePort.sendPort,
network: network,
));
receivePort.listen((msg) {
receivePort.close();
isolateIns.kill(priority: Isolate.immediate);
completer.complete(msg);
});
return completer.future;
}

static Future<Map> fetchTokenPrice(SubScanRequestParams para) async {
String url = '${getSnEndpoint(para.network)}/token';
Map<String, String> headers = {"Content-type": "application/json"};

Response res = await post(url, headers: headers);
if (res.body != null) {
final obj = await compute(jsonDecode, res.body);
if (para.sendPort != null) {
para.sendPort.send(obj['data']);
}
return obj['data'];
}
if (para.sendPort != null) {
para.sendPort.send({});
}
return {};
}
}
15 changes: 15 additions & 0 deletions lib/service/substrateApi/apiAssets.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class ApiAssets {
if (store.settings.endpoint.info == networkEndpointLaminar.info) {
apiRoot.laminar.fetchTokens(store.account.currentAccount.pubKey);
}
_fetchMarketPrice();
}

Future<Map> updateTxs(int page) async {
Expand All @@ -47,4 +48,18 @@ class ApiAssets {
store.assets.setTxsLoading(false);
return res;
}

Future<void> _fetchMarketPrice() async {
if (store.settings.endpoint.info == network_name_kusama ||
store.settings.endpoint.info == network_name_polkadot) {
final Map res = await webApi.subScanApi
.fetchTokenPriceAsync(store.settings.endpoint.info);
if (res['token'] == null) {
print('fetch market price failed');
return;
}
final String token = res['token'][0];
store.assets.setMarketPrices(token, res['detail'][token]['price']);
}
}
}
8 changes: 8 additions & 0 deletions lib/store/assets/assets.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ abstract class _AssetsStore with Store {
@observable
List announcements;

@observable
ObservableMap<String, double> marketPrices = ObservableMap<String, double>();

@computed
ObservableList<TransferData> get txsView {
return ObservableList.of(txs.where((i) {
Expand Down Expand Up @@ -212,6 +215,11 @@ abstract class _AssetsStore with Store {
announcements = data;
}

@action
void setMarketPrices(String token, String price) {
marketPrices[token] = double.parse(price);
}

@action
Future<void> loadAccountCache() async {
// return if currentAccount not exist
Expand Down
Loading

0 comments on commit c2d8f9f

Please sign in to comment.