token balance fix

This commit is contained in:
julian 2023-04-10 11:55:02 -06:00
parent 7ee5c196a0
commit 004c39102c

View file

@ -1,6 +1,5 @@
import 'dart:async'; import 'dart:async';
import 'package:decimal/decimal.dart';
import 'package:ethereum_addresses/ethereum_addresses.dart'; import 'package:ethereum_addresses/ethereum_addresses.dart';
import 'package:flutter/widgets.dart'; import 'package:flutter/widgets.dart';
import 'package:http/http.dart'; import 'package:http/http.dart';
@ -40,7 +39,6 @@ class EthTokenWallet extends ChangeNotifier with EthTokenCache {
// late web3dart.EthereumAddress _contractAddress; // late web3dart.EthereumAddress _contractAddress;
late web3dart.EthPrivateKey _credentials; late web3dart.EthPrivateKey _credentials;
late web3dart.DeployedContract _deployedContract; late web3dart.DeployedContract _deployedContract;
late web3dart.ContractFunction _balanceFunction;
late web3dart.ContractFunction _sendFunction; late web3dart.ContractFunction _sendFunction;
late web3dart.Web3Client _client; late web3dart.Web3Client _client;
@ -284,7 +282,6 @@ class EthTokenWallet extends ChangeNotifier with EthTokenCache {
); );
try { try {
_balanceFunction = _deployedContract.function('balanceOf');
_sendFunction = _deployedContract.function('transfer'); _sendFunction = _deployedContract.function('transfer');
} catch (_) { } catch (_) {
//==================================================================== //====================================================================
@ -359,7 +356,6 @@ class EthTokenWallet extends ChangeNotifier with EthTokenCache {
contractAddress, contractAddress,
); );
_balanceFunction = _deployedContract.function('balanceOf');
_sendFunction = _deployedContract.function('transfer'); _sendFunction = _deployedContract.function('transfer');
_client = await getEthClient(); _client = await getEthClient();
@ -405,34 +401,33 @@ class EthTokenWallet extends ChangeNotifier with EthTokenCache {
} }
Future<void> refreshCachedBalance() async { Future<void> refreshCachedBalance() async {
final balanceRequest = await _client.call( final response = await EthereumAPI.getWalletTokenBalance(
contract: _deployedContract, address: _credentials.address.hex,
function: _balanceFunction, contractAddress: tokenContract.address,
params: [_credentials.address],
); );
String _balance = balanceRequest.first.toString(); if (response.value != null) {
await updateCachedBalance(
final newBalance = Balance( Balance(
total: Amount.fromDecimal( total: response.value!,
Decimal.parse(_balance), spendable: response.value!,
fractionDigits: tokenContract.decimals, blockedTotal: Amount(
), rawValue: BigInt.zero,
spendable: Amount.fromDecimal( fractionDigits: tokenContract.decimals,
Decimal.parse(_balance), ),
fractionDigits: tokenContract.decimals, pendingSpendable: Amount(
), rawValue: BigInt.zero,
blockedTotal: Amount( fractionDigits: tokenContract.decimals,
rawValue: BigInt.zero, ),
fractionDigits: tokenContract.decimals, ),
), );
pendingSpendable: Amount( notifyListeners();
rawValue: BigInt.zero, } else {
fractionDigits: tokenContract.decimals, Logging.instance.log(
), "CachedEthTokenBalance.fetchAndUpdateCachedBalance failed: ${response.exception}",
); level: LogLevel.Warning,
await updateCachedBalance(newBalance); );
notifyListeners(); }
} }
Future<List<Transaction>> get transactions => ethWallet.db Future<List<Transaction>> get transactions => ethWallet.db