From d855d86500bc96ded7fd94da1d540cdad8e9df89 Mon Sep 17 00:00:00 2001 From: OmarHatem Date: Wed, 5 Apr 2023 19:20:25 +0200 Subject: [PATCH] Change sign/send transaction flow --- cw_ethereum/lib/ethereum_client.dart | 26 +++++++----- cw_ethereum/lib/ethereum_wallet.dart | 9 ++--- .../lib/pending_ethereum_transaction.dart | 40 +++++++------------ 3 files changed, 33 insertions(+), 42 deletions(-) diff --git a/cw_ethereum/lib/ethereum_client.dart b/cw_ethereum/lib/ethereum_client.dart index c812f7ea2..8f74a9a00 100644 --- a/cw_ethereum/lib/ethereum_client.dart +++ b/cw_ethereum/lib/ethereum_client.dart @@ -39,7 +39,7 @@ class EthereumClient { return result.map((e) => e.toInt()).toList(); } - Future signTransaction( + Future 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 sendTransaction(EthPrivateKey privateKey, String toAddress, String amount) async { + Future 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); } } diff --git a/cw_ethereum/lib/ethereum_wallet.dart b/cw_ethereum/lib/ethereum_wallet.dart index 3b78922eb..4abe6b887 100644 --- a/cw_ethereum/lib/ethereum_wallet.dart +++ b/cw_ethereum/lib/ethereum_wallet.dart @@ -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 open({ diff --git a/cw_ethereum/lib/pending_ethereum_transaction.dart b/cw_ethereum/lib/pending_ethereum_transaction.dart index 724146699..9cd29f286 100644 --- a/cw_ethereum/lib/pending_ethereum_transaction.dart +++ b/cw_ethereum/lib/pending_ethereum_transaction.dart @@ -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 commit() async { - for (var output in credentials.outputs) { - await client.sendTransaction(privateKey, output.address, output.cryptoAmount!); - } + Future 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(); }