Skip to content

Commit

Permalink
[FIX] rate calculation when creating using crypto amount instead of usd
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielbazan7 committed Sep 28, 2023
1 parent 9b58617 commit edce616
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
1 change: 1 addition & 0 deletions src/store/shop/shop.models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ export interface Invoice {
threshold: number;
};
};
usdAmount: number;
}

export interface PhoneCountryInfo {
Expand Down
44 changes: 33 additions & 11 deletions src/store/wallet/effects/send/send.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,15 +354,31 @@ export const getNonce = (
};

export const getInvoiceEffectiveRate =
(invoice: Invoice, coin: string, chain: string): Effect<number | undefined> =>
(
invoice: Invoice,
defaultAltCurrencyIsoCode: string,
coin: string,
chain: string,
): Effect<number | undefined> =>
dispatch => {
const precision = dispatch(GetPrecision(coin, chain));
if (!precision) {
return undefined;
}

const invoiceCurrency = getCurrencyCodeFromCoinAndChain(coin, chain);
return (
precision &&
invoice.price /
(invoice.paymentSubtotals[invoiceCurrency] / precision.unitToSatoshi)
);
const toUnit =
invoice.paymentSubtotals[invoiceCurrency] / precision.unitToSatoshi;

if (defaultAltCurrencyIsoCode === 'USD') {
return invoice.usdAmount / toUnit;
}

if (defaultAltCurrencyIsoCode === invoice.currency) {
return invoice.price / toUnit;
}

return undefined;
};

/*
Expand Down Expand Up @@ -445,11 +461,17 @@ export const buildTxDetails =
}

amount = Number(amount); // Support BN (use number instead string only for view)
const effectiveRate =
(invoice &&
invoiceCurrency &&
dispatch(getInvoiceEffectiveRate(invoice, invoiceCurrency, chain))) ||
undefined;
let effectiveRate;
if (invoice && invoiceCurrency) {
effectiveRate = dispatch(
getInvoiceEffectiveRate(
invoice,
defaultAltCurrencyIsoCode,
invoiceCurrency,
chain,
),
);
}
const opts = {
effectiveRate,
defaultAltCurrencyIsoCode,
Expand Down

0 comments on commit edce616

Please sign in to comment.