stack_wallet/lib/services/ethereum/ethereum_token_service.dart

421 lines
13 KiB
Dart
Raw Normal View History

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';
import 'package:isar/isar.dart';
import 'package:stackwallet/db/isar/main_db.dart';
import 'package:stackwallet/dto/ethereum/eth_token_tx_dto.dart';
import 'package:stackwallet/dto/ethereum/eth_token_tx_extra_dto.dart';
import 'package:stackwallet/models/isar/models/isar_models.dart';
import 'package:stackwallet/models/node_model.dart';
2023-01-25 09:29:20 +00:00
import 'package:stackwallet/models/paymint/fee_object_model.dart';
import 'package:stackwallet/models/token_balance.dart';
import 'package:stackwallet/services/coins/ethereum/ethereum_wallet.dart';
2023-02-23 22:59:58 +00:00
import 'package:stackwallet/services/ethereum/ethereum_api.dart';
import 'package:stackwallet/services/event_bus/events/global/updated_in_background_event.dart';
import 'package:stackwallet/services/event_bus/events/global/wallet_sync_status_changed_event.dart';
import 'package:stackwallet/services/event_bus/global_event_bus.dart';
import 'package:stackwallet/services/mixins/eth_token_cache.dart';
import 'package:stackwallet/services/node_service.dart';
import 'package:stackwallet/services/transaction_notification_tracker.dart';
import 'package:stackwallet/utilities/default_nodes.dart';
import 'package:stackwallet/utilities/enums/coin_enum.dart';
import 'package:stackwallet/utilities/enums/fee_rate_type_enum.dart';
import 'package:stackwallet/utilities/eth_commons.dart';
import 'package:stackwallet/utilities/flutter_secure_storage_interface.dart';
import 'package:stackwallet/utilities/format.dart';
import 'package:stackwallet/utilities/logger.dart';
import 'package:tuple/tuple.dart';
2023-02-24 14:45:34 +00:00
import 'package:web3dart/web3dart.dart' as web3dart;
2023-01-25 16:08:27 +00:00
2023-03-03 14:36:56 +00:00
class EthTokenWallet extends ChangeNotifier with EthTokenCache {
final EthereumWallet ethWallet;
final TransactionNotificationTracker tracker;
final SecureStorageInterface _secureStore;
2023-02-23 22:59:58 +00:00
// late web3dart.EthereumAddress _contractAddress;
2023-02-24 14:45:34 +00:00
late web3dart.EthPrivateKey _credentials;
late web3dart.DeployedContract _deployedContract;
2023-02-24 14:45:34 +00:00
late web3dart.ContractFunction _balanceFunction;
late web3dart.ContractFunction _sendFunction;
late web3dart.Web3Client _client;
2023-01-25 09:29:20 +00:00
static const _gasLimit = 200000;
2023-01-25 16:08:27 +00:00
2023-03-03 14:36:56 +00:00
EthTokenWallet({
required EthContract token,
required this.ethWallet,
2023-01-27 12:32:05 +00:00
required SecureStorageInterface secureStore,
required this.tracker,
}) : _secureStore = secureStore,
_tokenContract = token {
// _contractAddress = web3dart.EthereumAddress.fromHex(token.address);
initCache(ethWallet.walletId, token);
}
2023-01-25 09:29:20 +00:00
EthContract get tokenContract => _tokenContract;
EthContract _tokenContract;
TokenBalance get balance => _balance ??= getCachedBalance();
TokenBalance? _balance;
2023-01-25 09:29:20 +00:00
Coin get coin => Coin.ethereum;
2023-01-25 09:29:20 +00:00
Future<String> confirmSend({required Map<String, dynamic> txData}) async {
final amount = txData['recipientAmt'];
final decimalAmount =
Format.satoshisToAmount(amount as int, coin: Coin.ethereum);
2023-02-23 22:59:58 +00:00
final bigIntAmount =
amountToBigInt(decimalAmount.toDouble(), tokenContract.decimals);
final sentTx = await _client.sendTransaction(
_credentials,
2023-02-24 14:45:34 +00:00
web3dart.Transaction.callContract(
contract: _deployedContract,
function: _sendFunction,
parameters: [
2023-02-24 14:45:34 +00:00
web3dart.EthereumAddress.fromHex(txData['address'] as String),
bigIntAmount
],
maxGas: _gasLimit,
2023-02-24 14:45:34 +00:00
gasPrice: web3dart.EtherAmount.fromUnitAndValue(
web3dart.EtherUnit.wei, txData['feeInWei'])));
return sentTx;
2023-01-25 09:29:20 +00:00
}
Future<String> get currentReceivingAddress async {
final address = await _currentReceivingAddress;
2023-03-01 21:19:53 +00:00
return checksumEthereumAddress(
address?.value ?? _credentials.address.toString());
}
2023-01-25 09:29:20 +00:00
Future<Address?> get _currentReceivingAddress => ethWallet.db
.getAddresses(ethWallet.walletId)
.filter()
.typeEqualTo(AddressType.ethereum)
.subTypeEqualTo(AddressSubType.receiving)
.sortByDerivationIndexDesc()
.findFirst();
Future<int> estimateFeeFor(int satoshiAmount, int feeRate) async {
final fee = estimateFee(feeRate, _gasLimit, tokenContract.decimals);
return Format.decimalAmountToSatoshis(Decimal.parse(fee.toString()), coin);
2023-01-25 09:29:20 +00:00
}
Future<FeeObject> get fees => _feeObject ??= _getFees();
Future<FeeObject>? _feeObject;
Future<FeeObject> _getFees() async {
2023-02-23 22:59:58 +00:00
return await EthereumAPI.getFees();
}
2023-01-25 09:29:20 +00:00
Future<EthContract> _updateTokenABI({
required EthContract forContract,
required String usingContractAddress,
}) async {
final abiResponse = await EthereumAPI.getTokenAbi(usingContractAddress);
// Fetch token ABI so we can call token functions
if (abiResponse.value != null) {
final updatedToken = forContract.copyWith(abi: abiResponse.value!);
// Store updated contract
final id = await MainDB.instance.putEthContract(updatedToken);
return updatedToken..id = id;
} else {
throw abiResponse.exception!;
}
}
Future<void> initialize() async {
final contractAddress =
web3dart.EthereumAddress.fromHex(tokenContract.address);
if (tokenContract.abi == null) {
_tokenContract = await _updateTokenABI(
forContract: tokenContract,
usingContractAddress: contractAddress.hex,
);
}
2023-01-27 12:32:05 +00:00
String? mnemonicString = await ethWallet.mnemonicString;
2023-01-25 16:08:27 +00:00
//Get private key for given mnemonic
String privateKey = getPrivateKey(
mnemonicString!,
(await ethWallet.mnemonicPassphrase) ?? "",
);
2023-02-24 14:45:34 +00:00
_credentials = web3dart.EthPrivateKey.fromHex(privateKey);
_deployedContract = web3dart.DeployedContract(
web3dart.ContractAbi.fromJson(tokenContract.abi!, tokenContract.name),
contractAddress,
);
try {
_balanceFunction = _deployedContract.function('balanceOf');
_sendFunction = _deployedContract.function('transfer');
} catch (_) {
2023-03-02 00:02:53 +00:00
// function not found so likely a proxy so we need to fetch the impl
final contractAddressResponse =
await EthereumAPI.getProxyTokenImplementation(contractAddress.hex);
if (contractAddressResponse.value != null) {
_tokenContract = await _updateTokenABI(
forContract: tokenContract,
usingContractAddress: contractAddressResponse.value!,
);
2023-03-02 00:02:53 +00:00
} else {
throw contractAddressResponse.exception!;
}
}
_deployedContract = web3dart.DeployedContract(
2023-03-02 00:02:53 +00:00
web3dart.ContractAbi.fromJson(
tokenContract.abi!,
tokenContract.name,
2023-03-02 00:02:53 +00:00
),
contractAddress,
2023-03-02 00:02:53 +00:00
);
_balanceFunction = _deployedContract.function('balanceOf');
_sendFunction = _deployedContract.function('transfer');
2023-03-02 00:02:53 +00:00
_client = await getEthClient();
2023-01-25 09:29:20 +00:00
unawaited(refresh());
}
2023-01-25 09:29:20 +00:00
bool get isRefreshing => _refreshLock;
2023-01-25 09:29:20 +00:00
Future<Map<String, dynamic>> prepareSend(
{required String address,
required int satoshiAmount,
Map<String, dynamic>? args}) async {
final feeRateType = args?["feeRate"];
int fee = 0;
final feeObject = await fees;
switch (feeRateType) {
case FeeRateType.fast:
fee = feeObject.fast;
break;
case FeeRateType.average:
fee = feeObject.medium;
break;
case FeeRateType.slow:
fee = feeObject.slow;
break;
}
final feeEstimate = await estimateFeeFor(satoshiAmount, fee);
Map<String, dynamic> txData = {
"fee": feeEstimate,
"feeInWei": fee,
"address": address,
"recipientAmt": satoshiAmount,
};
return txData;
2023-01-25 09:29:20 +00:00
}
bool _refreshLock = false;
Future<void> refresh() async {
if (!_refreshLock) {
_refreshLock = true;
try {
GlobalEventBus.instance.fire(
WalletSyncStatusChangedEvent(
WalletSyncStatus.syncing,
ethWallet.walletId + tokenContract.address,
coin,
),
);
await refreshCachedBalance();
await _refreshTransactions();
} catch (e, s) {
Logging.instance.log(
"Caught exception in ${tokenContract.name} ${ethWallet.walletName} ${ethWallet.walletId} refresh(): $e\n$s",
level: LogLevel.Warning,
);
} finally {
_refreshLock = false;
GlobalEventBus.instance.fire(
WalletSyncStatusChangedEvent(
WalletSyncStatus.synced,
ethWallet.walletId + tokenContract.address,
coin,
),
);
notifyListeners();
}
}
2023-01-25 09:29:20 +00:00
}
Future<void> refreshCachedBalance() async {
final balanceRequest = await _client.call(
contract: _deployedContract,
function: _balanceFunction,
params: [_credentials.address],
);
String _balance = balanceRequest.first.toString();
final newBalance = TokenBalance(
contractAddress: tokenContract.address,
total: int.parse(_balance),
spendable: int.parse(_balance),
blockedTotal: 0,
pendingSpendable: 0,
decimalPlaces: tokenContract.decimals,
2023-02-23 22:59:58 +00:00
);
await updateCachedBalance(newBalance);
notifyListeners();
}
2023-01-25 09:29:20 +00:00
Future<List<Transaction>> get transactions => ethWallet.db
.getTransactions(ethWallet.walletId)
.filter()
.otherDataEqualTo(tokenContract.address)
.sortByTimestampDesc()
.findAll();
2023-01-25 09:29:20 +00:00
Future<void> _refreshTransactions() async {
String addressString =
checksumEthereumAddress(await currentReceivingAddress);
2023-01-27 12:32:05 +00:00
final response = await EthereumAPI.getTokenTransactions(
address: addressString,
tokenContractAddress: tokenContract.address,
);
2023-01-27 12:32:05 +00:00
if (response.value == null) {
throw response.exception ??
Exception("Failed to fetch token transaction data");
}
2023-03-23 19:51:56 +00:00
// no need to continue if no transactions found
if (response.value!.isEmpty) {
return;
}
final response2 = await EthereumAPI.getEthTokenTransactionsByTxids(
response.value!.map((e) => e.transactionHash).toList(),
);
if (response2.value == null) {
throw response2.exception ??
Exception("Failed to fetch token transactions");
}
final List<Tuple2<EthTokenTxDto, EthTokenTxExtraDTO>> data = [];
for (final tokenDto in response.value!) {
data.add(
Tuple2(
tokenDto,
response2.value!.firstWhere(
(e) => e.hash == tokenDto.transactionHash,
),
),
);
}
2023-01-27 12:32:05 +00:00
final List<Tuple2<Transaction, Address?>> txnsData = [];
2023-01-27 12:32:05 +00:00
for (final tuple in data) {
bool isIncoming;
if (checksumEthereumAddress(tuple.item1.articulatedLog.inputs.from) ==
addressString) {
isIncoming = false;
} else {
isIncoming = true;
}
2023-01-27 12:32:05 +00:00
final txn = Transaction(
walletId: ethWallet.walletId,
txid: tuple.item1.transactionHash,
timestamp: tuple.item2.timestamp,
type: isIncoming ? TransactionType.incoming : TransactionType.outgoing,
subType: TransactionSubType.ethToken,
amount: int.parse(tuple.item1.articulatedLog.inputs.amount),
fee: tuple.item2.gasUsed * tuple.item2.gasPrice.toInt(),
height: tuple.item1.blockNumber,
isCancelled: false,
isLelantus: false,
slateId: null,
otherData: tuple.item1.address,
inputs: [],
outputs: [],
);
Address? transactionAddress = await ethWallet.db
.getAddresses(ethWallet.walletId)
.filter()
.valueEqualTo(addressString)
.findFirst();
if (transactionAddress == null) {
if (isIncoming) {
transactionAddress = Address(
walletId: ethWallet.walletId,
value: addressString,
publicKey: [],
derivationIndex: 0,
derivationPath: DerivationPath()..value = "$hdPathEthereum/0",
type: AddressType.ethereum,
subType: AddressSubType.receiving,
);
2023-01-27 12:32:05 +00:00
} else {
final myRcvAddr = await currentReceivingAddress;
final isSentToSelf = myRcvAddr == addressString;
transactionAddress = Address(
walletId: ethWallet.walletId,
value: addressString,
publicKey: [],
derivationIndex: isSentToSelf ? 0 : -1,
derivationPath: isSentToSelf
? (DerivationPath()..value = "$hdPathEthereum/0")
: null,
type: AddressType.ethereum,
subType: isSentToSelf
? AddressSubType.receiving
: AddressSubType.nonWallet,
);
2023-01-27 12:32:05 +00:00
}
2023-02-23 22:59:58 +00:00
}
2023-01-27 12:32:05 +00:00
txnsData.add(Tuple2(txn, transactionAddress));
}
await ethWallet.db.addNewTransactionData(txnsData, ethWallet.walletId);
// quick hack to notify manager to call notifyListeners if
// transactions changed
if (txnsData.isNotEmpty) {
GlobalEventBus.instance.fire(
UpdatedInBackgroundEvent(
"${tokenContract.name} transactions updated/added for: ${ethWallet.walletId} ${ethWallet.walletName}",
ethWallet.walletId,
),
);
2023-01-27 12:32:05 +00:00
}
}
2023-01-25 09:29:20 +00:00
bool validateAddress(String address) {
return isValidEthereumAddress(address);
}
2023-01-27 12:32:05 +00:00
Future<NodeModel> getCurrentNode() async {
return NodeService(secureStorageInterface: _secureStore)
.getPrimaryNodeFor(coin: coin) ??
DefaultNodes.getNodeFor(coin);
}
2023-02-24 14:45:34 +00:00
Future<web3dart.Web3Client> getEthClient() async {
2023-01-27 12:32:05 +00:00
final node = await getCurrentNode();
2023-02-24 14:45:34 +00:00
return web3dart.Web3Client(node.host, Client());
2023-01-25 09:29:20 +00:00
}
}