Skip to content

Commit

Permalink
\test: add shouldTrigger success case
Browse files Browse the repository at this point in the history
  • Loading branch information
lissavxo committed Aug 7, 2024
1 parent dcf5027 commit 4516f81
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 44 deletions.
94 changes: 56 additions & 38 deletions react/lib/components/Widget/WidgetContainer.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { OptionsObject, SnackbarProvider, useSnackbar } from 'notistack';
import React, { useCallback, useEffect, useMemo, useState } from 'react';
import { AltpaymentShift, getAltpaymentClient } from '../../altpayment';

import successSound from '../../assets/success.mp3.json';

Expand All @@ -11,7 +12,6 @@ import {
generatePaymentId,
getCurrencyTypeFromAddress,
isCrypto,
isFiat,
isGreaterThanZero,
isValidCurrency,
resolveNumber,
Expand All @@ -21,7 +21,7 @@ import {
import Widget, { WidgetProps } from './Widget';

export interface WidgetContainerProps
extends Omit<WidgetProps, 'success' | 'setNewTxs' | 'setCurrencyObject'> {
extends Omit<WidgetProps, 'success' | 'setNewTxs' | 'setCurrencyObject' | 'setAltpaymentShift' | 'useAltpayment' | 'setUseAltpayment' | 'shiftCompleted' | 'setShiftCompleted' > {
active?: boolean;
amount?: number;
opReturn?: string;
Expand Down Expand Up @@ -106,6 +106,7 @@ export const WidgetContainer: React.FunctionComponent<WidgetContainerProps> =

const [thisPaymentId, setThisPaymentId] = useState<string | undefined>();
const [thisPrice, setThisPrice] = useState(0);
const [usdPrice, setUsdPrice] = useState(0);
useEffect(() => {
if ((paymentId === undefined || paymentId === '') && !disablePaymentId) {
const newPaymentId = generatePaymentId(8);
Expand All @@ -118,6 +119,12 @@ export const WidgetContainer: React.FunctionComponent<WidgetContainerProps> =
const { enqueueSnackbar } = useSnackbar();

const [newTxs, setNewTxs] = useState<Transaction[] | undefined>();
const [useAltpayment, setUseAltpayment] = useState(false);
const [altpaymentShift, setAltpaymentShift] = useState<AltpaymentShift | undefined>();
const [shiftCompleted, setShiftCompleted] = useState(false);

const paymentClient = getAltpaymentClient()

const addrType = getCurrencyTypeFromAddress(to);
if (
!isValidCurrency(currency) ||
Expand All @@ -133,35 +140,43 @@ export const WidgetContainer: React.FunctionComponent<WidgetContainerProps> =

const handlePayment = useCallback(
async (transaction: Transaction) => {
const expectedAmount = amount ? resolveNumber(amount) : undefined;
const receivedAmount = resolveNumber(transaction.amount);
if (shouldTriggerOnSuccess(
transaction,
thisPrice,
currency,
paymentId,
expectedAmount,
opReturn,
)) {
if (sound) {
txSound.play().catch(() => {});
if (altpaymentShift) {
const shiftStatus = await paymentClient.getPaymentStatus(altpaymentShift.id)
if (shiftStatus.status === 'settled') {
if (sound) txSound.play().catch(() => {});
onSuccess?.(transaction);
setShiftCompleted(true)
}

const currencyTicker = getCurrencyTypeFromAddress(to);
if (!hideToasts)
enqueueSnackbar(
`${
successText ? successText + ' | ' : ''
}Received ${receivedAmount} ${currencyTicker}`,
snackbarOptions,
);

setSuccess(true);
onSuccess?.(transaction);
} else {
onTransaction?.(transaction);
}
const expectedAmount = amount ? resolveNumber(amount) : undefined;
const receivedAmount = resolveNumber(transaction.amount);
if (shouldTriggerOnSuccess(
transaction,
thisPrice,
currency,
paymentId,
expectedAmount,
opReturn,
)) {
if (sound) {
txSound.play().catch(() => {});
}

const currencyTicker = getCurrencyTypeFromAddress(to);
if (!hideToasts)
enqueueSnackbar(
`${
successText ? successText + ' | ' : ''
}Received ${receivedAmount} ${currencyTicker}`,
snackbarOptions,
);

setSuccess(true);
onSuccess?.(transaction);
} else {
onTransaction?.(transaction);
}
}
setNewTxs([]);
},
[
Expand All @@ -171,35 +186,31 @@ export const WidgetContainer: React.FunctionComponent<WidgetContainerProps> =
hideToasts,
sound,
txSound,
amount,
cryptoAmount,
successText,
to,
thisPaymentId,
cryptoAmount,
altpaymentShift
],
);

const getPrice = useCallback(
async () => {
const price = await getFiatPrice(currency, to, apiBaseUrl)
const usdPrice = await getFiatPrice('USD', to, apiBaseUrl)
if (price !== null) setThisPrice(price)
if (usdPrice !== null) setUsdPrice(usdPrice)
}
, [currency, to, apiBaseUrl]
);

useEffect(() => {
if (price === undefined) {
getPrice();
} else {
setThisPrice(price)
}
}, [currency, price]);

useEffect(() => {
if (isFiat(currency) && price === undefined) {
(async () => {
getPrice();
})()
} else {
setThisPrice(price)
}
}, [currency, price]);

Expand Down Expand Up @@ -238,6 +249,7 @@ export const WidgetContainer: React.FunctionComponent<WidgetContainerProps> =
setCurrencyObject={setCurrencyObj}
randomSatoshis={randomSatoshis}
price={thisPrice}
usdPrice={usdPrice}
success={success}
disabled={disabled}
editable={editable}
Expand All @@ -247,6 +259,12 @@ export const WidgetContainer: React.FunctionComponent<WidgetContainerProps> =
apiBaseUrl={apiBaseUrl}
successText={successText}
hoverText={hoverText}
altpaymentShift={altpaymentShift}
setAltpaymentShift={setAltpaymentShift}
useAltpayment={useAltpayment}
setUseAltpayment={setUseAltpayment}
shiftCompleted={shiftCompleted}
setShiftCompleted={setShiftCompleted}
/>
</React.Fragment>
);
Expand Down
31 changes: 30 additions & 1 deletion react/lib/tests/util/api-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,5 +289,34 @@ describe('API Client Util Tests', () => {
),
).toBe(true);
});

it('true when amount, opReturn, and paymentId match the ones received in transaction, with currency USD', () => {
const transaction: Transaction = {
amount: '32851.51',
paymentId: '123',
message: 'test message',
rawMessage: 'test message',
hash: '',
timestamp: 0,
address: 'ecash:qrmm7edwuj4jf7tnvygjyztyy0a0qxvl7quss2vxek',
};
const expectedAmount = resolveNumber(1);
const expectedOpReturn = 'test message';
const expectedPaymentId = '123';
const price = 0.00003044;
const currency = 'USD';

expect(
shouldTriggerOnSuccess(
transaction,
price,
currency,
expectedPaymentId,
expectedAmount,
expectedOpReturn,
),
).toBe(true);

});
});
});
})
7 changes: 2 additions & 5 deletions react/lib/util/api-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ export const shouldTriggerOnSuccess = (
message,
amount,
address } = transaction;

let isAmountValid = true;

if(expectedAmount) {
const transactionCurrency: Currency = getCurrencyTypeFromAddress(address);
if ((transactionCurrency !== currency)) {
Expand All @@ -42,14 +40,14 @@ export const shouldTriggerOnSuccess = (
isAmountValid = expectedAmount.isEqualTo(amount)
}
}

const paymentIdsMatch = expectedPaymentId === paymentId;
const isPaymentIdValid = expectedPaymentId ? paymentIdsMatch : true;

const rawOpReturnIsEmptyOrUndefined = rawOpReturn === '' || rawOpReturn === undefined;
const opReturn = rawOpReturnIsEmptyOrUndefined ? message : rawOpReturn
const opReturnIsEmptyOrUndefined = opReturn === '' || opReturn === undefined;

const opReturnsMatch = opReturn === expectedOpReturn;
const isOpReturnValid = expectedOpReturn ? opReturnsMatch : opReturnIsEmptyOrUndefined;

Expand Down Expand Up @@ -117,7 +115,6 @@ export const getFiatPrice = async (currency: string, to: string, apiBaseUrl?: st
return data.price;
} else if (isFiat(currency) && isValidXecAddress(to)) {
const data = await getXecFiatPrice(currency, apiBaseUrl);
console.log("aqui")
return data.price;
}
return null
Expand Down

0 comments on commit 4516f81

Please sign in to comment.