Skip to content

Commit

Permalink
Merge pull request #189 from xch-dev/tx-confirm-improvement
Browse files Browse the repository at this point in the history
Improve tx confirm flow
  • Loading branch information
Rigidity authored Dec 24, 2024
2 parents a92fbb3 + b34a43c commit a0f7d14
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 44 deletions.
2 changes: 1 addition & 1 deletion crates/sage-wallet/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ pub async fn upsert_coin(
let start = Instant::now();
let is_p2 = tx.is_p2_puzzle_hash(coin_state.coin.puzzle_hash).await?;
counters.is_p2 += start.elapsed();
// If the coin is XCH, there's no reason to sync the puzzle.

// If the coin is XCH, there's no reason to sync the puzzle.
let start = Instant::now();
tx.insert_coin_state(coin_state, is_p2, transaction_id)
.await?;
Expand Down
71 changes: 35 additions & 36 deletions crates/sage-wallet/src/utils/submit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ pub async fn submit_to_peers(
) -> Result<Status, WalletError> {
let transaction_id = spend_bundle.name();

info!(
"Broadcasting transaction id {}: {:?}",
transaction_id, spend_bundle
);
info!("Checking transaction id {}", transaction_id);

let mut mempool = false;
let mut status = 0;
Expand Down Expand Up @@ -60,37 +57,6 @@ pub async fn submit_transaction(
spend_bundle: SpendBundle,
genesis_challenge: Bytes32,
) -> Result<Status, WalletError> {
let ack = match timeout(
Duration::from_secs(3),
peer.send_transaction(spend_bundle.clone()),
)
.await
{
Ok(Ok(ack)) => ack,
Err(_timeout) => {
warn!("Send transaction timed out for {}", peer.socket_addr());
return Ok(Status::Unknown);
}
Ok(Err(err)) => {
warn!(
"Send transaction failed for {}: {}",
peer.socket_addr(),
err
);
return Ok(Status::Unknown);
}
};

info!(
"Transaction sent to {} with ack {:?}",
peer.socket_addr(),
ack
);

if ack.status == 1 {
return Ok(Status::Pending);
};

let coin_ids: Vec<Bytes32> = spend_bundle
.coin_spends
.iter()
Expand Down Expand Up @@ -124,5 +90,38 @@ pub async fn submit_transaction(
return Ok(Status::Failed(3, None));
}

Ok(Status::Failed(3, None))
info!("Submitting transaction to {}", peer.socket_addr());

let ack = match timeout(
Duration::from_secs(3),
peer.send_transaction(spend_bundle.clone()),
)
.await
{
Ok(Ok(ack)) => ack,
Err(_timeout) => {
warn!("Send transaction timed out for {}", peer.socket_addr());
return Ok(Status::Unknown);
}
Ok(Err(err)) => {
warn!(
"Send transaction failed for {}: {}",
peer.socket_addr(),
err
);
return Ok(Status::Unknown);
}
};

info!(
"Transaction response received from {} with ack {:?}",
peer.socket_addr(),
ack
);

if ack.status == 1 {
return Ok(Status::Pending);
};

Ok(Status::Failed(ack.status, ack.error))
}
1 change: 1 addition & 0 deletions src/components/ConfirmationDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ export default function ConfirmationDialog({
if (!data) return reset();

onConfirm?.();
reset();
})().finally(() => setPending(false));
}}
disabled={pending}
Expand Down
2 changes: 1 addition & 1 deletion src/components/Layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import { Cog, LogOut } from 'lucide-react';
import { PropsWithChildren, useMemo } from 'react';
import { Link, useNavigate } from 'react-router-dom';

import { useInsets } from '@/contexts/SafeAreaContext';
import useInitialization from '@/hooks/useInitialization';
import { usePeers } from '@/hooks/usePeers';
import { useWallet } from '@/hooks/useWallet';
import icon from '@/icon.png';
import { logoutAndUpdateState, useWalletState } from '@/state';
import { Nav } from './Nav';
import { useInsets } from '@/contexts/SafeAreaContext';

export default function Layout(props: PropsWithChildren<object>) {
const navigate = useNavigate();
Expand Down
21 changes: 17 additions & 4 deletions src/components/MultiSelectActions.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { commands, TransactionResponse } from '@/bindings';
import { useErrors } from '@/hooks/useErrors';
import { toMojos } from '@/lib/utils';
import { useWalletState } from '@/state';
import {
ChevronDown,
Expand Down Expand Up @@ -43,23 +44,35 @@ export function MultiSelectActions({

const onTransferSubmit = (address: string, fee: string) => {
commands
.transferNfts({ nft_ids: selected, address, fee })
.transferNfts({
nft_ids: selected,
address,
fee: toMojos(fee, walletState.sync.unit.decimals),
})
.then(setResponse)
.catch(addError)
.finally(() => setTransferOpen(false));
};

const onAssignSubmit = (profile: string, fee: string) => {
commands
.assignNftsToDid({ nft_ids: selected, did_id: profile, fee })
.assignNftsToDid({
nft_ids: selected,
did_id: profile,
fee: toMojos(fee, walletState.sync.unit.decimals),
})
.then(setResponse)
.catch(addError)
.finally(() => setAssignOpen(false));
};

const onUnassignSubmit = (fee: string) => {
commands
.assignNftsToDid({ nft_ids: selected, did_id: null, fee })
.assignNftsToDid({
nft_ids: selected,
did_id: null,
fee: toMojos(fee, walletState.sync.unit.decimals),
})
.then(setResponse)
.catch(addError)
.finally(() => setUnassignOpen(false));
Expand All @@ -70,7 +83,7 @@ export function MultiSelectActions({
.transferNfts({
nft_ids: selected,
address: walletState.sync.burn_address,
fee,
fee: toMojos(fee, walletState.sync.unit.decimals),
})
.then(setResponse)
.catch(addError)
Expand Down
4 changes: 2 additions & 2 deletions src/pages/Login.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import SafeAreaView from '@/components/SafeAreaView';
import { Button } from '@/components/ui/button';
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
import {
Expand Down Expand Up @@ -35,7 +36,6 @@ import { useNavigate } from 'react-router-dom';
import { commands, KeyInfo, SecretKeyInfo } from '../bindings';
import Container from '../components/Container';
import { loginAndUpdateState } from '../state';
import SafeAreaView from '@/components/SafeAreaView';

export default function Login() {
const navigate = useNavigate();
Expand Down Expand Up @@ -66,7 +66,7 @@ export default function Login() {

return (
<SafeAreaView>
<div className='flex-1 space-y-4 p-8 pt-6'>
<div className='flex-1 space-y-4 p-8 pt-6 overflow-y-scroll'>
<div className='flex items-center justify-between space-y-2'>
{(keys?.length ?? 0) > 0 && (
<>
Expand Down

0 comments on commit a0f7d14

Please sign in to comment.