Change sign/send transaction flow

This commit is contained in:
OmarHatem 2023-04-05 19:20:25 +02:00
parent b02b653ceb
commit d855d86500
3 changed files with 33 additions and 42 deletions

View file

@ -39,7 +39,7 @@ class EthereumClient {
return result.map((e) => e.toInt()).toList();
}
Future<String> signTransaction(
Future<TransactionInformation> signTransaction(
EthPrivateKey privateKey,
String toAddress,
String amount,
@ -52,21 +52,25 @@ class EthereumClient {
// maxPriorityFeePerGas: EtherAmount.inWei(BigInt.from(fee)),
);
final Uint8List signedTransaction = await _client!.signTransaction(privateKey, transaction);
final Uint8List signedTransactionRaw = await _client!.signTransaction(privateKey, transaction);
return bytesToHex(signedTransaction);
final transactionHash = bytesToHex(signedTransactionRaw);
final signedTransaction = await _client!.getTransactionByHash(transactionHash);
return signedTransaction;
}
Future<String> sendTransaction(EthPrivateKey privateKey, String toAddress, String amount) async {
Future<String> sendTransaction(
EthPrivateKey privateKey, TransactionInformation transactionInformation) async {
final transaction = Transaction(
from: privateKey.address,
to: EthereumAddress.fromHex(toAddress),
value: EtherAmount.fromUnitAndValue(EtherUnit.ether, amount),
from: transactionInformation.from,
to: transactionInformation.to,
value: transactionInformation.value,
gasPrice: transactionInformation.gasPrice,
data: transactionInformation.input,
);
return await _client!.sendTransaction(
privateKey,
transaction,
);
return await _client!.sendTransaction(privateKey, transaction);
}
}

View file

@ -147,7 +147,7 @@ abstract class EthereumWalletBase
}
}
await _client.signTransaction(
final transactionInfo = await _client.signTransaction(
_privateKey,
_credentials.outputs.first.address,
totalAmount.toString(),
@ -155,10 +155,8 @@ abstract class EthereumWalletBase
);
return PendingEthereumTransaction(
client: _client,
credentials: _credentials,
privateKey: _privateKey,
amount: totalAmount,
sendTransaction: () => _client.sendTransaction(_privateKey, transactionInfo),
transactionInformation: transactionInfo,
);
}
@ -225,7 +223,6 @@ abstract class EthereumWalletBase
String toJSON() => json.encode({
'mnemonic': _mnemonic,
'balance': balance[currency]!.toJSON(),
// TODO: save other attributes
});
static Future<EthereumWallet> open({

View file

@ -1,42 +1,32 @@
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';
import 'package:web3dart/web3dart.dart';
class PendingEthereumTransaction with PendingTransaction {
final EthereumClient client;
final EthereumTransactionCredentials credentials;
final EthPrivateKey privateKey;
final int amount;
final Function sendTransaction;
final TransactionInformation transactionInformation;
PendingEthereumTransaction({
required this.client,
required this.credentials,
required this.privateKey,
required this.amount,
required this.sendTransaction,
required this.transactionInformation,
});
@override
String get amountFormatted => AmountConverter.amountIntToString(CryptoCurrency.eth, amount);
String get amountFormatted =>
transactionInformation.value.getValueInUnit(EtherUnit.ether).toString();
@override
Future<void> commit() async {
for (var output in credentials.outputs) {
await client.sendTransaction(privateKey, output.address, output.cryptoAmount!);
}
Future<void> commit() async => sendTransaction();
@override
String get feeFormatted {
final fee = transactionInformation.gasPrice.getInWei * BigInt.from(transactionInformation.gas);
return EtherAmount.inWei(fee).getValueInUnit(EtherUnit.ether).toString();
}
@override
// TODO: implement feeFormatted
String get feeFormatted => "0.01";
String get hex => transactionInformation.hash;
@override
// TODO: implement hex
String get hex => "hex";
@override
// TODO: implement id
String get id => "id";
String get id => transactionInformation.hashCode.toString();
}