Add Eth address initial setup

This commit is contained in:
OmarHatem 2023-03-31 20:41:56 +02:00
parent 3d8c2af4a2
commit db90dde952
5 changed files with 64 additions and 68 deletions

View file

@ -1,6 +1,7 @@
import 'package:cw_core/node.dart';
import 'package:cw_ethereum/ethereum_transaction_priority.dart';
import 'package:http/http.dart';
import 'package:web3dart/crypto.dart';
import 'package:web3dart/web3dart.dart';
class EthereumClient {
@ -30,14 +31,32 @@ class EthereumClient {
Future<List<int>> getEstimatedGasForPriorities() async {
final result = await Future.wait(EthereumTransactionPriority.all.map(
(priority) => _client!.estimateGas(
maxPriorityFeePerGas: EtherAmount.fromUnitAndValue(EtherUnit.gwei, priority.tip),
// maxFeePerGas: EtherAmount.fromUnitAndValue(EtherUnit.gwei, 100),
),
// maxPriorityFeePerGas: EtherAmount.fromUnitAndValue(EtherUnit.gwei, priority.tip),
// maxFeePerGas: EtherAmount.fromUnitAndValue(EtherUnit.gwei, 100),
),
));
return result.map((e) => e.toInt()).toList();
}
Future<String> signTransaction(String privateKey, String toAddress, String amount) async {
final credentials = EthPrivateKey.fromHex(privateKey);
final transaction = Transaction(
from: credentials.address,
to: EthereumAddress.fromHex(toAddress),
value: EtherAmount.zero(),
);
print("@@@@@@@@@@@@@@@@@");
print(transaction);
String tr = bytesToHex(await _client!.signTransaction(credentials, transaction));
print("@@@@@@@@@@@@@@@@@");
print(tr);
return tr;
}
Future<String> sendTransaction(String privateKey, String toAddress, String amount) async {
final credentials = EthPrivateKey.fromHex(privateKey);
@ -47,8 +66,6 @@ class EthereumClient {
value: EtherAmount.fromUnitAndValue(EtherUnit.ether, amount),
);
await _client!.signTransaction(credentials, transaction);
return await _client!.sendTransaction(
credentials,
transaction,

View file

@ -1,6 +1,9 @@
class EthereumTransactionCreationException implements Exception {
EthereumTransactionCreationException();
final String exceptionMessage;
EthereumTransactionCreationException(
{this.exceptionMessage = 'Wrong balance. Not enough Ether on your balance.'});
@override
String toString() => 'Wrong balance. Not enough Ether on your balance.';
String toString() => exceptionMessage;
}

View file

@ -72,6 +72,7 @@ abstract class EthereumWalletBase
late ObservableMap<CryptoCurrency, EthereumBalance> balance;
Future<void> init() async {
await walletAddresses.init();
_privateKey = await getPrivateKey(_mnemonic, _password);
transactionHistory = EthereumTransactionHistory();
walletAddresses.address = EthPrivateKey.fromHex(_privateKey).address.toString();
@ -120,76 +121,42 @@ abstract class EthereumWalletBase
@override
Future<PendingTransaction> createTransaction(Object credentials) async {
print("!!!!!!!!!!!!!!!!!!!");
final _credentials = credentials as EthereumTransactionCredentials;
final outputs = _credentials.outputs;
final hasMultiDestination = outputs.length > 1;
final balance = await _client.getBalance(_privateKey);
int totalAmount = 0;
if (hasMultiDestination) {
outputs.any((element) => element.sendAll);
}
if (hasMultiDestination) {
if (outputs.any((item) => item.sendAll
|| (item.formattedCryptoAmount ?? 0) <= 0)) {
if (outputs.any((item) => item.sendAll || (item.formattedCryptoAmount ?? 0) <= 0)) {
throw EthereumTransactionCreationException();
}
final BigInt totalAmount = outputs.fold(0, (acc, value) =>
acc + (value.formattedCryptoAmount ?? 0));
totalAmount = outputs.fold(0, (acc, value) => acc + (value.formattedCryptoAmount ?? 0));
if (balance.getInWei < EtherAmount.inWei(totalAmount)) {
throw MoneroTransactionCreationException('Wrong balance. Not enough XMR on your balance.');
if (balance.getInWei < EtherAmount.inWei(totalAmount as BigInt).getInWei) {
throw EthereumTransactionCreationException();
}
final moneroOutputs = outputs.map((output) {
final outputAddress = output.isParsedAddress
? output.extractedAddress
: output.address;
return MoneroOutput(
address: outputAddress!,
amount: output.cryptoAmount!.replaceAll(',', '.'));
}).toList();
pendingTransactionDescription =
await transaction_history.createTransactionMultDest(
outputs: moneroOutputs,
priorityRaw: _credentials.priority.serialize(),
accountIndex: walletAddresses.account!.id);
} else {
final output = outputs.first;
final address = output.isParsedAddress
? output.extractedAddress
: output.address;
final amount = output.sendAll
? null
: output.cryptoAmount!.replaceAll(',', '.');
final formattedAmount = output.sendAll
? null
: output.formattedCryptoAmount;
final int allAmount = balance.getInWei.toInt() - feeRate(_credentials.priority!);
totalAmount = output.sendAll ? allAmount : output.formattedCryptoAmount ?? 0;
if ((formattedAmount != null && unlockedBalance < formattedAmount) ||
(formattedAmount == null && unlockedBalance <= 0)) {
final formattedBalance = moneroAmountToString(amount: unlockedBalance);
throw MoneroTransactionCreationException(
'Incorrect unlocked balance. Unlocked: $formattedBalance. Transaction amount: ${output.cryptoAmount}.');
if ((output.sendAll &&
balance.getInWei < EtherAmount.inWei(totalAmount as BigInt).getInWei) ||
(!output.sendAll && balance.getInWei.toInt() <= 0)) {
throw EthereumTransactionCreationException();
}
pendingTransactionDescription =
await transaction_history.createTransaction(
address: address!,
amount: amount,
priorityRaw: _credentials.priority.serialize(),
accountIndex: walletAddresses.account!.id);
}
await _client.signTransaction(_privateKey, _credentials.outputs.first.address, totalAmount.toString());
return PendingEthereumTransaction(
client: _client,
credentials: _credentials,
privateKey: _privateKey,
amount: totalAmount,
);
}
@ -208,6 +175,7 @@ abstract class EthereumWalletBase
@override
Future<void> save() async {
await walletAddresses.updateAddressesInBox();
final path = await makePath();
await write(path: path, password: _password, data: toJSON());
await transactionHistory.save();

View file

@ -15,14 +15,19 @@ abstract class EthereumWalletAddressesBase extends WalletAddresses with Store {
String address;
@override
Future<void> init() {
// TODO: implement init
throw UnimplementedError();
Future<void> init() async {
address = walletInfo.address;
await updateAddressesInBox();
}
@override
Future<void> updateAddressesInBox() {
// TODO: implement updateAddressesInBox
throw UnimplementedError();
Future<void> updateAddressesInBox() async {
try {
addressesMap.clear();
addressesMap[address] = '';
await saveAddressesInBox();
} catch (e) {
print(e.toString());
}
}
}

View file

@ -1,3 +1,5 @@
import 'package:cw_core/amount_converter.dart';
import 'package:cw_core/crypto_currency.dart';
import 'package:cw_core/pending_transaction.dart';
import 'package:cw_ethereum/ethereum_client.dart';
import 'package:cw_ethereum/ethereum_transaction_credentials.dart';
@ -6,16 +8,17 @@ class PendingEthereumTransaction with PendingTransaction {
final EthereumClient client;
final EthereumTransactionCredentials credentials;
final String privateKey;
final int amount;
PendingEthereumTransaction({
required this.client,
required this.credentials,
required this.privateKey,
required this.amount,
});
@override
// TODO: implement amountFormatted
String get amountFormatted => throw UnimplementedError();
String get amountFormatted => AmountConverter.amountIntToString(CryptoCurrency.eth, amount);
@override
Future<void> commit() async {
@ -26,13 +29,13 @@ class PendingEthereumTransaction with PendingTransaction {
@override
// TODO: implement feeFormatted
String get feeFormatted => throw UnimplementedError();
String get feeFormatted => "0.01";
@override
// TODO: implement hex
String get hex => throw UnimplementedError();
String get hex => "hex";
@override
// TODO: implement id
String get id => throw UnimplementedError();
String get id => "id";
}