mirror of
https://github.com/cypherstack/stack_wallet.git
synced 2025-03-12 09:27:01 +00:00
pre cache in wallet db sent eth and eth token txns on successful broadcast
This commit is contained in:
parent
7da81d01ad
commit
0ddecd2f97
2 changed files with 137 additions and 5 deletions
|
@ -84,6 +84,66 @@ class EthereumWallet extends Bip39Wallet with PrivateKeyInterface {
|
|||
_credentials = web3.EthPrivateKey.fromHex(privateKey);
|
||||
}
|
||||
|
||||
TxData _prepareTempTx(TxData txData, String myAddress) {
|
||||
// hack eth tx data into inputs and outputs
|
||||
final List<OutputV2> outputs = [];
|
||||
final List<InputV2> inputs = [];
|
||||
|
||||
final amount = txData.recipients!.first.amount;
|
||||
final addressTo = txData.recipients!.first.address;
|
||||
|
||||
final OutputV2 output = OutputV2.isarCantDoRequiredInDefaultConstructor(
|
||||
scriptPubKeyHex: "00",
|
||||
valueStringSats: amount.raw.toString(),
|
||||
addresses: [
|
||||
addressTo,
|
||||
],
|
||||
walletOwns: addressTo == myAddress,
|
||||
);
|
||||
final InputV2 input = InputV2.isarCantDoRequiredInDefaultConstructor(
|
||||
scriptSigHex: null,
|
||||
scriptSigAsm: null,
|
||||
sequence: null,
|
||||
outpoint: null,
|
||||
addresses: [myAddress],
|
||||
valueStringSats: amount.raw.toString(),
|
||||
witness: null,
|
||||
innerRedeemScriptAsm: null,
|
||||
coinbase: null,
|
||||
walletOwns: true,
|
||||
);
|
||||
|
||||
outputs.add(output);
|
||||
inputs.add(input);
|
||||
|
||||
final otherData = {
|
||||
"nonce": txData.nonce,
|
||||
"isCancelled": false,
|
||||
"overrideFee": txData.fee!.toJsonString(),
|
||||
};
|
||||
|
||||
final txn = TransactionV2(
|
||||
walletId: walletId,
|
||||
blockHash: null,
|
||||
hash: txData.txHash!,
|
||||
txid: txData.txid!,
|
||||
timestamp: DateTime.now().millisecondsSinceEpoch ~/ 1000,
|
||||
height: null,
|
||||
inputs: List.unmodifiable(inputs),
|
||||
outputs: List.unmodifiable(outputs),
|
||||
version: -1,
|
||||
type: addressTo == myAddress
|
||||
? TransactionType.sentToSelf
|
||||
: TransactionType.outgoing,
|
||||
subType: TransactionSubType.none,
|
||||
otherData: jsonEncode(otherData),
|
||||
);
|
||||
|
||||
return txData.copyWith(
|
||||
tempTx: txn,
|
||||
);
|
||||
}
|
||||
|
||||
// ==================== Overrides ============================================
|
||||
|
||||
@override
|
||||
|
@ -447,7 +507,10 @@ class EthereumWallet extends Bip39Wallet with PrivateKeyInterface {
|
|||
}
|
||||
|
||||
@override
|
||||
Future<TxData> confirmSend({required TxData txData}) async {
|
||||
Future<TxData> confirmSend({
|
||||
required TxData txData,
|
||||
TxData Function(TxData txData, String myAddress)? prepareTempTx,
|
||||
}) async {
|
||||
final client = getEthClient();
|
||||
if (_credentials == null) {
|
||||
await _initCredentials();
|
||||
|
@ -459,10 +522,15 @@ class EthereumWallet extends Bip39Wallet with PrivateKeyInterface {
|
|||
chainId: txData.chainId!.toInt(),
|
||||
);
|
||||
|
||||
return txData.copyWith(
|
||||
txid: txid,
|
||||
txHash: txid,
|
||||
final data = (prepareTempTx ?? _prepareTempTx)(
|
||||
txData.copyWith(
|
||||
txid: txid,
|
||||
txHash: txid,
|
||||
),
|
||||
(await getCurrentReceivingAddress())!.value,
|
||||
);
|
||||
|
||||
return await updateSentCachedTxData(txData: data);
|
||||
}
|
||||
|
||||
@override
|
||||
|
|
|
@ -67,6 +67,67 @@ class EthTokenWallet extends Wallet {
|
|||
String _addressFromTopic(String topic) =>
|
||||
checksumEthereumAddress("0x${topic.substring(topic.length - 40)}");
|
||||
|
||||
TxData _prepareTempTx(TxData txData, String myAddress) {
|
||||
final otherData = {
|
||||
"nonce": txData.nonce!,
|
||||
"isCancelled": false,
|
||||
"overrideFee": txData.fee!.toJsonString(),
|
||||
"contractAddress": tokenContract.address,
|
||||
};
|
||||
|
||||
final amount = txData.recipients!.first.amount;
|
||||
final addressTo = txData.recipients!.first.address;
|
||||
|
||||
// hack eth tx data into inputs and outputs
|
||||
final List<OutputV2> outputs = [];
|
||||
final List<InputV2> inputs = [];
|
||||
|
||||
final output = OutputV2.isarCantDoRequiredInDefaultConstructor(
|
||||
scriptPubKeyHex: "00",
|
||||
valueStringSats: amount.raw.toString(),
|
||||
addresses: [
|
||||
addressTo,
|
||||
],
|
||||
walletOwns: addressTo == myAddress,
|
||||
);
|
||||
final input = InputV2.isarCantDoRequiredInDefaultConstructor(
|
||||
scriptSigHex: null,
|
||||
scriptSigAsm: null,
|
||||
sequence: null,
|
||||
outpoint: null,
|
||||
addresses: [myAddress],
|
||||
valueStringSats: amount.raw.toString(),
|
||||
witness: null,
|
||||
innerRedeemScriptAsm: null,
|
||||
coinbase: null,
|
||||
walletOwns: true,
|
||||
);
|
||||
|
||||
outputs.add(output);
|
||||
inputs.add(input);
|
||||
|
||||
final tempTx = TransactionV2(
|
||||
walletId: walletId,
|
||||
blockHash: null,
|
||||
hash: txData.txHash!,
|
||||
txid: txData.txid!,
|
||||
timestamp: DateTime.now().millisecondsSinceEpoch ~/ 1000,
|
||||
height: null,
|
||||
inputs: List.unmodifiable(inputs),
|
||||
outputs: List.unmodifiable(outputs),
|
||||
version: -1,
|
||||
type: addressTo == myAddress
|
||||
? TransactionType.sentToSelf
|
||||
: TransactionType.outgoing,
|
||||
subType: TransactionSubType.ethToken,
|
||||
otherData: jsonEncode(otherData),
|
||||
);
|
||||
|
||||
return txData.copyWith(
|
||||
tempTx: tempTx,
|
||||
);
|
||||
}
|
||||
|
||||
// ===========================================================================
|
||||
|
||||
@override
|
||||
|
@ -205,7 +266,10 @@ class EthTokenWallet extends Wallet {
|
|||
@override
|
||||
Future<TxData> confirmSend({required TxData txData}) async {
|
||||
try {
|
||||
return await ethWallet.confirmSend(txData: txData);
|
||||
return await ethWallet.confirmSend(
|
||||
txData: txData,
|
||||
prepareTempTx: _prepareTempTx,
|
||||
);
|
||||
} catch (e) {
|
||||
// rethrow to pass error in alert
|
||||
rethrow;
|
||||
|
|
Loading…
Reference in a new issue