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

View file

@ -147,7 +147,7 @@ abstract class EthereumWalletBase
} }
} }
await _client.signTransaction( final transactionInfo = await _client.signTransaction(
_privateKey, _privateKey,
_credentials.outputs.first.address, _credentials.outputs.first.address,
totalAmount.toString(), totalAmount.toString(),
@ -155,10 +155,8 @@ abstract class EthereumWalletBase
); );
return PendingEthereumTransaction( return PendingEthereumTransaction(
client: _client, sendTransaction: () => _client.sendTransaction(_privateKey, transactionInfo),
credentials: _credentials, transactionInformation: transactionInfo,
privateKey: _privateKey,
amount: totalAmount,
); );
} }
@ -225,7 +223,6 @@ abstract class EthereumWalletBase
String toJSON() => json.encode({ String toJSON() => json.encode({
'mnemonic': _mnemonic, 'mnemonic': _mnemonic,
'balance': balance[currency]!.toJSON(), 'balance': balance[currency]!.toJSON(),
// TODO: save other attributes
}); });
static Future<EthereumWallet> open({ 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_core/pending_transaction.dart';
import 'package:cw_ethereum/ethereum_client.dart';
import 'package:cw_ethereum/ethereum_transaction_credentials.dart';
import 'package:web3dart/web3dart.dart'; import 'package:web3dart/web3dart.dart';
class PendingEthereumTransaction with PendingTransaction { class PendingEthereumTransaction with PendingTransaction {
final EthereumClient client; final Function sendTransaction;
final EthereumTransactionCredentials credentials; final TransactionInformation transactionInformation;
final EthPrivateKey privateKey;
final int amount;
PendingEthereumTransaction({ PendingEthereumTransaction({
required this.client, required this.sendTransaction,
required this.credentials, required this.transactionInformation,
required this.privateKey,
required this.amount,
}); });
@override @override
String get amountFormatted => AmountConverter.amountIntToString(CryptoCurrency.eth, amount); String get amountFormatted =>
transactionInformation.value.getValueInUnit(EtherUnit.ether).toString();
@override @override
Future<void> commit() async { Future<void> commit() async => sendTransaction();
for (var output in credentials.outputs) {
await client.sendTransaction(privateKey, output.address, output.cryptoAmount!); @override
} String get feeFormatted {
final fee = transactionInformation.gasPrice.getInWei * BigInt.from(transactionInformation.gas);
return EtherAmount.inWei(fee).getValueInUnit(EtherUnit.ether).toString();
} }
@override @override
// TODO: implement feeFormatted String get hex => transactionInformation.hash;
String get feeFormatted => "0.01";
@override @override
// TODO: implement hex String get id => transactionInformation.hashCode.toString();
String get hex => "hex";
@override
// TODO: implement id
String get id => "id";
} }