From 004c39102ce84d5b6f3a4947a1a0df2607a02b36 Mon Sep 17 00:00:00 2001 From: julian Date: Mon, 10 Apr 2023 11:55:02 -0600 Subject: [PATCH] token balance fix --- .../ethereum/ethereum_token_service.dart | 55 +++++++++---------- 1 file changed, 25 insertions(+), 30 deletions(-) diff --git a/lib/services/ethereum/ethereum_token_service.dart b/lib/services/ethereum/ethereum_token_service.dart index f56561e8d..13e52b72e 100644 --- a/lib/services/ethereum/ethereum_token_service.dart +++ b/lib/services/ethereum/ethereum_token_service.dart @@ -1,6 +1,5 @@ import 'dart:async'; -import 'package:decimal/decimal.dart'; import 'package:ethereum_addresses/ethereum_addresses.dart'; import 'package:flutter/widgets.dart'; import 'package:http/http.dart'; @@ -40,7 +39,6 @@ class EthTokenWallet extends ChangeNotifier with EthTokenCache { // late web3dart.EthereumAddress _contractAddress; late web3dart.EthPrivateKey _credentials; late web3dart.DeployedContract _deployedContract; - late web3dart.ContractFunction _balanceFunction; late web3dart.ContractFunction _sendFunction; late web3dart.Web3Client _client; @@ -284,7 +282,6 @@ class EthTokenWallet extends ChangeNotifier with EthTokenCache { ); try { - _balanceFunction = _deployedContract.function('balanceOf'); _sendFunction = _deployedContract.function('transfer'); } catch (_) { //==================================================================== @@ -359,7 +356,6 @@ class EthTokenWallet extends ChangeNotifier with EthTokenCache { contractAddress, ); - _balanceFunction = _deployedContract.function('balanceOf'); _sendFunction = _deployedContract.function('transfer'); _client = await getEthClient(); @@ -405,34 +401,33 @@ class EthTokenWallet extends ChangeNotifier with EthTokenCache { } Future refreshCachedBalance() async { - final balanceRequest = await _client.call( - contract: _deployedContract, - function: _balanceFunction, - params: [_credentials.address], + final response = await EthereumAPI.getWalletTokenBalance( + address: _credentials.address.hex, + contractAddress: tokenContract.address, ); - String _balance = balanceRequest.first.toString(); - - final newBalance = Balance( - total: Amount.fromDecimal( - Decimal.parse(_balance), - fractionDigits: tokenContract.decimals, - ), - spendable: Amount.fromDecimal( - Decimal.parse(_balance), - fractionDigits: tokenContract.decimals, - ), - blockedTotal: Amount( - rawValue: BigInt.zero, - fractionDigits: tokenContract.decimals, - ), - pendingSpendable: Amount( - rawValue: BigInt.zero, - fractionDigits: tokenContract.decimals, - ), - ); - await updateCachedBalance(newBalance); - notifyListeners(); + if (response.value != null) { + await updateCachedBalance( + Balance( + total: response.value!, + spendable: response.value!, + blockedTotal: Amount( + rawValue: BigInt.zero, + fractionDigits: tokenContract.decimals, + ), + pendingSpendable: Amount( + rawValue: BigInt.zero, + fractionDigits: tokenContract.decimals, + ), + ), + ); + notifyListeners(); + } else { + Logging.instance.log( + "CachedEthTokenBalance.fetchAndUpdateCachedBalance failed: ${response.exception}", + level: LogLevel.Warning, + ); + } } Future> get transactions => ethWallet.db