btc: add basic error handling

This commit is contained in:
pkova 2021-05-12 17:06:18 +03:00 committed by ixv
parent 6847bf0813
commit d93020d9c6
8 changed files with 86 additions and 13 deletions

View File

@ -290,7 +290,7 @@
[u.uw eny.bowl block.btc-state ~ feyb.comm ~[[address.comm value.comm ~]]]
?~ tb
%- (slog ~[leaf+"insufficient balance or not enough confirmed balance"])
`state
[[(give-update %error %insufficient-balance)]~ state]
=^ tb=(unit txbu) state
?~ chng `state
=/ [addr=address =idx w=walt]
@ -307,10 +307,12 @@
:: overwrites any payment being built in poym
::
%init-payment
~| "Can't pay ourselves; no comets; can't do while tx is being signed"
?< =(src.bowl payee.comm)
?< ?=(%pawn (clan:title payee.comm))
?< is-broadcasting
?: =(src.bowl payee.comm)
[[(give-update %error %cant-pay-ourselves)]~ state]
?: ?=(%pawn (clan:title payee.comm))
[[(give-update %error %no-comets)]~ state]
?: is-broadcasting
[[(give-update %error %tx-being-signed)]~ state]
:_ state(poym [~ note.comm], feybs (~(put by feybs) payee.comm feyb.comm))
~[(poke-peer payee.comm [%gen-pay-address value.comm note.comm])]
::
@ -396,7 +398,7 @@
(generate-txbu u.curr-xpub `src.bowl feyb ~[[address.act value.act ~]])
=/ po=^poym ?~(tb [~ ~] [tb note.poym])
:_ state(poym po)
?~ tb ~
?~ tb [(give-update %error %insufficient-balance)]~
%+ turn txis.u.tb
|=(=txi (poke-provider [%raw-tx txid.utxo.txi]))
::

View File

@ -110,6 +110,7 @@
%cancel-tx (hexb txid.upd)
%new-address (address address.upd)
%balance (balance balance.upd)
%error s+error.upd
==
::
++ initial

View File

@ -117,6 +117,13 @@
note=(unit @t)
==
+$ history (map txid hest)
::
+$ error
$? %cant-pay-ourselves
%no-comets
%tx-being-signed
%insufficient-balance
==
:: data to send to the frontend
::
+$ update
@ -136,6 +143,7 @@
[%cancel-tx =txid]
[%new-address =address]
[%balance balance=(unit [confirmed=sats unconfirmed=sats])]
[%error =error]
==
::
--

View File

@ -72,9 +72,10 @@ export default class Balance extends Component {
sats={sats}
conversion={conversion}
network={this.props.network}
error={this.props.state.error}
stopSending={() => {
this.setState({sending: false});
store.handleEvent({data: {psbt: ''}});
store.handleEvent({data: {psbt: '', error: ''}});
}}
/> :
<Col

View File

@ -0,0 +1,29 @@
import React, { Component } from 'react';
import { Text } from '@tlon/indigo-react';
const errorToString = (error) => {
if (error === 'cant-pay-ourselves') {
return 'Cannot pay ourselves';
}
if (error === 'no-comets') {
return 'Cannot pay comets';
}
if (error === 'tx-being-signed') {
return 'Cannot pay when transaction is being signed';
}
if (error === 'insufficient-balance') {
return 'Insufficient confirmed balance';
}
}
export default function Error(props) {
const error = errorToString(props.error);
return(
<Text
color='red'
{...props}>
{error}
</Text>
);
}

View File

@ -13,6 +13,7 @@ import {
import Invoice from './invoice.js'
import FeePicker from './feePicker.js'
import Error from './error.js'
import { validate } from 'bitcoin-address-validation';
@ -75,10 +76,13 @@ export default class Send extends Component {
}
checkPayee(e){
store.handleEvent({data: {error: ''}});
let payee = e.target.value;
let isPatp = ob.isValidPatp(payee);
let isAddress = validate(payee);
if (isPatp) {
let command = {'check-payee': payee}
this.props.api.btcWalletCommand(command)
@ -110,6 +114,10 @@ export default class Send extends Component {
}
componentDidUpdate(prevProps, prevState) {
if (prevProps.error !== this.props.error && this.props.error !== '') {
this.setState({signing: false});
}
if (!this.state.ready && this.state.checkingPatp) {
if (this.props.shipWallets[this.state.payee.slice(1)]) {
this.setState({ready: true, checkingPatp: false, validPayee: true});
@ -145,7 +153,11 @@ export default class Send extends Component {
let payeeColor = "black";
let payeeBg = "white";
let payeeBorder = "lightGray";
if (this.state.focusPayee && this.state.validPayee) {
if (this.props.error) {
payeeColor="red";
payeeBorder = "red";
payeeBg="veryLightRed";
} else if (this.state.focusPayee && this.state.validPayee) {
payeeColor = "green";
payeeBorder = "green";
payeeBg = "veryLightGreen";
@ -167,7 +179,7 @@ export default class Send extends Component {
}
const { api, value, conversion, stopSending, denomination, psbt, currencyRates } = this.props;
const { api, value, conversion, stopSending, denomination, psbt, currencyRates, error } = this.props;
const { denomAmount, satsAmount, signing, payee } = this.state;
const signReady = (this.state.ready && (parseInt(this.state.satsAmount) > 0)) && !signing;
@ -231,6 +243,20 @@ export default class Send extends Component {
onChange={this.checkPayee}
/>
</Row>
{error &&
<Row
alignItems='center'
justifyContent='space-between'>
{/* yes this is a hack */}
<Box width='calc(40% - 30px)'/>
<Error
error={error}
fontSize='14px'
ml={2}
mt={2}
width='100%' />
</Row>
}
<Row
alignItems='center'
mt={4}
@ -350,16 +376,14 @@ export default class Send extends Component {
mt={4}
py='24px'
px='24px'
onClick={() =>{
this.initPayment()
}}
onClick={this.initPayment}
color={signReady ? "white" : "lighterGray"}
backgroundColor={signReady ? "blue" : "veryLightGray"}
disabled={!signReady}
border="none"
style={{cursor: signReady ? "pointer" : "default"}}
/>
{ (!signing) ? null :
{ (!(signing && !error)) ? null :
<LoadingSpinner mr={2} background="midOrange" foreground="orange"/>
}
</Row>

View File

@ -36,6 +36,9 @@ export class UpdateReducer {
if (json.balance) {
this.reduceBalance(json.balance, state);
}
if (json.hasOwnProperty('error')) {
this.reduceError(json.error, state);
}
}
reduceProviderStatus(json, state) {
@ -99,4 +102,8 @@ export class UpdateReducer {
state.unconfirmedBalance = json.unconfirmed;
state.confirmedBalance = json.confirmed;
}
reduceError(json, state) {
state.error = json;
}
}

View File

@ -24,6 +24,7 @@ class Store {
},
denomination: 'BTC',
showWarning: true,
error: '',
};
this.initialReducer = new InitialReducer();