diff --git a/react/lib/components/Widget/WidgetContainer.tsx b/react/lib/components/Widget/WidgetContainer.tsx index 96790787..73d667c9 100644 --- a/react/lib/components/Widget/WidgetContainer.tsx +++ b/react/lib/components/Widget/WidgetContainer.tsx @@ -16,7 +16,6 @@ import { isValidCurrency, resolveNumber, shouldTriggerOnSuccess, - getCurrencyObject, isPropsTrue } from '../../util'; @@ -88,8 +87,6 @@ export const WidgetContainer: React.FunctionComponent = paymentId, amount, setAmount, - setCurrencyObj, - currencyObj, currency = '' as Currency, cryptoAmount, price, @@ -111,6 +108,9 @@ export const WidgetContainer: React.FunctionComponent = disableSound, ...widgetProps } = props; + const [internalCurrencyObj, setInternalCurrencyObj] = useState(); + const setCurrencyObj = props.setCurrencyObj || setInternalCurrencyObj; + const currencyObj = props.currencyObj || internalCurrencyObj; const [thisPaymentId, setThisPaymentId] = useState(); const [thisPrice, setThisPrice] = useState(0); @@ -156,22 +156,19 @@ export const WidgetContainer: React.FunctionComponent = setShiftCompleted(true) } } else { - const expectedAmount = amount ? resolveNumber(amount) : undefined; + const expectedAmount = currencyObj ? currencyObj?.float : undefined const receivedAmount = resolveNumber(transaction.amount); if (await shouldTriggerOnSuccess( transaction, currency, thisPrice, + randomSatoshis, disablePaymentId, thisPaymentId, expectedAmount, opReturn, - currencyObj ?? getCurrencyObject( - Number(props.amount), - currency, - randomSatoshis, - ), + currencyObj )) { if (sound && !isPropsTrue(disableSound)) { txSound.play().catch(() => {}); @@ -210,6 +207,7 @@ export const WidgetContainer: React.FunctionComponent = altpaymentShift, thisPrice, currencyObj, + randomSatoshis ], ); diff --git a/react/lib/tests/util/validate.test.ts b/react/lib/tests/util/validate.test.ts index c7e0ea1a..e97452cb 100644 --- a/react/lib/tests/util/validate.test.ts +++ b/react/lib/tests/util/validate.test.ts @@ -30,6 +30,7 @@ describe('Validate Util Tests', () => { currency, price, false, + false, expectedPaymentId, expectedAmount, expectedOpReturn @@ -58,6 +59,7 @@ describe('Validate Util Tests', () => { transaction, currency, price, + false, true, expectedPaymentId, expectedAmount, @@ -88,6 +90,7 @@ describe('Validate Util Tests', () => { currency, price, false, + false, expectedPaymentId, expectedAmount, expectedOpReturn @@ -117,6 +120,7 @@ describe('Validate Util Tests', () => { currency, price, false, + false, expectedPaymentId, expectedAmount, expectedOpReturn @@ -146,6 +150,7 @@ describe('Validate Util Tests', () => { currency, price, false, + false, expectedPaymentId, expectedAmount, expectedOpReturn @@ -175,6 +180,7 @@ describe('Validate Util Tests', () => { currency, price, false, + false, expectedPaymentId, expectedAmount, expectedOpReturn @@ -204,6 +210,7 @@ describe('Validate Util Tests', () => { currency, price, false, + false, expectedPaymentId, expectedAmount, expectedOpReturn @@ -232,6 +239,7 @@ describe('Validate Util Tests', () => { transaction, currency, price, + false, true, expectedPaymentId, expectedAmount, @@ -262,6 +270,7 @@ describe('Validate Util Tests', () => { currency, price, false, + false, expectedPaymentId, expectedAmount, expectedOpReturn @@ -294,6 +303,7 @@ describe('Validate Util Tests', () => { currency, price, false, + false, expectedPaymentId, expectedAmount, expectedOpReturn @@ -323,6 +333,7 @@ describe('Validate Util Tests', () => { currency, price, false, + false, expectedPaymentId, expectedAmount, expectedOpReturn @@ -357,6 +368,43 @@ describe('Validate Util Tests', () => { currency, price, false, + false, + expectedPaymentId, + expectedAmount, + expectedOpReturn, + currencyObject + ); + + expect(result).toBe(true); + }); + + it('true when randomSatoshis is true and paymentId does not match', async () => { + const transaction: Transaction = { + amount: '3152585.12', + paymentId: '1234', + message: 'test opReturn', + rawMessage: 'test opReturn', + hash: '', + timestamp: 0, + address: 'ecash:qrmm7edwuj4jf7tnvygjyztyy0a0qxvl7quss2vxek', + }; + const expectedPaymentId = '123'; + const expectedAmount = resolveNumber('101.00'); + const expectedOpReturn = 'test opReturn'; + const price = 0.00003172; + const currency = 'USD'; + const currencyObject: CurrencyObject = { + currency: 'USD', + string: '$100', + float: 100, + }; + + const result = await shouldTriggerOnSuccess( + transaction, + currency, + price, + true, + false, expectedPaymentId, expectedAmount, expectedOpReturn, diff --git a/react/lib/util/validate.ts b/react/lib/util/validate.ts index cf73fff8..221c3e53 100644 --- a/react/lib/util/validate.ts +++ b/react/lib/util/validate.ts @@ -8,9 +8,10 @@ export const shouldTriggerOnSuccess = async ( transaction: Transaction, currency: string, price: number, + randomSatoshis: number | boolean, disablePaymentId?: boolean, expectedPaymentId?: string, - expectedAmount?: BigNumber, + expectedAmount?: BigNumber | number, expectedOpReturn?: string, currencyObject?: CurrencyObject, ) => { @@ -24,6 +25,9 @@ export const shouldTriggerOnSuccess = async ( let isAmountValid = true; if(expectedAmount) { + if (typeof expectedAmount === 'number'){ + expectedAmount = new BigNumber(expectedAmount); + } const transactionCurrency: Currency = getCurrencyTypeFromAddress(address); if (transactionCurrency !== currency) { if (currencyObject){ @@ -33,19 +37,23 @@ export const shouldTriggerOnSuccess = async ( isAmountValid = false } } else { - isAmountValid = expectedAmount.isEqualTo(amount) + isAmountValid = expectedAmount.isEqualTo(amount); } - } - - const paymentIdsMatch = expectedPaymentId === paymentId; - const isPaymentIdValid = disablePaymentId ? true : paymentIdsMatch; + } + let isPaymentIdValid = true + let isOpReturnValid = true - const rawOpReturnIsEmptyOrUndefined = rawOpReturn === '' || rawOpReturn === undefined; - const opReturn = rawOpReturnIsEmptyOrUndefined ? message : rawOpReturn - const opReturnIsEmptyOrUndefined = opReturn === '' || opReturn === undefined; + if(!randomSatoshis || randomSatoshis === 0){ + const paymentIdsMatch = expectedPaymentId === paymentId; + isPaymentIdValid = disablePaymentId ? true : paymentIdsMatch; - const opReturnsMatch = opReturn === expectedOpReturn; - const isOpReturnValid = expectedOpReturn ? opReturnsMatch : opReturnIsEmptyOrUndefined; + const rawOpReturnIsEmptyOrUndefined = rawOpReturn === '' || rawOpReturn === undefined; + const opReturn = rawOpReturnIsEmptyOrUndefined ? message : rawOpReturn + const opReturnIsEmptyOrUndefined = opReturn === '' || opReturn === undefined; + const opReturnsMatch = opReturn === expectedOpReturn; + isOpReturnValid = expectedOpReturn ? opReturnsMatch : opReturnIsEmptyOrUndefined; + } + return isAmountValid && isPaymentIdValid && isOpReturnValid; }; \ No newline at end of file