Skip to content
This repository has been archived by the owner on Jan 6, 2025. It is now read-only.

Commit

Permalink
feat: Parse new "Waiting for deposit" log message
Browse files Browse the repository at this point in the history
  • Loading branch information
binarybaron committed Jun 7, 2024
1 parent 519d4df commit 8958403
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 40 deletions.
5 changes: 4 additions & 1 deletion src/models/cliModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,13 @@ export interface CliLogWaitingForBtcDeposit extends CliLog {
fields: {
message: 'Waiting for Bitcoin deposit';
deposit_address: string;
min_deposit_until_swap_will_start: string;
max_deposit_until_maximum_amount_is_reached: string;
max_giveable: string;
minimum_amount: string;
maximum_amount: string;
min_deposit: string;
min_bitcoin_lock_tx_fee: string;
price: string;
};
}

Expand Down
6 changes: 6 additions & 0 deletions src/models/storeModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ export interface SwapStateWaitingForBtcDeposit extends SwapState {
minimumAmount: number;
maximumAmount: number;
minDeposit: number;
maxDeposit: number;
minBitcoinLockTxFee: number;
price: number | null;
}

Expand All @@ -80,6 +82,10 @@ export function isSwapStateWaitingForBtcDeposit(

export interface SwapStateStarted extends SwapState {
type: SwapStateType.STARTED;
txLockDetails: {
amount: number;
fees: number;
} | null;
}

export function isSwapStateStarted(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
makeStyles,
Typography,
} from '@material-ui/core';
import { ReactNode } from 'react';

const useStyles = makeStyles((theme) => ({
subtitle: {
Expand All @@ -14,7 +15,7 @@ const useStyles = makeStyles((theme) => ({
export default function CircularProgressWithSubtitle({
description,
}: {
description: string;
description: string | ReactNode;
}) {
const classes = useStyles();

Expand Down
2 changes: 1 addition & 1 deletion src/renderer/components/modal/swap/pages/SwapStatePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export default function SwapStatePage({
return <WaitingForBitcoinDepositPage state={swapState} />;
}
if (isSwapStateStarted(swapState)) {
return <StartedPage />;
return <StartedPage state={swapState} />;
}
if (isSwapStateBtcLockInMempool(swapState)) {
return <BitcoinLockTxInMempoolPage state={swapState} />;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import { SwapStateStarted } from 'models/storeModel';
import CircularProgressWithSubtitle from '../../CircularProgressWithSubtitle';
import { BitcoinAmount } from 'renderer/components/other/Units';

export default function StartedPage() {
return <CircularProgressWithSubtitle description="Locking Bitcoin" />;
export default function StartedPage({ state}: { state: SwapStateStarted }) {
const description = state.txLockDetails ? (
<>
Locking <BitcoinAmount amount={state.txLockDetails.amount} /> with a network fee of <BitcoinAmount amount={state.txLockDetails.fees} />
</>
) : (
'Locking Bitcoin'
);

return <CircularProgressWithSubtitle description={description} />;
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,8 @@ function calcBtcAmountWithoutFees(amount: number, fees: number) {

export default function DepositAmountHelper({
state,
btcFees,
}: {
state: SwapStateWaitingForBtcDeposit;
btcFees: number;
}) {
const classes = useStyles();
const [amount, setAmount] = useState(state.minDeposit);
Expand All @@ -56,13 +54,13 @@ export default function DepositAmountHelper({
if (state.price == null) return null;

console.log(
`Calculating calcBtcAmountWithoutFees(${getTotalAmountAfterDeposit()}, ${btcFees}) / ${
`Calculating calcBtcAmountWithoutFees(${getTotalAmountAfterDeposit()}, ${state.minBitcoinLockTxFee}) / ${
state.price
} - ${MONERO_FEE}`
);

return (
calcBtcAmountWithoutFees(getTotalAmountAfterDeposit(), btcFees) /
calcBtcAmountWithoutFees(getTotalAmountAfterDeposit(), state.minBitcoinLockTxFee) /
state.price -
MONERO_FEE
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,6 @@ export default function WaitingForBtcDepositPage({
const classes = useStyles();
const bitcoinBalance = useAppSelector((s) => s.rpc.state.balance) || 0;

// Convert to integer for accurate arithmetic operations
const fees = satsToBtc(
btcToSats(state.minDeposit) -
btcToSats(state.minimumAmount) +
bitcoinBalance
);

// TODO: Account for BTC lock tx fees
return (
<Box>
Expand All @@ -61,7 +54,7 @@ export default function WaitingForBtcDepositPage({
Send any amount between{' '}
<BitcoinAmount amount={state.minDeposit} /> and{' '}
<BitcoinAmount
amount={state.maximumAmount - satsToBtc(bitcoinBalance)}
amount={state.maxDeposit}
/>{' '}
to the address above
{bitcoinBalance > 0 && (
Expand All @@ -75,7 +68,7 @@ export default function WaitingForBtcDepositPage({
</li>
<li>
The network fee of{' '}
<BitcoinAmount amount={fees >= 0 ? fees : null} /> will
<BitcoinAmount amount={state.minBitcoinLockTxFee} /> will
automatically be deducted from the deposited coins
</li>
<li>
Expand All @@ -84,7 +77,7 @@ export default function WaitingForBtcDepositPage({
</li>
</ul>
</Typography>
<DepositAmountHelper btcFees={fees} state={state} />
<DepositAmountHelper btcFees={state.minBitcoinLockTxFee} state={state} />
</Box>
}
icon={<BitcoinIcon />}
Expand Down
48 changes: 27 additions & 21 deletions src/store/features/swapSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,25 +101,32 @@ export const swapSlice = createSlice({
log.fields.max_giveable
);
const minDeposit = extractAmountFromUnitString(
log.fields.min_deposit
log.fields.min_deposit_until_swap_will_start
);
const maxDeposit = extractAmountFromUnitString(
log.fields.max_deposit_until_maximum_amount_is_reached
);
const minimumAmount = extractAmountFromUnitString(
log.fields.minimum_amount
);
const maximumAmount = extractAmountFromUnitString(
log.fields.maximum_amount
);
const minBitcoinLockTxFee = extractAmountFromUnitString(
log.fields.min_bitcoin_lock_tx_fee
);
const price = extractAmountFromUnitString(log.fields.price);

const depositAddress = log.fields.deposit_address;
const price = isSwapStateReceivedQuote(slice.state)
? slice.state.price
: null;

if (
maxGiveable != null &&
minimumAmount != null &&
maximumAmount != null &&
minDeposit != null
minDeposit != null &&
maxDeposit != null &&
minBitcoinLockTxFee != null &&
price != null
) {
const nextState: SwapStateWaitingForBtcDeposit = {
type: SwapStateType.WAITING_FOR_BTC_DEPOSIT,
Expand All @@ -128,34 +135,33 @@ export const swapSlice = createSlice({
minimumAmount,
maximumAmount,
minDeposit,
maxDeposit,
price,
minBitcoinLockTxFee,
};

slice.state = nextState;
}
} else if (isCliLogReceivedBtc(log)) {
const maxGiveable = extractAmountFromUnitString(
log.fields.max_giveable
);

if (
isSwapStateWaitingForBtcDeposit(slice.state) &&
maxGiveable != null
) {
slice.state.maxGiveable = maxGiveable;
}
} else if (isCliLogDeterminedSwapAmount(log)) {
const nextState: SwapStateStarted = {
type: SwapStateType.STARTED,
};
const amount = extractAmountFromUnitString(log.fields.amount);
const fees = extractAmountFromUnitString(log.fields.fees);

slice.state = nextState;
} else if (isCliLogStartedSwap(log)) {
const nextState: SwapStateStarted = {
type: SwapStateType.STARTED,
txLockDetails: (amount != null && fees != null) ? { amount, fees } : null,
};

slice.state = nextState;
} else if (isCliLogStartedSwap(log)) {
if(slice.state?.type !== SwapStateType.STARTED) {
const nextState: SwapStateStarted = {
type: SwapStateType.STARTED,
txLockDetails: null,
};

slice.state = nextState;
}

slice.swapId = log.fields.swap_id;
} else if (isCliLogPublishedBtcTx(log)) {
if (log.fields.kind === 'lock') {
Expand Down

0 comments on commit 8958403

Please sign in to comment.