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,23 +401,16 @@ 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,
),
spendable: Amount.fromDecimal(
Decimal.parse(_balance),
fractionDigits: tokenContract.decimals,
),
blockedTotal: Amount( blockedTotal: Amount(
rawValue: BigInt.zero, rawValue: BigInt.zero,
fractionDigits: tokenContract.decimals, fractionDigits: tokenContract.decimals,
@ -430,9 +419,15 @@ class EthTokenWallet extends ChangeNotifier with EthTokenCache {
rawValue: BigInt.zero, rawValue: BigInt.zero,
fractionDigits: tokenContract.decimals, fractionDigits: tokenContract.decimals,
), ),
),
); );
await updateCachedBalance(newBalance);
notifyListeners(); notifyListeners();
} else {
Logging.instance.log(
"CachedEthTokenBalance.fetchAndUpdateCachedBalance failed: ${response.exception}",
level: LogLevel.Warning,
);
}
} }
Future<List<Transaction>> get transactions => ethWallet.db Future<List<Transaction>> get transactions => ethWallet.db