From 490618ebd6b7af0a509fda06c2e9f8273dc1772a Mon Sep 17 00:00:00 2001 From: fosse Date: Fri, 28 Jul 2023 09:30:11 -0400 Subject: [PATCH] balance working --- cw_nano/lib/nano_balance.dart | 5 +- cw_nano/lib/nano_client.dart | 94 +++++++++++++---------------------- cw_nano/lib/nano_wallet.dart | 3 +- 3 files changed, 39 insertions(+), 63 deletions(-) diff --git a/cw_nano/lib/nano_balance.dart b/cw_nano/lib/nano_balance.dart index 13ef1b4d3..23abc242e 100644 --- a/cw_nano/lib/nano_balance.dart +++ b/cw_nano/lib/nano_balance.dart @@ -1,6 +1,7 @@ import 'package:cw_core/balance.dart'; import 'package:cw_core/currency.dart'; import 'package:cw_core/monero_amount_format.dart'; +import 'package:cw_nano/nano_util.dart'; String rawToFormattedAmount(BigInt amount, Currency currency) { return ""; @@ -29,11 +30,11 @@ class NanoBalance extends Balance { @override String get formattedAvailableBalance { - return "0"; + return NanoUtil.getRawAsUsableString(currentBalance.toString(), NanoUtil.rawPerNano); } @override String get formattedAdditionalBalance { - return "0"; + return NanoUtil.getRawAsUsableString(receivableBalance.toString(), NanoUtil.rawPerNano); } } diff --git a/cw_nano/lib/nano_client.dart b/cw_nano/lib/nano_client.dart index 4455880d9..5efb19edf 100644 --- a/cw_nano/lib/nano_client.dart +++ b/cw_nano/lib/nano_client.dart @@ -29,67 +29,41 @@ class NanoClient { void setListeners(EthereumAddress userAddress, Function(FilterEvent) onNewTransaction) async {} Future getBalance(String address) async { - return NanoBalance(currentBalance: BigInt.zero, receivableBalance: BigInt.zero); + // this is the preferred rpc call but the test node isn't returning this one: + // final response = await _httpClient.post( + // _node!.uri, + // headers: {"Content-Type": "application/json"}, + // body: jsonEncode( + // { + // "action": "account_balance", + // "account": address, + // }, + // ), + // ); + // final data = await jsonDecode(response.body); + // final String currentBalance = data["balance"] as String; + // final String receivableBalance = data["receivable"] as String; + // final BigInt cur = BigInt.parse(currentBalance); + // final BigInt rec = BigInt.parse(receivableBalance); + + final response = await _httpClient.post( + _node!.uri, + headers: {"Content-Type": "application/json"}, + body: jsonEncode( + { + "action": "accounts_balances", + "accounts": [address], + }, + ), + ); + final data = await jsonDecode(response.body); + final String currentBalance = data["balances"][address]["balance"] as String; + final String receivableBalance = data["balances"][address]["receivable"] as String; + final BigInt cur = BigInt.parse(currentBalance); + final BigInt rec = BigInt.parse(receivableBalance); + return NanoBalance(currentBalance: cur, receivableBalance: rec); } - // Future signTransaction({ - // required EthPrivateKey privateKey, - // required String toAddress, - // required String amount, - // required int gas, - // required EthereumTransactionPriority priority, - // required CryptoCurrency currency, - // required int exponent, - // String? contractAddress, - // }) async { - // assert(currency == CryptoCurrency.eth || contractAddress != null); - - // bool _isEthereum = currency == CryptoCurrency.eth; - - // final price = await _client!.getGasPrice(); - - // final Transaction transaction = Transaction( - // from: privateKey.address, - // to: EthereumAddress.fromHex(toAddress), - // maxGas: gas, - // gasPrice: price, - // value: _isEthereum ? EtherAmount.inWei(BigInt.parse(amount)) : EtherAmount.zero(), - // ); - - // final signedTransaction = await _client!.signTransaction(privateKey, transaction); - - // final BigInt estimatedGas; - // final Function _sendTransaction; - - // if (_isEthereum) { - // estimatedGas = BigInt.from(21000); - // _sendTransaction = () async => await sendTransaction(signedTransaction); - // } else { - // estimatedGas = BigInt.from(50000); - - // final erc20 = Erc20( - // client: _client!, - // address: EthereumAddress.fromHex(contractAddress!), - // ); - - // _sendTransaction = () async { - // await erc20.transfer( - // EthereumAddress.fromHex(toAddress), - // BigInt.parse(amount), - // credentials: privateKey, - // ); - // }; - // } - - // return PendingEthereumTransaction( - // signedTransaction: signedTransaction, - // amount: amount, - // fee: estimatedGas * price.getInWei, - // sendTransaction: _sendTransaction, - // exponent: exponent, - // ); - // } - Future getTransactionDetails(String transactionHash) async { throw UnimplementedError(); } @@ -106,7 +80,7 @@ class NanoClient { body: jsonEncode({ "action": "account_history", "account": address, - "count": "250",// TODO: pick a number + "count": "250", // TODO: pick a number // "raw": true, })); final data = await jsonDecode(response.body); diff --git a/cw_nano/lib/nano_wallet.dart b/cw_nano/lib/nano_wallet.dart index 4c1c4f449..baa148fc4 100644 --- a/cw_nano/lib/nano_wallet.dart +++ b/cw_nano/lib/nano_wallet.dart @@ -131,7 +131,6 @@ abstract class NanoWalletBase } Future updateTransactions() async { - print("updating_transactions"); try { if (_isTransactionUpdating) { return; @@ -246,6 +245,8 @@ abstract class NanoWalletBase } Future _updateBalance() async { + // this.balance.update(CryptoCurrency.nano, (value) => (await _client.getBalance(_publicAddress))); + balance[currency] = await _client.getBalance(_publicAddress); await save(); }