Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions react/lib/components/Widget/WidgetContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
isValidCurrency,
resolveNumber,
shouldTriggerOnSuccess,
getCurrencyObject,
isPropsTrue
} from '../../util';

Expand Down Expand Up @@ -88,8 +87,6 @@ export const WidgetContainer: React.FunctionComponent<WidgetContainerProps> =
paymentId,
amount,
setAmount,
setCurrencyObj,
currencyObj,
currency = '' as Currency,
cryptoAmount,
price,
Expand All @@ -111,6 +108,9 @@ export const WidgetContainer: React.FunctionComponent<WidgetContainerProps> =
disableSound,
...widgetProps
} = props;
const [internalCurrencyObj, setInternalCurrencyObj] = useState<CurrencyObject>();
const setCurrencyObj = props.setCurrencyObj || setInternalCurrencyObj;
const currencyObj = props.currencyObj || internalCurrencyObj;

const [thisPaymentId, setThisPaymentId] = useState<string | undefined>();
const [thisPrice, setThisPrice] = useState(0);
Expand Down Expand Up @@ -156,22 +156,19 @@ export const WidgetContainer: React.FunctionComponent<WidgetContainerProps> =
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(() => {});
Expand Down Expand Up @@ -210,6 +207,7 @@ export const WidgetContainer: React.FunctionComponent<WidgetContainerProps> =
altpaymentShift,
thisPrice,
currencyObj,
randomSatoshis
],
);

Expand Down
48 changes: 48 additions & 0 deletions react/lib/tests/util/validate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ describe('Validate Util Tests', () => {
currency,
price,
false,
false,
expectedPaymentId,
expectedAmount,
expectedOpReturn
Expand Down Expand Up @@ -58,6 +59,7 @@ describe('Validate Util Tests', () => {
transaction,
currency,
price,
false,
true,
expectedPaymentId,
expectedAmount,
Expand Down Expand Up @@ -88,6 +90,7 @@ describe('Validate Util Tests', () => {
currency,
price,
false,
false,
expectedPaymentId,
expectedAmount,
expectedOpReturn
Expand Down Expand Up @@ -117,6 +120,7 @@ describe('Validate Util Tests', () => {
currency,
price,
false,
false,
expectedPaymentId,
expectedAmount,
expectedOpReturn
Expand Down Expand Up @@ -146,6 +150,7 @@ describe('Validate Util Tests', () => {
currency,
price,
false,
false,
expectedPaymentId,
expectedAmount,
expectedOpReturn
Expand Down Expand Up @@ -175,6 +180,7 @@ describe('Validate Util Tests', () => {
currency,
price,
false,
false,
expectedPaymentId,
expectedAmount,
expectedOpReturn
Expand Down Expand Up @@ -204,6 +210,7 @@ describe('Validate Util Tests', () => {
currency,
price,
false,
false,
expectedPaymentId,
expectedAmount,
expectedOpReturn
Expand Down Expand Up @@ -232,6 +239,7 @@ describe('Validate Util Tests', () => {
transaction,
currency,
price,
false,
true,
expectedPaymentId,
expectedAmount,
Expand Down Expand Up @@ -262,6 +270,7 @@ describe('Validate Util Tests', () => {
currency,
price,
false,
false,
expectedPaymentId,
expectedAmount,
expectedOpReturn
Expand Down Expand Up @@ -294,6 +303,7 @@ describe('Validate Util Tests', () => {
currency,
price,
false,
false,
expectedPaymentId,
expectedAmount,
expectedOpReturn
Expand Down Expand Up @@ -323,6 +333,7 @@ describe('Validate Util Tests', () => {
currency,
price,
false,
false,
expectedPaymentId,
expectedAmount,
expectedOpReturn
Expand Down Expand Up @@ -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,
Expand Down
30 changes: 19 additions & 11 deletions react/lib/util/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ export const shouldTriggerOnSuccess = async (
transaction: Transaction,
currency: string,
price: number,
randomSatoshis: number | boolean,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why 'number'? AFAIK this is a boolean.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check WidgetContainerProps randomSatoshis is boolean | number there

disablePaymentId?: boolean,
expectedPaymentId?: string,
expectedAmount?: BigNumber,
expectedAmount?: BigNumber | number,
expectedOpReturn?: string,
currencyObject?: CurrencyObject,
) => {
Expand All @@ -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){
Expand All @@ -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;
};