From a0fed904ddeab0fe8c76c2a3af2b45400522ddfe Mon Sep 17 00:00:00 2001 From: lissavxo Date: Thu, 10 Apr 2025 16:16:27 -0300 Subject: [PATCH 1/3] fix: randomSatochis trigger success --- react/lib/components/Widget/WidgetContainer.tsx | 14 +++++--------- react/lib/util/validate.ts | 7 +++++-- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/react/lib/components/Widget/WidgetContainer.tsx b/react/lib/components/Widget/WidgetContainer.tsx index 96790787..b2bb082d 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,7 +156,7 @@ 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( @@ -167,11 +167,7 @@ export const WidgetContainer: React.FunctionComponent = thisPaymentId, expectedAmount, opReturn, - currencyObj ?? getCurrencyObject( - Number(props.amount), - currency, - randomSatoshis, - ), + currencyObj )) { if (sound && !isPropsTrue(disableSound)) { txSound.play().catch(() => {}); diff --git a/react/lib/util/validate.ts b/react/lib/util/validate.ts index cf73fff8..c4016d05 100644 --- a/react/lib/util/validate.ts +++ b/react/lib/util/validate.ts @@ -10,7 +10,7 @@ export const shouldTriggerOnSuccess = async ( price: number, disablePaymentId?: boolean, expectedPaymentId?: string, - expectedAmount?: BigNumber, + expectedAmount?: BigNumber | number, expectedOpReturn?: string, currencyObject?: CurrencyObject, ) => { @@ -24,6 +24,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,7 +36,7 @@ export const shouldTriggerOnSuccess = async ( isAmountValid = false } } else { - isAmountValid = expectedAmount.isEqualTo(amount) + isAmountValid = expectedAmount.isEqualTo(amount); } } From d669358c064ed150f5987bf47ae4cd00ef4251ae Mon Sep 17 00:00:00 2001 From: lissavxo Date: Wed, 23 Apr 2025 11:03:23 -0300 Subject: [PATCH 2/3] feat: ignore validation op-return paymentID when randomsatoshis --- .../lib/components/Widget/WidgetContainer.tsx | 6 +++-- react/lib/util/validate.ts | 23 +++++++++++-------- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/react/lib/components/Widget/WidgetContainer.tsx b/react/lib/components/Widget/WidgetContainer.tsx index b2bb082d..73d667c9 100644 --- a/react/lib/components/Widget/WidgetContainer.tsx +++ b/react/lib/components/Widget/WidgetContainer.tsx @@ -108,8 +108,8 @@ export const WidgetContainer: React.FunctionComponent = disableSound, ...widgetProps } = props; - const [internalCurrencyObj, _setInternalCurrencyObj] = useState(); - const setCurrencyObj = props.setCurrencyObj || _setInternalCurrencyObj; + const [internalCurrencyObj, setInternalCurrencyObj] = useState(); + const setCurrencyObj = props.setCurrencyObj || setInternalCurrencyObj; const currencyObj = props.currencyObj || internalCurrencyObj; const [thisPaymentId, setThisPaymentId] = useState(); @@ -163,6 +163,7 @@ export const WidgetContainer: React.FunctionComponent = transaction, currency, thisPrice, + randomSatoshis, disablePaymentId, thisPaymentId, expectedAmount, @@ -206,6 +207,7 @@ export const WidgetContainer: React.FunctionComponent = altpaymentShift, thisPrice, currencyObj, + randomSatoshis ], ); diff --git a/react/lib/util/validate.ts b/react/lib/util/validate.ts index c4016d05..221c3e53 100644 --- a/react/lib/util/validate.ts +++ b/react/lib/util/validate.ts @@ -8,6 +8,7 @@ export const shouldTriggerOnSuccess = async ( transaction: Transaction, currency: string, price: number, + randomSatoshis: number | boolean, disablePaymentId?: boolean, expectedPaymentId?: string, expectedAmount?: BigNumber | number, @@ -38,17 +39,21 @@ export const shouldTriggerOnSuccess = async ( } else { 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 From fb51c8a1b8b82ea18b3cb26ca9d2eafff2bac6e5 Mon Sep 17 00:00:00 2001 From: lissavxo Date: Wed, 23 Apr 2025 11:21:57 -0300 Subject: [PATCH 3/3] test: shouldTriggerOnSuccess add case --- react/lib/tests/util/validate.test.ts | 48 +++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) 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,