Skip to content

Commit

Permalink
add homaLite tx history pages
Browse files Browse the repository at this point in the history
  • Loading branch information
RomeroYang committed Aug 26, 2021
1 parent c2f30e5 commit 46a65b7
Show file tree
Hide file tree
Showing 10 changed files with 226 additions and 127 deletions.
26 changes: 18 additions & 8 deletions lib/api/types/txHomaData.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'package:polkawallet_ui/utils/format.dart';

class TxHomaData extends _TxHomaData {
static const String actionMint = 'mint';
static const String actionRedeem = 'redeem';
Expand All @@ -8,19 +10,27 @@ class TxHomaData extends _TxHomaData {
static const String redeemTypeWait = 'WaitForUnbonding';
static TxHomaData fromJson(Map<String, dynamic> json) {
TxHomaData data = TxHomaData();
data.hash = json['hash'];
data.action = json['action'];
data.amountPay = json['amountPay'];
data.amountReceive = json['amountReceive'];
data.time = DateTime.fromMillisecondsSinceEpoch(json['time']);
data.action = json['extrinsic']['method'];
data.hash = json['extrinsic']['id'];

data.action = json['extrinsic']['method'];
data.amountPay = Fmt.balanceInt(json['data'][1]['value'].toString());
data.amountReceive = Fmt.balanceInt(json['data'][2]['value'].toString());

data.time = (json['extrinsic']['timestamp'] as String).replaceAll(' ', '');
data.isSuccess = json['extrinsic']['isSuccess'];
return data;
}
}

abstract class _TxHomaData {
String block;
String hash;

String action;
String amountPay;
String amountReceive;
DateTime time;
BigInt amountPay;
BigInt amountReceive;

String time;
bool isSuccess = true;
}
4 changes: 2 additions & 2 deletions lib/common/constants/base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ const ss58_prefix_karura = 8;

const GraphQLConfig = {
plugin_name_acala: {'httpUri': 'https://api.polkawallet.io/acala-subql'},
plugin_name_karura: {'httpUri': 'https://api.polkawallet.io/karura-subql'},
// plugin_name_karura: {'httpUri': 'http://47.92.117.129/'},
plugin_name_karura: {'httpUri': 'https://api.polkawallet.io/acala-subql'},
// plugin_name_karura: {'httpUri': 'http://47.243.180.213/'},
};
64 changes: 32 additions & 32 deletions lib/common/constants/nodeList.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,40 +14,40 @@ const node_list = {
},
],
plugin_name_karura: [
// {
// 'name': 'Karura (Hosted by Polkawallet)',
// 'ss58': ss58_prefix_karura,
// 'endpoint': 'wss://karura.polkawallet.io',
// },
// {
// 'name': 'Karura (Hosted by Acala Foundation 0)',
// 'ss58': ss58_prefix_karura,
// 'endpoint': 'wss://karura-rpc-0.aca-api.network',
// },
// {
// 'name': 'Karura (Hosted by Acala Foundation 1)',
// 'ss58': ss58_prefix_karura,
// 'endpoint': 'wss://karura-rpc-1.aca-api.network',
// },
// {
// 'name': 'Karura (Hosted by Acala Foundation 2)',
// 'ss58': ss58_prefix_karura,
// 'endpoint': 'wss://karura-rpc-2.aca-api.network',
// },
// {
// 'name': 'Karura (Hosted by Acala Foundation 3)',
// 'ss58': ss58_prefix_karura,
// 'endpoint': 'wss://karura-rpc-3.aca-api.network',
// },
// {
// 'name': 'Karura (Hosted by OnFinality)',
// 'ss58': ss58_prefix_karura,
// 'endpoint': 'wss://karura.api.onfinality.io',
// },
{
'name': 'Acala Karura (Polkawallet dev node)',
'name': 'Karura (Hosted by Polkawallet)',
'ss58': ss58_prefix_karura,
'endpoint': 'wss://kusama-1.polkawallet.io:9944',
'endpoint': 'wss://karura.polkawallet.io',
},
{
'name': 'Karura (Hosted by Acala Foundation 0)',
'ss58': ss58_prefix_karura,
'endpoint': 'wss://karura-rpc-0.aca-api.network',
},
{
'name': 'Karura (Hosted by Acala Foundation 1)',
'ss58': ss58_prefix_karura,
'endpoint': 'wss://karura-rpc-1.aca-api.network',
},
{
'name': 'Karura (Hosted by Acala Foundation 2)',
'ss58': ss58_prefix_karura,
'endpoint': 'wss://karura-rpc-2.aca-api.network',
},
{
'name': 'Karura (Hosted by Acala Foundation 3)',
'ss58': ss58_prefix_karura,
'endpoint': 'wss://karura-rpc-3.aca-api.network',
},
{
'name': 'Karura (Hosted by OnFinality)',
'ss58': ss58_prefix_karura,
'endpoint': 'wss://karura.api.onfinality.io',
},
// {
// 'name': 'Acala Karura (Polkawallet dev node)',
// 'ss58': ss58_prefix_karura,
// 'endpoint': 'wss://kusama-1.polkawallet.io:9944',
// },
],
};
29 changes: 6 additions & 23 deletions lib/common/constants/subQuery.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,34 +79,17 @@ const dexStakeQuery = r'''
''';
const homaQuery = r'''
query ($account: String) {
calls(filter: {
and: [
{
or: [
{ method: {equalTo: "mint"} },
{ method: {equalTo: "redeem"} }
]
},
{ section: {equalTo: "homa"} },
{ signerId: { equalTo: $account } }
]
}, orderBy: TIMESTAMP_DESC, first: 20) {
homaActions(filter: {accountId: {equalTo: $account}},
orderBy: TIMESTAMP_DESC, first: 20) {
nodes {
id
method
section
args
isSuccess
type
data
extrinsic {
id
block {number}
method
timestamp
events {
nodes {
data,
method
}
}
isSuccess
}
}
}
Expand Down
106 changes: 74 additions & 32 deletions lib/pages/homa/homaHistoryPage.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:graphql_flutter/graphql_flutter.dart';
import 'package:intl/intl.dart';
import 'package:polkawallet_plugin_acala/api/types/txHomaData.dart';
import 'package:polkawallet_plugin_acala/common/constants/index.dart';
import 'package:polkawallet_plugin_acala/common/constants/subQuery.dart';
import 'package:polkawallet_plugin_acala/pages/homa/homaTxDetailPage.dart';
import 'package:polkawallet_plugin_acala/polkawallet_plugin_acala.dart';
import 'package:polkawallet_plugin_acala/utils/i18n/index.dart';
import 'package:polkawallet_sdk/storage/keyring.dart';
Expand All @@ -19,49 +23,87 @@ class HomaHistoryPage extends StatelessWidget {

@override
Widget build(BuildContext context) {
final symbols = plugin.networkState.tokenSymbol;
final decimals = plugin.networkState.tokenDecimals;
final symbol = relay_chain_token_symbol[plugin.basic.name];
final list = plugin.store.homa.txs.reversed.toList();
return Scaffold(
appBar: AppBar(
title: Text(
I18n.of(context).getDic(i18n_full_dic_acala, 'acala')['loan.txs']),
centerTitle: true,
),
body: SafeArea(
child: ListView.builder(
itemCount: list.length + 1,
itemBuilder: (BuildContext context, int i) {
if (i == list.length) {
return ListTail(isEmpty: list.length == 0, isLoading: false);
child: Query(
options: QueryOptions(
document: gql(homaQuery),
variables: <String, String>{
'account': keyring.current.address,
},
),
builder: (
QueryResult result, {
Future<QueryResult> Function() refetch,
FetchMore fetchMore,
}) {
if (result.data == null) {
return Container(
height: MediaQuery.of(context).size.height / 3,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [CupertinoActivityIndicator()],
),
);
}

final detail = list[i];
final list = List.of(result.data['homaActions']['nodes'])
.map((i) => TxHomaData.fromJson(i as Map))
.toList();

String amountPay = detail.amountPay ?? '0';
String amountReceive = detail.amountReceive ?? '0';
if (detail.action == TxHomaData.actionRedeem) {
amountPay += ' L$symbol';
amountReceive += ' $symbol';
} else {
amountPay += ' $symbol';
amountReceive += ' L$symbol';
}
return Container(
decoration: BoxDecoration(
border: Border(
bottom: BorderSide(width: 0.5, color: Colors.black12)),
),
child: ListTile(
title: Text('${list[i].action} $amountReceive'),
subtitle: Text(Fmt.dateTime(list[i].time)),
leading:
SvgPicture.asset('assets/images/assets_up.svg', width: 32),
trailing: Text(
amountPay,
style: Theme.of(context).textTheme.headline4,
textAlign: TextAlign.end,
),
),
final nativeDecimal = decimals[symbols.indexOf(symbol)];
final liquidDecimal = decimals[symbols.indexOf('L$symbol')];

return ListView.builder(
itemCount: list.length + 1,
itemBuilder: (BuildContext context, int i) {
if (i == list.length) {
return ListTail(isEmpty: list.length == 0, isLoading: false);
}

final detail = list[i];

String amountPay =
Fmt.priceFloorBigInt(detail.amountPay, nativeDecimal);
String amountReceive =
Fmt.priceFloorBigInt(detail.amountReceive, liquidDecimal);
if (detail.action == TxHomaData.actionRedeem) {
amountPay += ' L$symbol';
amountReceive += ' $symbol';
} else {
amountPay += ' $symbol';
amountReceive += ' L$symbol';
}
return Container(
decoration: BoxDecoration(
border: Border(
bottom: BorderSide(width: 0.5, color: Colors.black12)),
),
child: ListTile(
title: Text('${detail.action} $amountReceive'),
subtitle: Text(Fmt.dateTime(
DateFormat("yyyy-MM-ddTHH:mm:ss")
.parse(detail.time, true))),
leading: SvgPicture.asset('assets/images/assets_up.svg',
width: 32),
trailing: Text(
amountPay,
style: Theme.of(context).textTheme.headline4,
textAlign: TextAlign.end,
),
onTap: () => Navigator.of(context)
.pushNamed(HomaTxDetailPage.route, arguments: detail),
),
);
},
);
},
),
Expand Down
3 changes: 2 additions & 1 deletion lib/pages/homa/homaPage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ class _HomaPageState extends State<HomaPage> {
final poolInfo = widget.plugin.store.homa.poolInfo;
final staked = poolInfo.staked ?? BigInt.zero;
final cap = poolInfo.cap ?? BigInt.zero;
final amountLeft = cap - staked;
final liquidTokenIssuance = poolInfo.liquidTokenIssuance ?? BigInt.zero;

final List<charts.Series> seriesList = [
Expand All @@ -164,7 +165,7 @@ class _HomaPageState extends State<HomaPage> {
measureFn: (num i, _) => i,
data: [
staked.toDouble(),
cap.toDouble(),
amountLeft.toDouble(),
],
)
];
Expand Down
65 changes: 65 additions & 0 deletions lib/pages/homa/homaTxDetailPage.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:polkawallet_plugin_acala/api/types/txHomaData.dart';
import 'package:polkawallet_plugin_acala/common/constants/index.dart';
import 'package:polkawallet_plugin_acala/polkawallet_plugin_acala.dart';
import 'package:polkawallet_plugin_acala/utils/i18n/index.dart';
import 'package:polkawallet_sdk/storage/keyring.dart';
import 'package:polkawallet_sdk/utils/i18n.dart';
import 'package:polkawallet_ui/components/txDetail.dart';
import 'package:polkawallet_ui/utils/format.dart';

class HomaTxDetailPage extends StatelessWidget {
HomaTxDetailPage(this.plugin, this.keyring);
final PluginAcala plugin;
final Keyring keyring;

static final String route = '/acala/homa/tx';

@override
Widget build(BuildContext context) {
final Map<String, String> dic =
I18n.of(context).getDic(i18n_full_dic_acala, 'acala');
final decimals = plugin.networkState.tokenDecimals;
final symbols = plugin.networkState.tokenSymbol;

final TxHomaData tx = ModalRoute.of(context).settings.arguments;

final symbol = relay_chain_token_symbol[plugin.basic.name];
final nativeDecimal = decimals[symbols.indexOf(symbol)];
final liquidDecimal = decimals[symbols.indexOf('L$symbol')];

String amountPay = Fmt.priceFloorBigInt(tx.amountPay, nativeDecimal);
String amountReceive =
Fmt.priceFloorBigInt(tx.amountReceive, liquidDecimal);
if (tx.action == TxHomaData.actionRedeem) {
amountPay += ' L$symbol';
amountReceive += ' $symbol';
} else {
amountPay += ' $symbol';
amountReceive += ' L$symbol';
}

final amountStyle = TextStyle(fontSize: 16, fontWeight: FontWeight.bold);
return TxDetail(
success: tx.isSuccess,
action: tx.action,
// blockNum: int.parse(tx.block),
hash: tx.hash,
blockTime:
Fmt.dateTime(DateFormat("yyyy-MM-ddTHH:mm:ss").parse(tx.time, true)),
networkName: plugin.basic.name,
infoItems: [
TxDetailInfoItem(
label: dic['dex.pay'],
content: Text(amountPay, style: amountStyle),
),
TxDetailInfoItem(
label: dic['dex.receive'],
content: Text(amountReceive, style: amountStyle),
)
],
);
}
}
Loading

0 comments on commit 46a65b7

Please sign in to comment.